[clang] 53d433e - [clang][bytecode] Only emit literal_comparison for string literals (#129691)

via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 4 05:07:57 PST 2025


Author: Timm Baeder
Date: 2025-03-04T14:07:53+01:00
New Revision: 53d433e702736f9edfee57ec2c1628c729336866

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

LOG: [clang][bytecode] Only emit literal_comparison for string literals (#129691)

This is what the current interpreter does as well.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.h
    clang/test/AST/ByteCode/builtin-functions.cpp
    clang/test/AST/ByteCode/functions.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index db35208a02941..2cf7ae2dd6f96 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -1065,7 +1065,8 @@ inline bool CmpHelperEQ<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
   for (const auto &P : {LHS, RHS}) {
     if (P.isZero())
       continue;
-    if (BothNonNull && P.pointsToLiteral()) {
+    if (BothNonNull && P.pointsToLiteral() &&
+        isa<StringLiteral>(P.getDeclDesc()->asExpr())) {
       const SourceInfo &Loc = S.Current->getSource(OpPC);
       S.FFDiag(Loc, diag::note_constexpr_literal_comparison);
       return false;

diff  --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index d51b039d40043..0c26d40ec5cd5 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1008,9 +1008,9 @@ namespace shufflevector {
 
 namespace FunctionStart {
   void a(void) {}
-  static_assert(__builtin_function_start(a) == a, ""); // both-error {{not an integral constant expression}} \
+  static_assert(__builtin_function_start(a) == a, ""); // ref-error {{not an integral constant expression}} \
                                                        // ref-note {{comparison against opaque constant address '&__builtin_function_start(a)'}} \
-                                                       // expected-note {{comparison of addresses of potentially overlapping literals has unspecified value}}
+                                                       // expected-error {{static assertion failed}}
 }
 
 namespace BuiltinInImplicitCtor {

diff  --git a/clang/test/AST/ByteCode/functions.cpp b/clang/test/AST/ByteCode/functions.cpp
index 66693a1fd7e32..a7c8836eac6b8 100644
--- a/clang/test/AST/ByteCode/functions.cpp
+++ b/clang/test/AST/ByteCode/functions.cpp
@@ -484,6 +484,18 @@ namespace AddressOf {
   void testAddressof(int x) {
     static_assert(&x == __builtin_addressof(x), "");
   }
+
+  struct TS {
+    constexpr bool f(TS s) const {
+      /// The addressof call has a CXXConstructExpr as a parameter.
+      return this != __builtin_addressof(s);
+    }
+  };
+  constexpr bool exprAddressOf() {
+    TS s;
+    return s.f(s);
+  }
+  static_assert(exprAddressOf(), "");
 }
 
 namespace std {


        


More information about the cfe-commits mailing list