[clang] 61c7a69 - [clang][Interp] Fix sizeof of reference types

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 7 04:32:35 PST 2024


Author: Timm Bäder
Date: 2024-02-07T13:32:07+01:00
New Revision: 61c7a69fa0c020e92b1b10882d5d2957f3b8da21

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

LOG: [clang][Interp] Fix sizeof of reference types

Added: 
    

Modified: 
    clang/lib/AST/Interp/ByteCodeExprGen.cpp
    clang/test/AST/Interp/literals.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 38b2d6fad043c..b95126ee20fe2 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1085,6 +1085,12 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryExprOrTypeTraitExpr(
 
   if (Kind == UETT_SizeOf) {
     QualType ArgType = E->getTypeOfArgument();
+
+    // 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>())
+      ArgType = Ref->getPointeeType();
+
     CharUnits Size;
     if (ArgType->isVoidType() || ArgType->isFunctionType())
       Size = CharUnits::One();

diff  --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index c2bc5338ea925..000d2bc8a4f6c 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -216,6 +216,8 @@ namespace PointerComparison {
 }
 
 namespace SizeOf {
+  static_assert(alignof(char&) == 1);
+
   constexpr int soint = sizeof(int);
   constexpr int souint = sizeof(unsigned int);
   static_assert(soint == souint, "");


        


More information about the cfe-commits mailing list