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 //
13 // Usage:
14 // clang-check <cmake-output-dir> <file1> <file2> ...
15 //
16 // Where <cmake-output-dir> is a CMake build directory in which a file named
17 // compile_commands.json exists (enable -DCMAKE_EXPORT_COMPILE_COMMANDS in
18 // CMake to get this output).
19 //
20 // <file1> ... specify the paths of files in the CMake source tree. This path
21 // is looked up in the compile command database. If the path of a file is
22 // absolute, it needs to point into CMake's source tree. If the path is
23 // relative, the current working directory needs to be in the CMake source
24 // tree and the file must be in a subdirectory of the current working
25 // directory. "./" prefixes in the relative files will be automatically
26 // removed, but the rest of a relative path must be a suffix of a path in
27 // the compile command line database.
28 //
29 // For example, to use clang-check on all files in a subtree of the source
30 // tree, use:
31 //
32 // /path/in/subtree $ find . -name '*.cpp'| xargs clang-check /path/to/source
33 //
34 //===----------------------------------------------------------------------===//
35
36 #include "llvm/Support/CommandLine.h"
37 #include "clang/Frontend/FrontendActions.h"
38 #include "clang/Tooling/CompilationDatabase.h"
39 #include "clang/Tooling/Tooling.h"
40
41 using namespace clang::tooling;
42 using namespace llvm;
43
44 cl::opt<std::string> BuildPath(
45 cl::Positional,
46 cl::desc("<build-path>"));
47
48 cl::list<std::string> SourcePaths(
49 cl::Positional,
50 cl::desc("<source0> [... <sourceN>]"),
51 cl::OneOrMore);
52
main(int argc,const char ** argv)53 int main(int argc, const char **argv) {
54 llvm::OwningPtr<CompilationDatabase> Compilations(
55 FixedCompilationDatabase::loadFromCommandLine(argc, argv));
56 cl::ParseCommandLineOptions(argc, argv);
57 if (!Compilations) {
58 std::string ErrorMessage;
59 Compilations.reset(CompilationDatabase::loadFromDirectory(BuildPath,
60 ErrorMessage));
61 if (!Compilations)
62 llvm::report_fatal_error(ErrorMessage);
63 }
64 ClangTool Tool(*Compilations, SourcePaths);
65 return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>());
66 }
67