Skip to content

[clang][bytecode] Fix rejecting invalid sizeof expressions #123332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2025

Conversation

tbaederr
Copy link
Contributor

Emit the invalid note instead of nothing.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jan 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 17, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Emit the invalid note instead of nothing.


Full diff: https://github.com/llvm/llvm-project/pull/123332.diff

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Compiler.cpp (+4-5)
  • (modified) clang/test/AST/ByteCode/literals.cpp (+10-15)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 4bfb80589620c1..ebef706d5fbb6a 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -2056,6 +2056,9 @@ bool Compiler<Emitter>::VisitUnaryExprOrTypeTraitExpr(
   if (Kind == UETT_SizeOf || Kind == UETT_DataSizeOf) {
     QualType ArgType = E->getTypeOfArgument();
 
+    if (DiscardResult)
+      return true;
+
     // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
     //   the result is the size of the referenced type."
     if (const auto *Ref = ArgType->getAs<ReferenceType>())
@@ -2066,17 +2069,13 @@ bool Compiler<Emitter>::VisitUnaryExprOrTypeTraitExpr(
       Size = CharUnits::One();
     else {
       if (ArgType->isDependentType() || !ArgType->isConstantSizeType())
-        return false;
+        return this->emitInvalid(E);
 
       if (Kind == UETT_SizeOf)
         Size = ASTCtx.getTypeSizeInChars(ArgType);
       else
         Size = ASTCtx.getTypeInfoDataSizeInChars(ArgType).Width;
     }
-
-    if (DiscardResult)
-      return true;
-
     return this->emitConst(Size.getQuantity(), E);
   }
 
diff --git a/clang/test/AST/ByteCode/literals.cpp b/clang/test/AST/ByteCode/literals.cpp
index 3d415a93a392a5..fdf1a6820e4466 100644
--- a/clang/test/AST/ByteCode/literals.cpp
+++ b/clang/test/AST/ByteCode/literals.cpp
@@ -261,31 +261,26 @@ namespace SizeOf {
   }
 
 #if __cplusplus >= 201402L
-  constexpr int IgnoredRejected() { // ref-error {{never produces a constant expression}}
+  constexpr int IgnoredRejected() { // both-error {{never produces a constant expression}}
     int n = 0;
     sizeof(int[n++]); // both-warning {{expression result unused}} \
-                      // ref-note 2{{subexpression not valid in a constant expression}}
+                      // both-note 2{{subexpression not valid in a constant expression}}
     return n;
   }
-  /// FIXME: This is rejected because the parameter so sizeof() is not constant.
-  ///   produce a proper diagnostic.
   static_assert(IgnoredRejected() == 0, ""); // both-error {{not an integral constant expression}} \
-                                             // ref-note {{in call to 'IgnoredRejected()'}}
+                                             // both-note {{in call to 'IgnoredRejected()'}}
 #endif
 
 
 #if __cplusplus >= 202002L
   /// FIXME: The following code should be accepted.
-  consteval int foo(int n) { // ref-error {{consteval function never produces a constant expression}}
-    return sizeof(int[n]); // ref-note 3{{not valid in a constant expression}}
-  }
-  constinit int var = foo(5); // ref-error {{not a constant expression}} \
-                              // ref-note 2{{in call to}} \
-                              // ref-error {{does not have a constant initializer}} \
-                              // ref-note {{required by 'constinit' specifier}} \
-                              // expected-error  {{is not a constant expression}} \
-                              // expected-error {{does not have a constant initializer}} \
-                              // expected-note {{required by 'constinit' specifier}} \
+  consteval int foo(int n) { // both-error {{consteval function never produces a constant expression}}
+    return sizeof(int[n]); // both-note 3{{not valid in a constant expression}}
+  }
+  constinit int var = foo(5); // both-error {{not a constant expression}} \
+                              // both-note 2{{in call to}} \
+                              // both-error {{does not have a constant initializer}} \
+                              // both-note {{required by 'constinit' specifier}}
 
 #endif
 };

Emit the invalid note instead of nothing.
@tbaederr tbaederr merged commit 0171e56 into llvm:main Jan 17, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants