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