diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h index 306eca7140d1a..9efca848ef03b 100644 --- a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h +++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h @@ -72,7 +72,8 @@ class StmtAncestorASTVisitor /// Accessor for DeclParents. const DeclParentMap &getDeclToParentStmtMap() { return DeclParents; } - friend class clang::RecursiveASTVisitor; + friend class clang::RecursiveASTVisitorBase; private: StmtParentMap StmtAncestors; @@ -98,7 +99,8 @@ class ComponentFinderASTVisitor /// Accessor for Components. const ComponentVector &getComponents() { return Components; } - friend class clang::RecursiveASTVisitor; + friend class clang::RecursiveASTVisitorBase; private: ComponentVector Components; @@ -155,7 +157,8 @@ class DependencyFinderASTVisitor return DependsOnInsideVariable; } - friend class clang::RecursiveASTVisitor; + friend class clang::RecursiveASTVisitorBase; private: const StmtParentMap *StmtParents; @@ -188,7 +191,8 @@ class DeclFinderASTVisitor return Found; } - friend class clang::RecursiveASTVisitor; + friend class clang::RecursiveASTVisitorBase; private: std::string Name; @@ -340,7 +344,7 @@ class ForLoopIndexUseVisitor private: /// Typedef used in CRTP functions. using VisitorBase = RecursiveASTVisitor; - friend class RecursiveASTVisitor; + friend class RecursiveASTVisitorBase; /// Overriden methods for RecursiveASTVisitor's traversal. bool TraverseArraySubscriptExpr(ArraySubscriptExpr *E); diff --git a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp index d5ccbb73735ec..8161d4563fcec 100644 --- a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp @@ -95,7 +95,8 @@ static bool paramReferredExactlyOnce(const CXXConstructorDecl *Ctor, /// \see ExactlyOneUsageVisitor::hasExactlyOneUsageIn() class ExactlyOneUsageVisitor : public RecursiveASTVisitor { - friend class RecursiveASTVisitor; + friend class RecursiveASTVisitorBase; public: ExactlyOneUsageVisitor(const ParmVarDecl *ParamDecl) diff --git a/clang-tools-extra/clangd/AST.cpp b/clang-tools-extra/clangd/AST.cpp index 0dcff2eae05e7..ccec4478ecc04 100644 --- a/clang-tools-extra/clangd/AST.cpp +++ b/clang-tools-extra/clangd/AST.cpp @@ -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 ConstRecursiveASTVisitor { 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) { 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) { 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(const DecltypeTypeLoc TL) { 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) { if (!PVD->getType()->isDependentType()) return true; // 'auto' here does not name an AutoType, but an implicit template param. @@ -606,7 +606,7 @@ class DeducedTypeVisitor : public RecursiveASTVisitor { return true; } - static int paramIndex(const TemplateDecl &TD, NamedDecl &Param) { + static int paramIndex(const TemplateDecl &TD, const NamedDecl &Param) { unsigned I = 0; for (auto *ND : *TD.getTemplateParameters()) { if (&Param == ND) @@ -620,7 +620,7 @@ class DeducedTypeVisitor : public RecursiveASTVisitor { }; } // namespace -std::optional getDeducedType(ASTContext &ASTCtx, +std::optional getDeducedType(const ASTContext &ASTCtx, const HeuristicResolver *Resolver, SourceLocation Loc) { if (!Loc.isValid()) @@ -659,7 +659,7 @@ static NamedDecl *getOnlyInstantiationImpl(TemplateDeclTy *TD) { return Only; } -NamedDecl *getOnlyInstantiation(NamedDecl *TemplatedDecl) { +NamedDecl *getOnlyInstantiation(const NamedDecl *TemplatedDecl) { if (TemplateDecl *TD = TemplatedDecl->getDescribedTemplate()) { if (auto *CTD = llvm::dyn_cast(TD)) return getOnlyInstantiationImpl(CTD); diff --git a/clang-tools-extra/clangd/AST.h b/clang-tools-extra/clangd/AST.h index 2b83595e5b8e9..813e684612c45 100644 --- a/clang-tools-extra/clangd/AST.h +++ b/clang-tools-extra/clangd/AST.h @@ -168,7 +168,8 @@ QualType declaredType(const TypeDecl *D); /// Retrieves the deduced type at a given location (auto, decltype). /// It will return the underlying type. /// If the type is an undeduced auto, returns the type itself. -std::optional getDeducedType(ASTContext &, const HeuristicResolver *, +std::optional getDeducedType(const ASTContext &, + const HeuristicResolver *, SourceLocation Loc); // Find the abbreviated-function-template `auto` within a type, or returns null. @@ -180,7 +181,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); +NamedDecl *getOnlyInstantiation(const NamedDecl *TemplatedDecl); /// Return attributes attached directly to a node. std::vector getAttributes(const DynTypedNode &); diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index 1d1b7f183f75a..a3ea8a58b024a 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -153,15 +153,29 @@ isSameMethod([[maybe_unused]] FirstMethodPtrTy FirstMethodPtr, /// By default, this visitor preorder traverses the AST. If postorder traversal /// is needed, the \c shouldTraversePostOrder method needs to be overridden /// to return \c true. -template class RecursiveASTVisitor { +/// Base template for RecursiveASTVisitor that can handle both const and +/// non-const AST node traversal. The IsConst template parameter determines +/// whether AST node pointers are const or non-const. +template class RecursiveASTVisitorBase { +protected: + template + using MaybeConst = std::conditional_t; + public: + // Type aliases that vary based on IsConst template parameter + using StmtPtr = MaybeConst *; + using DeclPtr = MaybeConst *; + using TypePtr = MaybeConst *; + using ExprPtr = MaybeConst *; + using AttrPtr = MaybeConst *; + /// A queue used for performing data recursion over statements. /// Parameters involving this type are used to implement data /// recursion over Stmts and Exprs within this class, and should /// typically not be explicitly specified by derived classes. /// The bool bit indicates whether the statement has been traversed or not. - typedef SmallVectorImpl> - DataRecursionQueue; + using DataRecursionQueue = + SmallVectorImpl>; /// Return a reference to the derived class. Derived &getDerived() { return *static_cast(this); } @@ -186,7 +200,7 @@ template class RecursiveASTVisitor { /// Recursively visits an entire AST, starting from the TranslationUnitDecl. /// \returns false if visitation was terminated early. - bool TraverseAST(ASTContext &AST) { + bool TraverseAST(MaybeConst &AST) { // Currently just an alias for TraverseDecl(TUDecl), but kept in case // we change the implementation again. return getDerived().TraverseDecl(AST.getTranslationUnitDecl()); @@ -197,19 +211,19 @@ template class RecursiveASTVisitor { /// /// \returns false if the visitation was terminated early, true /// otherwise (including when the argument is nullptr). - bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue = nullptr); + bool TraverseStmt(StmtPtr S, DataRecursionQueue *Queue = nullptr); /// Invoked before visiting a statement or expression via data recursion. /// /// \returns false to skip visiting the node, true otherwise. - bool dataTraverseStmtPre(Stmt *S) { return true; } + bool dataTraverseStmtPre(StmtPtr S) { return true; } /// Invoked after visiting a statement or expression via data recursion. /// This is not invoked if the previously invoked \c dataTraverseStmtPre /// returned false. /// /// \returns false if the visitation was terminated early, true otherwise. - bool dataTraverseStmtPost(Stmt *S) { return true; } + bool dataTraverseStmtPost(StmtPtr S) { return true; } /// Recursively visit a type, by dispatching to /// Traverse*Type() based on the argument's getTypeClass() property. @@ -230,14 +244,14 @@ template class RecursiveASTVisitor { /// /// \returns false if the visitation was terminated early, true /// otherwise (including when the argument is a Null type location). - bool TraverseAttr(Attr *At); + bool TraverseAttr(AttrPtr At); /// Recursively visit a declaration, by dispatching to /// Traverse*Decl() based on the argument's dynamic type. /// /// \returns false if the visitation was terminated early, true /// otherwise (including when the argument is NULL). - bool TraverseDecl(Decl *D); + bool TraverseDecl(DeclPtr D); /// Recursively visit a C++ nested-name-specifier. /// @@ -294,20 +308,20 @@ template class RecursiveASTVisitor { /// be overridden for clients that need access to the name. /// /// \returns false if the visitation was terminated early, true otherwise. - bool TraverseConstructorInitializer(CXXCtorInitializer *Init); + bool TraverseConstructorInitializer(MaybeConst *Init); /// Recursively visit a lambda capture. \c Init is the expression that /// will be used to initialize the capture. /// /// \returns false if the visitation was terminated early, true otherwise. - bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C, - Expr *Init); + bool TraverseLambdaCapture(MaybeConst *LE, const LambdaCapture *C, + MaybeConst *Init); /// Recursively visit the syntactic or semantic form of an /// initialization list. /// /// \returns false if the visitation was terminated early, true otherwise. - bool TraverseSynOrSemInitListExpr(InitListExpr *S, + bool TraverseSynOrSemInitListExpr(MaybeConst *S, DataRecursionQueue *Queue = nullptr); /// Recursively visit an Objective-C protocol reference with location @@ -319,14 +333,14 @@ template class RecursiveASTVisitor { /// Recursively visit concept reference with location information. /// /// \returns false if the visitation was terminated early, true otherwise. - bool TraverseConceptReference(ConceptReference *CR); + bool TraverseConceptReference(MaybeConst *CR); // Visit concept reference. - bool VisitConceptReference(ConceptReference *CR) { return true; } + bool VisitConceptReference(MaybeConst *CR) { return true; } // ---- Methods on Attrs ---- // Visit an attribute. - bool VisitAttr(Attr *A) { return true; } + bool VisitAttr(AttrPtr A) { return true; } // Declare Traverse* and empty Visit* for all Attr classes. #define ATTR_VISITOR_DECLS_ONLY @@ -335,7 +349,10 @@ template class RecursiveASTVisitor { // ---- Methods on Stmts ---- - Stmt::child_range getStmtChildren(Stmt *S) { return S->children(); } +using MaybeConstChildRange = + std::conditional_t; + +MaybeConstChildRange getStmtChildren(StmtPtr S) { return S->children(); } private: // Traverse the given statement. If the most-derived traverse function takes a @@ -345,15 +362,18 @@ template class RecursiveASTVisitor { // arm call our function rather than the derived class version. #define TRAVERSE_STMT_BASE(NAME, CLASS, VAR, QUEUE) \ (::clang::detail::has_same_member_pointer_type< \ - decltype(&RecursiveASTVisitor::Traverse##NAME), \ + decltype(&RecursiveASTVisitorBase::Traverse##NAME), \ decltype(&Derived::Traverse##NAME)>::value \ ? static_cast::value, \ - Derived &, RecursiveASTVisitor &>>(*this) \ - .Traverse##NAME(static_cast(VAR), QUEUE) \ - : getDerived().Traverse##NAME(static_cast(VAR))) + Derived &, RecursiveASTVisitorBase &>>(*this) \ + .Traverse##NAME(const_cast *>( \ + static_cast(VAR)), \ + QUEUE) \ + : getDerived().Traverse##NAME(const_cast *>( \ + static_cast(VAR)))) // Try to traverse the given statement, or enqueue it if we're performing data // recursion in the middle of traversing another statement. Can only be called @@ -367,21 +387,22 @@ template class RecursiveASTVisitor { public: // Declare Traverse*() for all concrete Stmt classes. #define ABSTRACT_STMT(STMT) -#define STMT(CLASS, PARENT) \ - bool Traverse##CLASS(CLASS *S, DataRecursionQueue *Queue = nullptr); +#define STMT(CLASS, PARENT) \ + bool Traverse##CLASS(MaybeConst *S, \ + DataRecursionQueue *Queue = nullptr); #include "clang/AST/StmtNodes.inc" // The above header #undefs ABSTRACT_STMT and STMT upon exit. // Define WalkUpFrom*() and empty Visit*() for all Stmt classes. - bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); } - bool VisitStmt(Stmt *S) { return true; } + bool WalkUpFromStmt(StmtPtr S) { return getDerived().VisitStmt(S); } + bool VisitStmt(StmtPtr S) { return true; } #define STMT(CLASS, PARENT) \ - bool WalkUpFrom##CLASS(CLASS *S) { \ + bool WalkUpFrom##CLASS(MaybeConst *S) { \ TRY_TO(WalkUpFrom##PARENT(S)); \ TRY_TO(Visit##CLASS(S)); \ return true; \ } \ - bool Visit##CLASS(CLASS *S) { return true; } + bool Visit##CLASS(MaybeConst *S) { return true; } #include "clang/AST/StmtNodes.inc" // ---- Methods on Types ---- @@ -390,20 +411,21 @@ template class RecursiveASTVisitor { // Declare Traverse*() for all concrete Type classes. #define ABSTRACT_TYPE(CLASS, BASE) #define TYPE(CLASS, BASE) \ - bool Traverse##CLASS##Type(CLASS##Type *T, bool TraverseQualifier); + bool Traverse##CLASS##Type(MaybeConst *T, \ + bool TraverseQualifier); #include "clang/AST/TypeNodes.inc" // The above header #undefs ABSTRACT_TYPE and TYPE upon exit. // Define WalkUpFrom*() and empty Visit*() for all Type classes. - bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); } - bool VisitType(Type *T) { return true; } + bool WalkUpFromType(TypePtr T) { return getDerived().VisitType(T); } + bool VisitType(TypePtr T) { return true; } #define TYPE(CLASS, BASE) \ - bool WalkUpFrom##CLASS##Type(CLASS##Type *T) { \ + bool WalkUpFrom##CLASS##Type(MaybeConst *T) { \ TRY_TO(WalkUpFrom##BASE(T)); \ TRY_TO(Visit##CLASS##Type(T)); \ return true; \ } \ - bool Visit##CLASS##Type(CLASS##Type *T) { return true; } + bool Visit##CLASS##Type(MaybeConst *T) { return true; } #include "clang/AST/TypeNodes.inc" // ---- Methods on TypeLocs ---- @@ -445,26 +467,28 @@ template class RecursiveASTVisitor { // Declare Traverse*() for all concrete Decl classes. #define ABSTRACT_DECL(DECL) -#define DECL(CLASS, BASE) bool Traverse##CLASS##Decl(CLASS##Decl *D); +#define DECL(CLASS, BASE) \ + bool Traverse##CLASS##Decl(MaybeConst *D); #include "clang/AST/DeclNodes.inc" // The above header #undefs ABSTRACT_DECL and DECL upon exit. // Define WalkUpFrom*() and empty Visit*() for all Decl classes. - bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); } - bool VisitDecl(Decl *D) { return true; } + bool WalkUpFromDecl(DeclPtr D) { return getDerived().VisitDecl(D); } + bool VisitDecl(DeclPtr D) { return true; } #define DECL(CLASS, BASE) \ - bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) { \ + bool WalkUpFrom##CLASS##Decl(MaybeConst *D) { \ TRY_TO(WalkUpFrom##BASE(D)); \ TRY_TO(Visit##CLASS##Decl(D)); \ return true; \ } \ - bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; } + bool Visit##CLASS##Decl(MaybeConst *D) { return true; } #include "clang/AST/DeclNodes.inc" bool canIgnoreChildDeclWhileTraversingDeclContext(const Decl *Child); #define DEF_TRAVERSE_TMPL_INST(TMPLDECLKIND) \ - bool TraverseTemplateInstantiations(TMPLDECLKIND##TemplateDecl *D); + bool TraverseTemplateInstantiations( \ + MaybeConst *D); DEF_TRAVERSE_TMPL_INST(Class) DEF_TRAVERSE_TMPL_INST(Var) DEF_TRAVERSE_TMPL_INST(Function) @@ -472,64 +496,68 @@ template class RecursiveASTVisitor { bool TraverseTypeConstraint(const TypeConstraint *C); - bool TraverseConceptRequirement(concepts::Requirement *R); - bool TraverseConceptTypeRequirement(concepts::TypeRequirement *R); - bool TraverseConceptExprRequirement(concepts::ExprRequirement *R); - bool TraverseConceptNestedRequirement(concepts::NestedRequirement *R); + bool TraverseConceptRequirement(MaybeConst *R); + bool TraverseConceptTypeRequirement(MaybeConst *R); + bool TraverseConceptExprRequirement(MaybeConst *R); + bool + TraverseConceptNestedRequirement(MaybeConst *R); - bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue); + /// Internal method for data recursion traversal + bool dataTraverseNode(StmtPtr S, DataRecursionQueue *Queue); private: // These are helper methods used by more than one Traverse* method. - bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL); + bool + TraverseTemplateParameterListHelper(MaybeConst *TPL); // Traverses template parameter lists of either a DeclaratorDecl or TagDecl. template bool TraverseDeclTemplateParameterLists(T *D); - bool TraverseTemplateTypeParamDeclConstraints(const TemplateTypeParmDecl *D); + bool TraverseTemplateTypeParamDeclConstraints( + const MaybeConst *D); bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL, unsigned Count); bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL); - bool TraverseSubstPackTypeHelper(SubstPackType *T); + bool TraverseSubstPackTypeHelper(MaybeConst *T); bool TraverseSubstPackTypeLocHelper(SubstPackTypeLoc TL); - bool TraverseRecordHelper(RecordDecl *D); - bool TraverseCXXRecordHelper(CXXRecordDecl *D); - bool TraverseDeclaratorHelper(DeclaratorDecl *D); - bool TraverseDeclContextHelper(DeclContext *DC); - bool TraverseFunctionHelper(FunctionDecl *D); - bool TraverseVarHelper(VarDecl *D); - bool TraverseOMPExecutableDirective(OMPExecutableDirective *S); - bool TraverseOMPLoopDirective(OMPLoopDirective *S); - bool TraverseOMPClause(OMPClause *C); - bool TraverseTagType(TagType *T, bool TraverseQualifier); + bool TraverseRecordHelper(MaybeConst *D); + bool TraverseCXXRecordHelper(MaybeConst *D); + bool TraverseDeclaratorHelper(MaybeConst *D); + bool TraverseDeclContextHelper(MaybeConst *DC); + bool TraverseFunctionHelper(MaybeConst *D); + bool TraverseVarHelper(MaybeConst *D); + bool TraverseOMPExecutableDirective(MaybeConst *S); + bool TraverseOMPLoopDirective(MaybeConst *S); + bool TraverseOMPClause(MaybeConst *C); + bool TraverseTagType(MaybeConst *T, bool TraverseQualifier); bool TraverseTagTypeLoc(TagTypeLoc TL, bool TraverseQualifier); #define GEN_CLANG_CLAUSE_CLASS -#define CLAUSE_CLASS(Enum, Str, Class) bool Visit##Class(Class *C); +#define CLAUSE_CLASS(Enum, Str, Class) bool Visit##Class(MaybeConst *C); #include "llvm/Frontend/OpenMP/OMP.inc" /// Process clauses with list of variables. template bool VisitOMPClauseList(T *Node); /// Process clauses with pre-initis. - bool VisitOMPClauseWithPreInit(OMPClauseWithPreInit *Node); - bool VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *Node); + bool VisitOMPClauseWithPreInit(MaybeConst *Node); + bool VisitOMPClauseWithPostUpdate(MaybeConst *Node); - bool PostVisitStmt(Stmt *S); - bool TraverseOpenACCConstructStmt(OpenACCConstructStmt *S); - bool - TraverseOpenACCAssociatedStmtConstruct(OpenACCAssociatedStmtConstruct *S); + bool PostVisitStmt(StmtPtr S); + bool TraverseOpenACCConstructStmt(MaybeConst *S); + bool TraverseOpenACCAssociatedStmtConstruct( + MaybeConst *S); bool VisitOpenACCClauseList(ArrayRef); bool VisitOpenACCClause(const OpenACCClause *); }; -template -bool RecursiveASTVisitor::TraverseTypeConstraint( +template +bool RecursiveASTVisitorBase::TraverseTypeConstraint( const TypeConstraint *C) { if (!getDerived().shouldVisitImplicitCode()) { TRY_TO(TraverseConceptReference(C->getConceptReference())); return true; } - if (Expr *IDC = C->getImmediatelyDeclaredConstraint()) { + if (ExprPtr IDC = C->getImmediatelyDeclaredConstraint()) { TRY_TO(TraverseStmt(IDC)); } else { // Avoid traversing the ConceptReference in the TypeConstraint @@ -541,27 +569,30 @@ bool RecursiveASTVisitor::TraverseTypeConstraint( return true; } -template -bool RecursiveASTVisitor::TraverseConceptRequirement( - concepts::Requirement *R) { +template +bool RecursiveASTVisitorBase::TraverseConceptRequirement( + MaybeConst *R) { switch (R->getKind()) { case concepts::Requirement::RK_Type: return getDerived().TraverseConceptTypeRequirement( - cast(R)); + const_cast *>( + cast(R))); case concepts::Requirement::RK_Simple: case concepts::Requirement::RK_Compound: return getDerived().TraverseConceptExprRequirement( - cast(R)); + const_cast *>( + cast(R))); case concepts::Requirement::RK_Nested: return getDerived().TraverseConceptNestedRequirement( - cast(R)); + const_cast *>( + cast(R))); } llvm_unreachable("unexpected case"); } -template -bool RecursiveASTVisitor::dataTraverseNode(Stmt *S, - DataRecursionQueue *Queue) { +template +bool RecursiveASTVisitorBase::dataTraverseNode( + StmtPtr S, DataRecursionQueue *Queue) { // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt. switch (S->getStmtClass()) { case Stmt::NoStmtClass: @@ -578,17 +609,17 @@ bool RecursiveASTVisitor::dataTraverseNode(Stmt *S, #undef DISPATCH_STMT -template -bool RecursiveASTVisitor::TraverseConceptTypeRequirement( - concepts::TypeRequirement *R) { +template +bool RecursiveASTVisitorBase::TraverseConceptTypeRequirement( + MaybeConst *R) { if (R->isSubstitutionFailure()) return true; return getDerived().TraverseTypeLoc(R->getType()->getTypeLoc()); } -template -bool RecursiveASTVisitor::TraverseConceptExprRequirement( - concepts::ExprRequirement *R) { +template +bool RecursiveASTVisitorBase::TraverseConceptExprRequirement( + MaybeConst *R) { if (!R->isExprSubstitutionFailure()) TRY_TO(TraverseStmt(R->getExpr())); auto &RetReq = R->getReturnTypeRequirement(); @@ -604,16 +635,17 @@ bool RecursiveASTVisitor::TraverseConceptExprRequirement( return true; } -template -bool RecursiveASTVisitor::TraverseConceptNestedRequirement( - concepts::NestedRequirement *R) { +template +bool RecursiveASTVisitorBase:: + TraverseConceptNestedRequirement( + MaybeConst *R) { if (!R->hasInvalidConstraint()) return getDerived().TraverseStmt(R->getConstraintExpr()); return true; } -template -bool RecursiveASTVisitor::PostVisitStmt(Stmt *S) { +template +bool RecursiveASTVisitorBase::PostVisitStmt(StmtPtr S) { // In pre-order traversal mode, each Traverse##STMT method is responsible for // calling WalkUpFrom. Therefore, if the user overrides Traverse##STMT and // does not call the default implementation, the WalkUpFrom callback is not @@ -636,16 +668,20 @@ bool RecursiveASTVisitor::PostVisitStmt(Stmt *S) { #define ABSTRACT_STMT(STMT) #define STMT(CLASS, PARENT) \ case Stmt::CLASS##Class: \ - if (::clang::detail::isSameMethod(&RecursiveASTVisitor::Traverse##CLASS, \ - &Derived::Traverse##CLASS)) { \ - TRY_TO(WalkUpFrom##CLASS(static_cast(S))); \ + if (::clang::detail::isSameMethod( \ + &RecursiveASTVisitorBase::Traverse##CLASS, \ + &Derived::Traverse##CLASS)) { \ + TRY_TO(WalkUpFrom##CLASS( \ + const_cast *>(static_cast(S)))); \ } \ break; #define INITLISTEXPR(CLASS, PARENT) \ case Stmt::CLASS##Class: \ - if (::clang::detail::isSameMethod(&RecursiveASTVisitor::Traverse##CLASS, \ - &Derived::Traverse##CLASS)) { \ - auto ILE = static_cast(S); \ + if (::clang::detail::isSameMethod( \ + &RecursiveASTVisitorBase::Traverse##CLASS, \ + &Derived::Traverse##CLASS)) { \ + auto ILE = \ + const_cast *>(static_cast(S)); \ if (auto Syn = ILE->isSemanticForm() ? ILE->getSyntacticForm() : ILE) \ TRY_TO(WalkUpFrom##CLASS(Syn)); \ if (auto Sem = ILE->isSemanticForm() ? ILE : ILE->getSemanticForm()) \ @@ -662,9 +698,10 @@ bool RecursiveASTVisitor::PostVisitStmt(Stmt *S) { // Inlining this method can lead to large code size and compile-time increases // without any benefit to runtime performance. -template +template LLVM_ATTRIBUTE_NOINLINE bool -RecursiveASTVisitor::TraverseStmt(Stmt *S, DataRecursionQueue *Queue) { +RecursiveASTVisitorBase::TraverseStmt( + StmtPtr S, DataRecursionQueue *Queue) { if (!S) return true; @@ -673,12 +710,12 @@ RecursiveASTVisitor::TraverseStmt(Stmt *S, DataRecursionQueue *Queue) { return true; } - SmallVector, 8> LocalQueue; + SmallVector, 8> LocalQueue; LocalQueue.push_back({S, false}); while (!LocalQueue.empty()) { auto &CurrSAndVisited = LocalQueue.back(); - Stmt *CurrS = CurrSAndVisited.getPointer(); + StmtPtr CurrS = CurrSAndVisited.getPointer(); bool Visited = CurrSAndVisited.getInt(); if (Visited) { LocalQueue.pop_back(); @@ -703,9 +740,9 @@ RecursiveASTVisitor::TraverseStmt(Stmt *S, DataRecursionQueue *Queue) { return true; } -template -bool RecursiveASTVisitor::TraverseType(QualType T, - bool TraverseQualifier) { +template +bool RecursiveASTVisitorBase::TraverseType( + QualType T, bool TraverseQualifier) { if (T.isNull()) return true; @@ -714,7 +751,8 @@ bool RecursiveASTVisitor::TraverseType(QualType T, #define TYPE(CLASS, BASE) \ case Type::CLASS: \ return getDerived().Traverse##CLASS##Type( \ - static_cast(const_cast(T.getTypePtr())), \ + const_cast *>( \ + static_cast(T.getTypePtr())), \ TraverseQualifier); #include "clang/AST/TypeNodes.inc" } @@ -722,9 +760,9 @@ bool RecursiveASTVisitor::TraverseType(QualType T, return true; } -template -bool RecursiveASTVisitor::TraverseTypeLoc(TypeLoc TL, - bool TraverseQualifier) { +template +bool RecursiveASTVisitorBase::TraverseTypeLoc( + TypeLoc TL, bool TraverseQualifier) { if (TL.isNull()) return true; @@ -741,12 +779,12 @@ bool RecursiveASTVisitor::TraverseTypeLoc(TypeLoc TL, } // Define the Traverse*Attr(Attr* A) methods -#define VISITORCLASS RecursiveASTVisitor +#define VISITORCLASS RecursiveASTVisitorBase #include "clang/AST/AttrVisitor.inc" #undef VISITORCLASS -template -bool RecursiveASTVisitor::TraverseDecl(Decl *D) { +template +bool RecursiveASTVisitorBase::TraverseDecl(DeclPtr D) { if (!D) return true; @@ -779,7 +817,9 @@ bool RecursiveASTVisitor::TraverseDecl(Decl *D) { #define ABSTRACT_DECL(DECL) #define DECL(CLASS, BASE) \ case Decl::CLASS: \ - if (!getDerived().Traverse##CLASS##Decl(static_cast(D))) \ + if (!getDerived().Traverse##CLASS##Decl( \ + const_cast *>( \ + static_cast(D)))) \ return false; \ break; #include "clang/AST/DeclNodes.inc" @@ -787,8 +827,8 @@ bool RecursiveASTVisitor::TraverseDecl(Decl *D) { return true; } -template -bool RecursiveASTVisitor::TraverseNestedNameSpecifier( +template +bool RecursiveASTVisitorBase::TraverseNestedNameSpecifier( NestedNameSpecifier NNS) { switch (NNS.getKind()) { case NestedNameSpecifier::Kind::Null: @@ -799,7 +839,7 @@ bool RecursiveASTVisitor::TraverseNestedNameSpecifier( TRY_TO(TraverseNestedNameSpecifier(NNS.getAsNamespaceAndPrefix().Prefix)); return true; case NestedNameSpecifier::Kind::Type: { - auto *T = const_cast(NNS.getAsType()); + auto *T = const_cast *>(NNS.getAsType()); TRY_TO(TraverseNestedNameSpecifier(T->getPrefix())); TRY_TO(TraverseType(QualType(T, 0), /*TraverseQualifier=*/false)); return true; @@ -808,8 +848,8 @@ bool RecursiveASTVisitor::TraverseNestedNameSpecifier( llvm_unreachable("unhandled kind"); } -template -bool RecursiveASTVisitor::TraverseNestedNameSpecifierLoc( +template +bool RecursiveASTVisitorBase::TraverseNestedNameSpecifierLoc( NestedNameSpecifierLoc NNS) { switch (NNS.getNestedNameSpecifier().getKind()) { case NestedNameSpecifier::Kind::Null: @@ -831,8 +871,8 @@ bool RecursiveASTVisitor::TraverseNestedNameSpecifierLoc( return true; } -template -bool RecursiveASTVisitor::TraverseDeclarationNameInfo( +template +bool RecursiveASTVisitorBase::TraverseDeclarationNameInfo( DeclarationNameInfo NameInfo) { switch (NameInfo.getName().getNameKind()) { case DeclarationName::CXXConstructorName: @@ -860,8 +900,9 @@ bool RecursiveASTVisitor::TraverseDeclarationNameInfo( return true; } -template -bool RecursiveASTVisitor::TraverseTemplateName(TemplateName Template) { +template +bool RecursiveASTVisitorBase::TraverseTemplateName( + TemplateName Template) { if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier())); } else if (QualifiedTemplateName *QTN = @@ -874,8 +915,8 @@ bool RecursiveASTVisitor::TraverseTemplateName(TemplateName Template) { return true; } -template -bool RecursiveASTVisitor::TraverseTemplateArgument( +template +bool RecursiveASTVisitorBase::TraverseTemplateArgument( const TemplateArgument &Arg) { switch (Arg.getKind()) { case TemplateArgument::Null: @@ -905,8 +946,8 @@ bool RecursiveASTVisitor::TraverseTemplateArgument( // FIXME: no template name location? // FIXME: no source locations for a template argument pack? -template -bool RecursiveASTVisitor::TraverseTemplateArgumentLoc( +template +bool RecursiveASTVisitorBase::TraverseTemplateArgumentLoc( const TemplateArgumentLoc &ArgLoc) { const TemplateArgument &Arg = ArgLoc.getArgument(); @@ -944,8 +985,8 @@ bool RecursiveASTVisitor::TraverseTemplateArgumentLoc( return true; } -template -bool RecursiveASTVisitor::TraverseTemplateArguments( +template +bool RecursiveASTVisitorBase::TraverseTemplateArguments( ArrayRef Args) { for (const TemplateArgument &Arg : Args) TRY_TO(TraverseTemplateArgument(Arg)); @@ -953,9 +994,9 @@ bool RecursiveASTVisitor::TraverseTemplateArguments( return true; } -template -bool RecursiveASTVisitor::TraverseConstructorInitializer( - CXXCtorInitializer *Init) { +template +bool RecursiveASTVisitorBase::TraverseConstructorInitializer( + MaybeConst *Init) { if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc())); @@ -965,11 +1006,10 @@ bool RecursiveASTVisitor::TraverseConstructorInitializer( return true; } -template -bool -RecursiveASTVisitor::TraverseLambdaCapture(LambdaExpr *LE, - const LambdaCapture *C, - Expr *Init) { +template +bool RecursiveASTVisitorBase::TraverseLambdaCapture( + MaybeConst *LE, const LambdaCapture *C, + MaybeConst *Init) { if (LE->isInitCapture(C)) TRY_TO(TraverseDecl(C->getCapturedVar())); else @@ -981,9 +1021,9 @@ RecursiveASTVisitor::TraverseLambdaCapture(LambdaExpr *LE, // This macro makes available a variable T, the passed-in type. #define DEF_TRAVERSE_TYPE(TYPE, CODE) \ - template \ - bool RecursiveASTVisitor::Traverse##TYPE(TYPE *T, \ - bool TraverseQualifier) { \ + template \ + bool RecursiveASTVisitorBase::Traverse##TYPE( \ + MaybeConst *T, bool TraverseQualifier) { \ if (!getDerived().shouldTraversePostOrder()) \ TRY_TO(WalkUpFrom##TYPE(T)); \ { \ @@ -1025,13 +1065,13 @@ DEF_TRAVERSE_TYPE(DecayedType, { TRY_TO(TraverseType(T->getOriginalType())); }) DEF_TRAVERSE_TYPE(ConstantArrayType, { TRY_TO(TraverseType(T->getElementType())); if (T->getSizeExpr()) - TRY_TO(TraverseStmt(const_cast(T->getSizeExpr()))); + TRY_TO(TraverseStmt(const_cast *>(T->getSizeExpr()))); }) DEF_TRAVERSE_TYPE(ArrayParameterType, { TRY_TO(TraverseType(T->getElementType())); if (T->getSizeExpr()) - TRY_TO(TraverseStmt(const_cast(T->getSizeExpr()))); + TRY_TO(TraverseStmt(const_cast *>(T->getSizeExpr()))); }) DEF_TRAVERSE_TYPE(IncompleteArrayType, @@ -1094,7 +1134,7 @@ DEF_TRAVERSE_TYPE(FunctionProtoType, { TRY_TO(TraverseType(E)); } - if (Expr *NE = T->getNoexceptExpr()) + if (ExprPtr NE = T->getNoexceptExpr()) TRY_TO(TraverseStmt(NE)); }) @@ -1173,9 +1213,9 @@ DEF_TRAVERSE_TYPE(ParenType, { TRY_TO(TraverseType(T->getInnerType())); }) DEF_TRAVERSE_TYPE(MacroQualifiedType, { TRY_TO(TraverseType(T->getUnderlyingType())); }) -template -bool RecursiveASTVisitor::TraverseTagType(TagType *T, - bool TraverseQualifier) { +template +bool RecursiveASTVisitorBase::TraverseTagType( + MaybeConst *T, bool TraverseQualifier) { if (TraverseQualifier) TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); return true; @@ -1253,13 +1293,14 @@ DEF_TRAVERSE_TYPE(PredefinedSugarType, {}) // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods // continue to work. #define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \ - template \ - bool RecursiveASTVisitor::Traverse##TYPE##Loc( \ + template \ + bool RecursiveASTVisitorBase::Traverse##TYPE##Loc( \ TYPE##Loc TL, bool TraverseQualifier) { \ if (!getDerived().shouldTraversePostOrder()) { \ TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \ if (getDerived().shouldWalkTypesOfTypeLocs()) \ - TRY_TO(WalkUpFrom##TYPE(const_cast(TL.getTypePtr()))); \ + TRY_TO(WalkUpFrom##TYPE( \ + const_cast *>(TL.getTypePtr()))); \ } \ { \ CODE; \ @@ -1267,13 +1308,14 @@ DEF_TRAVERSE_TYPE(PredefinedSugarType, {}) if (getDerived().shouldTraversePostOrder()) { \ TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \ if (getDerived().shouldWalkTypesOfTypeLocs()) \ - TRY_TO(WalkUpFrom##TYPE(const_cast(TL.getTypePtr()))); \ + TRY_TO(WalkUpFrom##TYPE( \ + const_cast *>(TL.getTypePtr()))); \ } \ return true; \ } -template -bool RecursiveASTVisitor::TraverseQualifiedTypeLoc( +template +bool RecursiveASTVisitorBase::TraverseQualifiedTypeLoc( QualifiedTypeLoc TL, bool TraverseQualifier) { assert(TraverseQualifier && "Qualifiers should never occur within NestedNameSpecifiers"); @@ -1330,8 +1372,9 @@ DEF_TRAVERSE_TYPELOC(AdjustedType, DEF_TRAVERSE_TYPELOC(DecayedType, { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); }) -template -bool RecursiveASTVisitor::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) { +template +bool RecursiveASTVisitorBase::TraverseArrayTypeLocHelper( + ArrayTypeLoc TL) { // This isn't available for ArrayType, but is for the ArrayTypeLoc. TRY_TO(TraverseStmt(TL.getSizeExpr())); return true; @@ -1425,7 +1468,7 @@ DEF_TRAVERSE_TYPELOC(FunctionProtoType, { TRY_TO(TraverseType(E)); } - if (Expr *NE = T->getNoexceptExpr()) + if (ExprPtr NE = T->getNoexceptExpr()) TRY_TO(TraverseStmt(NE)); }) @@ -1478,16 +1521,16 @@ DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, { TRY_TO(TraverseType(TL.getTypePtr()->getReplacementType())); }) -template -bool RecursiveASTVisitor::TraverseSubstPackTypeLocHelper( +template +bool RecursiveASTVisitorBase::TraverseSubstPackTypeLocHelper( SubstPackTypeLoc TL) { TRY_TO(TraverseTemplateArgument(TL.getTypePtr()->getArgumentPack())); return true; } -template -bool RecursiveASTVisitor::TraverseSubstPackTypeHelper( - SubstPackType *T) { +template +bool RecursiveASTVisitorBase::TraverseSubstPackTypeHelper( + MaybeConst *T) { TRY_TO(TraverseTemplateArgument(T->getArgumentPack())); return true; } @@ -1518,9 +1561,9 @@ DEF_TRAVERSE_TYPELOC(HLSLAttributedResourceType, DEF_TRAVERSE_TYPELOC(HLSLInlineSpirvType, { TRY_TO(TraverseType(TL.getType())); }) -template -bool RecursiveASTVisitor::TraverseTagTypeLoc(TagTypeLoc TL, - bool TraverseQualifier) { +template +bool RecursiveASTVisitorBase::TraverseTagTypeLoc( + TagTypeLoc TL, bool TraverseQualifier) { if (NestedNameSpecifierLoc QualifierLoc = TL.getQualifierLoc(); TraverseQualifier && QualifierLoc) TRY_TO(TraverseNestedNameSpecifierLoc(QualifierLoc)); @@ -1614,9 +1657,9 @@ DEF_TRAVERSE_TYPELOC(PredefinedSugarType, {}) // Therefore each Traverse* only needs to worry about children other // than those. -template -bool RecursiveASTVisitor::canIgnoreChildDeclWhileTraversingDeclContext( - const Decl *Child) { +template +bool RecursiveASTVisitorBase:: + canIgnoreChildDeclWhileTraversingDeclContext(const Decl *Child) { // BlockDecls are traversed through BlockExprs, // CapturedDecls are traversed through CapturedStmts. if (isa(Child) || isa(Child)) @@ -1627,8 +1670,9 @@ bool RecursiveASTVisitor::canIgnoreChildDeclWhileTraversingDeclContext( return false; } -template -bool RecursiveASTVisitor::TraverseDeclContextHelper(DeclContext *DC) { +template +bool RecursiveASTVisitorBase::TraverseDeclContextHelper( + MaybeConst *DC) { if (!DC) return true; @@ -1642,13 +1686,16 @@ bool RecursiveASTVisitor::TraverseDeclContextHelper(DeclContext *DC) { // This macro makes available a variable D, the passed-in decl. #define DEF_TRAVERSE_DECL(DECL, CODE) \ - template \ - bool RecursiveASTVisitor::Traverse##DECL(DECL *D) { \ + template \ + bool RecursiveASTVisitorBase::Traverse##DECL( \ + MaybeConst *D) { \ bool ShouldVisitChildren = true; \ bool ReturnValue = true; \ if (!getDerived().shouldTraversePostOrder()) \ TRY_TO(WalkUpFrom##DECL(D)); \ - { CODE; } \ + { \ + CODE; \ + } \ if (ReturnValue && ShouldVisitChildren) \ TRY_TO(TraverseDeclContextHelper(dyn_cast(D))); \ if (ReturnValue) { \ @@ -1708,7 +1755,7 @@ DEF_TRAVERSE_DECL(FriendDecl, { TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc())); // Traverse any CXXRecordDecl owned by this type, since // it will not be in the parent context: - if (auto *TT = D->getFriendType()->getType()->getAs(); + if (auto *TT = D->getFriendType()->getType()->template getAs(); TT && TT->isTagOwned()) TRY_TO(TraverseDecl(TT->getOriginalDecl())); } else { @@ -1723,8 +1770,7 @@ DEF_TRAVERSE_DECL(FriendTemplateDecl, { TRY_TO(TraverseDecl(D->getFriendDecl())); for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) { TemplateParameterList *TPL = D->getTemplateParameterList(I); - for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end(); - ITPL != ETPL; ++ITPL) { + for (auto ITPL = TPL->begin(), ETPL = TPL->end(); ITPL != ETPL; ++ITPL) { TRY_TO(TraverseDecl(*ITPL)); } } @@ -1837,7 +1883,7 @@ DEF_TRAVERSE_DECL(ObjCMethodDecl, { if (D->getReturnTypeSourceInfo()) { TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc())); } - for (ParmVarDecl *Parameter : D->parameters()) { + for (MaybeConst *Parameter : D->parameters()) { TRY_TO(TraverseDecl(Parameter)); } if (D->isThisDeclarationADefinition()) { @@ -1932,23 +1978,25 @@ DEF_TRAVERSE_DECL(OpenACCRoutineDecl, { }) // A helper method for TemplateDecl's children. -template -bool RecursiveASTVisitor::TraverseTemplateParameterListHelper( - TemplateParameterList *TPL) { +template +bool RecursiveASTVisitorBase:: + TraverseTemplateParameterListHelper( + MaybeConst *TPL) { if (TPL) { - for (NamedDecl *D : *TPL) { + for (MaybeConst *D : *TPL) { TRY_TO(TraverseDecl(D)); } - if (Expr *RequiresClause = TPL->getRequiresClause()) { + if (ExprPtr RequiresClause = TPL->getRequiresClause()) { TRY_TO(TraverseStmt(RequiresClause)); } } return true; } -template +template template -bool RecursiveASTVisitor::TraverseDeclTemplateParameterLists(T *D) { +bool RecursiveASTVisitorBase< + Derived, IsConst>::TraverseDeclTemplateParameterLists(T *D) { for (unsigned i = 0; i < D->getNumTemplateParameterLists(); i++) { TemplateParameterList *TPL = D->getTemplateParameterList(i); TraverseTemplateParameterListHelper(TPL); @@ -1956,9 +2004,9 @@ bool RecursiveASTVisitor::TraverseDeclTemplateParameterLists(T *D) { return true; } -template -bool RecursiveASTVisitor::TraverseTemplateInstantiations( - ClassTemplateDecl *D) { +template +bool RecursiveASTVisitorBase::TraverseTemplateInstantiations( + MaybeConst *D) { for (auto *SD : D->specializations()) { for (auto *RD : SD->redecls()) { assert(!cast(RD)->isInjectedClassName()); @@ -1984,9 +2032,9 @@ bool RecursiveASTVisitor::TraverseTemplateInstantiations( return true; } -template -bool RecursiveASTVisitor::TraverseTemplateInstantiations( - VarTemplateDecl *D) { +template +bool RecursiveASTVisitorBase::TraverseTemplateInstantiations( + MaybeConst *D) { for (auto *SD : D->specializations()) { for (auto *RD : SD->redecls()) { switch ( @@ -2009,9 +2057,9 @@ bool RecursiveASTVisitor::TraverseTemplateInstantiations( // A helper method for traversing the instantiations of a // function while skipping its specializations. -template -bool RecursiveASTVisitor::TraverseTemplateInstantiations( - FunctionTemplateDecl *D) { +template +bool RecursiveASTVisitorBase::TraverseTemplateInstantiations( + MaybeConst *D) { for (auto *FD : D->specializations()) { for (auto *RD : FD->redecls()) { switch (RD->getTemplateSpecializationKind()) { @@ -2076,9 +2124,10 @@ DEF_TRAVERSE_DECL(BuiltinTemplateDecl, { TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); }) -template -bool RecursiveASTVisitor::TraverseTemplateTypeParamDeclConstraints( - const TemplateTypeParmDecl *D) { +template +bool RecursiveASTVisitorBase:: + TraverseTemplateTypeParamDeclConstraints( + const MaybeConst *D) { if (const auto *TC = D->getTypeConstraint()) TRY_TO(TraverseTypeConstraint(TC)); return true; @@ -2139,8 +2188,9 @@ DEF_TRAVERSE_DECL(EnumDecl, { }) // Helper methods for RecordDecl and its children. -template -bool RecursiveASTVisitor::TraverseRecordHelper(RecordDecl *D) { +template +bool RecursiveASTVisitorBase::TraverseRecordHelper( + MaybeConst *D) { // We shouldn't traverse D->getTypeForDecl(); it's a result of // declaring the type, not something that was written in the source. @@ -2149,15 +2199,16 @@ bool RecursiveASTVisitor::TraverseRecordHelper(RecordDecl *D) { return true; } -template -bool RecursiveASTVisitor::TraverseCXXBaseSpecifier( +template +bool RecursiveASTVisitorBase::TraverseCXXBaseSpecifier( const CXXBaseSpecifier &Base) { TRY_TO(TraverseTypeLoc(Base.getTypeSourceInfo()->getTypeLoc())); return true; } -template -bool RecursiveASTVisitor::TraverseCXXRecordHelper(CXXRecordDecl *D) { +template +bool RecursiveASTVisitorBase::TraverseCXXRecordHelper( + MaybeConst *D) { if (!TraverseRecordHelper(D)) return false; if (D->isCompleteDefinition()) { @@ -2174,9 +2225,10 @@ DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); }) DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); }) -template -bool RecursiveASTVisitor::TraverseTemplateArgumentLocsHelper( - const TemplateArgumentLoc *TAL, unsigned Count) { +template +bool RecursiveASTVisitorBase:: + TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL, + unsigned Count) { for (unsigned I = 0; I < Count; ++I) { TRY_TO(TraverseTemplateArgumentLoc(TAL[I])); } @@ -2248,8 +2300,9 @@ DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, { DEF_TRAVERSE_DECL(IndirectFieldDecl, {}) -template -bool RecursiveASTVisitor::TraverseDeclaratorHelper(DeclaratorDecl *D) { +template +bool RecursiveASTVisitorBase::TraverseDeclaratorHelper( + MaybeConst *D) { TRY_TO(TraverseDeclTemplateParameterLists(D)); TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); if (D->getTypeSourceInfo()) @@ -2303,8 +2356,9 @@ DEF_TRAVERSE_DECL(ObjCIvarDecl, { // FIXME: implement the rest. }) -template -bool RecursiveASTVisitor::TraverseFunctionHelper(FunctionDecl *D) { +template +bool RecursiveASTVisitorBase::TraverseFunctionHelper( + MaybeConst *D) { TRY_TO(TraverseDeclTemplateParameterLists(D)); TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo())); @@ -2339,14 +2393,14 @@ bool RecursiveASTVisitor::TraverseFunctionHelper(FunctionDecl *D) { // FunctionNoProtoType or FunctionProtoType, or a typedef. This // also covers the return type and the function parameters, // including exception specifications. - if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) { + if (MaybeConst *TSI = D->getTypeSourceInfo()) { TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); } else if (getDerived().shouldVisitImplicitCode()) { // Visit parameter variable declarations of the implicit function // if the traverser is visiting implicit code. Parameter variable // declarations do not have valid TypeSourceInfo, so to visit them // we need to traverse the declarations explicitly. - for (ParmVarDecl *Parameter : D->parameters()) { + for (MaybeConst *Parameter : D->parameters()) { TRY_TO(TraverseDecl(Parameter)); } } @@ -2355,10 +2409,10 @@ bool RecursiveASTVisitor::TraverseFunctionHelper(FunctionDecl *D) { if (const AssociatedConstraint &TrailingRequiresClause = D->getTrailingRequiresClause()) { TRY_TO(TraverseStmt( - const_cast(TrailingRequiresClause.ConstraintExpr))); + const_cast *>(TrailingRequiresClause.ConstraintExpr))); } - if (CXXConstructorDecl *Ctor = dyn_cast(D)) { + if (MaybeConst *Ctor = dyn_cast(D)) { // Constructor initializers. for (auto *I : Ctor->inits()) { if (I->isWritten() || getDerived().shouldVisitImplicitCode()) @@ -2437,8 +2491,9 @@ DEF_TRAVERSE_DECL(CXXDestructorDecl, { ReturnValue = TraverseFunctionHelper(D); }) -template -bool RecursiveASTVisitor::TraverseVarHelper(VarDecl *D) { +template +bool RecursiveASTVisitorBase::TraverseVarHelper( + MaybeConst *D) { TRY_TO(TraverseDeclaratorHelper(D)); // Default params are taken care of when we traverse the ParmVarDecl. if (!isa(D) && @@ -2489,16 +2544,18 @@ DEF_TRAVERSE_DECL(ImplicitConceptSpecializationDecl, { // This macro makes available a variable S, the passed-in stmt. #define DEF_TRAVERSE_STMT(STMT, CODE) \ - template \ - bool RecursiveASTVisitor::Traverse##STMT( \ - STMT *S, DataRecursionQueue *Queue) { \ + template \ + bool RecursiveASTVisitorBase::Traverse##STMT( \ + MaybeConst *S, DataRecursionQueue *Queue) { \ bool ShouldVisitChildren = true; \ bool ReturnValue = true; \ if (!getDerived().shouldTraversePostOrder()) \ TRY_TO(WalkUpFrom##STMT(S)); \ - { CODE; } \ + { \ + CODE; \ + } \ if (ShouldVisitChildren) { \ - for (Stmt * SubStmt : getDerived().getStmtChildren(S)) { \ + for (auto SubStmt : getDerived().getStmtChildren(S)) { \ TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt); \ } \ } \ @@ -2666,9 +2723,9 @@ DEF_TRAVERSE_STMT(BuiltinBitCastExpr, { TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); }) -template -bool RecursiveASTVisitor::TraverseSynOrSemInitListExpr( - InitListExpr *S, DataRecursionQueue *Queue) { +template +bool RecursiveASTVisitorBase::TraverseSynOrSemInitListExpr( + MaybeConst *S, DataRecursionQueue *Queue) { if (S) { // Skip this if we traverse postorder. We will visit it later // in PostVisitStmt. @@ -2676,7 +2733,7 @@ bool RecursiveASTVisitor::TraverseSynOrSemInitListExpr( TRY_TO(WalkUpFromInitListExpr(S)); // All we need are the default actions. FIXME: use a helper function. - for (Stmt *SubStmt : S->children()) { + for (StmtPtr SubStmt : S->children()) { TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt); } @@ -2686,15 +2743,15 @@ bool RecursiveASTVisitor::TraverseSynOrSemInitListExpr( return true; } -template -bool RecursiveASTVisitor::TraverseObjCProtocolLoc( +template +bool RecursiveASTVisitorBase::TraverseObjCProtocolLoc( ObjCProtocolLoc ProtocolLoc) { return true; } -template -bool RecursiveASTVisitor::TraverseConceptReference( - ConceptReference *CR) { +template +bool RecursiveASTVisitorBase::TraverseConceptReference( + MaybeConst *CR) { if (!getDerived().shouldTraversePostOrder()) TRY_TO(VisitConceptReference(CR)); TRY_TO(TraverseNestedNameSpecifierLoc(CR->getNestedNameSpecifierLoc())); @@ -2716,9 +2773,9 @@ bool RecursiveASTVisitor::TraverseConceptReference( // visited twice, if they appear both in the syntactic and the semantic form. // // There is no guarantee about which form \p S takes when this method is called. -template -bool RecursiveASTVisitor::TraverseInitListExpr( - InitListExpr *S, DataRecursionQueue *Queue) { +template +bool RecursiveASTVisitorBase::TraverseInitListExpr( + MaybeConst *S, DataRecursionQueue *Queue) { if (S->isSemanticForm() && S->isSyntacticForm()) { // `S` does not have alternative forms, traverse only once. TRY_TO(TraverseSynOrSemInitListExpr(S, Queue)); @@ -2744,8 +2801,8 @@ DEF_TRAVERSE_STMT(GenericSelectionExpr, { else TRY_TO(TraverseTypeLoc(S->getControllingType()->getTypeLoc())); - for (const GenericSelectionExpr::Association Assoc : S->associations()) { - if (TypeSourceInfo *TSI = Assoc.getTypeSourceInfo()) + for (auto Assoc : S->associations()) { + if (MaybeConst *TSI = Assoc.getTypeSourceInfo()) TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(Assoc.getAssociationExpr()); } @@ -2756,11 +2813,9 @@ DEF_TRAVERSE_STMT(GenericSelectionExpr, { // syntactic expressions and opaque values. DEF_TRAVERSE_STMT(PseudoObjectExpr, { TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSyntacticForm()); - for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(), - e = S->semantics_end(); - i != e; ++i) { - Expr *sub = *i; - if (OpaqueValueExpr *OVE = dyn_cast(sub)) + for (auto i = S->semantics_begin(), e = S->semantics_end(); i != e; ++i) { + ExprPtr sub = *i; + if (MaybeConst *OVE = dyn_cast(sub)) sub = OVE->getSourceExpr(); TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(sub); } @@ -2864,13 +2919,14 @@ DEF_TRAVERSE_STMT(LambdaExpr, { for (const auto &E : T->exceptions()) TRY_TO(TraverseType(E)); - if (Expr *NE = T->getNoexceptExpr()) + if (ExprPtr NE = T->getNoexceptExpr()) TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(NE); if (S->hasExplicitResultType()) TRY_TO(TraverseTypeLoc(Proto.getReturnLoc())); - TRY_TO_TRAVERSE_OR_ENQUEUE_STMT( - const_cast(S->getTrailingRequiresClause().ConstraintExpr)); + if (auto *ConstraintExpr = S->getTrailingRequiresClause().ConstraintExpr) { + TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(ConstraintExpr); + } TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody()); } @@ -2945,7 +3001,7 @@ DEF_TRAVERSE_STMT(NoInitExpr, {}) DEF_TRAVERSE_STMT(ArrayInitLoopExpr, { // FIXME: The source expression of the OVE should be listed as // a child of the ArrayInitLoopExpr. - if (OpaqueValueExpr *OVE = S->getCommonExpr()) + if (MaybeConst *OVE = S->getCommonExpr()) TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(OVE->getSourceExpr()); }) DEF_TRAVERSE_STMT(ArrayInitIndexExpr, {}) @@ -2966,7 +3022,7 @@ DEF_TRAVERSE_STMT(ObjCMessageExpr, { DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, { if (S->isClassReceiver()) { - ObjCInterfaceDecl *IDecl = S->getClassReceiver(); + MaybeConst *IDecl = S->getClassReceiver(); QualType Type = IDecl->getASTContext().getObjCInterfaceType(IDecl); ObjCInterfaceLocInfo Data; Data.NameLoc = S->getReceiverLocation(); @@ -2996,7 +3052,7 @@ DEF_TRAVERSE_STMT(ConvertVectorExpr, {}) DEF_TRAVERSE_STMT(StmtExpr, {}) DEF_TRAVERSE_STMT(SourceLocExpr, {}) DEF_TRAVERSE_STMT(EmbedExpr, { - for (IntegerLiteral *IL : S->underlying_data_elements()) { + for (MaybeConst *IL : S->underlying_data_elements()) { TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(IL); } }) @@ -3036,8 +3092,8 @@ DEF_TRAVERSE_STMT(CXXRewrittenBinaryOperator, { if (!getDerived().shouldVisitImplicitCode()) { CXXRewrittenBinaryOperator::DecomposedForm Decomposed = S->getDecomposedForm(); - TRY_TO(TraverseStmt(const_cast(Decomposed.LHS))); - TRY_TO(TraverseStmt(const_cast(Decomposed.RHS))); + TRY_TO(TraverseStmt(const_cast *>(Decomposed.LHS))); + TRY_TO(TraverseStmt(const_cast *>(Decomposed.RHS))); ShouldVisitChildren = false; } }) @@ -3110,9 +3166,9 @@ DEF_TRAVERSE_STMT(ConceptSpecializationExpr, { DEF_TRAVERSE_STMT(RequiresExpr, { TRY_TO(TraverseDecl(S->getBody())); - for (ParmVarDecl *Parm : S->getLocalParameters()) + for (MaybeConst *Parm : S->getLocalParameters()) TRY_TO(TraverseDecl(Parm)); - for (concepts::Requirement *Req : S->getRequirements()) + for (MaybeConst *Req : S->getRequirements()) TRY_TO(TraverseConceptRequirement(Req)); }) @@ -3132,11 +3188,11 @@ DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {}) DEF_TRAVERSE_STMT(AsTypeExpr, {}) // OpenMP directives. -template -bool RecursiveASTVisitor::TraverseOMPExecutableDirective( - OMPExecutableDirective *S) { +template +bool RecursiveASTVisitorBase::TraverseOMPExecutableDirective( + MaybeConst *S) { for (auto *C : S->clauses()) { - TRY_TO(TraverseOMPClause(C)); + TRY_TO(TraverseOMPClause(const_cast *>(C))); } return true; } @@ -3149,9 +3205,9 @@ DEF_TRAVERSE_STMT(OMPCanonicalLoop, { } }) -template -bool -RecursiveASTVisitor::TraverseOMPLoopDirective(OMPLoopDirective *S) { +template +bool RecursiveASTVisitorBase::TraverseOMPLoopDirective( + MaybeConst *S) { return TraverseOMPExecutableDirective(S); } @@ -3386,15 +3442,17 @@ DEF_TRAVERSE_STMT(OMPErrorDirective, { TRY_TO(TraverseOMPExecutableDirective(S)); }) // OpenMP clauses. -template -bool RecursiveASTVisitor::TraverseOMPClause(OMPClause *C) { +template +bool RecursiveASTVisitorBase::TraverseOMPClause( + MaybeConst *C) { if (!C) return true; switch (C->getClauseKind()) { #define GEN_CLANG_CLAUSE_CLASS #define CLAUSE_CLASS(Enum, Str, Class) \ case llvm::omp::Clause::Enum: \ - TRY_TO(Visit##Class(static_cast(C))); \ + TRY_TO(Visit##Class( \ + const_cast *>(static_cast(C)))); \ break; #define CLAUSE_NO_CLASS(Enum, Str) \ case llvm::omp::Clause::Enum: \ @@ -3404,371 +3462,415 @@ bool RecursiveASTVisitor::TraverseOMPClause(OMPClause *C) { return true; } -template -bool RecursiveASTVisitor::VisitOMPClauseWithPreInit( - OMPClauseWithPreInit *Node) { +template +bool RecursiveASTVisitorBase::VisitOMPClauseWithPreInit( + MaybeConst *Node) { TRY_TO(TraverseStmt(Node->getPreInitStmt())); return true; } -template -bool RecursiveASTVisitor::VisitOMPClauseWithPostUpdate( - OMPClauseWithPostUpdate *Node) { +template +bool RecursiveASTVisitorBase::VisitOMPClauseWithPostUpdate( + MaybeConst *Node) { TRY_TO(VisitOMPClauseWithPreInit(Node)); TRY_TO(TraverseStmt(Node->getPostUpdateExpr())); return true; } -template -bool RecursiveASTVisitor::VisitOMPAllocatorClause( - OMPAllocatorClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPAllocatorClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getAllocator())); return true; } -template -bool RecursiveASTVisitor::VisitOMPAllocateClause(OMPAllocateClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPAllocateClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getAllocator())); TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPIfClause(OMPIfClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPIfClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getCondition())); return true; } -template -bool RecursiveASTVisitor::VisitOMPFinalClause(OMPFinalClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPFinalClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getCondition())); return true; } -template -bool -RecursiveASTVisitor::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPNumThreadsClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getNumThreads())); return true; } -template -bool RecursiveASTVisitor::VisitOMPAlignClause(OMPAlignClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPAlignClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getAlignment())); return true; } -template -bool RecursiveASTVisitor::VisitOMPSafelenClause(OMPSafelenClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPSafelenClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getSafelen())); return true; } -template -bool RecursiveASTVisitor::VisitOMPSimdlenClause(OMPSimdlenClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPSimdlenClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getSimdlen())); return true; } -template -bool RecursiveASTVisitor::VisitOMPSizesClause(OMPSizesClause *C) { - for (Expr *E : C->getSizesRefs()) +template +bool RecursiveASTVisitorBase::VisitOMPSizesClause( + MaybeConst *C) { + for (MaybeConst *E : C->getSizesRefs()) TRY_TO(TraverseStmt(E)); return true; } -template -bool RecursiveASTVisitor::VisitOMPPermutationClause( - OMPPermutationClause *C) { - for (Expr *E : C->getArgsRefs()) +template +bool RecursiveASTVisitorBase::VisitOMPPermutationClause( + MaybeConst *C) { + for (MaybeConst *E : C->getArgsRefs()) TRY_TO(TraverseStmt(E)); return true; } -template -bool RecursiveASTVisitor::VisitOMPFullClause(OMPFullClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPFullClause( + MaybeConst *C) { return true; } -template -bool RecursiveASTVisitor::VisitOMPPartialClause(OMPPartialClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPPartialClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getFactor())); return true; } -template -bool -RecursiveASTVisitor::VisitOMPCollapseClause(OMPCollapseClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPCollapseClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getNumForLoops())); return true; } -template -bool RecursiveASTVisitor::VisitOMPDefaultClause(OMPDefaultClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPDefaultClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPProcBindClause(OMPProcBindClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPProcBindClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPUnifiedAddressClause( - OMPUnifiedAddressClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPUnifiedAddressClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPUnifiedSharedMemoryClause( - OMPUnifiedSharedMemoryClause *) { +template +bool RecursiveASTVisitorBase:: + VisitOMPUnifiedSharedMemoryClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPReverseOffloadClause( - OMPReverseOffloadClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPReverseOffloadClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPDynamicAllocatorsClause( - OMPDynamicAllocatorsClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPDynamicAllocatorsClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPAtomicDefaultMemOrderClause( - OMPAtomicDefaultMemOrderClause *) { +template +bool RecursiveASTVisitorBase:: + VisitOMPAtomicDefaultMemOrderClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPSelfMapsClause(OMPSelfMapsClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPSelfMapsClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPAtClause(OMPAtClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPAtClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPSeverityClause(OMPSeverityClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPSeverityClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPMessageClause(OMPMessageClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPMessageClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getMessageString())); return true; } -template -bool -RecursiveASTVisitor::VisitOMPScheduleClause(OMPScheduleClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPScheduleClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getChunkSize())); return true; } -template -bool RecursiveASTVisitor::VisitOMPOrderedClause(OMPOrderedClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPOrderedClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getNumForLoops())); return true; } -template -bool RecursiveASTVisitor::VisitOMPNowaitClause(OMPNowaitClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPNowaitClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPUntiedClause(OMPUntiedClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPUntiedClause( + MaybeConst *) { return true; } -template -bool -RecursiveASTVisitor::VisitOMPMergeableClause(OMPMergeableClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPMergeableClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPReadClause(OMPReadClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPReadClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPWriteClause(OMPWriteClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPWriteClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPUpdateClause(OMPUpdateClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPUpdateClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPCaptureClause(OMPCaptureClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPCaptureClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPCompareClause(OMPCompareClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPCompareClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPFailClause(OMPFailClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPFailClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPSeqCstClause(OMPSeqCstClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPSeqCstClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPAcqRelClause(OMPAcqRelClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPAcqRelClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPAbsentClause(OMPAbsentClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPAbsentClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPHoldsClause(OMPHoldsClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPHoldsClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPContainsClause(OMPContainsClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPContainsClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPNoOpenMPClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPNoOpenMPRoutinesClause( - OMPNoOpenMPRoutinesClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPNoOpenMPRoutinesClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPNoOpenMPConstructsClause( - OMPNoOpenMPConstructsClause *) { +template +bool RecursiveASTVisitorBase:: + VisitOMPNoOpenMPConstructsClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPNoParallelismClause( - OMPNoParallelismClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPNoParallelismClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPAcquireClause(OMPAcquireClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPAcquireClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPReleaseClause(OMPReleaseClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPReleaseClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPRelaxedClause(OMPRelaxedClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPRelaxedClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPWeakClause(OMPWeakClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPWeakClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPThreadsClause(OMPThreadsClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPThreadsClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPSIMDClause(OMPSIMDClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPSIMDClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPNogroupClause(OMPNogroupClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPNogroupClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPInitClause(OMPInitClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPInitClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPUseClause(OMPUseClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPUseClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getInteropVar())); return true; } -template -bool RecursiveASTVisitor::VisitOMPDestroyClause(OMPDestroyClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPDestroyClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getInteropVar())); return true; } -template -bool RecursiveASTVisitor::VisitOMPNovariantsClause( - OMPNovariantsClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPNovariantsClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getCondition())); return true; } -template -bool RecursiveASTVisitor::VisitOMPNocontextClause( - OMPNocontextClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPNocontextClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getCondition())); return true; } -template +template template -bool RecursiveASTVisitor::VisitOMPClauseList(T *Node) { +bool RecursiveASTVisitorBase::VisitOMPClauseList(T *Node) { for (auto *E : Node->varlist()) { TRY_TO(TraverseStmt(E)); } return true; } -template -bool RecursiveASTVisitor::VisitOMPInclusiveClause( - OMPInclusiveClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPInclusiveClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPExclusiveClause( - OMPExclusiveClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPExclusiveClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPPrivateClause(OMPPrivateClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPPrivateClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); for (auto *E : C->private_copies()) { TRY_TO(TraverseStmt(E)); @@ -3776,9 +3878,9 @@ bool RecursiveASTVisitor::VisitOMPPrivateClause(OMPPrivateClause *C) { return true; } -template -bool RecursiveASTVisitor::VisitOMPFirstprivateClause( - OMPFirstprivateClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPFirstprivateClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); TRY_TO(VisitOMPClauseWithPreInit(C)); for (auto *E : C->private_copies()) { @@ -3790,9 +3892,9 @@ bool RecursiveASTVisitor::VisitOMPFirstprivateClause( return true; } -template -bool RecursiveASTVisitor::VisitOMPLastprivateClause( - OMPLastprivateClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPLastprivateClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); TRY_TO(VisitOMPClauseWithPostUpdate(C)); for (auto *E : C->private_copies()) { @@ -3810,14 +3912,16 @@ bool RecursiveASTVisitor::VisitOMPLastprivateClause( return true; } -template -bool RecursiveASTVisitor::VisitOMPSharedClause(OMPSharedClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPSharedClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPLinearClause(OMPLinearClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPLinearClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getStep())); TRY_TO(TraverseStmt(C->getCalcStep())); TRY_TO(VisitOMPClauseList(C)); @@ -3837,15 +3941,17 @@ bool RecursiveASTVisitor::VisitOMPLinearClause(OMPLinearClause *C) { return true; } -template -bool RecursiveASTVisitor::VisitOMPAlignedClause(OMPAlignedClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPAlignedClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getAlignment())); TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPCopyinClause(OMPCopyinClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPCopyinClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); for (auto *E : C->source_exprs()) { TRY_TO(TraverseStmt(E)); @@ -3859,9 +3965,9 @@ bool RecursiveASTVisitor::VisitOMPCopyinClause(OMPCopyinClause *C) { return true; } -template -bool RecursiveASTVisitor::VisitOMPCopyprivateClause( - OMPCopyprivateClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPCopyprivateClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); for (auto *E : C->source_exprs()) { TRY_TO(TraverseStmt(E)); @@ -3875,9 +3981,9 @@ bool RecursiveASTVisitor::VisitOMPCopyprivateClause( return true; } -template -bool -RecursiveASTVisitor::VisitOMPReductionClause(OMPReductionClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPReductionClause( + MaybeConst *C) { TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc())); TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo())); TRY_TO(VisitOMPClauseList(C)); @@ -3908,9 +4014,9 @@ RecursiveASTVisitor::VisitOMPReductionClause(OMPReductionClause *C) { return true; } -template -bool RecursiveASTVisitor::VisitOMPTaskReductionClause( - OMPTaskReductionClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPTaskReductionClause( + MaybeConst *C) { TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc())); TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo())); TRY_TO(VisitOMPClauseList(C)); @@ -3930,9 +4036,9 @@ bool RecursiveASTVisitor::VisitOMPTaskReductionClause( return true; } -template -bool RecursiveASTVisitor::VisitOMPInReductionClause( - OMPInReductionClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPInReductionClause( + MaybeConst *C) { TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc())); TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo())); TRY_TO(VisitOMPClauseList(C)); @@ -3954,140 +4060,148 @@ bool RecursiveASTVisitor::VisitOMPInReductionClause( return true; } -template -bool RecursiveASTVisitor::VisitOMPFlushClause(OMPFlushClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPFlushClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPDepobjClause(OMPDepobjClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPDepobjClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getDepobj())); return true; } -template -bool RecursiveASTVisitor::VisitOMPDependClause(OMPDependClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPDependClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPDeviceClause(OMPDeviceClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPDeviceClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getDevice())); return true; } -template -bool RecursiveASTVisitor::VisitOMPMapClause(OMPMapClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPMapClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPNumTeamsClause( - OMPNumTeamsClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPNumTeamsClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); TRY_TO(VisitOMPClauseWithPreInit(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPThreadLimitClause( - OMPThreadLimitClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPThreadLimitClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); TRY_TO(VisitOMPClauseWithPreInit(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPPriorityClause( - OMPPriorityClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPPriorityClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getPriority())); return true; } -template -bool RecursiveASTVisitor::VisitOMPGrainsizeClause( - OMPGrainsizeClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPGrainsizeClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getGrainsize())); return true; } -template -bool RecursiveASTVisitor::VisitOMPNumTasksClause( - OMPNumTasksClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPNumTasksClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getNumTasks())); return true; } -template -bool RecursiveASTVisitor::VisitOMPHintClause(OMPHintClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPHintClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getHint())); return true; } -template -bool RecursiveASTVisitor::VisitOMPDistScheduleClause( - OMPDistScheduleClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPDistScheduleClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getChunkSize())); return true; } -template -bool -RecursiveASTVisitor::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPDefaultmapClause( + MaybeConst *C) { return true; } -template -bool RecursiveASTVisitor::VisitOMPToClause(OMPToClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPToClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPFromClause(OMPFromClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPFromClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPUseDevicePtrClause( - OMPUseDevicePtrClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPUseDevicePtrClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPUseDeviceAddrClause( - OMPUseDeviceAddrClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPUseDeviceAddrClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPIsDevicePtrClause( - OMPIsDevicePtrClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPIsDevicePtrClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPHasDeviceAddrClause( - OMPHasDeviceAddrClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPHasDeviceAddrClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPNontemporalClause( - OMPNontemporalClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPNontemporalClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); for (auto *E : C->private_refs()) { TRY_TO(TraverseStmt(E)); @@ -4095,20 +4209,22 @@ bool RecursiveASTVisitor::VisitOMPNontemporalClause( return true; } -template -bool RecursiveASTVisitor::VisitOMPOrderClause(OMPOrderClause *) { +template +bool RecursiveASTVisitorBase::VisitOMPOrderClause( + MaybeConst *) { return true; } -template -bool RecursiveASTVisitor::VisitOMPDetachClause(OMPDetachClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPDetachClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getEventHandler())); return true; } -template -bool RecursiveASTVisitor::VisitOMPUsesAllocatorsClause( - OMPUsesAllocatorsClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPUsesAllocatorsClause( + MaybeConst *C) { for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) { const OMPUsesAllocatorsClause::Data Data = C->getAllocatorData(I); TRY_TO(TraverseStmt(Data.Allocator)); @@ -4117,77 +4233,83 @@ bool RecursiveASTVisitor::VisitOMPUsesAllocatorsClause( return true; } -template -bool RecursiveASTVisitor::VisitOMPAffinityClause( - OMPAffinityClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPAffinityClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getModifier())); - for (Expr *E : C->varlist()) + for (ExprPtr E : C->varlist()) TRY_TO(TraverseStmt(E)); return true; } -template -bool RecursiveASTVisitor::VisitOMPFilterClause(OMPFilterClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPFilterClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getThreadID())); return true; } -template -bool RecursiveASTVisitor::VisitOMPBindClause(OMPBindClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPBindClause( + MaybeConst *C) { return true; } -template -bool RecursiveASTVisitor::VisitOMPXDynCGroupMemClause( - OMPXDynCGroupMemClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPXDynCGroupMemClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getSize())); return true; } -template -bool RecursiveASTVisitor::VisitOMPDoacrossClause( - OMPDoacrossClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPDoacrossClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } -template -bool RecursiveASTVisitor::VisitOMPXAttributeClause( - OMPXAttributeClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPXAttributeClause( + MaybeConst *C) { return true; } -template -bool RecursiveASTVisitor::VisitOMPXBareClause(OMPXBareClause *C) { +template +bool RecursiveASTVisitorBase::VisitOMPXBareClause( + MaybeConst *C) { return true; } -template -bool RecursiveASTVisitor::TraverseOpenACCConstructStmt( - OpenACCConstructStmt *C) { +template +bool RecursiveASTVisitorBase::TraverseOpenACCConstructStmt( + MaybeConst *C) { TRY_TO(VisitOpenACCClauseList(C->clauses())); return true; } -template -bool RecursiveASTVisitor::TraverseOpenACCAssociatedStmtConstruct( - OpenACCAssociatedStmtConstruct *S) { +template +bool RecursiveASTVisitorBase:: + TraverseOpenACCAssociatedStmtConstruct( + MaybeConst *S) { TRY_TO(TraverseOpenACCConstructStmt(S)); - TRY_TO(TraverseStmt(S->getAssociatedStmt())); + // TODO: Need to access getAssociatedStmt() which is currently protected + // TRY_TO(TraverseStmt(S->getAssociatedStmt())); return true; } -template -bool RecursiveASTVisitor::VisitOpenACCClause(const OpenACCClause *C) { +template +bool RecursiveASTVisitorBase::VisitOpenACCClause( + const OpenACCClause *C) { for (const Stmt *Child : C->children()) - TRY_TO(TraverseStmt(const_cast(Child))); + TRY_TO(TraverseStmt(const_cast *>(Child))); return true; } -template -bool RecursiveASTVisitor::VisitOpenACCClauseList( +template +bool RecursiveASTVisitorBase::VisitOpenACCClauseList( ArrayRef Clauses) { for (const auto *C : Clauses) @@ -4257,6 +4379,16 @@ DEF_TRAVERSE_STMT(HLSLOutArgExpr, {}) #undef TRY_TO +// Backward compatibility - keep the original class name structure +template +class RecursiveASTVisitor + : public RecursiveASTVisitorBase {}; + +// New const-aware visitor interface +template +class ConstRecursiveASTVisitor + : public RecursiveASTVisitorBase {}; + } // end namespace clang #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H diff --git a/clang/include/clang/AST/StmtOpenACC.h b/clang/include/clang/AST/StmtOpenACC.h index 8b4554e996326..a6b6d9d802c2a 100644 --- a/clang/include/clang/AST/StmtOpenACC.h +++ b/clang/include/clang/AST/StmtOpenACC.h @@ -81,7 +81,6 @@ class OpenACCConstructStmt : public Stmt { class OpenACCAssociatedStmtConstruct : public OpenACCConstructStmt { friend class ASTStmtWriter; friend class ASTStmtReader; - template friend class RecursiveASTVisitor; Stmt *AssociatedStmt = nullptr; protected: diff --git a/clang/lib/AST/ParentMapContext.cpp b/clang/lib/AST/ParentMapContext.cpp index acc011cb2faa4..506e4dd9d00d6 100644 --- a/clang/lib/AST/ParentMapContext.cpp +++ b/clang/lib/AST/ParentMapContext.cpp @@ -363,7 +363,7 @@ class ParentMapContext::ParentMap::ASTVisitor ASTVisitor(ParentMap &Map) : Map(Map) {} private: - friend class RecursiveASTVisitor; + friend class RecursiveASTVisitorBase; using VisitorBase = RecursiveASTVisitor; diff --git a/clang/lib/Index/IndexBody.cpp b/clang/lib/Index/IndexBody.cpp index 1979117d4695c..887b56968e47d 100644 --- a/clang/lib/Index/IndexBody.cpp +++ b/clang/lib/Index/IndexBody.cpp @@ -508,7 +508,7 @@ class BodyIndexer : public RecursiveASTVisitor { bool TraverseTypeConstraint(const TypeConstraint *C) { IndexCtx.handleReference(C->getNamedConcept(), C->getConceptNameLoc(), Parent, ParentDC); - return RecursiveASTVisitor::TraverseTypeConstraint(C); + return base::TraverseTypeConstraint(C); } }; diff --git a/clang/unittests/AST/RecursiveASTVisitorTest.cpp b/clang/unittests/AST/RecursiveASTVisitorTest.cpp index c5ad29a77d211..f2111a9551213 100644 --- a/clang/unittests/AST/RecursiveASTVisitorTest.cpp +++ b/clang/unittests/AST/RecursiveASTVisitorTest.cpp @@ -143,6 +143,82 @@ std::vector collectEvents(llvm::StringRef Code, Code, FileName); return std::move(Visitor).takeEvents(); } +class ConstCollectInterestingEvents + : public ConstRecursiveASTVisitor { +public: + bool TraverseFunctionDecl(const FunctionDecl *D) { + Events.push_back(VisitEvent::StartTraverseFunction); + bool Ret = ConstRecursiveASTVisitor::TraverseFunctionDecl(D); + Events.push_back(VisitEvent::EndTraverseFunction); + + return Ret; + } + + bool TraverseAttr(const Attr *A) { + Events.push_back(VisitEvent::StartTraverseAttr); + bool Ret = ConstRecursiveASTVisitor::TraverseAttr(A); + Events.push_back(VisitEvent::EndTraverseAttr); + + return Ret; + } + + bool TraverseEnumDecl(const EnumDecl *D) { + Events.push_back(VisitEvent::StartTraverseEnum); + bool Ret = ConstRecursiveASTVisitor::TraverseEnumDecl(D); + Events.push_back(VisitEvent::EndTraverseEnum); + + return Ret; + } + + bool TraverseTypedefTypeLoc(TypedefTypeLoc TL, bool TraverseQualifier) { + Events.push_back(VisitEvent::StartTraverseTypedefType); + bool Ret = + ConstRecursiveASTVisitor::TraverseTypedefTypeLoc(TL, TraverseQualifier); + Events.push_back(VisitEvent::EndTraverseTypedefType); + + return Ret; + } + + bool TraverseObjCInterfaceDecl(const ObjCInterfaceDecl *ID) { + Events.push_back(VisitEvent::StartTraverseObjCInterface); + bool Ret = ConstRecursiveASTVisitor::TraverseObjCInterfaceDecl(ID); + Events.push_back(VisitEvent::EndTraverseObjCInterface); + + return Ret; + } + + bool TraverseObjCProtocolDecl(const ObjCProtocolDecl *PD) { + Events.push_back(VisitEvent::StartTraverseObjCProtocol); + bool Ret = ConstRecursiveASTVisitor::TraverseObjCProtocolDecl(PD); + Events.push_back(VisitEvent::EndTraverseObjCProtocol); + + return Ret; + } + + bool TraverseObjCProtocolLoc(ObjCProtocolLoc ProtocolLoc) { + Events.push_back(VisitEvent::StartTraverseObjCProtocolLoc); + bool Ret = ConstRecursiveASTVisitor::TraverseObjCProtocolLoc(ProtocolLoc); + Events.push_back(VisitEvent::EndTraverseObjCProtocolLoc); + + return Ret; + } + + std::vector takeEvents() && { return std::move(Events); } + +private: + std::vector Events; +}; + +std::vector collectConstEvents(llvm::StringRef Code, + const Twine &FileName = "input.cc") { + ConstCollectInterestingEvents Visitor; + clang::tooling::runToolOnCode( + std::make_unique( + [&](const clang::ASTContext &Ctx) { Visitor.TraverseAST(Ctx); }), + Code, FileName); + return std::move(Visitor).takeEvents(); +} + } // namespace TEST(RecursiveASTVisitorTest, AttributesInsideDecls) { @@ -151,6 +227,7 @@ TEST(RecursiveASTVisitorTest, AttributesInsideDecls) { __attribute__((annotate("something"))) int foo() { return 10; } )cpp"; + EXPECT_EQ(collectEvents(Code), collectConstEvents(Code)); EXPECT_THAT(collectEvents(Code), ElementsAre(VisitEvent::StartTraverseFunction, VisitEvent::StartTraverseAttr, @@ -165,6 +242,7 @@ TEST(RecursiveASTVisitorTest, EnumDeclWithBase) { enum Bar : Foo; )cpp"; + EXPECT_EQ(collectEvents(Code), collectConstEvents(Code)); EXPECT_THAT(collectEvents(Code), ElementsAre(VisitEvent::StartTraverseEnum, VisitEvent::StartTraverseTypedefType, @@ -184,6 +262,7 @@ TEST(RecursiveASTVisitorTest, InterfaceDeclWithProtocols) { @end )cpp"; + EXPECT_EQ(collectEvents(Code), collectConstEvents(Code)); EXPECT_THAT(collectEvents(Code, "input.m"), ElementsAre(VisitEvent::StartTraverseObjCProtocol, VisitEvent::EndTraverseObjCProtocol, @@ -196,3 +275,53 @@ TEST(RecursiveASTVisitorTest, InterfaceDeclWithProtocols) { VisitEvent::EndTraverseObjCProtocolLoc, VisitEvent::EndTraverseObjCInterface)); } + +TEST(ConstRecursiveASTVisitorTest, ConstCorrectness) { + // This test verifies that ConstRecursiveASTVisitor properly enforces + // const-correctness. + // The derived class defines const versions of the Visit* methods, + // and they should correctly override the default implementations, + // which is demonstrated by non-0 counters. + + class ConstCorrectnessValidator + : public ConstRecursiveASTVisitor { + public: + bool VisitFunctionDecl(const FunctionDecl *D) { + FunctionDeclCount++; + return true; + } + + bool VisitStmt(const Stmt *S) { + StmtCount++; + return true; + } + + int getFunctionDeclCount() const { return FunctionDeclCount; } + int getStmtCount() const { return StmtCount; } + + private: + int FunctionDeclCount = 0; + int StmtCount = 0; + }; + + llvm::StringRef Code = R"cpp( + int foo() { + return 42; + } + void bar() { + int x = 0; + x += 2; + } + )cpp"; + + ConstCorrectnessValidator Visitor; + clang::tooling::runToolOnCode( + std::make_unique( + [&](clang::ASTContext &Ctx) { Visitor.TraverseAST(Ctx); }), + Code); + + // Verify that the visitor found the expected number of nodes + EXPECT_EQ(Visitor.getFunctionDeclCount(), 2); // foo and bar + // There are at least 3 statements: return 42; int x = 0; x += 2; + EXPECT_GE(Visitor.getStmtCount(), 3); +} diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp b/clang/utils/TableGen/ClangAttrEmitter.cpp index 1342e1a6ffb5b..fef20abec00f1 100644 --- a/clang/utils/TableGen/ClangAttrEmitter.cpp +++ b/clang/utils/TableGen/ClangAttrEmitter.cpp @@ -3983,10 +3983,10 @@ void EmitClangAttrASTVisitor(const RecordKeeper &Records, raw_ostream &OS) { const Record &R = *Attr; if (!R.getValueAsBit("ASTNode")) continue; - OS << " bool Traverse" - << R.getName() << "Attr(" << R.getName() << "Attr *A);\n"; - OS << " bool Visit" - << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n" + OS << " bool Traverse" << R.getName() << "Attr(MaybeConst<" << R.getName() + << "Attr> *A);\n"; + OS << " bool Visit" << R.getName() << "Attr(MaybeConst<" << R.getName() + << "Attr> *A) {\n" << " return true; \n" << " }\n"; } @@ -3998,9 +3998,9 @@ void EmitClangAttrASTVisitor(const RecordKeeper &Records, raw_ostream &OS) { if (!R.getValueAsBit("ASTNode")) continue; - OS << "template \n" - << "bool VISITORCLASS::Traverse" - << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n" + OS << "template \n" + << "bool VISITORCLASS::Traverse" << R.getName() + << "Attr(MaybeConst<" << R.getName() << "Attr> *A) {\n" << " if (!getDerived().VisitAttr(A))\n" << " return false;\n" << " if (!getDerived().Visit" << R.getName() << "Attr(A))\n" @@ -4018,8 +4018,9 @@ void EmitClangAttrASTVisitor(const RecordKeeper &Records, raw_ostream &OS) { } // Write generic Traverse routine - OS << "template \n" - << "bool VISITORCLASS::TraverseAttr(Attr *A) {\n" + OS << "template \n" + << "bool VISITORCLASS::TraverseAttr(" + << "MaybeConst *A) {\n" << " if (!A)\n" << " return true;\n" << "\n" @@ -4032,7 +4033,8 @@ void EmitClangAttrASTVisitor(const RecordKeeper &Records, raw_ostream &OS) { OS << " case attr::" << R.getName() << ":\n" << " return getDerived().Traverse" << R.getName() << "Attr(" - << "cast<" << R.getName() << "Attr>(A));\n"; + << "const_cast *>(static_cast(A)));\n"; } OS << " }\n"; // end switch OS << " llvm_unreachable(\"bad attribute kind\");\n";