| 1 | //===--- WalkAST.cpp - Find declaration references in the AST -------------===// |
| 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 "AnalysisInternal.h" |
| 10 | #include "clang/AST/Decl.h" |
| 11 | #include "clang/AST/DeclCXX.h" |
| 12 | #include "clang/AST/Expr.h" |
| 13 | #include "clang/AST/ExprCXX.h" |
| 14 | #include "clang/AST/RecursiveASTVisitor.h" |
| 15 | #include "clang/AST/TemplateName.h" |
| 16 | #include "clang/AST/Type.h" |
| 17 | #include "clang/AST/TypeLoc.h" |
| 18 | #include "clang/Basic/SourceLocation.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | #include "llvm/Support/Casting.h" |
| 21 | |
| 22 | namespace clang::include_cleaner { |
| 23 | namespace { |
| 24 | using DeclCallback = |
| 25 | llvm::function_ref<void(SourceLocation, NamedDecl &, RefType)>; |
| 26 | |
| 27 | class ASTWalker : public RecursiveASTVisitor<ASTWalker> { |
| 28 | DeclCallback Callback; |
| 29 | |
| 30 | void report(SourceLocation Loc, NamedDecl *ND, |
| 31 | RefType RT = RefType::Explicit) { |
| 32 | if (!ND || Loc.isInvalid()) |
| 33 | return; |
| 34 | Callback(Loc, *cast<NamedDecl>(ND->getCanonicalDecl()), RT); |
| 35 | } |
| 36 | |
| 37 | NamedDecl *resolveTemplateName(TemplateName TN) { |
| 38 | // For using-templates, only mark the alias. |
| 39 | if (auto *USD = TN.getAsUsingShadowDecl()) |
| 40 | return USD; |
| 41 | return TN.getAsTemplateDecl(); |
| 42 | } |
| 43 | NamedDecl *getMemberProvider(QualType Base) { |
| 44 | if (Base->isPointerType()) |
| 45 | return getMemberProvider(Base->getPointeeType()); |
| 46 | // Unwrap the sugar ElaboratedType. |
| 47 | if (const auto *ElTy = dyn_cast<ElaboratedType>(Base)) |
| 48 | return getMemberProvider(ElTy->getNamedType()); |
| 49 | |
| 50 | if (const auto *TT = dyn_cast<TypedefType>(Base)) |
| 51 | return TT->getDecl(); |
| 52 | if (const auto *UT = dyn_cast<UsingType>(Base)) |
| 53 | return UT->getFoundDecl(); |
| 54 | // A heuristic: to resolve a template type to **only** its template name. |
| 55 | // We're only using this method for the base type of MemberExpr, in general |
| 56 | // the template provides the member, and the critical case `unique_ptr<Foo>` |
| 57 | // is supported (the base type is a Foo*). |
| 58 | // |
| 59 | // There are some exceptions that this heuristic could fail (dependent base, |
| 60 | // dependent typealias), but we believe these are rare. |
| 61 | if (const auto *TST = dyn_cast<TemplateSpecializationType>(Base)) |
| 62 | return resolveTemplateName(TST->getTemplateName()); |
| 63 | return Base->getAsRecordDecl(); |
| 64 | } |
| 65 | |
| 66 | public: |
| 67 | ASTWalker(DeclCallback Callback) : Callback(Callback) {} |
| 68 | |
| 69 | bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *S) { |
| 70 | if (!WalkUpFromCXXOperatorCallExpr(S)) |
| 71 | return false; |
| 72 | |
| 73 | // Operators are always ADL extension points, by design references to them |
| 74 | // doesn't count as uses (generally the type should provide them). |
| 75 | // Don't traverse the callee. |
| 76 | |
| 77 | for (auto *Arg : S->arguments()) |
| 78 | if (!TraverseStmt(Arg)) |
| 79 | return false; |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 84 | report(DRE->getLocation(), DRE->getFoundDecl()); |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | bool VisitMemberExpr(MemberExpr *E) { |
| 89 | // Reporting a usage of the member decl would cause issues (e.g. force |
| 90 | // including the base class for inherited members). Instead, we report a |
| 91 | // usage of the base type of the MemberExpr, so that e.g. code |
| 92 | // `returnFoo().bar` can keep #include "foo.h" (rather than inserting |
| 93 | // "bar.h" for the underlying base type `Bar`). |
| 94 | QualType Type = E->getBase()->IgnoreImpCasts()->getType(); |
| 95 | report(E->getMemberLoc(), getMemberProvider(Type), RefType::Implicit); |
| 96 | return true; |
| 97 | } |
| 98 | bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { |
| 99 | report(E->getMemberLoc(), getMemberProvider(E->getBaseType()), |
| 100 | RefType::Implicit); |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | bool VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 105 | report(E->getLocation(), getMemberProvider(E->getType()), |
| 106 | E->getParenOrBraceRange().isValid() ? RefType::Explicit |
| 107 | : RefType::Implicit); |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | bool VisitOverloadExpr(OverloadExpr *E) { |
| 112 | // Since we can't prove which overloads are used, report all of them. |
| 113 | llvm::for_each(E->decls(), [this, E](NamedDecl *D) { |
| 114 | report(E->getNameLoc(), D, RefType::Ambiguous); |
| 115 | }); |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | bool VisitUsingDecl(UsingDecl *UD) { |
| 120 | for (const auto *Shadow : UD->shadows()) { |
| 121 | auto *TD = Shadow->getTargetDecl(); |
| 122 | auto IsUsed = TD->isUsed() || TD->isReferenced(); |
| 123 | report(UD->getLocation(), TD, |
| 124 | IsUsed ? RefType::Explicit : RefType::Ambiguous); |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | bool VisitFunctionDecl(FunctionDecl *FD) { |
| 130 | // Mark declaration from definition as it needs type-checking. |
| 131 | if (FD->isThisDeclarationADefinition()) |
| 132 | report(FD->getLocation(), FD); |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | bool VisitEnumDecl(EnumDecl *D) { |
| 137 | // Definition of an enum with an underlying type references declaration for |
| 138 | // type-checking purposes. |
| 139 | if (D->isThisDeclarationADefinition() && D->getIntegerTypeSourceInfo()) |
| 140 | report(D->getLocation(), D); |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | // TypeLoc visitors. |
| 145 | bool VisitUsingTypeLoc(UsingTypeLoc TL) { |
| 146 | report(TL.getNameLoc(), TL.getFoundDecl()); |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | bool VisitTagTypeLoc(TagTypeLoc TTL) { |
| 151 | report(TTL.getNameLoc(), TTL.getDecl()); |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | bool VisitTypedefTypeLoc(TypedefTypeLoc TTL) { |
| 156 | report(TTL.getNameLoc(), TTL.getTypedefNameDecl()); |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) { |
| 161 | // FIXME: Handle explicit specializations. |
| 162 | report(TL.getTemplateNameLoc(), |
| 163 | resolveTemplateName(TL.getTypePtr()->getTemplateName())); |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | bool VisitDeducedTemplateSpecializationTypeLoc( |
| 168 | DeducedTemplateSpecializationTypeLoc TL) { |
| 169 | // FIXME: Handle specializations. |
| 170 | report(TL.getTemplateNameLoc(), |
| 171 | resolveTemplateName(TL.getTypePtr()->getTemplateName())); |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &TL) { |
| 176 | auto &Arg = TL.getArgument(); |
| 177 | // Template-template parameters require special attention, as there's no |
| 178 | // TemplateNameLoc. |
| 179 | if (Arg.getKind() == TemplateArgument::Template || |
| 180 | Arg.getKind() == TemplateArgument::TemplateExpansion) { |
| 181 | report(TL.getLocation(), |
| 182 | resolveTemplateName(Arg.getAsTemplateOrTemplatePattern())); |
| 183 | return true; |
| 184 | } |
| 185 | return RecursiveASTVisitor::TraverseTemplateArgumentLoc(TL); |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | } // namespace |
| 190 | |
| 191 | void walkAST(Decl &Root, DeclCallback Callback) { |
| 192 | ASTWalker(Callback).TraverseDecl(&Root); |
| 193 | } |
| 194 | |
| 195 | } // namespace clang::include_cleaner |