• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 using namespace clang::driver;
28 using namespace clang::tooling;
29 using namespace llvm;
30 
31 static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
32 static 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 
47 static OwningPtr<OptTable> Options(createDriverOptTable());
48 static cl::opt<bool> ASTDump(
49     "ast-dump",
50     cl::desc(Options->getOptionHelpText(options::OPT_ast_dump)));
51 static cl::opt<bool> ASTList(
52     "ast-list",
53     cl::desc(Options->getOptionHelpText(options::OPT_ast_list)));
54 static cl::opt<bool> ASTPrint(
55     "ast-print",
56     cl::desc(Options->getOptionHelpText(options::OPT_ast_print)));
57 static 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 10.6.
62 // "Non-global symbol: ... can't be a weak_definition"
63 namespace clang_check {
64 class ClangCheckActionFactory {
65 public:
newASTConsumer()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 
main(int argc,const char ** argv)78 int main(int argc, const char **argv) {
79   clang_check::ClangCheckActionFactory Factory;
80   CommonOptionsParser OptionsParser(argc, argv);
81   ClangTool Tool(OptionsParser.GetCompilations(),
82                  OptionsParser.GetSourcePathList());
83   return Tool.run(newFrontendActionFactory(&Factory));
84 }
85