10 #include "llvm/ADT/STLExtras.h" 15 using transformer::RewriteRule;
19 return C.Explanation !=
nullptr;
29 std::function<Optional<RewriteRule>(
const LangOptions &,
33 :
ClangTidyCheck(Name, Context), Rule(MakeRule(getLangOpts(), Options)) {
36 "clang-tidy checks must have an explanation by default;" 37 " explicitly provide an empty explanation if none is desired");
45 "clang-tidy checks must have an explanation by default;" 46 " explicitly provide an empty explanation if none is desired");
50 const SourceManager &SM, Preprocessor *
PP, Preprocessor *ModuleExpanderPP) {
53 if (Rule && llvm::any_of(Rule->Cases, [](
const RewriteRule::Case &C) {
54 return !C.AddedIncludes.empty();
56 Inserter = std::make_unique<IncludeInserter>(
58 PP->addPPCallbacks(Inserter->CreatePPCallbacks());
63 ast_matchers::MatchFinder *Finder) {
65 for (
auto &Matcher : transformer::detail::buildMatchers(*Rule))
66 Finder->addDynamicMatcher(Matcher,
this);
70 const ast_matchers::MatchFinder::MatchResult &Result) {
71 if (Result.Context->getDiagnostics().hasErrorOccurred())
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: " 85 if (Transformations->empty())
88 Expected<std::string> Explanation = Case.Explanation->eval(Result);
90 llvm::errs() <<
"Error in explanation: " 96 DiagnosticBuilder Diag =
97 diag((*Transformations)[0].
Range.getBegin(), *Explanation);
98 for (
const auto &T : *Transformations)
99 Diag << FixItHint::CreateReplacement(T.Range, T.Replacement);
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 I.second == transformer::IncludeFormat::Angled)) {
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
static bool hasExplanation(const RewriteRule::Case &C)
static constexpr llvm::StringLiteral Name
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
CharSourceRange Range
SourceRange for the file name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
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))
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.