blob: 8fcb26a4a429efd3a24af1e0815741369e5ac384 [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:
Daniel Jasperd217a662012-07-11 15:05:24 +000014// clang-check [-p <cmake-output-dir>] <file1> <file2> ...
Manuel Klimekcb971c62012-04-04 12:07:46 +000015//
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
Daniel Jasperd217a662012-07-11 15:05:24 +000018// CMake to get this output). If not provided, clang-check will search for this
19// file in all of <file1>'s parent directories.
Manuel Klimekcb971c62012-04-04 12:07:46 +000020//
Daniel Jasperd217a662012-07-11 15:05:24 +000021// <file1> ... specify the paths of files in the CMake source tree. This path
Manuel Klimekcb971c62012-04-04 12:07:46 +000022// is looked up in the compile command database. If the path of a file is
23// absolute, it needs to point into CMake's source tree. If the path is
24// relative, the current working directory needs to be in the CMake source
25// tree and the file must be in a subdirectory of the current working
26// directory. "./" prefixes in the relative files will be automatically
27// removed, but the rest of a relative path must be a suffix of a path in
28// the compile command line database.
29//
30// For example, to use clang-check on all files in a subtree of the source
31// tree, use:
32//
Daniel Jasperd217a662012-07-11 15:05:24 +000033// /path/in/subtree $ find . -name '*.cpp'| xargs clang-check
Manuel Klimekcb971c62012-04-04 12:07:46 +000034//
35//===----------------------------------------------------------------------===//
36
37#include "llvm/Support/CommandLine.h"
38#include "clang/Frontend/FrontendActions.h"
39#include "clang/Tooling/CompilationDatabase.h"
40#include "clang/Tooling/Tooling.h"
41
42using namespace clang::tooling;
43using namespace llvm;
44
45cl::opt<std::string> BuildPath(
Manuel Klimek8fa2fb82012-07-10 13:10:51 +000046 "p",
47 cl::desc("<build-path>"),
48 cl::Optional);
Manuel Klimekcb971c62012-04-04 12:07:46 +000049
50cl::list<std::string> SourcePaths(
51 cl::Positional,
52 cl::desc("<source0> [... <sourceN>]"),
53 cl::OneOrMore);
54
Manuel Klimek30318e62012-04-18 07:41:50 +000055int main(int argc, const char **argv) {
Manuel Klimekcb971c62012-04-04 12:07:46 +000056 llvm::OwningPtr<CompilationDatabase> Compilations(
Manuel Klimek30318e62012-04-18 07:41:50 +000057 FixedCompilationDatabase::loadFromCommandLine(argc, argv));
58 cl::ParseCommandLineOptions(argc, argv);
59 if (!Compilations) {
60 std::string ErrorMessage;
Manuel Klimek8fa2fb82012-07-10 13:10:51 +000061 if (!BuildPath.empty()) {
Arnaud A. de Grandmaison4187df52012-07-10 16:56:35 +000062 Compilations.reset(
63 CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage));
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}