diff --git a/clang/docs/LibTooling.html b/clang/docs/LibTooling.html index ed0ad45e4e7f..d896a5f88576 100644 --- a/clang/docs/LibTooling.html +++ b/clang/docs/LibTooling.html @@ -21,19 +21,20 @@ a tool using LibTooling.

Introduction

-

Tools built with LibTooling, like Clang Plugins, run FrontendActions over -code. +

Tools built with LibTooling, like Clang Plugins, run +FrontendActions over code. + In this tutorial, we'll demonstrate the different ways of running clang's -SyntaxOnlyAction, which runs a quick syntax check, over a bunch of +SyntaxOnlyAction, which runs a quick syntax check, over a bunch of code.

Parsing a code snippet in memory.

-

If you ever wanted to run a FrontendAction over some sample code, for example -to unit test parts of the Clang AST, runToolOnCode is what you looked for. Let -me give you an example: +

If you ever wanted to run a FrontendAction over some sample +code, for example to unit test parts of the Clang AST, +runToolOnCode is what you looked for. Let me give you an example:

   #include "clang/Tooling/Tooling.h"
 
@@ -48,53 +49,44 @@ me give you an example:
 

Writing a standalone tool.

-

Once you unit tested your FrontendAction to the point where it cannot -possibly break, it's time to create a standalone tool. For a standalone tool -to run clang, it first needs to figure out what command line arguments to use -for a specified file. To that end we create a CompilationDatabase.

+

Once you unit tested your FrontendAction to the point where it +cannot possibly break, it's time to create a standalone tool. For a standalone +tool to run clang, it first needs to figure out what command line arguments to +use for a specified file. To that end we create a +CompilationDatabase. There are different ways to create a +compilation database, and we need to support all of them depending on +command-line options. There's the CommonOptionsParser class +that takes the responsibility to parse command-line parameters related to +compilation databases and inputs, so that all tools share the implementation. +

-

Creating a compilation database.

-

CompilationDatabase provides static factory functions to help with parsing -compile commands from a build directory or the command line. The following code -allows for both explicit specification of a compile command line, as well as -retrieving the compile commands lines from a database. +

Parsing common tools options.

+

CompilationDatabase can be read from a build directory or the +command line. Using CommonOptionsParser allows for explicit +specification of a compile command line, specification of build path using the +-p command-line option, and automatic location of the compilation +database using source files paths.

+#include "clang/Tooling/CommonOptionsParser.h"
+
+using namespace clang::tooling;
+
 int main(int argc, const char **argv) {
-  // First, try to create a fixed compile command database from the command line
-  // arguments.
-  llvm::OwningPtr<CompilationDatabase> Compilations(
-    FixedCompilationDatabase::loadFromCommandLine(argc, argv));
+  // CommonOptionsParser constructor will parse arguments and create a
+  // CompilationDatabase. In case of error it will terminate the program.
+  CommonOptionsParser OptionsParser(argc, argv);
 
-  // Next, use normal llvm command line parsing to get the tool specific
-  // parameters.
-  cl::ParseCommandLineOptions(argc, argv);
-
-  if (!Compilations) {
-    // In case the user did not specify the compile command line via positional
-    // command line arguments after "--", try to load the compile commands from
-    // a database in the specified build directory or auto-detect it from a
-    // source file.
-    std::string ErrorMessage;
-    if (!BuildPath.empty()) {
-      Compilations.reset(
-         CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage));
-    } else {
-      Compilations.reset(CompilationDatabase::autoDetectFromSource(
-          SourcePaths[0], ErrorMessage));
-    }
-    // If there is still no valid compile command database, we don't know how
-    // to run the tool.
-    if (!Compilations)
-      llvm::report_fatal_error(ErrorMessage);
-  }
+  // Use OptionsParser.GetCompilations() and OptionsParser.GetSourcePathList()
+  // to retrieve CompilationDatabase and the list of input file paths.
 }
 

Creating and running a ClangTool.

-

Once we have a CompilationDatabase, we can create a ClangTool and run our -FrontendAction over some code. For example, to run the SyntaxOnlyAction over -the files "a.cc" and "b.cc" one would write: +

Once we have a CompilationDatabase, we can create a +ClangTool and run our FrontendAction over some code. +For example, to run the SyntaxOnlyAction over the files "a.cc" and +"b.cc" one would write:

   // A clang tool can run over a number of sources in the same process...
   std::vector<std::string> Sources;
@@ -103,7 +95,7 @@ the files "a.cc" and "b.cc" one would write:
 
   // We hand the CompilationDatabase we created and the sources to run over into
   // the tool constructor.
-  ClangTool Tool(*Compilations, Sources);
+  ClangTool Tool(OptionsParser.GetCompilations(), Sources);
 
   // The ClangTool needs a new FrontendAction for each translation unit we run
   // on. Thus, it takes a FrontendActionFactory as parameter. To create a
