[clang] [clang-tools-extra] [Clang][AST] Introduce `ExplicitInstantiationDecl` to preserve source info and fix diagnostic locations (PR #191658)
Matheus Izvekov via cfe-commits
cfe-commits at lists.llvm.org
Sun Apr 12 12:35:26 PDT 2026
================
@@ -1784,3 +1784,41 @@ const Decl &clang::adjustDeclToTemplate(const Decl &D) {
// FIXME: Adjust alias templates?
return D;
}
+
+void ExplicitInstantiationDecl::anchor() {}
+
+ExplicitInstantiationDecl *ExplicitInstantiationDecl::Create(
+ ASTContext &C, DeclContext *DC, NamedDecl *Specialization,
+ SourceLocation ExternLoc, SourceLocation TemplateLoc,
+ SourceLocation TagKWLoc, NestedNameSpecifierLoc QualifierLoc,
+ const ASTTemplateArgumentListInfo *ArgsAsWritten, SourceLocation NameLoc,
+ TypeSourceInfo *TypeAsWritten, TemplateSpecializationKind TSK) {
+ return new (C, DC) ExplicitInstantiationDecl(
+ DC, Specialization, ExternLoc, TemplateLoc, TagKWLoc, QualifierLoc,
+ ArgsAsWritten, NameLoc, TypeAsWritten, TSK);
+}
+
+ExplicitInstantiationDecl *
+ExplicitInstantiationDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
+ return new (C, ID) ExplicitInstantiationDecl(EmptyShell());
+}
+
+SourceLocation ExplicitInstantiationDecl::getEndLoc() const {
+ // Pick the rightmost location among the name, template args, and type.
+ // For postfix types (arrays, function pointers) and function templates,
+ // the TypeLoc extends past the name. Cf. DeclaratorDecl::getSourceRange().
+ SourceLocation End = NameLoc;
+ if (TemplateArgsAsWritten && TemplateArgsAsWritten->RAngleLoc > End)
+ End = TemplateArgsAsWritten->RAngleLoc;
+ if (TypeAsWritten) {
+ SourceLocation TypeEnd = TypeAsWritten->getTypeLoc().getEndLoc();
+ if (TypeEnd.isValid() && TypeEnd > End)
+ End = TypeEnd;
+ }
----------------
mizvekov wrote:
Not many places rely on this assumption that you can order SourceLocations, because that only works when they have the same FileID.
Formally, this could be composed from token from different files, even if it's not in any way common practice.
I think we should be able to quickly tell where the end location is supposed to come from, based on the kind of template is being instantiated.
https://github.com/llvm/llvm-project/pull/191658
More information about the cfe-commits
mailing list