Make some methods const. The only interesting change here is that
[oota-llvm.git] / include / llvm / Support / PassNameParser.h
1 //===- llvm/Support/PassNameParser.h ----------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
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).
20 //
21 //===----------------------------------------------------------------------===//
22
23 #ifndef LLVM_SUPPORT_PASS_NAME_PARSER_H
24 #define LLVM_SUPPORT_PASS_NAME_PARSER_H
25
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Pass.h"
30 #include <algorithm>
31 #include <cstring>
32
33 namespace llvm {
34
35 //===----------------------------------------------------------------------===//
36 // PassNameParser class - Make use of the pass registration mechanism to
37 // automatically add a command line argument to opt for each pass.
38 //
39 class PassNameParser : public PassRegistrationListener,
40                        public cl::parser<const PassInfo*> {
41   cl::Option *Opt;
42 public:
43   PassNameParser() : Opt(0) {}
44
45   void initialize(cl::Option &O) {
46     Opt = &O;
47     cl::parser<const PassInfo*>::initialize(O);
48
49     // Add all of the passes to the map that got initialized before 'this' did.
50     enumeratePasses();
51   }
52
53   // ignorablePassImpl - Can be overriden in subclasses to refine the list of
54   // which passes we want to include.
55   //
56   virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
57
58   inline bool ignorablePass(const PassInfo *P) const {
59     // Ignore non-selectable and non-constructible passes!  Ignore
60     // non-optimizations.
61     return P->getPassArgument() == 0 || *P->getPassArgument() == 0 ||
62            P->getNormalCtor() == 0 || ignorablePassImpl(P);
63   }
64
65   // Implement the PassRegistrationListener callbacks used to populate our map
66   //
67   virtual void passRegistered(const PassInfo *P) {
68     if (ignorablePass(P) || !Opt) return;
69     if (findOption(P->getPassArgument()) != getNumOptions()) {
70       errs() << "Two passes with the same argument (-"
71            << P->getPassArgument() << ") attempted to be registered!\n";
72       llvm_unreachable(0);
73     }
74     addLiteralOption(P->getPassArgument(), P, P->getPassName());
75   }
76   virtual void passEnumerate(const PassInfo *P) { passRegistered(P); }
77
78   // ValLessThan - Provide a sorting comparator for Values elements...
79   typedef std::pair<const char*,
80                     std::pair<const PassInfo*, const char*> > ValType;
81   static bool ValLessThan(const ValType &VT1, const ValType &VT2) {
82     return std::string(VT1.first) < std::string(VT2.first);
83   }
84
85   // printOptionInfo - Print out information about this option.  Override the
86   // default implementation to sort the table before we print...
87   virtual void printOptionInfo(const cl::Option &O, size_t GlobalWidth) const {
88     PassNameParser *PNP = const_cast<PassNameParser*>(this);
89     std::sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
90     cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
91   }
92 };
93
94 ///===----------------------------------------------------------------------===//
95 /// FilteredPassNameParser class - Make use of the pass registration
96 /// mechanism to automatically add a command line argument to opt for
97 /// each pass that satisfies a filter criteria.  Filter should return
98 /// true for passes to be registered as command-line options.
99 ///
100 template<typename Filter>
101 class FilteredPassNameParser : public PassNameParser {
102 private:
103   Filter filter;
104
105 public:
106   bool ignorablePassImpl(const PassInfo *P) const { return !filter(*P); }
107 };
108
109 ///===----------------------------------------------------------------------===//
110 /// PassArgFilter - A filter for use with PassNameFilterParser that only
111 /// accepts a Pass whose Arg matches certain strings.
112 ///
113 /// Use like this:
114 ///
115 /// extern const char AllowedPassArgs[] = "-anders_aa -dse";
116 ///
117 /// static cl::list<
118 ///   const PassInfo*,
119 ///   bool,
120 ///   FilteredPassNameParser<PassArgFilter<AllowedPassArgs> > >
121 /// PassList(cl::desc("Passes available:"));
122 ///
123 /// Only the -anders_aa and -dse options will be available to the user.
124 ///
125 template<const char *Args>
126 class PassArgFilter {
127 public:
128   bool operator()(const PassInfo &P) const {
129     return(std::strstr(Args, P.getPassArgument()));
130   }
131 };
132
133 } // End llvm namespace
134
135 #endif