ClangCheck.cpp revision c80ca020300f6f4ecc595cca1e6cb93af76a4434
1//===--- tools/clang-check/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//  This tool uses the Clang Tooling infrastructure, see
14//    http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
15//  for details on setting it up with LLVM source tree.
16//
17//===----------------------------------------------------------------------===//
18
19#include "clang/AST/ASTConsumer.h"
20#include "clang/Driver/OptTable.h"
21#include "clang/Driver/Options.h"
22#include "clang/Frontend/ASTConsumers.h"
23#include "clang/Tooling/CommonOptionsParser.h"
24#include "clang/Tooling/Tooling.h"
25#include "llvm/Support/CommandLine.h"
26
27using namespace clang::driver;
28using namespace clang::tooling;
29using namespace llvm;
30
31static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
32static cl::extrahelp MoreHelp(
33    "\tFor example, to run clang-check on all files in a subtree of the\n"
34    "\tsource tree, use:\n"
35    "\n"
36    "\t  find path/in/subtree -name '*.cpp'|xargs clang-check\n"
37    "\n"
38    "\tor using a specific build path:\n"
39    "\n"
40    "\t  find path/in/subtree -name '*.cpp'|xargs clang-check -p build/path\n"
41    "\n"
42    "\tNote, that path/in/subtree and current directory should follow the\n"
43    "\trules described above.\n"
44    "\n"
45);
46
47static OwningPtr<OptTable> Options(createDriverOptTable());
48static cl::opt<bool> ASTDump(
49    "ast-dump",
50    cl::desc(Options->getOptionHelpText(options::OPT_ast_dump)));
51static cl::opt<bool> ASTList(
52    "ast-list",
53    cl::desc(Options->getOptionHelpText(options::OPT_ast_list)));
54static cl::opt<bool> ASTPrint(
55    "ast-print",
56    cl::desc(Options->getOptionHelpText(options::OPT_ast_print)));
57static cl::opt<std::string> ASTDumpFilter(
58    "ast-dump-filter",
59    cl::desc(Options->getOptionHelpText(options::OPT_ast_dump_filter)));
60
61// Anonymous namespace here causes problems with gcc <= 4.4 on MacOS:
62// http://llvm.org/bugs/show_bug.cgi?id=13777
63// namespace {
64class ActionFactory {
65public:
66  clang::ASTConsumer *newASTConsumer() {
67    if (ASTList)
68      return clang::CreateASTDeclNodeLister();
69    if (ASTDump)
70      return clang::CreateASTDumper(ASTDumpFilter);
71    if (ASTPrint)
72      return clang::CreateASTPrinter(&llvm::outs(), ASTDumpFilter);
73    return new clang::ASTConsumer();
74  }
75};
76// }
77
78int main(int argc, const char **argv) {
79  ActionFactory Factory;
80  CommonOptionsParser OptionsParser(argc, argv);
81  ClangTool Tool(OptionsParser.GetCompilations(),
82                 OptionsParser.GetSourcePathList());
83  return Tool.run(newFrontendActionFactory(&Factory));
84}
85