blob: b5b6bd5a14aeb92e234c36048956093e5029f984 [file] [log] [blame]
Manuel Klimekcb971c62012-04-04 12:07:46 +00001//===- 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
41using namespace clang::tooling;
42using namespace llvm;
43
44cl::opt<std::string> BuildPath(
45 cl::Positional,
46 cl::desc("<build-path>"));
47
48cl::list<std::string> SourcePaths(
49 cl::Positional,
50 cl::desc("<source0> [... <sourceN>]"),
51 cl::OneOrMore);
52
53int main(int argc, char **argv) {
54 cl::ParseCommandLineOptions(argc, argv);
55 std::string ErrorMessage;
56 llvm::OwningPtr<CompilationDatabase> Compilations(
57 CompilationDatabase::loadFromDirectory(BuildPath, ErrorMessage));
58 if (!Compilations)
59 llvm::report_fatal_error(ErrorMessage);
60 ClangTool Tool(*Compilations, SourcePaths);
61 return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>());
62}