diff options
author | Manuel Klimek <klimek@google.com> | 2012-07-12 18:32:50 +0000 |
---|---|---|
committer | Manuel Klimek <klimek@google.com> | 2012-07-12 18:32:50 +0000 |
commit | 59d7cc9e4c376c12d4fa2c30337761dbec94e92d (patch) | |
tree | 483969658402d98468d0100b9e28a69ecec77533 | |
parent | 99b28e7e6b136acd8b294a807550e66f78c07f44 (diff) |
Updates the example to the latest incarnation of clang-check and
adds a paragraph on builtin headers.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@160138 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | docs/LibTooling.html | 91 |
1 files changed, 56 insertions, 35 deletions
diff --git a/docs/LibTooling.html b/docs/LibTooling.html index 44e4123d8c..560d115489 100644 --- a/docs/LibTooling.html +++ b/docs/LibTooling.html @@ -72,17 +72,21 @@ int main(int argc, const char **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. + // a database in the specified build directory or auto-detect it from a + // source file. std::string ErrorMessage; - Compilations.reset(CompilationDatabase::loadFromDirectory(BuildPath, - 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); } -... } </pre> </p> @@ -113,37 +117,43 @@ the files "a.cc" and "b.cc" one would write: <p>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. <pre> - #include "llvm/Support/CommandLine.h" - #include "clang/Frontend/FrontendActions.h" - #include "clang/Tooling/CompilationDatabase.h" - #include "clang/Tooling/Tooling.h" +#include "llvm/Support/CommandLine.h" +#include "clang/Frontend/FrontendActions.h" +#include "clang/Tooling/CompilationDatabase.h" +#include "clang/Tooling/Tooling.h" + +using namespace clang::tooling; +using namespace llvm; + +cl::opt<std::string> BuildPath( + "p", + cl::desc("<build-path>"), + cl::Optional); - using namespace clang::tooling; - using namespace llvm; - - cl::opt<std::string> BuildPath( - cl::Positional, - cl::desc("<build-path>")); - - cl::list<std::string> SourcePaths( - cl::Positional, - cl::desc("<source0> [... <sourceN>]"), - cl::OneOrMore); - - 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(CompilationDatabase::loadFromDirectory(BuildPath, - ErrorMessage)); - if (!Compilations) - llvm::report_fatal_error(ErrorMessage); +cl::list<std::string> SourcePaths( + cl::Positional, + cl::desc("<source0> [... <sourceN>]"), + cl::OneOrMore); + +int main(int argc, const char **argv) { + llvm::OwningPtr<CompilationDatabase> Compilations( + FixedCompilationDatabase::loadFromCommandLine(argc, argv)); + cl::ParseCommandLineOptions(argc, argv); + if (!Compilations) { + std::string ErrorMessage; + if (!BuildPath.empty()) { + Compilations.reset( + CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage)); + } else { + Compilations.reset(CompilationDatabase::autoDetectFromSource( + SourcePaths[0], ErrorMessage)); } - ClangTool Tool(*Compilations, SourcePaths); - return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>()); + if (!Compilations) + llvm::report_fatal_error(ErrorMessage); } + ClangTool Tool(*Compilations, SourcePaths); + return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>()); +} </pre> </p> @@ -155,7 +165,7 @@ all the needed parameters after a "--" separator: <pre> $ cd /path/to/source/llvm $ export BD=/path/to/build/llvm - $ $BD/bin/clang-check . tools/clang/tools/clang-check/ClangCheck.cpp -- \ + $ $BD/bin/clang-check tools/clang/tools/clang-check/ClangCheck.cpp -- \ clang++ -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS \ -Itools/clang/include -I$BD/include -Iinclude -Itools/clang/lib/Headers -c </pre> @@ -176,10 +186,21 @@ as first argument and some source files as further positional arguments: <pre> $ cd /path/to/source/llvm $ export BD=/path/to/build/llvm - $ $BD/bin/clang-check $BD tools/clang/tools/clang-check/ClangCheck.cpp + $ $BD/bin/clang-check -p $BD tools/clang/tools/clang-check/ClangCheck.cpp </pre> </p> +<h3 id="builtin">Builtin includes.</h3> +<p>Clang tools need their builtin headers and search for them the same way clang +does. Thus, the default location to look for builtin headers is in a path +$(dirname /path/to/tool)/../lib/clang/3.2/include relative to the tool +binary. This works out-of-the-box for tools running from llvm's toplevel +binary directory after building clang-headers, or if the tool is running +from the binary directory of a clang install next to the clang binary.</p> + +<p>Tips: if your tool fails to find stddef.h or similar headers, call +the tool with -v and look at the search paths it looks through.</p> + <h3 id="linking">Linking.</h3> <p>Please note that this presents the linking requirements at the time of this writing. For the most up-to-date information, look at one of the tools' |