Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 1 | //===--- tools/clang-check/ClangCheck.cpp - Clang check tool --------------===// |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 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 | // |
Daniel Jasper | 5e5f726 | 2012-10-03 13:28:43 +0000 | [diff] [blame] | 10 | // This file implements a clang-check tool that runs clang based on the info |
| 11 | // stored in a compilation database. |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 12 | // |
Alexander Kornienko | dea8fba | 2012-07-16 12:46:48 +0000 | [diff] [blame] | 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 | // |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Alexander Kornienko | 4846470 | 2012-08-13 10:50:08 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTConsumer.h" |
| 20 | #include "clang/Driver/OptTable.h" |
| 21 | #include "clang/Driver/Options.h" |
| 22 | #include "clang/Frontend/ASTConsumers.h" |
Daniel Jasper | 5e5f726 | 2012-10-03 13:28:43 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/CompilerInstance.h" |
| 24 | #include "clang/Rewrite/Frontend/FixItRewriter.h" |
| 25 | #include "clang/Rewrite/Frontend/FrontendActions.h" |
Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 26 | #include "clang/Tooling/CommonOptionsParser.h" |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 27 | #include "clang/Tooling/Tooling.h" |
Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
Daniel Jasper | 5e5f726 | 2012-10-03 13:28:43 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Path.h" |
NAKAMURA Takumi | b8238b6 | 2013-01-18 13:46:48 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Signals.h" |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 31 | |
Alexander Kornienko | 4846470 | 2012-08-13 10:50:08 +0000 | [diff] [blame] | 32 | using namespace clang::driver; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 33 | using namespace clang::tooling; |
| 34 | using namespace llvm; |
| 35 | |
Alexander Kornienko | 6fbe982 | 2012-08-24 00:39:14 +0000 | [diff] [blame] | 36 | static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); |
Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 37 | static cl::extrahelp MoreHelp( |
Alexander Kornienko | dea8fba | 2012-07-16 12:46:48 +0000 | [diff] [blame] | 38 | "\tFor example, to run clang-check on all files in a subtree of the\n" |
| 39 | "\tsource tree, use:\n" |
Alexander Kornienko | 140d513 | 2012-07-12 14:34:23 +0000 | [diff] [blame] | 40 | "\n" |
Alexander Kornienko | dea8fba | 2012-07-16 12:46:48 +0000 | [diff] [blame] | 41 | "\t find path/in/subtree -name '*.cpp'|xargs clang-check\n" |
Alexander Kornienko | 140d513 | 2012-07-12 14:34:23 +0000 | [diff] [blame] | 42 | "\n" |
Alexander Kornienko | dea8fba | 2012-07-16 12:46:48 +0000 | [diff] [blame] | 43 | "\tor using a specific build path:\n" |
Alexander Kornienko | 140d513 | 2012-07-12 14:34:23 +0000 | [diff] [blame] | 44 | "\n" |
Alexander Kornienko | dea8fba | 2012-07-16 12:46:48 +0000 | [diff] [blame] | 45 | "\t find path/in/subtree -name '*.cpp'|xargs clang-check -p build/path\n" |
Alexander Kornienko | 140d513 | 2012-07-12 14:34:23 +0000 | [diff] [blame] | 46 | "\n" |
Alexander Kornienko | dea8fba | 2012-07-16 12:46:48 +0000 | [diff] [blame] | 47 | "\tNote, that path/in/subtree and current directory should follow the\n" |
| 48 | "\trules described above.\n" |
Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 49 | "\n" |
| 50 | ); |
| 51 | |
| 52 | static OwningPtr<OptTable> Options(createDriverOptTable()); |
| 53 | static cl::opt<bool> ASTDump( |
| 54 | "ast-dump", |
| 55 | cl::desc(Options->getOptionHelpText(options::OPT_ast_dump))); |
| 56 | static cl::opt<bool> ASTList( |
| 57 | "ast-list", |
| 58 | cl::desc(Options->getOptionHelpText(options::OPT_ast_list))); |
| 59 | static cl::opt<bool> ASTPrint( |
| 60 | "ast-print", |
| 61 | cl::desc(Options->getOptionHelpText(options::OPT_ast_print))); |
| 62 | static cl::opt<std::string> ASTDumpFilter( |
| 63 | "ast-dump-filter", |
| 64 | cl::desc(Options->getOptionHelpText(options::OPT_ast_dump_filter))); |
Alexander Kornienko | 140d513 | 2012-07-12 14:34:23 +0000 | [diff] [blame] | 65 | |
Daniel Jasper | 5e5f726 | 2012-10-03 13:28:43 +0000 | [diff] [blame] | 66 | static cl::opt<bool> Fixit( |
| 67 | "fixit", |
| 68 | cl::desc(Options->getOptionHelpText(options::OPT_fixit))); |
| 69 | static cl::opt<bool> FixWhatYouCan( |
| 70 | "fix-what-you-can", |
| 71 | cl::desc(Options->getOptionHelpText(options::OPT_fix_what_you_can))); |
| 72 | |
| 73 | namespace { |
| 74 | |
| 75 | // FIXME: Move FixItRewriteInPlace from lib/Rewrite/Frontend/FrontendActions.cpp |
| 76 | // into a header file and reuse that. |
| 77 | class FixItOptions : public clang::FixItOptions { |
| 78 | public: |
| 79 | FixItOptions() { |
| 80 | FixWhatYouCan = ::FixWhatYouCan; |
| 81 | } |
| 82 | |
| 83 | std::string RewriteFilename(const std::string& filename, int &fd) { |
| 84 | assert(llvm::sys::path::is_absolute(filename) && |
| 85 | "clang-fixit expects absolute paths only."); |
| 86 | |
| 87 | // We don't need to do permission checking here since clang will diagnose |
| 88 | // any I/O errors itself. |
| 89 | |
| 90 | fd = -1; // No file descriptor for file. |
| 91 | |
| 92 | return filename; |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | /// \brief Subclasses \c clang::FixItRewriter to not count fixed errors/warnings |
| 97 | /// in the final error counts. |
| 98 | /// |
| 99 | /// This has the side-effect that clang-check -fixit exits with code 0 on |
| 100 | /// successfully fixing all errors. |
| 101 | class FixItRewriter : public clang::FixItRewriter { |
| 102 | public: |
| 103 | FixItRewriter(clang::DiagnosticsEngine& Diags, |
| 104 | clang::SourceManager& SourceMgr, |
| 105 | const clang::LangOptions& LangOpts, |
| 106 | clang::FixItOptions* FixItOpts) |
| 107 | : clang::FixItRewriter(Diags, SourceMgr, LangOpts, FixItOpts) { |
| 108 | } |
| 109 | |
| 110 | virtual bool IncludeInDiagnosticCounts() const { return false; } |
| 111 | }; |
| 112 | |
| 113 | /// \brief Subclasses \c clang::FixItAction so that we can install the custom |
| 114 | /// \c FixItRewriter. |
| 115 | class FixItAction : public clang::FixItAction { |
| 116 | public: |
| 117 | virtual bool BeginSourceFileAction(clang::CompilerInstance& CI, |
| 118 | StringRef Filename) { |
| 119 | FixItOpts.reset(new FixItOptions); |
| 120 | Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(), |
| 121 | CI.getLangOpts(), FixItOpts.get())); |
| 122 | return true; |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | } // namespace |
| 127 | |
Alexander Kornienko | 18857f7 | 2012-09-10 14:54:38 +0000 | [diff] [blame] | 128 | // Anonymous namespace here causes problems with gcc <= 4.4 on MacOS 10.6. |
| 129 | // "Non-global symbol: ... can't be a weak_definition" |
| 130 | namespace clang_check { |
| 131 | class ClangCheckActionFactory { |
Alexander Kornienko | 4846470 | 2012-08-13 10:50:08 +0000 | [diff] [blame] | 132 | public: |
Alexander Kornienko | 4846470 | 2012-08-13 10:50:08 +0000 | [diff] [blame] | 133 | clang::ASTConsumer *newASTConsumer() { |
| 134 | if (ASTList) |
| 135 | return clang::CreateASTDeclNodeLister(); |
| 136 | if (ASTDump) |
| 137 | return clang::CreateASTDumper(ASTDumpFilter); |
| 138 | if (ASTPrint) |
| 139 | return clang::CreateASTPrinter(&llvm::outs(), ASTDumpFilter); |
| 140 | return new clang::ASTConsumer(); |
| 141 | } |
Alexander Kornienko | 4846470 | 2012-08-13 10:50:08 +0000 | [diff] [blame] | 142 | }; |
Alexander Kornienko | 18857f7 | 2012-09-10 14:54:38 +0000 | [diff] [blame] | 143 | } |
Alexander Kornienko | 4846470 | 2012-08-13 10:50:08 +0000 | [diff] [blame] | 144 | |
Manuel Klimek | 30318e6 | 2012-04-18 07:41:50 +0000 | [diff] [blame] | 145 | int main(int argc, const char **argv) { |
NAKAMURA Takumi | b8238b6 | 2013-01-18 13:46:48 +0000 | [diff] [blame] | 146 | llvm::sys::PrintStackTraceOnErrorSignal(); |
Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 147 | CommonOptionsParser OptionsParser(argc, argv); |
Edwin Vane | b1f67db | 2012-12-14 18:58:25 +0000 | [diff] [blame] | 148 | ClangTool Tool(OptionsParser.getCompilations(), |
| 149 | OptionsParser.getSourcePathList()); |
Daniel Jasper | 5e5f726 | 2012-10-03 13:28:43 +0000 | [diff] [blame] | 150 | if (Fixit) |
| 151 | return Tool.run(newFrontendActionFactory<FixItAction>()); |
| 152 | clang_check::ClangCheckActionFactory Factory; |
Alexander Kornienko | 4846470 | 2012-08-13 10:50:08 +0000 | [diff] [blame] | 153 | return Tool.run(newFrontendActionFactory(&Factory)); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 154 | } |