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"
22#define DEBUG_TYPE "clang-tidy"
34 return DenseMapInfo<clang::SourceLocation>::getHashValue(Val.first) +
35 DenseMapInfo<StringRef>::getHashValue(Val.second);
50 llvm::PointerIntPair<const NamedDecl *, 1, bool> Data;
53 explicit NameLookup(
const NamedDecl *ND) : Data(ND, false) {}
54 explicit NameLookup(std::nullopt_t) : Data(nullptr, true) {}
55 explicit NameLookup(std::nullptr_t) : Data(nullptr, false) {}
56 NameLookup() : NameLookup(nullptr) {}
58 bool hasMultipleResolutions()
const {
return Data.getInt(); }
59 const NamedDecl *getDecl()
const {
60 assert(!hasMultipleResolutions() &&
"Found multiple decls");
61 return Data.getPointer();
63 operator bool()
const {
return !hasMultipleResolutions(); }
64 const NamedDecl *operator*()
const {
return getDecl(); }
69static const NamedDecl *
findDecl(
const RecordDecl &RecDecl,
71 for (
const Decl *D : RecDecl.decls()) {
72 if (
const auto *ND = dyn_cast<NamedDecl>(D)) {
73 if (ND->getDeclName().isIdentifier() && ND->getName() == DeclName)
84 if (Method->size_overridden_methods() != 1)
88 Method = *Method->begin_overridden_methods();
89 assert(Method &&
"Overridden method shouldn't be null");
90 const unsigned NumOverrides = Method->size_overridden_methods();
91 if (NumOverrides == 0)
99 return !Decl->getIdentifier() || Decl->getName().empty();
103 const auto *Canonical = cast<NamedDecl>(ND->getCanonicalDecl());
107 if (
const auto *Method = dyn_cast<CXXMethodDecl>(ND)) {
109 Canonical = cast<NamedDecl>(Overridden->getCanonicalDecl());
110 else if (
const FunctionTemplateDecl *Primary = Method->getPrimaryTemplate())
111 if (
const FunctionDecl *TemplatedDecl = Primary->getTemplatedDecl())
112 Canonical = cast<NamedDecl>(TemplatedDecl->getCanonicalDecl());
128 bool AggressiveTemplateLookup) {
129 if (!Parent.hasDefinition())
130 return NameLookup(
nullptr);
131 if (
const NamedDecl *InClassRef =
findDecl(Parent, DeclName))
132 return NameLookup(InClassRef);
133 const NamedDecl *Found =
nullptr;
135 for (
const CXXBaseSpecifier Base : Parent.bases()) {
136 const auto *Record = Base.getType()->getAsCXXRecordDecl();
137 if (!Record && AggressiveTemplateLookup) {
138 if (
const auto *TST =
139 Base.getType()->getAs<TemplateSpecializationType>()) {
140 if (
const auto *TD = dyn_cast_or_null<ClassTemplateDecl>(
141 TST->getTemplateName().getAsTemplateDecl()))
142 Record = TD->getTemplatedDecl();
157 return NameLookup(std::nullopt);
160 return NameLookup(Found);
166class RenamerClangTidyCheckPPCallbacks :
public PPCallbacks {
168 RenamerClangTidyCheckPPCallbacks(
const SourceManager &SM,
169 RenamerClangTidyCheck *Check)
170 : SM(SM), Check(Check) {}
173 void MacroDefined(
const Token &MacroNameTok,
174 const MacroDirective *
MD)
override {
175 const MacroInfo *Info =
MD->getMacroInfo();
176 if (Info->isBuiltinMacro())
178 if (SM.isWrittenInBuiltinFile(MacroNameTok.getLocation()))
180 if (SM.isWrittenInCommandLineFile(MacroNameTok.getLocation()))
182 if (SM.isInSystemHeader(MacroNameTok.getLocation()))
184 Check->checkMacro(MacroNameTok, Info, SM);
188 void MacroExpands(
const Token &MacroNameTok,
const MacroDefinition &
MD,
190 const MacroArgs * )
override {
191 Check->expandMacro(MacroNameTok,
MD.getMacroInfo(), SM);
195 const SourceManager &SM;
196 RenamerClangTidyCheck *Check;
199class RenamerClangTidyVisitor
200 :
public RecursiveASTVisitor<RenamerClangTidyVisitor> {
202 RenamerClangTidyVisitor(RenamerClangTidyCheck *Check,
const SourceManager &SM,
203 bool AggressiveDependentMemberLookup)
204 : Check(Check), SM(SM),
205 AggressiveDependentMemberLookup(AggressiveDependentMemberLookup) {}
207 bool shouldVisitTemplateInstantiations()
const {
return true; }
209 bool shouldVisitImplicitCode()
const {
return false; }
211 bool VisitCXXConstructorDecl(CXXConstructorDecl *Decl) {
212 if (Decl->isImplicit())
214 Check->addUsage(Decl->getParent(), Decl->getNameInfo().getSourceRange(),
217 for (
const auto *Init : Decl->inits()) {
218 if (!Init->isWritten() || Init->isInClassMemberInitializer())
220 if (
const FieldDecl *FD = Init->getAnyMember())
221 Check->addUsage(FD, SourceRange(Init->getMemberLocation()), SM);
229 bool VisitCXXDestructorDecl(CXXDestructorDecl *Decl) {
230 if (Decl->isImplicit())
232 SourceRange Range = Decl->getNameInfo().getSourceRange();
233 if (Range.getBegin().isInvalid())
238 Range.setBegin(CharSourceRange::getTokenRange(Range).getEnd());
239 Check->addUsage(Decl->getParent(), Range, SM);
243 bool VisitUsingDecl(UsingDecl *Decl) {
244 for (
const auto *Shadow : Decl->shadows())
245 Check->addUsage(Shadow->getTargetDecl(),
246 Decl->getNameInfo().getSourceRange(), SM);
250 bool VisitUsingDirectiveDecl(UsingDirectiveDecl *Decl) {
251 Check->addUsage(Decl->getNominatedNamespaceAsWritten(),
252 Decl->getIdentLocation(), SM);
256 bool VisitNamedDecl(NamedDecl *Decl) {
257 const SourceRange UsageRange =
258 DeclarationNameInfo(Decl->getDeclName(), Decl->getLocation())
260 Check->addUsage(Decl, UsageRange, SM);
264 bool VisitDeclRefExpr(DeclRefExpr *DeclRef) {
265 const SourceRange Range = DeclRef->getNameInfo().getSourceRange();
266 Check->addUsage(DeclRef->getDecl(), Range, SM);
270 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc Loc) {
271 if (
const NestedNameSpecifier Spec = Loc.getNestedNameSpecifier();
272 Spec.getKind() == NestedNameSpecifier::Kind::Namespace) {
273 if (
const auto *Decl =
274 dyn_cast<NamespaceDecl>(Spec.getAsNamespaceAndPrefix().Namespace))
275 Check->addUsage(Decl, Loc.getLocalSourceRange(), SM);
278 using Base = RecursiveASTVisitor<RenamerClangTidyVisitor>;
279 return Base::TraverseNestedNameSpecifierLoc(Loc);
282 bool VisitMemberExpr(MemberExpr *MemberRef) {
283 const SourceRange Range = MemberRef->getMemberNameInfo().getSourceRange();
284 Check->addUsage(MemberRef->getMemberDecl(), Range, SM);
289 VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *DepMemberRef) {
290 const QualType BaseType =
291 DepMemberRef->isArrow() ? DepMemberRef->getBaseType()->getPointeeType()
292 : DepMemberRef->getBaseType();
293 if (BaseType.isNull())
295 const CXXRecordDecl *Base = BaseType.getTypePtr()->getAsCXXRecordDecl();
298 const DeclarationName DeclName =
299 DepMemberRef->getMemberNameInfo().getName();
300 if (!DeclName.isIdentifier())
302 const StringRef
DependentName = DeclName.getAsIdentifierInfo()->getName();
305 *Base, DependentName, AggressiveDependentMemberLookup)) {
307 Check->addUsage(*Resolved,
308 DepMemberRef->getMemberNameInfo().getSourceRange(), SM);
314 bool VisitTypedefTypeLoc(
const TypedefTypeLoc &Loc) {
315 Check->addUsage(Loc.getDecl(), Loc.getNameLoc(), SM);
319 bool VisitTagTypeLoc(
const TagTypeLoc &Loc) {
320 Check->addUsage(Loc.getDecl(), Loc.getNameLoc(), SM);
324 bool VisitUnresolvedUsingTypeLoc(
const UnresolvedUsingTypeLoc &Loc) {
325 Check->addUsage(Loc.getDecl(), Loc.getNameLoc(), SM);
329 bool VisitTemplateTypeParmTypeLoc(
const TemplateTypeParmTypeLoc &Loc) {
330 Check->addUsage(Loc.getDecl(), Loc.getNameLoc(), SM);
335 VisitTemplateSpecializationTypeLoc(
const TemplateSpecializationTypeLoc &Loc) {
336 const TemplateDecl *Decl =
337 Loc.getTypePtr()->getTemplateName().getAsTemplateDecl(
342 if (
const auto *ClassDecl = dyn_cast<TemplateDecl>(Decl))
343 if (
const NamedDecl *TemplDecl = ClassDecl->getTemplatedDecl())
344 Check->addUsage(TemplDecl, Loc.getTemplateNameLoc(), SM);
349 bool VisitDesignatedInitExpr(DesignatedInitExpr *Expr) {
350 for (
const DesignatedInitExpr::Designator &D : Expr->designators()) {
351 if (!
D.isFieldDesignator())
353 const FieldDecl *FD =
D.getFieldDecl();
356 const IdentifierInfo *II = FD->getIdentifier();
359 const SourceRange FixLocation{
D.getFieldLoc(),
D.getFieldLoc()};
360 Check->addUsage(FD, FixLocation, SM);
367 RenamerClangTidyCheck *Check;
368 const SourceManager &SM;
369 const bool AggressiveDependentMemberLookup;
377 AggressiveDependentMemberLookup(
378 Options.get(
"AggressiveDependentMemberLookup", false)) {}
382 Options.store(Opts,
"AggressiveDependentMemberLookup",
383 AggressiveDependentMemberLookup);
387 Finder->addMatcher(translationUnitDecl(),
this);
391 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
392 ModuleExpanderPP->addPPCallbacks(
393 std::make_unique<RenamerClangTidyCheckPPCallbacks>(SM,
this));
396std::pair<RenamerClangTidyCheck::NamingCheckFailureMap::iterator, bool>
399 SourceRange UsageRange,
const SourceManager &SourceMgr) {
401 if (UsageRange.isInvalid())
402 return {NamingCheckFailures.end(),
false};
407 SourceLocation FixLocation = UsageRange.getBegin();
408 FixLocation = SourceMgr.getSpellingLoc(FixLocation);
409 if (FixLocation.isInvalid())
410 return {NamingCheckFailures.end(),
false};
413 if (SourceMgr.isInSystemHeader(FixLocation))
414 return {NamingCheckFailures.end(),
false};
416 auto EmplaceResult = NamingCheckFailures.try_emplace(FailureId);
421 if (!Failure.RawUsageLocs.insert(FixLocation).second)
422 return EmplaceResult;
425 return EmplaceResult;
427 if (SourceMgr.isWrittenInScratchSpace(FixLocation))
433 return EmplaceResult;
437 SourceRange UsageRange,
438 const SourceManager &SourceMgr) {
439 if (SourceMgr.isInSystemHeader(Decl->getLocation()))
447 if (isa<ClassTemplateSpecializationDecl>(Decl))
457 std::optional<FailureInfo> MaybeFailure =
463 FailureDecl->getName());
465 auto [FailureIter, NewFailure] =
addUsage(FailureId, UsageRange, SourceMgr);
467 if (FailureIter == NamingCheckFailures.end()) {
478 Failure.Info = std::move(*MaybeFailure);
481 if (!Failure.shouldFix())
483 const IdentifierTable &Idents = FailureDecl->getASTContext().Idents;
484 auto CheckNewIdentifier = Idents.find(Failure.Info.Fixup);
485 if (CheckNewIdentifier != Idents.end()) {
486 const IdentifierInfo *Ident = CheckNewIdentifier->second;
487 if (Ident->isKeyword(getLangOpts()))
489 else if (Ident->hasMacroDefinition())
491 }
else if (!isValidAsciiIdentifier(Failure.Info.Fixup)) {
497 if (!Result.SourceManager) {
503 RenamerClangTidyVisitor Visitor(
this, *Result.SourceManager,
504 AggressiveDependentMemberLookup);
505 Visitor.TraverseAST(*Result.Context);
510 const SourceManager &SourceMgr) {
511 std::optional<FailureInfo> MaybeFailure =
516 const StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
519 const SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
521 if (!isValidAsciiIdentifier(Info.Fixup))
524 Failure.Info = std::move(Info);
530 const SourceManager &SourceMgr) {
531 const StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
534 auto Failure = NamingCheckFailures.find(ID);
535 if (Failure == NamingCheckFailures.end())
538 const SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
544 const std::string &Fixup) {
547 return "; cannot be fixed automatically";
554 return "; cannot be fixed because '" + Fixup +
555 "' would conflict with a keyword";
558 return "; cannot be fixed because '" + Fixup +
559 "' would conflict with a macro definition";
560 llvm_unreachable(
"invalid ShouldFixStatus");
564 for (
const auto &Pair : NamingCheckFailures) {
568 if (Failure.Info.KindName.empty())
571 if (Failure.shouldNotify()) {
573 auto Diag = diag(Decl.first,
575 Failure.Info.Fixup));
578 if (Failure.shouldFix()) {
579 for (
const auto &Loc : Failure.RawUsageLocs) {
590 Diag << FixItHint::CreateReplacement(SourceRange(Loc),
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for MD output.")
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) final
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) final
std::pair< SourceLocation, StringRef > NamingCheckId
void onEndOfTranslationUnit() final
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.
~RenamerClangTidyCheck() override
bool rangeCanBeFixed(SourceRange Range, const SourceManager *SM)
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 overriding.
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.
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...
static bool isEqual(const NamingCheckId &LHS, const NamingCheckId &RHS)
static unsigned getHashValue(NamingCheckId Val)
clang::tidy::RenamerClangTidyCheck::NamingCheckId NamingCheckId