[PATCH] D154861: [clang][AST] Propagate the contains-errors bit to DeclRefExpr from VarDecl's initializer.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 10 09:35:03 PDT 2023


hokein created this revision.
hokein added reviewers: aaron.ballman, shafik, sammccall.
Herald added a project: All.
hokein requested review of this revision.
Herald added a project: clang.

Similar to the https://reviews.llvm.org/D86048 (it only sets the bit for C++
code), we propagate the contains-errors bit for C-code path.

Fixes https://github.com/llvm/llvm-project/issues/50236
Fixes https://github.com/llvm/llvm-project/issues/50243
Fixes https://github.com/llvm/llvm-project/issues/48636
Fixes https://github.com/llvm/llvm-project/issues/50320


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154861

Files:
  clang/lib/AST/ComputeDependence.cpp
  clang/test/AST/ast-dump-recovery.c
  clang/test/SemaCXX/cxx11-crashes.cpp


Index: clang/test/SemaCXX/cxx11-crashes.cpp
===================================================================
--- clang/test/SemaCXX/cxx11-crashes.cpp
+++ clang/test/SemaCXX/cxx11-crashes.cpp
@@ -65,7 +65,7 @@
   struct S {}; // expected-note 3{{candidate}}
   void f() {
     S s(1, 2, 3); // expected-error {{no matching}}
-    for (auto x : s) { // expected-error {{invalid range expression of}}
+    for (auto x : s) {
       // We used to attempt to evaluate the initializer of this variable,
       // and crash because it has an undeduced type.
       const int &n(x);
Index: clang/test/AST/ast-dump-recovery.c
===================================================================
--- clang/test/AST/ast-dump-recovery.c
+++ clang/test/AST/ast-dump-recovery.c
@@ -126,3 +126,25 @@
   // CHECK-NEXT:   `-RecoveryExpr {{.*}} '<dependent type>'
   sizeof array / sizeof foo(undef);
 }
+
+// No crash on DeclRefExpr that refers to ValueDecl with invalid initializers.
+void test7() {
+  int b[] = {""()};
+
+  // CHECK:      CStyleCastExpr {{.*}} 'unsigned int' contains-errors
+  // CHECK-NEXT: | `-DeclRefExpr {{.*}} 'int[]' contains-errors
+  (unsigned) b; // GH50236
+
+  // CHECK:      BinaryOperator {{.*}} '<dependent type>' contains-errors '+'
+  // CHECK-NEXT: |-DeclRefExpr {{.*}} 'int[]' contains-errors
+  // CHECK-NEXT: `-IntegerLiteral {{.*}}
+  b + 1; // GH50243
+
+  // CHECK:      CallExpr {{.*}} '<dependent type>' contains-errors
+  // CHECK-NEXT: |-DeclRefExpr {{.*}} 'int ()' Function
+  // CHECK-NEXT: `-DeclRefExpr {{.*}} 'int[]' contains-errors
+  return c(b); // GH48636
+}
+int test8_GH50320_b[] = {""()};
+// CHECK: ArraySubscriptExpr {{.*}} 'int' contains-errors lvalue
+int test8 = test_8GH50320_b[0];
\ No newline at end of file
Index: clang/lib/AST/ComputeDependence.cpp
===================================================================
--- clang/lib/AST/ComputeDependence.cpp
+++ clang/lib/AST/ComputeDependence.cpp
@@ -489,7 +489,7 @@
   // more bullets here that we handle by treating the declaration as having a
   // dependent type if they involve a placeholder type that can't be deduced.]
   if (Type->isDependentType())
-    return Deps | ExprDependence::TypeValueInstantiation;
+    Deps |= ExprDependence::TypeValueInstantiation;
   else if (Type->isInstantiationDependentType())
     Deps |= ExprDependence::Instantiation;
 
@@ -525,12 +525,14 @@
   //   - it names a potentially-constant variable that is initialized with an
   //     expression that is value-dependent
   if (const auto *Var = dyn_cast<VarDecl>(Decl)) {
+    if (const Expr *Init = Var->getAnyInitializer();
+        Init && Init->containsErrors())
+      Deps |= ExprDependence::Error;
+
     if (Var->mightBeUsableInConstantExpressions(Ctx)) {
       if (const Expr *Init = Var->getAnyInitializer()) {
         if (Init->isValueDependent())
           Deps |= ExprDependence::ValueInstantiation;
-        if (Init->containsErrors())
-          Deps |= ExprDependence::Error;
       }
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154861.538699.patch
Type: text/x-patch
Size: 3010 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230710/9fe4572d/attachment.bin>


More information about the cfe-commits mailing list