clang-tools 10.0.0

TransformerClangTidyCheck.cpp
Go to the documentation of this file.
1 //===---------- TransformerClangTidyCheck.cpp - clang-tidy ----------------===//
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 #include "TransformerClangTidyCheck.h"
10 #include "llvm/ADT/STLExtras.h"
11
12 namespace clang {
13 namespace tidy {
14 namespace utils {
15 using transformer::RewriteRule;
16
17 #ifndef NDEBUG
18 static bool hasExplanation(const RewriteRule::Case &C) {
19 return C.Explanation != nullptr;
20 }
21 #endif
22
23 // This constructor cannot dispatch to the simpler one (below), because, in
24 // order to get meaningful results from `getLangOpts` and `Options`, we need the
25 // `ClangTidyCheck()` constructor to have been called. If we were to dispatch,
26 // we would be accessing `getLangOpts` and `Options` before the underlying
27 // `ClangTidyCheck` instance was properly initialized.
28 TransformerClangTidyCheck::TransformerClangTidyCheck(
29 std::function<Optional<RewriteRule>(const LangOptions &,
30 const OptionsView &)>
31 MakeRule,
32 StringRef Name, ClangTidyContext *Context)
33 : ClangTidyCheck(Name, Context), Rule(MakeRule(getLangOpts(), Options)) {
34 if (Rule)
35 assert(llvm::all_of(Rule->Cases, hasExplanation) &&
36 "clang-tidy checks must have an explanation by default;"
37 " explicitly provide an empty explanation if none is desired");
38 }
39
40 TransformerClangTidyCheck::TransformerClangTidyCheck(RewriteRule R,
41 StringRef Name,
42 ClangTidyContext *Context)
43 : ClangTidyCheck(Name, Context), Rule(std::move(R)) {
44 assert(llvm::all_of(Rule->Cases, hasExplanation) &&
45 "clang-tidy checks must have an explanation by default;"
46 " explicitly provide an empty explanation if none is desired");
47 }
48
49 void TransformerClangTidyCheck::registerPPCallbacks(
50 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
51 // Only allocate and register the IncludeInsert when some `Case` will add
52 // includes.
53 if (Rule && llvm::any_of(Rule->Cases, [](const RewriteRule::Case &C) {
54 return !C.AddedIncludes.empty();
55 })) {
56 Inserter = std::make_unique<IncludeInserter>(
57 SM, getLangOpts(), utils::IncludeSorter::IS_LLVM);
58 PP->addPPCallbacks(Inserter->CreatePPCallbacks());
59 }
60 }
61
62 void TransformerClangTidyCheck::registerMatchers(
63 ast_matchers::MatchFinder *Finder) {
64 if (Rule)
65 for (auto &Matcher : transformer::detail::buildMatchers(*Rule))
66 Finder->addDynamicMatcher(Matcher, this);
67 }
68
69 void TransformerClangTidyCheck::check(
70 const ast_matchers::MatchFinder::MatchResult &Result) {
71 if (Result.Context->getDiagnostics().hasErrorOccurred())
72 return;
73
74 assert(Rule && "check() should not fire if Rule is None");
75 RewriteRule::Case Case = transformer::detail::findSelectedCase(Result, *Rule);
76 Expected<SmallVector<transformer::detail::Transformation, 1>>
77 Transformations = transformer::detail::translateEdits(Result, Case.Edits);
78 if (!Transformations) {
79 llvm::errs() << "Rewrite failed: "
80 << llvm::toString(Transformations.takeError()) << "\n";
81 return;
82 }
83
84 // No rewrite applied, but no error encountered either.
85 if (Transformations->empty())
86 return;
87
88 Expected<std::string> Explanation = Case.Explanation->eval(Result);
89 if (!Explanation) {
90 llvm::errs() << "Error in explanation: "
91 << llvm::toString(Explanation.takeError()) << "\n";
92 return;
93 }
94
95 // Associate the diagnostic with the location of the first change.
96 DiagnosticBuilder Diag =
97 diag((*Transformations)[0].Range.getBegin(), *Explanation);
98 for (const auto &T : *Transformations)
99 Diag << FixItHint::CreateReplacement(T.Range, T.Replacement);
100
101 for (const auto &I : Case.AddedIncludes) {
102 auto &Header = I.first;
103 if (Optional<FixItHint> Fix = Inserter->CreateIncludeInsertion(
104 Result.SourceManager->getMainFileID(), Header,
105 /*IsAngled=*/I.second == transformer::IncludeFormat::Angled)) {
106 Diag << *Fix;
107 }
108 }
109 }
110
111 } // namespace utils
112 } // namespace tidy
113 } // namespace clang
clang::tidy::cppcoreguidelines::toString
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
Definition: SpecialMemberFunctionsCheck.cpp:58
TransformerClangTidyCheck.h
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::utils::IncludeSorter::IS_LLVM
Definition: IncludeSorter.h:26
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:188
clang::tidy::utils::TransformerClangTidyCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition: TransformerClangTidyCheck.cpp:49
clang::tidy::utils::TransformerClangTidyCheck::TransformerClangTidyCheck
TransformerClangTidyCheck(std::function< Optional< transformer::RewriteRule >(const LangOptions &, const OptionsView &)> MakeRule, StringRef Name, ClangTidyContext *Context)
clang::tidy::utils::TransformerClangTidyCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) final
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: TransformerClangTidyCheck.cpp:69
clang::tidy::utils::hasExplanation
static bool hasExplanation(const RewriteRule::Case &C)
Definition: TransformerClangTidyCheck.cpp:18
clang::tidy::utils::TransformerClangTidyCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) final
Override this to register AST matchers with Finder.
Definition: TransformerClangTidyCheck.cpp:62
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
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::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
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::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21