@@ -117,40 +109,28 @@ the files "a.cc" and "b.cc" one would write:
 

Now we combine the two previous steps into our first real tool. This example tool is also checked into the clang tree at tools/clang-check/ClangCheck.cpp.

-#include "llvm/Support/CommandLine.h"
+// Declares clang::SyntaxOnlyAction.
 #include "clang/Frontend/FrontendActions.h"
-#include "clang/Tooling/CompilationDatabase.h"
-#include "clang/Tooling/Tooling.h"
+#include "clang/Tooling/CommonOptionsParser.h"
+// Declares llvm::cl::extrahelp.
+#include "llvm/Support/CommandLine.h"
 
 using namespace clang::tooling;
 using namespace llvm;
 
-cl::opt<std::string> BuildPath(
-  "p",
-  cl::desc("<build-path>"),
-  cl::Optional);
+// CommonOptionsParser declares HelpMessage with a description of the common
+// command-line options related to the compilation database and input files.
+// It's nice to have this help message in all tools.
+static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
 
-cl::list<std::string> SourcePaths(
-  cl::Positional,
-  cl::desc("<source0> [... <sourceN>]"),
-  cl::OneOrMore);
+// A help message for this specific tool can be added afterwards.
+static cl::extrahelp MoreHelp("\nMore help text...");
 
 int main(int argc, const char **argv) {
-  llvm::OwningPtr<CompilationDatabase> Compilations(
-    FixedCompilationDatabase::loadFromCommandLine(argc, argv));
-  cl::ParseCommandLineOptions(argc, argv);
-  if (!Compilations) {
-    std::string ErrorMessage;
-    Compilations.reset(
-      !BuildPath.empty() ?
-        CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage) :
-        CompilationDatabase::autoDetectFromSource(SourcePaths[0], ErrorMessage)
-      );
-    if (!Compilations)
-      llvm::report_fatal_error(ErrorMessage);
-  }
-  ClangTool Tool(*Compilations, SourcePaths);
-  return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>());
+  CommonOptionsParser OptionsParser(argc, argv);
+  ClangTool Tool(OptionsParser.GetCompilations(),
+                 OptionsParser.GetSourcePathList());
+  return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>());
 }
 

diff --git a/clang/include/clang/Tooling/CommonOptionsParser.h b/clang/include/clang/Tooling/CommonOptionsParser.h index 9eed3d322918..a1bad1269d80 100644 --- a/clang/include/clang/Tooling/CommonOptionsParser.h +++ b/clang/include/clang/Tooling/CommonOptionsParser.h @@ -28,13 +28,9 @@ #define LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H #include "clang/Tooling/CompilationDatabase.h" -#include "clang/Frontend/FrontendActions.h" namespace clang { namespace tooling { - -extern const char *const CommonHelpMessage; - /// \brief A parser for options common to all command-line Clang tools. /// /// Parses a common subset of command-line arguments, locates and loads a @@ -43,13 +39,14 @@ extern const char *const CommonHelpMessage; /// /// An example of usage: /// \code -/// #include "llvm/Support/CommandLine.h" +/// #include "clang/Frontend/FrontendActions.h" /// #include "clang/Tooling/CommonOptionsParser.h" +/// #include "llvm/Support/CommandLine.h" /// /// using namespace clang::tooling; /// using namespace llvm; /// -/// static cl::extrahelp CommonHelp(CommonHelpMessage); +/// static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); /// static cl::extrahelp MoreHelp("\nMore help text..."); /// static cl:opt YourOwnOption(...); /// ... @@ -79,6 +76,8 @@ public: return SourcePathList; } + static const char *const HelpMessage; + private: llvm::OwningPtr Compilations; std::vector SourcePathList; diff --git a/clang/lib/Tooling/CommonOptionsParser.cpp b/clang/lib/Tooling/CommonOptionsParser.cpp index eefd468ac195..2c8175786bfa 100644 --- a/clang/lib/Tooling/CommonOptionsParser.cpp +++ b/clang/lib/Tooling/CommonOptionsParser.cpp @@ -31,7 +31,7 @@ using namespace clang::tooling; using namespace llvm; -const char *const clang::tooling::CommonHelpMessage = +const char *const CommonOptionsParser::HelpMessage = "\n" "-p is used to read a compile command database.\n" "\n" diff --git a/clang/tools/clang-check/ClangCheck.cpp b/clang/tools/clang-check/ClangCheck.cpp index c02c23da8929..243ea0a05617 100644 --- a/clang/tools/clang-check/ClangCheck.cpp +++ b/clang/tools/clang-check/ClangCheck.cpp @@ -28,7 +28,7 @@ using namespace clang::driver; using namespace clang::tooling; using namespace llvm; -static cl::extrahelp CommonHelp(CommonHelpMessage); +static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); static cl::extrahelp MoreHelp( "\tFor example, to run clang-check on all files in a subtree of the\n" "\tsource tree, use:\n"