blob: d51d90e90652c9da0ed46fc8f9f9c7e33ceaa9c2 [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(
Manuel Klimek8fa2fb82012-07-10 13:10:51 +000045 "p",
46 cl::desc("<build-path>"),
47 cl::Optional);
Manuel Klimekcb971c62012-04-04 12:07:46 +000048
49cl::list<std::string> SourcePaths(
50 cl::Positional,
51 cl::desc("<source0> [... <sourceN>]"),
52 cl::OneOrMore);
53
Manuel Klimek30318e62012-04-18 07:41:50 +000054int main(int argc, const char **argv) {
Manuel Klimekcb971c62012-04-04 12:07:46 +000055 llvm::OwningPtr<CompilationDatabase> Compilations(
Manuel Klimek30318e62012-04-18 07:41:50 +000056 FixedCompilationDatabase::loadFromCommandLine(argc, argv));
57 cl::ParseCommandLineOptions(argc, argv);
58 if (!Compilations) {
59 std::string ErrorMessage;
Manuel Klimek8fa2fb82012-07-10 13:10:51 +000060 if (!BuildPath.empty()) {
Arnaud A. de Grandmaison4187df52012-07-10 16:56:35 +000061 Compilations.reset(
62 CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage));
63
Manuel Klimek8fa2fb82012-07-10 13:10:51 +000064 } else {
65 Compilations.reset(CompilationDatabase::autoDetectFromSource(
66 SourcePaths[0], ErrorMessage));
67 }
Manuel Klimek30318e62012-04-18 07:41:50 +000068 if (!Compilations)
69 llvm::report_fatal_error(ErrorMessage);
70 }
Manuel Klimekcb971c62012-04-04 12:07:46 +000071 ClangTool Tool(*Compilations, SourcePaths);
72 return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>());
73}