[PATCH] D151033: [clang][AST] TextNodeDumper should not evaluate the initializer of constexpr variable declaration when it has a dependent type

Takuya Shimizu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun May 21 04:47:36 PDT 2023


hazohelet updated this revision to Diff 524095.
hazohelet retitled this revision from "[clang][AST] TextNodeDumper should not evaluate the initializer of constexpr variable template definition" to "[clang][AST] TextNodeDumper should not evaluate the initializer of constexpr variable declaration when it has a dependent type".
hazohelet edited the summary of this revision.
hazohelet added a comment.

- Evaluate the initializer if the type of the declaration is not a dependent type
- Add a relevant test
- Update release note for this change


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D151033/new/

https://reviews.llvm.org/D151033

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/AST/TextNodeDumper.cpp
  clang/test/AST/ast-dump-decl.cpp


Index: clang/test/AST/ast-dump-decl.cpp
===================================================================
--- clang/test/AST/ast-dump-decl.cpp
+++ clang/test/AST/ast-dump-decl.cpp
@@ -818,3 +818,38 @@
 // CHECK:       `-TextComment
 // CHECK: VarDecl {{.*}} Test 'int' extern
 // CHECK-NOT: FullComment
+
+namespace TestConstexprVariableTemplateWithInitializer {
+  template<typename T> constexpr T foo{};
+  // CHECK:      VarTemplateDecl 0x{{.+}} <{{.+}}:[[@LINE-1]]:3, col:40> col:36 foo
+  // CHECK-NEXT: |-TemplateTypeParmDecl 0x{{.+}} <col:12, col:21> col:21 referenced typename depth 0 index 0 T
+  // CHECK-NEXT: `-VarDecl 0x{{.+}} <col:24, col:40> col:36 foo 'const T' constexpr listinit
+  // CHECK-NEXT:  `-InitListExpr 0x{{.+}} <col:39, col:40> 'void'
+
+  template<typename T> constexpr int val{42};
+  // CHECK:      VarTemplateDecl 0x{{.+}} <{{.+}}:[[@LINE-1]]:3, col:44> col:38 val
+  // CHECK-NEXT: |-TemplateTypeParmDecl 0x{{.+}} <col:12, col:21> col:21 typename depth 0 index 0 T
+  // CHECK-NEXT: `-VarDecl 0x{{.+}} <col:24, col:44> col:38 val 'const int' constexpr listinit
+  // CHECK-NEXT:  |-value: Int 42
+  // CHECK-NEXT:  `-InitListExpr 0x{{.+}} <col:41, col:44> 'int'
+
+  template <typename _Tp>
+  struct in_place_type_t {
+    explicit in_place_type_t() = default;
+  };
+
+  template <typename _Tp>
+  inline constexpr in_place_type_t<_Tp> in_place_type{};
+  // CHECK:     -VarTemplateDecl 0x{{.+}} <line:[[@LINE-2]]:3, line:[[@LINE-1]]:55> col:41 in_place_type
+  // CHECK-NEXT: |-TemplateTypeParmDecl 0x{{.+}} <line:[[@LINE-3]]:13, col:22> col:22 referenced typename depth 0 index 0 _Tp
+  // CHECK-NEXT: `-VarDecl 0x{{.+}} <line:[[@LINE-3]]:3, col:55> col:41 in_place_type 'const in_place_type_t<_Tp>':'const in_place_type_t<_Tp>' inline constexpr listinit
+  // CHECK-NEXT:  `-InitListExpr 0x{{.+}} <col:54, col:55> 'void'
+
+  template <typename T> constexpr T call_init(0);
+  // CHECK:     -VarTemplateDecl 0x{{.+}} <line:[[@LINE-1]]:3, col:48> col:37 call_init
+  // CHECK-NEXT: |-TemplateTypeParmDecl 0x{{.+}} <col:13, col:22> col:22 referenced typename depth 0 index 0 T
+  // CHECK-NEXT: `-VarDecl 0x{{.+}} <col:25, col:48> col:37 call_init 'const T' constexpr callinit
+  // CHECK-NEXT:  `-ParenListExpr 0x{{.+}} <col:46, col:48> 'NULL TYPE'
+  // CHECK-NEXT:   `-IntegerLiteral 0x{{.+}} <col:47> 'int' 0
+
+}
Index: clang/lib/AST/TextNodeDumper.cpp
===================================================================
--- clang/lib/AST/TextNodeDumper.cpp
+++ clang/lib/AST/TextNodeDumper.cpp
@@ -1821,7 +1821,8 @@
   if (D->hasInit()) {
     const Expr *E = D->getInit();
     // Only dump the value of constexpr VarDecls for now.
-    if (E && !E->isValueDependent() && D->isConstexpr()) {
+    if (E && !E->isValueDependent() && D->isConstexpr() &&
+        !D->getType()->isDependentType()) {
       const APValue *Value = D->evaluateValue();
       if (Value)
         AddChild("value", [=] { Visit(*Value, E->getType()); });
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -418,6 +418,8 @@
 - Propagate the value-dependent bit for VAArgExpr. Fixes a crash where a
   __builtin_va_arg call has invalid arguments.
   (`#62711 <https://github.com/llvm/llvm-project/issues/62711>`_).
+- Clang `TextNodeDumper` enabled through `-ast-dump` flag no longer evaluates the
+  initializer of constexpr `VarDecl` if the declaration has a dependent type.
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151033.524095.patch
Type: text/x-patch
Size: 3582 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230521/4b29b762/attachment-0001.bin>


More information about the cfe-commits mailing list