Options.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //===- llvm/Support/Options.h - Debug options support -----------*- 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. /// \file
  10. /// This file declares helper objects for defining debug options that can be
  11. /// configured via the command line. The new API currently builds on the cl::opt
  12. /// API, but does not require the use of static globals.
  13. ///
  14. /// With this API options are registered during initialization. For passes, this
  15. /// happens during pass initialization. Passes with options will call a static
  16. /// registerOptions method during initialization that registers options with the
  17. /// OptionRegistry. An example implementation of registerOptions is:
  18. ///
  19. /// static void registerOptions() {
  20. /// OptionRegistry::registerOption<bool, Scalarizer,
  21. /// &Scalarizer::ScalarizeLoadStore>(
  22. /// "scalarize-load-store",
  23. /// "Allow the scalarizer pass to scalarize loads and store", false);
  24. /// }
  25. ///
  26. /// When reading data for options the interface is via the LLVMContext. Option
  27. /// data for passes should be read from the context during doInitialization. An
  28. /// example of reading the above option would be:
  29. ///
  30. /// ScalarizeLoadStore =
  31. /// M.getContext().getOption<bool,
  32. /// Scalarizer,
  33. /// &Scalarizer::ScalarizeLoadStore>();
  34. ///
  35. //===----------------------------------------------------------------------===//
  36. #ifndef LLVM_SUPPORT_OPTIONS_H
  37. #define LLVM_SUPPORT_OPTIONS_H
  38. #include "llvm/ADT/DenseMap.h"
  39. #include "llvm/Support/CommandLine.h"
  40. namespace llvm {
  41. namespace detail {
  42. // Options are keyed of the unique address of a static character synthesized
  43. // based on template arguments.
  44. template <typename ValT, typename Base, ValT(Base::*Mem)> class OptionKey {
  45. public:
  46. static char ID;
  47. };
  48. template <typename ValT, typename Base, ValT(Base::*Mem)>
  49. char OptionKey<ValT, Base, Mem>::ID = 0;
  50. } // namespace detail
  51. /// \brief Singleton class used to register debug options.
  52. ///
  53. /// The OptionRegistry is responsible for managing lifetimes of the options and
  54. /// provides interfaces for option registration and reading values from options.
  55. /// This object is a singleton, only one instance should ever exist so that all
  56. /// options are registered in the same place.
  57. class OptionRegistry {
  58. private:
  59. DenseMap<void *, cl::Option *> Options;
  60. /// \brief Adds a cl::Option to the registry.
  61. ///
  62. /// \param Key unique key for option
  63. /// \param O option to map to \p Key
  64. ///
  65. /// Allocated cl::Options are owened by the OptionRegistry and are deallocated
  66. /// on destruction or removal
  67. void addOption(void *Key, cl::Option *O);
  68. public:
  69. ~OptionRegistry();
  70. OptionRegistry() {}
  71. /// \brief Returns a reference to the singleton instance.
  72. static OptionRegistry &instance();
  73. /// \brief Registers an option with the OptionRegistry singleton.
  74. ///
  75. /// \tparam ValT type of the option's data
  76. /// \tparam Base class used to key the option
  77. /// \tparam Mem member of \p Base used for keying the option
  78. ///
  79. /// Options are keyed off the template parameters to generate unique static
  80. /// characters. The template parameters are (1) the type of the data the
  81. /// option stores (\p ValT), the class that will read the option (\p Base),
  82. /// and the memeber that the class will store the data into (\p Mem).
  83. template <typename ValT, typename Base, ValT(Base::*Mem)>
  84. static void registerOption(const char *ArgStr, const char *Desc,
  85. const ValT &InitValue) {
  86. cl::opt<ValT> *Option = new cl::opt<ValT>(ArgStr, cl::desc(Desc),
  87. cl::Hidden, cl::init(InitValue));
  88. instance().addOption(&detail::OptionKey<ValT, Base, Mem>::ID, Option);
  89. }
  90. /// \brief Returns the value of the option.
  91. ///
  92. /// \tparam ValT type of the option's data
  93. /// \tparam Base class used to key the option
  94. /// \tparam Mem member of \p Base used for keying the option
  95. ///
  96. /// Reads option values based on the key generated by the template parameters.
  97. /// Keying for get() is the same as keying for registerOption.
  98. template <typename ValT, typename Base, ValT(Base::*Mem)> ValT get() const {
  99. auto It = Options.find(&detail::OptionKey<ValT, Base, Mem>::ID);
  100. assert(It != Options.end() && "Option not in OptionRegistry");
  101. return *(cl::opt<ValT> *)It->second;
  102. }
  103. };
  104. } // namespace llvm
  105. #endif