[clang] 256a0b2 - [clang] Correct source locations for instantiations of function templates.

Tom Honermann via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 18 12:52:05 PDT 2023


Author: Tom Honermann
Date: 2023-09-18T12:51:45-07:00
New Revision: 256a0b298c68b89688b80350b034daf2f7785b67

URL: https://github.com/llvm/llvm-project/commit/256a0b298c68b89688b80350b034daf2f7785b67
DIFF: https://github.com/llvm/llvm-project/commit/256a0b298c68b89688b80350b034daf2f7785b67.diff

LOG: [clang] Correct source locations for instantiations of function templates.

This change corrects some cases where the source location for an
instantiated specialization of a function template or a member function
of a class template was assigned the location of a non-defining
declaration rather than the location of the definition the
specialization was instantiated from.

Fixes https://github.com/llvm/llvm-project/issues/26057

Reviewed By: cor3ntin

Differential Revision: https://reviews.llvm.org/D64087

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
    clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
    clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
    clang/test/SemaCXX/member-init.cpp
    clang/test/SemaTemplate/virtual-member-functions.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 500f9c9a0cda741..7e964f6eb435bb0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -291,6 +291,12 @@ Bug Fixes to C++ Support
   pointers on win32.
   (`#62594 <https://github.com/llvm/llvm-project/issues/62594>`_)
 
+- Fixed some cases where the source location for an instantiated specialization
+  of a function template or a member function of a class template was assigned
+  the location of a non-defining declaration rather than the location of the
+  definition the specialization was instantiated from.
+  (`#26057 <https://github.com/llvm/llvm-project/issues/26057>`_`)
+
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
 - Fixed an import failure of recursive friend class template.

diff  --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 37a7d6204413a38..fa839e9b71a3cf9 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -4990,8 +4990,10 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
   // unimported module.
   Function->setVisibleDespiteOwningModule();
 
-  // Copy the inner loc start from the pattern.
+  // Copy the source locations from the pattern.
+  Function->setLocation(PatternDecl->getLocation());
   Function->setInnerLocStart(PatternDecl->getInnerLocStart());
+  Function->setRangeEnd(PatternDecl->getEndLoc());
 
   EnterExpressionEvaluationContext EvalContext(
       *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);

diff  --git a/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp b/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
index b595825e1750adc..252860bfc4de077 100644
--- a/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
+++ b/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
@@ -209,10 +209,10 @@ bool operator==(e *, int *) = default; // expected-error{{must have at least one
 
 namespace p2085_2 {
 template <class T> struct S6 {
-  // expected-error at +2{{found 'const int &'}}
-  // expected-error at +1{{found 'const float &'}}
   bool operator==(T const &) const;
 };
+// expected-error at +2{{found 'const int &'}}
+// expected-error at +1{{found 'const float &'}}
 template <class T> bool S6<T>::operator==(T const &) const = default;
 
 template struct S6<int>; // expected-note{{S6<int>::operator==' requested}}

diff  --git a/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp b/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
index c5eb7f139327505..def3bbb8adf0fce 100644
--- a/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
+++ b/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
@@ -102,14 +102,14 @@ constexpr int f(T t) {
 
 namespace forward_declare_consteval{
 template <typename T>
-constexpr int f(T t);  // expected-note {{'f<int>' defined here}}
+constexpr int f(T t);
 
 auto a = &f<char>;
 auto b = &f<int>; // expected-error {{immediate function 'f<int>' used before it is defined}} \
                   // expected-note {{in instantiation of function template specialization}}
 
 template <typename T>
-constexpr int f(T t) {
+constexpr int f(T t) { // expected-note {{'f<int>' defined here}}
     return id(t); // expected-note {{'f<int>' is an immediate function because its body contains a call to a consteval function 'id' and that call is not a constant expression}}
 }
 }

diff  --git a/clang/test/SemaCXX/member-init.cpp b/clang/test/SemaCXX/member-init.cpp
index e98a66ca9cab9f4..087561e679f4eae 100644
--- a/clang/test/SemaCXX/member-init.cpp
+++ b/clang/test/SemaCXX/member-init.cpp
@@ -164,11 +164,11 @@ struct A {
 
 namespace explicit_instantiation {
 template<typename T> struct X {
-  X(); // expected-note {{in instantiation of default member initializer 'explicit_instantiation::X<float>::n' requested here}}
+  X();
   int n = T::error; // expected-error {{type 'float' cannot be used prior to '::' because it has no members}}
 };
 template struct X<int>; // ok
-template<typename T> X<T>::X() {}
+template<typename T> X<T>::X() {} // expected-note {{in instantiation of default member initializer 'explicit_instantiation::X<float>::n' requested here}}
 template struct X<float>; // expected-note {{in instantiation of member function 'explicit_instantiation::X<float>::X' requested here}}
 }
 
@@ -197,3 +197,15 @@ void foo(T v) {
 }
 template void foo(int);
 }
+
+namespace GH26057 {
+template<typename T>
+struct S {
+  S();
+  int dm = T::error; // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
+};
+template<typename T>
+S<T>::S() = default; // expected-note {{in instantiation of default member initializer 'GH26057::S<int>::dm' requested here}} \
+                     // expected-note {{in evaluation of exception specification for 'GH26057::S<int>::S' needed here}}
+template struct S<int>; // expected-note {{in instantiation of member function 'GH26057::S<int>::S' requested here}}
+}

diff  --git a/clang/test/SemaTemplate/virtual-member-functions.cpp b/clang/test/SemaTemplate/virtual-member-functions.cpp
index cc4d51e7dc43fd1..4e505e53986a37a 100644
--- a/clang/test/SemaTemplate/virtual-member-functions.cpp
+++ b/clang/test/SemaTemplate/virtual-member-functions.cpp
@@ -7,10 +7,10 @@
 
 namespace PR5557 {
 template <class T> struct A {
-  A(); // expected-note{{instantiation}}
+  A();
   virtual int a(T x);
 };
-template<class T> A<T>::A() {}
+template<class T> A<T>::A() {} // expected-note{{instantiation}}
 
 template<class T> int A<T>::a(T x) { 
   return *x; // expected-error{{requires pointer operand}}
@@ -33,10 +33,10 @@ void X<int>::f() { }
 namespace PR5557_dtor {
 template <class T> struct A {
   A(); // Don't have an implicit constructor.
-  ~A(); // expected-note{{instantiation}}
+  ~A();
   virtual int a(T x);
 };
-template<class T> A<T>::~A() {}
+template<class T> A<T>::~A() {} // expected-note{{instantiation}}
 
 template<class T> int A<T>::a(T x) { 
   return *x; // expected-error{{requires pointer operand}}


        


More information about the cfe-commits mailing list