[clang] e90f4fc - [clang][ExprConstant] Print template arguments when describing stack frame

Takuya Shimizu via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 31 01:07:34 PDT 2023


Author: Takuya Shimizu
Date: 2023-07-31T17:05:56+09:00
New Revision: e90f4fc6acaffd216e06e47df0aee6a8b5b2d4a9

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

LOG: [clang][ExprConstant] Print template arguments when describing stack frame

This patch adds additional printing of template argument list when the described function is a template specialization.
This can be useful when handling complex template functions in constexpr evaluator.

Reviewed By: cjdb, dblaikie
Differential Revision: https://reviews.llvm.org/D154366

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/AST/ExprConstant.cpp
    clang/test/AST/Interp/literals.cpp
    clang/test/SemaCXX/builtin-align-cxx.cpp
    clang/test/SemaCXX/constant-expression-cxx11.cpp
    clang/test/SemaCXX/constant-expression-cxx14.cpp
    clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
    clang/test/SemaCXX/constexpr-frame-describe.cpp
    clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp
    clang/test/SemaCXX/cxx2b-consteval-propagate.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4b8a323093ccd5..d369af7944ebeb 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -95,6 +95,8 @@ Attribute Changes in Clang
 
 Improvements to Clang's diagnostics
 -----------------------------------
+- Clang constexpr evaluator now prints template arguments when displaying
+  template-specialization function calls.
 
 Bug Fixes in This Version
 -------------------------

