Counter Strike : Global Offensive Source Code

137 lines
4.9 KiB

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