[clang] [clang-tools-extra] [clang] Add a valid begin source location for abbreviated function templates (PR #174723)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 7 12:10:21 PST 2026
================
@@ -1104,6 +1104,19 @@ class FunctionTemplateDecl : public RedeclarableTemplateDecl {
static FunctionTemplateDecl *CreateDeserialized(ASTContext &C,
GlobalDeclID ID);
+ SourceRange getSourceRange() const override LLVM_READONLY {
+ SourceLocation BeginLoc = getTemplateParameters()->getTemplateLoc();
+ if (BeginLoc.isInvalid() && isAbbreviated()) {
+ // The BeginLoc of FunctionTemplateDecls is derived from the template
+ // keyword. But "pure" abbreviated templates do not use the template
+ // keyword. Hence the BeginLoc is invalid. Therefore just use the
+ // beginning of the templated declaration instead.
+ BeginLoc = getTemplatedDecl()->getBeginLoc();
+ }
+
+ return SourceRange(BeginLoc, TemplatedDecl->getSourceRange().getEnd());
+ }
+
----------------
tcottin wrote:
I added a new commit which sets the begin and end source locations when creating the template parameter list in `Sema::ActOnFinishFunctionDeclarationDeclarator`.
I also think this is the better place in general.
E.g. my initial change did not handle the `err_template_linkage` error (see the change in `clang/test/CXX/temp/temp.pre/p6.cpp`).
But with the new change I am kind of misusing the attributes of `TemplateParameterList`.
E.g. `TemplateLoc` is the begin location of the function declaration and `RAngleLoc` is the end of the function declaration.
Also lambda functions are not handled by this change, meaning they have no valid begin source location again.
Do you have some suggestions on how to proceed?
https://github.com/llvm/llvm-project/pull/174723
More information about the cfe-commits
mailing list