diff options
author | Krystian Stasiowski <sdkrystian@gmail.com> | 2024-05-07 14:45:52 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-07 14:45:52 -0400 |
commit | 7115ed0fff027b65fa76fdfae215ed1382ed1473 (patch) | |
tree | 05fcb20f501954053c368ff5266a8fc446638d4c /clang/tools | |
parent | 1a2a1fbd7c03381fe5e4f459f7081bef13366ef4 (diff) | |
download | llvm-7115ed0fff027b65fa76fdfae215ed1382ed1473.zip llvm-7115ed0fff027b65fa76fdfae215ed1382ed1473.tar.gz llvm-7115ed0fff027b65fa76fdfae215ed1382ed1473.tar.bz2 |
[Clang] Unify interface for accessing template arguments as written for class/variable template specializations (#81642)
Our current method of storing the template arguments as written for
`(Class/Var)Template(Partial)SpecializationDecl` suffers from a number
of flaws:
- We use `TypeSourceInfo` to store `TemplateArgumentLocs` for class
template/variable template partial/explicit specializations. For
variable template specializations, this is a rather unintuitive hack (as
we store a non-type specialization as a type). Moreover, we don't ever
*need* the type as written -- in almost all cases, we only want the
template arguments (e.g. in tooling use-cases).
- The template arguments as written are stored in a number of redundant
data members. For example, `(Class/Var)TemplatePartialSpecialization`
have their own `ArgsAsWritten` member that stores an
`ASTTemplateArgumentListInfo` (the template arguments).
`VarTemplateSpecializationDecl` has yet _another_ redundant member
"`TemplateArgsInfo`" that also stores an `ASTTemplateArgumentListInfo`.
This patch eliminates all
`(Class/Var)Template(Partial)SpecializationDecl` members which store the
template arguments as written, and turns the `ExplicitInfo` member into
a `llvm::PointerUnion<const ASTTemplateArgumentListInfo*,
ExplicitInstantiationInfo*>` (to avoid unnecessary allocations when the
declaration isn't an explicit instantiation). The template arguments as
written are now accessed via `getTemplateArgsWritten` in all cases.
The "most breaking" change is to AST Matchers, insofar that `hasTypeLoc`
will no longer match class template specializations (since they no
longer store the type as written).
Diffstat (limited to 'clang/tools')
-rw-r--r-- | clang/tools/libclang/CIndex.cpp | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index b845a38..60241af 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -743,14 +743,10 @@ bool CursorVisitor::VisitClassTemplateSpecializationDecl( } // Visit the template arguments used in the specialization. - if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { - TypeLoc TL = SpecType->getTypeLoc(); - if (TemplateSpecializationTypeLoc TSTLoc = - TL.getAs<TemplateSpecializationTypeLoc>()) { - for (unsigned I = 0, N = TSTLoc.getNumArgs(); I != N; ++I) - if (VisitTemplateArgumentLoc(TSTLoc.getArgLoc(I))) - return true; - } + if (const auto *ArgsWritten = D->getTemplateArgsAsWritten()) { + for (const TemplateArgumentLoc &Arg : ArgsWritten->arguments()) + if (VisitTemplateArgumentLoc(Arg)) + return true; } return ShouldVisitBody && VisitCXXRecordDecl(D); @@ -5659,16 +5655,19 @@ CXString clang_getCursorDisplayName(CXCursor C) { if (const ClassTemplateSpecializationDecl *ClassSpec = dyn_cast<ClassTemplateSpecializationDecl>(D)) { - // If the type was explicitly written, use that. - if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) - return cxstring::createDup(TSInfo->getType().getAsString(Policy)); - SmallString<128> Str; llvm::raw_svector_ostream OS(Str); OS << *ClassSpec; - printTemplateArgumentList( - OS, ClassSpec->getTemplateArgs().asArray(), Policy, - ClassSpec->getSpecializedTemplate()->getTemplateParameters()); + // If the template arguments were written explicitly, use them.. + if (const auto *ArgsWritten = ClassSpec->getTemplateArgsAsWritten()) { + printTemplateArgumentList( + OS, ArgsWritten->arguments(), Policy, + ClassSpec->getSpecializedTemplate()->getTemplateParameters()); + } else { + printTemplateArgumentList( + OS, ClassSpec->getTemplateArgs().asArray(), Policy, + ClassSpec->getSpecializedTemplate()->getTemplateParameters()); + } return cxstring::createDup(OS.str()); } |