[clang] c79d1fa - [clang][bytecode] Don't discard all void-typed expressions (#105625)

via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 22 00:51:20 PDT 2024


Author: Timm Baeder
Date: 2024-08-22T09:51:16+02:00
New Revision: c79d1fa540390f6e37e1ea326153559eeadd0de6

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

LOG: [clang][bytecode] Don't discard all void-typed expressions (#105625)

For void-types InitListExprs, we need to diagnose them as invalid. But
only if we are _not_ discarding.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Compiler.cpp
    clang/test/AST/ByteCode/literals.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 6d05f75131640a..10f3222726fd43 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -1318,15 +1318,6 @@ bool Compiler<Emitter>::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
 template <class Emitter>
 bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
                                       const Expr *ArrayFiller, const Expr *E) {
-
-  QualType QT = E->getType();
-
-  if (const auto *AT = QT->getAs<AtomicType>())
-    QT = AT->getValueType();
-
-  if (QT->isVoidType())
-    return this->emitInvalid(E);
-
   // Handle discarding first.
   if (DiscardResult) {
     for (const Expr *Init : Inits) {
@@ -1336,6 +1327,13 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
     return true;
   }
 
+  QualType QT = E->getType();
+  if (const auto *AT = QT->getAs<AtomicType>())
+    QT = AT->getValueType();
+
+  if (QT->isVoidType())
+    return this->emitInvalid(E);
+
   // Primitive values.
   if (std::optional<PrimType> T = classify(QT)) {
     assert(!DiscardResult);
@@ -3251,12 +3249,9 @@ template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) {
   if (E->getType().isNull())
     return false;
 
-  if (E->getType()->isVoidType())
-    return this->discard(E);
-
   // Create local variable to hold the return value.
-  if (!E->isGLValue() && !E->getType()->isAnyComplexType() &&
-      !classify(E->getType())) {
+  if (!E->getType()->isVoidType() && !E->isGLValue() &&
+      !E->getType()->isAnyComplexType() && !classify(E->getType())) {
     std::optional<unsigned> LocalIndex = allocateLocal(E);
     if (!LocalIndex)
       return false;

diff  --git a/clang/test/AST/ByteCode/literals.cpp b/clang/test/AST/ByteCode/literals.cpp
index a46f6ed747ec2f..2329d4d973f01d 100644
--- a/clang/test/AST/ByteCode/literals.cpp
+++ b/clang/test/AST/ByteCode/literals.cpp
@@ -46,6 +46,7 @@ static_assert(Failed2 == 0, ""); // both-error {{not an integral constant expres
                                  // both-note {{initializer of 'Failed2' is not a constant expression}}
 
 const int x = *(volatile int*)0x1234;
+static_assert((void{}, true), "");
 
 namespace ScalarTypes {
   constexpr int ScalarInitInt = int();


        


More information about the cfe-commits mailing list