1 //===- llvm/Support/PassNameParser.h ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file the PassNameParser and FilteredPassNameParser<> classes, which are
11 // used to add command line arguments to a utility for all of the passes that
12 // have been registered into the system.
14 // The PassNameParser class adds ALL passes linked into the system (that are
15 // creatable) as command line arguments to the tool (when instantiated with the
16 // appropriate command line option template). The FilteredPassNameParser<>
17 // template is used for the same purposes as PassNameParser, except that it only
18 // includes passes that have a PassType that are compatible with the filter
19 // (which is the template argument).
21 //===----------------------------------------------------------------------===//
23 #ifndef LLVM_SUPPORT_PASS_NAME_PARSER_H
24 #define LLVM_SUPPORT_PASS_NAME_PARSER_H
26 #include "llvm/Pass.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/raw_ostream.h"
35 //===----------------------------------------------------------------------===//
36 // PassNameParser class - Make use of the pass registration mechanism to
37 // automatically add a command line argument to opt for each pass.
39 class PassNameParser : public PassRegistrationListener,
40 public cl::parser<const PassInfo*> {
43 PassNameParser() : Opt(0) {}
44 virtual ~PassNameParser();
46 void initialize(cl::Option &O) {
48 cl::parser<const PassInfo*>::initialize(O);
50 // Add all of the passes to the map that got initialized before 'this' did.
54 // ignorablePassImpl - Can be overriden in subclasses to refine the list of
55 // which passes we want to include.
57 virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
59 inline bool ignorablePass(const PassInfo *P) const {
60 // Ignore non-selectable and non-constructible passes! Ignore
62 return P->getPassArgument() == 0 || *P->getPassArgument() == 0 ||
63 P->getNormalCtor() == 0 || ignorablePassImpl(P);
66 // Implement the PassRegistrationListener callbacks used to populate our map
68 virtual void passRegistered(const PassInfo *P) {
69 if (ignorablePass(P) || !Opt) return;
70 if (findOption(P->getPassArgument()) != getNumOptions()) {
71 errs() << "Two passes with the same argument (-"
72 << P->getPassArgument() << ") attempted to be registered!\n";
75 addLiteralOption(P->getPassArgument(), P, P->getPassName());
77 virtual void passEnumerate(const PassInfo *P) { passRegistered(P); }
79 // printOptionInfo - Print out information about this option. Override the
80 // default implementation to sort the table before we print...
81 virtual void printOptionInfo(const cl::Option &O, size_t GlobalWidth) const {
82 PassNameParser *PNP = const_cast<PassNameParser*>(this);
83 array_pod_sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
84 cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
88 // ValLessThan - Provide a sorting comparator for Values elements...
89 static int ValLessThan(const void *VT1, const void *VT2) {
90 typedef PassNameParser::OptionInfo ValType;
91 return std::strcmp(static_cast<const ValType *>(VT1)->Name,
92 static_cast<const ValType *>(VT2)->Name);
96 ///===----------------------------------------------------------------------===//
97 /// FilteredPassNameParser class - Make use of the pass registration
98 /// mechanism to automatically add a command line argument to opt for
99 /// each pass that satisfies a filter criteria. Filter should return
100 /// true for passes to be registered as command-line options.
102 template<typename Filter>
103 class FilteredPassNameParser : public PassNameParser {
108 bool ignorablePassImpl(const PassInfo *P) const { return !filter(*P); }
111 ///===----------------------------------------------------------------------===//
112 /// PassArgFilter - A filter for use with PassNameFilterParser that only
113 /// accepts a Pass whose Arg matches certain strings.
117 /// extern const char AllowedPassArgs[] = "-anders_aa -dse";
122 /// FilteredPassNameParser<PassArgFilter<AllowedPassArgs> > >
123 /// PassList(cl::desc("Passes available:"));
125 /// Only the -anders_aa and -dse options will be available to the user.
127 template<const char *Args>
128 class PassArgFilter {
130 bool operator()(const PassInfo &P) const {
131 return(std::strstr(Args, P.getPassArgument()));
135 } // End llvm namespace