Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 1 | //===- examples/Tooling/ClangCheck.cpp - Clang check tool -----------------===// |
| 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 | // |
| 10 | // This file implements a clang-check tool that runs the |
| 11 | // clang::SyntaxOnlyAction over a number of translation units. |
| 12 | // |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Support/CommandLine.h" |
| 16 | #include "clang/Frontend/FrontendActions.h" |
| 17 | #include "clang/Tooling/CompilationDatabase.h" |
| 18 | #include "clang/Tooling/Tooling.h" |
| 19 | |
| 20 | using namespace clang::tooling; |
| 21 | using namespace llvm; |
| 22 | |
| 23 | cl::opt<std::string> BuildPath( |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 24 | "p", |
| 25 | cl::desc("<build-path>"), |
| 26 | cl::Optional); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 27 | |
| 28 | cl::list<std::string> SourcePaths( |
| 29 | cl::Positional, |
| 30 | cl::desc("<source0> [... <sourceN>]"), |
| 31 | cl::OneOrMore); |
| 32 | |
Alexander Kornienko | 140d513 | 2012-07-12 14:34:23 +0000 | [diff] [blame^] | 33 | static cl::extrahelp MoreHelp( |
| 34 | "\n" |
| 35 | "<build-path> is used to read a compile command database.\n" |
| 36 | "\n" |
| 37 | "For example, it can be a CMake build directory in which a file named\n" |
| 38 | "compile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON\n" |
| 39 | "CMake option to get this output). When no build path is specified,\n" |
| 40 | "clang-check will attempt to locate it automatically using all parent\n" |
| 41 | "paths of the first input file.\n" |
| 42 | "\n" |
| 43 | "<source0> ... specify the paths of source files. These paths are looked\n" |
| 44 | "up in the compile command database. If the path of a file is absolute,\n" |
| 45 | "it needs to point into CMake's source tree. If the path is relative,\n" |
| 46 | "the current working directory needs to be in the CMake source tree and\n" |
| 47 | "the file must be in a subdirectory of the current working directory.\n" |
| 48 | "\"./\" prefixes in the relative files will be automatically removed,\n" |
| 49 | "but the rest of a relative path must be a suffix of a path in the\n" |
| 50 | "compile command database.\n" |
| 51 | "\n" |
| 52 | "For example, to use clang-check on all files in a subtree of the source\n" |
| 53 | "tree, use:\n" |
| 54 | "\n" |
| 55 | " find path/in/subtree -name '*.cpp'|xargs clang-check\n" |
| 56 | "\n" |
| 57 | "or using a specific build path:\n" |
| 58 | "\n" |
| 59 | " find path/in/subtree -name '*.cpp'|xargs clang-check -p build/path\n" |
| 60 | "\n" |
| 61 | "Note, that path/in/subtree and current directory should follow the\n" |
| 62 | "rules described above.\n" |
| 63 | "\n" |
| 64 | ); |
| 65 | |
Manuel Klimek | 30318e6 | 2012-04-18 07:41:50 +0000 | [diff] [blame] | 66 | int main(int argc, const char **argv) { |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 67 | llvm::OwningPtr<CompilationDatabase> Compilations( |
Manuel Klimek | 30318e6 | 2012-04-18 07:41:50 +0000 | [diff] [blame] | 68 | FixedCompilationDatabase::loadFromCommandLine(argc, argv)); |
| 69 | cl::ParseCommandLineOptions(argc, argv); |
| 70 | if (!Compilations) { |
| 71 | std::string ErrorMessage; |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 72 | if (!BuildPath.empty()) { |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 73 | Compilations.reset( |
| 74 | CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage)); |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 75 | } else { |
| 76 | Compilations.reset(CompilationDatabase::autoDetectFromSource( |
| 77 | SourcePaths[0], ErrorMessage)); |
| 78 | } |
Manuel Klimek | 30318e6 | 2012-04-18 07:41:50 +0000 | [diff] [blame] | 79 | if (!Compilations) |
| 80 | llvm::report_fatal_error(ErrorMessage); |
| 81 | } |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 82 | ClangTool Tool(*Compilations, SourcePaths); |
| 83 | return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>()); |
| 84 | } |