blob: 36af01b2e8d410c8072be23e5299786037b43b72 [file] [log] [blame]
Manuel Klimek47c245a2012-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//
Manuel Klimek47c245a2012-04-04 12:07:46 +000013//===----------------------------------------------------------------------===//
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
20using namespace clang::tooling;
21using namespace llvm;
22
23cl::opt<std::string> BuildPath(
Manuel Klimek65fd0e12012-07-10 13:10:51 +000024 "p",
25 cl::desc("<build-path>"),
26 cl::Optional);
Manuel Klimek47c245a2012-04-04 12:07:46 +000027
28cl::list<std::string> SourcePaths(
29 cl::Positional,
30 cl::desc("<source0> [... <sourceN>]"),
31 cl::OneOrMore);
32
Alexander Kornienko8480d422012-07-12 14:34:23 +000033static 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 Klimekff26efc2012-04-18 07:41:50 +000066int main(int argc, const char **argv) {
Manuel Klimek47c245a2012-04-04 12:07:46 +000067 llvm::OwningPtr<CompilationDatabase> Compilations(
Manuel Klimekff26efc2012-04-18 07:41:50 +000068 FixedCompilationDatabase::loadFromCommandLine(argc, argv));
69 cl::ParseCommandLineOptions(argc, argv);
70 if (!Compilations) {
71 std::string ErrorMessage;
Manuel Klimek65fd0e12012-07-10 13:10:51 +000072 if (!BuildPath.empty()) {
Arnaud A. de Grandmaison617f5262012-07-10 16:56:35 +000073 Compilations.reset(
74 CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage));
Manuel Klimek65fd0e12012-07-10 13:10:51 +000075 } else {
76 Compilations.reset(CompilationDatabase::autoDetectFromSource(
77 SourcePaths[0], ErrorMessage));
78 }
Manuel Klimekff26efc2012-04-18 07:41:50 +000079 if (!Compilations)
80 llvm::report_fatal_error(ErrorMessage);
81 }
Manuel Klimek47c245a2012-04-04 12:07:46 +000082 ClangTool Tool(*Compilations, SourcePaths);
83 return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>());
84}