clang-tools 20.0.0git
RenamerClangTidyCheck.cpp
Go to the documentation of this file.
1//===--- RenamerClangTidyCheck.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
10#include "ASTUtils.h"
11#include "clang/AST/CXXInheritance.h"
12#include "clang/AST/RecursiveASTVisitor.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Basic/CharInfo.h"
15#include "clang/Frontend/CompilerInstance.h"
16#include "clang/Lex/PPCallbacks.h"
17#include "clang/Lex/Preprocessor.h"
18#include "llvm/ADT/DenseMapInfo.h"
19#include "llvm/ADT/PointerIntPair.h"
20#include <optional>
21
22#define DEBUG_TYPE "clang-tidy"
23
24using namespace clang::ast_matchers;
25
26namespace llvm {
27
28/// Specialization of DenseMapInfo to allow NamingCheckId objects in DenseMaps
29template <>
32
33 static inline NamingCheckId getEmptyKey() {
34 return {DenseMapInfo<clang::SourceLocation>::getEmptyKey(), "EMPTY"};
35 }
36
38 return {DenseMapInfo<clang::SourceLocation>::getTombstoneKey(),
39 "TOMBSTONE"};
40 }
41
42 static unsigned getHashValue(NamingCheckId Val) {
43 assert(Val != getEmptyKey() && "Cannot hash the empty key!");
44 assert(Val != getTombstoneKey() && "Cannot hash the tombstone key!");
45
46 return DenseMapInfo<clang::SourceLocation>::getHashValue(Val.first) +
47 DenseMapInfo<StringRef>::getHashValue(Val.second);
48 }
49
50 static bool isEqual(const NamingCheckId &LHS, const NamingCheckId &RHS) {
51 if (RHS == getEmptyKey())
52 return LHS == getEmptyKey();
53 if (RHS == getTombstoneKey())
54 return LHS == getTombstoneKey();
55 return LHS == RHS;
56 }
57};
58
59} // namespace llvm
60
61namespace clang::tidy {
62
63namespace {
64
65class NameLookup {
66 llvm::PointerIntPair<const NamedDecl *, 1, bool> Data;
67
68public:
69 explicit NameLookup(const NamedDecl *ND) : Data(ND, false) {}
70 explicit NameLookup(std::nullopt_t) : Data(nullptr, true) {}
71 explicit NameLookup(std::nullptr_t) : Data(nullptr, false) {}
72 NameLookup() : NameLookup(nullptr) {}
73
74 bool hasMultipleResolutions() const { return Data.getInt(); }
75 const NamedDecl *getDecl() const {
76 assert(!hasMultipleResolutions() && "Found multiple decls");
77 return Data.getPointer();
78 }
79 operator bool() const { return !hasMultipleResolutions(); }
80 const NamedDecl *operator*() const { return getDecl(); }
81};
82
83} // namespace
84
85static const NamedDecl *findDecl(const RecordDecl &RecDecl,
86 StringRef DeclName) {
87 for (const Decl *D : RecDecl.decls()) {
88 if (const auto *ND = dyn_cast<NamedDecl>(D)) {
89 if (ND->getDeclName().isIdentifier() && ND->getName() == DeclName)
90 return ND;
91 }
92 }
93 return nullptr;
94}
95
96/// Returns the function that \p Method is overridding. If There are none or
97/// multiple overrides it returns nullptr. If the overridden function itself is
98/// overridding then it will recurse up to find the first decl of the function.
99static const CXXMethodDecl *getOverrideMethod(const CXXMethodDecl *Method) {
100 if (Method->size_overridden_methods() != 1)
101 return nullptr;
102
103 while (true) {
104 Method = *Method->begin_overridden_methods();
105 assert(Method && "Overridden method shouldn't be null");
106 unsigned NumOverrides = Method->size_overridden_methods();
107 if (NumOverrides == 0)
108 return Method;
109 if (NumOverrides > 1)
110 return nullptr;
111 }
112}
113
114static bool hasNoName(const NamedDecl *Decl) {
115 return !Decl->getIdentifier() || Decl->getName().empty();
116}
117
118static const NamedDecl *getFailureForNamedDecl(const NamedDecl *ND) {
119 const auto *Canonical = cast<NamedDecl>(ND->getCanonicalDecl());
120 if (Canonical != ND)
121 return Canonical;
122
123 if (const auto *Method = dyn_cast<CXXMethodDecl>(ND)) {
124 if (const CXXMethodDecl *Overridden = getOverrideMethod(Method))
125 Canonical = cast<NamedDecl>(Overridden->getCanonicalDecl());
126 else if (const FunctionTemplateDecl *Primary = Method->getPrimaryTemplate())
127 if (const FunctionDecl *TemplatedDecl = Primary->getTemplatedDecl())
128 Canonical = cast<NamedDecl>(TemplatedDecl->getCanonicalDecl());
129
130 if (Canonical != ND)
131 return Canonical;
132 }
133
134 return ND;
135}
136
137/// Returns a decl matching the \p DeclName in \p Parent or one of its base
138/// classes. If \p AggressiveTemplateLookup is `true` then it will check
139/// template dependent base classes as well.
140/// If a matching decl is found in multiple base classes then it will return a
141/// flag indicating the multiple resolutions.
142static NameLookup findDeclInBases(const CXXRecordDecl &Parent,
143 StringRef DeclName,
144 bool AggressiveTemplateLookup) {
145 if (!Parent.hasDefinition())
146 return NameLookup(nullptr);
147 if (const NamedDecl *InClassRef = findDecl(Parent, DeclName))
148 return NameLookup(InClassRef);
149 const NamedDecl *Found = nullptr;
150
151 for (CXXBaseSpecifier Base : Parent.bases()) {
152 const auto *Record = Base.getType()->getAsCXXRecordDecl();
153 if (!Record && AggressiveTemplateLookup) {
154 if (const auto *TST =
155 Base.getType()->getAs<TemplateSpecializationType>()) {
156 if (const auto *TD = llvm::dyn_cast_or_null<ClassTemplateDecl>(
157 TST->getTemplateName().getAsTemplateDecl()))
158 Record = TD->getTemplatedDecl();
159 }
160 }
161 if (!Record)
162 continue;
163 if (auto Search =
164 findDeclInBases(*Record, DeclName, AggressiveTemplateLookup)) {
165 if (*Search) {
166 if (Found)
167 return NameLookup(
168 std::nullopt); // Multiple decls found in different base classes.
169 Found = *Search;
170 continue;
171 }
172 } else
173 return NameLookup(std::nullopt); // Propagate multiple resolution back up.
174 }
175 return NameLookup(Found); // If nullptr, decl wasn't found.
176}
177
178namespace {
179
180/// Callback supplies macros to RenamerClangTidyCheck::checkMacro
181class RenamerClangTidyCheckPPCallbacks : public PPCallbacks {
182public:
183 RenamerClangTidyCheckPPCallbacks(const SourceManager &SM,
184 RenamerClangTidyCheck *Check)
185 : SM(SM), Check(Check) {}
186
187 /// MacroDefined calls checkMacro for macros in the main file
188 void MacroDefined(const Token &MacroNameTok,
189 const MacroDirective *MD) override {
190 const MacroInfo *Info = MD->getMacroInfo();
191 if (Info->isBuiltinMacro())
192 return;
193 if (SM.isWrittenInBuiltinFile(MacroNameTok.getLocation()))
194 return;
195 if (SM.isWrittenInCommandLineFile(MacroNameTok.getLocation()))
196 return;
197 Check->checkMacro(MacroNameTok, Info, SM);
198 }
199
200 /// MacroExpands calls expandMacro for macros in the main file
201 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
202 SourceRange /*Range*/,
203 const MacroArgs * /*Args*/) override {
204 Check->expandMacro(MacroNameTok, MD.getMacroInfo(), SM);
205 }
206
207private:
208 const SourceManager &SM;
209 RenamerClangTidyCheck *Check;
210};
211
212class RenamerClangTidyVisitor
213 : public RecursiveASTVisitor<RenamerClangTidyVisitor> {
214public:
215 RenamerClangTidyVisitor(RenamerClangTidyCheck *Check, const SourceManager &SM,
216 bool AggressiveDependentMemberLookup)
217 : Check(Check), SM(SM),
218 AggressiveDependentMemberLookup(AggressiveDependentMemberLookup) {}
219
220 bool shouldVisitTemplateInstantiations() const { return true; }
221
222 bool shouldVisitImplicitCode() const { return false; }
223
224 bool VisitCXXConstructorDecl(CXXConstructorDecl *Decl) {
225 if (Decl->isImplicit())
226 return true;
227 Check->addUsage(Decl->getParent(), Decl->getNameInfo().getSourceRange(),
228 SM);
229
230 for (const auto *Init : Decl->inits()) {
231 if (!Init->isWritten() || Init->isInClassMemberInitializer())
232 continue;
233 if (const FieldDecl *FD = Init->getAnyMember())
234 Check->addUsage(FD, SourceRange(Init->getMemberLocation()), SM);
235 // Note: delegating constructors and base class initializers are handled
236 // via the "typeLoc" matcher.
237 }
238
239 return true;
240 }
241
242 bool VisitCXXDestructorDecl(CXXDestructorDecl *Decl) {
243 if (Decl->isImplicit())
244 return true;
245 SourceRange Range = Decl->getNameInfo().getSourceRange();
246 if (Range.getBegin().isInvalid())
247 return true;
248
249 // The first token that will be found is the ~ (or the equivalent trigraph),
250 // we want instead to replace the next token, that will be the identifier.
251 Range.setBegin(CharSourceRange::getTokenRange(Range).getEnd());
252 Check->addUsage(Decl->getParent(), Range, SM);
253 return true;
254 }
255
256 bool VisitUsingDecl(UsingDecl *Decl) {
257 for (const auto *Shadow : Decl->shadows())
258 Check->addUsage(Shadow->getTargetDecl(),
259 Decl->getNameInfo().getSourceRange(), SM);
260 return true;
261 }
262
263 bool VisitUsingDirectiveDecl(UsingDirectiveDecl *Decl) {
264 Check->addUsage(Decl->getNominatedNamespaceAsWritten(),
265 Decl->getIdentLocation(), SM);
266 return true;
267 }
268
269 bool VisitNamedDecl(NamedDecl *Decl) {
270 SourceRange UsageRange =
271 DeclarationNameInfo(Decl->getDeclName(), Decl->getLocation())
272 .getSourceRange();
273 Check->addUsage(Decl, UsageRange, SM);
274 return true;
275 }
276
277 bool VisitDeclRefExpr(DeclRefExpr *DeclRef) {
278 SourceRange Range = DeclRef->getNameInfo().getSourceRange();
279 Check->addUsage(DeclRef->getDecl(), Range, SM);
280 return true;
281 }
282
283 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc Loc) {
284 if (const NestedNameSpecifier *Spec = Loc.getNestedNameSpecifier()) {
285 if (const NamespaceDecl *Decl = Spec->getAsNamespace())
286 Check->addUsage(Decl, Loc.getLocalSourceRange(), SM);
287 }
288
290 return Base::TraverseNestedNameSpecifierLoc(Loc);
291 }
292
293 bool VisitMemberExpr(MemberExpr *MemberRef) {
294 SourceRange Range = MemberRef->getMemberNameInfo().getSourceRange();
295 Check->addUsage(MemberRef->getMemberDecl(), Range, SM);
296 return true;
297 }
298
299 bool
300 VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *DepMemberRef) {
301 QualType BaseType = DepMemberRef->isArrow()
302 ? DepMemberRef->getBaseType()->getPointeeType()
303 : DepMemberRef->getBaseType();
304 if (BaseType.isNull())
305 return true;
306 const CXXRecordDecl *Base = BaseType.getTypePtr()->getAsCXXRecordDecl();
307 if (!Base)
308 return true;
309 DeclarationName DeclName = DepMemberRef->getMemberNameInfo().getName();
310 if (!DeclName.isIdentifier())
311 return true;
312 StringRef DependentName = DeclName.getAsIdentifierInfo()->getName();
313
314 if (NameLookup Resolved = findDeclInBases(
315 *Base, DependentName, AggressiveDependentMemberLookup)) {
316 if (*Resolved)
317 Check->addUsage(*Resolved,
318 DepMemberRef->getMemberNameInfo().getSourceRange(), SM);
319 }
320
321 return true;
322 }
323
324 bool VisitTypedefTypeLoc(const TypedefTypeLoc &Loc) {
325 Check->addUsage(Loc.getTypedefNameDecl(), Loc.getSourceRange(), SM);
326 return true;
327 }
328
329 bool VisitTagTypeLoc(const TagTypeLoc &Loc) {
330 Check->addUsage(Loc.getDecl(), Loc.getSourceRange(), SM);
331 return true;
332 }
333
334 bool VisitInjectedClassNameTypeLoc(const InjectedClassNameTypeLoc &Loc) {
335 Check->addUsage(Loc.getDecl(), Loc.getSourceRange(), SM);
336 return true;
337 }
338
339 bool VisitUnresolvedUsingTypeLoc(const UnresolvedUsingTypeLoc &Loc) {
340 Check->addUsage(Loc.getDecl(), Loc.getSourceRange(), SM);
341 return true;
342 }
343
344 bool VisitTemplateTypeParmTypeLoc(const TemplateTypeParmTypeLoc &Loc) {
345 Check->addUsage(Loc.getDecl(), Loc.getSourceRange(), SM);
346 return true;
347 }
348
349 bool
350 VisitTemplateSpecializationTypeLoc(const TemplateSpecializationTypeLoc &Loc) {
351 const TemplateDecl *Decl =
352 Loc.getTypePtr()->getTemplateName().getAsTemplateDecl();
353
354 SourceRange Range(Loc.getTemplateNameLoc(), Loc.getTemplateNameLoc());
355 if (const auto *ClassDecl = dyn_cast<TemplateDecl>(Decl)) {
356 if (const NamedDecl *TemplDecl = ClassDecl->getTemplatedDecl())
357 Check->addUsage(TemplDecl, Range, SM);
358 }
359
360 return true;
361 }
362
363 bool VisitDependentTemplateSpecializationTypeLoc(
364 const DependentTemplateSpecializationTypeLoc &Loc) {
365 if (const TagDecl *Decl = Loc.getTypePtr()->getAsTagDecl())
366 Check->addUsage(Decl, Loc.getSourceRange(), SM);
367
368 return true;
369 }
370
371 bool VisitDesignatedInitExpr(DesignatedInitExpr *Expr) {
372 for (const DesignatedInitExpr::Designator &D : Expr->designators()) {
373 if (!D.isFieldDesignator())
374 continue;
375 const FieldDecl *FD = D.getFieldDecl();
376 if (!FD)
377 continue;
378 const IdentifierInfo *II = FD->getIdentifier();
379 if (!II)
380 continue;
381 SourceRange FixLocation{D.getFieldLoc(), D.getFieldLoc()};
382 Check->addUsage(FD, FixLocation, SM);
383 }
384
385 return true;
386 }
387
388private:
389 RenamerClangTidyCheck *Check;
390 const SourceManager &SM;
391 const bool AggressiveDependentMemberLookup;
392};
393
394} // namespace
395
397 ClangTidyContext *Context)
398 : ClangTidyCheck(CheckName, Context),
399 AggressiveDependentMemberLookup(
400 Options.get("AggressiveDependentMemberLookup", false)) {}
402
404 Options.store(Opts, "AggressiveDependentMemberLookup",
405 AggressiveDependentMemberLookup);
406}
407
409 Finder->addMatcher(translationUnitDecl(), this);
410}
411
413 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
414 ModuleExpanderPP->addPPCallbacks(
415 std::make_unique<RenamerClangTidyCheckPPCallbacks>(SM, this));
416}
417
418std::pair<RenamerClangTidyCheck::NamingCheckFailureMap::iterator, bool>
421 SourceRange UsageRange, const SourceManager &SourceMgr) {
422 // Do nothing if the provided range is invalid.
423 if (UsageRange.isInvalid())
424 return {NamingCheckFailures.end(), false};
425
426 // Get the spelling location for performing the fix. This is necessary because
427 // macros can map the same spelling location to different source locations,
428 // and we only want to fix the token once, before it is expanded by the macro.
429 SourceLocation FixLocation = UsageRange.getBegin();
430 FixLocation = SourceMgr.getSpellingLoc(FixLocation);
431 if (FixLocation.isInvalid())
432 return {NamingCheckFailures.end(), false};
433
434 auto EmplaceResult = NamingCheckFailures.try_emplace(FailureId);
435 NamingCheckFailure &Failure = EmplaceResult.first->second;
436
437 // Try to insert the identifier location in the Usages map, and bail out if it
438 // is already in there
439 if (!Failure.RawUsageLocs.insert(FixLocation).second)
440 return EmplaceResult;
441
443 return EmplaceResult;
444
445 if (SourceMgr.isWrittenInScratchSpace(FixLocation))
447
448 if (!utils::rangeCanBeFixed(UsageRange, &SourceMgr))
450
451 return EmplaceResult;
452}
453
455 SourceRange UsageRange,
456 const SourceManager &SourceMgr) {
457 if (hasNoName(Decl))
458 return;
459
460 // Ignore ClassTemplateSpecializationDecl which are creating duplicate
461 // replacements with CXXRecordDecl.
462 if (isa<ClassTemplateSpecializationDecl>(Decl))
463 return;
464
465 // We don't want to create a failure for every NamedDecl we find. Ideally
466 // there is just one NamedDecl in every group of "related" NamedDecls that
467 // becomes the failure. This NamedDecl and all of its related NamedDecls
468 // become usages. E.g. Since NamedDecls are Redeclarable, only the canonical
469 // NamedDecl becomes the failure and all redeclarations become usages.
470 const NamedDecl *FailureDecl = getFailureForNamedDecl(Decl);
471
472 std::optional<FailureInfo> MaybeFailure =
473 getDeclFailureInfo(FailureDecl, SourceMgr);
474 if (!MaybeFailure)
475 return;
476
477 NamingCheckId FailureId(FailureDecl->getLocation(), FailureDecl->getName());
478
479 auto [FailureIter, NewFailure] = addUsage(FailureId, UsageRange, SourceMgr);
480
481 if (FailureIter == NamingCheckFailures.end()) {
482 // Nothing to do if the usage wasn't accepted.
483 return;
484 }
485 if (!NewFailure) {
486 // FailureInfo has already been provided.
487 return;
488 }
489
490 // Update the stored failure with info regarding the FailureDecl.
491 NamingCheckFailure &Failure = FailureIter->second;
492 Failure.Info = std::move(*MaybeFailure);
493
494 // Don't overwritte the failure status if it was already set.
495 if (!Failure.shouldFix()) {
496 return;
497 }
498 const IdentifierTable &Idents = FailureDecl->getASTContext().Idents;
499 auto CheckNewIdentifier = Idents.find(Failure.Info.Fixup);
500 if (CheckNewIdentifier != Idents.end()) {
501 const IdentifierInfo *Ident = CheckNewIdentifier->second;
502 if (Ident->isKeyword(getLangOpts()))
504 else if (Ident->hasMacroDefinition())
506 } else if (!isValidAsciiIdentifier(Failure.Info.Fixup)) {
508 }
509}
510
511void RenamerClangTidyCheck::check(const MatchFinder::MatchResult &Result) {
512 if (!Result.SourceManager) {
513 // In principle SourceManager is not null but going only by the definition
514 // of MatchResult it must be handled. Cannot rename anything without a
515 // SourceManager.
516 return;
517 }
518 RenamerClangTidyVisitor Visitor(this, *Result.SourceManager,
519 AggressiveDependentMemberLookup);
520 Visitor.TraverseAST(*Result.Context);
521}
522
523void RenamerClangTidyCheck::checkMacro(const Token &MacroNameTok,
524 const MacroInfo *MI,
525 const SourceManager &SourceMgr) {
526 std::optional<FailureInfo> MaybeFailure =
527 getMacroFailureInfo(MacroNameTok, SourceMgr);
528 if (!MaybeFailure)
529 return;
530 FailureInfo &Info = *MaybeFailure;
531 StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
532 NamingCheckId ID(MI->getDefinitionLoc(), Name);
533 NamingCheckFailure &Failure = NamingCheckFailures[ID];
534 SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
535
536 if (!isValidAsciiIdentifier(Info.Fixup))
538
539 Failure.Info = std::move(Info);
540 addUsage(ID, Range, SourceMgr);
541}
542
543void RenamerClangTidyCheck::expandMacro(const Token &MacroNameTok,
544 const MacroInfo *MI,
545 const SourceManager &SourceMgr) {
546 StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
547 NamingCheckId ID(MI->getDefinitionLoc(), Name);
548
549 auto Failure = NamingCheckFailures.find(ID);
550 if (Failure == NamingCheckFailures.end())
551 return;
552
553 SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
554 addUsage(ID, Range, SourceMgr);
555}
556
557static std::string
559 const std::string &Fixup) {
560 if (Fixup.empty() ||
562 return "; cannot be fixed automatically";
564 return {};
565 if (FixStatus >=
567 return {};
569 return "; cannot be fixed because '" + Fixup +
570 "' would conflict with a keyword";
571 if (FixStatus ==
573 return "; cannot be fixed because '" + Fixup +
574 "' would conflict with a macro definition";
575 llvm_unreachable("invalid ShouldFixStatus");
576}
577
579 for (const auto &Pair : NamingCheckFailures) {
580 const NamingCheckId &Decl = Pair.first;
581 const NamingCheckFailure &Failure = Pair.second;
582
583 if (Failure.Info.KindName.empty())
584 continue;
585
586 if (Failure.shouldNotify()) {
587 auto DiagInfo = getDiagInfo(Decl, Failure);
588 auto Diag = diag(Decl.first,
590 Failure.Info.Fixup));
591 DiagInfo.ApplyArgs(Diag);
592
593 if (Failure.shouldFix()) {
594 for (const auto &Loc : Failure.RawUsageLocs) {
595 // We assume that the identifier name is made of one token only. This
596 // is always the case as we ignore usages in macros that could build
597 // identifier names by combining multiple tokens.
598 //
599 // For destructors, we already take care of it by remembering the
600 // location of the start of the identifier and not the start of the
601 // tilde.
602 //
603 // Other multi-token identifiers, such as operators are not checked at
604 // all.
605 Diag << FixItHint::CreateReplacement(SourceRange(Loc),
606 Failure.Info.Fixup);
607 }
608 }
609 }
610 }
611}
612
613} // namespace clang::tidy
const FunctionDecl * Decl
Bracket::Index Pair
Definition: Bracket.cpp:80
llvm::SmallString< 256U > Name
const Node * Parent
FunctionInfo Info
CharSourceRange Range
SourceRange for the file name.
SourceLocation Loc
const DeclRefExpr * DeclRef
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) final
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) final
Override this to register PPCallbacks in the preprocessor.
std::pair< SourceLocation, StringRef > NamingCheckId
virtual DiagInfo getDiagInfo(const NamingCheckId &ID, const NamingCheckFailure &Failure) const =0
Overridden by derived classes, returns a description of the diagnostic that should be emitted for the...
void expandMacro(const Token &MacroNameTok, const MacroInfo *MI, const SourceManager &SourceMgr)
Add a usage of a macro if it already has a violation.
void registerMatchers(ast_matchers::MatchFinder *Finder) final
Derived classes should not implement any matching logic themselves; this class will do the matching a...
RenamerClangTidyCheck(StringRef CheckName, ClangTidyContext *Context)
ShouldFixStatus
This enum will be used in select of the diagnostic message.
@ IgnoreFailureThreshold
Values pass this threshold will be ignored completely i.e no message, no fixup.
@ ConflictsWithMacroDefinition
The fixup will conflict with a macro definition, so we can't fix it automatically.
@ ConflictsWithKeyword
The fixup will conflict with a language keyword, so we can't fix it automatically.
@ InsideMacro
If the identifier was used or declared within a macro we won't offer a fixup for safety reasons.
@ FixInvalidIdentifier
The fixup results in an identifier that is not a valid c/c++ identifier.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Derived classes that override this function should call this method from the overridden method.
virtual std::optional< FailureInfo > getMacroFailureInfo(const Token &MacroNameTok, const SourceManager &SM) const =0
Overridden by derived classes, returns information about if and how a macro failed the check.
void addUsage(const NamedDecl *Decl, SourceRange Range, const SourceManager &SourceMgr)
virtual std::optional< FailureInfo > getDeclFailureInfo(const NamedDecl *Decl, const SourceManager &SM) const =0
Overridden by derived classes, returns information about if and how a Decl failed the check.
void checkMacro(const Token &MacroNameTok, const MacroInfo *MI, const SourceManager &SourceMgr)
Check Macros for style violations.
bool rangeCanBeFixed(SourceRange Range, const SourceManager *SM)
Definition: ASTUtils.cpp:86
static NameLookup findDeclInBases(const CXXRecordDecl &Parent, StringRef DeclName, bool AggressiveTemplateLookup)
Returns a decl matching the DeclName in Parent or one of its base classes.
static std::string getDiagnosticSuffix(const RenamerClangTidyCheck::ShouldFixStatus FixStatus, const std::string &Fixup)
static const CXXMethodDecl * getOverrideMethod(const CXXMethodDecl *Method)
Returns the function that Method is overridding.
static const NamedDecl * getFailureForNamedDecl(const NamedDecl *ND)
static const NamedDecl * findDecl(const RecordDecl &RecDecl, StringRef DeclName)
static bool hasNoName(const NamedDecl *Decl)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Some operations such as code completion produce a set of candidates.
Definition: Generators.h:58
llvm::StringMap< ClangTidyValue > OptionMap
Represents customized diagnostic text and how arguments should be applied.
llvm::unique_function< void(DiagnosticBuilder &)> ApplyArgs
Information describing a failed check.
Holds an identifier name check failure, tracking the kind of the identifier, its possible fixup and t...
llvm::DenseSet< SourceLocation > RawUsageLocs
A set of all the identifier usages starting SourceLocation.
bool shouldFix() const
Whether the failure should be fixed or not.
static bool isEqual(const NamingCheckId &LHS, const NamingCheckId &RHS)