Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 1 | //===--- tools/extra/clang-tidy/ClangTidy.cpp - Clang tidy 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 | /// \file This file implements a clang-tidy tool. |
| 11 | /// |
| 12 | /// This tool uses the Clang Tooling infrastructure, see |
| 13 | /// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html |
| 14 | /// for details on setting it up with LLVM source tree. |
| 15 | /// |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "ClangTidy.h" |
| 19 | #include "ClangTidyDiagnosticConsumer.h" |
| 20 | #include "ClangTidyModuleRegistry.h" |
| 21 | #include "clang/AST/ASTConsumer.h" |
| 22 | #include "clang/AST/ASTContext.h" |
| 23 | #include "clang/AST/Decl.h" |
| 24 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/ASTConsumers.h" |
| 26 | #include "clang/Frontend/CompilerInstance.h" |
| 27 | #include "clang/Frontend/FrontendActions.h" |
Alexander Kornienko | 38d81b4 | 2014-03-27 10:24:11 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/FrontendDiagnostic.h" |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/MultiplexConsumer.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 +0000 | [diff] [blame] | 31 | #include "clang/Lex/PPCallbacks.h" |
| 32 | #include "clang/Lex/Preprocessor.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 33 | #include "clang/Rewrite/Frontend/FixItRewriter.h" |
| 34 | #include "clang/Rewrite/Frontend/FrontendActions.h" |
Alexander Kornienko | d1199cb | 2014-01-03 17:24:20 +0000 | [diff] [blame] | 35 | #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 36 | #include "clang/Tooling/Refactoring.h" |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 +0000 | [diff] [blame] | 37 | #include "clang/Tooling/Tooling.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Path.h" |
Alexander Kornienko | 54461eb | 2014-02-06 14:50:10 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Process.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Signals.h" |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 41 | #include <algorithm> |
Alexander Kornienko | 38d81b4 | 2014-03-27 10:24:11 +0000 | [diff] [blame] | 42 | #include <utility> |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 43 | |
| 44 | using namespace clang::ast_matchers; |
| 45 | using namespace clang::driver; |
| 46 | using namespace clang::tooling; |
| 47 | using namespace llvm; |
| 48 | |
NAKAMURA Takumi | 71b982b | 2014-07-03 14:12:47 +0000 | [diff] [blame] | 49 | template class llvm::Registry<clang::tidy::ClangTidyModule>; |
| 50 | |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 51 | namespace clang { |
| 52 | namespace tidy { |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 53 | |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 54 | namespace { |
Alexander Kornienko | 54461eb | 2014-02-06 14:50:10 +0000 | [diff] [blame] | 55 | static const char *AnalyzerCheckNamePrefix = "clang-analyzer-"; |
Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 56 | |
Alexander Kornienko | 54461eb | 2014-02-06 14:50:10 +0000 | [diff] [blame] | 57 | static StringRef StaticAnalyzerChecks[] = { |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 58 | #define GET_CHECKERS |
| 59 | #define CHECKER(FULLNAME, CLASS, DESCFILE, HELPTEXT, GROUPINDEX, HIDDEN) \ |
| 60 | FULLNAME, |
NAKAMURA Takumi | 321b7d3 | 2014-01-03 10:24:51 +0000 | [diff] [blame] | 61 | #include "../../../lib/StaticAnalyzer/Checkers/Checkers.inc" |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 62 | #undef CHECKER |
| 63 | #undef GET_CHECKERS |
| 64 | }; |
| 65 | |
Alexander Kornienko | 54461eb | 2014-02-06 14:50:10 +0000 | [diff] [blame] | 66 | class AnalyzerDiagnosticConsumer : public ento::PathDiagnosticConsumer { |
| 67 | public: |
| 68 | AnalyzerDiagnosticConsumer(ClangTidyContext &Context) : Context(Context) {} |
| 69 | |
Alexander Kornienko | cb9272f | 2014-02-27 13:14:51 +0000 | [diff] [blame] | 70 | void FlushDiagnosticsImpl(std::vector<const ento::PathDiagnostic *> &Diags, |
Craig Topper | a3dbe84 | 2014-03-02 10:20:11 +0000 | [diff] [blame] | 71 | FilesMade *filesMade) override { |
Alexander Kornienko | df1e3cb | 2014-03-06 10:17:46 +0000 | [diff] [blame] | 72 | for (const ento::PathDiagnostic *PD : Diags) { |
Alexander Kornienko | d1afc70 | 2014-02-12 09:52:07 +0000 | [diff] [blame] | 73 | SmallString<64> CheckName(AnalyzerCheckNamePrefix); |
| 74 | CheckName += PD->getCheckName(); |
Alexander Kornienko | 95cd50f | 2014-03-06 13:24:28 +0000 | [diff] [blame] | 75 | Context.diag(CheckName, PD->getLocation().asLocation(), |
| 76 | PD->getShortDescription()) |
| 77 | << PD->path.back()->getRanges(); |
Alexander Kornienko | 54461eb | 2014-02-06 14:50:10 +0000 | [diff] [blame] | 78 | |
Alexander Kornienko | df1e3cb | 2014-03-06 10:17:46 +0000 | [diff] [blame] | 79 | for (const auto &DiagPiece : |
| 80 | PD->path.flatten(/*ShouldFlattenMacros=*/true)) { |
Alexander Kornienko | 95cd50f | 2014-03-06 13:24:28 +0000 | [diff] [blame] | 81 | Context.diag(CheckName, DiagPiece->getLocation().asLocation(), |
| 82 | DiagPiece->getString(), DiagnosticIDs::Note) |
| 83 | << DiagPiece->getRanges(); |
Alexander Kornienko | 54461eb | 2014-02-06 14:50:10 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Craig Topper | a3dbe84 | 2014-03-02 10:20:11 +0000 | [diff] [blame] | 88 | StringRef getName() const override { return "ClangTidyDiags"; } |
| 89 | bool supportsLogicalOpControlFlow() const override { return true; } |
| 90 | bool supportsCrossFileDiagnostics() const override { return true; } |
Alexander Kornienko | 54461eb | 2014-02-06 14:50:10 +0000 | [diff] [blame] | 91 | |
| 92 | private: |
| 93 | ClangTidyContext &Context; |
Alexander Kornienko | 54461eb | 2014-02-06 14:50:10 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Alexander Kornienko | 38d81b4 | 2014-03-27 10:24:11 +0000 | [diff] [blame] | 96 | class ErrorReporter { |
| 97 | public: |
| 98 | ErrorReporter(bool ApplyFixes) |
| 99 | : Files(FileSystemOptions()), DiagOpts(new DiagnosticOptions()), |
| 100 | DiagPrinter(new TextDiagnosticPrinter(llvm::outs(), &*DiagOpts)), |
| 101 | Diags(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts, |
| 102 | DiagPrinter), |
| 103 | SourceMgr(Diags, Files), Rewrite(SourceMgr, LangOpts), |
NAKAMURA Takumi | 4dd1813 | 2014-03-27 14:53:37 +0000 | [diff] [blame] | 104 | ApplyFixes(ApplyFixes), TotalFixes(0), AppliedFixes(0) { |
Alexander Kornienko | 38d81b4 | 2014-03-27 10:24:11 +0000 | [diff] [blame] | 105 | DiagOpts->ShowColors = llvm::sys::Process::StandardOutHasColors(); |
| 106 | DiagPrinter->BeginSourceFile(LangOpts); |
| 107 | } |
| 108 | |
Alexander Kornienko | 742790c | 2014-07-02 15:05:04 +0000 | [diff] [blame] | 109 | void reportDiagnostic(const ClangTidyError &Error) { |
| 110 | const ClangTidyMessage &Message = Error.Message; |
Alexander Kornienko | 38d81b4 | 2014-03-27 10:24:11 +0000 | [diff] [blame] | 111 | SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset); |
| 112 | // Contains a pair for each attempted fix: location and whether the fix was |
| 113 | // applied successfully. |
| 114 | SmallVector<std::pair<SourceLocation, bool>, 4> FixLocations; |
| 115 | { |
Alexander Kornienko | 742790c | 2014-07-02 15:05:04 +0000 | [diff] [blame] | 116 | auto Level = static_cast<DiagnosticsEngine::Level>(Error.DiagLevel); |
Alexander Kornienko | 38d81b4 | 2014-03-27 10:24:11 +0000 | [diff] [blame] | 117 | DiagnosticBuilder Diag = |
Alexander Kornienko | 742790c | 2014-07-02 15:05:04 +0000 | [diff] [blame] | 118 | Diags.Report(Loc, Diags.getCustomDiagID(Level, "%0 [%1]")) |
| 119 | << Message.Message << Error.CheckName; |
| 120 | for (const tooling::Replacement &Fix : Error.Fix) { |
| 121 | SourceLocation FixLoc = getLocation(Fix.getFilePath(), Fix.getOffset()); |
| 122 | SourceLocation FixEndLoc = FixLoc.getLocWithOffset(Fix.getLength()); |
| 123 | Diag << FixItHint::CreateReplacement(SourceRange(FixLoc, FixEndLoc), |
| 124 | Fix.getReplacementText()); |
| 125 | ++TotalFixes; |
| 126 | if (ApplyFixes) { |
| 127 | bool Success = Fix.isApplicable() && Fix.apply(Rewrite); |
| 128 | if (Success) |
| 129 | ++AppliedFixes; |
| 130 | FixLocations.push_back(std::make_pair(FixLoc, Success)); |
Alexander Kornienko | 38d81b4 | 2014-03-27 10:24:11 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } |
| 134 | for (auto Fix : FixLocations) { |
| 135 | Diags.Report(Fix.first, Fix.second ? diag::note_fixit_applied |
| 136 | : diag::note_fixit_failed); |
| 137 | } |
Alexander Kornienko | 742790c | 2014-07-02 15:05:04 +0000 | [diff] [blame] | 138 | for (const ClangTidyMessage &Note : Error.Notes) |
| 139 | reportNote(Note); |
Alexander Kornienko | 38d81b4 | 2014-03-27 10:24:11 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void Finish() { |
| 143 | // FIXME: Run clang-format on changes. |
| 144 | if (ApplyFixes && TotalFixes > 0) { |
| 145 | llvm::errs() << "clang-tidy applied " << AppliedFixes << " of " |
| 146 | << TotalFixes << " suggested fixes.\n"; |
| 147 | Rewrite.overwriteChangedFiles(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | private: |
| 152 | SourceLocation getLocation(StringRef FilePath, unsigned Offset) { |
| 153 | if (FilePath.empty()) |
| 154 | return SourceLocation(); |
| 155 | |
| 156 | const FileEntry *File = SourceMgr.getFileManager().getFile(FilePath); |
| 157 | FileID ID = SourceMgr.createFileID(File, SourceLocation(), SrcMgr::C_User); |
| 158 | return SourceMgr.getLocForStartOfFile(ID).getLocWithOffset(Offset); |
| 159 | } |
| 160 | |
Alexander Kornienko | 742790c | 2014-07-02 15:05:04 +0000 | [diff] [blame] | 161 | void reportNote(const ClangTidyMessage &Message) { |
| 162 | SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset); |
| 163 | DiagnosticBuilder Diag = |
| 164 | Diags.Report(Loc, Diags.getCustomDiagID(DiagnosticsEngine::Note, "%0")) |
| 165 | << Message.Message; |
| 166 | } |
| 167 | |
Alexander Kornienko | 38d81b4 | 2014-03-27 10:24:11 +0000 | [diff] [blame] | 168 | FileManager Files; |
| 169 | LangOptions LangOpts; // FIXME: use langopts from each original file |
| 170 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts; |
| 171 | DiagnosticConsumer *DiagPrinter; |
| 172 | DiagnosticsEngine Diags; |
| 173 | SourceManager SourceMgr; |
| 174 | Rewriter Rewrite; |
| 175 | bool ApplyFixes; |
NAKAMURA Takumi | 4dd1813 | 2014-03-27 14:53:37 +0000 | [diff] [blame] | 176 | unsigned TotalFixes; |
| 177 | unsigned AppliedFixes; |
Alexander Kornienko | 38d81b4 | 2014-03-27 10:24:11 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 180 | class ClangTidyASTConsumer : public MultiplexConsumer { |
| 181 | public: |
David Blaikie | 680c4c8 | 2014-08-10 19:56:59 +0000 | [diff] [blame] | 182 | ClangTidyASTConsumer(std::vector<std::unique_ptr<ASTConsumer>> Consumers, |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 183 | std::unique_ptr<ast_matchers::MatchFinder> Finder, |
| 184 | std::vector<std::unique_ptr<ClangTidyCheck>> Checks) |
David Blaikie | 680c4c8 | 2014-08-10 19:56:59 +0000 | [diff] [blame] | 185 | : MultiplexConsumer(std::move(Consumers)), Finder(std::move(Finder)), |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 186 | Checks(std::move(Checks)) {} |
| 187 | |
| 188 | private: |
| 189 | std::unique_ptr<ast_matchers::MatchFinder> Finder; |
| 190 | std::vector<std::unique_ptr<ClangTidyCheck>> Checks; |
| 191 | }; |
| 192 | |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 193 | } // namespace |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 194 | |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 195 | ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory( |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 196 | ClangTidyContext &Context) |
| 197 | : Context(Context), CheckFactories(new ClangTidyCheckFactories) { |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 198 | for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin(), |
| 199 | E = ClangTidyModuleRegistry::end(); |
| 200 | I != E; ++I) { |
Ahmed Charles | 6a2dc5c | 2014-03-09 09:24:40 +0000 | [diff] [blame] | 201 | std::unique_ptr<ClangTidyModule> Module(I->instantiate()); |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 202 | Module->addCheckFactories(*CheckFactories); |
| 203 | } |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 204 | } |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 205 | |
David Blaikie | 680c4c8 | 2014-08-10 19:56:59 +0000 | [diff] [blame] | 206 | std::unique_ptr<clang::ASTConsumer> |
| 207 | ClangTidyASTConsumerFactory::CreateASTConsumer( |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 208 | clang::CompilerInstance &Compiler, StringRef File) { |
| 209 | // FIXME: Move this to a separate method, so that CreateASTConsumer doesn't |
| 210 | // modify Compiler. |
| 211 | Context.setSourceManager(&Compiler.getSourceManager()); |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 212 | Context.setCurrentFile(File); |
Alexander Kornienko | ad21688 | 2014-07-14 14:10:03 +0000 | [diff] [blame] | 213 | Context.setASTContext(&Compiler.getASTContext()); |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 214 | |
| 215 | std::vector<std::unique_ptr<ClangTidyCheck>> Checks; |
Alexander Kornienko | b3d331d | 2014-08-06 11:49:10 +0000 | [diff] [blame] | 216 | GlobList &Filter = Context.getChecksFilter(); |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 217 | CheckFactories->createChecks(Filter, Checks); |
| 218 | |
| 219 | std::unique_ptr<ast_matchers::MatchFinder> Finder( |
| 220 | new ast_matchers::MatchFinder); |
| 221 | for (auto &Check : Checks) { |
| 222 | Check->setContext(&Context); |
| 223 | Check->registerMatchers(&*Finder); |
Alexander Kornienko | df1e3cb | 2014-03-06 10:17:46 +0000 | [diff] [blame] | 224 | Check->registerPPCallbacks(Compiler); |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 225 | } |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 226 | |
David Blaikie | 680c4c8 | 2014-08-10 19:56:59 +0000 | [diff] [blame] | 227 | std::vector<std::unique_ptr<ASTConsumer>> Consumers; |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 228 | if (!Checks.empty()) |
| 229 | Consumers.push_back(Finder->newASTConsumer()); |
Alexander Kornienko | 298b382 | 2014-02-13 16:10:47 +0000 | [diff] [blame] | 230 | |
Alex McCarthy | fec08c7 | 2014-04-30 14:09:24 +0000 | [diff] [blame] | 231 | AnalyzerOptionsRef AnalyzerOptions = Compiler.getAnalyzerOpts(); |
| 232 | // FIXME: Remove this option once clang's cfg-temporary-dtors option defaults |
| 233 | // to true. |
| 234 | AnalyzerOptions->Config["cfg-temporary-dtors"] = |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 235 | Context.getOptions().AnalyzeTemporaryDtors ? "true" : "false"; |
Alex McCarthy | fec08c7 | 2014-04-30 14:09:24 +0000 | [diff] [blame] | 236 | |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 237 | AnalyzerOptions->CheckersControlList = getCheckersControlList(Filter); |
Alex McCarthy | fec08c7 | 2014-04-30 14:09:24 +0000 | [diff] [blame] | 238 | if (!AnalyzerOptions->CheckersControlList.empty()) { |
| 239 | AnalyzerOptions->AnalysisStoreOpt = RegionStoreModel; |
| 240 | AnalyzerOptions->AnalysisDiagOpt = PD_NONE; |
| 241 | AnalyzerOptions->AnalyzeNestedBlocks = true; |
| 242 | AnalyzerOptions->eagerlyAssumeBinOpBifurcation = true; |
David Blaikie | 680c4c8 | 2014-08-10 19:56:59 +0000 | [diff] [blame] | 243 | std::unique_ptr<ento::AnalysisASTConsumer> AnalysisConsumer = |
| 244 | ento::CreateAnalysisConsumer( |
| 245 | Compiler.getPreprocessor(), Compiler.getFrontendOpts().OutputFile, |
| 246 | AnalyzerOptions, Compiler.getFrontendOpts().Plugins); |
Alexander Kornienko | 298b382 | 2014-02-13 16:10:47 +0000 | [diff] [blame] | 247 | AnalysisConsumer->AddDiagnosticConsumer( |
| 248 | new AnalyzerDiagnosticConsumer(Context)); |
David Blaikie | 680c4c8 | 2014-08-10 19:56:59 +0000 | [diff] [blame] | 249 | Consumers.push_back(std::move(AnalysisConsumer)); |
Alexander Kornienko | 298b382 | 2014-02-13 16:10:47 +0000 | [diff] [blame] | 250 | } |
David Blaikie | 680c4c8 | 2014-08-10 19:56:59 +0000 | [diff] [blame] | 251 | return llvm::make_unique<ClangTidyASTConsumer>( |
| 252 | std::move(Consumers), std::move(Finder), std::move(Checks)); |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 255 | std::vector<std::string> |
Alexander Kornienko | b3d331d | 2014-08-06 11:49:10 +0000 | [diff] [blame] | 256 | ClangTidyASTConsumerFactory::getCheckNames(GlobList &Filter) { |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 257 | std::vector<std::string> CheckNames; |
Alexander Kornienko | df1e3cb | 2014-03-06 10:17:46 +0000 | [diff] [blame] | 258 | for (const auto &CheckFactory : *CheckFactories) { |
Alexander Kornienko | b3d331d | 2014-08-06 11:49:10 +0000 | [diff] [blame] | 259 | if (Filter.contains(CheckFactory.first)) |
Alexander Kornienko | df1e3cb | 2014-03-06 10:17:46 +0000 | [diff] [blame] | 260 | CheckNames.push_back(CheckFactory.first); |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 263 | for (const auto &AnalyzerCheck : getCheckersControlList(Filter)) |
Alexander Kornienko | df1e3cb | 2014-03-06 10:17:46 +0000 | [diff] [blame] | 264 | CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first); |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 265 | |
| 266 | std::sort(CheckNames.begin(), CheckNames.end()); |
| 267 | return CheckNames; |
| 268 | } |
| 269 | |
| 270 | ClangTidyASTConsumerFactory::CheckersList |
Alexander Kornienko | b3d331d | 2014-08-06 11:49:10 +0000 | [diff] [blame] | 271 | ClangTidyASTConsumerFactory::getCheckersControlList(GlobList &Filter) { |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 272 | CheckersList List; |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 273 | |
| 274 | bool AnalyzerChecksEnabled = false; |
Alexander Kornienko | df1e3cb | 2014-03-06 10:17:46 +0000 | [diff] [blame] | 275 | for (StringRef CheckName : StaticAnalyzerChecks) { |
| 276 | std::string Checker((AnalyzerCheckNamePrefix + CheckName).str()); |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 277 | AnalyzerChecksEnabled = |
| 278 | AnalyzerChecksEnabled || |
Alexander Kornienko | b3d331d | 2014-08-06 11:49:10 +0000 | [diff] [blame] | 279 | (!CheckName.startswith("debug") && Filter.contains(Checker)); |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | if (AnalyzerChecksEnabled) { |
| 283 | // Run our regex against all possible static analyzer checkers. Note that |
| 284 | // debug checkers print values / run programs to visualize the CFG and are |
| 285 | // thus not applicable to clang-tidy in general. |
| 286 | // |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 287 | // Always add all core checkers if any other static analyzer checks are |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 288 | // enabled. This is currently necessary, as other path sensitive checks |
| 289 | // rely on the core checkers. |
Alexander Kornienko | df1e3cb | 2014-03-06 10:17:46 +0000 | [diff] [blame] | 290 | for (StringRef CheckName : StaticAnalyzerChecks) { |
| 291 | std::string Checker((AnalyzerCheckNamePrefix + CheckName).str()); |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 292 | |
Alexander Kornienko | df1e3cb | 2014-03-06 10:17:46 +0000 | [diff] [blame] | 293 | if (CheckName.startswith("core") || |
Alexander Kornienko | b3d331d | 2014-08-06 11:49:10 +0000 | [diff] [blame] | 294 | (!CheckName.startswith("debug") && Filter.contains(Checker))) |
Alexander Kornienko | df1e3cb | 2014-03-06 10:17:46 +0000 | [diff] [blame] | 295 | List.push_back(std::make_pair(CheckName, true)); |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 298 | return List; |
| 299 | } |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 300 | |
Peter Collingbourne | b17a3b3 | 2014-03-02 23:34:48 +0000 | [diff] [blame] | 301 | DiagnosticBuilder ClangTidyCheck::diag(SourceLocation Loc, StringRef Message, |
| 302 | DiagnosticIDs::Level Level) { |
| 303 | return Context->diag(CheckName, Loc, Message, Level); |
Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 306 | void ClangTidyCheck::run(const ast_matchers::MatchFinder::MatchResult &Result) { |
| 307 | Context->setSourceManager(Result.SourceManager); |
| 308 | check(Result); |
| 309 | } |
| 310 | |
Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 311 | void ClangTidyCheck::setName(StringRef Name) { |
| 312 | assert(CheckName.empty()); |
| 313 | CheckName = Name.str(); |
| 314 | } |
| 315 | |
Alexander Kornienko | 33a9bcc | 2014-04-29 15:20:10 +0000 | [diff] [blame] | 316 | std::vector<std::string> getCheckNames(const ClangTidyOptions &Options) { |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 317 | clang::tidy::ClangTidyContext Context( |
| 318 | new DefaultOptionsProvider(ClangTidyGlobalOptions(), Options)); |
| 319 | ClangTidyASTConsumerFactory Factory(Context); |
| 320 | return Factory.getCheckNames(Context.getChecksFilter()); |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 323 | ClangTidyStats runClangTidy(ClangTidyOptionsProvider *OptionsProvider, |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 324 | const tooling::CompilationDatabase &Compilations, |
Alexander Kornienko | 1e1ad5c | 2014-05-28 15:21:14 +0000 | [diff] [blame] | 325 | ArrayRef<std::string> InputFiles, |
Alexander Kornienko | 826b5ad | 2014-05-09 12:24:09 +0000 | [diff] [blame] | 326 | std::vector<ClangTidyError> *Errors) { |
Alexander Kornienko | 1e1ad5c | 2014-05-28 15:21:14 +0000 | [diff] [blame] | 327 | ClangTool Tool(Compilations, InputFiles); |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 328 | clang::tidy::ClangTidyContext Context(OptionsProvider); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 329 | ClangTidyDiagnosticConsumer DiagConsumer(Context); |
Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 330 | |
| 331 | Tool.setDiagnosticConsumer(&DiagConsumer); |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 332 | |
| 333 | class ActionFactory : public FrontendActionFactory { |
| 334 | public: |
Benjamin Kramer | 6e91424 | 2014-07-24 10:23:33 +0000 | [diff] [blame] | 335 | ActionFactory(ClangTidyContext &Context) : ConsumerFactory(Context) {} |
| 336 | FrontendAction *create() override { return new Action(&ConsumerFactory); } |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 337 | |
| 338 | private: |
| 339 | class Action : public ASTFrontendAction { |
| 340 | public: |
| 341 | Action(ClangTidyASTConsumerFactory *Factory) : Factory(Factory) {} |
David Blaikie | 680c4c8 | 2014-08-10 19:56:59 +0000 | [diff] [blame] | 342 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler, |
| 343 | StringRef File) override { |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 344 | return Factory->CreateASTConsumer(Compiler, File); |
| 345 | } |
| 346 | |
| 347 | private: |
| 348 | ClangTidyASTConsumerFactory *Factory; |
| 349 | }; |
| 350 | |
Benjamin Kramer | 6e91424 | 2014-07-24 10:23:33 +0000 | [diff] [blame] | 351 | ClangTidyASTConsumerFactory ConsumerFactory; |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 352 | }; |
| 353 | |
Benjamin Kramer | 6e91424 | 2014-07-24 10:23:33 +0000 | [diff] [blame] | 354 | ActionFactory Factory(Context); |
| 355 | Tool.run(&Factory); |
Alexander Kornienko | 826b5ad | 2014-05-09 12:24:09 +0000 | [diff] [blame] | 356 | *Errors = Context.getErrors(); |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 357 | return Context.getStats(); |
Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Alexander Kornienko | 826b5ad | 2014-05-09 12:24:09 +0000 | [diff] [blame] | 360 | void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix) { |
Alexander Kornienko | 38d81b4 | 2014-03-27 10:24:11 +0000 | [diff] [blame] | 361 | ErrorReporter Reporter(Fix); |
Alexander Kornienko | 742790c | 2014-07-02 15:05:04 +0000 | [diff] [blame] | 362 | for (const ClangTidyError &Error : Errors) |
| 363 | Reporter.reportDiagnostic(Error); |
Alexander Kornienko | 38d81b4 | 2014-03-27 10:24:11 +0000 | [diff] [blame] | 364 | Reporter.Finish(); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 367 | } // namespace tidy |
| 368 | } // namespace clang |