CommonOptionsParser.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CommonOptionsParser.h - common options for clang tools -*- C++ -*-=====//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file implements the CommonOptionsParser class used to parse common
  15. // command-line options for clang tools, so that they can be run as separate
  16. // command-line applications with a consistent common interface for handling
  17. // compilation database and input files.
  18. //
  19. // It provides a common subset of command-line options, common algorithm
  20. // for locating a compilation database and source files, and help messages
  21. // for the basic command-line interface.
  22. //
  23. // It creates a CompilationDatabase and reads common command-line options.
  24. //
  25. // This class uses the Clang Tooling infrastructure, see
  26. // http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
  27. // for details on setting it up with LLVM source tree.
  28. //
  29. //===----------------------------------------------------------------------===//
  30. #ifndef LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H
  31. #define LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H
  32. #include "clang/Tooling/ArgumentsAdjusters.h"
  33. #include "clang/Tooling/CompilationDatabase.h"
  34. #include "llvm/Support/CommandLine.h"
  35. #include "llvm/Support/Error.h"
  36. namespace clang {
  37. namespace tooling {
  38. /// A parser for options common to all command-line Clang tools.
  39. ///
  40. /// Parses a common subset of command-line arguments, locates and loads a
  41. /// compilation commands database and runs a tool with user-specified action. It
  42. /// also contains a help message for the common command-line options.
  43. ///
  44. /// An example of usage:
  45. /// \code
  46. /// #include "clang/Frontend/FrontendActions.h"
  47. /// #include "clang/Tooling/CommonOptionsParser.h"
  48. /// #include "clang/Tooling/Tooling.h"
  49. /// #include "llvm/Support/CommandLine.h"
  50. ///
  51. /// using namespace clang::tooling;
  52. /// using namespace llvm;
  53. ///
  54. /// static cl::OptionCategory MyToolCategory("My tool options");
  55. /// static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
  56. /// static cl::extrahelp MoreHelp("\nMore help text...\n");
  57. /// static cl::opt<bool> YourOwnOption(...);
  58. /// ...
  59. ///
  60. /// int main(int argc, const char **argv) {
  61. /// CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
  62. /// ClangTool Tool(OptionsParser.getCompilations(),
  63. /// OptionsParser.getSourcePathList());
  64. /// return Tool.run(newFrontendActionFactory<SyntaxOnlyAction>().get());
  65. /// }
  66. /// \endcode
  67. class CommonOptionsParser {
  68. protected:
  69. /// Parses command-line, initializes a compilation database.
  70. ///
  71. /// This constructor can change argc and argv contents, e.g. consume
  72. /// command-line options used for creating FixedCompilationDatabase.
  73. ///
  74. /// All options not belonging to \p Category become hidden.
  75. ///
  76. /// It also allows calls to set the required number of positional parameters.
  77. CommonOptionsParser(
  78. int &argc, const char **argv, llvm::cl::OptionCategory &Category,
  79. llvm::cl::NumOccurrencesFlag OccurrencesFlag = llvm::cl::OneOrMore,
  80. const char *Overview = nullptr);
  81. public:
  82. /// A factory method that is similar to the above constructor, except
  83. /// this returns an error instead exiting the program on error.
  84. static llvm::Expected<CommonOptionsParser>
  85. create(int &argc, const char **argv, llvm::cl::OptionCategory &Category,
  86. llvm::cl::NumOccurrencesFlag OccurrencesFlag = llvm::cl::OneOrMore,
  87. const char *Overview = nullptr);
  88. /// Returns a reference to the loaded compilations database.
  89. CompilationDatabase &getCompilations() {
  90. return *Compilations;
  91. }
  92. /// Returns a list of source file paths to process.
  93. const std::vector<std::string> &getSourcePathList() const {
  94. return SourcePathList;
  95. }
  96. /// Returns the argument adjuster calculated from "--extra-arg" and
  97. //"--extra-arg-before" options.
  98. ArgumentsAdjuster getArgumentsAdjuster() { return Adjuster; }
  99. static const char *const HelpMessage;
  100. private:
  101. CommonOptionsParser() = default;
  102. llvm::Error init(int &argc, const char **argv,
  103. llvm::cl::OptionCategory &Category,
  104. llvm::cl::NumOccurrencesFlag OccurrencesFlag,
  105. const char *Overview);
  106. std::unique_ptr<CompilationDatabase> Compilations;
  107. std::vector<std::string> SourcePathList;
  108. ArgumentsAdjuster Adjuster;
  109. };
  110. class ArgumentsAdjustingCompilations : public CompilationDatabase {
  111. public:
  112. ArgumentsAdjustingCompilations(
  113. std::unique_ptr<CompilationDatabase> Compilations)
  114. : Compilations(std::move(Compilations)) {}
  115. void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster);
  116. std::vector<CompileCommand>
  117. getCompileCommands(StringRef FilePath) const override;
  118. std::vector<std::string> getAllFiles() const override;
  119. std::vector<CompileCommand> getAllCompileCommands() const override;
  120. private:
  121. std::unique_ptr<CompilationDatabase> Compilations;
  122. std::vector<ArgumentsAdjuster> Adjusters;
  123. std::vector<CompileCommand>
  124. adjustCommands(std::vector<CompileCommand> Commands) const;
  125. };
  126. } // namespace tooling
  127. } // namespace clang
  128. #endif // LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H
  129. #ifdef __GNUC__
  130. #pragma GCC diagnostic pop
  131. #endif