From 33b7d5f1063f982e059382edcaef659c6791d54a Mon Sep 17 00:00:00 2001 From: alandefreitas Date: Mon, 27 Jan 2025 16:35:16 -0300 Subject: [PATCH] AST visits const declarations #refactor --- src/lib/AST/ASTVisitor.cpp | 165 +++++++++++------------- src/lib/AST/ASTVisitor.hpp | 140 ++++++++++---------- src/lib/AST/ClangHelpers.cpp | 4 +- src/lib/AST/ClangHelpers.hpp | 38 +++--- src/lib/AST/InstantiatedFromVisitor.hpp | 60 ++++----- 5 files changed, 192 insertions(+), 215 deletions(-) diff --git a/src/lib/AST/ASTVisitor.cpp b/src/lib/AST/ASTVisitor.cpp index f23260021..150f1d2ea 100644 --- a/src/lib/AST/ASTVisitor.cpp +++ b/src/lib/AST/ASTVisitor.cpp @@ -75,7 +75,7 @@ build() // traverse the translation unit, only extracting // declarations which satisfy all filter conditions. // dependencies will be tracked, but not extracted - auto* TU = context_.getTranslationUnitDecl(); + TranslationUnitDecl const* TU = context_.getTranslationUnitDecl(); traverse(TU); MRDOCS_ASSERT(find(SymbolID::global)); } @@ -85,7 +85,7 @@ template < std::derived_from DeclTy> Info* ASTVisitor:: -traverse(DeclTy* D) +traverse(DeclTy const* D) { MRDOCS_ASSERT(D); MRDOCS_CHECK_OR(!D->isInvalidDecl(), nullptr); @@ -142,7 +142,7 @@ traverse(DeclTy* D) Info* ASTVisitor:: -traverse(FunctionTemplateDecl* D) +traverse(FunctionTemplateDecl const* D) { // Route the traversal to GuideInfo or FunctionInfo if (FunctionDecl* FD = D->getTemplatedDecl(); @@ -155,19 +155,19 @@ traverse(FunctionTemplateDecl* D) Info* ASTVisitor:: -traverse(UsingDirectiveDecl* D) +traverse(UsingDirectiveDecl const* D) { // Find the parent namespace ScopeExitRestore s1(mode_, TraversalMode::Dependency); - Decl* P = getParent(D); + Decl const* P = getParent(D); MRDOCS_SYMBOL_TRACE(P, context_); Info* PI = findOrTraverse(P); MRDOCS_CHECK_OR(PI, nullptr); - auto PNI = dynamic_cast(PI); + auto* const PNI = dynamic_cast(PI); MRDOCS_CHECK_OR(PNI, nullptr); // Find the nominated namespace - Decl* ND = D->getNominatedNamespace(); + Decl const* ND = D->getNominatedNamespace(); MRDOCS_SYMBOL_TRACE(ND, context_); ScopeExitRestore s2(mode_, TraversalMode::Dependency); Info* NDI = findOrTraverse(ND); @@ -182,7 +182,7 @@ traverse(UsingDirectiveDecl* D) Info* ASTVisitor:: -traverse(IndirectFieldDecl* D) +traverse(IndirectFieldDecl const* D) { return traverse(D->getAnonField()); } @@ -193,7 +193,7 @@ template < requires (!std::derived_from) void ASTVisitor:: -traverseMembers(InfoTy& I, DeclTy* DC) +traverseMembers(InfoTy& I, DeclTy const* DC) { // When a declaration context is a function, we should // not traverse its members as function arguments are @@ -242,7 +242,7 @@ template < std::derived_from DeclTy> void ASTVisitor:: -traverseMembers(InfoTy& I, DeclTy* D) +traverseMembers(InfoTy& I, DeclTy const* D) { traverseMembers(I, D->getTemplatedDecl()); } @@ -253,10 +253,10 @@ template < requires (!std::derived_from) void ASTVisitor:: -traverseParents(InfoTy& I, DeclTy* DC) +traverseParents(InfoTy& I, DeclTy const* DC) { MRDOCS_SYMBOL_TRACE(DC, context_); - if (Decl* PD = getParent(DC)) + if (Decl const* PD = getParent(DC)) { MRDOCS_SYMBOL_TRACE(PD, context_); @@ -288,7 +288,7 @@ template < std::derived_from DeclTy> void ASTVisitor:: -traverseParents(InfoTy& I, DeclTy* D) +traverseParents(InfoTy& I, DeclTy const* D) { traverseParents(I, D->getTemplatedDecl()); } @@ -525,7 +525,7 @@ isDefinition(DeclTy* D) template DeclTy> void ASTVisitor:: -populate(Info& I, bool const isNew, DeclTy* D) +populate(Info& I, bool const isNew, DeclTy const* D) { // Populate the documentation bool const isDocumented = generateJavadoc(I.javadoc, D); @@ -595,7 +595,7 @@ void ASTVisitor:: populate( NamespaceInfo& I, - NamespaceDecl* D) + NamespaceDecl const* D) { I.IsAnonymous = D->isAnonymousNamespace(); if (!I.IsAnonymous) @@ -609,7 +609,7 @@ void ASTVisitor:: populate( NamespaceInfo& I, - TranslationUnitDecl*) + TranslationUnitDecl const*) { I.id = SymbolID::global; I.IsAnonymous = false; @@ -620,7 +620,7 @@ void ASTVisitor:: populate( RecordInfo& I, - CXXRecordDecl* D) + CXXRecordDecl const* D) { if (D->getTypedefNameForAnonDecl()) { @@ -668,7 +668,7 @@ populate( void ASTVisitor:: -populate(RecordInfo& I, ClassTemplateDecl* D) +populate(RecordInfo& I, ClassTemplateDecl const* D) { populate(I.Template, D->getTemplatedDecl(), D); populate(I, D->getTemplatedDecl()); @@ -676,7 +676,7 @@ populate(RecordInfo& I, ClassTemplateDecl* D) void ASTVisitor:: -populate(RecordInfo& I, ClassTemplateSpecializationDecl* D) +populate(RecordInfo& I, ClassTemplateSpecializationDecl const* D) { populate(I.Template, D, D->getSpecializedTemplate()); populate(I, cast(D)); @@ -686,7 +686,7 @@ void ASTVisitor:: populate( FunctionInfo& I, - FunctionDecl* D) + FunctionDecl const* D) { MRDOCS_SYMBOL_TRACE(D, context_); @@ -851,18 +851,18 @@ populate( void ASTVisitor:: -populate(FunctionInfo& I, FunctionTemplateDecl* D) +populate(FunctionInfo& I, FunctionTemplateDecl const* D) { - FunctionDecl* TD = D->getTemplatedDecl(); + FunctionDecl const* TD = D->getTemplatedDecl(); populate(I.Template, TD, D); populate(I, TD); } void ASTVisitor:: -populate(FunctionInfo& I, CXXMethodDecl* D) +populate(FunctionInfo& I, CXXMethodDecl const* D) { - FunctionDecl* FD = D; + FunctionDecl const* FD = D; populate(I, FD); I.IsVirtual |= D->isVirtual(); I.IsVirtualAsWritten |= D->isVirtualAsWritten(); @@ -875,26 +875,26 @@ populate(FunctionInfo& I, CXXMethodDecl* D) void ASTVisitor:: -populate(FunctionInfo& I, CXXConstructorDecl* D) +populate(FunctionInfo& I, CXXConstructorDecl const* D) { - CXXMethodDecl* FD = D; + CXXMethodDecl const* FD = D; populate(I, FD); populate(I.Explicit, D->getExplicitSpecifier()); } void ASTVisitor:: -populate(FunctionInfo& I, CXXDestructorDecl* D) +populate(FunctionInfo& I, CXXDestructorDecl const* D) { - CXXMethodDecl* FD = D; + CXXMethodDecl const* FD = D; populate(I, FD); } void ASTVisitor:: -populate(FunctionInfo& I, CXXConversionDecl* D) +populate(FunctionInfo& I, CXXConversionDecl const* D) { - CXXMethodDecl* FD = D; + CXXMethodDecl const* FD = D; populate(I, FD); populate(I.Explicit, D->getExplicitSpecifier()); } @@ -904,7 +904,7 @@ void ASTVisitor:: populate( EnumInfo& I, - EnumDecl* D) + EnumDecl const* D) { I.Scoped = D->isScoped(); if (D->isFixed()) @@ -917,7 +917,7 @@ void ASTVisitor:: populate( EnumConstantInfo& I, - EnumConstantDecl* D) + EnumConstantDecl const* D) { I.Name = extractName(D); populate( @@ -928,7 +928,7 @@ populate( void ASTVisitor:: -populate(TypedefInfo& I, TypedefNameDecl* D) +populate(TypedefInfo& I, TypedefNameDecl const* D) { QualType const QT = D->getUnderlyingType(); I.Type = toTypeInfo(QT); @@ -936,14 +936,14 @@ populate(TypedefInfo& I, TypedefNameDecl* D) void ASTVisitor:: -populate(TypedefInfo& I, TypedefDecl* D) +populate(TypedefInfo& I, TypedefDecl const* D) { populate(I, cast(D)); } void ASTVisitor:: -populate(TypedefInfo& I, TypeAliasDecl* D) +populate(TypedefInfo& I, TypeAliasDecl const* D) { I.IsUsing = isa(D); populate(I, cast(D)); @@ -951,7 +951,7 @@ populate(TypedefInfo& I, TypeAliasDecl* D) void ASTVisitor:: -populate(TypedefInfo& I, TypeAliasTemplateDecl* D) +populate(TypedefInfo& I, TypeAliasTemplateDecl const* D) { populate(I.Template, D->getTemplatedDecl(), D); if (auto* TD = D->getTemplatedDecl(); @@ -969,7 +969,7 @@ void ASTVisitor:: populate( VariableInfo& I, - VarDecl* D) + VarDecl const* D) { // KRYSTIAN FIXME: we need to properly merge storage class if (StorageClass const SC = D->getStorageClass()) @@ -1003,11 +1003,27 @@ populate( populateAttributes(I, D); } +void +ASTVisitor:: +populate(VariableInfo& I, VarTemplateDecl const* D) +{ + populate(I.Template, D->getTemplatedDecl(), D); + populate(I, D->getTemplatedDecl()); +} + +void +ASTVisitor:: +populate(VariableInfo& I, VarTemplateSpecializationDecl const* D) +{ + populate(I.Template, D, D->getSpecializedTemplate()); + populate(I, cast(D)); +} + void ASTVisitor:: populate( FieldInfo& I, - FieldDecl* D) + FieldDecl const* D) { I.Type = toTypeInfo(D->getType()); I.IsVariant = D->getParent()->isUnion(); @@ -1027,27 +1043,11 @@ populate( populateAttributes(I, D); } -void -ASTVisitor:: -populate(VariableInfo& I, VarTemplateDecl* D) -{ - populate(I.Template, D->getTemplatedDecl(), D); - populate(I, D->getTemplatedDecl()); -} - -void -ASTVisitor:: -populate(VariableInfo& I, VarTemplateSpecializationDecl* D) -{ - populate(I.Template, D, D->getSpecializedTemplate()); - populate(I, cast(D)); -} - void ASTVisitor:: populate( SpecializationInfo& I, - ClassTemplateSpecializationDecl* D) + ClassTemplateSpecializationDecl const* D) { CXXRecordDecl const* PD = getInstantiatedFrom(D); populate(I.Args, D->getTemplateArgs().asArray()); @@ -1058,7 +1058,7 @@ void ASTVisitor:: populate( FriendInfo& I, - FriendDecl* D) + FriendDecl const* D) { // A NamedDecl nominated by a FriendDecl // will be one of the following: @@ -1092,7 +1092,7 @@ void ASTVisitor:: populate( GuideInfo& I, - CXXDeductionGuideDecl* D) + CXXDeductionGuideDecl const* D) { I.Deduced = toTypeInfo(D->getReturnType()); for (const ParmVarDecl* P : D->parameters()) @@ -1110,7 +1110,7 @@ void ASTVisitor:: populate( GuideInfo& I, - FunctionTemplateDecl* D) + FunctionTemplateDecl const* D) { populate(I.Template, D->getTemplatedDecl(), D); populate(I, cast(D->getTemplatedDecl())); @@ -1120,7 +1120,7 @@ void ASTVisitor:: populate( NamespaceAliasInfo& I, - NamespaceAliasDecl* D) + NamespaceAliasDecl const* D) { NamedDecl const* Aliased = D->getAliasedNamespace(); NestedNameSpecifier const* NNS = D->getQualifier(); @@ -1132,7 +1132,7 @@ void ASTVisitor:: populate( UsingInfo& I, - UsingDecl* D) + UsingDecl const* D) { I.Class = UsingClass::Normal; I.Qualifier = toNameInfo(D->getQualifier()); @@ -1151,7 +1151,7 @@ void ASTVisitor:: populate( ConceptInfo& I, - ConceptDecl* D) + ConceptDecl const* D) { populate(I.Template, D, D); populate(I.Constraint, D->getConstraintExpr()); @@ -1165,31 +1165,19 @@ template < std::derived_from TemplateDeclTy> void ASTVisitor:: -populate(TemplateInfo& Template, DeclTy*, TemplateDeclTy* TD) +populate(TemplateInfo& Template, DeclTy const*, TemplateDeclTy const* TD) { MRDOCS_ASSERT(TD); TemplateParameterList const* TPL = TD->getTemplateParameters(); populate(Template, TPL); } -template CXXRecordDeclTy> -void -ASTVisitor:: -populate( - TemplateInfo& Template, - CXXRecordDeclTy*, - ClassTemplateDecl* CTD) -{ - MRDOCS_ASSERT(CTD); - populate(Template, CTD->getTemplateParameters()); -} - void ASTVisitor:: populate( TemplateInfo& Template, - ClassTemplateSpecializationDecl* CTSD, - ClassTemplateDecl* CTD) + ClassTemplateSpecializationDecl const* CTSD, + ClassTemplateDecl const* CTD) { MRDOCS_ASSERT(CTD); @@ -1197,9 +1185,12 @@ populate( generateID(getInstantiatedFrom(CTD), Template.Primary); // Extract the template arguments of the specialization - if (const auto* argsAsWritten = CTSD->getTemplateArgsAsWritten()) { + if (const auto* argsAsWritten = CTSD->getTemplateArgsAsWritten()) + { populate(Template.Args, argsAsWritten); - } else { + } + else + { // Implicit specializations do not have template arguments as written populate(Template.Args, CTSD->getTemplateArgs().asArray()); } @@ -1238,7 +1229,6 @@ populate( TemplateParameterList* params = CTPSD->getTemplateParameters(); populate(Template, params); } - } template VarDeclTy> @@ -1246,8 +1236,8 @@ void ASTVisitor:: populate( TemplateInfo& Template, - VarDeclTy* D, - VarTemplateDecl* VTD) + VarDeclTy const* D, + VarTemplateDecl const* VTD) { MRDOCS_ASSERT(VTD); @@ -1258,8 +1248,10 @@ populate( // extract the template arguments of the specialization populate(Template.Args, VTSD->getTemplateArgsAsWritten()); // extract the template parameters if this is a partial specialization - if(auto* VTPSD = dyn_cast(D)) + if (auto* VTPSD = dyn_cast(D)) + { populate(Template, VTPSD->getTemplateParameters()); + } } else { @@ -1268,12 +1260,11 @@ populate( } } - void ASTVisitor:: populate( NoexceptInfo& I, - const FunctionProtoType* FPT) + FunctionProtoType const* FPT) { MRDOCS_ASSERT(FPT); I.Implicit = ! FPT->hasNoexceptExceptionSpec(); @@ -1545,7 +1536,7 @@ populate( template InfoTy> void ASTVisitor:: -populateAttributes(InfoTy& I, const Decl* D) +populateAttributes(InfoTy& I, Decl const* D) { if constexpr (requires { I.Attributes; }) { @@ -2880,7 +2871,7 @@ find(SymbolID const& id) Info* ASTVisitor:: -find(Decl* D) +find(Decl const* D) { auto ID = generateID(D); MRDOCS_CHECK_OR(ID, nullptr); @@ -3058,7 +3049,7 @@ Expected< InfoTypeFor_t, InfoTy>>> ASTVisitor:: -upsert(DeclType* D) +upsert(DeclType const* D) { ExtractionMode const m = checkFilters(D); if (m == ExtractionMode::Dependency) diff --git a/src/lib/AST/ASTVisitor.hpp b/src/lib/AST/ASTVisitor.hpp index f0943d225..f348e9e71 100644 --- a/src/lib/AST/ASTVisitor.hpp +++ b/src/lib/AST/ASTVisitor.hpp @@ -313,7 +313,7 @@ class ASTVisitor class InfoTy = void, std::derived_from DeclTy> Info* - traverse(DeclTy* D); + traverse(DeclTy const* D); /* Traverse a function template @@ -323,7 +323,7 @@ class ASTVisitor */ Info* - traverse(FunctionTemplateDecl* D); + traverse(FunctionTemplateDecl const* D); /* Traverse a using directive @@ -331,10 +331,10 @@ class ASTVisitor such as `using namespace std;`. If the parent declaration is a Namespace, we - update its `UsingDirectives` field. + update its `UsingDirectives` const field. */ Info* - traverse(UsingDirectiveDecl* D); + traverse(UsingDirectiveDecl const* D); /* Traverse a member of an anonymous union. @@ -342,7 +342,7 @@ class ASTVisitor as a regular `FieldDecl`. */ Info* - traverse(IndirectFieldDecl*); + traverse(IndirectFieldDecl const* D); // ================================================= // AST Traversal Helpers @@ -361,13 +361,13 @@ class ASTVisitor std::derived_from DeclTy> requires (!std::derived_from) void - traverseMembers(InfoTy& I, DeclTy* DC); + traverseMembers(InfoTy& I, DeclTy const* DC); template < std::derived_from InfoTy, std::derived_from DeclTy> void - traverseMembers(InfoTy& I, DeclTy* DC); + traverseMembers(InfoTy& I, DeclTy const* DC); /* Traverse the parents of a declaration @@ -388,13 +388,13 @@ class ASTVisitor std::derived_from DeclTy> requires (!std::derived_from) void - traverseParents(InfoTy& I, DeclTy* DC); + traverseParents(InfoTy& I, DeclTy const* DC); template < std::derived_from InfoTy, std::derived_from DeclTy> void - traverseParents(InfoTy& I, DeclTy* DC); + traverseParents(InfoTy& I, DeclTy const* DC); /* Generates a Unified Symbol Resolution value for a declaration. @@ -406,7 +406,7 @@ class ASTVisitor @returns true if USR generation succeeded. */ Expected> - generateUSR(const Decl* D) const; + generateUSR(Decl const* D) const; /* Generate the symbol ID for a declaration. @@ -426,7 +426,7 @@ class ASTVisitor @return true if the symbol ID could be extracted. */ bool - generateID(const Decl* D, SymbolID& id) const; + generateID(Decl const* D, SymbolID& id) const; /* Extracts the symbol ID for a declaration. @@ -439,102 +439,102 @@ class ASTVisitor @return the symbol ID for the declaration. */ SymbolID - generateID(const Decl* D) const; + generateID(Decl const* D) const; // ================================================= // Populate functions // ================================================= template DeclTy> void - populate(Info& I, bool isNew, DeclTy* D); + populate(Info& I, bool isNew, DeclTy const* D); void populate(SourceInfo& I, clang::SourceLocation loc, bool definition, bool documented); void - populate(NamespaceInfo& I, NamespaceDecl* D); + populate(NamespaceInfo& I, NamespaceDecl const* D); static void - populate(NamespaceInfo& I, TranslationUnitDecl* D); + populate(NamespaceInfo& I, TranslationUnitDecl const* D); void - populate(RecordInfo& I, CXXRecordDecl* D); + populate(RecordInfo& I, CXXRecordDecl const* D); void - populate(RecordInfo& I, ClassTemplateDecl* D); + populate(RecordInfo& I, ClassTemplateDecl const* D); void - populate(RecordInfo& I, ClassTemplateSpecializationDecl* D); + populate(RecordInfo& I, ClassTemplateSpecializationDecl const* D); void - populate(FunctionInfo& I, FunctionDecl* D); + populate(FunctionInfo& I, FunctionDecl const* D); void - populate(FunctionInfo& I, FunctionTemplateDecl* D); + populate(FunctionInfo& I, FunctionTemplateDecl const* D); void - populate(FunctionInfo& I, CXXMethodDecl* D); + populate(FunctionInfo& I, CXXMethodDecl const* D); void - populate(FunctionInfo& I, CXXConstructorDecl* D); + populate(FunctionInfo& I, CXXConstructorDecl const* D); void - populate(FunctionInfo& I, CXXDestructorDecl* D); + populate(FunctionInfo& I, CXXDestructorDecl const* D); void - populate(FunctionInfo& I, CXXConversionDecl* D); + populate(FunctionInfo& I, CXXConversionDecl const* D); void - populate(EnumInfo& I, EnumDecl* D); + populate(EnumInfo& I, EnumDecl const* D); void - populate(EnumConstantInfo& I, EnumConstantDecl* D); + populate(EnumConstantInfo& I, EnumConstantDecl const* D); void - populate(TypedefInfo& I, TypedefNameDecl* D); + populate(TypedefInfo& I, TypedefNameDecl const* D); void - populate(TypedefInfo& I, TypedefDecl* D); + populate(TypedefInfo& I, TypedefDecl const* D); void - populate(TypedefInfo& I, TypeAliasDecl* D); + populate(TypedefInfo& I, TypeAliasDecl const* D); void - populate(TypedefInfo& I, TypeAliasTemplateDecl* D); + populate(TypedefInfo& I, TypeAliasTemplateDecl const* D); void - populate(VariableInfo& I, VarDecl* D); + populate(VariableInfo& I, VarDecl const* D); void - populate(VariableInfo& I, VarTemplateDecl* D); + populate(VariableInfo& I, VarTemplateDecl const* D); void - populate(VariableInfo& I, VarTemplateSpecializationDecl* D); + populate(VariableInfo& I, VarTemplateSpecializationDecl const* D); void - populate(FieldInfo& I, FieldDecl* D); + populate(FieldInfo& I, FieldDecl const* D); void - populate(SpecializationInfo& I, ClassTemplateSpecializationDecl* D); + populate(SpecializationInfo& I, ClassTemplateSpecializationDecl const* D); void - populate(FriendInfo& I, FriendDecl* D); + populate(FriendInfo& I, FriendDecl const* D); void - populate(GuideInfo& I, CXXDeductionGuideDecl* D); + populate(GuideInfo& I, CXXDeductionGuideDecl const* D); void - populate(GuideInfo& I, FunctionTemplateDecl* D); + populate(GuideInfo& I, FunctionTemplateDecl const* D); void - populate(NamespaceAliasInfo& I, NamespaceAliasDecl* D); + populate(NamespaceAliasInfo& I, NamespaceAliasDecl const* D); void - populate(UsingInfo& I, UsingDecl* D); + populate(UsingInfo& I, UsingDecl const* D); void - populate(ConceptInfo& I, ConceptDecl* D); + populate(ConceptInfo& I, ConceptDecl const* D); /* Default function to populate the template information @@ -546,19 +546,10 @@ class ASTVisitor std::derived_from DeclTy, std::derived_from TemplateDeclTy> void - populate(TemplateInfo& Template, DeclTy* D, TemplateDeclTy* TD); + populate(TemplateInfo& Template, DeclTy const* D, TemplateDeclTy const* TD); - /* Populate the template information for a class template - - The function will populate the template parameters - depending on whether the record is a specialization. - */ - template CXXRecordDeclTy> void - populate(TemplateInfo& Template, CXXRecordDeclTy*, ClassTemplateDecl* CTD); - - void - populate(TemplateInfo& Template, ClassTemplateSpecializationDecl* D, ClassTemplateDecl* CTD); + populate(TemplateInfo& Template, ClassTemplateSpecializationDecl const* D, ClassTemplateDecl const* CTD); /* Populate the template information for a variable template @@ -567,13 +558,13 @@ class ASTVisitor */ template VarDeclTy> void - populate(TemplateInfo& Template, VarDeclTy* D, VarTemplateDecl* VTD); + populate(TemplateInfo& Template, VarDeclTy const* D, VarTemplateDecl const* VTD); template< std::derived_from DeclTy, std::derived_from TemplateDeclTy> void - populate(std::optional& Template, DeclTy* D, TemplateDeclTy* VTD) + populate(std::optional& Template, DeclTy const* D, TemplateDeclTy const* VTD) { if (!Template) { @@ -584,27 +575,28 @@ class ASTVisitor } void - populate(NoexceptInfo& I, const FunctionProtoType* FPT); + populate(NoexceptInfo& I, FunctionProtoType const* FPT); void - populate(ExplicitInfo& I, const ExplicitSpecifier& ES); + populate(ExplicitInfo& I, ExplicitSpecifier const& ES); void - populate(ExprInfo& I, const Expr* E); + populate(ExprInfo& I, Expr const* E); template void - populate(ConstantExprInfo& I, const Expr* E); + populate(ConstantExprInfo& I, Expr const* E); template void - populate(ConstantExprInfo& I, const Expr* E, const llvm::APInt& V); + populate(ConstantExprInfo& I, Expr const* E, llvm::APInt const& V); void - populate(PolymorphicValue& I, const NamedDecl* N); + populate(PolymorphicValue& I, NamedDecl const* N); void - populate(std::optional& TI, const TemplateParameterList* TPL) { + populate(std::optional& TI, TemplateParameterList const* TPL) + { if (!TI) { TI.emplace(); @@ -613,7 +605,7 @@ class ASTVisitor } void - populate(TemplateInfo& TI, const TemplateParameterList* TPL); + populate(TemplateInfo& TI, TemplateParameterList const* TPL); template Range> void @@ -644,7 +636,7 @@ class ASTVisitor void populate( std::vector>& result, - const ASTTemplateArgumentListInfo* args); + ASTTemplateArgumentListInfo const* args); template InfoTy> static @@ -690,7 +682,7 @@ class ASTVisitor toTypeInfo(QualType qt, TraversalMode mode); PolymorphicValue - toTypeInfo(QualType qt) + toTypeInfo(QualType const qt) { return toTypeInfo(qt, TraversalMode::Dependency); } @@ -704,29 +696,29 @@ class ASTVisitor toNameInfo( DeclarationName Name, std::optional TArgs = std::nullopt, - const NestedNameSpecifier* NNS = nullptr); + NestedNameSpecifier const* NNS = nullptr); template > PolymorphicValue toNameInfo( - const Decl* D, + Decl const* D, std::optional TArgs = std::nullopt, - const NestedNameSpecifier* NNS = nullptr); + NestedNameSpecifier const* NNS = nullptr); PolymorphicValue - toTArg(const TemplateArgument& A); + toTArg(TemplateArgument const& A); // Pretty-print an expression std::string - toString(const Expr* E); + toString(Expr const* E); // Pretty-print a type std::string - toString(const Type* T); + toString(Type const* T); template Integer - toInteger(const llvm::APInt& V); + toInteger(llvm::APInt const& V); /* Get the original source code in a range @@ -996,7 +988,7 @@ class ASTVisitor to get the Info object for the declaration. */ Info* - find(Decl* D); + find(Decl const* D); /* Find or traverse a declaration @@ -1027,7 +1019,7 @@ class ASTVisitor @return a pointer to the Info object. */ Info* - findOrTraverse(Decl* D) + findOrTraverse(Decl const* D) { MRDOCS_CHECK_OR(D, nullptr); if (auto* I = find(D)) @@ -1145,7 +1137,7 @@ class ASTVisitor std::same_as, InfoTypeFor_t, InfoTy>>> - upsert(DeclType* D); + upsert(DeclType const* D); }; } // clang::mrdocs diff --git a/src/lib/AST/ClangHelpers.cpp b/src/lib/AST/ClangHelpers.cpp index 529e31dd5..7e66a10cc 100644 --- a/src/lib/AST/ClangHelpers.cpp +++ b/src/lib/AST/ClangHelpers.cpp @@ -213,8 +213,8 @@ getNTTPFromExpr(const Expr* E, unsigned const Depth) return NTTPD; } -Decl* -getParent(Decl* D) +Decl const* +getParent(Decl const* D) { while((D = cast_if_present< Decl>(D->getDeclContext()))) diff --git a/src/lib/AST/ClangHelpers.hpp b/src/lib/AST/ClangHelpers.hpp index c65027195..b99c71b65 100644 --- a/src/lib/AST/ClangHelpers.hpp +++ b/src/lib/AST/ClangHelpers.hpp @@ -706,15 +706,14 @@ TypeLocToKind() user-written `Decl` will be the `std::vector` template. */ template -DeclTy* -getInstantiatedFrom(DeclTy* D) +DeclTy const* +getInstantiatedFrom(DeclTy const* D) { if (!D) { return nullptr; } - auto* decayedD = const_cast(static_cast(D)); - Decl* resultDecl = InstantiatedFromVisitor().Visit(decayedD); + Decl const* resultDecl = InstantiatedFromVisitor().Visit(D); return cast(resultDecl); } @@ -722,8 +721,8 @@ template requires std::derived_from || std::same_as> -FunctionDecl* -getInstantiatedFrom(DeclTy* D) +FunctionDecl const* +getInstantiatedFrom(DeclTy const* D) { return dyn_cast_if_present( getInstantiatedFrom(D)); @@ -733,8 +732,8 @@ template requires std::derived_from || std::same_as> -CXXRecordDecl* -getInstantiatedFrom(DeclTy* D) +CXXRecordDecl const* +getInstantiatedFrom(DeclTy const* D) { return dyn_cast_if_present( getInstantiatedFrom(D)); @@ -744,8 +743,8 @@ template requires std::derived_from || std::same_as> -VarDecl* -getInstantiatedFrom(DeclTy* D) +VarDecl const* +getInstantiatedFrom(DeclTy const* D) { return dyn_cast_if_present( getInstantiatedFrom(D)); @@ -755,8 +754,8 @@ template requires std::derived_from || std::same_as> -TypedefNameDecl* -getInstantiatedFrom(DeclTy* D) +TypedefNameDecl const* +getInstantiatedFrom(DeclTy const* D) { return dyn_cast_if_present( getInstantiatedFrom(D)); @@ -769,11 +768,11 @@ getInstantiatedFrom(DeclTy* D) */ MRDOCS_DECL AccessSpecifier -getAccess(const Decl* D); +getAccess(Decl const* D); MRDOCS_DECL QualType -getDeclaratorType(const DeclaratorDecl* DD); +getDeclaratorType(DeclaratorDecl const* DD); /** Get the NonTypeTemplateParm of an expression @@ -797,19 +796,12 @@ getDeclaratorType(const DeclaratorDecl* DD); */ MRDOCS_DECL NonTypeTemplateParmDecl const* -getNTTPFromExpr(const Expr* E, unsigned Depth); +getNTTPFromExpr(Expr const* E, unsigned Depth); // Get the parent declaration of a declaration MRDOCS_DECL -Decl* -getParent(Decl* D); - -// Get the parent declaration of a declaration -inline Decl const* -getParent(Decl const* D) { - return getParent(const_cast(D)); -} +getParent(Decl const* D); MRDOCS_DECL void diff --git a/src/lib/AST/InstantiatedFromVisitor.hpp b/src/lib/AST/InstantiatedFromVisitor.hpp index 4872997d6..f284ba637 100644 --- a/src/lib/AST/InstantiatedFromVisitor.hpp +++ b/src/lib/AST/InstantiatedFromVisitor.hpp @@ -24,16 +24,16 @@ namespace clang::mrdocs { and retrieve the original declarations from which they were instantiated. */ class InstantiatedFromVisitor - : public DeclVisitor + : public ConstDeclVisitor { public: - Decl* - VisitDecl(Decl* D) + Decl const* + VisitDecl(Decl const* D) { return D; } - FunctionDecl* + FunctionDecl const* VisitFunctionTemplateDecl(FunctionTemplateDecl const* D) { while(auto* MT = D->getInstantiatedFromMemberTemplate()) @@ -47,7 +47,7 @@ class InstantiatedFromVisitor return D->getTemplatedDecl(); } - CXXRecordDecl* + CXXRecordDecl const* VisitClassTemplateDecl(ClassTemplateDecl const* D) { while (auto* MT = D->getInstantiatedFromMemberTemplate()) @@ -61,7 +61,7 @@ class InstantiatedFromVisitor return D->getTemplatedDecl(); } - VarDecl* + VarDecl const* VisitVarTemplateDecl(VarTemplateDecl const* D) { while(auto* MT = D->getInstantiatedFromMemberTemplate()) @@ -75,7 +75,7 @@ class InstantiatedFromVisitor return D->getTemplatedDecl(); } - TypedefNameDecl* + TypedefNameDecl const* VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl const* D) { if(auto* MT = D->getInstantiatedFromMemberTemplate()) @@ -89,8 +89,8 @@ class InstantiatedFromVisitor return VisitTypedefNameDecl(D->getTemplatedDecl()); } - FunctionDecl* - VisitFunctionDecl(FunctionDecl* D) + FunctionDecl const* + VisitFunctionDecl(FunctionDecl const* D) { if (FunctionDecl const* DD = nullptr; D->isDefined(DD, false)) @@ -117,8 +117,8 @@ class InstantiatedFromVisitor return D; } - CXXRecordDecl* - VisitClassTemplatePartialSpecializationDecl(ClassTemplatePartialSpecializationDecl* D) + CXXRecordDecl const* + VisitClassTemplatePartialSpecializationDecl(ClassTemplatePartialSpecializationDecl const* D) { while (auto* MT = D->getInstantiatedFromMember()) { @@ -131,8 +131,8 @@ class InstantiatedFromVisitor return VisitClassTemplateSpecializationDecl(D); } - CXXRecordDecl* - VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl* D) + CXXRecordDecl const* + VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl const* D) { if (!D->isExplicitSpecialization()) { @@ -152,8 +152,8 @@ class InstantiatedFromVisitor return VisitCXXRecordDecl(D); } - CXXRecordDecl* - VisitCXXRecordDecl(CXXRecordDecl* D) + CXXRecordDecl const* + VisitCXXRecordDecl(CXXRecordDecl const* D) { while (MemberSpecializationInfo const* MSI = D->getMemberSpecializationInfo()) @@ -169,8 +169,8 @@ class InstantiatedFromVisitor return D; } - VarDecl* - VisitVarTemplatePartialSpecializationDecl(VarTemplatePartialSpecializationDecl* D) + VarDecl const* + VisitVarTemplatePartialSpecializationDecl(VarTemplatePartialSpecializationDecl const* D) { while(auto* MT = D->getInstantiatedFromMember()) { @@ -183,8 +183,8 @@ class InstantiatedFromVisitor return VisitVarTemplateSpecializationDecl(D); } - VarDecl* - VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl* D) + VarDecl const* + VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl const* D) { if(! D->isExplicitSpecialization()) { @@ -205,8 +205,8 @@ class InstantiatedFromVisitor return VisitVarDecl(D); } - VarDecl* - VisitVarDecl(VarDecl* D) + VarDecl const* + VisitVarDecl(VarDecl const* D) { while(MemberSpecializationInfo* MSI = D->getMemberSpecializationInfo()) @@ -220,8 +220,8 @@ class InstantiatedFromVisitor return D; } - EnumDecl* - VisitEnumDecl(EnumDecl* D) + EnumDecl const* + VisitEnumDecl(EnumDecl const* D) { while(MemberSpecializationInfo* MSI = D->getMemberSpecializationInfo()) @@ -235,16 +235,18 @@ class InstantiatedFromVisitor return D; } - TypedefNameDecl* - VisitTypedefNameDecl(TypedefNameDecl* D) + TypedefNameDecl const* + VisitTypedefNameDecl(TypedefNameDecl const* D) { - DeclContext* Context = D->getNonTransparentDeclContext(); + DeclContext const* Context = D->getNonTransparentDeclContext(); if (Context->isFileContext()) { return D; } - auto const* ContextPattern = - cast(Visit(cast(Context))); + Decl const* ContextDecl = Decl::castFromDeclContext(Context); + Decl const* ContextInstatiationContextDecl = Visit(ContextDecl); + DeclContext const* ContextPattern = + Decl::castToDeclContext(ContextInstatiationContextDecl); if (Context == ContextPattern) { return D; @@ -252,7 +254,7 @@ class InstantiatedFromVisitor for (auto lookup = ContextPattern->lookup(D->getDeclName()); NamedDecl * ND : lookup) { - if (auto* TND = dyn_cast(ND)) + if (auto const* TND = dyn_cast(ND)) { return TND; }