diff  --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index c06ca3c405370e..c688467a2fef74 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -1950,7 +1950,8 @@ void CallStackFrame::describe(raw_ostream &Out) const {
                       cast<CXXMethodDecl>(Callee)->isInstance();
 
   if (!IsMemberCall)
-    Out << *Callee << '(';
+    Callee->getNameForDiagnostic(Out, Info.Ctx.getPrintingPolicy(),
+                                 /*Qualified=*/false);
 
   if (This && IsMemberCall) {
     if (const auto *MCE = dyn_cast_if_present<CXXMemberCallExpr>(CallExpr)) {
@@ -1975,10 +1976,13 @@ void CallStackFrame::describe(raw_ostream &Out) const {
           Info.Ctx.getLValueReferenceType(This->Designator.MostDerivedType));
       Out << ".";
     }
-    Out << *Callee << '(';
+    Callee->getNameForDiagnostic(Out, Info.Ctx.getPrintingPolicy(),
+                                 /*Qualified=*/false);
     IsMemberCall = false;
   }
 
+  Out << '(';
+
   for (FunctionDecl::param_const_iterator I = Callee->param_begin(),
        E = Callee->param_end(); I != E; ++I, ++ArgIndex) {
     if (ArgIndex > (unsigned)IsMemberCall)

diff  --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index d2b5f4a5a1901f..ad0adc23b42e40 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -502,22 +502,22 @@ namespace IncDec {
     return 1;
   }
   static_assert(uninit<int, true>(), ""); // ref-error {{not an integral constant expression}} \
-                                          // ref-note {{in call to 'uninit()'}} \
+                                          // ref-note {{in call to 'uninit<int, true>()'}} \
                                           // expected-error {{not an integral constant expression}} \
                                           // expected-note {{in call to 'uninit()'}}
 
   static_assert(uninit<int, false>(), ""); // ref-error {{not an integral constant expression}} \
-                                           // ref-note {{in call to 'uninit()'}} \
+                                           // ref-note {{in call to 'uninit<int, false>()'}} \
                                            // expected-error {{not an integral constant expression}} \
                                            // expected-note {{in call to 'uninit()'}}
 
   static_assert(uninit<float, true>(), ""); // ref-error {{not an integral constant expression}} \
-                                            // ref-note {{in call to 'uninit()'}} \
+                                            // ref-note {{in call to 'uninit<float, true>()'}} \
                                             // expected-error {{not an integral constant expression}} \
                                             // expected-note {{in call to 'uninit()'}}
 
   static_assert(uninit<float, false>(), ""); // ref-error {{not an integral constant expression}} \
-                                             // ref-note {{in call to 'uninit()'}} \
+                                             // ref-note {{in call to 'uninit<float, false>()'}} \
                                              // expected-error {{not an integral constant expression}} \
                                              // expected-note {{in call to 'uninit()'}}
 

diff  --git a/clang/test/SemaCXX/builtin-align-cxx.cpp b/clang/test/SemaCXX/builtin-align-cxx.cpp
index 8ecd513f439c2c..d18bc2bf665511 100644
--- a/clang/test/SemaCXX/builtin-align-cxx.cpp
+++ b/clang/test/SemaCXX/builtin-align-cxx.cpp
@@ -137,26 +137,26 @@ static_assert(!wrap_is_aligned(wrap_align_down(wrap_align_up(22, 16), 32), 64),
 constexpr long const_value(long l) { return l; }
 // Check some invalid values during constant-evaluation
 static_assert(wrap_align_down(1, const_value(-1)), ""); // expected-error{{not an integral constant expression}}
-// expected-note at -1{{in call to 'wrap_align_down(1, -1)'}}
+// expected-note at -1{{in call to 'wrap_align_down<int>(1, -1)'}}
 static_assert(wrap_align_up(1, const_value(-2)), ""); // expected-error{{not an integral constant expression}}
-// expected-note at -1{{in call to 'wrap_align_up(1, -2)'}}
+// expected-note at -1{{in call to 'wrap_align_up<int>(1, -2)'}}
 static_assert(wrap_is_aligned(1, const_value(-3)), ""); // expected-error{{not an integral constant expression}}
-// expected-note at -1{{in call to 'wrap_is_aligned(1, -3)'}}
+// expected-note at -1{{in call to 'wrap_is_aligned<int>(1, -3)'}}
 static_assert(wrap_align_down(1, const_value(17)), ""); // expected-error{{not an integral constant expression}}
-// expected-note at -1{{in call to 'wrap_align_down(1, 17)'}}
+// expected-note at -1{{in call to 'wrap_align_down<int>(1, 17)'}}
 static_assert(wrap_align_up(1, const_value(18)), ""); // expected-error{{not an integral constant expression}}
-// expected-note at -1{{in call to 'wrap_align_up(1, 18)'}}
+// expected-note at -1{{in call to 'wrap_align_up<int>(1, 18)'}}
 static_assert(wrap_is_aligned(1, const_value(19)), ""); // expected-error{{not an integral constant expression}}
-// expected-note at -1{{in call to 'wrap_is_aligned(1, 19)'}}
+// expected-note at -1{{in call to 'wrap_is_aligned<int>(1, 19)'}}
 
 // Check invalid values for smaller types:
 static_assert(wrap_align_down(static_cast<short>(1), const_value(1 << 20)), ""); // expected-error{{not an integral constant expression}}
-// expected-note at -1{{in call to 'wrap_align_down(1, 1048576)'}}
+// expected-note at -1{{in call to 'wrap_align_down<short>(1, 1048576)'}}
 // Check invalid boolean type
 static_assert(wrap_align_up(static_cast<int>(1), const_value(1ull << 33)), ""); // expected-error{{not an integral constant expression}}
-// expected-note at -1{{in call to 'wrap_align_up(1, 8589934592)'}}
+// expected-note at -1{{in call to 'wrap_align_up<int>(1, 8589934592)'}}
 static_assert(wrap_is_aligned(static_cast<char>(1), const_value(1 << 22)), ""); // expected-error{{not an integral constant expression}}
-// expected-note at -1{{in call to 'wrap_is_aligned(1, 4194304)'}}
+// expected-note at -1{{in call to 'wrap_is_aligned<char>(1, 4194304)'}}
 
 // Check invalid boolean type
 static_assert(wrap_align_up(static_cast<bool>(1), const_value(1 << 21)), ""); // expected-error{{not an integral constant expression}}

diff  --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp
index 2a92eb6a3af4ea..89d1b3ea6de05e 100644
--- a/clang/test/SemaCXX/constant-expression-cxx11.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp
@@ -1781,7 +1781,7 @@ namespace Void {
   static_assert(get(arr, 1) == 1, "");
   static_assert(get(arr, 4) == 4, "");
   static_assert(get(arr, 0) == 4, ""); // expected-error{{not an integral constant expression}} \
-  // expected-note{{in call to 'get(arr, 0)'}}
+  // expected-note{{in call to 'get<const int, 5UL>(arr, 0)'}}
 }
 
 namespace std { struct type_info; }

diff  --git a/clang/test/SemaCXX/constant-expression-cxx14.cpp b/clang/test/SemaCXX/constant-expression-cxx14.cpp
index cf242aaf90aa63..a43ee74e3df56c 100644
--- a/clang/test/SemaCXX/constant-expression-cxx14.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx14.cpp
@@ -1039,7 +1039,7 @@ const char Cs[] = {'a', 'b'}; // expected-note 2{{declared here}}
 void foo() __attribute__((enable_if(sum(Cs) == 'a' + 'b', "")));
 void run() { foo(); }
 
-static_assert(sum(Cs) == 'a' + 'b', ""); // expected-error{{not an integral constant expression}} expected-note{{in call to 'sum(Cs)'}}
+static_assert(sum(Cs) == 'a' + 'b', ""); // expected-error{{not an integral constant expression}} expected-note{{in call to 'sum<2>(Cs)'}}
 constexpr int S = sum(Cs); // expected-error{{must be initialized by a constant expression}} expected-note{{in call}}
 }
 

diff  --git a/clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp b/clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
index 36ced2e298c691..a6ebe0572d063b 100644
--- a/clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
+++ b/clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
@@ -117,11 +117,11 @@ void test_partially_initialized() {
 
   constexpr pad pir{4, 4};
   // expected-error at +2 {{constexpr variable 'piw' must be initialized by a constant expression}}
-  // expected-note at +1 {{in call to 'bit_cast(pir)'}}
+  // expected-note at +1 {{in call to 'bit_cast<no_pad, pad>(pir)'}}
   constexpr int piw = bit_cast<no_pad>(pir).x;
 
   // expected-error at +2 {{constexpr variable 'bad' must be initialized by a constant expression}}
-  // expected-note at +1 {{in call to 'bit_cast(pir)'}}
+  // expected-note at +1 {{in call to 'bit_cast<no_pad, pad>(pir)'}}
   constexpr no_pad bad = bit_cast<no_pad>(pir);
 
   constexpr pad fine = bit_cast<pad>(no_pad{1, 2, 3, 4, 5});

diff  --git a/clang/test/SemaCXX/constexpr-frame-describe.cpp b/clang/test/SemaCXX/constexpr-frame-describe.cpp
index 3a87232ebeabe2..7b832d9a4b4a18 100644
--- a/clang/test/SemaCXX/constexpr-frame-describe.cpp
+++ b/clang/test/SemaCXX/constexpr-frame-describe.cpp
@@ -58,3 +58,24 @@ struct D {
 constexpr D d{};
 static_assert(d.c->b.a->foo() == 1); // expected-error {{constant expression}} \
                                         expected-note {{in call to 'd.c->b.a->foo()'}}
+
+template <typename T>
+struct Bar {
+  template <typename U>
+  constexpr int fail1() const { return 1 / 0; } // expected-warning {{division by zero}} \
+                                                // expected-note {{division by zero}}
+  template <typename U, int num>
+  constexpr int fail2() const { return 1 / 0; } // expected-warning {{division by zero}} \
+                                                // expected-note {{division by zero}}
+  template <typename ...Args>
+  constexpr int fail3(Args... args) const { return 1 / 0; } // expected-warning {{division by zero}} \
+                                                // expected-note {{division by zero}}
+};
+
+constexpr Bar<int> bar;
+static_assert(bar.fail1<int>()); // expected-error {{constant expression}} \
+                                 // expected-note {{in call to 'bar.fail1<int>()'}}
+static_assert(bar.fail2<int*, 42>()); // expected-error {{constant expression}} \
+                                      // expected-note {{in call to 'bar.fail2<int *, 42>()'}}
+static_assert(bar.fail3(3, 4UL, bar, &bar)); // expected-error {{constant expression}} \
+                                             // expected-note {{in call to 'bar.fail3<int, unsigned long, Bar<int>, const Bar<int> *>(3, 4, {}, &bar)'}}

diff  --git a/clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp b/clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp
index a85d44f40926fb..2178154b2e363f 100644
--- a/clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp
+++ b/clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp
@@ -88,11 +88,11 @@ constexpr int f = stack_array<1025>(); // expected-error {{constexpr variable 'f
 void ohno() {
   int bar[stack_array<1024>()];
   int foo[stack_array<1025>()]; // expected-warning {{variable length arrays are a C99 feature}} \
-                                // expected-note {{in call to 'stack_array()'}}
+                                // expected-note {{in call to 'stack_array<1025>()'}}
 
   constexpr int foo[stack_array<1025>()]; // expected-warning {{variable length arrays are a C99 feature}} \
                                           // expected-error {{constexpr variable cannot have non-literal type 'const int[stack_array<1025>()]'}} \
-                                          // expected-note {{in call to 'stack_array()'}}
+                                          // expected-note {{in call to 'stack_array<1025>()'}}
 }
 
 }

diff  --git a/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp b/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
index 333b98d30b75e7..c5eb7f13932750 100644
--- a/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
+++ b/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
@@ -45,7 +45,7 @@ constexpr T hh() {           // hh<int> is an immediate function
 }
 
 int i = hh<int>(); // expected-error {{call to immediate function 'examples::hh<int>' is not a constant expression}} \
-                   // expected-note {{in call to 'hh()'}}
+                   // expected-note {{in call to 'hh<int>()'}}
 
 struct A {
   int x;
@@ -180,7 +180,7 @@ constexpr S immediate(auto) {
 
 void test_runtime() {
     (void)immediate(0); // expected-error {{call to immediate function 'Aggregate::immediate<int>' is not a constant expression}} \
-                        // expected-note {{in call to 'immediate(0)'}}
+                        // expected-note {{in call to 'immediate<int>(0)'}}
 }
 consteval int f(int i) {
     return i;


        


More information about the cfe-commits mailing list