clang-tools 10.0.0

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