[clang] 18a628e - [AST] Use correct APSInt width when evaluating string literals

Sergei Barannikov via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 5 09:14:23 PDT 2023


Author: Sergei Barannikov
Date: 2023-09-05T19:14:09+03:00
New Revision: 18a628ec4ef72bcc005e2e9d5757445c31c6e2f8

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

LOG: [AST] Use correct APSInt width when evaluating string literals

The width of the APSInt values should be the width of an element.
getCharByteWidth returns the size of an element in _host_ bytes, which
makes the width N times greater, where N is the ratio between target's
CHAR_BIT and host's CHAR_BIT.
This is NFC for in-tree targets because all of them have CHAR_BIT == 8.

Reviewed By: cor3ntin, aaron.ballman

Differential Revision: https://reviews.llvm.org/D154773

Added: 
    

Modified: 
    clang/lib/AST/ExprConstant.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 0f37bcf0d5c852..d8632f53bb1eef 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -3463,8 +3463,7 @@ static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit,
   assert(CAT && "string literal isn't an array");
   QualType CharType = CAT->getElementType();
   assert(CharType->isIntegerType() && "unexpected character type");
-
-  APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(),
+  APSInt Value(Info.Ctx.getTypeSize(CharType),
                CharType->isUnsignedIntegerType());
   if (Index < S->getLength())
     Value = S->getCodeUnit(Index);
@@ -3487,7 +3486,7 @@ static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S,
   unsigned Elts = CAT->getSize().getZExtValue();
   Result = APValue(APValue::UninitArray(),
                    std::min(S->getLength(), Elts), Elts);
-  APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(),
+  APSInt Value(Info.Ctx.getTypeSize(CharType),
                CharType->isUnsignedIntegerType());
   if (Result.hasArrayFiller())
     Result.getArrayFiller() = APValue(Value);


        


More information about the cfe-commits mailing list