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