[clang] [clang-tools-extra] [clang] Add a valid begin source location for abbreviated function templates (PR #174723)

Younan Zhang via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 9 04:18:51 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());
+  }
+
----------------
zyn0217 wrote:

(Sorry I missed your comment)

For lambdas, I think you will have some luck in `SemaLambda.cpp:getGenericLambdaTemplateParameterList`, which should be first called from `Sema::ActOnLambdaClosureParameters`.

> But with the new change I am kind of misusing the attributes of TemplateParameterList.

I think they are just fine? It's already marked as 'invented' so it might also be reasonable to extend the meaning of TemplateLoc and AngleLoc.

@cor3ntin in case you have any thoughts


https://github.com/llvm/llvm-project/pull/174723


More information about the cfe-commits mailing list