Author: Timm Baeder
Date: 2025-12-26T07:03:45+01:00
New Revision: 41fb244d29e2e5e7647d97ff6a545675d30cf669

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

LOG: [clang][ExprConstant] Reject integral casts of addr-label-diffs... 
(#171437)

... if the result is narrower than 32 bits.

See the discussion in https://github.com/llvm/llvm-project/issues/136135

Added: 
    

Modified: 
    clang/lib/AST/ExprConstant.cpp
    clang/test/AST/ast-dump-APValue-addrlabeldiff.c
    clang/test/CodeGenCXX/const-init.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 6ccd57bdc4df8..f80dabf5444c7 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -18689,12 +18689,15 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr 
*E) {
 
     if (!Result.isInt()) {
       // Allow casts of address-of-label 
diff erences if they are no-ops
-      // or narrowing.  (The narrowing case isn't actually guaranteed to
+      // or narrowing, if the result is at least 32 bits wide.
+      // (The narrowing case isn't actually guaranteed to
       // be constant-evaluatable except in some narrow cases which are hard
       // to detect here.  We let it through on the assumption the user knows
       // what they are doing.)
-      if (Result.isAddrLabelDiff())
-        return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType);
+      if (Result.isAddrLabelDiff()) {
+        unsigned DestBits = Info.Ctx.getTypeSize(DestType);
+        return DestBits >= 32 && DestBits <= Info.Ctx.getTypeSize(SrcType);
+      }
       // Only allow casts of lvalues if they are lossless.
       return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
     }

diff  --git a/clang/test/AST/ast-dump-APValue-addrlabel
diff .c b/clang/test/AST/ast-dump-APValue-addrlabel
diff .c
index 481098eabedb9..ec6eddbb187cb 100644
--- a/clang/test/AST/ast-dump-APValue-addrlabel
diff .c
+++ b/clang/test/AST/ast-dump-APValue-addrlabel
diff .c
@@ -12,7 +12,7 @@
 
 // CHECK:  |   |-value: AddrLabelDiff &&l2 - &&l1
 int Test(void) {
-  constexpr char ar = &&l2 - &&l1;
+  constexpr long long ar = &&l2 - &&l1;
 l1:
   return 10;
 l2:

diff  --git a/clang/test/CodeGenCXX/const-init.cpp 
b/clang/test/CodeGenCXX/const-init.cpp
index fd6fd24ae1d94..f5b715949f23a 100644
--- a/clang/test/CodeGenCXX/const-init.cpp
+++ b/clang/test/CodeGenCXX/const-init.cpp
@@ -73,6 +73,10 @@ __int128_t PR11705 = (__int128_t)&PR11705;
 // CHECK: @_ZZ23UnfoldableAddrLabelDiffvE1x = internal global i128 0
 void UnfoldableAddrLabelDiff() { static __int128_t x = (long)&&a-(long)&&b; 
a:b:return;}
 
+// CHECK: @_ZZ24UnfoldableAddrLabelDiff2vE1x = internal global i16 0
+void UnfoldableAddrLabelDiff2() { static short x = (long)&&a-(long)&&b; 
a:b:return;}
+
+
 // But make sure we do fold this.
 // CHECK: @_ZZ21FoldableAddrLabelDiffvE1x = internal global i64 sub (i64 
ptrtoint (ptr blockaddress(@_Z21FoldableAddrLabelDiffv
 void FoldableAddrLabelDiff() { static long x = (long)&&a-(long)&&b; 
a:b:return;}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to