diff --git a/clang-tools-extra/clang-doc/Mapper.h b/clang-tools-extra/clang-doc/Mapper.h index 322df6d594b3d..3d2e505515715 100644 --- a/clang-tools-extra/clang-doc/Mapper.h +++ b/clang-tools-extra/clang-doc/Mapper.h @@ -18,7 +18,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_MAPPER_H #include "Representation.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/Tooling/Execution.h" using namespace clang::comments; @@ -27,22 +27,22 @@ using namespace clang::tooling; namespace clang { namespace doc { -class MapASTVisitor : public clang::RecursiveASTVisitor, +class MapASTVisitor : public ConstDynamicRecursiveASTVisitor, public ASTConsumer { public: explicit MapASTVisitor(ASTContext *Ctx, ClangDocContext CDCtx) : CDCtx(CDCtx) {} void HandleTranslationUnit(ASTContext &Context) override; - bool VisitNamespaceDecl(const NamespaceDecl *D); - bool VisitRecordDecl(const RecordDecl *D); - bool VisitEnumDecl(const EnumDecl *D); - bool VisitCXXMethodDecl(const CXXMethodDecl *D); - bool VisitFunctionDecl(const FunctionDecl *D); - bool VisitTypedefDecl(const TypedefDecl *D); - bool VisitTypeAliasDecl(const TypeAliasDecl *D); - bool VisitConceptDecl(const ConceptDecl *D); - bool VisitVarDecl(const VarDecl *D); + bool VisitNamespaceDecl(const NamespaceDecl *D) override; + bool VisitRecordDecl(const RecordDecl *D) override; + bool VisitEnumDecl(const EnumDecl *D) override; + bool VisitCXXMethodDecl(const CXXMethodDecl *D) override; + bool VisitFunctionDecl(const FunctionDecl *D) override; + bool VisitTypedefDecl(const TypedefDecl *D) override; + bool VisitTypeAliasDecl(const TypeAliasDecl *D) override; + bool VisitConceptDecl(const ConceptDecl *D) override; + bool VisitVarDecl(const VarDecl *D) override; private: template bool mapDecl(const T *D, bool IsDefinition); diff --git a/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp index 2c8856298e7be..66e34418fb0ac 100644 --- a/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp @@ -8,7 +8,7 @@ #include "AssignmentInIfConditionCheck.h" #include "clang/AST/ASTContext.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/ASTMatchers/ASTMatchFinder.h" using namespace clang::ast_matchers; @@ -21,13 +21,13 @@ void AssignmentInIfConditionCheck::registerMatchers(MatchFinder *Finder) { void AssignmentInIfConditionCheck::check( const ast_matchers::MatchFinder::MatchResult &Result) { - class Visitor : public RecursiveASTVisitor { + class Visitor : public ConstDynamicRecursiveASTVisitor { AssignmentInIfConditionCheck &Check; public: explicit Visitor(AssignmentInIfConditionCheck &Check) : Check(Check) {} - bool VisitIfStmt(IfStmt *If) { - class ConditionVisitor : public RecursiveASTVisitor { + bool VisitIfStmt(const IfStmt *If) override { + class ConditionVisitor : public ConstDynamicRecursiveASTVisitor { AssignmentInIfConditionCheck &Check; public: @@ -35,23 +35,20 @@ void AssignmentInIfConditionCheck::check( : Check(Check) {} // Dont traverse into any lambda expressions. - bool TraverseLambdaExpr(LambdaExpr *, DataRecursionQueue * = nullptr) { - return true; - } + bool TraverseLambdaExpr(const LambdaExpr *) override { return true; } // Dont traverse into any requires expressions. - bool TraverseRequiresExpr(RequiresExpr *, - DataRecursionQueue * = nullptr) { + bool TraverseRequiresExpr(const RequiresExpr *) override { return true; } - bool VisitBinaryOperator(BinaryOperator *BO) { + bool VisitBinaryOperator(const BinaryOperator *BO) override { if (BO->isAssignmentOp()) Check.report(BO); return true; } - bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *OCE) { + bool VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *OCE) override { if (OCE->isAssignmentOp()) Check.report(OCE); return true; diff --git a/clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp index d8207b30f1b5e..1a677b512320c 100644 --- a/clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp @@ -9,7 +9,7 @@ #include "EasilySwappableParametersCheck.h" #include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Lex/Lexer.h" #include "llvm/ADT/SmallSet.h" @@ -1600,8 +1600,8 @@ static bool lazyMapOfSetsIntersectionExists(const MapTy &Map, const ElemTy &E1, /// a usage for both in the same strict expression subtree. A strict /// expression subtree is a tree which only includes Expr nodes, i.e. no /// Stmts and no Decls. -class AppearsInSameExpr : public RecursiveASTVisitor { - using Base = RecursiveASTVisitor; +class AppearsInSameExpr : public ConstDynamicRecursiveASTVisitor { + using Base = ConstDynamicRecursiveASTVisitor; const FunctionDecl *FD; const Expr *CurrentExprOnlyTreeRoot = nullptr; @@ -1612,7 +1612,7 @@ class AppearsInSameExpr : public RecursiveASTVisitor { public: void setup(const FunctionDecl *FD) { this->FD = FD; - TraverseFunctionDecl(const_cast(FD)); + TraverseFunctionDecl(FD); } bool operator()(const ParmVarDecl *Param1, const ParmVarDecl *Param2) const { @@ -1620,13 +1620,13 @@ class AppearsInSameExpr : public RecursiveASTVisitor { Param2); } - bool TraverseDecl(Decl *D) { + bool TraverseDecl(const Decl *D) override { CurrentExprOnlyTreeRoot = nullptr; return Base::TraverseDecl(D); } - bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue = nullptr) { - if (auto *E = dyn_cast_or_null(S)) { + bool TraverseStmt(const Stmt *S) override { + if (const auto *E = dyn_cast_or_null(S)) { bool RootSetInCurrentStackFrame = false; if (!CurrentExprOnlyTreeRoot) { CurrentExprOnlyTreeRoot = E; @@ -1646,11 +1646,11 @@ class AppearsInSameExpr : public RecursiveASTVisitor { return Base::TraverseStmt(S); } - bool VisitDeclRefExpr(DeclRefExpr *DRE) { + bool VisitDeclRefExpr(const DeclRefExpr *DRE) override { if (!CurrentExprOnlyTreeRoot) return true; - if (auto *PVD = dyn_cast(DRE->getDecl())) + if (const auto *PVD = dyn_cast(DRE->getDecl())) if (llvm::find(FD->parameters(), PVD)) ParentExprsForParamRefs[PVD].insert(CurrentExprOnlyTreeRoot); diff --git a/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp index 9f4c215614287..47b592842df5b 100644 --- a/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp @@ -7,7 +7,7 @@ //===----------------------------------------------------------------------===// #include "DeprecatedHeadersCheck.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Lex/PPCallbacks.h" #include "clang/Lex/Preprocessor.h" @@ -44,19 +44,19 @@ class IncludeModernizePPCallbacks : public PPCallbacks { bool CheckHeaderFile; }; -class ExternCRefutationVisitor - : public RecursiveASTVisitor { +class ExternCRefutationVisitor : public ConstDynamicRecursiveASTVisitor { std::vector &IncludesToBeProcessed; const SourceManager &SM; public: ExternCRefutationVisitor(std::vector &IncludesToBeProcessed, SourceManager &SM) - : IncludesToBeProcessed(IncludesToBeProcessed), SM(SM) {} - bool shouldWalkTypesOfTypeLocs() const { return false; } - bool shouldVisitLambdaBody() const { return false; } + : IncludesToBeProcessed(IncludesToBeProcessed), SM(SM) { + ShouldWalkTypesOfTypeLocs = false; + ShouldVisitLambdaBody = false; + } - bool VisitLinkageSpecDecl(LinkageSpecDecl *LinkSpecDecl) const { + bool VisitLinkageSpecDecl(const LinkageSpecDecl *LinkSpecDecl) override { if (LinkSpecDecl->getLanguage() != LinkageSpecLanguageIDs::C || !LinkSpecDecl->hasBraces()) return true; diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp index 286c39be44ce4..f9de75656a0d9 100644 --- a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp +++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp @@ -34,10 +34,10 @@ namespace clang::tidy::modernize { /// RecursiveASTVisitor::TraverseStmt() and pop_back() afterwards. The Stmt atop /// the stack is the parent of the current statement (NULL for the topmost /// statement). -bool StmtAncestorASTVisitor::TraverseStmt(Stmt *Statement) { +bool StmtAncestorASTVisitor::TraverseStmt(const Stmt *Statement) { StmtAncestors.insert(std::make_pair(Statement, StmtStack.back())); StmtStack.push_back(Statement); - RecursiveASTVisitor::TraverseStmt(Statement); + ConstDynamicRecursiveASTVisitor::TraverseStmt(Statement); StmtStack.pop_back(); return true; } @@ -47,7 +47,7 @@ bool StmtAncestorASTVisitor::TraverseStmt(Stmt *Statement) { /// Combined with StmtAncestors, this provides roughly the same information as /// Scope, as we can map a VarDecl to its DeclStmt, then walk up the parent tree /// using StmtAncestors. -bool StmtAncestorASTVisitor::VisitDeclStmt(DeclStmt *Statement) { +bool StmtAncestorASTVisitor::VisitDeclStmt(const DeclStmt *Statement) { for (const auto *Decl : Statement->decls()) { if (const auto *V = dyn_cast(Decl)) DeclParents.insert(std::make_pair(V, Statement)); @@ -56,27 +56,27 @@ bool StmtAncestorASTVisitor::VisitDeclStmt(DeclStmt *Statement) { } /// record the DeclRefExpr as part of the parent expression. -bool ComponentFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *E) { +bool ComponentFinderASTVisitor::VisitDeclRefExpr(const DeclRefExpr *E) { Components.push_back(E); return true; } /// record the MemberExpr as part of the parent expression. -bool ComponentFinderASTVisitor::VisitMemberExpr(MemberExpr *Member) { +bool ComponentFinderASTVisitor::VisitMemberExpr(const MemberExpr *Member) { Components.push_back(Member); return true; } /// Forward any DeclRefExprs to a check on the referenced variable /// declaration. -bool DependencyFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *DeclRef) { +bool DependencyFinderASTVisitor::VisitDeclRefExpr(const DeclRefExpr *DeclRef) { if (auto *V = dyn_cast_or_null(DeclRef->getDecl())) return VisitVarDecl(V); return true; } /// Determine if any this variable is declared inside the ContainingStmt. -bool DependencyFinderASTVisitor::VisitVarDecl(VarDecl *V) { +bool DependencyFinderASTVisitor::VisitVarDecl(const VarDecl *V) { const Stmt *Curr = DeclParents->lookup(V); // First, see if the variable was declared within an inner scope of the loop. while (Curr != nullptr) { @@ -100,7 +100,7 @@ bool DependencyFinderASTVisitor::VisitVarDecl(VarDecl *V) { /// If we already created a variable for TheLoop, check to make sure /// that the name was not already taken. -bool DeclFinderASTVisitor::VisitForStmt(ForStmt *TheLoop) { +bool DeclFinderASTVisitor::VisitForStmt(const ForStmt *TheLoop) { StmtGeneratedVarNameMap::const_iterator I = GeneratedDecls->find(TheLoop); if (I != GeneratedDecls->end() && I->second == Name) { Found = true; @@ -111,7 +111,7 @@ bool DeclFinderASTVisitor::VisitForStmt(ForStmt *TheLoop) { /// If any named declaration within the AST subtree has the same name, /// then consider Name already taken. -bool DeclFinderASTVisitor::VisitNamedDecl(NamedDecl *D) { +bool DeclFinderASTVisitor::VisitNamedDecl(const NamedDecl *D) { const IdentifierInfo *Ident = D->getIdentifier(); if (Ident && Ident->getName() == Name) { Found = true; @@ -122,7 +122,7 @@ bool DeclFinderASTVisitor::VisitNamedDecl(NamedDecl *D) { /// Forward any declaration references to the actual check on the /// referenced declaration. -bool DeclFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *DeclRef) { +bool DeclFinderASTVisitor::VisitDeclRefExpr(const DeclRefExpr *DeclRef) { if (auto *D = dyn_cast(DeclRef->getDecl())) return VisitNamedDecl(D); return true; @@ -497,7 +497,7 @@ void ForLoopIndexUseVisitor::addUsage(const Usage &U) { /// int k = *i + 2; /// } /// \endcode -bool ForLoopIndexUseVisitor::TraverseUnaryOperator(UnaryOperator *Uop) { +bool ForLoopIndexUseVisitor::TraverseUnaryOperator(const UnaryOperator *Uop) { // If we dereference an iterator that's actually a pointer, count the // occurrence. if (isDereferenceOfUop(Uop, IndexVar)) { @@ -533,7 +533,7 @@ bool ForLoopIndexUseVisitor::TraverseUnaryOperator(UnaryOperator *Uop) { /// } /// \endcode /// will not. -bool ForLoopIndexUseVisitor::TraverseMemberExpr(MemberExpr *Member) { +bool ForLoopIndexUseVisitor::TraverseMemberExpr(const MemberExpr *Member) { const Expr *Base = Member->getBase(); const DeclRefExpr *Obj = getDeclRef(Base); const Expr *ResultExpr = Member; @@ -593,8 +593,8 @@ bool ForLoopIndexUseVisitor::TraverseMemberExpr(MemberExpr *Member) { /// Calls on the iterator object are not permitted, unless done through /// operator->(). The one exception is allowing vector::at() for pseudoarrays. bool ForLoopIndexUseVisitor::TraverseCXXMemberCallExpr( - CXXMemberCallExpr *MemberCall) { - auto *Member = + const CXXMemberCallExpr *MemberCall) { + const auto *Member = dyn_cast(MemberCall->getCallee()->IgnoreParenImpCasts()); if (!Member) return VisitorBase::TraverseCXXMemberCallExpr(MemberCall); @@ -638,7 +638,7 @@ bool ForLoopIndexUseVisitor::TraverseCXXMemberCallExpr( /// } /// \endcode bool ForLoopIndexUseVisitor::TraverseCXXOperatorCallExpr( - CXXOperatorCallExpr *OpCall) { + const CXXOperatorCallExpr *OpCall) { switch (OpCall->getOperator()) { case OO_Star: if (isDereferenceOfOpCall(OpCall, IndexVar)) { @@ -683,8 +683,9 @@ bool ForLoopIndexUseVisitor::TraverseCXXOperatorCallExpr( /// \endcode /// and further checking needs to be done later to ensure that exactly one array /// is referenced. -bool ForLoopIndexUseVisitor::TraverseArraySubscriptExpr(ArraySubscriptExpr *E) { - Expr *Arr = E->getBase(); +bool ForLoopIndexUseVisitor::TraverseArraySubscriptExpr( + const ArraySubscriptExpr *E) { + const Expr *Arr = E->getBase(); if (!isIndexInSubscriptExpr(E->getIdx(), IndexVar)) return VisitorBase::TraverseArraySubscriptExpr(E); @@ -737,7 +738,7 @@ bool ForLoopIndexUseVisitor::TraverseArraySubscriptExpr(ArraySubscriptExpr *E) { /// for (int i = 0; i < obj.getVector().size(); ++i) /// obj.foo(10); // using `obj` is considered risky /// \endcode -bool ForLoopIndexUseVisitor::VisitDeclRefExpr(DeclRefExpr *E) { +bool ForLoopIndexUseVisitor::VisitDeclRefExpr(const DeclRefExpr *E) { const ValueDecl *TheDecl = E->getDecl(); if (areSameVariable(IndexVar, TheDecl) || exprReferencesVariable(IndexVar, E) || areSameVariable(EndVar, TheDecl) || @@ -770,9 +771,9 @@ bool ForLoopIndexUseVisitor::VisitDeclRefExpr(DeclRefExpr *E) { /// f(elem); /// } /// \endcode -bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE, +bool ForLoopIndexUseVisitor::TraverseLambdaCapture(const LambdaExpr *LE, const LambdaCapture *C, - Expr *Init) { + const Expr *Init) { if (C->capturesVariable()) { ValueDecl *VDecl = C->getCapturedVar(); if (areSameVariable(IndexVar, VDecl)) { @@ -785,7 +786,7 @@ bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE, C->getLocation())); } if (VDecl->isInitCapture()) - TraverseStmtImpl(cast(VDecl)->getInit()); + traverseStmtImpl(cast(VDecl)->getInit()); } return VisitorBase::TraverseLambdaCapture(LE, C, Init); } @@ -794,7 +795,7 @@ bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE, /// element, note it for reuse as the loop variable. /// /// See the comments for isAliasDecl. -bool ForLoopIndexUseVisitor::VisitDeclStmt(DeclStmt *S) { +bool ForLoopIndexUseVisitor::VisitDeclStmt(const DeclStmt *S) { if (!AliasDecl && S->isSingleDecl() && isAliasDecl(Context, S->getSingleDecl(), IndexVar)) { AliasDecl = S; @@ -815,7 +816,7 @@ bool ForLoopIndexUseVisitor::VisitDeclStmt(DeclStmt *S) { return true; } -bool ForLoopIndexUseVisitor::TraverseStmtImpl(Stmt *S) { +bool ForLoopIndexUseVisitor::traverseStmtImpl(const Stmt *S) { // All this pointer swapping is a mechanism for tracking immediate parentage // of Stmts. const Stmt *OldNextParent = NextStmtParent; @@ -826,7 +827,7 @@ bool ForLoopIndexUseVisitor::TraverseStmtImpl(Stmt *S) { return Result; } -bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) { +bool ForLoopIndexUseVisitor::TraverseStmt(const Stmt *S) { // If this is an initialization expression for a lambda capture, prune the // traversal so that we don't end up diagnosing the contained DeclRefExpr as // inconsistent usage. No need to record the usage here -- this is done in @@ -838,7 +839,7 @@ bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) { return true; } } - return TraverseStmtImpl(S); + return traverseStmtImpl(S); } std::string VariableNamer::createIndexName() { diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h index 306eca7140d1a..dc7ab847d9921 100644 --- a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h +++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h @@ -10,7 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_LOOP_CONVERT_UTILS_H #include "clang/AST/ASTContext.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Basic/SourceLocation.h" #include "llvm/ADT/DenseMap.h" @@ -53,8 +53,7 @@ using ComponentVector = llvm::SmallVector; /// Class used build the reverse AST properties needed to detect /// name conflicts and free variables. -class StmtAncestorASTVisitor - : public clang::RecursiveASTVisitor { +class StmtAncestorASTVisitor : public ConstDynamicRecursiveASTVisitor { public: StmtAncestorASTVisitor() { StmtStack.push_back(nullptr); } @@ -72,45 +71,37 @@ class StmtAncestorASTVisitor /// Accessor for DeclParents. const DeclParentMap &getDeclToParentStmtMap() { return DeclParents; } - friend class clang::RecursiveASTVisitor; - private: StmtParentMap StmtAncestors; DeclParentMap DeclParents; llvm::SmallVector StmtStack; - bool TraverseStmt(clang::Stmt *Statement); - bool VisitDeclStmt(clang::DeclStmt *Statement); + bool TraverseStmt(const Stmt *Statement) override; + bool VisitDeclStmt(const DeclStmt *Statement) override; }; /// Class used to find the variables and member expressions on which an /// arbitrary expression depends. -class ComponentFinderASTVisitor - : public clang::RecursiveASTVisitor { +class ComponentFinderASTVisitor : public ConstDynamicRecursiveASTVisitor { public: ComponentFinderASTVisitor() = default; /// Find the components of an expression and place them in a ComponentVector. - void findExprComponents(const clang::Expr *SourceExpr) { - TraverseStmt(const_cast(SourceExpr)); - } + void findExprComponents(const Expr *SourceExpr) { TraverseStmt(SourceExpr); } /// Accessor for Components. const ComponentVector &getComponents() { return Components; } - friend class clang::RecursiveASTVisitor; - private: ComponentVector Components; - bool VisitDeclRefExpr(clang::DeclRefExpr *E); - bool VisitMemberExpr(clang::MemberExpr *Member); + bool VisitDeclRefExpr(const DeclRefExpr *E) override; + bool VisitMemberExpr(const MemberExpr *Member) override; }; /// Class used to determine if an expression is dependent on a variable declared /// inside of the loop where it would be used. -class DependencyFinderASTVisitor - : public clang::RecursiveASTVisitor { +class DependencyFinderASTVisitor : public ConstDynamicRecursiveASTVisitor { public: DependencyFinderASTVisitor(const StmtParentMap *StmtParents, const DeclParentMap *DeclParents, @@ -149,14 +140,12 @@ class DependencyFinderASTVisitor /// In order to avoid this, this class looks at the container expression /// `arr[k]` and decides whether or not it contains a sub-expression declared /// within the loop body. - bool dependsOnInsideVariable(const clang::Stmt *Body) { + bool dependsOnInsideVariable(const Stmt *Body) { DependsOnInsideVariable = false; - TraverseStmt(const_cast(Body)); + TraverseStmt(Body); return DependsOnInsideVariable; } - friend class clang::RecursiveASTVisitor; - private: const StmtParentMap *StmtParents; const DeclParentMap *DeclParents; @@ -164,16 +153,15 @@ class DependencyFinderASTVisitor const ReplacedVarsMap *ReplacedVars; bool DependsOnInsideVariable; - bool VisitVarDecl(clang::VarDecl *V); - bool VisitDeclRefExpr(clang::DeclRefExpr *D); + bool VisitVarDecl(const VarDecl *V) override; + bool VisitDeclRefExpr(const DeclRefExpr *D) override; }; /// Class used to determine if any declarations used in a Stmt would conflict /// with a particular identifier. This search includes the names that don't /// actually appear in the AST (i.e. created by a refactoring tool) by including /// a map from Stmts to generated names associated with those stmts. -class DeclFinderASTVisitor - : public clang::RecursiveASTVisitor { +class DeclFinderASTVisitor : public ConstDynamicRecursiveASTVisitor { public: DeclFinderASTVisitor(const StringRef &Name, const StmtGeneratedVarNameMap *GeneratedDecls) @@ -182,14 +170,12 @@ class DeclFinderASTVisitor /// Attempts to find any usages of variables name Name in Body, returning /// true when it is used in Body. This includes the generated loop variables /// of ForStmts which have already been transformed. - bool findUsages(const clang::Stmt *Body) { + bool findUsages(const Stmt *Body) { Found = false; - TraverseStmt(const_cast(Body)); + TraverseStmt(Body); return Found; } - friend class clang::RecursiveASTVisitor; - private: std::string Name; /// GeneratedDecls keeps track of ForStmts which have been transformed, @@ -197,10 +183,10 @@ class DeclFinderASTVisitor const StmtGeneratedVarNameMap *GeneratedDecls; bool Found = false; - bool VisitForStmt(clang::ForStmt *); - bool VisitNamedDecl(clang::NamedDecl *); - bool VisitDeclRefExpr(clang::DeclRefExpr *); - bool VisitTypeLoc(clang::TypeLoc); + bool VisitForStmt(const ForStmt *) override; + bool VisitNamedDecl(const NamedDecl *) override; + bool VisitDeclRefExpr(const DeclRefExpr *) override; + bool VisitTypeLoc(TypeLoc) override; }; /// The information needed to describe a valid convertible usage @@ -283,8 +269,7 @@ bool areSameVariable(const ValueDecl *First, const ValueDecl *Second); /// /// Given an index variable, recursively crawls a for loop to discover if the /// index variable is used in a way consistent with range-based for loop access. -class ForLoopIndexUseVisitor - : public RecursiveASTVisitor { +class ForLoopIndexUseVisitor : public ConstDynamicRecursiveASTVisitor { public: ForLoopIndexUseVisitor(ASTContext *Context, const VarDecl *IndexVar, const VarDecl *EndVar, const Expr *ContainerExpr, @@ -338,23 +323,22 @@ class ForLoopIndexUseVisitor bool aliasFromForInit() const { return AliasFromForInit; } private: - /// Typedef used in CRTP functions. - using VisitorBase = RecursiveASTVisitor; - friend class RecursiveASTVisitor; - - /// Overriden methods for RecursiveASTVisitor's traversal. - bool TraverseArraySubscriptExpr(ArraySubscriptExpr *E); - bool TraverseCXXMemberCallExpr(CXXMemberCallExpr *MemberCall); - bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *OpCall); - bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C, - Expr *Init); - bool TraverseMemberExpr(MemberExpr *Member); - bool TraverseUnaryOperator(UnaryOperator *Uop); - bool VisitDeclRefExpr(DeclRefExpr *E); - bool VisitDeclStmt(DeclStmt *S); - bool TraverseStmt(Stmt *S); - - bool TraverseStmtImpl(Stmt *S); + /// Typedef used in derived functions. + using VisitorBase = ConstDynamicRecursiveASTVisitor; + + /// Overridden methods for RecursiveASTVisitor's traversal. + bool TraverseArraySubscriptExpr(const ArraySubscriptExpr *E) override; + bool TraverseCXXMemberCallExpr(const CXXMemberCallExpr *MemberCall) override; + bool TraverseCXXOperatorCallExpr(const CXXOperatorCallExpr *OpCall) override; + bool TraverseLambdaCapture(const LambdaExpr *LE, const LambdaCapture *C, + const Expr *Init) override; + bool TraverseMemberExpr(const MemberExpr *Member) override; + bool TraverseUnaryOperator(const UnaryOperator *Uop) override; + bool VisitDeclRefExpr(const DeclRefExpr *E) override; + bool VisitDeclStmt(const DeclStmt *S) override; + bool TraverseStmt(const Stmt *S) override; + + bool traverseStmtImpl(const Stmt *S); /// Add an expression to the list of expressions on which the container /// expression depends. diff --git a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp index d5ccbb73735ec..f9b6b6a3b4f89 100644 --- a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp @@ -8,7 +8,7 @@ #include "PassByValueCheck.h" #include "clang/AST/ASTContext.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" #include "clang/Frontend/CompilerInstance.h" @@ -93,10 +93,7 @@ static bool paramReferredExactlyOnce(const CXXConstructorDecl *Ctor, /// \c ParmVarDecl is used exactly one time. /// /// \see ExactlyOneUsageVisitor::hasExactlyOneUsageIn() - class ExactlyOneUsageVisitor - : public RecursiveASTVisitor { - friend class RecursiveASTVisitor; - + class ExactlyOneUsageVisitor : public ConstDynamicRecursiveASTVisitor { public: ExactlyOneUsageVisitor(const ParmVarDecl *ParamDecl) : ParamDecl(ParamDecl) {} @@ -106,7 +103,7 @@ static bool paramReferredExactlyOnce(const CXXConstructorDecl *Ctor, /// given constructor. bool hasExactlyOneUsageIn(const CXXConstructorDecl *Ctor) { Count = 0U; - TraverseDecl(const_cast(Ctor)); + TraverseDecl(Ctor); return Count == 1U; } @@ -114,7 +111,7 @@ static bool paramReferredExactlyOnce(const CXXConstructorDecl *Ctor, /// Counts the number of references to a variable. /// /// Stops the AST traversal if more than one usage is found. - bool VisitDeclRefExpr(DeclRefExpr *D) { + bool VisitDeclRefExpr(const DeclRefExpr *D) override { if (const ParmVarDecl *To = dyn_cast(D->getDecl())) { if (To == ParamDecl) { ++Count; diff --git a/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp index 3e27d8fa1fe42..4616faa274509 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp @@ -8,7 +8,7 @@ #include "UseTrailingReturnTypeCheck.h" #include "clang/AST/ASTContext.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Lex/Preprocessor.h" #include "clang/Tooling/FixIt.h" @@ -45,14 +45,13 @@ using namespace clang::ast_matchers; namespace clang::tidy::modernize { namespace { -struct UnqualNameVisitor : public RecursiveASTVisitor { -public: - UnqualNameVisitor(const FunctionDecl &F) : F(F) {} +struct UnqualNameVisitor : ConstDynamicRecursiveASTVisitor { + UnqualNameVisitor(const FunctionDecl &F) : F(F) { + ShouldWalkTypesOfTypeLocs = false; + } bool Collision = false; - bool shouldWalkTypesOfTypeLocs() const { return false; } - bool visitUnqualName(StringRef UnqualName) { // Check for collisions with function arguments. for (ParmVarDecl *Param : F.parameters()) @@ -64,7 +63,7 @@ struct UnqualNameVisitor : public RecursiveASTVisitor { return false; } - bool TraverseTypeLoc(TypeLoc TL, bool TraverseQualifier = true) { + bool TraverseTypeLoc(TypeLoc TL, bool TraverseQualifier = true) override { if (TL.isNull()) return true; @@ -115,17 +114,18 @@ struct UnqualNameVisitor : public RecursiveASTVisitor { break; } - return RecursiveASTVisitor::TraverseTypeLoc( - TL, TraverseQualifier); + return ConstDynamicRecursiveASTVisitor::TraverseTypeLoc(TL, + TraverseQualifier); } // Replace the base method in order to call our own // TraverseTypeLoc(). - bool TraverseQualifiedTypeLoc(QualifiedTypeLoc TL, bool TraverseQualifier) { + bool TraverseQualifiedTypeLoc(QualifiedTypeLoc TL, + bool TraverseQualifier) override { return TraverseTypeLoc(TL.getUnqualifiedLoc(), TraverseQualifier); } - bool VisitDeclRefExpr(DeclRefExpr *S) { + bool VisitDeclRefExpr(const DeclRefExpr *S) override { DeclarationName Name = S->getNameInfo().getName(); return S->getQualifierLoc() || Name.isEmpty() || !Name.isIdentifier() || !visitUnqualName(Name.getAsIdentifierInfo()->getName()); diff --git a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp index 6da4cf7c6bf94..9a9f66503713a 100644 --- a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp +++ b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp @@ -9,7 +9,7 @@ #include "ConvertMemberFunctionsToStatic.h" #include "clang/AST/ASTContext.h" #include "clang/AST/DeclCXX.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Basic/SourceLocation.h" #include "clang/Lex/Lexer.h" @@ -55,18 +55,20 @@ AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, } AST_MATCHER(CXXMethodDecl, usesThis) { - class FindUsageOfThis : public RecursiveASTVisitor { + class FindUsageOfThis : public ConstDynamicRecursiveASTVisitor { public: bool Used = false; - bool VisitCXXThisExpr(const CXXThisExpr *E) { + bool VisitCXXThisExpr(const CXXThisExpr *E) override { Used = true; return false; // Stop traversal. } // If we enter a class declaration, don't traverse into it as any usages of // `this` will correspond to the nested class. - bool TraverseCXXRecordDecl(CXXRecordDecl *RD) { return true; } + bool TraverseCXXRecordDecl(const CXXRecordDecl *RD) override { + return true; + } } UsageOfThis; diff --git a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp index 4791df037d77d..51a116071fcd9 100644 --- a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp @@ -10,8 +10,8 @@ #include "../ClangTidyDiagnosticConsumer.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclBase.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/Expr.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/Stmt.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" @@ -207,9 +207,8 @@ void CognitiveComplexity::account(SourceLocation Loc, unsigned short Nesting, namespace { -class FunctionASTVisitor final - : public RecursiveASTVisitor { - using Base = RecursiveASTVisitor; +class FunctionASTVisitor final : public ConstDynamicRecursiveASTVisitor { + using Base = ConstDynamicRecursiveASTVisitor; // If set to true, macros are ignored during analysis. const bool IgnoreMacros; @@ -227,21 +226,25 @@ class FunctionASTVisitor final explicit FunctionASTVisitor(const bool IgnoreMacros) : IgnoreMacros(IgnoreMacros) {} - bool traverseStmtWithIncreasedNestingLevel(Stmt *Node) { + bool traverseStmtWithIncreasedNestingLevel(const Stmt *Node) { ++CurrentNestingLevel; bool ShouldContinue = Base::TraverseStmt(Node); --CurrentNestingLevel; return ShouldContinue; } - bool traverseDeclWithIncreasedNestingLevel(Decl *Node) { + bool traverseDeclWithIncreasedNestingLevel(const Decl *Node) { ++CurrentNestingLevel; bool ShouldContinue = Base::TraverseDecl(Node); --CurrentNestingLevel; return ShouldContinue; } - bool TraverseIfStmt(IfStmt *Node, bool InElseIf = false) { + bool TraverseIfStmt(const IfStmt *Node) override { + return traverseIfStmt(Node); + } + + bool traverseIfStmt(const IfStmt *Node, bool InElseIf = false) { if (!Node) return Base::TraverseIfStmt(Node); @@ -290,7 +293,7 @@ class FunctionASTVisitor final return true; if (auto *E = dyn_cast(Node->getElse())) - return TraverseIfStmt(E, true); + return traverseIfStmt(E, true); { CognitiveComplexity::Criteria Reasons = @@ -315,7 +318,7 @@ class FunctionASTVisitor final // In a sequence of binary logical operators, if the new operator is different // from the previous one, then the cognitive complexity is increased. - bool TraverseBinaryOperator(BinaryOperator *Op) { + bool TraverseBinaryOperator(const BinaryOperator *Op) override { if (!Op || !Op->isLogicalOp()) return Base::TraverseBinaryOperator(Op); @@ -346,7 +349,7 @@ class FunctionASTVisitor final // It would make sense for the function call to start the new binary // operator sequence, thus let's make sure that it creates a new stack frame. - bool TraverseCallExpr(CallExpr *Node) { + bool TraverseCallExpr(const CallExpr *Node) override { // If we are not currently processing any binary operator sequence, then // no Node-handling is needed. if (!Node || BinaryOperatorsStack.empty() || !CurrentBinaryOperator) @@ -363,7 +366,7 @@ class FunctionASTVisitor final #undef CurrentBinaryOperator - bool TraverseStmt(Stmt *Node) { + bool TraverseStmt(const Stmt *Node) override { if (!Node) return Base::TraverseStmt(Node); @@ -464,7 +467,9 @@ class FunctionASTVisitor final // function is the entry point, then the Nesting level should not be // increased. Thus that parameter is there and is used to fall-through // directly to traversing if this is the main function that is being analyzed. - bool TraverseDecl(Decl *Node, bool MainAnalyzedFunction = false) { + bool TraverseDecl(const Decl *Node) override { return traverseDecl(Node); } + + bool traverseDecl(const Decl *Node, bool MainAnalyzedFunction = false) { if (!Node || MainAnalyzedFunction) return Base::TraverseDecl(Node); @@ -530,10 +535,10 @@ void FunctionCognitiveComplexityCheck::check( "The matchers should only match the functions that " "have user-provided body."); Loc = TheDecl->getLocation(); - Visitor.TraverseDecl(const_cast(TheDecl), true); + Visitor.traverseDecl(TheDecl, true); } else { Loc = TheLambdaExpr->getBeginLoc(); - Visitor.TraverseLambdaExpr(const_cast(TheLambdaExpr)); + Visitor.TraverseLambdaExpr(TheLambdaExpr); } if (Visitor.CC.Total <= Threshold) diff --git a/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp b/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp index 8c58346ede3fa..d34920c39ceb1 100644 --- a/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp @@ -7,7 +7,7 @@ //===----------------------------------------------------------------------===// #include "FunctionSizeCheck.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "llvm/ADT/BitVector.h" @@ -16,11 +16,11 @@ using namespace clang::ast_matchers; namespace clang::tidy::readability { namespace { -class FunctionASTVisitor : public RecursiveASTVisitor { - using Base = RecursiveASTVisitor; +class FunctionASTVisitor : public ConstDynamicRecursiveASTVisitor { + using Base = ConstDynamicRecursiveASTVisitor; public: - bool VisitVarDecl(VarDecl *VD) { + bool VisitVarDecl(const VarDecl *VD) override { // Do not count function params. // Do not count decomposition declarations (C++17's structured bindings). if (StructNesting == 0 && @@ -28,14 +28,14 @@ class FunctionASTVisitor : public RecursiveASTVisitor { ++Info.Variables; return true; } - bool VisitBindingDecl(BindingDecl *BD) { + bool VisitBindingDecl(const BindingDecl *BD) override { // Do count each of the bindings (in the decomposition declaration). if (StructNesting == 0) ++Info.Variables; return true; } - bool TraverseStmt(Stmt *Node) { + bool TraverseStmt(const Stmt *Node) override { if (!Node) return Base::TraverseStmt(Node); @@ -66,7 +66,7 @@ class FunctionASTVisitor : public RecursiveASTVisitor { return true; } - bool TraverseCompoundStmt(CompoundStmt *Node) { + bool TraverseCompoundStmt(const CompoundStmt *Node) override { // If this new compound statement is located in a compound statement, which // is already nested NestingThreshold levels deep, record the start location // of this new compound statement. @@ -80,35 +80,35 @@ class FunctionASTVisitor : public RecursiveASTVisitor { return true; } - bool TraverseDecl(Decl *Node) { + bool TraverseDecl(const Decl *Node) override { TrackedParent.push_back(false); Base::TraverseDecl(Node); TrackedParent.pop_back(); return true; } - bool TraverseLambdaExpr(LambdaExpr *Node) { + bool TraverseLambdaExpr(const LambdaExpr *Node) override { ++StructNesting; Base::TraverseLambdaExpr(Node); --StructNesting; return true; } - bool TraverseCXXRecordDecl(CXXRecordDecl *Node) { + bool TraverseCXXRecordDecl(const CXXRecordDecl *Node) override { ++StructNesting; Base::TraverseCXXRecordDecl(Node); --StructNesting; return true; } - bool TraverseStmtExpr(StmtExpr *SE) { + bool TraverseStmtExpr(const StmtExpr *SE) override { ++StructNesting; Base::TraverseStmtExpr(SE); --StructNesting; return true; } - bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { + bool TraverseConstructorInitializer(const CXXCtorInitializer *Init) override { if (CountMemberInitAsStmt) ++Info.Statements; @@ -173,7 +173,7 @@ void FunctionSizeCheck::check(const MatchFinder::MatchResult &Result) { FunctionASTVisitor Visitor; Visitor.Info.NestingThreshold = NestingThreshold.value_or(-1); Visitor.CountMemberInitAsStmt = CountMemberInitAsStmt; - Visitor.TraverseDecl(const_cast(Func)); + Visitor.TraverseDecl(Func); auto &FI = Visitor.Info; if (FI.Statements == 0) diff --git a/clang-tools-extra/clang-tidy/readability/MakeMemberFunctionConstCheck.cpp b/clang-tools-extra/clang-tidy/readability/MakeMemberFunctionConstCheck.cpp index bea68884e3bda..c035693a0a5d5 100644 --- a/clang-tools-extra/clang-tidy/readability/MakeMemberFunctionConstCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/MakeMemberFunctionConstCheck.cpp @@ -8,8 +8,8 @@ #include "MakeMemberFunctionConstCheck.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/ParentMapContext.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Lex/Lexer.h" @@ -51,7 +51,7 @@ AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, enum UsageKind { Unused, Const, NonConst }; -class FindUsageOfThis : public RecursiveASTVisitor { +class FindUsageOfThis : public ConstDynamicRecursiveASTVisitor { ASTContext &Ctxt; public: @@ -73,14 +73,14 @@ class FindUsageOfThis : public RecursiveASTVisitor { return Parent; } - bool VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *) { + bool VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *) override { // An UnresolvedMemberExpr might resolve to a non-const non-static // member function. Usage = NonConst; return false; // Stop traversal. } - bool VisitCXXConstCastExpr(const CXXConstCastExpr *) { + bool VisitCXXConstCastExpr(const CXXConstCastExpr *) override { // Workaround to support the pattern // class C { // const S *get() const; @@ -171,7 +171,7 @@ class FindUsageOfThis : public RecursiveASTVisitor { return false; // Stop traversal. } - bool VisitCXXThisExpr(const CXXThisExpr *E) { + bool VisitCXXThisExpr(const CXXThisExpr *E) override { Usage = Const; const auto *Parent = getParentExprIgnoreParens(E); diff --git a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp index 4184c295b5f0a..411d66e557e52 100644 --- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp @@ -7,8 +7,8 @@ //===----------------------------------------------------------------------===// #include "SimplifyBooleanExprCheck.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/Expr.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/Basic/DiagnosticIDs.h" #include "clang/Lex/Lexer.h" #include "llvm/Support/SaveAndRestore.h" @@ -258,8 +258,9 @@ static bool containsDiscardedTokens(const ASTContext &Context, return false; } -class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { - using Base = RecursiveASTVisitor; +class SimplifyBooleanExprCheck::Visitor + : public ConstDynamicRecursiveASTVisitor { + using Base = ConstDynamicRecursiveASTVisitor; public: Visitor(SimplifyBooleanExprCheck *Check, ASTContext &Context) @@ -267,7 +268,7 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { bool traverse() { return TraverseAST(Context); } - static bool shouldIgnore(Stmt *S) { + static bool shouldIgnore(const Stmt *S) { switch (S->getStmtClass()) { case Stmt::ImplicitCastExprClass: case Stmt::MaterializeTemporaryExprClass: @@ -278,7 +279,7 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { } } - bool dataTraverseStmtPre(Stmt *S) { + bool dataTraverseStmtPre(const Stmt *S) override { if (!S) { return true; } @@ -289,7 +290,7 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { return true; } - bool dataTraverseStmtPost(Stmt *S) { + bool dataTraverseStmtPost(const Stmt *S) override { if (S && !shouldIgnore(S)) { assert(StmtStack.back() == S); StmtStack.pop_back(); @@ -297,7 +298,7 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { return true; } - bool VisitBinaryOperator(const BinaryOperator *Op) const { + bool VisitBinaryOperator(const BinaryOperator *Op) override { Check->reportBinOp(Context, Op); return true; } @@ -346,8 +347,8 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { /// only 1 statement in the \c CompoundStmt, applies F on that single /// statement. template - static auto checkSingleStatement(Stmt *S, Functor F) -> decltype(F(S)) { - if (auto *CS = dyn_cast(S)) { + static auto checkSingleStatement(const Stmt *S, Functor F) -> decltype(F(S)) { + if (const auto *CS = dyn_cast(S)) { if (CS->size() == 1) return F(CS->body_front()); return {}; @@ -355,11 +356,11 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { return F(S); } - Stmt *parent() const { + const Stmt *parent() const { return StmtStack.size() < 2 ? nullptr : StmtStack[StmtStack.size() - 2]; } - bool VisitIfStmt(IfStmt *If) { + bool VisitIfStmt(const IfStmt *If) override { // Skip any if's that have a condition var or an init statement, or are // "if consteval" statements. if (If->hasInitStorage() || If->hasVarStorage() || If->isConsteval()) @@ -369,7 +370,7 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { * if (false) ThenStmt(); -> ; * if (false) ThenStmt(); else ElseStmt() -> ElseStmt(); */ - Expr *Cond = If->getCond()->IgnoreImplicit(); + const Expr *Cond = If->getCond()->IgnoreImplicit(); if (std::optional Bool = getAsBoolLiteral(Cond, true)) { if (*Bool) Check->replaceWithThenStatement(Context, If, Cond); @@ -439,7 +440,7 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { return true; } - bool VisitConditionalOperator(ConditionalOperator *Cond) { + bool VisitConditionalOperator(const ConditionalOperator *Cond) override { /* * Condition ? true : false; -> Condition * Condition ? false : true; -> !Condition; @@ -455,7 +456,7 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { return true; } - bool VisitCompoundStmt(CompoundStmt *CS) { + bool VisitCompoundStmt(const CompoundStmt *CS) override { if (CS->size() < 2) return true; bool CurIf = false, PrevIf = false; @@ -558,7 +559,7 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { } } - bool TraverseUnaryOperator(UnaryOperator *Op) { + bool TraverseUnaryOperator(const UnaryOperator *Op) override { if (!Check->SimplifyDeMorgan || Op->getOpcode() != UO_LNot) return Base::TraverseUnaryOperator(Op); const Expr *SubImp = Op->getSubExpr()->IgnoreImplicit(); @@ -587,7 +588,7 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { private: bool IsProcessing = false; SimplifyBooleanExprCheck *Check; - SmallVector StmtStack; + SmallVector StmtStack; ASTContext &Context; }; diff --git a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp index 70f6092a5e4bc..93aa0fa586ea4 100644 --- a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp +++ b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp @@ -9,7 +9,7 @@ #include "RenamerClangTidyCheck.h" #include "ASTUtils.h" #include "clang/AST/CXXInheritance.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Basic/CharInfo.h" #include "clang/Frontend/CompilerInstance.h" @@ -211,19 +211,17 @@ class RenamerClangTidyCheckPPCallbacks : public PPCallbacks { RenamerClangTidyCheck *Check; }; -class RenamerClangTidyVisitor - : public RecursiveASTVisitor { +class RenamerClangTidyVisitor : public ConstDynamicRecursiveASTVisitor { public: RenamerClangTidyVisitor(RenamerClangTidyCheck *Check, const SourceManager &SM, bool AggressiveDependentMemberLookup) : Check(Check), SM(SM), - AggressiveDependentMemberLookup(AggressiveDependentMemberLookup) {} - - bool shouldVisitTemplateInstantiations() const { return true; } - - bool shouldVisitImplicitCode() const { return false; } + AggressiveDependentMemberLookup(AggressiveDependentMemberLookup) { + ShouldVisitTemplateInstantiations = true; + ShouldVisitImplicitCode = false; + } - bool VisitCXXConstructorDecl(CXXConstructorDecl *Decl) { + bool VisitCXXConstructorDecl(const CXXConstructorDecl *Decl) override { if (Decl->isImplicit()) return true; Check->addUsage(Decl->getParent(), Decl->getNameInfo().getSourceRange(), @@ -241,7 +239,7 @@ class RenamerClangTidyVisitor return true; } - bool VisitCXXDestructorDecl(CXXDestructorDecl *Decl) { + bool VisitCXXDestructorDecl(const CXXDestructorDecl *Decl) override { if (Decl->isImplicit()) return true; SourceRange Range = Decl->getNameInfo().getSourceRange(); @@ -255,20 +253,20 @@ class RenamerClangTidyVisitor return true; } - bool VisitUsingDecl(UsingDecl *Decl) { + bool VisitUsingDecl(const UsingDecl *Decl) override { for (const auto *Shadow : Decl->shadows()) Check->addUsage(Shadow->getTargetDecl(), Decl->getNameInfo().getSourceRange(), SM); return true; } - bool VisitUsingDirectiveDecl(UsingDirectiveDecl *Decl) { + bool VisitUsingDirectiveDecl(const UsingDirectiveDecl *Decl) override { Check->addUsage(Decl->getNominatedNamespaceAsWritten(), Decl->getIdentLocation(), SM); return true; } - bool VisitNamedDecl(NamedDecl *Decl) { + bool VisitNamedDecl(const NamedDecl *Decl) override { SourceRange UsageRange = DeclarationNameInfo(Decl->getDeclName(), Decl->getLocation()) .getSourceRange(); @@ -276,13 +274,13 @@ class RenamerClangTidyVisitor return true; } - bool VisitDeclRefExpr(DeclRefExpr *DeclRef) { + bool VisitDeclRefExpr(const DeclRefExpr *DeclRef) override { SourceRange Range = DeclRef->getNameInfo().getSourceRange(); Check->addUsage(DeclRef->getDecl(), Range, SM); return true; } - bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc Loc) { + bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc Loc) override { if (NestedNameSpecifier Spec = Loc.getNestedNameSpecifier(); Spec.getKind() == NestedNameSpecifier::Kind::Namespace) { if (const auto *Decl = @@ -290,18 +288,17 @@ class RenamerClangTidyVisitor Check->addUsage(Decl, Loc.getLocalSourceRange(), SM); } - using Base = RecursiveASTVisitor; - return Base::TraverseNestedNameSpecifierLoc(Loc); + return ConstDynamicRecursiveASTVisitor::TraverseNestedNameSpecifierLoc(Loc); } - bool VisitMemberExpr(MemberExpr *MemberRef) { + bool VisitMemberExpr(const MemberExpr *MemberRef) override { SourceRange Range = MemberRef->getMemberNameInfo().getSourceRange(); Check->addUsage(MemberRef->getMemberDecl(), Range, SM); return true; } - bool - VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *DepMemberRef) { + bool VisitCXXDependentScopeMemberExpr( + const CXXDependentScopeMemberExpr *DepMemberRef) override { QualType BaseType = DepMemberRef->isArrow() ? DepMemberRef->getBaseType()->getPointeeType() : DepMemberRef->getBaseType(); @@ -325,28 +322,28 @@ class RenamerClangTidyVisitor return true; } - bool VisitTypedefTypeLoc(const TypedefTypeLoc &Loc) { + bool VisitTypedefTypeLoc(TypedefTypeLoc Loc) override { Check->addUsage(Loc.getDecl(), Loc.getNameLoc(), SM); return true; } - bool VisitTagTypeLoc(const TagTypeLoc &Loc) { + bool VisitTagTypeLoc(TagTypeLoc Loc) override { Check->addUsage(Loc.getOriginalDecl(), Loc.getNameLoc(), SM); return true; } - bool VisitUnresolvedUsingTypeLoc(const UnresolvedUsingTypeLoc &Loc) { + bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc Loc) override { Check->addUsage(Loc.getDecl(), Loc.getNameLoc(), SM); return true; } - bool VisitTemplateTypeParmTypeLoc(const TemplateTypeParmTypeLoc &Loc) { + bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc Loc) override { Check->addUsage(Loc.getDecl(), Loc.getNameLoc(), SM); return true; } - bool - VisitTemplateSpecializationTypeLoc(const TemplateSpecializationTypeLoc &Loc) { + bool VisitTemplateSpecializationTypeLoc( + TemplateSpecializationTypeLoc Loc) override { const TemplateDecl *Decl = Loc.getTypePtr()->getTemplateName().getAsTemplateDecl( /*IgnoreDeduced=*/true); @@ -360,7 +357,7 @@ class RenamerClangTidyVisitor return true; } - bool VisitDesignatedInitExpr(DesignatedInitExpr *Expr) { + bool VisitDesignatedInitExpr(const DesignatedInitExpr *Expr) override { for (const DesignatedInitExpr::Designator &D : Expr->designators()) { if (!D.isFieldDesignator()) continue; diff --git a/clang-tools-extra/clangd/AST.cpp b/clang-tools-extra/clangd/AST.cpp index 0dcff2eae05e7..cf302124b3b61 100644 --- a/clang-tools-extra/clangd/AST.cpp +++ b/clang-tools-extra/clangd/AST.cpp @@ -17,10 +17,10 @@ #include "clang/AST/DeclObjC.h" #include "clang/AST/DeclTemplate.h" #include "clang/AST/DeclarationName.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/ExprCXX.h" #include "clang/AST/NestedNameSpecifier.h" #include "clang/AST/PrettyPrinter.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/Stmt.h" #include "clang/AST/TemplateBase.h" #include "clang/AST/TypeLoc.h" @@ -479,7 +479,7 @@ namespace { /// not have the deduced type set. Instead, we have to go to the appropriate /// DeclaratorDecl/FunctionDecl and work our back to the AutoType that does have /// a deduced type set. The AST should be improved to simplify this scenario. -class DeducedTypeVisitor : public RecursiveASTVisitor { +class DeducedTypeVisitor : public ConstDynamicRecursiveASTVisitor { SourceLocation SearchedLocation; const HeuristicResolver *Resolver; @@ -493,7 +493,7 @@ class DeducedTypeVisitor : public RecursiveASTVisitor { //- decltype(auto) i = 1; //- auto& i = 1; //- auto* i = &a; - bool VisitDeclaratorDecl(DeclaratorDecl *D) { + bool VisitDeclaratorDecl(const DeclaratorDecl *D) override { if (!D->getTypeSourceInfo() || !D->getTypeSourceInfo()->getTypeLoc().getContainedAutoTypeLoc() || D->getTypeSourceInfo() @@ -522,7 +522,7 @@ class DeducedTypeVisitor : public RecursiveASTVisitor { //- auto foo() -> int {} //- auto foo() -> decltype(1+1) {} //- operator auto() const { return 10; } - bool VisitFunctionDecl(FunctionDecl *D) { + bool VisitFunctionDecl(const FunctionDecl *D) override { if (!D->getTypeSourceInfo()) return true; // Loc of auto in return type (c++14). @@ -553,7 +553,7 @@ class DeducedTypeVisitor : public RecursiveASTVisitor { // Handle non-auto decltype, e.g.: // - auto foo() -> decltype(expr) {} // - decltype(expr); - bool VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { + bool VisitDecltypeTypeLoc(DecltypeTypeLoc TL) override { if (TL.getBeginLoc() != SearchedLocation) return true; @@ -571,7 +571,7 @@ class DeducedTypeVisitor : public RecursiveASTVisitor { // Handle functions/lambdas with `auto` typed parameters. // We deduce the type if there's exactly one instantiation visible. - bool VisitParmVarDecl(ParmVarDecl *PVD) { + bool VisitParmVarDecl(const ParmVarDecl *PVD) override { if (!PVD->getType()->isDependentType()) return true; // 'auto' here does not name an AutoType, but an implicit template param. @@ -659,13 +659,13 @@ static NamedDecl *getOnlyInstantiationImpl(TemplateDeclTy *TD) { return Only; } -NamedDecl *getOnlyInstantiation(NamedDecl *TemplatedDecl) { - if (TemplateDecl *TD = TemplatedDecl->getDescribedTemplate()) { - if (auto *CTD = llvm::dyn_cast(TD)) +const NamedDecl *getOnlyInstantiation(const NamedDecl *TemplatedDecl) { + if (const TemplateDecl *TD = TemplatedDecl->getDescribedTemplate()) { + if (const auto *CTD = llvm::dyn_cast(TD)) return getOnlyInstantiationImpl(CTD); - if (auto *FTD = llvm::dyn_cast(TD)) + if (const auto *FTD = llvm::dyn_cast(TD)) return getOnlyInstantiationImpl(FTD); - if (auto *VTD = llvm::dyn_cast(TD)) + if (const auto *VTD = llvm::dyn_cast(TD)) return getOnlyInstantiationImpl(VTD); } return nullptr; @@ -810,23 +810,22 @@ const TemplateTypeParmType *getUnderlyingPackType(const ParmVarDecl *Param) { // resolved parameters (passed as argument to a parameter that is not an // expanded template type parameter pack) and forwarding parameters (passed to a // parameter that is an expanded template type parameter pack). -class ForwardingCallVisitor - : public RecursiveASTVisitor { +class ForwardingCallVisitor : public ConstDynamicRecursiveASTVisitor { public: ForwardingCallVisitor(ArrayRef Parameters) : Parameters{Parameters}, PackType{getUnderlyingPackType(Parameters.front())} {} - bool VisitCallExpr(CallExpr *E) { - auto *Callee = getCalleeDeclOrUniqueOverload(E); + bool VisitCallExpr(const CallExpr *E) override { + const auto *Callee = getCalleeDeclOrUniqueOverload(E); if (Callee) { handleCall(Callee, E->arguments()); } return !Info.has_value(); } - bool VisitCXXConstructExpr(CXXConstructExpr *E) { - auto *Callee = E->getConstructor(); + bool VisitCXXConstructExpr(const CXXConstructExpr *E) override { + const auto *Callee = E->getConstructor(); if (Callee) { handleCall(Callee, E->arguments()); } @@ -853,7 +852,7 @@ class ForwardingCallVisitor ArrayRef Tail; // If the parameters were resolved to another forwarding FunctionDecl, this // is it. - std::optional PackTarget; + std::optional PackTarget; }; // The output of this visitor @@ -862,7 +861,8 @@ class ForwardingCallVisitor private: // inspects the given callee with the given args to check whether it // contains Parameters, and sets Info accordingly. - void handleCall(FunctionDecl *Callee, typename CallExpr::arg_range Args) { + void handleCall(const FunctionDecl *Callee, + typename CallExpr::const_arg_range Args) { // Skip functions with less parameters, they can't be the target. if (Callee->parameters().size() < Parameters.size()) return; @@ -899,7 +899,7 @@ class ForwardingCallVisitor // Returns the beginning of the expanded pack represented by Parameters // in the given arguments, if it is there. - std::optional findPack(typename CallExpr::arg_range Args) { + std::optional findPack(typename CallExpr::const_arg_range Args) { // find the argument directly referring to the first parameter assert(Parameters.size() <= static_cast(llvm::size(Args))); for (auto Begin = Args.begin(), End = Args.end() - Parameters.size() + 1; @@ -920,9 +920,9 @@ class ForwardingCallVisitor return std::nullopt; } - static FunctionDecl *getCalleeDeclOrUniqueOverload(CallExpr *E) { - Decl *CalleeDecl = E->getCalleeDecl(); - auto *Callee = dyn_cast_or_null(CalleeDecl); + static const FunctionDecl *getCalleeDeclOrUniqueOverload(const CallExpr *E) { + const Decl *CalleeDecl = E->getCalleeDecl(); + const auto *Callee = dyn_cast_or_null(CalleeDecl); if (!Callee) { if (auto *Lookup = dyn_cast(E->getCallee())) { Callee = resolveOverload(Lookup, E); @@ -934,8 +934,8 @@ class ForwardingCallVisitor return nullptr; } - static FunctionDecl *resolveOverload(UnresolvedLookupExpr *Lookup, - CallExpr *E) { + static FunctionDecl *resolveOverload(const UnresolvedLookupExpr *Lookup, + const CallExpr *E) { FunctionDecl *MatchingDecl = nullptr; if (!Lookup->requiresADL()) { // Check whether there is a single overload with this number of diff --git a/clang-tools-extra/clangd/AST.h b/clang-tools-extra/clangd/AST.h index 2b83595e5b8e9..44b94020206ab 100644 --- a/clang-tools-extra/clangd/AST.h +++ b/clang-tools-extra/clangd/AST.h @@ -180,7 +180,7 @@ TemplateTypeParmTypeLoc getContainedAutoParamType(TypeLoc TL); // If TemplatedDecl is the generic body of a template, and the template has // exactly one visible instantiation, return the instantiated body. -NamedDecl *getOnlyInstantiation(NamedDecl *TemplatedDecl); +const NamedDecl *getOnlyInstantiation(const NamedDecl *TemplatedDecl); /// Return attributes attached directly to a node. std::vector getAttributes(const DynTypedNode &); diff --git a/clang-tools-extra/clangd/DumpAST.cpp b/clang-tools-extra/clangd/DumpAST.cpp index 9a8d41d870929..87856501ce825 100644 --- a/clang-tools-extra/clangd/DumpAST.cpp +++ b/clang-tools-extra/clangd/DumpAST.cpp @@ -11,11 +11,11 @@ #include "SourceCode.h" #include "support/Logger.h" #include "clang/AST/ASTTypeTraits.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/Expr.h" #include "clang/AST/ExprCXX.h" #include "clang/AST/NestedNameSpecifier.h" #include "clang/AST/PrettyPrinter.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/TextNodeDumper.h" #include "clang/AST/Type.h" #include "clang/AST/TypeLoc.h" @@ -37,14 +37,14 @@ template std::string toString(const Print &C) { return std::move(OS.str()); } -bool isInjectedClassName(Decl *D) { +bool isInjectedClassName(const Decl *D) { if (const auto *CRD = llvm::dyn_cast(D)) return CRD->isInjectedClassName(); return false; } -class DumpVisitor : public RecursiveASTVisitor { - using Base = RecursiveASTVisitor; +class DumpVisitor : public ConstDynamicRecursiveASTVisitor { + using Base = ConstDynamicRecursiveASTVisitor; const syntax::TokenBuffer &Tokens; const ASTContext &Ctx; @@ -337,68 +337,75 @@ class DumpVisitor : public RecursiveASTVisitor { // Override traversal to record the nodes we care about. // Generally, these are nodes with position information (TypeLoc, not Type). - bool TraverseDecl(Decl *D) { + bool TraverseDecl(const Decl *D) override { return !D || isInjectedClassName(D) || traverseNode("declaration", D, [&] { Base::TraverseDecl(D); }); } - bool TraverseTypeLoc(TypeLoc TL, bool TraverseQualifier = true) { + bool TraverseTypeLoc(TypeLoc TL, bool TraverseQualifier = true) override { return !TL || traverseNode("type", TL, [&] { Base::TraverseTypeLoc(TL, TraverseQualifier); }); } - bool TraverseTemplateName(const TemplateName &TN) { + bool TraverseTemplateName(TemplateName TN) override { return traverseNode("template name", TN, [&] { Base::TraverseTemplateName(TN); }); } - bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { + bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &TAL) override { return traverseNode("template argument", TAL, [&] { Base::TraverseTemplateArgumentLoc(TAL); }); } - bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSL) { + bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSL) override { return !NNSL || traverseNode("specifier", NNSL, [&] { Base::TraverseNestedNameSpecifierLoc(NNSL); }); } - bool TraverseConstructorInitializer(CXXCtorInitializer *CCI) { + bool TraverseConstructorInitializer(const CXXCtorInitializer *CCI) override { return !CCI || traverseNode("constructor initializer", CCI, [&] { Base::TraverseConstructorInitializer(CCI); }); } - bool TraverseAttr(Attr *A) { + bool TraverseAttr(const Attr *A) override { return !A || traverseNode("attribute", A, [&] { Base::TraverseAttr(A); }); } - bool TraverseConceptReference(ConceptReference *C) { + bool TraverseConceptReference(const ConceptReference *C) override { return !C || traverseNode("reference", C, [&] { Base::TraverseConceptReference(C); }); } - bool TraverseCXXBaseSpecifier(const CXXBaseSpecifier &CBS) { + bool TraverseCXXBaseSpecifier(const CXXBaseSpecifier &CBS) override { return traverseNode("base", CBS, [&] { Base::TraverseCXXBaseSpecifier(CBS); }); } // Stmt is the same, but this form allows the data recursion optimization. - bool dataTraverseStmtPre(Stmt *S) { + bool dataTraverseStmtPre(const Stmt *S) override { return S && traverseNodePre(isa(S) ? "expression" : "statement", S); } - bool dataTraverseStmtPost(Stmt *X) { return traverseNodePost(); } + bool dataTraverseStmtPost(const Stmt *X) override { + return traverseNodePost(); + } // QualifiedTypeLoc is handled strangely in RecursiveASTVisitor: the derived // TraverseTypeLoc is not called for the inner UnqualTypeLoc. // This means we'd never see 'int' in 'const int'! Work around that here. // (The reason for the behavior is to avoid traversing the nested Type twice, // but we ignore TraverseType anyway). - bool TraverseQualifiedTypeLoc(QualifiedTypeLoc QTL, bool TraverseQualifier) { + bool TraverseQualifiedTypeLoc(QualifiedTypeLoc QTL, + bool TraverseQualifier) override { return TraverseTypeLoc(QTL.getUnqualifiedLoc()); } // Uninteresting parts of the AST that don't have locations within them. - bool TraverseNestedNameSpecifier(NestedNameSpecifier) { return true; } - bool TraverseType(QualType) { return true; } + bool TraverseNestedNameSpecifier(NestedNameSpecifier) override { + return true; + } + bool TraverseType(QualType, bool TraverseQualifier = true) override { + return true; + } // OpaqueValueExpr blocks traversal, we must explicitly traverse it. - bool TraverseOpaqueValueExpr(OpaqueValueExpr *E) { + bool TraverseOpaqueValueExpr(const OpaqueValueExpr *E) override { return TraverseStmt(E->getSourceExpr()); } // We only want to traverse the *syntactic form* to understand the selection. - bool TraversePseudoObjectExpr(PseudoObjectExpr *E) { + bool TraversePseudoObjectExpr(const PseudoObjectExpr *E) override { return TraverseStmt(E->getSyntacticForm()); } }; @@ -410,26 +417,25 @@ ASTNode dumpAST(const DynTypedNode &N, const syntax::TokenBuffer &Tokens, DumpVisitor V(Tokens, Ctx); // DynTypedNode only works with const, RecursiveASTVisitor only non-const :-( if (const auto *D = N.get()) - V.TraverseDecl(const_cast(D)); + V.TraverseDecl(D); else if (const auto *S = N.get()) - V.TraverseStmt(const_cast(S)); + V.TraverseStmt(S); else if (const auto *NNSL = N.get()) - V.TraverseNestedNameSpecifierLoc( - *const_cast(NNSL)); + V.TraverseNestedNameSpecifierLoc(*NNSL); else if (const auto *NNS = N.get()) V.TraverseNestedNameSpecifier(*NNS); else if (const auto *TL = N.get()) - V.TraverseTypeLoc(*const_cast(TL)); + V.TraverseTypeLoc(*TL); else if (const auto *QT = N.get()) - V.TraverseType(*const_cast(QT)); + V.TraverseType(*QT); else if (const auto *CCI = N.get()) - V.TraverseConstructorInitializer(const_cast(CCI)); + V.TraverseConstructorInitializer(CCI); else if (const auto *TAL = N.get()) - V.TraverseTemplateArgumentLoc(*const_cast(TAL)); + V.TraverseTemplateArgumentLoc(*TAL); else if (const auto *CBS = N.get()) - V.TraverseCXXBaseSpecifier(*const_cast(CBS)); + V.TraverseCXXBaseSpecifier(*CBS); else if (const auto *CR = N.get()) - V.TraverseConceptReference(const_cast(CR)); + V.TraverseConceptReference(CR); else elog("dumpAST: unhandled DynTypedNode kind {0}", N.getNodeKind().asStringRef()); diff --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp index 8aae41420b83e..ba5d72ed4d7ca 100644 --- a/clang-tools-extra/clangd/FindTarget.cpp +++ b/clang-tools-extra/clangd/FindTarget.cpp @@ -17,13 +17,13 @@ #include "clang/AST/DeclTemplate.h" #include "clang/AST/DeclVisitor.h" #include "clang/AST/DeclarationName.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/Expr.h" #include "clang/AST/ExprCXX.h" #include "clang/AST/ExprConcepts.h" #include "clang/AST/ExprObjC.h" #include "clang/AST/NestedNameSpecifier.h" #include "clang/AST/PrettyPrinter.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/StmtVisitor.h" #include "clang/AST/TemplateBase.h" #include "clang/AST/Type.h" @@ -923,8 +923,7 @@ refInTypeLoc(TypeLoc L, const HeuristicResolver *Resolver) { return V.Refs; } -class ExplicitReferenceCollector - : public RecursiveASTVisitor { +class ExplicitReferenceCollector : public ConstDynamicRecursiveASTVisitor { public: ExplicitReferenceCollector(llvm::function_ref Out, const HeuristicResolver *Resolver) @@ -932,36 +931,37 @@ class ExplicitReferenceCollector assert(Out); } - bool VisitTypeLoc(TypeLoc TTL) { + bool VisitTypeLoc(TypeLoc TTL) override { if (TypeLocsToSkip.count(TTL.getBeginLoc())) return true; visitNode(DynTypedNode::create(TTL)); return true; } - bool VisitStmt(Stmt *S) { + bool VisitStmt(const Stmt *S) override { visitNode(DynTypedNode::create(*S)); return true; } - bool TraverseOpaqueValueExpr(OpaqueValueExpr *OVE) { + bool TraverseOpaqueValueExpr(const OpaqueValueExpr *OVE) override { visitNode(DynTypedNode::create(*OVE)); // Not clear why the source expression is skipped by default... // FIXME: can we just make RecursiveASTVisitor do this? - return RecursiveASTVisitor::TraverseStmt(OVE->getSourceExpr()); + return ConstDynamicRecursiveASTVisitor::TraverseStmt(OVE->getSourceExpr()); } - bool TraversePseudoObjectExpr(PseudoObjectExpr *POE) { + bool TraversePseudoObjectExpr(const PseudoObjectExpr *POE) override { visitNode(DynTypedNode::create(*POE)); // Traverse only the syntactic form to find the *written* references. // (The semantic form also contains lots of duplication) - return RecursiveASTVisitor::TraverseStmt(POE->getSyntacticForm()); + return ConstDynamicRecursiveASTVisitor::TraverseStmt( + POE->getSyntacticForm()); } // We re-define Traverse*, since there's no corresponding Visit*. // TemplateArgumentLoc is the only way to get locations for references to // template template parameters. - bool TraverseTemplateArgumentLoc(TemplateArgumentLoc A) { + bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &A) override { switch (A.getArgument().getKind()) { case TemplateArgument::Template: case TemplateArgument::TemplateExpansion: @@ -985,36 +985,37 @@ class ExplicitReferenceCollector case TemplateArgument::StructuralValue: break; // Handled by VisitType and VisitExpression. }; - return RecursiveASTVisitor::TraverseTemplateArgumentLoc(A); + return ConstDynamicRecursiveASTVisitor::TraverseTemplateArgumentLoc(A); } - bool VisitDecl(Decl *D) { + bool VisitDecl(const Decl *D) override { visitNode(DynTypedNode::create(*D)); return true; } // We have to use Traverse* because there is no corresponding Visit*. - bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc L) { + bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc L) override { if (!L.getNestedNameSpecifier()) return true; visitNode(DynTypedNode::create(L)); // Inner type is missing information about its qualifier, skip it. if (auto TL = L.getAsTypeLoc()) TypeLocsToSkip.insert(TL.getBeginLoc()); - return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L); + return ConstDynamicRecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L); } - bool TraverseObjCProtocolLoc(ObjCProtocolLoc ProtocolLoc) { + bool TraverseObjCProtocolLoc(ObjCProtocolLoc ProtocolLoc) override { visitNode(DynTypedNode::create(ProtocolLoc)); return true; } - bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { + bool TraverseConstructorInitializer(const CXXCtorInitializer *Init) override { visitNode(DynTypedNode::create(*Init)); - return RecursiveASTVisitor::TraverseConstructorInitializer(Init); + return ConstDynamicRecursiveASTVisitor::TraverseConstructorInitializer( + Init); } - bool VisitConceptReference(const ConceptReference *CR) { + bool VisitConceptReference(const ConceptReference *CR) override { visitNode(DynTypedNode::create(*CR)); return true; } @@ -1115,19 +1116,18 @@ void findExplicitReferences(const Stmt *S, llvm::function_ref Out, const HeuristicResolver *Resolver) { assert(S); - ExplicitReferenceCollector(Out, Resolver).TraverseStmt(const_cast(S)); + ExplicitReferenceCollector(Out, Resolver).TraverseStmt(S); } void findExplicitReferences(const Decl *D, llvm::function_ref Out, const HeuristicResolver *Resolver) { assert(D); - ExplicitReferenceCollector(Out, Resolver).TraverseDecl(const_cast(D)); + ExplicitReferenceCollector(Out, Resolver).TraverseDecl(D); } void findExplicitReferences(const ASTContext &AST, llvm::function_ref Out, const HeuristicResolver *Resolver) { - ExplicitReferenceCollector(Out, Resolver) - .TraverseAST(const_cast(AST)); + ExplicitReferenceCollector(Out, Resolver).TraverseAST(AST); } llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelation R) { diff --git a/clang-tools-extra/clangd/InlayHints.cpp b/clang-tools-extra/clangd/InlayHints.cpp index d56b93e5f36dc..1ec94fca55373 100644 --- a/clang-tools-extra/clangd/InlayHints.cpp +++ b/clang-tools-extra/clangd/InlayHints.cpp @@ -16,9 +16,9 @@ #include "clang/AST/Decl.h" #include "clang/AST/DeclBase.h" #include "clang/AST/DeclarationName.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/Expr.h" #include "clang/AST/ExprCXX.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/Stmt.h" #include "clang/AST/StmtVisitor.h" #include "clang/AST/Type.h" @@ -373,7 +373,7 @@ struct Callee { FunctionProtoTypeLoc Loc; }; -class InlayHintVisitor : public RecursiveASTVisitor { +class InlayHintVisitor : public ConstDynamicRecursiveASTVisitor { public: InlayHintVisitor(std::vector &Results, ParsedAST &AST, const Config &Cfg, std::optional RestrictRange, @@ -397,14 +397,14 @@ class InlayHintVisitor : public RecursiveASTVisitor { // SuppressDefaultTemplateArgs (set by default) to have an effect. } - bool VisitTypeLoc(TypeLoc TL) { + bool VisitTypeLoc(TypeLoc TL) override { if (const auto *DT = llvm::dyn_cast(TL.getType())) if (QualType UT = DT->getUnderlyingType(); !UT->isDependentType()) addTypeHint(TL.getSourceRange(), UT, ": "); return true; } - bool VisitCXXConstructExpr(CXXConstructExpr *E) { + bool VisitCXXConstructExpr(const CXXConstructExpr *E) override { // Weed out constructor calls that don't look like a function call with // an argument list, by checking the validity of getParenOrBraceRange(). // Also weed out std::initializer_list constructors as there are no names @@ -425,8 +425,8 @@ class InlayHintVisitor : public RecursiveASTVisitor { // Carefully recurse into PseudoObjectExprs, which typically incorporate // a syntactic expression and several semantic expressions. - bool TraversePseudoObjectExpr(PseudoObjectExpr *E) { - Expr *SyntacticExpr = E->getSyntacticForm(); + bool TraversePseudoObjectExpr(const PseudoObjectExpr *E) override { + const Expr *SyntacticExpr = E->getSyntacticForm(); if (isa(SyntacticExpr)) // Since the counterpart semantics usually get the identical source // locations as the syntactic one, visiting those would end up presenting @@ -434,7 +434,7 @@ class InlayHintVisitor : public RecursiveASTVisitor { // Thus, only traverse the syntactic forms if this is written as a // CallExpr. This leaves the door open in case the arguments in the // syntactic form could possibly get parameter names. - return RecursiveASTVisitor::TraverseStmt(SyntacticExpr); + return ConstDynamicRecursiveASTVisitor::TraverseStmt(SyntacticExpr); // We don't want the hints for some of the MS property extensions. // e.g. // struct S { @@ -445,10 +445,10 @@ class InlayHintVisitor : public RecursiveASTVisitor { if (isa(SyntacticExpr)) return true; // FIXME: Handle other forms of a pseudo object expression. - return RecursiveASTVisitor::TraversePseudoObjectExpr(E); + return ConstDynamicRecursiveASTVisitor::TraversePseudoObjectExpr(E); } - bool VisitCallExpr(CallExpr *E) { + bool VisitCallExpr(const CallExpr *E) override { if (!Cfg.InlayHints.Parameters) return true; @@ -497,7 +497,7 @@ class InlayHintVisitor : public RecursiveASTVisitor { return true; } - bool VisitFunctionDecl(FunctionDecl *D) { + bool VisitFunctionDecl(const FunctionDecl *D) override { if (auto *FPT = llvm::dyn_cast(D->getType().getTypePtr())) { if (!FPT->hasTrailingReturn()) { @@ -514,7 +514,7 @@ class InlayHintVisitor : public RecursiveASTVisitor { return true; } - bool VisitForStmt(ForStmt *S) { + bool VisitForStmt(const ForStmt *S) override { if (Cfg.InlayHints.BlockEnd) { std::string Name; // Common case: for (int I = 0; I < N; I++). Use "I" as the name. @@ -528,19 +528,19 @@ class InlayHintVisitor : public RecursiveASTVisitor { return true; } - bool VisitCXXForRangeStmt(CXXForRangeStmt *S) { + bool VisitCXXForRangeStmt(const CXXForRangeStmt *S) override { if (Cfg.InlayHints.BlockEnd) markBlockEnd(S->getBody(), "for", getSimpleName(*S->getLoopVariable())); return true; } - bool VisitWhileStmt(WhileStmt *S) { + bool VisitWhileStmt(const WhileStmt *S) override { if (Cfg.InlayHints.BlockEnd) markBlockEnd(S->getBody(), "while", summarizeExpr(S->getCond())); return true; } - bool VisitSwitchStmt(SwitchStmt *S) { + bool VisitSwitchStmt(const SwitchStmt *S) override { if (Cfg.InlayHints.BlockEnd) markBlockEnd(S->getBody(), "switch", summarizeExpr(S->getCond())); return true; @@ -553,7 +553,7 @@ class InlayHintVisitor : public RecursiveASTVisitor { // For now, the answer is neither, just mark as "if". // The ElseIf is a different IfStmt that doesn't know about the outer one. llvm::DenseSet ElseIfs; // not eligible for names - bool VisitIfStmt(IfStmt *S) { + bool VisitIfStmt(const IfStmt *S) override { if (Cfg.InlayHints.BlockEnd) { if (const auto *ElseIf = llvm::dyn_cast_or_null(S->getElse())) ElseIfs.insert(ElseIf); @@ -574,7 +574,7 @@ class InlayHintVisitor : public RecursiveASTVisitor { addBlockEndHint(CS->getSourceRange(), Label, Name, ""); } - bool VisitTagDecl(TagDecl *D) { + bool VisitTagDecl(const TagDecl *D) override { if (Cfg.InlayHints.BlockEnd && D->isThisDeclarationADefinition()) { std::string DeclPrefix = D->getKindName().str(); if (const auto *ED = dyn_cast(D)) { @@ -586,7 +586,7 @@ class InlayHintVisitor : public RecursiveASTVisitor { return true; } - bool VisitNamespaceDecl(NamespaceDecl *D) { + bool VisitNamespaceDecl(const NamespaceDecl *D) override { if (Cfg.InlayHints.BlockEnd) { // For namespace, the range actually starts at the namespace keyword. But // it should be fine since it's usually very short. @@ -595,7 +595,7 @@ class InlayHintVisitor : public RecursiveASTVisitor { return true; } - bool VisitLambdaExpr(LambdaExpr *E) { + bool VisitLambdaExpr(const LambdaExpr *E) override { FunctionDecl *D = E->getCallOperator(); if (!E->hasExplicitResultType()) { SourceLocation TypeHintLoc; @@ -609,14 +609,14 @@ class InlayHintVisitor : public RecursiveASTVisitor { return true; } - void addReturnTypeHint(FunctionDecl *D, SourceRange Range) { + void addReturnTypeHint(const FunctionDecl *D, SourceRange Range) { auto *AT = D->getReturnType()->getContainedAutoType(); if (!AT || AT->getDeducedType().isNull()) return; addTypeHint(Range, D->getReturnType(), /*Prefix=*/"-> "); } - bool VisitVarDecl(VarDecl *D) { + bool VisitVarDecl(const VarDecl *D) override { // Do not show hints for the aggregate in a structured binding, // but show hints for the individual bindings. if (auto *DD = dyn_cast(D)) { @@ -661,11 +661,11 @@ class InlayHintVisitor : public RecursiveASTVisitor { } // Handle templates like `int foo(auto x)` with exactly one instantiation. - if (auto *PVD = llvm::dyn_cast(D)) { + if (const auto *PVD = llvm::dyn_cast(D)) { if (D->getIdentifier() && PVD->getType()->isDependentType() && !getContainedAutoParamType(D->getTypeSourceInfo()->getTypeLoc()) .isNull()) { - if (auto *IPVD = getOnlyParamInstantiation(PVD)) + if (const auto *IPVD = getOnlyParamInstantiation(PVD)) addTypeHint(D->getLocation(), IPVD->getType(), /*Prefix=*/": "); } } @@ -673,11 +673,11 @@ class InlayHintVisitor : public RecursiveASTVisitor { return true; } - ParmVarDecl *getOnlyParamInstantiation(ParmVarDecl *D) { + const ParmVarDecl *getOnlyParamInstantiation(const ParmVarDecl *D) { auto *TemplateFunction = llvm::dyn_cast(D->getDeclContext()); if (!TemplateFunction) return nullptr; - auto *InstantiatedFunction = llvm::dyn_cast_or_null( + const auto *InstantiatedFunction = llvm::dyn_cast_or_null( getOnlyInstantiation(TemplateFunction)); if (!InstantiatedFunction) return nullptr; @@ -699,7 +699,7 @@ class InlayHintVisitor : public RecursiveASTVisitor { return InstantiatedFunction->getParamDecl(ParamIdx); } - bool VisitInitListExpr(InitListExpr *Syn) { + bool VisitInitListExpr(const InitListExpr *Syn) override { // We receive the syntactic form here (shouldVisitImplicitCode() is false). // This is the one we will ultimately attach designators to. // It may have subobject initializers inlined without braces. The *semantic* @@ -1150,8 +1150,8 @@ class InlayHintVisitor : public RecursiveASTVisitor { return Range{HintStart, HintEnd}; } - static bool isFunctionObjectCallExpr(CallExpr *E) noexcept { - if (auto *CallExpr = dyn_cast(E)) + static bool isFunctionObjectCallExpr(const CallExpr *E) noexcept { + if (const auto *CallExpr = dyn_cast(E)) return CallExpr->getOperator() == OverloadedOperatorKind::OO_Call; return false; } diff --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp index ab720ebe6b47f..166d71dd27ed0 100644 --- a/clang-tools-extra/clangd/SemanticHighlighting.cpp +++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -19,8 +19,8 @@ #include "clang/AST/DeclObjC.h" #include "clang/AST/DeclTemplate.h" #include "clang/AST/DeclarationName.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/ExprCXX.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/Type.h" #include "clang/AST/TypeLoc.h" #include "clang/Basic/LangOptions.h" @@ -637,34 +637,33 @@ std::optional scopeModifier(const Type *T) { /// Produces highlightings, which are not captured by findExplicitReferences, /// e.g. highlights dependent names and 'auto' as the underlying type. -class CollectExtraHighlightings - : public RecursiveASTVisitor { - using Base = RecursiveASTVisitor; +class CollectExtraHighlightings : public ConstDynamicRecursiveASTVisitor { + using Base = ConstDynamicRecursiveASTVisitor; public: CollectExtraHighlightings(HighlightingsBuilder &H) : H(H) {} - bool VisitCXXConstructExpr(CXXConstructExpr *E) { + bool VisitCXXConstructExpr(const CXXConstructExpr *E) override { highlightMutableReferenceArguments(E->getConstructor(), {E->getArgs(), E->getNumArgs()}); return true; } - bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { + bool TraverseConstructorInitializer(const CXXCtorInitializer *Init) override { if (Init->isMemberInitializer()) if (auto *Member = Init->getMember()) highlightMutableReferenceArgument(Member->getType(), Init->getInit()); return Base::TraverseConstructorInitializer(Init); } - bool TraverseTypeConstraint(const TypeConstraint *C) { + bool TraverseTypeConstraint(const TypeConstraint *C) override { if (auto *Args = C->getTemplateArgsAsWritten()) H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc()); return Base::TraverseTypeConstraint(C); } - bool VisitPredefinedExpr(PredefinedExpr *E) { + bool VisitPredefinedExpr(const PredefinedExpr *E) override { H.addToken(E->getLocation(), HighlightingKind::LocalVariable) .addModifier(HighlightingModifier::Static) .addModifier(HighlightingModifier::Readonly) @@ -672,19 +671,20 @@ class CollectExtraHighlightings return true; } - bool VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) { + bool + VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E) override { if (auto *Args = E->getTemplateArgsAsWritten()) H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc()); return true; } - bool VisitTemplateDecl(TemplateDecl *D) { + bool VisitTemplateDecl(const TemplateDecl *D) override { if (auto *TPL = D->getTemplateParameters()) H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc()); return true; } - bool VisitTagDecl(TagDecl *D) { + bool VisitTagDecl(const TagDecl *D) override { for (unsigned i = 0; i < D->getNumTemplateParameterLists(); ++i) { if (auto *TPL = D->getTemplateParameterList(i)) H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc()); @@ -692,43 +692,44 @@ class CollectExtraHighlightings return true; } - bool - VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *D) { + bool VisitClassTemplateSpecializationDecl( + const ClassTemplateSpecializationDecl *D) override { if (auto *Args = D->getTemplateArgsAsWritten()) H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc()); return true; } bool VisitClassTemplatePartialSpecializationDecl( - ClassTemplatePartialSpecializationDecl *D) { + const ClassTemplatePartialSpecializationDecl *D) override { if (auto *TPL = D->getTemplateParameters()) H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc()); return true; } - bool VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) { + bool VisitVarTemplateSpecializationDecl( + const VarTemplateSpecializationDecl *D) override { if (auto *Args = D->getTemplateArgsAsWritten()) H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc()); return true; } bool VisitVarTemplatePartialSpecializationDecl( - VarTemplatePartialSpecializationDecl *D) { + const VarTemplatePartialSpecializationDecl *D) override { if (auto *TPL = D->getTemplateParameters()) H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc()); return true; } - bool VisitDeclRefExpr(DeclRefExpr *E) { + bool VisitDeclRefExpr(const DeclRefExpr *E) override { H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc()); return true; } - bool VisitMemberExpr(MemberExpr *E) { + bool VisitMemberExpr(const MemberExpr *E) override { H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc()); return true; } - bool VisitFunctionDecl(FunctionDecl *D) { + bool VisitFunctionDecl(const FunctionDecl *D) override { if (D->isOverloadedOperator()) { const auto AddOpDeclToken = [&](SourceLocation Loc) { auto &Token = H.addToken(Loc, HighlightingKind::Operator) @@ -747,7 +748,7 @@ class CollectExtraHighlightings return true; } - bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) { + bool VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *E) override { const auto AddOpToken = [&](SourceLocation Loc) { H.addToken(Loc, HighlightingKind::Operator) .addModifier(HighlightingModifier::UserDefined); @@ -761,47 +762,47 @@ class CollectExtraHighlightings return true; } - bool VisitUnaryOperator(UnaryOperator *Op) { + bool VisitUnaryOperator(const UnaryOperator *Op) override { auto &Token = H.addToken(Op->getOperatorLoc(), HighlightingKind::Operator); if (Op->getSubExpr()->isTypeDependent()) Token.addModifier(HighlightingModifier::UserDefined); return true; } - bool VisitBinaryOperator(BinaryOperator *Op) { + bool VisitBinaryOperator(const BinaryOperator *Op) override { auto &Token = H.addToken(Op->getOperatorLoc(), HighlightingKind::Operator); if (Op->getLHS()->isTypeDependent() || Op->getRHS()->isTypeDependent()) Token.addModifier(HighlightingModifier::UserDefined); return true; } - bool VisitConditionalOperator(ConditionalOperator *Op) { + bool VisitConditionalOperator(const ConditionalOperator *Op) override { H.addToken(Op->getQuestionLoc(), HighlightingKind::Operator); H.addToken(Op->getColonLoc(), HighlightingKind::Operator); return true; } - bool VisitCXXNewExpr(CXXNewExpr *E) { + bool VisitCXXNewExpr(const CXXNewExpr *E) override { auto &Token = H.addToken(E->getBeginLoc(), HighlightingKind::Operator); if (isa_and_present(E->getOperatorNew())) Token.addModifier(HighlightingModifier::UserDefined); return true; } - bool VisitCXXDeleteExpr(CXXDeleteExpr *E) { + bool VisitCXXDeleteExpr(const CXXDeleteExpr *E) override { auto &Token = H.addToken(E->getBeginLoc(), HighlightingKind::Operator); if (isa_and_present(E->getOperatorDelete())) Token.addModifier(HighlightingModifier::UserDefined); return true; } - bool VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { + bool VisitCXXNamedCastExpr(const CXXNamedCastExpr *E) override { const auto &B = E->getAngleBrackets(); H.addAngleBracketTokens(B.getBegin(), B.getEnd()); return true; } - bool VisitCallExpr(CallExpr *E) { + bool VisitCallExpr(const CallExpr *E) override { // Highlighting parameters passed by non-const reference does not really // make sense for literals... if (isa(E)) @@ -880,7 +881,7 @@ class CollectExtraHighlightings } } - bool VisitDecltypeTypeLoc(DecltypeTypeLoc L) { + bool VisitDecltypeTypeLoc(const DecltypeTypeLoc L) override { if (auto K = kindForType(L.getTypePtr(), H.getResolver())) { auto &Tok = H.addToken(L.getBeginLoc(), *K) .addModifier(HighlightingModifier::Deduced); @@ -892,7 +893,7 @@ class CollectExtraHighlightings return true; } - bool VisitCXXDestructorDecl(CXXDestructorDecl *D) { + bool VisitCXXDestructorDecl(const CXXDestructorDecl *D) override { if (auto *TI = D->getNameInfo().getNamedTypeInfo()) { SourceLocation Loc = TI->getTypeLoc().getBeginLoc(); H.addExtraModifier(Loc, HighlightingModifier::ConstructorOrDestructor); @@ -903,7 +904,7 @@ class CollectExtraHighlightings return true; } - bool VisitCXXMemberCallExpr(CXXMemberCallExpr *CE) { + bool VisitCXXMemberCallExpr(const CXXMemberCallExpr *CE) override { // getMethodDecl can return nullptr with member pointers, e.g. // `(foo.*pointer_to_member_fun)(arg);` if (auto *D = CE->getMethodDecl()) { @@ -925,7 +926,7 @@ class CollectExtraHighlightings return true; } - bool VisitDeclaratorDecl(DeclaratorDecl *D) { + bool VisitDeclaratorDecl(const DeclaratorDecl *D) override { for (unsigned i = 0; i < D->getNumTemplateParameterLists(); ++i) { if (auto *TPL = D->getTemplateParameterList(i)) H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc()); @@ -979,7 +980,7 @@ class CollectExtraHighlightings } } - bool VisitObjCMethodDecl(ObjCMethodDecl *OMD) { + bool VisitObjCMethodDecl(const ObjCMethodDecl *OMD) override { llvm::SmallVector Locs; OMD->getSelectorLocs(Locs); highlightObjCSelector(Locs, /*Decl=*/true, @@ -988,11 +989,11 @@ class CollectExtraHighlightings return true; } - bool VisitObjCMessageExpr(ObjCMessageExpr *OME) { + bool VisitObjCMessageExpr(const ObjCMessageExpr *OME) override { llvm::SmallVector Locs; OME->getSelectorLocs(Locs); bool DefaultLibrary = false; - if (ObjCMethodDecl *OMD = OME->getMethodDecl()) + if (const ObjCMethodDecl *OMD = OME->getMethodDecl()) DefaultLibrary = isDefaultLibrary(OMD); highlightObjCSelector(Locs, /*Decl=*/false, /*Def=*/false, OME->isClassMessage(), DefaultLibrary); @@ -1013,7 +1014,7 @@ class CollectExtraHighlightings Tok.addModifier(HighlightingModifier::DefaultLibrary); } - bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *OPRE) { + bool VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *OPRE) override { // We need to handle implicit properties here since they will appear to // reference `ObjCMethodDecl` via an implicit `ObjCMessageExpr`, so normal // highlighting will not work. @@ -1035,7 +1036,7 @@ class CollectExtraHighlightings return true; } - bool VisitOverloadExpr(OverloadExpr *E) { + bool VisitOverloadExpr(const OverloadExpr *E) override { H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc()); if (!E->decls().empty()) return true; // handled by findExplicitReferences. @@ -1047,7 +1048,8 @@ class CollectExtraHighlightings return true; } - bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { + bool VisitCXXDependentScopeMemberExpr( + const CXXDependentScopeMemberExpr *E) override { H.addToken(E->getMemberNameInfo().getLoc(), HighlightingKind::Unknown) .addModifier(HighlightingModifier::DependentName) .addModifier(HighlightingModifier::ClassScope); @@ -1055,7 +1057,8 @@ class CollectExtraHighlightings return true; } - bool VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { + bool + VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) override { H.addToken(E->getNameInfo().getLoc(), HighlightingKind::Unknown) .addModifier(HighlightingModifier::DependentName) .addModifier(HighlightingModifier::ClassScope); @@ -1063,7 +1066,7 @@ class CollectExtraHighlightings return true; } - bool VisitAttr(Attr *A) { + bool VisitAttr(const Attr *A) override { switch (A->getKind()) { case attr::Override: case attr::Final: @@ -1075,14 +1078,15 @@ class CollectExtraHighlightings return true; } - bool VisitDependentNameTypeLoc(DependentNameTypeLoc L) { + bool VisitDependentNameTypeLoc(DependentNameTypeLoc L) override { H.addToken(L.getNameLoc(), HighlightingKind::Type) .addModifier(HighlightingModifier::DependentName) .addModifier(HighlightingModifier::ClassScope); return true; } - bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) { + bool + VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) override { if (!L.getTypePtr()->getTemplateName().getAsTemplateDecl( /*IgnoreDeduced=*/true)) H.addToken(L.getTemplateNameLoc(), HighlightingKind::Type) @@ -1092,12 +1096,12 @@ class CollectExtraHighlightings return true; } - bool TraverseTemplateArgumentLoc(TemplateArgumentLoc L) { + bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &L) override { // Handle template template arguments only (other arguments are handled by // their Expr, TypeLoc etc values). if (L.getArgument().getKind() != TemplateArgument::Template && L.getArgument().getKind() != TemplateArgument::TemplateExpansion) - return RecursiveASTVisitor::TraverseTemplateArgumentLoc(L); + return ConstDynamicRecursiveASTVisitor::TraverseTemplateArgumentLoc(L); TemplateName N = L.getArgument().getAsTemplateOrTemplatePattern(); switch (N.getKind()) { @@ -1120,7 +1124,7 @@ class CollectExtraHighlightings // Names that could be resolved to a TemplateDecl are handled elsewhere. break; } - return RecursiveASTVisitor::TraverseTemplateArgumentLoc(L); + return ConstDynamicRecursiveASTVisitor::TraverseTemplateArgumentLoc(L); } private: diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp index 05e04ac161e54..e2af24ad2398c 100644 --- a/clang-tools-extra/clangd/XRefs.cpp +++ b/clang-tools-extra/clangd/XRefs.cpp @@ -35,8 +35,8 @@ #include "clang/AST/DeclObjC.h" #include "clang/AST/DeclTemplate.h" #include "clang/AST/DeclVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/ExprCXX.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/Stmt.h" #include "clang/AST/StmtCXX.h" #include "clang/AST/StmtVisitor.h" @@ -1043,7 +1043,7 @@ const Stmt *getLoopBody(DynTypedNode N) { // AST traversal to highlight control flow statements under some root. // Once we hit further control flow we prune the tree (or at least restrict // what we highlight) so we capture e.g. breaks from the outer loop only. -class FindControlFlow : public RecursiveASTVisitor { +class FindControlFlow : public ConstDynamicRecursiveASTVisitor { // Types of control-flow statements we might highlight. enum Target { Break = 1, @@ -1091,39 +1091,39 @@ class FindControlFlow : public RecursiveASTVisitor { // When traversing function or loops, limit targets to those that still // refer to the original root. - bool TraverseDecl(Decl *D) { + bool TraverseDecl(const Decl *D) override { return !D || filterAndTraverse(DynTypedNode::create(*D), [&] { - return RecursiveASTVisitor::TraverseDecl(D); + return ConstDynamicRecursiveASTVisitor::TraverseDecl(D); }); } - bool TraverseStmt(Stmt *S) { + bool TraverseStmt(const Stmt *S) override { return !S || filterAndTraverse(DynTypedNode::create(*S), [&] { - return RecursiveASTVisitor::TraverseStmt(S); + return ConstDynamicRecursiveASTVisitor::TraverseStmt(S); }); } // Add leaves that we found and want. - bool VisitReturnStmt(ReturnStmt *R) { + bool VisitReturnStmt(const ReturnStmt *R) override { found(Return, R->getReturnLoc()); return true; } - bool VisitBreakStmt(BreakStmt *B) { + bool VisitBreakStmt(const BreakStmt *B) override { found(Break, B->getKwLoc()); return true; } - bool VisitContinueStmt(ContinueStmt *C) { + bool VisitContinueStmt(const ContinueStmt *C) override { found(Continue, C->getKwLoc()); return true; } - bool VisitSwitchCase(SwitchCase *C) { + bool VisitSwitchCase(const SwitchCase *C) override { found(Case, C->getKeywordLoc()); return true; } - bool VisitCXXThrowExpr(CXXThrowExpr *T) { + bool VisitCXXThrowExpr(const CXXThrowExpr *T) override { found(Throw, T->getThrowLoc()); return true; } - bool VisitGotoStmt(GotoStmt *G) { + bool VisitGotoStmt(const GotoStmt *G) override { // Goto is interesting if its target is outside the root. if (const auto *LD = G->getLabel()) { if (SM.isBeforeInTranslationUnit(LD->getLocation(), Bounds.getBegin()) || @@ -1243,7 +1243,7 @@ std::vector relatedControlFlow(const SelectionTree::Node &N) { if (Root) { if (!Bounds.isValid()) Bounds = Root->getSourceRange(); - FindControlFlow(Bounds, Result, SM).TraverseStmt(const_cast(Root)); + FindControlFlow(Bounds, Result, SM).TraverseStmt(Root); } return Result; } diff --git a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp index f65c74fdbc9ee..6acbc481257a2 100644 --- a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp +++ b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp @@ -12,9 +12,9 @@ #include "refactor/Tweak.h" #include "support/Logger.h" #include "clang/AST/Decl.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/Expr.h" #include "clang/AST/NestedNameSpecifier.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/Type.h" #include "clang/AST/TypeLoc.h" #include "clang/Basic/LLVM.h" @@ -79,13 +79,13 @@ std::string AddUsing::title() const { } // Locates all "using" statements relevant to SelectionDeclContext. -class UsingFinder : public RecursiveASTVisitor { +class UsingFinder : public ConstDynamicRecursiveASTVisitor { public: UsingFinder(std::vector &Results, const DeclContext *SelectionDeclContext, const SourceManager &SM) : Results(Results), SelectionDeclContext(SelectionDeclContext), SM(SM) {} - bool VisitUsingDecl(UsingDecl *D) { + bool VisitUsingDecl(const UsingDecl *D) override { auto Loc = D->getUsingLoc(); if (SM.getFileID(Loc) != SM.getMainFileID()) { return true; @@ -96,7 +96,7 @@ class UsingFinder : public RecursiveASTVisitor { return true; } - bool TraverseDecl(Decl *Node) { + bool TraverseDecl(const Decl *Node) override { if (!Node) return true; // There is no need to go deeper into nodes that do not enclose selection, @@ -104,7 +104,7 @@ class UsingFinder : public RecursiveASTVisitor { // insertion point. if (!Node->getDeclContext() || Node->getDeclContext()->Encloses(SelectionDeclContext)) { - return RecursiveASTVisitor::TraverseDecl(Node); + return ConstDynamicRecursiveASTVisitor::TraverseDecl(Node); } return true; } diff --git a/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp index bc9a790232507..5678ff565e515 100644 --- a/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp +++ b/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp @@ -56,9 +56,9 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclBase.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/ExprCXX.h" #include "clang/AST/NestedNameSpecifier.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/Stmt.h" #include "clang/Basic/LangOptions.h" #include "clang/Basic/SourceLocation.h" @@ -576,24 +576,23 @@ CapturedZoneInfo captureZoneInfo(const ExtractionZone &ExtZone) { // We use the ASTVisitor instead of using the selection tree since we need to // find references in the PostZone as well. // FIXME: Check which statements we don't allow to extract. - class ExtractionZoneVisitor - : public clang::RecursiveASTVisitor { + class ExtractionZoneVisitor : public ConstDynamicRecursiveASTVisitor { public: ExtractionZoneVisitor(const ExtractionZone &ExtZone) : ExtZone(ExtZone) { - TraverseDecl(const_cast(ExtZone.EnclosingFunction)); + ExtractionZoneVisitor::TraverseDecl(ExtZone.EnclosingFunction); } - bool TraverseStmt(Stmt *S) { + bool TraverseStmt(const Stmt *S) override { if (!S) return true; - bool IsRootStmt = ExtZone.isRootStmt(const_cast(S)); + bool IsRootStmt = ExtZone.isRootStmt(S); // If we are starting traversal of a RootStmt, we are somewhere inside // ExtractionZone if (IsRootStmt) CurrentLocation = ZoneRelative::Inside; addToLoopSwitchCounters(S, 1); // Traverse using base class's TraverseStmt - RecursiveASTVisitor::TraverseStmt(S); + ConstDynamicRecursiveASTVisitor::TraverseStmt(S); addToLoopSwitchCounters(S, -1); // We set the current location as after since next stmt will either be a // RootStmt (handled at the beginning) or after extractionZone @@ -604,7 +603,7 @@ CapturedZoneInfo captureZoneInfo(const ExtractionZone &ExtZone) { // Add Increment to CurNumberOf{Loops,Switch} if statement is // {Loop,Switch} and inside Extraction Zone. - void addToLoopSwitchCounters(Stmt *S, int Increment) { + void addToLoopSwitchCounters(const Stmt *S, int Increment) { if (CurrentLocation != ZoneRelative::Inside) return; if (isLoop(S)) @@ -613,12 +612,12 @@ CapturedZoneInfo captureZoneInfo(const ExtractionZone &ExtZone) { CurNumberOfSwitch += Increment; } - bool VisitDecl(Decl *D) { + bool VisitDecl(const Decl *D) override { Info.createDeclInfo(D, CurrentLocation); return true; } - bool VisitDeclRefExpr(DeclRefExpr *DRE) { + bool VisitDeclRefExpr(const DeclRefExpr *DRE) override { // Find the corresponding Decl and mark it's occurrence. const Decl *D = DRE->getDecl(); auto *DeclInfo = Info.getDeclInfoFor(D); @@ -630,13 +629,13 @@ CapturedZoneInfo captureZoneInfo(const ExtractionZone &ExtZone) { return true; } - bool VisitReturnStmt(ReturnStmt *Return) { + bool VisitReturnStmt(const ReturnStmt *Return) override { if (CurrentLocation == ZoneRelative::Inside) Info.HasReturnStmt = true; return true; } - bool VisitBreakStmt(BreakStmt *Break) { + bool VisitBreakStmt(const BreakStmt *Break) override { // Control flow is broken if break statement is selected without any // parent loop or switch statement. if (CurrentLocation == ZoneRelative::Inside && @@ -645,7 +644,7 @@ CapturedZoneInfo captureZoneInfo(const ExtractionZone &ExtZone) { return true; } - bool VisitContinueStmt(ContinueStmt *Continue) { + bool VisitContinueStmt(const ContinueStmt *Continue) override { // Control flow is broken if Continue statement is selected without any // parent loop if (CurrentLocation == ZoneRelative::Inside && !CurNumberOfNestedLoops) @@ -851,10 +850,9 @@ tooling::Replacement createForwardDeclaration(const NewFunction &ExtractedFunc, // Returns true if ExtZone contains any ReturnStmts. bool hasReturnStmt(const ExtractionZone &ExtZone) { - class ReturnStmtVisitor - : public clang::RecursiveASTVisitor { + class ReturnStmtVisitor : public ConstDynamicRecursiveASTVisitor { public: - bool VisitReturnStmt(ReturnStmt *Return) { + bool VisitReturnStmt(const ReturnStmt *Return) override { Found = true; return false; // We found the answer, abort the scan. } @@ -863,7 +861,7 @@ bool hasReturnStmt(const ExtractionZone &ExtZone) { ReturnStmtVisitor V; for (const Stmt *RootStmt : ExtZone.RootStmts) { - V.TraverseStmt(const_cast(RootStmt)); + V.TraverseStmt(RootStmt); if (V.Found) break; } diff --git a/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp index c74250ccbe9ea..1d722b1b142f0 100644 --- a/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp +++ b/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp @@ -14,11 +14,11 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/Expr.h" #include "clang/AST/ExprCXX.h" #include "clang/AST/LambdaCapture.h" #include "clang/AST/OperationKinds.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/Stmt.h" #include "clang/AST/StmtCXX.h" #include "clang/Basic/LangOptions.h" @@ -62,7 +62,7 @@ class ExtractionContext { const SourceManager &SM; const ASTContext &Ctx; // Decls referenced in the Expr - std::vector ReferencedDecls; + std::vector ReferencedDecls; // returns true if the Expr doesn't reference any variable declared in scope bool exprIsValidOutside(const clang::Stmt *Scope) const; // computes the Stmt before which we will extract out Expr @@ -70,14 +70,12 @@ class ExtractionContext { }; // Returns all the Decls referenced inside the given Expr -static std::vector -computeReferencedDecls(const clang::Expr *Expr) { +std::vector computeReferencedDecls(const Expr *Expr) { // RAV subclass to find all DeclRefs in a given Stmt - class FindDeclRefsVisitor - : public clang::RecursiveASTVisitor { + class FindDeclRefsVisitor : public ConstDynamicRecursiveASTVisitor { public: - std::vector ReferencedDecls; - bool VisitDeclRefExpr(DeclRefExpr *DeclRef) { // NOLINT + std::vector ReferencedDecls; + bool VisitDeclRefExpr(const DeclRefExpr *DeclRef) override { // NOLINT // Stop the call operator of lambdas from being marked as a referenced // DeclRefExpr in immediately invoked lambdas. if (const auto *const Method = @@ -94,7 +92,7 @@ computeReferencedDecls(const clang::Expr *Expr) { // the DeclRefExprs inside the initializers of init-capture variables, // variables mentioned in trailing return types, constraints and explicit // defaulted template parameters. - bool TraverseLambdaExpr(LambdaExpr *LExpr) { + bool TraverseLambdaExpr(const LambdaExpr *LExpr) override { for (const auto &[Capture, Initializer] : llvm::zip(LExpr->captures(), LExpr->capture_inits())) { TraverseLambdaCapture(LExpr, &Capture, Initializer); @@ -102,7 +100,7 @@ computeReferencedDecls(const clang::Expr *Expr) { if (const clang::Expr *RequiresClause = LExpr->getTrailingRequiresClause().ConstraintExpr) { - TraverseStmt(const_cast(RequiresClause)); + TraverseStmt(RequiresClause); } for (auto *const TemplateParam : LExpr->getExplicitTemplateParameters()) @@ -125,7 +123,7 @@ computeReferencedDecls(const clang::Expr *Expr) { }; FindDeclRefsVisitor Visitor; - Visitor.TraverseStmt(const_cast(cast(Expr))); + Visitor.TraverseStmt(cast(Expr)); return Visitor.ReferencedDecls; } diff --git a/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp b/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp index 5baa970bcb0c5..db258e0225410 100644 --- a/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp +++ b/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp @@ -13,7 +13,7 @@ #include "clang/AST/Decl.h" #include "clang/AST/DeclBase.h" #include "clang/AST/DeclCXX.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/Basic/SourceLocation.h" #include "clang/Tooling/Core/Replacement.h" #include @@ -48,14 +48,14 @@ class RemoveUsingNamespace : public Tweak { }; REGISTER_TWEAK(RemoveUsingNamespace) -class FindSameUsings : public RecursiveASTVisitor { +class FindSameUsings : public ConstDynamicRecursiveASTVisitor { public: FindSameUsings(const UsingDirectiveDecl &Target, std::vector &Results) : TargetNS(Target.getNominatedNamespace()), TargetCtx(Target.getDeclContext()), Results(Results) {} - bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { + bool VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) override { if (D->getNominatedNamespace() != TargetNS || D->getDeclContext() != TargetCtx) return true; diff --git a/clang-tools-extra/clangd/unittests/ASTTests.cpp b/clang-tools-extra/clangd/unittests/ASTTests.cpp index 91ae727d8c944..25151873c33c1 100644 --- a/clang-tools-extra/clangd/unittests/ASTTests.cpp +++ b/clang-tools-extra/clangd/unittests/ASTTests.cpp @@ -305,11 +305,11 @@ TEST(ClangdAST, GetOnlyInstantiation) { PrintingPolicy PP = AST.getASTContext().getPrintingPolicy(); PP.TerseOutput = true; std::string Name; - if (auto *Result = getOnlyInstantiation( - const_cast(&findDecl(AST, [&](const NamedDecl &D) { + if (const auto *Result = + getOnlyInstantiation(&findDecl(AST, [&](const NamedDecl &D) { return D.getDescribedTemplate() != nullptr && D.getDeclKindName() == Case.NodeType; - })))) { + }))) { llvm::raw_string_ostream OS(Name); Result->print(OS, PP); } diff --git a/clang-tools-extra/clangd/unittests/PrintASTTests.cpp b/clang-tools-extra/clangd/unittests/PrintASTTests.cpp index 746966bbf171b..81e686d1cc876 100644 --- a/clang-tools-extra/clangd/unittests/PrintASTTests.cpp +++ b/clang-tools-extra/clangd/unittests/PrintASTTests.cpp @@ -11,7 +11,7 @@ #include "Protocol.h" #include "SourceCode.h" #include "TestTU.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -32,9 +32,9 @@ TEST_P(ASTUtils, PrintTemplateArgs) { auto Pair = GetParam(); Annotations Test(Pair.AnnotatedCode); auto AST = TestTU::withCode(Test.code()).build(); - struct Visitor : RecursiveASTVisitor { + struct Visitor : ConstDynamicRecursiveASTVisitor { Visitor(std::vector Points) : Points(std::move(Points)) {} - bool VisitNamedDecl(const NamedDecl *ND) { + bool VisitNamedDecl(const NamedDecl *ND) override { if (TemplateArgsAtPoints.size() == Points.size()) return true; auto Pos = sourceLocToPosition(ND->getASTContext().getSourceManager(), diff --git a/clang-tools-extra/clangd/unittests/TestTU.cpp b/clang-tools-extra/clangd/unittests/TestTU.cpp index a733fac932bf4..62a17d278d7cd 100644 --- a/clang-tools-extra/clangd/unittests/TestTU.cpp +++ b/clang-tools-extra/clangd/unittests/TestTU.cpp @@ -13,7 +13,7 @@ #include "TestFS.h" #include "index/FileIndex.h" #include "index/SymbolOrigin.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/Basic/Diagnostic.h" #include "clang/Frontend/CompilerInvocation.h" #include "llvm/ADT/ScopeExit.h" @@ -240,10 +240,10 @@ const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName) { const NamedDecl &findDecl(ParsedAST &AST, std::function Filter) { TraverseHeadersToo Too(AST); - struct Visitor : RecursiveASTVisitor { + struct Visitor : ConstDynamicRecursiveASTVisitor { decltype(Filter) F; llvm::SmallVector Decls; - bool VisitNamedDecl(const NamedDecl *ND) { + bool VisitNamedDecl(const NamedDecl *ND) override { if (F(*ND)) Decls.push_back(ND); return true; diff --git a/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp b/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp index 0ac243937e6e4..b0a3b0d4d1549 100644 --- a/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp +++ b/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp @@ -11,8 +11,8 @@ #include "clang-include-cleaner/Analysis.h" #include "clang-include-cleaner/Record.h" #include "clang-include-cleaner/Types.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/Expr.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/Basic/FileEntry.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/LLVM.h" @@ -182,9 +182,9 @@ TEST_F(FindHeadersTest, NonSelfContainedTraverseExporter) { } TEST_F(FindHeadersTest, TargetIsExpandedFromMacroInHeader) { - struct CustomVisitor : RecursiveASTVisitor { + struct CustomVisitor : ConstDynamicRecursiveASTVisitor { const Decl *Out = nullptr; - bool VisitNamedDecl(const NamedDecl *ND) { + bool VisitNamedDecl(const NamedDecl *ND) override { if (ND->getName() == "FLAG_foo" || ND->getName() == "Foo") { EXPECT_TRUE(Out == nullptr); Out = ND; @@ -285,11 +285,11 @@ TEST_F(FindHeadersTest, PreferredHeaderHint) { class HeadersForSymbolTest : public FindHeadersTest { protected: llvm::SmallVector
headersFor(llvm::StringRef Name) { - struct Visitor : public RecursiveASTVisitor { + struct Visitor : ConstDynamicRecursiveASTVisitor { const NamedDecl *Out = nullptr; llvm::StringRef Name; Visitor(llvm::StringRef Name) : Name(Name) {} - bool VisitNamedDecl(const NamedDecl *ND) { + bool VisitNamedDecl(const NamedDecl *ND) override { if (auto *TD = ND->getDescribedTemplate()) ND = TD; @@ -600,9 +600,9 @@ TEST_F(HeadersForSymbolTest, AmbiguousStdSymbolsUsingShadow) { buildAST(); // Find the DeclRefExpr in the std::remove("abc") function call. - struct Visitor : public RecursiveASTVisitor { + struct Visitor : ConstDynamicRecursiveASTVisitor { const DeclRefExpr *Out = nullptr; - bool VisitDeclRefExpr(const DeclRefExpr *DRE) { + bool VisitDeclRefExpr(const DeclRefExpr *DRE) override { EXPECT_TRUE(Out == nullptr) << "Found multiple DeclRefExpr!"; Out = DRE; return true; diff --git a/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp b/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp index 1e7baf142a75a..360a0e10b3d2d 100644 --- a/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp +++ b/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp @@ -10,7 +10,7 @@ #include "clang-include-cleaner/Types.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclBase.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/Basic/LangOptions.h" #include "clang/Basic/SourceLocation.h" #include "clang/Lex/Preprocessor.h" @@ -51,10 +51,10 @@ struct LocateExample { }()) {} const Decl &findDecl(llvm::StringRef SymbolName) { - struct Visitor : RecursiveASTVisitor { + struct Visitor : ConstDynamicRecursiveASTVisitor { llvm::StringRef NameToFind; const NamedDecl *Out = nullptr; - bool VisitNamedDecl(const NamedDecl *ND) { + bool VisitNamedDecl(const NamedDecl *ND) override { // Skip the templated decls, as they have the same name and matches in // this file care about the outer template name. if (auto *TD = ND->getDescribedTemplate()) diff --git a/clang-tools-extra/modularize/Modularize.cpp b/clang-tools-extra/modularize/Modularize.cpp index 376ad0c7875bf..52752242d6a13 100644 --- a/clang-tools-extra/modularize/Modularize.cpp +++ b/clang-tools-extra/modularize/Modularize.cpp @@ -229,7 +229,7 @@ #include "PreprocessorTracker.h" #include "clang/AST/ASTConsumer.h" #include "clang/AST/ASTContext.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/Basic/SourceManager.h" #include "clang/Driver/Options.h" #include "clang/Frontend/CompilerInstance.h" @@ -525,8 +525,7 @@ class EntityMap : public std::map> { DenseMap AllHeaderContents; }; -class CollectEntitiesVisitor - : public RecursiveASTVisitor { +class CollectEntitiesVisitor : public ConstDynamicRecursiveASTVisitor { public: CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities, Preprocessor &PP, PreprocessorTracker &PPTracker, @@ -534,30 +533,42 @@ class CollectEntitiesVisitor : SM(SM), Entities(Entities), PP(PP), PPTracker(PPTracker), HadErrors(HadErrors) {} - bool TraverseStmt(Stmt *S) { return true; } - bool TraverseType(QualType T) { return true; } - bool TraverseTypeLoc(TypeLoc TL) { return true; } - bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; } - bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { + bool TraverseStmt(const Stmt *S) override { return true; } + bool TraverseType(QualType T, bool TraverseQualifier) override { return true; } - bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) { + bool TraverseTypeLoc(TypeLoc TL, bool TraverseQualifier) override { return true; } - bool TraverseTemplateName(TemplateName Template) { return true; } - bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; } - bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) { + bool TraverseNestedNameSpecifier(NestedNameSpecifier NNS) override { return true; } - bool TraverseTemplateArguments(ArrayRef) { return true; } - bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; } - bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C, - Expr *Init) { + bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) override { + return true; + } + bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) override { + return true; + } + bool TraverseTemplateName(TemplateName Template) override { return true; } + bool TraverseTemplateArgument(const TemplateArgument &Arg) override { + return true; + } + bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) override { + return true; + } + bool TraverseTemplateArguments(ArrayRef) override { + return true; + } + bool TraverseConstructorInitializer(const CXXCtorInitializer *Init) override { + return true; + } + bool TraverseLambdaCapture(const LambdaExpr *LE, const LambdaCapture *C, + const Expr *Init) override { return true; } // Check 'extern "*" {}' block for #include directives. - bool VisitLinkageSpecDecl(LinkageSpecDecl *D) { + bool VisitLinkageSpecDecl(const LinkageSpecDecl *D) override { // Bail if not a block. if (!D->hasBraces()) return true; @@ -578,7 +589,7 @@ class CollectEntitiesVisitor } // Check 'namespace (name) {}' block for #include directives. - bool VisitNamespaceDecl(const NamespaceDecl *D) { + bool VisitNamespaceDecl(const NamespaceDecl *D) override { SourceRange BlockRange = D->getSourceRange(); std::string Label("namespace "); Label += D->getName(); @@ -590,7 +601,7 @@ class CollectEntitiesVisitor } // Collect definition entities. - bool VisitNamedDecl(NamedDecl *ND) { + bool VisitNamedDecl(const NamedDecl *ND) override { // We only care about file-context variables. if (!ND->getDeclContext()->isFileContext()) return true; @@ -714,47 +725,52 @@ class ModularizeFrontendActionFactory : public FrontendActionFactory { int &HadErrors; }; -class CompileCheckVisitor - : public RecursiveASTVisitor { +class CompileCheckVisitor : public ConstDynamicRecursiveASTVisitor { public: CompileCheckVisitor() {} - bool TraverseStmt(Stmt *S) { return true; } - bool TraverseType(QualType T) { return true; } - bool TraverseTypeLoc(TypeLoc TL) { return true; } - bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; } - bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { + bool TraverseStmt(const Stmt *S) override { return true; } + bool TraverseType(QualType T, bool TraverseQualifier) override { return true; } - bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) { + bool TraverseTypeLoc(TypeLoc TL, bool TraverseQualifier) override { return true; } - bool TraverseTemplateName(TemplateName Template) { return true; } - bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; } - bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) { + bool TraverseNestedNameSpecifier(NestedNameSpecifier NNS) override { return true; } - bool TraverseTemplateArguments(ArrayRef) { return true; } - bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; } - bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C, - Expr *Init) { + bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) override { return true; } - - // Check 'extern "*" {}' block for #include directives. - bool VisitLinkageSpecDecl(LinkageSpecDecl *D) { + bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) override { return true; } - - // Check 'namespace (name) {}' block for #include directives. - bool VisitNamespaceDecl(const NamespaceDecl *D) { + bool TraverseTemplateName(TemplateName Template) override { return true; } + bool TraverseTemplateArgument(const TemplateArgument &Arg) override { return true; } - - // Collect definition entities. - bool VisitNamedDecl(NamedDecl *ND) { + bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) override { + return true; + } + bool TraverseTemplateArguments(ArrayRef) override { + return true; + } + bool TraverseConstructorInitializer(const CXXCtorInitializer *Init) override { return true; } + bool TraverseLambdaCapture(const LambdaExpr *LE, const LambdaCapture *C, + const Expr *Init) override { + return true; + } + + // Check 'extern "*" {}' block for #include directives. + bool VisitLinkageSpecDecl(const LinkageSpecDecl *D) override { return true; } + + // Check 'namespace (name) {}' block for #include directives. + bool VisitNamespaceDecl(const NamespaceDecl *D) override { return true; } + + // Collect definition entities. + bool VisitNamedDecl(const NamedDecl *ND) override { return true; } }; class CompileCheckConsumer : public ASTConsumer { diff --git a/clang-tools-extra/unittests/clang-doc/SerializeTest.cpp b/clang-tools-extra/unittests/clang-doc/SerializeTest.cpp index 6d5e7da689c5c..440de5178a840 100644 --- a/clang-tools-extra/unittests/clang-doc/SerializeTest.cpp +++ b/clang-tools-extra/unittests/clang-doc/SerializeTest.cpp @@ -10,14 +10,13 @@ #include "ClangDocTest.h" #include "Representation.h" #include "clang/AST/Comment.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "gtest/gtest.h" namespace clang { namespace doc { -class ClangDocSerializeTestVisitor - : public RecursiveASTVisitor { +class ClangDocSerializeTestVisitor : public ConstDynamicRecursiveASTVisitor { EmittedInfoList &EmittedInfos; bool Public; @@ -45,24 +44,30 @@ class ClangDocSerializeTestVisitor return true; } - bool VisitNamespaceDecl(const NamespaceDecl *D) { return mapDecl(D); } + bool VisitNamespaceDecl(const NamespaceDecl *D) override { + return mapDecl(D); + } - bool VisitFunctionDecl(const FunctionDecl *D) { + bool VisitFunctionDecl(const FunctionDecl *D) override { // Don't visit CXXMethodDecls twice if (dyn_cast(D)) return true; return mapDecl(D); } - bool VisitCXXMethodDecl(const CXXMethodDecl *D) { return mapDecl(D); } + bool VisitCXXMethodDecl(const CXXMethodDecl *D) override { + return mapDecl(D); + } - bool VisitRecordDecl(const RecordDecl *D) { return mapDecl(D); } + bool VisitRecordDecl(const RecordDecl *D) override { return mapDecl(D); } - bool VisitEnumDecl(const EnumDecl *D) { return mapDecl(D); } + bool VisitEnumDecl(const EnumDecl *D) override { return mapDecl(D); } - bool VisitTypedefDecl(const TypedefDecl *D) { return mapDecl(D); } + bool VisitTypedefDecl(const TypedefDecl *D) override { return mapDecl(D); } - bool VisitTypeAliasDecl(const TypeAliasDecl *D) { return mapDecl(D); } + bool VisitTypeAliasDecl(const TypeAliasDecl *D) override { + return mapDecl(D); + } }; void ExtractInfosFromCode(StringRef Code, size_t NumExpectedInfos, bool Public, diff --git a/clang-tools-extra/unittests/clang-tidy/OverlappingReplacementsTest.cpp b/clang-tools-extra/unittests/clang-tidy/OverlappingReplacementsTest.cpp index 3aaf5491d22c5..1d038b0f22021 100644 --- a/clang-tools-extra/unittests/clang-tidy/OverlappingReplacementsTest.cpp +++ b/clang-tools-extra/unittests/clang-tidy/OverlappingReplacementsTest.cpp @@ -7,7 +7,7 @@ //===----------------------------------------------------------------------===// #include "ClangTidyTest.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "gtest/gtest.h" namespace clang { @@ -83,19 +83,19 @@ class RefactorCheck : public ClangTidyCheck { VD->getLocation()), NewName); - class UsageVisitor : public RecursiveASTVisitor { + class UsageVisitor : public ConstDynamicRecursiveASTVisitor { public: UsageVisitor(const ValueDecl *VD, StringRef NewName, DiagnosticBuilder &Diag) : VD(VD), NewName(NewName), Diag(Diag) {} - bool VisitDeclRefExpr(DeclRefExpr *E) { + bool VisitDeclRefExpr(const DeclRefExpr *E) override { if (const ValueDecl *D = E->getDecl()) { if (VD->getCanonicalDecl() == D->getCanonicalDecl()) { Diag << FixItHint::CreateReplacement( CharSourceRange::getTokenRange(E->getSourceRange()), NewName); } } - return RecursiveASTVisitor::VisitDeclRefExpr(E); + return ConstDynamicRecursiveASTVisitor::VisitDeclRefExpr(E); } private: diff --git a/clang/include/clang/AST/DynamicRecursiveASTVisitor.h b/clang/include/clang/AST/DynamicRecursiveASTVisitor.h index 7b5bdca318348..b752e05a75559 100644 --- a/clang/include/clang/AST/DynamicRecursiveASTVisitor.h +++ b/clang/include/clang/AST/DynamicRecursiveASTVisitor.h @@ -166,8 +166,7 @@ template class DynamicRecursiveASTVisitorBase { /// /// \returns false if the visitation was terminated early, true otherwise. // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead. - // Not virtual for now because no-one overrides it. - bool TraverseTemplateArguments(ArrayRef Args); + virtual bool TraverseTemplateArguments(ArrayRef Args); /// Recursively visit a template name and dispatch to the /// appropriate method. diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index 1d1b7f183f75a..425e699c74843 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -91,6 +91,8 @@ isSameMethod([[maybe_unused]] FirstMethodPtrTy FirstMethodPtr, /// A class that does preorder or postorder /// depth-first traversal on the entire Clang AST and visits each node. /// +/// Prefer to use (Const)DynamicRecursiveASTVisitor instead if possible. +/// /// This class performs three distinct tasks: /// 1. traverse the AST (i.e. go to each node); /// 2. at a given node, walk up the class hierarchy, starting from diff --git a/clang/include/clang/InstallAPI/Visitor.h b/clang/include/clang/InstallAPI/Visitor.h index 3680ee566ca87..b425cc2ca74f9 100644 --- a/clang/include/clang/InstallAPI/Visitor.h +++ b/clang/include/clang/InstallAPI/Visitor.h @@ -13,8 +13,8 @@ #ifndef LLVM_CLANG_INSTALLAPI_VISITOR_H #define LLVM_CLANG_INSTALLAPI_VISITOR_H +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/Mangle.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/Basic/TargetInfo.h" #include "clang/Frontend/FrontendActions.h" #include "clang/InstallAPI/Context.h" @@ -26,35 +26,37 @@ namespace installapi { /// ASTVisitor for collecting declarations that represent global symbols. class InstallAPIVisitor final : public ASTConsumer, - public RecursiveASTVisitor { + public ConstDynamicRecursiveASTVisitor { public: InstallAPIVisitor(ASTContext &ASTCtx, InstallAPIContext &Ctx, SourceManager &SrcMgr, Preprocessor &PP) : Ctx(Ctx), SrcMgr(SrcMgr), PP(PP), MC(ItaniumMangleContext::create(ASTCtx, ASTCtx.getDiagnostics())), - Layout(ASTCtx.getTargetInfo().getDataLayoutString()) {} + Layout(ASTCtx.getTargetInfo().getDataLayoutString()) { + ShouldVisitTemplateInstantiations = true; + } + void HandleTranslationUnit(ASTContext &ASTCtx) override; - bool shouldVisitTemplateInstantiations() const { return true; } /// Collect global variables. - bool VisitVarDecl(const VarDecl *D); + bool VisitVarDecl(const VarDecl *D) override; /// Collect global functions. - bool VisitFunctionDecl(const FunctionDecl *D); + bool VisitFunctionDecl(const FunctionDecl *D) override; /// Collect Objective-C Interface declarations. /// Every Objective-C class has an interface declaration that lists all the /// ivars, properties, and methods of the class. - bool VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D); + bool VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) override; /// Collect Objective-C Category/Extension declarations. /// /// The class that is being extended might come from a different library and /// is therefore itself not collected. - bool VisitObjCCategoryDecl(const ObjCCategoryDecl *D); + bool VisitObjCCategoryDecl(const ObjCCategoryDecl *D) override; /// Collect global c++ declarations. - bool VisitCXXRecordDecl(const CXXRecordDecl *D); + bool VisitCXXRecordDecl(const CXXRecordDecl *D) override; private: std::string getMangledName(const NamedDecl *D) const; diff --git a/clang/lib/AST/ASTImporterLookupTable.cpp b/clang/lib/AST/ASTImporterLookupTable.cpp index 29c3af1703b4f..0b10dc7793096 100644 --- a/clang/lib/AST/ASTImporterLookupTable.cpp +++ b/clang/lib/AST/ASTImporterLookupTable.cpp @@ -13,18 +13,23 @@ #include "clang/AST/ASTImporterLookupTable.h" #include "clang/AST/Decl.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclFriend.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "llvm/Support/FormatVariadic.h" namespace clang { namespace { -struct Builder : RecursiveASTVisitor { +struct Builder : DynamicRecursiveASTVisitor { ASTImporterLookupTable < - Builder(ASTImporterLookupTable <) : LT(LT) {} + Builder(ASTImporterLookupTable <) : LT(LT) { + ShouldVisitTemplateInstantiations = true; + ShouldVisitImplicitCode = true; + } - bool VisitTypedefNameDecl(TypedefNameDecl *D) { + bool VisitTypedefNameDecl(TypedefNameDecl *D) override { QualType Ty = D->getUnderlyingType(); Ty = Ty.getCanonicalType(); if (const auto *RTy = dyn_cast(Ty)) { @@ -37,7 +42,7 @@ struct Builder : RecursiveASTVisitor { return true; } - bool VisitNamedDecl(NamedDecl *D) { + bool VisitNamedDecl(NamedDecl *D) override { LT.add(D); return true; } @@ -46,7 +51,7 @@ struct Builder : RecursiveASTVisitor { // visitation. However, there are cases when the befriended class is not a // child, thus it must be fetched explicitly from the FriendDecl, and only // then can we add it to the lookup table. - bool VisitFriendDecl(FriendDecl *D) { + bool VisitFriendDecl(FriendDecl *D) override { if (D->getFriendType()) { QualType Ty = D->getFriendType()->getType(); // A FriendDecl with a dependent type (e.g. ClassTemplateSpecialization) @@ -76,10 +81,6 @@ struct Builder : RecursiveASTVisitor { } return true; } - - // Override default settings of base. - bool shouldVisitTemplateInstantiations() const { return true; } - bool shouldVisitImplicitCode() const { return true; } }; } // anonymous namespace diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp index cf018c8c7de2a..a575b9524aa59 100644 --- a/clang/lib/CodeGen/CGHLSLRuntime.cpp +++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp @@ -21,7 +21,7 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/Attrs.inc" #include "clang/AST/Decl.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/Type.h" #include "clang/Basic/TargetOptions.h" #include "clang/Frontend/FrontendDiagnostic.h" @@ -874,13 +874,13 @@ llvm::Instruction *CGHLSLRuntime::getConvergenceToken(BasicBlock &BB) { return nullptr; } -class OpaqueValueVisitor : public RecursiveASTVisitor { +class OpaqueValueVisitor : public DynamicRecursiveASTVisitor { public: llvm::SmallVector OVEs; llvm::SmallPtrSet Visited; OpaqueValueVisitor() {} - bool VisitHLSLOutArgExpr(HLSLOutArgExpr *) { + bool VisitHLSLOutArgExpr(HLSLOutArgExpr *) override { // These need to be bound in CodeGenFunction::EmitHLSLOutArgLValues // or CodeGenFunction::EmitHLSLOutArgExpr. If they are part of this // traversal, the temporary containing the copy out will not have @@ -888,7 +888,7 @@ class OpaqueValueVisitor : public RecursiveASTVisitor { return false; } - bool VisitOpaqueValueExpr(OpaqueValueExpr *E) { + bool VisitOpaqueValueExpr(OpaqueValueExpr *E) override { // Traverse the source expression first. if (E->getSourceExpr()) TraverseStmt(E->getSourceExpr()); diff --git a/clang/lib/CodeGen/CodeGenPGO.cpp b/clang/lib/CodeGen/CodeGenPGO.cpp index 98b30e084b18b..a90ecf91fbd0a 100644 --- a/clang/lib/CodeGen/CodeGenPGO.cpp +++ b/clang/lib/CodeGen/CodeGenPGO.cpp @@ -14,7 +14,7 @@ #include "CGDebugInfo.h" #include "CodeGenFunction.h" #include "CoverageMappingGen.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/StmtVisitor.h" #include "llvm/IR/Intrinsics.h" #include "llvm/IR/MDBuilder.h" @@ -156,8 +156,8 @@ static PGOHashVersion getPGOHashVersion(llvm::IndexedInstrProfReader *PGOReader, } /// A RecursiveASTVisitor that fills a map of statements to PGO counters. -struct MapRegionCounters : public RecursiveASTVisitor { - using Base = RecursiveASTVisitor; +struct MapRegionCounters : ConstDynamicRecursiveASTVisitor { + using Base = ConstDynamicRecursiveASTVisitor; /// The next counter value to assign. unsigned NextCounter; @@ -184,16 +184,16 @@ struct MapRegionCounters : public RecursiveASTVisitor { // Blocks and lambdas are handled as separate functions, so we need not // traverse them in the parent context. - bool TraverseBlockExpr(BlockExpr *BE) { return true; } - bool TraverseLambdaExpr(LambdaExpr *LE) { + bool TraverseBlockExpr(const BlockExpr *BE) override { return true; } + bool TraverseLambdaExpr(const LambdaExpr *LE) override { // Traverse the captures, but not the body. for (auto C : zip(LE->captures(), LE->capture_inits())) TraverseLambdaCapture(LE, &std::get<0>(C), std::get<1>(C)); return true; } - bool TraverseCapturedStmt(CapturedStmt *CS) { return true; } + bool TraverseCapturedStmt(const CapturedStmt *CS) override { return true; } - bool VisitDecl(const Decl *D) { + bool VisitDecl(const Decl *D) override { switch (D->getKind()) { default: break; @@ -213,7 +213,7 @@ struct MapRegionCounters : public RecursiveASTVisitor { /// If \p S gets a fresh counter, update the counter mappings. Return the /// V1 hash of \p S. - PGOHash::HashType updateCounterMappings(Stmt *S) { + PGOHash::HashType updateCounterMappings(const Stmt *S) { auto Type = getHashType(PGO_HASH_V1, S); if (Type != PGOHash::None) CounterMap[S] = NextCounter++; @@ -235,7 +235,7 @@ struct MapRegionCounters : public RecursiveASTVisitor { SmallVector LogOpStack; // Hook: dataTraverseStmtPre() is invoked prior to visiting an AST Stmt node. - bool dataTraverseStmtPre(Stmt *S) { + bool dataTraverseStmtPre(const Stmt *S) override { /// If MC/DC is not enabled, MCDCMaxCond will be set to 0. Do nothing. if (MCDCMaxCond == 0) return true; @@ -275,7 +275,7 @@ struct MapRegionCounters : public RecursiveASTVisitor { // Hook: dataTraverseStmtPost() is invoked by the AST visitor after visiting // an AST Stmt node. MC/DC will use it to to signal when the top of a // logical operation (boolean expression) nest is encountered. - bool dataTraverseStmtPost(Stmt *S) { + bool dataTraverseStmtPost(const Stmt *S) override { /// If MC/DC is not enabled, MCDCMaxCond will be set to 0. Do nothing. if (MCDCMaxCond == 0) return true; @@ -328,7 +328,7 @@ struct MapRegionCounters : public RecursiveASTVisitor { /// semantics of the operator. This is only valid for ">= v7" of the profile /// version so that we facilitate backward compatibility. In addition, in /// order to use MC/DC, count the number of total LHS and RHS conditions. - bool VisitBinaryOperator(BinaryOperator *S) { + bool VisitBinaryOperator(const BinaryOperator *S) override { if (S->isLogicalOp()) { if (CodeGenFunction::isInstrumentedCondition(S->getLHS())) NumCond++; @@ -343,7 +343,7 @@ struct MapRegionCounters : public RecursiveASTVisitor { return Base::VisitBinaryOperator(S); } - bool VisitConditionalOperator(ConditionalOperator *S) { + bool VisitConditionalOperator(const ConditionalOperator *S) override { if (llvm::EnableSingleByteCoverage && S->getTrueExpr()) CounterMap[S->getTrueExpr()] = NextCounter++; if (llvm::EnableSingleByteCoverage && S->getFalseExpr()) @@ -352,7 +352,7 @@ struct MapRegionCounters : public RecursiveASTVisitor { } /// Include \p S in the function hash. - bool VisitStmt(Stmt *S) { + bool VisitStmt(const Stmt *S) override { auto Type = updateCounterMappings(S); if (Hash.getHashVersion() != PGO_HASH_V1) Type = getHashType(Hash.getHashVersion(), S); @@ -361,7 +361,7 @@ struct MapRegionCounters : public RecursiveASTVisitor { return true; } - bool TraverseIfStmt(IfStmt *If) { + bool TraverseIfStmt(const IfStmt *If) override { // If we used the V1 hash, use the default traversal. if (Hash.getHashVersion() == PGO_HASH_V1) return Base::TraverseIfStmt(If); @@ -369,7 +369,7 @@ struct MapRegionCounters : public RecursiveASTVisitor { // When single byte coverage mode is enabled, add a counter to then and // else. bool NoSingleByteCoverage = !llvm::EnableSingleByteCoverage; - for (Stmt *CS : If->children()) { + for (const Stmt *CS : If->children()) { if (!CS || NoSingleByteCoverage) continue; if (CS == If->getThen()) @@ -381,7 +381,7 @@ struct MapRegionCounters : public RecursiveASTVisitor { // Otherwise, keep track of which branch we're in while traversing. VisitStmt(If); - for (Stmt *CS : If->children()) { + for (const Stmt *CS : If->children()) { if (!CS) continue; if (CS == If->getThen()) @@ -394,11 +394,11 @@ struct MapRegionCounters : public RecursiveASTVisitor { return true; } - bool TraverseWhileStmt(WhileStmt *While) { + bool TraverseWhileStmt(const WhileStmt *While) override { // When single byte coverage mode is enabled, add a counter to condition and // body. bool NoSingleByteCoverage = !llvm::EnableSingleByteCoverage; - for (Stmt *CS : While->children()) { + for (const Stmt *CS : While->children()) { if (!CS || NoSingleByteCoverage) continue; if (CS == While->getCond()) @@ -413,11 +413,11 @@ struct MapRegionCounters : public RecursiveASTVisitor { return true; } - bool TraverseDoStmt(DoStmt *Do) { + bool TraverseDoStmt(const DoStmt *Do) override { // When single byte coverage mode is enabled, add a counter to condition and // body. bool NoSingleByteCoverage = !llvm::EnableSingleByteCoverage; - for (Stmt *CS : Do->children()) { + for (const Stmt *CS : Do->children()) { if (!CS || NoSingleByteCoverage) continue; if (CS == Do->getCond()) @@ -432,11 +432,11 @@ struct MapRegionCounters : public RecursiveASTVisitor { return true; } - bool TraverseForStmt(ForStmt *For) { + bool TraverseForStmt(const ForStmt *For) override { // When single byte coverage mode is enabled, add a counter to condition, // increment and body. bool NoSingleByteCoverage = !llvm::EnableSingleByteCoverage; - for (Stmt *CS : For->children()) { + for (const Stmt *CS : For->children()) { if (!CS || NoSingleByteCoverage) continue; if (CS == For->getCond()) @@ -453,10 +453,10 @@ struct MapRegionCounters : public RecursiveASTVisitor { return true; } - bool TraverseCXXForRangeStmt(CXXForRangeStmt *ForRange) { + bool TraverseCXXForRangeStmt(const CXXForRangeStmt *ForRange) override { // When single byte coverage mode is enabled, add a counter to body. bool NoSingleByteCoverage = !llvm::EnableSingleByteCoverage; - for (Stmt *CS : ForRange->children()) { + for (const Stmt *CS : ForRange->children()) { if (!CS || NoSingleByteCoverage) continue; if (CS == ForRange->getBody()) @@ -473,7 +473,7 @@ struct MapRegionCounters : public RecursiveASTVisitor { // stability, define a custom traversal which tracks the end of the statement // in the hash (provided we're not using the V1 hash). #define DEFINE_NESTABLE_TRAVERSAL(N) \ - bool Traverse##N(N *S) { \ + bool Traverse##N(const N *S) override { \ Base::Traverse##N(S); \ if (Hash.getHashVersion() != PGO_HASH_V1) \ Hash.combine(PGOHash::EndOfScope); \ diff --git a/clang/lib/Frontend/ASTConsumers.cpp b/clang/lib/Frontend/ASTConsumers.cpp index 67c8761511e0c..b1d99fd8657d5 100644 --- a/clang/lib/Frontend/ASTConsumers.cpp +++ b/clang/lib/Frontend/ASTConsumers.cpp @@ -13,9 +13,10 @@ #include "clang/Frontend/ASTConsumers.h" #include "clang/AST/ASTConsumer.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/DeclObjC.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/PrettyPrinter.h" #include "clang/AST/RecordLayout.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/Basic/Diagnostic.h" #include "llvm/Support/Timer.h" #include "llvm/Support/raw_ostream.h" @@ -25,143 +26,144 @@ using namespace clang; /// ASTPrinter - Pretty-printer and dumper of ASTs namespace { - class ASTPrinter : public ASTConsumer, - public RecursiveASTVisitor { - typedef RecursiveASTVisitor base; - - public: - enum Kind { DumpFull, Dump, Print, None }; - ASTPrinter(std::unique_ptr Out, Kind K, - ASTDumpOutputFormat Format, StringRef FilterString, - bool DumpLookups = false, bool DumpDeclTypes = false) - : Out(Out ? *Out : llvm::outs()), OwnedOut(std::move(Out)), - OutputKind(K), OutputFormat(Format), FilterString(FilterString), - DumpLookups(DumpLookups), DumpDeclTypes(DumpDeclTypes) {} - - ASTPrinter(raw_ostream &Out, Kind K, ASTDumpOutputFormat Format, - StringRef FilterString, bool DumpLookups = false, - bool DumpDeclTypes = false) - : Out(Out), OwnedOut(nullptr), OutputKind(K), OutputFormat(Format), - FilterString(FilterString), DumpLookups(DumpLookups), - DumpDeclTypes(DumpDeclTypes) {} - - void HandleTranslationUnit(ASTContext &Context) override { - TranslationUnitDecl *D = Context.getTranslationUnitDecl(); - - if (FilterString.empty()) - return print(D); - - TraverseDecl(D); - } +class ASTPrinter : public ASTConsumer, public DynamicRecursiveASTVisitor { + using base = DynamicRecursiveASTVisitor; - bool shouldWalkTypesOfTypeLocs() const { return false; } +public: + enum Kind { DumpFull, Dump, Print, None }; + ASTPrinter(std::unique_ptr Out, Kind K, + ASTDumpOutputFormat Format, StringRef FilterString, + bool DumpLookups = false, bool DumpDeclTypes = false) + : Out(Out ? *Out : llvm::outs()), OwnedOut(std::move(Out)), OutputKind(K), + OutputFormat(Format), FilterString(FilterString), + DumpLookups(DumpLookups), DumpDeclTypes(DumpDeclTypes) { + ShouldWalkTypesOfTypeLocs = false; + } - bool TraverseDecl(Decl *D) { - if (D && filterMatches(D)) { - bool ShowColors = Out.has_colors(); - if (ShowColors) - Out.changeColor(raw_ostream::BLUE); + ASTPrinter(raw_ostream &Out, Kind K, ASTDumpOutputFormat Format, + StringRef FilterString, bool DumpLookups = false, + bool DumpDeclTypes = false) + : Out(Out), OwnedOut(nullptr), OutputKind(K), OutputFormat(Format), + FilterString(FilterString), DumpLookups(DumpLookups), + DumpDeclTypes(DumpDeclTypes) { + ShouldWalkTypesOfTypeLocs = false; + } - if (OutputFormat == ADOF_Default) - Out << (OutputKind != Print ? "Dumping " : "Printing ") << getName(D) - << ":\n"; + void HandleTranslationUnit(ASTContext &Context) override { + TranslationUnitDecl *D = Context.getTranslationUnitDecl(); - if (ShowColors) - Out.resetColor(); - print(D); - Out << "\n"; - // Don't traverse child nodes to avoid output duplication. - return true; - } - return base::TraverseDecl(D); - } + if (FilterString.empty()) + return print(D); - private: - std::string getName(Decl *D) { - if (isa(D)) - return cast(D)->getQualifiedNameAsString(); - return ""; + TraverseDecl(D); + } + + bool TraverseDecl(Decl *D) override { + if (D && filterMatches(D)) { + bool ShowColors = Out.has_colors(); + if (ShowColors) + Out.changeColor(raw_ostream::BLUE); + + if (OutputFormat == ADOF_Default) + Out << (OutputKind != Print ? "Dumping " : "Printing ") << getName(D) + << ":\n"; + + if (ShowColors) + Out.resetColor(); + print(D); + Out << "\n"; + // Don't traverse child nodes to avoid output duplication. + return true; } - bool filterMatches(Decl *D) { - return getName(D).find(FilterString) != std::string::npos; + return base::TraverseDecl(D); + } + +private: + std::string getName(Decl *D) { + if (isa(D)) + return cast(D)->getQualifiedNameAsString(); + return ""; + } + bool filterMatches(Decl *D) { + return getName(D).find(FilterString) != std::string::npos; + } + void print(Decl *D) { + if (DumpLookups) { + if (DeclContext *DC = dyn_cast(D)) { + if (DC == DC->getPrimaryContext()) + DC->dumpLookups(Out, OutputKind != None, OutputKind == DumpFull); + else + Out << "Lookup map is in primary DeclContext " + << DC->getPrimaryContext() << "\n"; + } else + Out << "Not a DeclContext\n"; + } else if (OutputKind == Print) { + PrintingPolicy Policy(D->getASTContext().getLangOpts()); + Policy.IncludeTagDefinition = true; + D->print(Out, Policy, /*Indentation=*/0, /*PrintInstantiation=*/true); + } else if (OutputKind != None) { + D->dump(Out, OutputKind == DumpFull, OutputFormat); } - void print(Decl *D) { - if (DumpLookups) { - if (DeclContext *DC = dyn_cast(D)) { - if (DC == DC->getPrimaryContext()) - DC->dumpLookups(Out, OutputKind != None, OutputKind == DumpFull); - else - Out << "Lookup map is in primary DeclContext " - << DC->getPrimaryContext() << "\n"; - } else - Out << "Not a DeclContext\n"; - } else if (OutputKind == Print) { - PrintingPolicy Policy(D->getASTContext().getLangOpts()); - Policy.IncludeTagDefinition = true; - D->print(Out, Policy, /*Indentation=*/0, /*PrintInstantiation=*/true); - } else if (OutputKind != None) { - D->dump(Out, OutputKind == DumpFull, OutputFormat); - } - if (DumpDeclTypes) { - Decl *InnerD = D; - if (auto *TD = dyn_cast(D)) - if (Decl *TempD = TD->getTemplatedDecl()) - InnerD = TempD; - - // FIXME: Support OutputFormat in type dumping. - // FIXME: Support combining -ast-dump-decl-types with -ast-dump-lookups. - if (auto *VD = dyn_cast(InnerD)) - VD->getType().dump(Out, VD->getASTContext()); - if (auto *TD = dyn_cast(InnerD)) { - const ASTContext &Ctx = TD->getASTContext(); - Ctx.getTypeDeclType(TD)->dump(Out, Ctx); - } + if (DumpDeclTypes) { + Decl *InnerD = D; + if (auto *TD = dyn_cast(D)) + if (Decl *TempD = TD->getTemplatedDecl()) + InnerD = TempD; + + // FIXME: Support OutputFormat in type dumping. + // FIXME: Support combining -ast-dump-decl-types with -ast-dump-lookups. + if (auto *VD = dyn_cast(InnerD)) + VD->getType().dump(Out, VD->getASTContext()); + if (auto *TD = dyn_cast(InnerD)) { + const ASTContext &Ctx = TD->getASTContext(); + Ctx.getTypeDeclType(TD)->dump(Out, Ctx); } } + } - raw_ostream &Out; - std::unique_ptr OwnedOut; - - /// How to output individual declarations. - Kind OutputKind; + raw_ostream &Out; + std::unique_ptr OwnedOut; - /// What format should the output take? - ASTDumpOutputFormat OutputFormat; + /// How to output individual declarations. + Kind OutputKind; - /// Which declarations or DeclContexts to display. - std::string FilterString; + /// What format should the output take? + ASTDumpOutputFormat OutputFormat; - /// Whether the primary output is lookup results or declarations. Individual - /// results will be output with a format determined by OutputKind. This is - /// incompatible with OutputKind == Print. - bool DumpLookups; + /// Which declarations or DeclContexts to display. + std::string FilterString; - /// Whether to dump the type for each declaration dumped. - bool DumpDeclTypes; - }; + /// Whether the primary output is lookup results or declarations. Individual + /// results will be output with a format determined by OutputKind. This is + /// incompatible with OutputKind == Print. + bool DumpLookups; - class ASTDeclNodeLister : public ASTConsumer, - public RecursiveASTVisitor { - public: - ASTDeclNodeLister(raw_ostream *Out = nullptr) - : Out(Out ? *Out : llvm::outs()) {} + /// Whether to dump the type for each declaration dumped. + bool DumpDeclTypes; +}; - void HandleTranslationUnit(ASTContext &Context) override { - TraverseDecl(Context.getTranslationUnitDecl()); - } +class ASTDeclNodeLister : public ASTConsumer, + public DynamicRecursiveASTVisitor { +public: + ASTDeclNodeLister(raw_ostream *Out = nullptr) + : Out(Out ? *Out : llvm::outs()) { + ShouldWalkTypesOfTypeLocs = false; + } - bool shouldWalkTypesOfTypeLocs() const { return false; } + void HandleTranslationUnit(ASTContext &Context) override { + TraverseDecl(Context.getTranslationUnitDecl()); + } - bool VisitNamedDecl(NamedDecl *D) { - D->printQualifiedName(Out); - Out << '\n'; - return true; - } + bool VisitNamedDecl(NamedDecl *D) override { + D->printQualifiedName(Out); + Out << '\n'; + return true; + } - private: - raw_ostream &Out; - }; +private: + raw_ostream &Out; +}; } // end anonymous namespace std::unique_ptr diff --git a/clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp b/clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp index d7cfd23bb0a7a..2dbf24522fa9d 100644 --- a/clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp +++ b/clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/Mangle.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/Basic/TargetInfo.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/FrontendActions.h" @@ -242,8 +242,8 @@ class InterfaceStubFunctionsConsumer : public ASTConsumer { : Instance(Instance), InFile(InFile), Format(Format) {} void HandleTranslationUnit(ASTContext &context) override { - struct Visitor : public RecursiveASTVisitor { - bool VisitNamedDecl(NamedDecl *ND) { + struct Visitor : ConstDynamicRecursiveASTVisitor { + bool VisitNamedDecl(const NamedDecl *ND) override { if (const auto *FD = dyn_cast(ND)) if (FD->isLateTemplateParsed()) { LateParsedDecls.insert(FD); @@ -260,7 +260,7 @@ class InterfaceStubFunctionsConsumer : public ASTConsumer { } std::set LateParsedDecls; - std::set NamedDecls; + std::set NamedDecls; std::set ValueDecls; } v; diff --git a/clang/lib/Index/IndexBody.cpp b/clang/lib/Index/IndexBody.cpp index 1979117d4695c..f117ee6a63def 100644 --- a/clang/lib/Index/IndexBody.cpp +++ b/clang/lib/Index/IndexBody.cpp @@ -10,8 +10,11 @@ #include "clang/AST/ASTConcept.h" #include "clang/AST/ASTLambda.h" #include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclObjC.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" +#include "clang/AST/ExprCXX.h" #include "clang/AST/ExprConcepts.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/ExprObjC.h" #include "clang/AST/Type.h" #include "clang/Sema/HeuristicResolver.h" @@ -20,41 +23,39 @@ using namespace clang::index; namespace { -class BodyIndexer : public RecursiveASTVisitor { +class BodyIndexer : public DynamicRecursiveASTVisitor { IndexingContext &IndexCtx; const NamedDecl *Parent; const DeclContext *ParentDC; SmallVector StmtStack; - typedef RecursiveASTVisitor base; - Stmt *getParentStmt() const { return StmtStack.size() < 2 ? nullptr : StmtStack.end()[-2]; } public: - BodyIndexer(IndexingContext &indexCtx, - const NamedDecl *Parent, const DeclContext *DC) - : IndexCtx(indexCtx), Parent(Parent), ParentDC(DC) { } - - bool shouldWalkTypesOfTypeLocs() const { return false; } + BodyIndexer(IndexingContext &indexCtx, const NamedDecl *Parent, + const DeclContext *DC) + : IndexCtx(indexCtx), Parent(Parent), ParentDC(DC) { + ShouldWalkTypesOfTypeLocs = false; + } - bool dataTraverseStmtPre(Stmt *S) { + bool dataTraverseStmtPre(Stmt *S) override { StmtStack.push_back(S); return true; } - bool dataTraverseStmtPost(Stmt *S) { + bool dataTraverseStmtPost(Stmt *S) override { assert(StmtStack.back() == S); StmtStack.pop_back(); return true; } - bool TraverseTypeLoc(TypeLoc TL) { + bool TraverseTypeLoc(TypeLoc TL, bool TraverseQualifier) override { IndexCtx.indexTypeLoc(TL, Parent, ParentDC); return true; } - bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { + bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) override { IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, ParentDC); return true; } @@ -141,39 +142,39 @@ class BodyIndexer : public RecursiveASTVisitor { Relations.emplace_back((unsigned)SymbolRole::RelationCalledBy, MD); } - bool VisitDeclRefExpr(DeclRefExpr *E) { + bool VisitDeclRefExpr(DeclRefExpr *E) override { SmallVector Relations; SymbolRoleSet Roles = getRolesForRef(E, Relations); return IndexCtx.handleReference(E->getDecl(), E->getLocation(), Parent, ParentDC, Roles, Relations, E); } - bool VisitGotoStmt(GotoStmt *S) { + bool VisitGotoStmt(GotoStmt *S) override { return IndexCtx.handleReference(S->getLabel(), S->getLabelLoc(), Parent, ParentDC); } - bool VisitCXXNewExpr(CXXNewExpr *E) { + bool VisitCXXNewExpr(CXXNewExpr *E) override { if (E->isGlobalNew() || !E->getOperatorNew()) return true; return IndexCtx.handleReference(E->getOperatorNew(), E->getBeginLoc(), Parent, ParentDC); } - bool VisitCXXDeleteExpr(CXXDeleteExpr *E) { + bool VisitCXXDeleteExpr(CXXDeleteExpr *E) override { if (E->isGlobalDelete() || !E->getOperatorDelete()) return true; return IndexCtx.handleReference(E->getOperatorDelete(), E->getBeginLoc(), Parent, ParentDC); } - bool VisitLabelStmt(LabelStmt *S) { + bool VisitLabelStmt(LabelStmt *S) override { if (IndexCtx.shouldIndexFunctionLocalSymbols()) return IndexCtx.handleDecl(S->getDecl()); return true; } - bool VisitMemberExpr(MemberExpr *E) { + bool VisitMemberExpr(MemberExpr *E) override { SourceLocation Loc = E->getMemberLoc(); if (Loc.isInvalid()) Loc = E->getBeginLoc(); @@ -196,21 +197,22 @@ class BodyIndexer : public RecursiveASTVisitor { Roles, Relations, E); } - bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { + bool + VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) override { auto *Resolver = IndexCtx.getResolver(); assert(Resolver); return indexDependentReference(E, E->getMemberNameInfo().getLoc(), Resolver->resolveMemberExpr(E)); } - bool VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { + bool VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) override { auto *Resolver = IndexCtx.getResolver(); assert(Resolver); return indexDependentReference(E, E->getNameInfo().getLoc(), Resolver->resolveDeclRefExpr(E)); } - bool VisitDesignatedInitExpr(DesignatedInitExpr *E) { + bool VisitDesignatedInitExpr(DesignatedInitExpr *E) override { for (DesignatedInitExpr::Designator &D : llvm::reverse(E->designators())) { if (D.isFieldDesignator()) { if (const FieldDecl *FD = D.getFieldDecl()) { @@ -222,14 +224,14 @@ class BodyIndexer : public RecursiveASTVisitor { return true; } - bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { + bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) override { SmallVector Relations; SymbolRoleSet Roles = getRolesForRef(E, Relations); return IndexCtx.handleReference(E->getDecl(), E->getLocation(), Parent, ParentDC, Roles, Relations, E); } - bool VisitObjCMessageExpr(ObjCMessageExpr *E) { + bool VisitObjCMessageExpr(ObjCMessageExpr *E) override { auto isDynamic = [](const ObjCMessageExpr *MsgE)->bool { if (MsgE->getReceiverKind() != ObjCMessageExpr::Instance) return false; @@ -301,7 +303,7 @@ class BodyIndexer : public RecursiveASTVisitor { return true; } - bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { + bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) override { if (E->isExplicitProperty()) { SmallVector Relations; SymbolRoleSet Roles = getRolesForRef(E, Relations); @@ -326,12 +328,12 @@ class BodyIndexer : public RecursiveASTVisitor { return true; } - bool VisitMSPropertyRefExpr(MSPropertyRefExpr *E) { + bool VisitMSPropertyRefExpr(MSPropertyRefExpr *E) override { return IndexCtx.handleReference(E->getPropertyDecl(), E->getMemberLoc(), Parent, ParentDC, SymbolRoleSet(), {}, E); } - bool VisitObjCProtocolExpr(ObjCProtocolExpr *E) { + bool VisitObjCProtocolExpr(ObjCProtocolExpr *E) override { return IndexCtx.handleReference(E->getProtocol(), E->getProtocolIdLoc(), Parent, ParentDC, SymbolRoleSet(), {}, E); } @@ -345,28 +347,28 @@ class BodyIndexer : public RecursiveASTVisitor { Roles, Relations, E); } - bool VisitObjCBoxedExpr(ObjCBoxedExpr *E) { + bool VisitObjCBoxedExpr(ObjCBoxedExpr *E) override { if (ObjCMethodDecl *MD = E->getBoxingMethod()) { return passObjCLiteralMethodCall(MD, E); } return true; } - bool VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { + bool VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) override { if (ObjCMethodDecl *MD = E->getDictWithObjectsMethod()) { return passObjCLiteralMethodCall(MD, E); } return true; } - bool VisitObjCArrayLiteral(ObjCArrayLiteral *E) { + bool VisitObjCArrayLiteral(ObjCArrayLiteral *E) override { if (ObjCMethodDecl *MD = E->getArrayWithObjectsMethod()) { return passObjCLiteralMethodCall(MD, E); } return true; } - bool VisitCXXConstructExpr(CXXConstructExpr *E) { + bool VisitCXXConstructExpr(CXXConstructExpr *E) override { SymbolRoleSet Roles{}; SmallVector Relations; addCallRole(Roles, Relations); @@ -374,14 +376,13 @@ class BodyIndexer : public RecursiveASTVisitor { Parent, ParentDC, Roles, Relations, E); } - bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *E, - DataRecursionQueue *Q = nullptr) { + bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *E) override { if (E->getOperatorLoc().isInvalid()) return true; // implicit. - return base::TraverseCXXOperatorCallExpr(E, Q); + return DynamicRecursiveASTVisitor::TraverseCXXOperatorCallExpr(E); } - bool VisitDeclStmt(DeclStmt *S) { + bool VisitDeclStmt(DeclStmt *S) override { if (IndexCtx.shouldIndexFunctionLocalSymbols()) { IndexCtx.indexDeclGroupRef(S->getDeclGroup()); return true; @@ -400,11 +401,11 @@ class BodyIndexer : public RecursiveASTVisitor { } bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C, - Expr *Init) { + Expr *Init) override { if (C->capturesThis() || C->capturesVLAType()) return true; - if (!base::TraverseStmt(Init)) + if (!DynamicRecursiveASTVisitor::TraverseStmt(Init)) return false; if (C->capturesVariable() && IndexCtx.shouldIndexFunctionLocalSymbols()) @@ -418,10 +419,10 @@ class BodyIndexer : public RecursiveASTVisitor { // the things that we visit. Make sure to only visit the semantic form. // Also visit things that are in the syntactic form but not the semantic one, // for example the indices in DesignatedInitExprs. - bool TraverseInitListExpr(InitListExpr *S, DataRecursionQueue *Q = nullptr) { + bool TraverseInitListExpr(InitListExpr *S) override { auto visitForm = [&](InitListExpr *Form) { for (Stmt *SubStmt : Form->children()) { - if (!TraverseStmt(SubStmt, Q)) + if (!TraverseStmt(SubStmt)) return false; } return true; @@ -469,7 +470,7 @@ class BodyIndexer : public RecursiveASTVisitor { return true; } - bool VisitOffsetOfExpr(OffsetOfExpr *S) { + bool VisitOffsetOfExpr(OffsetOfExpr *S) override { for (unsigned I = 0, E = S->getNumComponents(); I != E; ++I) { const OffsetOfNode &Component = S->getComponent(I); if (Component.getKind() == OffsetOfNode::Field) @@ -480,7 +481,7 @@ class BodyIndexer : public RecursiveASTVisitor { return true; } - bool VisitParmVarDecl(ParmVarDecl* D) { + bool VisitParmVarDecl(ParmVarDecl *D) override { // Index the parameters of lambda expression and requires expression. if (IndexCtx.shouldIndexFunctionLocalSymbols()) { const auto *DC = D->getDeclContext(); @@ -490,7 +491,7 @@ class BodyIndexer : public RecursiveASTVisitor { return true; } - bool VisitOverloadExpr(OverloadExpr *E) { + bool VisitOverloadExpr(OverloadExpr *E) override { SmallVector Relations; SymbolRoleSet Roles = getRolesForRef(E, Relations); for (auto *D : E->decls()) @@ -499,16 +500,16 @@ class BodyIndexer : public RecursiveASTVisitor { return true; } - bool VisitConceptSpecializationExpr(ConceptSpecializationExpr *R) { + bool VisitConceptSpecializationExpr(ConceptSpecializationExpr *R) override { IndexCtx.handleReference(R->getNamedConcept(), R->getConceptNameLoc(), Parent, ParentDC); return true; } - bool TraverseTypeConstraint(const TypeConstraint *C) { + bool TraverseTypeConstraint(const TypeConstraint *C) override { IndexCtx.handleReference(C->getNamedConcept(), C->getConceptNameLoc(), Parent, ParentDC); - return RecursiveASTVisitor::TraverseTypeConstraint(C); + return DynamicRecursiveASTVisitor::TraverseTypeConstraint(C); } }; diff --git a/clang/lib/Index/IndexTypeSourceInfo.cpp b/clang/lib/Index/IndexTypeSourceInfo.cpp index 74c6c116b274e..d8effcfd2b021 100644 --- a/clang/lib/Index/IndexTypeSourceInfo.cpp +++ b/clang/lib/Index/IndexTypeSourceInfo.cpp @@ -8,8 +8,9 @@ #include "IndexingContext.h" #include "clang/AST/ASTConcept.h" +#include "clang/AST/DeclObjC.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/PrettyPrinter.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/TypeLoc.h" #include "clang/Sema/HeuristicResolver.h" #include "llvm/ADT/ScopeExit.h" @@ -19,19 +20,19 @@ using namespace index; namespace { -class TypeIndexer : public RecursiveASTVisitor { +class TypeIndexer : public DynamicRecursiveASTVisitor { IndexingContext &IndexCtx; const NamedDecl *Parent; const DeclContext *ParentDC; bool IsBase; SmallVector Relations; - typedef RecursiveASTVisitor base; - public: TypeIndexer(IndexingContext &indexCtx, const NamedDecl *parent, const DeclContext *DC, bool isBase, bool isIBType) : IndexCtx(indexCtx), Parent(parent), ParentDC(DC), IsBase(isBase) { + ShouldWalkTypesOfTypeLocs = false; + if (IsBase) { assert(Parent); Relations.emplace_back((unsigned)SymbolRole::RelationBaseOf, Parent); @@ -42,22 +43,20 @@ class TypeIndexer : public RecursiveASTVisitor { } } - bool shouldWalkTypesOfTypeLocs() const { return false; } - #define TRY_TO(CALL_EXPR) \ do { \ if (!CALL_EXPR) \ return false; \ } while (0) - bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TTPL) { + bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TTPL) override { SourceLocation Loc = TTPL.getNameLoc(); TemplateTypeParmDecl *TTPD = TTPL.getDecl(); return IndexCtx.handleReference(TTPD, Loc, Parent, ParentDC, SymbolRoleSet()); } - bool VisitTypedefTypeLoc(TypedefTypeLoc TL) { + bool VisitTypedefTypeLoc(TypedefTypeLoc TL) override { SourceLocation Loc = TL.getNameLoc(); TypedefNameDecl *ND = TL.getDecl(); if (ND->isTransparentTag()) { @@ -81,7 +80,7 @@ class TypeIndexer : public RecursiveASTVisitor { return true; } - bool VisitAutoTypeLoc(AutoTypeLoc TL) { + bool VisitAutoTypeLoc(AutoTypeLoc TL) override { if (auto *C = TL.getNamedConcept()) return IndexCtx.handleReference(C, TL.getConceptNameLoc(), Parent, ParentDC); @@ -95,7 +94,7 @@ class TypeIndexer : public RecursiveASTVisitor { return true; } - bool TraverseParmVarDecl(ParmVarDecl *D) { + bool TraverseParmVarDecl(ParmVarDecl *D) override { // Avoid visiting default arguments from the definition that were already // visited in the declaration. // FIXME: A free function definition can have default arguments. @@ -108,15 +107,15 @@ class TypeIndexer : public RecursiveASTVisitor { } } - return base::TraverseParmVarDecl(D); + return DynamicRecursiveASTVisitor::TraverseParmVarDecl(D); } - bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { + bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) override { IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, ParentDC); return true; } - bool VisitTagTypeLoc(TagTypeLoc TL) { + bool VisitTagTypeLoc(TagTypeLoc TL) override { TagDecl *D = TL.getOriginalDecl(); if (!IndexCtx.shouldIndexFunctionLocalSymbols() && D->getParentFunctionOrMethod()) @@ -132,12 +131,12 @@ class TypeIndexer : public RecursiveASTVisitor { Relations); } - bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { + bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) override { return IndexCtx.handleReference(TL.getIFaceDecl(), TL.getNameLoc(), Parent, ParentDC, SymbolRoleSet(), Relations); } - bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { + bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) override { for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) { IndexCtx.handleReference(TL.getProtocol(i), TL.getProtocolLoc(i), Parent, ParentDC, SymbolRoleSet(), Relations); @@ -162,7 +161,8 @@ class TypeIndexer : public RecursiveASTVisitor { } } - bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) { + bool VisitTemplateSpecializationTypeLoc( + TemplateSpecializationTypeLoc TL) override { auto *T = TL.getTypePtr(); if (!T) return true; @@ -173,7 +173,7 @@ class TypeIndexer : public RecursiveASTVisitor { } bool TraverseTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL, - bool TraverseQualifier) { + bool TraverseQualifier) override { if (!WalkUpFromTemplateSpecializationTypeLoc(TL)) return false; if (!TraverseTemplateName(TL.getTypePtr()->getTemplateName())) @@ -193,7 +193,8 @@ class TypeIndexer : public RecursiveASTVisitor { return true; } - bool VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc TL) { + bool VisitDeducedTemplateSpecializationTypeLoc( + DeducedTemplateSpecializationTypeLoc TL) override { auto *T = TL.getTypePtr(); if (!T) return true; @@ -203,7 +204,7 @@ class TypeIndexer : public RecursiveASTVisitor { return true; } - bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { + bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL) override { std::vector Symbols = IndexCtx.getResolver()->resolveDependentNameType(TL.getTypePtr()); if (Symbols.size() != 1) @@ -212,7 +213,7 @@ class TypeIndexer : public RecursiveASTVisitor { ParentDC, SymbolRoleSet(), Relations); } - bool TraverseStmt(Stmt *S) { + bool TraverseStmt(Stmt *S) override { IndexCtx.indexBody(S, Parent, ParentDC); return true; } diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp index d8539eaaac49f..91f7d215965a1 100644 --- a/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp @@ -11,7 +11,7 @@ #include "PtrTypesSemantics.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/Analysis/DomainSpecific/CocoaConventions.h" #include "clang/Basic/SourceLocation.h" #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" @@ -44,54 +44,53 @@ class ForwardDeclChecker : public Checker> { // The calls to checkAST* from AnalysisConsumer don't // visit template instantiations or lambda classes. We // want to visit those, so we make our own RecursiveASTVisitor. - struct LocalVisitor : public RecursiveASTVisitor { - using Base = RecursiveASTVisitor; + struct LocalVisitor : ConstDynamicRecursiveASTVisitor { + using Base = ConstDynamicRecursiveASTVisitor; const ForwardDeclChecker *Checker; - Decl *DeclWithIssue{nullptr}; + const Decl *DeclWithIssue{nullptr}; explicit LocalVisitor(const ForwardDeclChecker *Checker) : Checker(Checker) { assert(Checker); + ShouldVisitTemplateInstantiations = true; + ShouldVisitImplicitCode = false; } - bool shouldVisitTemplateInstantiations() const { return true; } - bool shouldVisitImplicitCode() const { return false; } - - bool VisitTypedefDecl(TypedefDecl *TD) { + bool VisitTypedefDecl(const TypedefDecl *TD) override { Checker->visitTypedef(TD); return true; } - bool VisitRecordDecl(const RecordDecl *RD) { + bool VisitRecordDecl(const RecordDecl *RD) override { Checker->visitRecordDecl(RD, DeclWithIssue); return true; } - bool TraverseDecl(Decl *D) { + bool TraverseDecl(const Decl *D) override { llvm::SaveAndRestore SavedDecl(DeclWithIssue); if (D && (isa(D) || isa(D))) DeclWithIssue = D; return Base::TraverseDecl(D); } - bool VisitVarDecl(VarDecl *V) { + bool VisitVarDecl(const VarDecl *V) override { if (V->isLocalVarDecl()) Checker->visitVarDecl(V, DeclWithIssue); return true; } - bool VisitCallExpr(const CallExpr *CE) { + bool VisitCallExpr(const CallExpr *CE) override { Checker->visitCallExpr(CE, DeclWithIssue); return true; } - bool VisitCXXConstructExpr(const CXXConstructExpr *CE) { + bool VisitCXXConstructExpr(const CXXConstructExpr *CE) override { Checker->visitConstructExpr(CE, DeclWithIssue); return true; } - bool VisitObjCMessageExpr(const ObjCMessageExpr *ObjCMsgExpr) { + bool VisitObjCMessageExpr(const ObjCMessageExpr *ObjCMsgExpr) override { Checker->visitObjCMessageExpr(ObjCMsgExpr, DeclWithIssue); return true; } @@ -99,7 +98,7 @@ class ForwardDeclChecker : public Checker> { LocalVisitor visitor(this); RTC.visitTranslationUnitDecl(TUD); - visitor.TraverseDecl(const_cast(TUD)); + visitor.TraverseDecl(TUD); } void visitTypedef(const TypedefDecl *TD) const { diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp index e1f9a77f5a5ca..a71f810516905 100644 --- a/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp @@ -8,7 +8,7 @@ #include "ASTUtils.h" #include "PtrTypesSemantics.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/Analysis/DomainSpecific/CocoaConventions.h" #include "clang/Analysis/RetainSummaryManager.h" #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" @@ -45,64 +45,63 @@ class RetainPtrCtorAdoptChecker // The calls to checkAST* from AnalysisConsumer don't // visit template instantiations or lambda classes. We // want to visit those, so we make our own RecursiveASTVisitor. - struct LocalVisitor : public RecursiveASTVisitor { + struct LocalVisitor : ConstDynamicRecursiveASTVisitor { const RetainPtrCtorAdoptChecker *Checker; - Decl *DeclWithIssue{nullptr}; + const Decl *DeclWithIssue{nullptr}; - using Base = RecursiveASTVisitor; + using Base = ConstDynamicRecursiveASTVisitor; explicit LocalVisitor(const RetainPtrCtorAdoptChecker *Checker) : Checker(Checker) { assert(Checker); + ShouldVisitTemplateInstantiations = true; + ShouldVisitImplicitCode = false; } - bool shouldVisitTemplateInstantiations() const { return true; } - bool shouldVisitImplicitCode() const { return false; } - - bool TraverseDecl(Decl *D) { + bool TraverseDecl(const Decl *D) override { llvm::SaveAndRestore SavedDecl(DeclWithIssue); if (D && (isa(D) || isa(D))) DeclWithIssue = D; return Base::TraverseDecl(D); } - bool TraverseClassTemplateDecl(ClassTemplateDecl *CTD) { + bool TraverseClassTemplateDecl(const ClassTemplateDecl *CTD) override { if (isRetainPtrOrOSPtr(safeGetName(CTD))) return true; // Skip the contents of RetainPtr. return Base::TraverseClassTemplateDecl(CTD); } - bool VisitTypedefDecl(TypedefDecl *TD) { + bool VisitTypedefDecl(const TypedefDecl *TD) override { Checker->RTC.visitTypedef(TD); return true; } - bool VisitCallExpr(const CallExpr *CE) { + bool VisitCallExpr(const CallExpr *CE) override { Checker->visitCallExpr(CE, DeclWithIssue); return true; } - bool VisitCXXConstructExpr(const CXXConstructExpr *CE) { + bool VisitCXXConstructExpr(const CXXConstructExpr *CE) override { Checker->visitConstructExpr(CE, DeclWithIssue); return true; } - bool VisitObjCMessageExpr(const ObjCMessageExpr *ObjCMsgExpr) { + bool VisitObjCMessageExpr(const ObjCMessageExpr *ObjCMsgExpr) override { Checker->visitObjCMessageExpr(ObjCMsgExpr, DeclWithIssue); return true; } - bool VisitReturnStmt(const ReturnStmt *RS) { + bool VisitReturnStmt(const ReturnStmt *RS) override { Checker->visitReturnStmt(RS, DeclWithIssue); return true; } - bool VisitVarDecl(const VarDecl *VD) { + bool VisitVarDecl(const VarDecl *VD) override { Checker->visitVarDecl(VD); return true; } - bool VisitBinaryOperator(const BinaryOperator *BO) { + bool VisitBinaryOperator(const BinaryOperator *BO) override { Checker->visitBinaryOperator(BO); return true; } @@ -113,7 +112,7 @@ class RetainPtrCtorAdoptChecker TUD->getASTContext(), true /* trackObjCAndCFObjects */, false /* trackOSObjects */); RTC.visitTranslationUnitDecl(TUD); - visitor.TraverseDecl(const_cast(TUD)); + visitor.TraverseDecl(TUD); } bool isAdoptFn(const Decl *FnDecl) const { diff --git a/clang/lib/Tooling/ASTDiff/ASTDiff.cpp b/clang/lib/Tooling/ASTDiff/ASTDiff.cpp index d70a679cc8dd0..5c2c7d79ec1da 100644 --- a/clang/lib/Tooling/ASTDiff/ASTDiff.cpp +++ b/clang/lib/Tooling/ASTDiff/ASTDiff.cpp @@ -11,8 +11,9 @@ //===----------------------------------------------------------------------===// #include "clang/Tooling/ASTDiff/ASTDiff.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" +#include "clang/AST/ExprCXX.h" #include "clang/AST/ParentMapContext.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/Basic/SourceManager.h" #include "clang/Lex/Lexer.h" #include "llvm/ADT/PriorityQueue.h" @@ -186,7 +187,7 @@ static bool isNodeExcluded(const SourceManager &SrcMgr, T *N) { namespace { // Sets Height, Parent and Children for each node. -struct PreorderVisitor : public RecursiveASTVisitor { +struct PreorderVisitor : DynamicRecursiveASTVisitor { int Id = 0, Depth = 0; NodeId Parent; SyntaxTree::Impl &Tree; @@ -228,30 +229,32 @@ struct PreorderVisitor : public RecursiveASTVisitor { for (NodeId Child : N.Children) N.Height = std::max(N.Height, 1 + Tree.getNode(Child).Height); } - bool TraverseDecl(Decl *D) { + bool TraverseDecl(Decl *D) override { if (isNodeExcluded(Tree.AST.getSourceManager(), D)) return true; auto SavedState = PreTraverse(D); - RecursiveASTVisitor::TraverseDecl(D); + DynamicRecursiveASTVisitor::TraverseDecl(D); PostTraverse(SavedState); return true; } - bool TraverseStmt(Stmt *S) { + bool TraverseStmt(Stmt *S) override { if (auto *E = dyn_cast_or_null(S)) S = E->IgnoreImplicit(); if (isNodeExcluded(Tree.AST.getSourceManager(), S)) return true; auto SavedState = PreTraverse(S); - RecursiveASTVisitor::TraverseStmt(S); + DynamicRecursiveASTVisitor::TraverseStmt(S); PostTraverse(SavedState); return true; } - bool TraverseType(QualType T, bool TraverseQualifier = true) { return true; } - bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { + bool TraverseType(QualType T, bool TraverseQualifier = true) override { + return true; + } + bool TraverseConstructorInitializer(CXXCtorInitializer *Init) override { if (isNodeExcluded(Tree.AST.getSourceManager(), Init)) return true; auto SavedState = PreTraverse(Init); - RecursiveASTVisitor::TraverseConstructorInitializer(Init); + DynamicRecursiveASTVisitor::TraverseConstructorInitializer(Init); PostTraverse(SavedState); return true; } diff --git a/clang/lib/Tooling/Refactoring/Rename/USRFinder.cpp b/clang/lib/Tooling/Refactoring/Rename/USRFinder.cpp index 91d5cafda4f7a..68677f5c05857 100644 --- a/clang/lib/Tooling/Refactoring/Rename/USRFinder.cpp +++ b/clang/lib/Tooling/Refactoring/Rename/USRFinder.cpp @@ -13,7 +13,7 @@ #include "clang/Tooling/Refactoring/Rename/USRFinder.h" #include "clang/AST/ASTContext.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/Basic/SourceManager.h" #include "clang/Index/USRGeneration.h" #include "clang/Lex/Lexer.h" @@ -96,14 +96,12 @@ namespace { /// Recursively visits each NamedDecl node to find the declaration with a /// specific name. -class NamedDeclFindingVisitor - : public RecursiveASTVisitor { -public: +struct NamedDeclFindingVisitor : ConstDynamicRecursiveASTVisitor { explicit NamedDeclFindingVisitor(StringRef Name) : Name(Name) {} // We don't have to traverse the uses to find some declaration with a // specific name, so just visit the named declarations. - bool VisitNamedDecl(const NamedDecl *ND) { + bool VisitNamedDecl(const NamedDecl *ND) override { if (!ND) return true; // Fully qualified name is used to find the declaration. diff --git a/clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp b/clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp index 07ef58079189e..55b4e34ecfe47 100644 --- a/clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp +++ b/clang/lib/Tooling/Refactoring/Rename/USRFindingAction.cpp @@ -16,7 +16,7 @@ #include "clang/AST/ASTConsumer.h" #include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Lex/Lexer.h" #include "clang/Tooling/Refactoring/Rename/USRFinder.h" @@ -53,10 +53,11 @@ namespace { // AdditionalUSRFinder. AdditionalUSRFinder adds USRs of ctor and dtor if given // Decl refers to class and adds USRs of all overridden methods if Decl refers // to virtual method. -class AdditionalUSRFinder : public RecursiveASTVisitor { -public: +struct AdditionalUSRFinder : ConstDynamicRecursiveASTVisitor { AdditionalUSRFinder(const Decl *FoundDecl, ASTContext &Context) - : FoundDecl(FoundDecl), Context(Context) {} + : FoundDecl(FoundDecl), Context(Context) { + ShouldVisitTemplateInstantiations = true; + } std::vector Find() { // Fill OverriddenMethods and PartialSpecs storages. @@ -95,9 +96,7 @@ class AdditionalUSRFinder : public RecursiveASTVisitor { return std::vector(USRSet.begin(), USRSet.end()); } - bool shouldVisitTemplateInstantiations() const { return true; } - - bool VisitCXXMethodDecl(const CXXMethodDecl *MethodDecl) { + bool VisitCXXMethodDecl(const CXXMethodDecl *MethodDecl) override { if (MethodDecl->isVirtual()) OverriddenMethods.push_back(MethodDecl); if (MethodDecl->getInstantiatedFromMemberFunction()) diff --git a/clang/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp b/clang/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp index c9108fc299cc1..3f24807e7f7c9 100644 --- a/clang/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp +++ b/clang/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp @@ -15,8 +15,8 @@ #include "clang/Tooling/Refactoring/Rename/USRLocFinder.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/ParentMapContext.h" -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/Basic/LLVM.h" #include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceManager.h" @@ -184,8 +184,7 @@ NestedNameSpecifier GetNestedNameForType(TypeLoc TL) { // // This class will traverse the AST and find every AST node whose USR is in the // given USRs' set. -class RenameLocFinder : public RecursiveASTVisitor { -public: +struct RenameLocFinder : ConstDynamicRecursiveASTVisitor { RenameLocFinder(llvm::ArrayRef USRs, ASTContext &Context) : USRSet(USRs.begin(), USRs.end()), Context(Context) {} @@ -210,7 +209,7 @@ class RenameLocFinder : public RecursiveASTVisitor { bool IgnorePrefixQualifiers; }; - bool VisitNamedDecl(const NamedDecl *Decl) { + bool VisitNamedDecl(const NamedDecl *Decl) override { // UsingDecl has been handled in other place. if (llvm::isa(Decl)) return true; @@ -243,7 +242,7 @@ class RenameLocFinder : public RecursiveASTVisitor { return true; } - bool VisitMemberExpr(const MemberExpr *Expr) { + bool VisitMemberExpr(const MemberExpr *Expr) override { const NamedDecl *Decl = Expr->getFoundDecl(); auto StartLoc = Expr->getMemberLoc(); auto EndLoc = Expr->getMemberLoc(); @@ -257,7 +256,7 @@ class RenameLocFinder : public RecursiveASTVisitor { return true; } - bool VisitDesignatedInitExpr(const DesignatedInitExpr *E) { + bool VisitDesignatedInitExpr(const DesignatedInitExpr *E) override { for (const DesignatedInitExpr::Designator &D : E->designators()) { if (D.isFieldDesignator()) { if (const FieldDecl *Decl = D.getFieldDecl()) { @@ -276,7 +275,7 @@ class RenameLocFinder : public RecursiveASTVisitor { return true; } - bool VisitCXXConstructorDecl(const CXXConstructorDecl *CD) { + bool VisitCXXConstructorDecl(const CXXConstructorDecl *CD) override { // Fix the constructor initializer when renaming class members. for (const auto *Initializer : CD->inits()) { // Ignore implicit initializers. @@ -297,7 +296,7 @@ class RenameLocFinder : public RecursiveASTVisitor { return true; } - bool VisitDeclRefExpr(const DeclRefExpr *Expr) { + bool VisitDeclRefExpr(const DeclRefExpr *Expr) override { const NamedDecl *Decl = Expr->getFoundDecl(); // Get the underlying declaration of the shadow declaration introduced by a // using declaration. @@ -371,7 +370,7 @@ class RenameLocFinder : public RecursiveASTVisitor { return true; } - bool VisitUsingDecl(const UsingDecl *Using) { + bool VisitUsingDecl(const UsingDecl *Using) override { for (const auto *UsingShadow : Using->shadows()) { if (isInUSRSet(UsingShadow->getTargetDecl())) { UsingDecls.push_back(Using); @@ -381,7 +380,7 @@ class RenameLocFinder : public RecursiveASTVisitor { return true; } - bool VisitNestedNameSpecifierLocations(NestedNameSpecifierLoc NestedLoc) { + bool visitNestedNameSpecifierLocations(NestedNameSpecifierLoc NestedLoc) { TypeLoc TL = NestedLoc.getAsTypeLoc(); if (!TL) return true; @@ -400,7 +399,7 @@ class RenameLocFinder : public RecursiveASTVisitor { return true; } - bool VisitTypeLoc(TypeLoc Loc) { + bool VisitTypeLoc(TypeLoc Loc) override { auto Parents = Context.getParents(Loc); TypeLoc ParentTypeLoc; if (!Parents.empty()) { @@ -409,7 +408,7 @@ class RenameLocFinder : public RecursiveASTVisitor { // The VisitNestedNameSpecifierLoc interface is not impelmented in // RecursiveASTVisitor, we have to handle it explicitly. if (const auto *NSL = Parents[0].get()) { - VisitNestedNameSpecifierLocations(*NSL); + visitNestedNameSpecifierLocations(*NSL); return true; } diff --git a/clang/unittests/AST/RecursiveASTVisitorTest.cpp b/clang/unittests/AST/RecursiveASTVisitorTest.cpp index c5ad29a77d211..06021509a7a0b 100644 --- a/clang/unittests/AST/RecursiveASTVisitorTest.cpp +++ b/clang/unittests/AST/RecursiveASTVisitorTest.cpp @@ -6,11 +6,11 @@ // //===----------------------------------------------------------------------===// -#include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/ASTConsumer.h" #include "clang/AST/ASTContext.h" #include "clang/AST/Attr.h" #include "clang/AST/Decl.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/AST/TypeLoc.h" #include "clang/Frontend/FrontendAction.h" #include "clang/Tooling/Tooling.h" @@ -68,61 +68,61 @@ enum class VisitEvent { EndTraverseObjCProtocolLoc, }; -class CollectInterestingEvents - : public RecursiveASTVisitor { +class CollectInterestingEvents : public DynamicRecursiveASTVisitor { public: - bool TraverseFunctionDecl(FunctionDecl *D) { + bool TraverseFunctionDecl(FunctionDecl *D) override { Events.push_back(VisitEvent::StartTraverseFunction); - bool Ret = RecursiveASTVisitor::TraverseFunctionDecl(D); + bool Ret = DynamicRecursiveASTVisitor::TraverseFunctionDecl(D); Events.push_back(VisitEvent::EndTraverseFunction); return Ret; } - bool TraverseAttr(Attr *A) { + bool TraverseAttr(Attr *A) override { Events.push_back(VisitEvent::StartTraverseAttr); - bool Ret = RecursiveASTVisitor::TraverseAttr(A); + bool Ret = DynamicRecursiveASTVisitor::TraverseAttr(A); Events.push_back(VisitEvent::EndTraverseAttr); return Ret; } - bool TraverseEnumDecl(EnumDecl *D) { + bool TraverseEnumDecl(EnumDecl *D) override { Events.push_back(VisitEvent::StartTraverseEnum); - bool Ret = RecursiveASTVisitor::TraverseEnumDecl(D); + bool Ret = DynamicRecursiveASTVisitor::TraverseEnumDecl(D); Events.push_back(VisitEvent::EndTraverseEnum); return Ret; } - bool TraverseTypedefTypeLoc(TypedefTypeLoc TL, bool TraverseQualifier) { + bool TraverseTypedefTypeLoc(TypedefTypeLoc TL, + bool TraverseQualifier) override { Events.push_back(VisitEvent::StartTraverseTypedefType); - bool Ret = - RecursiveASTVisitor::TraverseTypedefTypeLoc(TL, TraverseQualifier); + bool Ret = DynamicRecursiveASTVisitor::TraverseTypedefTypeLoc( + TL, TraverseQualifier); Events.push_back(VisitEvent::EndTraverseTypedefType); return Ret; } - bool TraverseObjCInterfaceDecl(ObjCInterfaceDecl *ID) { + bool TraverseObjCInterfaceDecl(ObjCInterfaceDecl *ID) override { Events.push_back(VisitEvent::StartTraverseObjCInterface); - bool Ret = RecursiveASTVisitor::TraverseObjCInterfaceDecl(ID); + bool Ret = DynamicRecursiveASTVisitor::TraverseObjCInterfaceDecl(ID); Events.push_back(VisitEvent::EndTraverseObjCInterface); return Ret; } - bool TraverseObjCProtocolDecl(ObjCProtocolDecl *PD) { + bool TraverseObjCProtocolDecl(ObjCProtocolDecl *PD) override { Events.push_back(VisitEvent::StartTraverseObjCProtocol); - bool Ret = RecursiveASTVisitor::TraverseObjCProtocolDecl(PD); + bool Ret = DynamicRecursiveASTVisitor::TraverseObjCProtocolDecl(PD); Events.push_back(VisitEvent::EndTraverseObjCProtocol); return Ret; } - bool TraverseObjCProtocolLoc(ObjCProtocolLoc ProtocolLoc) { + bool TraverseObjCProtocolLoc(ObjCProtocolLoc ProtocolLoc) override { Events.push_back(VisitEvent::StartTraverseObjCProtocolLoc); - bool Ret = RecursiveASTVisitor::TraverseObjCProtocolLoc(ProtocolLoc); + bool Ret = DynamicRecursiveASTVisitor::TraverseObjCProtocolLoc(ProtocolLoc); Events.push_back(VisitEvent::EndTraverseObjCProtocolLoc); return Ret; diff --git a/clang/unittests/Frontend/NoAlterCodeGenActionTest.cpp b/clang/unittests/Frontend/NoAlterCodeGenActionTest.cpp index fed2d255a9fe8..97556e1eb1535 100644 --- a/clang/unittests/Frontend/NoAlterCodeGenActionTest.cpp +++ b/clang/unittests/Frontend/NoAlterCodeGenActionTest.cpp @@ -11,7 +11,7 @@ //===----------------------------------------------------------------------===// #include "clang/AST/ASTConsumer.h" -#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/DynamicRecursiveASTVisitor.h" #include "clang/Basic/LangStandard.h" #include "clang/CodeGen/BackendUtil.h" #include "clang/CodeGen/CodeGenAction.h" @@ -30,16 +30,16 @@ using namespace clang::tooling; namespace { -class ASTChecker : public RecursiveASTVisitor { +class ASTChecker : public DynamicRecursiveASTVisitor { public: ASTContext &Ctx; ASTChecker(ASTContext &Ctx) : Ctx(Ctx) {} - bool VisitReturnStmt(ReturnStmt *RS) { + bool VisitReturnStmt(ReturnStmt *RS) override { EXPECT_TRUE(RS->getRetValue()); return true; } - bool VisitCoroutineBodyStmt(CoroutineBodyStmt *CS) { + bool VisitCoroutineBodyStmt(CoroutineBodyStmt *CS) override { return VisitReturnStmt(cast(CS->getReturnStmt())); } };