Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:12

0001 //===- CommonOptionsParser.h - common options for clang tools -*- C++ -*-=====//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 //  This file implements the CommonOptionsParser class used to parse common
0010 //  command-line options for clang tools, so that they can be run as separate
0011 //  command-line applications with a consistent common interface for handling
0012 //  compilation database and input files.
0013 //
0014 //  It provides a common subset of command-line options, common algorithm
0015 //  for locating a compilation database and source files, and help messages
0016 //  for the basic command-line interface.
0017 //
0018 //  It creates a CompilationDatabase and reads common command-line options.
0019 //
0020 //  This class uses the Clang Tooling infrastructure, see
0021 //    http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
0022 //  for details on setting it up with LLVM source tree.
0023 //
0024 //===----------------------------------------------------------------------===//
0025 
0026 #ifndef LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H
0027 #define LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H
0028 
0029 #include "clang/Tooling/ArgumentsAdjusters.h"
0030 #include "clang/Tooling/CompilationDatabase.h"
0031 #include "llvm/Support/CommandLine.h"
0032 #include "llvm/Support/Error.h"
0033 
0034 namespace clang {
0035 namespace tooling {
0036 /// A parser for options common to all command-line Clang tools.
0037 ///
0038 /// Parses a common subset of command-line arguments, locates and loads a
0039 /// compilation commands database and runs a tool with user-specified action. It
0040 /// also contains a help message for the common command-line options.
0041 ///
0042 /// An example of usage:
0043 /// \code
0044 /// #include "clang/Frontend/FrontendActions.h"
0045 /// #include "clang/Tooling/CommonOptionsParser.h"
0046 /// #include "clang/Tooling/Tooling.h"
0047 /// #include "llvm/Support/CommandLine.h"
0048 ///
0049 /// using namespace clang::tooling;
0050 /// using namespace llvm;
0051 ///
0052 /// static cl::OptionCategory MyToolCategory("my-tool options");
0053 /// static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
0054 /// static cl::extrahelp MoreHelp("\nMore help text...\n");
0055 ///
0056 /// int main(int argc, const char **argv) {
0057 ///   auto ExpectedParser =
0058 ///       CommonOptionsParser::create(argc, argv, MyToolCategory);
0059 ///   if (!ExpectedParser) {
0060 ///     llvm::errs() << ExpectedParser.takeError();
0061 ///     return 1;
0062 ///   }
0063 ///   CommonOptionsParser& OptionsParser = ExpectedParser.get();
0064 ///   ClangTool Tool(OptionsParser.getCompilations(),
0065 ///                  OptionsParser.getSourcePathList());
0066 ///   return Tool.run(
0067 ///       newFrontendActionFactory<clang::SyntaxOnlyAction>().get());
0068 /// }
0069 /// \endcode
0070 class CommonOptionsParser {
0071 
0072 protected:
0073   /// Parses command-line, initializes a compilation database.
0074   ///
0075   /// This constructor can change argc and argv contents, e.g. consume
0076   /// command-line options used for creating FixedCompilationDatabase.
0077   ///
0078   /// All options not belonging to \p Category become hidden.
0079   ///
0080   /// It also allows calls to set the required number of positional parameters.
0081   CommonOptionsParser(
0082       int &argc, const char **argv, llvm::cl::OptionCategory &Category,
0083       llvm::cl::NumOccurrencesFlag OccurrencesFlag = llvm::cl::OneOrMore,
0084       const char *Overview = nullptr);
0085 
0086 public:
0087   /// A factory method that is similar to the above constructor, except
0088   /// this returns an error instead exiting the program on error.
0089   static llvm::Expected<CommonOptionsParser>
0090   create(int &argc, const char **argv, llvm::cl::OptionCategory &Category,
0091          llvm::cl::NumOccurrencesFlag OccurrencesFlag = llvm::cl::OneOrMore,
0092          const char *Overview = nullptr);
0093 
0094   /// Returns a reference to the loaded compilations database.
0095   CompilationDatabase &getCompilations() {
0096     return *Compilations;
0097   }
0098 
0099   /// Returns a list of source file paths to process.
0100   const std::vector<std::string> &getSourcePathList() const {
0101     return SourcePathList;
0102   }
0103 
0104   /// Returns the argument adjuster calculated from "--extra-arg" and
0105   //"--extra-arg-before" options.
0106   ArgumentsAdjuster getArgumentsAdjuster() { return Adjuster; }
0107 
0108   static const char *const HelpMessage;
0109 
0110 private:
0111   CommonOptionsParser() = default;
0112 
0113   llvm::Error init(int &argc, const char **argv,
0114                    llvm::cl::OptionCategory &Category,
0115                    llvm::cl::NumOccurrencesFlag OccurrencesFlag,
0116                    const char *Overview);
0117 
0118   std::unique_ptr<CompilationDatabase> Compilations;
0119   std::vector<std::string> SourcePathList;
0120   ArgumentsAdjuster Adjuster;
0121 };
0122 
0123 class ArgumentsAdjustingCompilations : public CompilationDatabase {
0124 public:
0125   ArgumentsAdjustingCompilations(
0126       std::unique_ptr<CompilationDatabase> Compilations)
0127       : Compilations(std::move(Compilations)) {}
0128 
0129   void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster);
0130 
0131   std::vector<CompileCommand>
0132   getCompileCommands(StringRef FilePath) const override;
0133 
0134   std::vector<std::string> getAllFiles() const override;
0135 
0136   std::vector<CompileCommand> getAllCompileCommands() const override;
0137 
0138 private:
0139   std::unique_ptr<CompilationDatabase> Compilations;
0140   std::vector<ArgumentsAdjuster> Adjusters;
0141 
0142   std::vector<CompileCommand>
0143   adjustCommands(std::vector<CompileCommand> Commands) const;
0144 };
0145 
0146 }  // namespace tooling
0147 }  // namespace clang
0148 
0149 #endif // LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H