[PATCH] D137386: [clang][Interp] Reject invalid declarations and expressions

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 25 03:53:19 PST 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGf4a6842c5a4d: [clang][Interp] Reject invalid declarations and expressions (authored by tbaeder).

Changed prior to commit:
  https://reviews.llvm.org/D137386?vs=474727&id=492054#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137386

Files:
  clang/lib/AST/Interp/ByteCodeExprGen.cpp
  clang/test/AST/Interp/functions.cpp


Index: clang/test/AST/Interp/functions.cpp
===================================================================
--- clang/test/AST/Interp/functions.cpp
+++ clang/test/AST/Interp/functions.cpp
@@ -84,3 +84,18 @@
   return 5;
 }
 static_assert(a() == 5, "");
+
+constexpr int invalid() {
+  // Invalid expression in visit().
+  while(huh) {} // expected-error {{use of undeclared identifier}} \
+                // ref-error {{use of undeclared identifier}}
+
+  return 0;
+}
+
+constexpr void invalid2() {
+  int i = 0;
+  // Invalid expression in discard().
+  huh(); // expected-error {{use of undeclared identifier}} \
+         // ref-error {{use of undeclared identifier}}
+}
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -588,12 +588,18 @@
 }
 
 template <class Emitter> bool ByteCodeExprGen<Emitter>::discard(const Expr *E) {
+  if (E->containsErrors())
+    return false;
+
   OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true);
   return this->Visit(E);
 }
 
 template <class Emitter>
 bool ByteCodeExprGen<Emitter>::visit(const Expr *E) {
+  if (E->containsErrors())
+    return false;
+
   OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false);
   return this->Visit(E);
 }
@@ -1156,6 +1162,7 @@
 /// We need to evaluate the initializer and return its value.
 template <class Emitter>
 bool ByteCodeExprGen<Emitter>::visitDecl(const VarDecl *VD) {
+  assert(!VD->isInvalidDecl() && "Trying to constant evaluate an invalid decl");
   std::optional<PrimType> VarT = classify(VD->getType());
 
   // Create and initialize the variable.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137386.492054.patch
Type: text/x-patch
Size: 1721 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230125/6f375df7/attachment.bin>


More information about the cfe-commits mailing list