Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [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 | [diff] [blame] | 25 | #include "clang/Frontend/ASTConsumers.h" |
| 26 | #include "clang/Frontend/CompilerInstance.h" |
| 27 | #include "clang/Frontend/FrontendActions.h" |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 | [diff] [blame] | 28 | #include "clang/Frontend/MultiplexConsumer.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [diff] [blame] | 29 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 | [diff] [blame^] | 30 | #include "clang/Lex/PPCallbacks.h" |
| 31 | #include "clang/Lex/Preprocessor.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [diff] [blame] | 32 | #include "clang/Rewrite/Frontend/FixItRewriter.h" |
| 33 | #include "clang/Rewrite/Frontend/FrontendActions.h" |
Alexander Kornienko | d1199cb | 2014-01-03 17:24:20 | [diff] [blame] | 34 | #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [diff] [blame] | 35 | #include "clang/Tooling/Refactoring.h" |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 | [diff] [blame^] | 36 | #include "clang/Tooling/Tooling.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [diff] [blame] | 37 | #include "llvm/Support/Path.h" |
| 38 | #include "llvm/Support/Signals.h" |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 | [diff] [blame] | 39 | #include <algorithm> |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [diff] [blame] | 40 | #include <vector> |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [diff] [blame] | 41 | |
| 42 | using namespace clang::ast_matchers; |
| 43 | using namespace clang::driver; |
| 44 | using namespace clang::tooling; |
| 45 | using namespace llvm; |
| 46 | |
| 47 | namespace clang { |
| 48 | namespace tidy { |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 | [diff] [blame] | 49 | |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [diff] [blame] | 50 | namespace { |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 | [diff] [blame] | 51 | static const char *AnalyzerCheckerNamePrefix = "clang-analyzer-"; |
Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 | [diff] [blame] | 52 | |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 | [diff] [blame] | 53 | static StringRef StaticAnalyzerCheckers[] = { |
| 54 | #define GET_CHECKERS |
| 55 | #define CHECKER(FULLNAME, CLASS, DESCFILE, HELPTEXT, GROUPINDEX, HIDDEN) \ |
| 56 | FULLNAME, |
NAKAMURA Takumi | 321b7d3 | 2014-01-03 10:24:51 | [diff] [blame] | 57 | #include "../../../lib/StaticAnalyzer/Checkers/Checkers.inc" |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 | [diff] [blame] | 58 | #undef CHECKER |
| 59 | #undef GET_CHECKERS |
| 60 | }; |
| 61 | |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 | [diff] [blame] | 62 | } // namespace |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 | [diff] [blame] | 63 | |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 | [diff] [blame] | 64 | ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory( |
| 65 | StringRef EnableChecksRegex, StringRef DisableChecksRegex, |
| 66 | ClangTidyContext &Context) |
| 67 | : Filter(EnableChecksRegex, DisableChecksRegex), Context(Context), |
| 68 | CheckFactories(new ClangTidyCheckFactories) { |
| 69 | for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin(), |
| 70 | E = ClangTidyModuleRegistry::end(); |
| 71 | I != E; ++I) { |
| 72 | OwningPtr<ClangTidyModule> Module(I->instantiate()); |
| 73 | Module->addCheckFactories(*CheckFactories); |
| 74 | } |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 | [diff] [blame] | 75 | |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 | [diff] [blame] | 76 | CheckFactories->createChecks(Filter, Checks); |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 | [diff] [blame] | 77 | |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 | [diff] [blame] | 78 | for (SmallVectorImpl<ClangTidyCheck *>::iterator I = Checks.begin(), |
| 79 | E = Checks.end(); |
| 80 | I != E; ++I) { |
| 81 | (*I)->setContext(&Context); |
| 82 | (*I)->registerMatchers(&Finder); |
| 83 | } |
| 84 | } |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 | [diff] [blame] | 85 | |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 | [diff] [blame] | 86 | ClangTidyASTConsumerFactory::~ClangTidyASTConsumerFactory() { |
| 87 | for (SmallVectorImpl<ClangTidyCheck *>::iterator I = Checks.begin(), |
| 88 | E = Checks.end(); |
| 89 | I != E; ++I) |
| 90 | delete *I; |
| 91 | } |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 | [diff] [blame] | 92 | |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 | [diff] [blame] | 93 | clang::ASTConsumer *ClangTidyASTConsumerFactory::CreateASTConsumer( |
| 94 | clang::CompilerInstance &Compiler, StringRef File) { |
| 95 | // FIXME: Move this to a separate method, so that CreateASTConsumer doesn't |
| 96 | // modify Compiler. |
| 97 | Context.setSourceManager(&Compiler.getSourceManager()); |
| 98 | for (SmallVectorImpl<ClangTidyCheck *>::iterator I = Checks.begin(), |
| 99 | E = Checks.end(); |
| 100 | I != E; ++I) |
| 101 | (*I)->registerPPCallbacks(Compiler); |
| 102 | |
| 103 | AnalyzerOptionsRef Options = Compiler.getAnalyzerOpts(); |
| 104 | Options->CheckersControlList = getCheckersControlList(); |
| 105 | Options->AnalysisStoreOpt = RegionStoreModel; |
| 106 | Options->AnalysisDiagOpt = PD_TEXT; |
| 107 | Options->AnalyzeNestedBlocks = true; |
| 108 | Options->eagerlyAssumeBinOpBifurcation = true; |
| 109 | ASTConsumer *Consumers[] = { |
| 110 | Finder.newASTConsumer(), |
| 111 | ento::CreateAnalysisConsumer(Compiler.getPreprocessor(), |
| 112 | Compiler.getFrontendOpts().OutputFile, Options, |
| 113 | Compiler.getFrontendOpts().Plugins) |
| 114 | }; |
| 115 | return new MultiplexConsumer(Consumers); |
| 116 | } |
| 117 | |
| 118 | std::vector<std::string> ClangTidyASTConsumerFactory::getCheckNames() { |
| 119 | std::vector<std::string> CheckNames; |
| 120 | for (ClangTidyCheckFactories::FactoryMap::const_iterator |
| 121 | I = CheckFactories->begin(), |
| 122 | E = CheckFactories->end(); |
| 123 | I != E; ++I) { |
| 124 | if (Filter.IsCheckEnabled(I->first)) |
| 125 | CheckNames.push_back(I->first); |
| 126 | } |
| 127 | |
| 128 | CheckersList AnalyzerChecks = getCheckersControlList(); |
| 129 | for (CheckersList::const_iterator I = AnalyzerChecks.begin(), |
| 130 | E = AnalyzerChecks.end(); |
| 131 | I != E; ++I) |
| 132 | CheckNames.push_back(AnalyzerCheckerNamePrefix + I->first); |
| 133 | |
| 134 | std::sort(CheckNames.begin(), CheckNames.end()); |
| 135 | return CheckNames; |
| 136 | } |
| 137 | |
| 138 | ClangTidyASTConsumerFactory::CheckersList |
| 139 | ClangTidyASTConsumerFactory::getCheckersControlList() { |
| 140 | CheckersList List; |
| 141 | ArrayRef<StringRef> Checkers(StaticAnalyzerCheckers); |
| 142 | |
| 143 | bool AnalyzerChecksEnabled = false; |
| 144 | for (unsigned i = 0; i < Checkers.size(); ++i) { |
| 145 | std::string Checker((AnalyzerCheckerNamePrefix + Checkers[i]).str()); |
| 146 | AnalyzerChecksEnabled |= |
| 147 | Filter.IsCheckEnabled(Checker) && !Checkers[i].startswith("debug"); |
| 148 | } |
| 149 | |
| 150 | if (AnalyzerChecksEnabled) { |
| 151 | // Run our regex against all possible static analyzer checkers. Note that |
| 152 | // debug checkers print values / run programs to visualize the CFG and are |
| 153 | // thus not applicable to clang-tidy in general. |
| 154 | // |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 | [diff] [blame] | 155 | // Always add all core checkers if any other static analyzer checks are |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 | [diff] [blame] | 156 | // enabled. This is currently necessary, as other path sensitive checks |
| 157 | // rely on the core checkers. |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 | [diff] [blame] | 158 | for (unsigned i = 0; i < Checkers.size(); ++i) { |
| 159 | std::string Checker((AnalyzerCheckerNamePrefix + Checkers[i]).str()); |
| 160 | |
| 161 | if (Checkers[i].startswith("core") || |
| 162 | (!Checkers[i].startswith("debug") && Filter.IsCheckEnabled(Checker))) |
| 163 | List.push_back(std::make_pair(Checkers[i], true)); |
| 164 | } |
| 165 | } |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 | [diff] [blame] | 166 | return List; |
| 167 | } |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [diff] [blame] | 168 | |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 | [diff] [blame] | 169 | ChecksFilter::ChecksFilter(StringRef EnableChecksRegex, |
| 170 | StringRef DisableChecksRegex) |
| 171 | : EnableChecks(EnableChecksRegex), DisableChecks(DisableChecksRegex) {} |
| 172 | |
| 173 | bool ChecksFilter::IsCheckEnabled(StringRef Name) { |
| 174 | return EnableChecks.match(Name) && !DisableChecks.match(Name); |
| 175 | } |
| 176 | |
Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 | [diff] [blame] | 177 | ClangTidyMessage::ClangTidyMessage(StringRef Message) : Message(Message) {} |
| 178 | |
| 179 | ClangTidyMessage::ClangTidyMessage(StringRef Message, |
| 180 | const SourceManager &Sources, |
| 181 | SourceLocation Loc) |
| 182 | : Message(Message) { |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [diff] [blame] | 183 | FilePath = Sources.getFilename(Loc); |
| 184 | FileOffset = Sources.getFileOffset(Loc); |
| 185 | } |
| 186 | |
Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 | [diff] [blame] | 187 | ClangTidyError::ClangTidyError(const ClangTidyMessage &Message) |
| 188 | : Message(Message) {} |
| 189 | |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [diff] [blame] | 190 | DiagnosticBuilder ClangTidyContext::Diag(SourceLocation Loc, |
| 191 | StringRef Message) { |
| 192 | return DiagEngine->Report( |
| 193 | Loc, DiagEngine->getCustomDiagID(DiagnosticsEngine::Warning, Message)); |
| 194 | } |
| 195 | |
| 196 | void ClangTidyContext::setDiagnosticsEngine(DiagnosticsEngine *Engine) { |
| 197 | DiagEngine = Engine; |
| 198 | } |
| 199 | |
| 200 | void ClangTidyContext::setSourceManager(SourceManager *SourceMgr) { |
| 201 | DiagEngine->setSourceManager(SourceMgr); |
| 202 | } |
| 203 | |
| 204 | /// \brief Store a \c ClangTidyError. |
| 205 | void ClangTidyContext::storeError(const ClangTidyError &Error) { |
| 206 | Errors->push_back(Error); |
| 207 | } |
| 208 | |
| 209 | void ClangTidyCheck::run(const ast_matchers::MatchFinder::MatchResult &Result) { |
| 210 | Context->setSourceManager(Result.SourceManager); |
| 211 | check(Result); |
| 212 | } |
| 213 | |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 | [diff] [blame] | 214 | std::vector<std::string> getCheckNames(StringRef EnableChecksRegex, |
| 215 | StringRef DisableChecksRegex) { |
| 216 | SmallVector<ClangTidyError, 8> Errors; |
| 217 | clang::tidy::ClangTidyContext Context(&Errors); |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 | [diff] [blame] | 218 | ClangTidyASTConsumerFactory Factory(EnableChecksRegex, DisableChecksRegex, |
| 219 | Context); |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 | [diff] [blame] | 220 | return Factory.getCheckNames(); |
| 221 | } |
| 222 | |
| 223 | void runClangTidy(StringRef EnableChecksRegex, StringRef DisableChecksRegex, |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [diff] [blame] | 224 | const tooling::CompilationDatabase &Compilations, |
| 225 | ArrayRef<std::string> Ranges, |
| 226 | SmallVectorImpl<ClangTidyError> *Errors) { |
| 227 | // FIXME: Ranges are currently full files. Support selecting specific |
| 228 | // (line-)ranges. |
| 229 | ClangTool Tool(Compilations, Ranges); |
| 230 | clang::tidy::ClangTidyContext Context(Errors); |
| 231 | ClangTidyDiagnosticConsumer DiagConsumer(Context); |
Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 | [diff] [blame] | 232 | |
| 233 | Tool.setDiagnosticConsumer(&DiagConsumer); |
Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 | [diff] [blame] | 234 | |
| 235 | class ActionFactory : public FrontendActionFactory { |
| 236 | public: |
| 237 | ActionFactory(ClangTidyASTConsumerFactory *ConsumerFactory) |
| 238 | : ConsumerFactory(ConsumerFactory) {} |
| 239 | FrontendAction *create() LLVM_OVERRIDE { |
| 240 | return new Action(ConsumerFactory); |
| 241 | } |
| 242 | |
| 243 | private: |
| 244 | class Action : public ASTFrontendAction { |
| 245 | public: |
| 246 | Action(ClangTidyASTConsumerFactory *Factory) : Factory(Factory) {} |
| 247 | ASTConsumer *CreateASTConsumer(CompilerInstance &Compiler, |
| 248 | StringRef File) LLVM_OVERRIDE { |
| 249 | return Factory->CreateASTConsumer(Compiler, File); |
| 250 | } |
| 251 | |
| 252 | private: |
| 253 | ClangTidyASTConsumerFactory *Factory; |
| 254 | }; |
| 255 | |
| 256 | ClangTidyASTConsumerFactory *ConsumerFactory; |
| 257 | }; |
| 258 | |
| 259 | Tool.run(new ActionFactory(new ClangTidyASTConsumerFactory( |
| 260 | EnableChecksRegex, DisableChecksRegex, Context))); |
Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static void reportDiagnostic(const ClangTidyMessage &Message, |
| 264 | SourceManager &SourceMgr, |
| 265 | DiagnosticsEngine::Level Level, |
| 266 | DiagnosticsEngine &Diags) { |
| 267 | SourceLocation Loc; |
| 268 | if (!Message.FilePath.empty()) { |
| 269 | const FileEntry *File = |
| 270 | SourceMgr.getFileManager().getFile(Message.FilePath); |
| 271 | FileID ID = SourceMgr.createFileID(File, SourceLocation(), SrcMgr::C_User); |
| 272 | Loc = SourceMgr.getLocForStartOfFile(ID); |
| 273 | Loc = Loc.getLocWithOffset(Message.FileOffset); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [diff] [blame] | 274 | } |
Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 | [diff] [blame] | 275 | Diags.Report(Loc, Diags.getCustomDiagID(Level, Message.Message)); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | void handleErrors(SmallVectorImpl<ClangTidyError> &Errors, bool Fix) { |
| 279 | FileManager Files((FileSystemOptions())); |
| 280 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 281 | DiagnosticConsumer *DiagPrinter = |
| 282 | new TextDiagnosticPrinter(llvm::outs(), &*DiagOpts); |
| 283 | DiagnosticsEngine Diags(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 284 | &*DiagOpts, DiagPrinter); |
| 285 | DiagPrinter->BeginSourceFile(LangOptions()); |
| 286 | SourceManager SourceMgr(Diags, Files); |
| 287 | Rewriter Rewrite(SourceMgr, LangOptions()); |
| 288 | for (SmallVectorImpl<ClangTidyError>::iterator I = Errors.begin(), |
| 289 | E = Errors.end(); |
| 290 | I != E; ++I) { |
Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 | [diff] [blame] | 291 | reportDiagnostic(I->Message, SourceMgr, DiagnosticsEngine::Warning, Diags); |
| 292 | for (unsigned i = 0, e = I->Notes.size(); i != e; ++i) { |
| 293 | reportDiagnostic(I->Notes[i], SourceMgr, DiagnosticsEngine::Note, Diags); |
| 294 | } |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [diff] [blame] | 295 | tooling::applyAllReplacements(I->Fix, Rewrite); |
| 296 | } |
| 297 | // FIXME: Run clang-format on changes. |
| 298 | if (Fix) |
| 299 | Rewrite.overwriteChangedFiles(); |
| 300 | } |
| 301 | |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 | [diff] [blame] | 302 | } // namespace tidy |
| 303 | } // namespace clang |