From a22916b437e8be763aab75b1f1dcd96a6f1ef96a Mon Sep 17 00:00:00 2001 From: Arseniy Zaostrovnykh Date: Fri, 19 Sep 2025 09:59:19 +0200 Subject: [PATCH 1/3] [clang][nfc] Define ConstRecursiveASTVisitor twin of RecursiveASTVisitor Downstream whenever we reach out for a RecursiveASTVisitor we always have to add a few const_casts to shoe it in. This NFC patch introduces a const version of the same CRTP class. To reduce code duplication, I factored out all the common logic (which is all of it) into `RecursiveASTVisitorBase` and made `RecursiveASTVisitor` and `ConstRecursiveASTVisitor` essentially the two instances of it, that you should use depending on whether you want to modify AST in your visitor. This is very similar to the DynamicRecursiveASTVisitor structure. One point of difference is that instead of type aliases I use inheritance to reduce the diff of this change because templated alias is not accepted in the implementation forwarding of the overridden member functions: `return RecursiveASTVisitor::TraverseStmt(S);` works only if `RecursiveASTVisitor` is defined as a derived class of `RecursiveASTVisitorBase` and not as a parametric alias. This was not an issue for DynamicRecursiveASTVisitor because it is not parametrised bythe `Derived` type. Unfortunately, I did not manager to maintain a full backwards compatibility when it comes to the `friend` declarations, you have to befriend the `RecursiveASTVisitorBase` and not `RecursiveASTVisitor`. Moreover, the error message is not obvious, as it speaks of the member function being private and does not point to the `friend` declaration. --- .../clang-tidy/modernize/LoopConvertUtils.h | 10 +- .../clang-tidy/modernize/PassByValueCheck.cpp | 2 +- clang/include/clang/AST/RecursiveASTVisitor.h | 1020 +++++++++-------- clang/include/clang/AST/StmtOpenACC.h | 1 - clang/lib/AST/ParentMapContext.cpp | 2 +- clang/lib/Index/IndexBody.cpp | 2 +- .../unittests/AST/RecursiveASTVisitorTest.cpp | 129 +++ clang/utils/TableGen/ClangAttrEmitter.cpp | 22 +- 8 files changed, 677 insertions(+), 511 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h index 306eca7140d1a..0c76678c5c81a 100644 --- a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h +++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h @@ -72,7 +72,7 @@ class StmtAncestorASTVisitor /// Accessor for DeclParents. const DeclParentMap &getDeclToParentStmtMap() { return DeclParents; } - friend class clang::RecursiveASTVisitor; + friend class clang::RecursiveASTVisitorBase; private: StmtParentMap StmtAncestors; @@ -98,7 +98,7 @@ class ComponentFinderASTVisitor /// Accessor for Components. const ComponentVector &getComponents() { return Components; } - friend class clang::RecursiveASTVisitor; + friend class clang::RecursiveASTVisitorBase; private: ComponentVector Components; @@ -155,7 +155,7 @@ class DependencyFinderASTVisitor return DependsOnInsideVariable; } - friend class clang::RecursiveASTVisitor; + friend class clang::RecursiveASTVisitorBase; private: const StmtParentMap *StmtParents; @@ -188,7 +188,7 @@ class DeclFinderASTVisitor return Found; } - friend class clang::RecursiveASTVisitor; + friend class clang::RecursiveASTVisitorBase; private: std::string Name; @@ -340,7 +340,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..f823a7019259d 100644 --- a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp @@ -95,7 +95,7 @@ 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/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index 1d1b7f183f75a..5aa2ebf7bc8f2 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -55,6 +55,7 @@ namespace clang { + // A helper macro to implement short-circuiting when recursing. It // invokes CALL_EXPR, which must be a method call, on the derived // object (s.t. a user of RecursiveASTVisitor can override the method @@ -153,15 +154,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 +201,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 +212,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 +245,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 +309,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 +334,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 +350,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 +363,15 @@ 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 @@ -368,20 +386,20 @@ template class RecursiveASTVisitor { // Declare Traverse*() for all concrete Stmt classes. #define ABSTRACT_STMT(STMT) #define STMT(CLASS, PARENT) \ - bool Traverse##CLASS(CLASS *S, DataRecursionQueue *Queue = nullptr); + 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 +408,20 @@ 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 +463,26 @@ 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 +490,65 @@ 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 PostVisitStmt(StmtPtr S); + bool TraverseOpenACCConstructStmt(MaybeConst *S); bool - TraverseOpenACCAssociatedStmtConstruct(OpenACCAssociatedStmtConstruct *S); + 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,26 +560,26 @@ 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, +template +bool RecursiveASTVisitorBase::dataTraverseNode(StmtPtr S, DataRecursionQueue *Queue) { // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt. switch (S->getStmtClass()) { @@ -578,17 +597,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 +623,16 @@ 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 +655,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 +685,9 @@ 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 +696,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,8 +726,8 @@ RecursiveASTVisitor::TraverseStmt(Stmt *S, DataRecursionQueue *Queue) { return true; } -template -bool RecursiveASTVisitor::TraverseType(QualType T, +template +bool RecursiveASTVisitorBase::TraverseType(QualType T, bool TraverseQualifier) { if (T.isNull()) return true; @@ -714,7 +737,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,8 +746,8 @@ bool RecursiveASTVisitor::TraverseType(QualType T, return true; } -template -bool RecursiveASTVisitor::TraverseTypeLoc(TypeLoc TL, +template +bool RecursiveASTVisitorBase::TraverseTypeLoc(TypeLoc TL, bool TraverseQualifier) { if (TL.isNull()) return true; @@ -741,12 +765,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 +803,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 +813,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 +825,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 +834,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 +857,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 +886,8 @@ 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 +900,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 +931,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 +970,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 +979,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 +991,11 @@ bool RecursiveASTVisitor::TraverseConstructorInitializer( return true; } -template +template bool -RecursiveASTVisitor::TraverseLambdaCapture(LambdaExpr *LE, +RecursiveASTVisitorBase::TraverseLambdaCapture(MaybeConst *LE, const LambdaCapture *C, - Expr *Init) { + MaybeConst *Init) { if (LE->isInitCapture(C)) TRY_TO(TraverseDecl(C->getCapturedVar())); else @@ -981,8 +1007,8 @@ 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, \ + template \ + bool RecursiveASTVisitorBase::Traverse##TYPE(MaybeConst *T, \ bool TraverseQualifier) { \ if (!getDerived().shouldTraversePostOrder()) \ TRY_TO(WalkUpFrom##TYPE(T)); \ @@ -1025,13 +1051,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 +1120,7 @@ DEF_TRAVERSE_TYPE(FunctionProtoType, { TRY_TO(TraverseType(E)); } - if (Expr *NE = T->getNoexceptExpr()) + if (ExprPtr NE = T->getNoexceptExpr()) TRY_TO(TraverseStmt(NE)); }) @@ -1173,8 +1199,8 @@ DEF_TRAVERSE_TYPE(ParenType, { TRY_TO(TraverseType(T->getInnerType())); }) DEF_TRAVERSE_TYPE(MacroQualifiedType, { TRY_TO(TraverseType(T->getUnderlyingType())); }) -template -bool RecursiveASTVisitor::TraverseTagType(TagType *T, +template +bool RecursiveASTVisitorBase::TraverseTagType(MaybeConst *T, bool TraverseQualifier) { if (TraverseQualifier) TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); @@ -1253,13 +1279,13 @@ 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 +1293,13 @@ 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 +1356,8 @@ 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 +1451,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 +1504,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,8 +1544,8 @@ DEF_TRAVERSE_TYPELOC(HLSLAttributedResourceType, DEF_TRAVERSE_TYPELOC(HLSLInlineSpirvType, { TRY_TO(TraverseType(TL.getType())); }) -template -bool RecursiveASTVisitor::TraverseTagTypeLoc(TagTypeLoc TL, +template +bool RecursiveASTVisitorBase::TraverseTagTypeLoc(TagTypeLoc TL, bool TraverseQualifier) { if (NestedNameSpecifierLoc QualifierLoc = TL.getQualifierLoc(); TraverseQualifier && QualifierLoc) @@ -1614,8 +1640,8 @@ DEF_TRAVERSE_TYPELOC(PredefinedSugarType, {}) // Therefore each Traverse* only needs to worry about children other // than those. -template -bool RecursiveASTVisitor::canIgnoreChildDeclWhileTraversingDeclContext( +template +bool RecursiveASTVisitorBase::canIgnoreChildDeclWhileTraversingDeclContext( const Decl *Child) { // BlockDecls are traversed through BlockExprs, // CapturedDecls are traversed through CapturedStmts. @@ -1627,8 +1653,8 @@ bool RecursiveASTVisitor::canIgnoreChildDeclWhileTraversingDeclContext( return false; } -template -bool RecursiveASTVisitor::TraverseDeclContextHelper(DeclContext *DC) { +template +bool RecursiveASTVisitorBase::TraverseDeclContextHelper(MaybeConst *DC) { if (!DC) return true; @@ -1642,8 +1668,8 @@ 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()) \ @@ -1708,7 +1734,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 +1749,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 +1862,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 +1957,23 @@ 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::TraverseDeclTemplateParameterLists(T *D) { for (unsigned i = 0; i < D->getNumTemplateParameterLists(); i++) { TemplateParameterList *TPL = D->getTemplateParameterList(i); TraverseTemplateParameterListHelper(TPL); @@ -1956,9 +1981,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 +2009,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 +2034,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 +2101,9 @@ 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 +2164,8 @@ 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 +2174,15 @@ 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,8 +2199,8 @@ DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); }) DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); }) -template -bool RecursiveASTVisitor::TraverseTemplateArgumentLocsHelper( +template +bool RecursiveASTVisitorBase::TraverseTemplateArgumentLocsHelper( const TemplateArgumentLoc *TAL, unsigned Count) { for (unsigned I = 0; I < Count; ++I) { TRY_TO(TraverseTemplateArgumentLoc(TAL[I])); @@ -2248,8 +2273,8 @@ 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 +2328,8 @@ 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 +2364,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 +2380,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 +2462,8 @@ 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 +2514,16 @@ 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; } \ if (ShouldVisitChildren) { \ - for (Stmt * SubStmt : getDerived().getStmtChildren(S)) { \ + for (auto SubStmt : getDerived().getStmtChildren(S)) { \ TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt); \ } \ } \ @@ -2666,9 +2691,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 +2701,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 +2711,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 +2741,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 +2769,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 +2781,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 +2887,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 +2969,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 +2990,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 +3020,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 +3060,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 +3134,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 +3156,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 +3173,9 @@ DEF_TRAVERSE_STMT(OMPCanonicalLoop, { } }) -template +template bool -RecursiveASTVisitor::TraverseOMPLoopDirective(OMPLoopDirective *S) { +RecursiveASTVisitorBase::TraverseOMPLoopDirective(MaybeConst *S) { return TraverseOMPExecutableDirective(S); } @@ -3386,15 +3410,16 @@ 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 +3429,371 @@ 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 +template bool -RecursiveASTVisitor::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { +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 +template bool -RecursiveASTVisitor::VisitOMPCollapseClause(OMPCollapseClause *C) { +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 +template bool -RecursiveASTVisitor::VisitOMPScheduleClause(OMPScheduleClause *C) { +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 +template bool -RecursiveASTVisitor::VisitOMPMergeableClause(OMPMergeableClause *) { +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 +3801,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 +3815,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 +3835,14 @@ 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 +3862,15 @@ 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 +3884,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 +3900,9 @@ bool RecursiveASTVisitor::VisitOMPCopyprivateClause( return true; } -template +template bool -RecursiveASTVisitor::VisitOMPReductionClause(OMPReductionClause *C) { +RecursiveASTVisitorBase::VisitOMPReductionClause(MaybeConst *C) { TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc())); TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo())); TRY_TO(VisitOMPClauseList(C)); @@ -3908,9 +3933,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 +3955,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 +3979,140 @@ 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 +template bool -RecursiveASTVisitor::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { +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 +4120,20 @@ 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 +4142,78 @@ 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 +4283,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..022b2465b356f 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"; From 70a4ab9e482b44f4e2bbd2f56c3fb5a8c953991f Mon Sep 17 00:00:00 2001 From: Arseniy Zaostrovnykh Date: Mon, 22 Sep 2025 12:18:06 +0200 Subject: [PATCH 2/3] [clangd][nfc] Demonstrate use of ConstRecursiveASTVisitor On the example of DeducedTypeVisitor. This is just a demonstration of what I would like to do downstream (but also upstream for the other unnecessary uses of RecursiveASTVisitor). --- clang-tools-extra/clangd/AST.cpp | 16 ++++++++-------- clang-tools-extra/clangd/AST.h | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) 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..854d227958e5b 100644 --- a/clang-tools-extra/clangd/AST.h +++ b/clang-tools-extra/clangd/AST.h @@ -168,7 +168,7 @@ 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 +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); +NamedDecl *getOnlyInstantiation(const NamedDecl *TemplatedDecl); /// Return attributes attached directly to a node. std::vector getAttributes(const DynTypedNode &); From b801e351ce1efb69fb4da452a92c293cc755b09c Mon Sep 17 00:00:00 2001 From: Arseniy Zaostrovnykh Date: Mon, 22 Sep 2025 13:37:31 +0200 Subject: [PATCH 3/3] [nfc] apply clang format --- .../clang-tidy/modernize/LoopConvertUtils.h | 12 +- .../clang-tidy/modernize/PassByValueCheck.cpp | 3 +- clang-tools-extra/clangd/AST.h | 3 +- clang/include/clang/AST/RecursiveASTVisitor.h | 418 +++++++++++------- clang/utils/TableGen/ClangAttrEmitter.cpp | 8 +- 5 files changed, 273 insertions(+), 171 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h index 0c76678c5c81a..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::RecursiveASTVisitorBase; + friend class clang::RecursiveASTVisitorBase; private: StmtParentMap StmtAncestors; @@ -98,7 +99,8 @@ class ComponentFinderASTVisitor /// Accessor for Components. const ComponentVector &getComponents() { return Components; } - friend class clang::RecursiveASTVisitorBase; + friend class clang::RecursiveASTVisitorBase; private: ComponentVector Components; @@ -155,7 +157,8 @@ class DependencyFinderASTVisitor return DependsOnInsideVariable; } - friend class clang::RecursiveASTVisitorBase; + friend class clang::RecursiveASTVisitorBase; private: const StmtParentMap *StmtParents; @@ -188,7 +191,8 @@ class DeclFinderASTVisitor return Found; } - friend class clang::RecursiveASTVisitorBase; + friend class clang::RecursiveASTVisitorBase; private: std::string Name; diff --git a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp index f823a7019259d..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 RecursiveASTVisitorBase; + friend class RecursiveASTVisitorBase; public: ExactlyOneUsageVisitor(const ParmVarDecl *ParamDecl) diff --git a/clang-tools-extra/clangd/AST.h b/clang-tools-extra/clangd/AST.h index 854d227958e5b..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(const ASTContext &, const HeuristicResolver *, +std::optional getDeducedType(const ASTContext &, + const HeuristicResolver *, SourceLocation Loc); // Find the abbreviated-function-template `auto` within a type, or returns null. diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index 5aa2ebf7bc8f2..a3ea8a58b024a 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -55,7 +55,6 @@ namespace clang { - // A helper macro to implement short-circuiting when recursing. It // invokes CALL_EXPR, which must be a method call, on the derived // object (s.t. a user of RecursiveASTVisitor can override the method @@ -154,11 +153,10 @@ 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. -/// 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 { +/// 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; @@ -176,7 +174,8 @@ class RecursiveASTVisitorBase { /// 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. - using DataRecursionQueue = SmallVectorImpl>; + using DataRecursionQueue = + SmallVectorImpl>; /// Return a reference to the derived class. Derived &getDerived() { return *static_cast(this); } @@ -350,10 +349,10 @@ class RecursiveASTVisitorBase { // ---- Methods on Stmts ---- - using MaybeConstChildRange = - std::conditional_t; +using MaybeConstChildRange = + std::conditional_t; - MaybeConstChildRange getStmtChildren(StmtPtr S) { return S->children(); } +MaybeConstChildRange getStmtChildren(StmtPtr S) { return S->children(); } private: // Traverse the given statement. If the most-derived traverse function takes a @@ -370,8 +369,11 @@ class RecursiveASTVisitorBase { decltype(&RecursiveASTVisitorBase::Traverse##NAME), \ decltype(&Derived::Traverse##NAME)>::value, \ Derived &, RecursiveASTVisitorBase &>>(*this) \ - .Traverse##NAME(const_cast *>(static_cast(VAR)), QUEUE) \ - : getDerived().Traverse##NAME(const_cast *>(static_cast(VAR)))) + .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 @@ -385,8 +387,9 @@ class RecursiveASTVisitorBase { public: // Declare Traverse*() for all concrete Stmt classes. #define ABSTRACT_STMT(STMT) -#define STMT(CLASS, PARENT) \ - bool Traverse##CLASS(MaybeConst *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. @@ -394,7 +397,7 @@ class RecursiveASTVisitorBase { bool WalkUpFromStmt(StmtPtr S) { return getDerived().VisitStmt(S); } bool VisitStmt(StmtPtr S) { return true; } #define STMT(CLASS, PARENT) \ - bool WalkUpFrom##CLASS(MaybeConst *S) { \ + bool WalkUpFrom##CLASS(MaybeConst *S) { \ TRY_TO(WalkUpFrom##PARENT(S)); \ TRY_TO(Visit##CLASS(S)); \ return true; \ @@ -408,7 +411,8 @@ class RecursiveASTVisitorBase { // Declare Traverse*() for all concrete Type classes. #define ABSTRACT_TYPE(CLASS, BASE) #define TYPE(CLASS, BASE) \ - bool Traverse##CLASS##Type(MaybeConst *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. @@ -416,7 +420,7 @@ class RecursiveASTVisitorBase { bool WalkUpFromType(TypePtr T) { return getDerived().VisitType(T); } bool VisitType(TypePtr T) { return true; } #define TYPE(CLASS, BASE) \ - bool WalkUpFrom##CLASS##Type(MaybeConst *T) { \ + bool WalkUpFrom##CLASS##Type(MaybeConst *T) { \ TRY_TO(WalkUpFrom##BASE(T)); \ TRY_TO(Visit##CLASS##Type(T)); \ return true; \ @@ -463,7 +467,8 @@ class RecursiveASTVisitorBase { // Declare Traverse*() for all concrete Decl classes. #define ABSTRACT_DECL(DECL) -#define DECL(CLASS, BASE) bool Traverse##CLASS##Decl(MaybeConst *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. @@ -471,7 +476,7 @@ class RecursiveASTVisitorBase { bool WalkUpFromDecl(DeclPtr D) { return getDerived().VisitDecl(D); } bool VisitDecl(DeclPtr D) { return true; } #define DECL(CLASS, BASE) \ - bool WalkUpFrom##CLASS##Decl(MaybeConst *D) { \ + bool WalkUpFrom##CLASS##Decl(MaybeConst *D) { \ TRY_TO(WalkUpFrom##BASE(D)); \ TRY_TO(Visit##CLASS##Decl(D)); \ return true; \ @@ -482,7 +487,8 @@ class RecursiveASTVisitorBase { bool canIgnoreChildDeclWhileTraversingDeclContext(const Decl *Child); #define DEF_TRAVERSE_TMPL_INST(TMPLDECLKIND) \ - bool TraverseTemplateInstantiations(MaybeConst *D); + bool TraverseTemplateInstantiations( \ + MaybeConst *D); DEF_TRAVERSE_TMPL_INST(Class) DEF_TRAVERSE_TMPL_INST(Var) DEF_TRAVERSE_TMPL_INST(Function) @@ -493,20 +499,23 @@ class RecursiveASTVisitorBase { bool TraverseConceptRequirement(MaybeConst *R); bool TraverseConceptTypeRequirement(MaybeConst *R); bool TraverseConceptExprRequirement(MaybeConst *R); - bool TraverseConceptNestedRequirement(MaybeConst *R); + bool + TraverseConceptNestedRequirement(MaybeConst *R); /// 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(MaybeConst *TPL); + bool + TraverseTemplateParameterListHelper(MaybeConst *TPL); // Traverses template parameter lists of either a DeclaratorDecl or TagDecl. template bool TraverseDeclTemplateParameterLists(T *D); - bool TraverseTemplateTypeParamDeclConstraints(const MaybeConst *D); + bool TraverseTemplateTypeParamDeclConstraints( + const MaybeConst *D); bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL, unsigned Count); @@ -535,8 +544,8 @@ class RecursiveASTVisitorBase { bool PostVisitStmt(StmtPtr S); bool TraverseOpenACCConstructStmt(MaybeConst *S); - bool - TraverseOpenACCAssociatedStmtConstruct(MaybeConst *S); + bool TraverseOpenACCAssociatedStmtConstruct( + MaybeConst *S); bool VisitOpenACCClauseList(ArrayRef); bool VisitOpenACCClause(const OpenACCClause *); }; @@ -566,21 +575,24 @@ bool RecursiveASTVisitorBase::TraverseConceptRequirement( switch (R->getKind()) { case concepts::Requirement::RK_Type: return getDerived().TraverseConceptTypeRequirement( - const_cast *>(cast(R))); + const_cast *>( + cast(R))); case concepts::Requirement::RK_Simple: case concepts::Requirement::RK_Compound: return getDerived().TraverseConceptExprRequirement( - const_cast *>(cast(R))); + const_cast *>( + cast(R))); case concepts::Requirement::RK_Nested: return getDerived().TraverseConceptNestedRequirement( - const_cast *>(cast(R))); + const_cast *>( + cast(R))); } llvm_unreachable("unexpected case"); } template -bool RecursiveASTVisitorBase::dataTraverseNode(StmtPtr S, - DataRecursionQueue *Queue) { +bool RecursiveASTVisitorBase::dataTraverseNode( + StmtPtr S, DataRecursionQueue *Queue) { // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt. switch (S->getStmtClass()) { case Stmt::NoStmtClass: @@ -624,8 +636,9 @@ bool RecursiveASTVisitorBase::TraverseConceptExprRequirement( } template -bool RecursiveASTVisitorBase::TraverseConceptNestedRequirement( - MaybeConst *R) { +bool RecursiveASTVisitorBase:: + TraverseConceptNestedRequirement( + MaybeConst *R) { if (!R->hasInvalidConstraint()) return getDerived().TraverseStmt(R->getConstraintExpr()); return true; @@ -668,7 +681,7 @@ bool RecursiveASTVisitorBase::PostVisitStmt(StmtPtr S) { &RecursiveASTVisitorBase::Traverse##CLASS, \ &Derived::Traverse##CLASS)) { \ auto ILE = \ - const_cast *>(static_cast(S)); \ + 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()) \ @@ -687,7 +700,8 @@ bool RecursiveASTVisitorBase::PostVisitStmt(StmtPtr S) { // without any benefit to runtime performance. template LLVM_ATTRIBUTE_NOINLINE bool -RecursiveASTVisitorBase::TraverseStmt(StmtPtr S, DataRecursionQueue *Queue) { +RecursiveASTVisitorBase::TraverseStmt( + StmtPtr S, DataRecursionQueue *Queue) { if (!S) return true; @@ -727,8 +741,8 @@ RecursiveASTVisitorBase::TraverseStmt(StmtPtr S, DataRecursion } template -bool RecursiveASTVisitorBase::TraverseType(QualType T, - bool TraverseQualifier) { +bool RecursiveASTVisitorBase::TraverseType( + QualType T, bool TraverseQualifier) { if (T.isNull()) return true; @@ -747,8 +761,8 @@ bool RecursiveASTVisitorBase::TraverseType(QualType T, } template -bool RecursiveASTVisitorBase::TraverseTypeLoc(TypeLoc TL, - bool TraverseQualifier) { +bool RecursiveASTVisitorBase::TraverseTypeLoc( + TypeLoc TL, bool TraverseQualifier) { if (TL.isNull()) return true; @@ -887,7 +901,8 @@ bool RecursiveASTVisitorBase::TraverseDeclarationNameInfo( } template -bool RecursiveASTVisitorBase::TraverseTemplateName(TemplateName Template) { +bool RecursiveASTVisitorBase::TraverseTemplateName( + TemplateName Template) { if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier())); } else if (QualifiedTemplateName *QTN = @@ -992,10 +1007,9 @@ bool RecursiveASTVisitorBase::TraverseConstructorInitializer( } template -bool -RecursiveASTVisitorBase::TraverseLambdaCapture(MaybeConst *LE, - const LambdaCapture *C, - MaybeConst *Init) { +bool RecursiveASTVisitorBase::TraverseLambdaCapture( + MaybeConst *LE, const LambdaCapture *C, + MaybeConst *Init) { if (LE->isInitCapture(C)) TRY_TO(TraverseDecl(C->getCapturedVar())); else @@ -1007,9 +1021,9 @@ RecursiveASTVisitorBase::TraverseLambdaCapture(MaybeConst \ - bool RecursiveASTVisitorBase::Traverse##TYPE(MaybeConst *T, \ - bool TraverseQualifier) { \ + template \ + bool RecursiveASTVisitorBase::Traverse##TYPE( \ + MaybeConst *T, bool TraverseQualifier) { \ if (!getDerived().shouldTraversePostOrder()) \ TRY_TO(WalkUpFrom##TYPE(T)); \ { \ @@ -1200,8 +1214,8 @@ DEF_TRAVERSE_TYPE(MacroQualifiedType, { TRY_TO(TraverseType(T->getUnderlyingType())); }) template -bool RecursiveASTVisitorBase::TraverseTagType(MaybeConst *T, - bool TraverseQualifier) { +bool RecursiveASTVisitorBase::TraverseTagType( + MaybeConst *T, bool TraverseQualifier) { if (TraverseQualifier) TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); return true; @@ -1279,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 RecursiveASTVisitorBase::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; \ @@ -1293,7 +1308,8 @@ 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; \ } @@ -1357,7 +1373,8 @@ DEF_TRAVERSE_TYPELOC(DecayedType, { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); }) template -bool RecursiveASTVisitorBase::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) { +bool RecursiveASTVisitorBase::TraverseArrayTypeLocHelper( + ArrayTypeLoc TL) { // This isn't available for ArrayType, but is for the ArrayTypeLoc. TRY_TO(TraverseStmt(TL.getSizeExpr())); return true; @@ -1545,8 +1562,8 @@ DEF_TRAVERSE_TYPELOC(HLSLInlineSpirvType, { TRY_TO(TraverseType(TL.getType())); }) template -bool RecursiveASTVisitorBase::TraverseTagTypeLoc(TagTypeLoc TL, - bool TraverseQualifier) { +bool RecursiveASTVisitorBase::TraverseTagTypeLoc( + TagTypeLoc TL, bool TraverseQualifier) { if (NestedNameSpecifierLoc QualifierLoc = TL.getQualifierLoc(); TraverseQualifier && QualifierLoc) TRY_TO(TraverseNestedNameSpecifierLoc(QualifierLoc)); @@ -1641,8 +1658,8 @@ DEF_TRAVERSE_TYPELOC(PredefinedSugarType, {}) // than those. template -bool RecursiveASTVisitorBase::canIgnoreChildDeclWhileTraversingDeclContext( - const Decl *Child) { +bool RecursiveASTVisitorBase:: + canIgnoreChildDeclWhileTraversingDeclContext(const Decl *Child) { // BlockDecls are traversed through BlockExprs, // CapturedDecls are traversed through CapturedStmts. if (isa(Child) || isa(Child)) @@ -1654,7 +1671,8 @@ bool RecursiveASTVisitorBase::canIgnoreChildDeclWhileTraversin } template -bool RecursiveASTVisitorBase::TraverseDeclContextHelper(MaybeConst *DC) { +bool RecursiveASTVisitorBase::TraverseDeclContextHelper( + MaybeConst *DC) { if (!DC) return true; @@ -1668,13 +1686,16 @@ bool RecursiveASTVisitorBase::TraverseDeclContextHelper(MaybeC // This macro makes available a variable D, the passed-in decl. #define DEF_TRAVERSE_DECL(DECL, CODE) \ - template \ - bool RecursiveASTVisitorBase::Traverse##DECL(MaybeConst *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) { \ @@ -1958,8 +1979,9 @@ DEF_TRAVERSE_DECL(OpenACCRoutineDecl, { // A helper method for TemplateDecl's children. template -bool RecursiveASTVisitorBase::TraverseTemplateParameterListHelper( - MaybeConst *TPL) { +bool RecursiveASTVisitorBase:: + TraverseTemplateParameterListHelper( + MaybeConst *TPL) { if (TPL) { for (MaybeConst *D : *TPL) { TRY_TO(TraverseDecl(D)); @@ -1973,7 +1995,8 @@ bool RecursiveASTVisitorBase::TraverseTemplateParameterListHel template template -bool RecursiveASTVisitorBase::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); @@ -2102,8 +2125,9 @@ DEF_TRAVERSE_DECL(BuiltinTemplateDecl, { }) template -bool RecursiveASTVisitorBase::TraverseTemplateTypeParamDeclConstraints( - const MaybeConst *D) { +bool RecursiveASTVisitorBase:: + TraverseTemplateTypeParamDeclConstraints( + const MaybeConst *D) { if (const auto *TC = D->getTypeConstraint()) TRY_TO(TraverseTypeConstraint(TC)); return true; @@ -2165,7 +2189,8 @@ DEF_TRAVERSE_DECL(EnumDecl, { // Helper methods for RecordDecl and its children. template -bool RecursiveASTVisitorBase::TraverseRecordHelper(MaybeConst *D) { +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. @@ -2182,7 +2207,8 @@ bool RecursiveASTVisitorBase::TraverseCXXBaseSpecifier( } template -bool RecursiveASTVisitorBase::TraverseCXXRecordHelper(MaybeConst *D) { +bool RecursiveASTVisitorBase::TraverseCXXRecordHelper( + MaybeConst *D) { if (!TraverseRecordHelper(D)) return false; if (D->isCompleteDefinition()) { @@ -2200,8 +2226,9 @@ DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); }) DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); }) template -bool RecursiveASTVisitorBase::TraverseTemplateArgumentLocsHelper( - const TemplateArgumentLoc *TAL, unsigned Count) { +bool RecursiveASTVisitorBase:: + TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL, + unsigned Count) { for (unsigned I = 0; I < Count; ++I) { TRY_TO(TraverseTemplateArgumentLoc(TAL[I])); } @@ -2274,7 +2301,8 @@ DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, { DEF_TRAVERSE_DECL(IndirectFieldDecl, {}) template -bool RecursiveASTVisitorBase::TraverseDeclaratorHelper(MaybeConst *D) { +bool RecursiveASTVisitorBase::TraverseDeclaratorHelper( + MaybeConst *D) { TRY_TO(TraverseDeclTemplateParameterLists(D)); TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); if (D->getTypeSourceInfo()) @@ -2329,7 +2357,8 @@ DEF_TRAVERSE_DECL(ObjCIvarDecl, { }) template -bool RecursiveASTVisitorBase::TraverseFunctionHelper(MaybeConst *D) { +bool RecursiveASTVisitorBase::TraverseFunctionHelper( + MaybeConst *D) { TRY_TO(TraverseDeclTemplateParameterLists(D)); TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo())); @@ -2463,7 +2492,8 @@ DEF_TRAVERSE_DECL(CXXDestructorDecl, { }) template -bool RecursiveASTVisitorBase::TraverseVarHelper(MaybeConst *D) { +bool RecursiveASTVisitorBase::TraverseVarHelper( + MaybeConst *D) { TRY_TO(TraverseDeclaratorHelper(D)); // Default params are taken care of when we traverse the ParmVarDecl. if (!isa(D) && @@ -2514,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 \ + template \ bool RecursiveASTVisitorBase::Traverse##STMT( \ - MaybeConst *S, DataRecursionQueue *Queue) { \ + MaybeConst *S, DataRecursionQueue *Queue) { \ bool ShouldVisitChildren = true; \ bool ReturnValue = true; \ if (!getDerived().shouldTraversePostOrder()) \ TRY_TO(WalkUpFrom##STMT(S)); \ - { CODE; } \ + { \ + CODE; \ + } \ if (ShouldVisitChildren) { \ - for (auto SubStmt : getDerived().getStmtChildren(S)) { \ + for (auto SubStmt : getDerived().getStmtChildren(S)) { \ TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt); \ } \ } \ @@ -3174,8 +3206,8 @@ DEF_TRAVERSE_STMT(OMPCanonicalLoop, { }) template -bool -RecursiveASTVisitorBase::TraverseOMPLoopDirective(MaybeConst *S) { +bool RecursiveASTVisitorBase::TraverseOMPLoopDirective( + MaybeConst *S) { return TraverseOMPExecutableDirective(S); } @@ -3411,7 +3443,8 @@ DEF_TRAVERSE_STMT(OMPErrorDirective, // OpenMP clauses. template -bool RecursiveASTVisitorBase::TraverseOMPClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::TraverseOMPClause( + MaybeConst *C) { if (!C) return true; switch (C->getClauseKind()) { @@ -3452,54 +3485,61 @@ bool RecursiveASTVisitorBase::VisitOMPAllocatorClause( } template -bool RecursiveASTVisitorBase::VisitOMPAllocateClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPAllocateClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getAllocator())); TRY_TO(VisitOMPClauseList(C)); return true; } template -bool RecursiveASTVisitorBase::VisitOMPIfClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPIfClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getCondition())); return true; } template -bool RecursiveASTVisitorBase::VisitOMPFinalClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPFinalClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getCondition())); return true; } template -bool -RecursiveASTVisitorBase::VisitOMPNumThreadsClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPNumThreadsClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getNumThreads())); return true; } template -bool RecursiveASTVisitorBase::VisitOMPAlignClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPAlignClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getAlignment())); return true; } template -bool RecursiveASTVisitorBase::VisitOMPSafelenClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPSafelenClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getSafelen())); return true; } template -bool RecursiveASTVisitorBase::VisitOMPSimdlenClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPSimdlenClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getSimdlen())); return true; } template -bool RecursiveASTVisitorBase::VisitOMPSizesClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPSizesClause( + MaybeConst *C) { for (MaybeConst *E : C->getSizesRefs()) TRY_TO(TraverseStmt(E)); return true; @@ -3514,30 +3554,34 @@ bool RecursiveASTVisitorBase::VisitOMPPermutationClause( } template -bool RecursiveASTVisitorBase::VisitOMPFullClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPFullClause( + MaybeConst *C) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPPartialClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPPartialClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getFactor())); return true; } template -bool -RecursiveASTVisitorBase::VisitOMPCollapseClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPCollapseClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getNumForLoops())); return true; } template -bool RecursiveASTVisitorBase::VisitOMPDefaultClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPDefaultClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPProcBindClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPProcBindClause( + MaybeConst *) { return true; } @@ -3548,8 +3592,9 @@ bool RecursiveASTVisitorBase::VisitOMPUnifiedAddressClause( } template -bool RecursiveASTVisitorBase::VisitOMPUnifiedSharedMemoryClause( - MaybeConst *) { +bool RecursiveASTVisitorBase:: + VisitOMPUnifiedSharedMemoryClause( + MaybeConst *) { return true; } @@ -3566,119 +3611,139 @@ bool RecursiveASTVisitorBase::VisitOMPDynamicAllocatorsClause( } template -bool RecursiveASTVisitorBase::VisitOMPAtomicDefaultMemOrderClause( - MaybeConst *) { +bool RecursiveASTVisitorBase:: + VisitOMPAtomicDefaultMemOrderClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPSelfMapsClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPSelfMapsClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPAtClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPAtClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPSeverityClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPSeverityClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPMessageClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPMessageClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getMessageString())); return true; } template -bool -RecursiveASTVisitorBase::VisitOMPScheduleClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPScheduleClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getChunkSize())); return true; } template -bool RecursiveASTVisitorBase::VisitOMPOrderedClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPOrderedClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getNumForLoops())); return true; } template -bool RecursiveASTVisitorBase::VisitOMPNowaitClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPNowaitClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPUntiedClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPUntiedClause( + MaybeConst *) { return true; } template -bool -RecursiveASTVisitorBase::VisitOMPMergeableClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPMergeableClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPReadClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPReadClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPWriteClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPWriteClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPUpdateClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPUpdateClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPCaptureClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPCaptureClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPCompareClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPCompareClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPFailClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPFailClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPSeqCstClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPSeqCstClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPAcqRelClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPAcqRelClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPAbsentClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPAbsentClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPHoldsClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPHoldsClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPContainsClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPContainsClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPNoOpenMPClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPNoOpenMPClause( + MaybeConst *) { return true; } @@ -3689,8 +3754,9 @@ bool RecursiveASTVisitorBase::VisitOMPNoOpenMPRoutinesClause( } template -bool RecursiveASTVisitorBase::VisitOMPNoOpenMPConstructsClause( - MaybeConst *) { +bool RecursiveASTVisitorBase:: + VisitOMPNoOpenMPConstructsClause( + MaybeConst *) { return true; } @@ -3701,54 +3767,64 @@ bool RecursiveASTVisitorBase::VisitOMPNoParallelismClause( } template -bool RecursiveASTVisitorBase::VisitOMPAcquireClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPAcquireClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPReleaseClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPReleaseClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPRelaxedClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPRelaxedClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPWeakClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPWeakClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPThreadsClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPThreadsClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPSIMDClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPSIMDClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPNogroupClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPNogroupClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPInitClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPInitClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } template -bool RecursiveASTVisitorBase::VisitOMPUseClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPUseClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getInteropVar())); return true; } template -bool RecursiveASTVisitorBase::VisitOMPDestroyClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPDestroyClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getInteropVar())); return true; } @@ -3793,7 +3869,8 @@ bool RecursiveASTVisitorBase::VisitOMPExclusiveClause( } template -bool RecursiveASTVisitorBase::VisitOMPPrivateClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPPrivateClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); for (auto *E : C->private_copies()) { TRY_TO(TraverseStmt(E)); @@ -3836,13 +3913,15 @@ bool RecursiveASTVisitorBase::VisitOMPLastprivateClause( } template -bool RecursiveASTVisitorBase::VisitOMPSharedClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPSharedClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } template -bool RecursiveASTVisitorBase::VisitOMPLinearClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPLinearClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getStep())); TRY_TO(TraverseStmt(C->getCalcStep())); TRY_TO(VisitOMPClauseList(C)); @@ -3863,14 +3942,16 @@ bool RecursiveASTVisitorBase::VisitOMPLinearClause(MaybeConst< } template -bool RecursiveASTVisitorBase::VisitOMPAlignedClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPAlignedClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getAlignment())); TRY_TO(VisitOMPClauseList(C)); return true; } template -bool RecursiveASTVisitorBase::VisitOMPCopyinClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPCopyinClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); for (auto *E : C->source_exprs()) { TRY_TO(TraverseStmt(E)); @@ -3901,8 +3982,8 @@ bool RecursiveASTVisitorBase::VisitOMPCopyprivateClause( } template -bool -RecursiveASTVisitorBase::VisitOMPReductionClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPReductionClause( + MaybeConst *C) { TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc())); TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo())); TRY_TO(VisitOMPClauseList(C)); @@ -3980,32 +4061,37 @@ bool RecursiveASTVisitorBase::VisitOMPInReductionClause( } template -bool RecursiveASTVisitorBase::VisitOMPFlushClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPFlushClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } template -bool RecursiveASTVisitorBase::VisitOMPDepobjClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPDepobjClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getDepobj())); return true; } template -bool RecursiveASTVisitorBase::VisitOMPDependClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPDependClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } template -bool RecursiveASTVisitorBase::VisitOMPDeviceClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPDeviceClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getDevice())); return true; } template -bool RecursiveASTVisitorBase::VisitOMPMapClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPMapClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } @@ -4051,7 +4137,8 @@ bool RecursiveASTVisitorBase::VisitOMPNumTasksClause( } template -bool RecursiveASTVisitorBase::VisitOMPHintClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPHintClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getHint())); return true; } @@ -4065,19 +4152,21 @@ bool RecursiveASTVisitorBase::VisitOMPDistScheduleClause( } template -bool -RecursiveASTVisitorBase::VisitOMPDefaultmapClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPDefaultmapClause( + MaybeConst *C) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPToClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPToClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } template -bool RecursiveASTVisitorBase::VisitOMPFromClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPFromClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseList(C)); return true; } @@ -4121,12 +4210,14 @@ bool RecursiveASTVisitorBase::VisitOMPNontemporalClause( } template -bool RecursiveASTVisitorBase::VisitOMPOrderClause(MaybeConst *) { +bool RecursiveASTVisitorBase::VisitOMPOrderClause( + MaybeConst *) { return true; } template -bool RecursiveASTVisitorBase::VisitOMPDetachClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPDetachClause( + MaybeConst *C) { TRY_TO(TraverseStmt(C->getEventHandler())); return true; } @@ -4152,14 +4243,16 @@ bool RecursiveASTVisitorBase::VisitOMPAffinityClause( } template -bool RecursiveASTVisitorBase::VisitOMPFilterClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPFilterClause( + MaybeConst *C) { TRY_TO(VisitOMPClauseWithPreInit(C)); TRY_TO(TraverseStmt(C->getThreadID())); return true; } template -bool RecursiveASTVisitorBase::VisitOMPBindClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPBindClause( + MaybeConst *C) { return true; } @@ -4185,7 +4278,8 @@ bool RecursiveASTVisitorBase::VisitOMPXAttributeClause( } template -bool RecursiveASTVisitorBase::VisitOMPXBareClause(MaybeConst *C) { +bool RecursiveASTVisitorBase::VisitOMPXBareClause( + MaybeConst *C) { return true; } @@ -4197,8 +4291,9 @@ bool RecursiveASTVisitorBase::TraverseOpenACCConstructStmt( } template -bool RecursiveASTVisitorBase::TraverseOpenACCAssociatedStmtConstruct( - MaybeConst *S) { +bool RecursiveASTVisitorBase:: + TraverseOpenACCAssociatedStmtConstruct( + MaybeConst *S) { TRY_TO(TraverseOpenACCConstructStmt(S)); // TODO: Need to access getAssociatedStmt() which is currently protected // TRY_TO(TraverseStmt(S->getAssociatedStmt())); @@ -4206,7 +4301,8 @@ bool RecursiveASTVisitorBase::TraverseOpenACCAssociatedStmtCon } template -bool RecursiveASTVisitorBase::VisitOpenACCClause(const OpenACCClause *C) { +bool RecursiveASTVisitorBase::VisitOpenACCClause( + const OpenACCClause *C) { for (const Stmt *Child : C->children()) TRY_TO(TraverseStmt(const_cast *>(Child))); return true; @@ -4286,12 +4382,12 @@ DEF_TRAVERSE_STMT(HLSLOutArgExpr, {}) // Backward compatibility - keep the original class name structure template class RecursiveASTVisitor - : public RecursiveASTVisitorBase {}; + : public RecursiveASTVisitorBase {}; // New const-aware visitor interface template class ConstRecursiveASTVisitor - : public RecursiveASTVisitorBase {}; + : public RecursiveASTVisitorBase {}; } // end namespace clang diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp b/clang/utils/TableGen/ClangAttrEmitter.cpp index 022b2465b356f..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(MaybeConst<" << R.getName() << "Attr> *A);\n"; - OS << " bool Visit" << R.getName() - << "Attr(MaybeConst<" << 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"; }