clang-tools 8.0.0

ClangTidy.cpp
Go to the documentation of this file.
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 "ClangTidyProfiling.h"
22 #include "clang/AST/ASTConsumer.h"
23 #include "clang/AST/ASTContext.h"
24 #include "clang/AST/Decl.h"
25 #include "clang/ASTMatchers/ASTMatchFinder.h"
26 #include "clang/Config/config.h"
27 #include "clang/Format/Format.h"
28 #include "clang/Frontend/ASTConsumers.h"
29 #include "clang/Frontend/CompilerInstance.h"
30 #include "clang/Frontend/FrontendActions.h"
31 #include "clang/Frontend/FrontendDiagnostic.h"
32 #include "clang/Frontend/MultiplexConsumer.h"
33 #include "clang/Frontend/TextDiagnosticPrinter.h"
34 #include "clang/Lex/PPCallbacks.h"
35 #include "clang/Lex/Preprocessor.h"
36 #include "clang/Rewrite/Frontend/FixItRewriter.h"
37 #include "clang/Rewrite/Frontend/FrontendActions.h"
38 #if CLANG_ENABLE_STATIC_ANALYZER
39 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
40 #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
41 #endif // CLANG_ENABLE_STATIC_ANALYZER
42 #include "clang/Tooling/DiagnosticsYaml.h"
43 #include "clang/Tooling/Refactoring.h"
44 #include "clang/Tooling/ReplacementsYaml.h"
45 #include "clang/Tooling/Tooling.h"
46 #include "llvm/Support/Process.h"
47 #include "llvm/Support/Signals.h"
48 #include <algorithm>
49 #include <utility>
50
51 using namespace clang::ast_matchers;
52 using namespace clang::driver;
53 using namespace clang::tooling;
54 using namespace llvm;
55
56 LLVM_INSTANTIATE_REGISTRY(clang::tidy::ClangTidyModuleRegistry)
57
58 namespace clang {
59 namespace tidy {
60
61 namespace {
62 #if CLANG_ENABLE_STATIC_ANALYZER
63 static const char *AnalyzerCheckNamePrefix = "clang-analyzer-";
64
65 class AnalyzerDiagnosticConsumer : public ento::PathDiagnosticConsumer {
66 public:
67 AnalyzerDiagnosticConsumer(ClangTidyContext &Context) : Context(Context) {}
68
69 void FlushDiagnosticsImpl(std::vector<const ento::PathDiagnostic *> &Diags,
70 FilesMade *filesMade) override {
71 for (const ento::PathDiagnostic *PD : Diags) {
72 SmallString<64> CheckName(AnalyzerCheckNamePrefix);
73 CheckName += PD->getCheckName();
74 Context.diag(CheckName, PD->getLocation().asLocation(),
75 PD->getShortDescription())
76 << PD->path.back()->getRanges();
77
78 for (const auto &DiagPiece :
79 PD->path.flatten(/*ShouldFlattenMacros=*/true)) {
80 Context.diag(CheckName, DiagPiece->getLocation().asLocation(),
81 DiagPiece->getString(), DiagnosticIDs::Note)
82 << DiagPiece->getRanges();
83 }
84 }
85 }
86
87 StringRef getName() const override { return "ClangTidyDiags"; }
88 bool supportsLogicalOpControlFlow() const override { return true; }
89 bool supportsCrossFileDiagnostics() const override { return true; }
90
91 private:
92 ClangTidyContext &Context;
93 };
94 #endif // CLANG_ENABLE_STATIC_ANALYZER
95
96 class ErrorReporter {
97 public:
98 ErrorReporter(ClangTidyContext &Context, bool ApplyFixes,
99 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS)
100 : Files(FileSystemOptions(), BaseFS), DiagOpts(new DiagnosticOptions()),
101 DiagPrinter(new TextDiagnosticPrinter(llvm::outs(), &*DiagOpts)),
102 Diags(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts,
103 DiagPrinter),
104 SourceMgr(Diags, Files), Context(Context), ApplyFixes(ApplyFixes),
105 TotalFixes(0), AppliedFixes(0), WarningsAsErrors(0) {
106 DiagOpts->ShowColors = llvm::sys::Process::StandardOutHasColors();
107 DiagPrinter->BeginSourceFile(LangOpts);
108 }
109
110 SourceManager &getSourceManager() { return SourceMgr; }
111
112 void reportDiagnostic(const ClangTidyError &Error) {
113 const tooling::DiagnosticMessage &Message = Error.Message;
114 SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset);
115 // Contains a pair for each attempted fix: location and whether the fix was
116 // applied successfully.
117 SmallVector<std::pair<SourceLocation, bool>, 4> FixLocations;
118 {
119 auto Level = static_cast<DiagnosticsEngine::Level>(Error.DiagLevel);
120 std::string Name = Error.DiagnosticName;
121 if (Error.IsWarningAsError) {
122 Name += ",-warnings-as-errors";
123 Level = DiagnosticsEngine::Error;
124 WarningsAsErrors++;
125 }
126 auto Diag = Diags.Report(Loc, Diags.getCustomDiagID(Level, "%0 [%1]"))
127 << Message.Message << Name;
128 for (const auto &FileAndReplacements : Error.Fix) {
129 for (const auto &Repl : FileAndReplacements.second) {
130 SourceLocation FixLoc;
131 ++TotalFixes;
132 bool CanBeApplied = false;
133 if (Repl.isApplicable()) {
134 SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
135 Files.makeAbsolutePath(FixAbsoluteFilePath);
136 if (ApplyFixes) {
137 tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
138 Repl.getLength(),
139 Repl.getReplacementText());
140 Replacements &Replacements = FileReplacements[R.getFilePath()];
141 llvm::Error Err = Replacements.add(R);
142 if (Err) {
143 // FIXME: Implement better conflict handling.
144 llvm::errs() << "Trying to resolve conflict: "
145 << llvm::toString(std::move(Err)) << "\n";
146 unsigned NewOffset =
147 Replacements.getShiftedCodePosition(R.getOffset());
148 unsigned NewLength = Replacements.getShiftedCodePosition(
149 R.getOffset() + R.getLength()) -
150 NewOffset;
151 if (NewLength == R.getLength()) {
152 R = Replacement(R.getFilePath(), NewOffset, NewLength,
153 R.getReplacementText());
154 Replacements = Replacements.merge(tooling::Replacements(R));
155 CanBeApplied = true;
156 ++AppliedFixes;
157 } else {
158 llvm::errs()
159 << "Can't resolve conflict, skipping the replacement.\n";
160 }
161
162 } else {
163 CanBeApplied = true;
164 ++AppliedFixes;
165 }
166 }
167 FixLoc = getLocation(FixAbsoluteFilePath, Repl.getOffset());
168 SourceLocation FixEndLoc =
169 FixLoc.getLocWithOffset(Repl.getLength());
170 // Retrieve the source range for applicable fixes. Macro definitions
171 // on the command line have locations in a virtual buffer and don't
172 // have valid file paths and are therefore not applicable.
173 CharSourceRange Range =
174 CharSourceRange::getCharRange(SourceRange(FixLoc, FixEndLoc));
175 Diag << FixItHint::CreateReplacement(Range,
176 Repl.getReplacementText());
177 }
178
179 if (ApplyFixes)
180 FixLocations.push_back(std::make_pair(FixLoc, CanBeApplied));
181 }
182 }
183 }
184 for (auto Fix : FixLocations) {
185 Diags.Report(Fix.first, Fix.second ? diag::note_fixit_applied
186 : diag::note_fixit_failed);
187 }
188 for (const auto &Note : Error.Notes)
189 reportNote(Note);
190 }
191
192 void Finish() {
193 if (ApplyFixes && TotalFixes > 0) {
194 Rewriter Rewrite(SourceMgr, LangOpts);
195 for (const auto &FileAndReplacements : FileReplacements) {
196 StringRef File = FileAndReplacements.first();
197 llvm::ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
198 SourceMgr.getFileManager().getBufferForFile(File);
199 if (!Buffer) {
200 llvm::errs() << "Can't get buffer for file " << File << ": "
201 << Buffer.getError().message() << "\n";
202 // FIXME: Maybe don't apply fixes for other files as well.
203 continue;
204 }
205 StringRef Code = Buffer.get()->getBuffer();
206 auto Style = format::getStyle(
207 *Context.getOptionsForFile(File).FormatStyle, File, "none");
208 if (!Style) {
209 llvm::errs() << llvm::toString(Style.takeError()) << "\n";
210 continue;
211 }
212 llvm::Expected<tooling::Replacements> Replacements =
213 format::cleanupAroundReplacements(Code, FileAndReplacements.second,
214 *Style);
215 if (!Replacements) {
216 llvm::errs() << llvm::toString(Replacements.takeError()) << "\n";
217 continue;
218 }
219 if (llvm::Expected<tooling::Replacements> FormattedReplacements =
220 format::formatReplacements(Code, *Replacements, *Style)) {
221 Replacements = std::move(FormattedReplacements);
222 if (!Replacements)
223 llvm_unreachable("!Replacements");
224 } else {
225 llvm::errs() << llvm::toString(FormattedReplacements.takeError())
226 << ". Skipping formatting.\n";
227 }
228 if (!tooling::applyAllReplacements(Replacements.get(), Rewrite)) {
229 llvm::errs() << "Can't apply replacements for file " << File << "\n";
230 }
231 }
232 if (Rewrite.overwriteChangedFiles()) {
233 llvm::errs() << "clang-tidy failed to apply suggested fixes.\n";
234 } else {
235 llvm::errs() << "clang-tidy applied " << AppliedFixes << " of "
236 << TotalFixes << " suggested fixes.\n";
237 }
238 }
239 }
240
241 unsigned getWarningsAsErrorsCount() const { return WarningsAsErrors; }
242
243 private:
244 SourceLocation getLocation(StringRef FilePath, unsigned Offset) {
245 if (FilePath.empty())
246 return SourceLocation();
247
248 const FileEntry *File = SourceMgr.getFileManager().getFile(FilePath);
249 FileID ID = SourceMgr.getOrCreateFileID(File, SrcMgr::C_User);
250 return SourceMgr.getLocForStartOfFile(ID).getLocWithOffset(Offset);
251 }
252
253 void reportNote(const tooling::DiagnosticMessage &Message) {
254 SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset);
255 Diags.Report(Loc, Diags.getCustomDiagID(DiagnosticsEngine::Note, "%0"))
256 << Message.Message;
257 }
258
259 FileManager Files;
260 LangOptions LangOpts; // FIXME: use langopts from each original file
261 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
262 DiagnosticConsumer *DiagPrinter;
263 DiagnosticsEngine Diags;
264 SourceManager SourceMgr;
265 llvm::StringMap<Replacements> FileReplacements;
266 ClangTidyContext &Context;
267 bool ApplyFixes;
268 unsigned TotalFixes;
269 unsigned AppliedFixes;
270 unsigned WarningsAsErrors;
271 };
272
273 class ClangTidyASTConsumer : public MultiplexConsumer {
274 public:
275 ClangTidyASTConsumer(std::vector<std::unique_ptr<ASTConsumer>> Consumers,
276 std::unique_ptr<ClangTidyProfiling> Profiling,
277 std::unique_ptr<ast_matchers::MatchFinder> Finder,
278 std::vector<std::unique_ptr<ClangTidyCheck>> Checks)
279 : MultiplexConsumer(std::move(Consumers)),
280 Profiling(std::move(Profiling)), Finder(std::move(Finder)),
281 Checks(std::move(Checks)) {}
282
283 private:
284 // Destructor order matters! Profiling must be destructed last.
285 // Or at least after Finder.
286 std::unique_ptr<ClangTidyProfiling> Profiling;
287 std::unique_ptr<ast_matchers::MatchFinder> Finder;
288 std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
289 };
290
291 } // namespace
292
293 ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory(
294 ClangTidyContext &Context)
295 : Context(Context), CheckFactories(new ClangTidyCheckFactories) {
296 for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin(),
297 E = ClangTidyModuleRegistry::end();
298 I != E; ++I) {
299 std::unique_ptr<ClangTidyModule> Module(I->instantiate());
300 Module->addCheckFactories(*CheckFactories);
301 }
302 }
303
304 #if CLANG_ENABLE_STATIC_ANALYZER
305 static void setStaticAnalyzerCheckerOpts(const ClangTidyOptions &Opts,
306 AnalyzerOptionsRef AnalyzerOptions) {
307 StringRef AnalyzerPrefix(AnalyzerCheckNamePrefix);
308 for (const auto &Opt : Opts.CheckOptions) {
309 StringRef OptName(Opt.first);
310 if (!OptName.startswith(AnalyzerPrefix))
311 continue;
312 AnalyzerOptions->Config[OptName.substr(AnalyzerPrefix.size())] = Opt.second;
313 }
314 }
315
316 typedef std::vector<std::pair<std::string, bool>> CheckersList;
317
318 static CheckersList getCheckersControlList(ClangTidyContext &Context,
319 bool IncludeExperimental) {
320 CheckersList List;
321
322 const auto &RegisteredCheckers =
323 AnalyzerOptions::getRegisteredCheckers(IncludeExperimental);
324 bool AnalyzerChecksEnabled = false;
325 for (StringRef CheckName : RegisteredCheckers) {
326 std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
327 AnalyzerChecksEnabled |= Context.isCheckEnabled(ClangTidyCheckName);
328 }
329
330 if (!AnalyzerChecksEnabled)
331 return List;
332
333 // List all static analyzer checkers that our filter enables.
334 //
335 // Always add all core checkers if any other static analyzer check is enabled.
336 // This is currently necessary, as other path sensitive checks rely on the
337 // core checkers.
338 for (StringRef CheckName : RegisteredCheckers) {
339 std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
340
341 if (CheckName.startswith("core") ||
342 Context.isCheckEnabled(ClangTidyCheckName)) {
343 List.emplace_back(CheckName, true);
344 }
345 }
346 return List;
347 }
348 #endif // CLANG_ENABLE_STATIC_ANALYZER
349
350 std::unique_ptr<clang::ASTConsumer>
351 ClangTidyASTConsumerFactory::CreateASTConsumer(
352 clang::CompilerInstance &Compiler, StringRef File) {
353 // FIXME: Move this to a separate method, so that CreateASTConsumer doesn't
354 // modify Compiler.
355 Context.setSourceManager(&Compiler.getSourceManager());
356 Context.setCurrentFile(File);
357 Context.setASTContext(&Compiler.getASTContext());
358
359 auto WorkingDir = Compiler.getSourceManager()
360 .getFileManager()
361 .getVirtualFileSystem()
362 ->getCurrentWorkingDirectory();
363 if (WorkingDir)
364 Context.setCurrentBuildDirectory(WorkingDir.get());
365
366 std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
367 CheckFactories->createChecks(&Context, Checks);
368
369 ast_matchers::MatchFinder::MatchFinderOptions FinderOptions;
370
371 std::unique_ptr<ClangTidyProfiling> Profiling;
372 if (Context.getEnableProfiling()) {
373 Profiling = llvm::make_unique<ClangTidyProfiling>(
374 Context.getProfileStorageParams());
375 FinderOptions.CheckProfiling.emplace(Profiling->Records);
376 }
377
378 std::unique_ptr<ast_matchers::MatchFinder> Finder(
379 new ast_matchers::MatchFinder(std::move(FinderOptions)));
380
381 for (auto &Check : Checks) {
382 Check->registerMatchers(&*Finder);
383 Check->registerPPCallbacks(Compiler);
384 }
385
386 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
387 if (!Checks.empty())
388 Consumers.push_back(Finder->newASTConsumer());
389
390 #if CLANG_ENABLE_STATIC_ANALYZER
391 AnalyzerOptionsRef AnalyzerOptions = Compiler.getAnalyzerOpts();
392 AnalyzerOptions->CheckersControlList =
393 getCheckersControlList(Context, Context.canEnableAnalyzerAlphaCheckers());
394 if (!AnalyzerOptions->CheckersControlList.empty()) {
395 setStaticAnalyzerCheckerOpts(Context.getOptions(), AnalyzerOptions);
396 AnalyzerOptions->AnalysisStoreOpt = RegionStoreModel;
397 AnalyzerOptions->AnalysisDiagOpt = PD_NONE;
398 AnalyzerOptions->AnalyzeNestedBlocks = true;
399 AnalyzerOptions->eagerlyAssumeBinOpBifurcation = true;
400 std::unique_ptr<ento::AnalysisASTConsumer> AnalysisConsumer =
401 ento::CreateAnalysisConsumer(Compiler);
402 AnalysisConsumer->AddDiagnosticConsumer(
403 new AnalyzerDiagnosticConsumer(Context));
404 Consumers.push_back(std::move(AnalysisConsumer));
405 }
406 #endif // CLANG_ENABLE_STATIC_ANALYZER
407 return llvm::make_unique<ClangTidyASTConsumer>(
408 std::move(Consumers), std::move(Profiling), std::move(Finder),
409 std::move(Checks));
410 }
411
412 std::vector<std::string> ClangTidyASTConsumerFactory::getCheckNames() {
413 std::vector<std::string> CheckNames;
414 for (const auto &CheckFactory : *CheckFactories) {
415 if (Context.isCheckEnabled(CheckFactory.first))
416 CheckNames.push_back(CheckFactory.first);
417 }
418
419 #if CLANG_ENABLE_STATIC_ANALYZER
420 for (const auto &AnalyzerCheck : getCheckersControlList(
421 Context, Context.canEnableAnalyzerAlphaCheckers()))
422 CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first);
423 #endif // CLANG_ENABLE_STATIC_ANALYZER
424
425 std::sort(CheckNames.begin(), CheckNames.end());
426 return CheckNames;
427 }
428
429 ClangTidyOptions::OptionMap ClangTidyASTConsumerFactory::getCheckOptions() {
430 ClangTidyOptions::OptionMap Options;
431 std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
432 CheckFactories->createChecks(&Context, Checks);
433 for (const auto &Check : Checks)
434 Check->storeOptions(Options);
435 return Options;
436 }
437
438 DiagnosticBuilder ClangTidyCheck::diag(SourceLocation Loc, StringRef Message,
439 DiagnosticIDs::Level Level) {
440 return Context->diag(CheckName, Loc, Message, Level);
441 }
442
443 void ClangTidyCheck::run(const ast_matchers::MatchFinder::MatchResult &Result) {
444 // For historical reasons, checks don't implement the MatchFinder run()
445 // callback directly. We keep the run()/check() distinction to avoid interface
446 // churn, and to allow us to add cross-cutting logic in the future.
447 check(Result);
448 }
449
450 OptionsView::OptionsView(StringRef CheckName,
451 const ClangTidyOptions::OptionMap &CheckOptions)
452 : NamePrefix(CheckName.str() + "."), CheckOptions(CheckOptions) {}
453
454 std::string OptionsView::get(StringRef LocalName, StringRef Default) const {
455 const auto &Iter = CheckOptions.find(NamePrefix + LocalName.str());
456 if (Iter != CheckOptions.end())
457 return Iter->second;
458 return Default;
459 }
460
461 std::string OptionsView::getLocalOrGlobal(StringRef LocalName,
462 StringRef Default) const {
463 auto Iter = CheckOptions.find(NamePrefix + LocalName.str());
464 if (Iter != CheckOptions.end())
465 return Iter->second;
466 // Fallback to global setting, if present.
467 Iter = CheckOptions.find(LocalName.str());
468 if (Iter != CheckOptions.end())
469 return Iter->second;
470 return Default;
471 }
472
473 void OptionsView::store(ClangTidyOptions::OptionMap &Options,
474 StringRef LocalName, StringRef Value) const {
475 Options[NamePrefix + LocalName.str()] = Value;
476 }
477
478 void OptionsView::store(ClangTidyOptions::OptionMap &Options,
479 StringRef LocalName, int64_t Value) const {
480 store(Options, LocalName, llvm::itostr(Value));
481 }
482
483 std::vector<std::string>
484 getCheckNames(const ClangTidyOptions &Options,
485 bool AllowEnablingAnalyzerAlphaCheckers) {
486 clang::tidy::ClangTidyContext Context(
487 llvm::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
488 Options),
489 AllowEnablingAnalyzerAlphaCheckers);
490 ClangTidyASTConsumerFactory Factory(Context);
491 return Factory.getCheckNames();
492 }
493
494 ClangTidyOptions::OptionMap
495 getCheckOptions(const ClangTidyOptions &Options,
496 bool AllowEnablingAnalyzerAlphaCheckers) {
497 clang::tidy::ClangTidyContext Context(
498 llvm::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
499 Options),
500 AllowEnablingAnalyzerAlphaCheckers);
501 ClangTidyASTConsumerFactory Factory(Context);
502 return Factory.getCheckOptions();
503 }
504
505 std::vector<ClangTidyError>
506 runClangTidy(clang::tidy::ClangTidyContext &Context,
507 const CompilationDatabase &Compilations,
508 ArrayRef<std::string> InputFiles,
509 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
510 bool EnableCheckProfile, llvm::StringRef StoreCheckProfile) {
511 ClangTool Tool(Compilations, InputFiles,
512 std::make_shared<PCHContainerOperations>(), BaseFS);
513
514 // Add extra arguments passed by the clang-tidy command-line.
515 ArgumentsAdjuster PerFileExtraArgumentsInserter =
516 [&Context](const CommandLineArguments &Args, StringRef Filename) {
517 ClangTidyOptions Opts = Context.getOptionsForFile(Filename);
518 CommandLineArguments AdjustedArgs = Args;
519 if (Opts.ExtraArgsBefore) {
520 auto I = AdjustedArgs.begin();
521 if (I != AdjustedArgs.end() && !StringRef(*I).startswith("-"))
522 ++I; // Skip compiler binary name, if it is there.
523 AdjustedArgs.insert(I, Opts.ExtraArgsBefore->begin(),
524 Opts.ExtraArgsBefore->end());
525 }
526 if (Opts.ExtraArgs)
527 AdjustedArgs.insert(AdjustedArgs.end(), Opts.ExtraArgs->begin(),
528 Opts.ExtraArgs->end());
529 return AdjustedArgs;
530 };
531
532 Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
533 Tool.appendArgumentsAdjuster(getStripPluginsAdjuster());
534 Context.setEnableProfiling(EnableCheckProfile);
535 Context.setProfileStoragePrefix(StoreCheckProfile);
536
537 ClangTidyDiagnosticConsumer DiagConsumer(Context);
538 DiagnosticsEngine DE(new DiagnosticIDs(), new DiagnosticOptions(),
539 &DiagConsumer, /*ShouldOwnClient=*/false);
540 Context.setDiagnosticsEngine(&DE);
541 Tool.setDiagnosticConsumer(&DiagConsumer);
542
543 class ActionFactory : public FrontendActionFactory {
544 public:
545 ActionFactory(ClangTidyContext &Context) : ConsumerFactory(Context) {}
546 FrontendAction *create() override { return new Action(&ConsumerFactory); }
547
548 bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
549 FileManager *Files,
550 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
551 DiagnosticConsumer *DiagConsumer) override {
552 // Explicitly set ProgramAction to RunAnalysis to make the preprocessor
553 // define __clang_analyzer__ macro. The frontend analyzer action will not
554 // be called here.
555 Invocation->getFrontendOpts().ProgramAction = frontend::RunAnalysis;
556 return FrontendActionFactory::runInvocation(
557 Invocation, Files, PCHContainerOps, DiagConsumer);
558 }
559
560 private:
561 class Action : public ASTFrontendAction {
562 public:
563 Action(ClangTidyASTConsumerFactory *Factory) : Factory(Factory) {}
564 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
565 StringRef File) override {
566 return Factory->CreateASTConsumer(Compiler, File);
567 }
568
569 private:
570 ClangTidyASTConsumerFactory *Factory;
571 };
572
573 ClangTidyASTConsumerFactory ConsumerFactory;
574 };
575
576 ActionFactory Factory(Context);
577 Tool.run(&Factory);
578 return DiagConsumer.take();
579 }
580
581 void handleErrors(llvm::ArrayRef<ClangTidyError> Errors,
582 ClangTidyContext &Context, bool Fix,
583 unsigned &WarningsAsErrorsCount,
584 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
585 ErrorReporter Reporter(Context, Fix, BaseFS);
586 llvm::vfs::FileSystem &FileSystem =
587 *Reporter.getSourceManager().getFileManager().getVirtualFileSystem();
588 auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
589 if (!InitialWorkingDir)
590 llvm::report_fatal_error("Cannot get current working path.");
591
592 for (const ClangTidyError &Error : Errors) {
593 if (!Error.BuildDirectory.empty()) {
594 // By default, the working directory of file system is the current
595 // clang-tidy running directory.
596 //
597 // Change the directory to the one used during the analysis.
598 FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
599 }
600 Reporter.reportDiagnostic(Error);
601 // Return to the initial directory to correctly resolve next Error.
602 FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
603 }
604 Reporter.Finish();
605 WarningsAsErrorsCount += Reporter.getWarningsAsErrorsCount();
606 }
607
608 void exportReplacements(const llvm::StringRef MainFilePath,
609 const std::vector<ClangTidyError> &Errors,
610 raw_ostream &OS) {
611 TranslationUnitDiagnostics TUD;
612 TUD.MainSourceFile = MainFilePath;
613 for (const auto &Error : Errors) {
614 tooling::Diagnostic Diag = Error;
615 TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
616 }
617
618 yaml::Output YAML(OS);
619 YAML << TUD;
620 }
621
622 } // namespace tidy
623 } // namespace clang
DiagnosticConsumer
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::tidy::ClangTidyASTConsumerFactory::getCheckNames
std::vector< std::string > getCheckNames()
Get the list of enabled checks.
Definition: ClangTidy.cpp:412
clang::ast_matchers
Definition: AbseilMatcher.h:15
clang::tidy::OptionsView::getLocalOrGlobal
std::string getLocalOrGlobal(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
Definition: ClangTidy.cpp:461
clang::tidy::ClangTidyOptions::ExtraArgs
llvm::Optional< ArgList > ExtraArgs
Add extra compilation arguments to the end of the list.
Definition: ClangTidyOptions.h:106
clang::tidy::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidy.cpp:473
ClangTidy.h
llvm
Some operations such as code completion produce a set of candidates.
Definition: YAMLGenerator.cpp:28
clang::tidy::ClangTidyContext::canEnableAnalyzerAlphaCheckers
bool canEnableAnalyzerAlphaCheckers() const
If the experimental alpha checkers from the static analyzer can be enabled.
Definition: ClangTidyDiagnosticConsumer.h:190
clang::tidy::OptionsView::get
std::string get(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
Definition: ClangTidy.cpp:454
clang::tidy::ClangTidyASTConsumerFactory::getCheckOptions
ClangTidyOptions::OptionMap getCheckOptions()
Get the union of options from all checks.
Definition: ClangTidy.cpp:429
clang::tidy::ClangTidyContext::isCheckEnabled
bool isCheckEnabled(StringRef CheckName) const
Returns true if the check is enabled for the CurrentFile.
Definition: ClangTidyDiagnosticConsumer.cpp:248
clang::tidy::ClangTidyASTConsumerFactory
Definition: ClangTidy.h:192
clang::tooling
Definition: ClangTidy.h:28
clang::tidy::ClangTidyContext::getEnableProfiling
bool getEnableProfiling() const
Definition: ClangTidyDiagnosticConsumer.h:171
StoreCheckProfile
static cl::opt< std::string > StoreCheckProfile("store-check-profile", cl::desc(R"( By default reports are printed in tabulated format to stderr. When this option is passed, these per-TU profiles are instead stored as JSON. )"), cl::value_desc("prefix"), cl::cat(ClangTidyCategory))
clang::tidy::cppcoreguidelines::toString
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
Definition: SpecialMemberFunctionsCheck.cpp:59
clang::tidy::modernize::Message
static const StringRef Message
Definition: UseTransparentFunctorsCheck.cpp:71
clang::tidy::ClangTidyOptions
Contains options for clang-tidy.
Definition: ClangTidyOptions.h:51
clang::tidy::ClangTidyGlobalOptions
Global options.
Definition: ClangTidyOptions.h:43
clang::tidy::runClangTidy
std::vector< ClangTidyError > runClangTidy(clang::tidy::ClangTidyContext &Context, const CompilationDatabase &Compilations, ArrayRef< std::string > InputFiles, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS, bool EnableCheckProfile, llvm::StringRef StoreCheckProfile)
Definition: ClangTidy.cpp:506
clang::tidy::getCheckOptions
ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options, bool AllowEnablingAnalyzerAlphaCheckers)
Returns the effective check-specific options.
Definition: ClangTidy.cpp:495
clang::tidy::ClangTidyCheckFactories
A collection of ClangTidyCheckFactory instances.
Definition: ClangTidyModule.h:27
clang::tidy::ClangTidyOptions::CheckOptions
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
Definition: ClangTidyOptions.h:101
clang::tidy::handleErrors
void handleErrors(llvm::ArrayRef< ClangTidyError > Errors, ClangTidyContext &Context, bool Fix, unsigned &WarningsAsErrorsCount, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS)
Displays the found Errors to the users.
Definition: ClangTidy.cpp:581
clang::tidy::ClangTidyOptions::ExtraArgsBefore
llvm::Optional< ArgList > ExtraArgsBefore
Add extra compilation arguments to the start of the list.
Definition: ClangTidyOptions.h:109
clang::tidy::ClangTidyContext::setCurrentFile
void setCurrentFile(StringRef File)
Should be called when starting to process new translation unit.
Definition: ClangTidyDiagnosticConsumer.cpp:206
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:39
AllowEnablingAnalyzerAlphaCheckers
static cl::opt< bool > AllowEnablingAnalyzerAlphaCheckers("allow-enabling-analyzer-alpha-checkers", cl::init(false), cl::Hidden, cl::cat(ClangTidyCategory))
This option allows enabling the experimental alpha checkers from the static analyzer.
clang::tidy::ClangTidyContext::diag
DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc, StringRef Message, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Report any errors detected using this method.
Definition: ClangTidyDiagnosticConsumer.cpp:192
clang::tidy::ClangTidyDiagnosticConsumer::take
std::vector< ClangTidyError > take()
Definition: ClangTidyDiagnosticConsumer.cpp:660
ClangTidyDiagnosticConsumer.h
Action
llvm::unique_function< void()> Action
Definition: TUScheduler.cpp:221
clang::tidy::ClangTidyContext::getProfileStorageParams
llvm::Optional< ClangTidyProfiling::StorageParams > getProfileStorageParams() const
Definition: ClangTidyDiagnosticConsumer.cpp:241
ClangTidyModuleRegistry.h
clang::tidy::ClangTidyContext::getOptionsForFile
ClangTidyOptions getOptionsForFile(StringRef File) const
Returns options for File.
Definition: ClangTidyDiagnosticConsumer.cpp:227
clang::tidy::ClangTidyContext::getOptions
const ClangTidyOptions & getOptions() const
Returns options for CurrentFile.
Definition: ClangTidyDiagnosticConsumer.cpp:223
clang::tidy::ClangTidyContext::setASTContext
void setASTContext(ASTContext *Context)
Sets ASTContext for the current translation unit.
Definition: ClangTidyDiagnosticConsumer.cpp:214
WarningsAsErrors
static cl::opt< std::string > WarningsAsErrors("warnings-as-errors", cl::desc(R"( Upgrades warnings to errors. Same format as '-checks'. This option's value is appended to the value of the 'WarningsAsErrors' option in .clang-tidy file, if any. )"), cl::init(""), cl::cat(ClangTidyCategory))
clang::tidy::ClangTidyDiagnosticConsumer
A diagnostic consumer that turns each Diagnostic into a SourceManager-independent ClangTidyError...
Definition: ClangTidyDiagnosticConsumer.h:226
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:98
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
clang::tidy::ClangTidyContext::setProfileStoragePrefix
void setProfileStoragePrefix(StringRef ProfilePrefix)
Control storage of profile date.
Definition: ClangTidyDiagnosticConsumer.cpp:236
EnableCheckProfile
static cl::opt< bool > EnableCheckProfile("enable-check-profile", cl::desc(R"( Enable per-check timing profiles, and print a report to stderr. )"), cl::init(false), cl::cat(ClangTidyCategory))
clang::tidy::ClangTidyContext::setSourceManager
void setSourceManager(SourceManager *SourceMgr)
Sets the SourceManager of the used DiagnosticsEngine.
Definition: ClangTidyDiagnosticConsumer.cpp:202
clang::tidy::OptionsView::OptionsView
OptionsView(StringRef CheckName, const ClangTidyOptions::OptionMap &CheckOptions)
Initializes the instance using CheckName + "." as a prefix.
Definition: ClangTidy.cpp:450
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::tidy::ClangTidyContext::setDiagnosticsEngine
void setDiagnosticsEngine(DiagnosticsEngine *DiagEngine)
Sets the DiagnosticsEngine that diag() will emit diagnostics to.
Definition: ClangTidyDiagnosticConsumer.h:108
clang::tidy::exportReplacements
void exportReplacements(const llvm::StringRef MainFilePath, const std::vector< ClangTidyError > &Errors, raw_ostream &OS)
Definition: ClangTidy.cpp:608
clang::tidy::getCheckNames
std::vector< std::string > getCheckNames(const ClangTidyOptions &Options, bool AllowEnablingAnalyzerAlphaCheckers)
Fills the list of check names that are enabled when the provided filters are applied.
Definition: ClangTidy.cpp:484
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
clang::tidy::ClangTidyError
A detected error complete with information to display diagnostic and automatic fix.
Definition: ClangTidyDiagnosticConsumer.h:44
Checks
static cl::opt< std::string > Checks("checks", cl::desc(R"( Comma-separated list of globs with optional '-' prefix. Globs are processed in order of appearance in the list. Globs without '-' prefix add checks with matching names to the set, globs with the '-' prefix remove checks with matching names from the set of enabled checks. This option's value is appended to the value of the 'Checks' option in .clang-tidy file, if any. )"), cl::init(""), cl::cat(ClangTidyCategory))
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:100
clang::tidy::ClangTidyModuleRegistry
llvm::Registry< ClangTidyModule > ClangTidyModuleRegistry
Definition: ClangTidyModuleRegistry.h:19
clang::tidy::ClangTidyASTConsumerFactory::CreateASTConsumer
std::unique_ptr< clang::ASTConsumer > CreateASTConsumer(clang::CompilerInstance &Compiler, StringRef File)
Returns an ASTConsumer that runs the specified clang-tidy checks.
Definition: ClangTidy.cpp:351
driver
Fix
static cl::opt< bool > Fix("fix", cl::desc(R"( Apply suggested fixes. Without -fix-errors clang-tidy will bail out if any compilation errors were found. )"), cl::init(false), cl::cat(ClangTidyCategory))
clang::tidy::ClangTidyContext::setCurrentBuildDirectory
void setCurrentBuildDirectory(StringRef BuildDirectory)
Should be called when starting to process new translation unit.
Definition: ClangTidyDiagnosticConsumer.h:179
clang::tidy::ClangTidyContext::setEnableProfiling
void setEnableProfiling(bool Profile)
Control profile collection in clang-tidy.
Definition: ClangTidyDiagnosticConsumer.cpp:234
MultiplexConsumer
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidy.cpp:438
ClangTidyProfiling.h