[clang] cdad183 - [clang] Fix #embed "fast path" (#121479)

via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 3 02:17:20 PST 2025


Author: Mariya Podchishchaeva
Date: 2025-01-03T11:17:16+01:00
New Revision: cdad18319425a7bf93cc25b276a7961fe5b1168b

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

LOG: [clang] Fix #embed "fast path" (#121479)

When a single #embed directive is used to initialize a char array, the
case is optimized via swap of EmbedExpr to underlying StringLiteral,
resulting in better performance in AST consumers.
While browsing through the code, I realized that
7122b70cfc8e23a069410215c363da76d842bda4
which changed type of EmbedExpr made the "fast path" unreachable. This
patch fixes this unfortunate situation.

Added: 
    

Modified: 
    clang/lib/Sema/SemaInit.cpp
    clang/test/Analysis/embed.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 5909457b04e663..0dd5f468cf60bf 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -2030,13 +2030,8 @@ canInitializeArrayWithEmbedDataString(ArrayRef<Expr *> ExprList,
 
   if (InitType->isArrayType()) {
     const ArrayType *InitArrayType = InitType->getAsArrayTypeUnsafe();
-    QualType InitElementTy = InitArrayType->getElementType();
-    QualType EmbedExprElementTy = EE->getDataStringLiteral()->getType();
-    const bool TypesMatch =
-        Context.typesAreCompatible(InitElementTy, EmbedExprElementTy) ||
-        (InitElementTy->isCharType() && EmbedExprElementTy->isCharType());
-    if (TypesMatch)
-      return true;
+    StringLiteral *SL = EE->getDataStringLiteral();
+    return IsStringInit(SL, InitArrayType, Context) == SIF_None;
   }
   return false;
 }

diff  --git a/clang/test/Analysis/embed.c b/clang/test/Analysis/embed.c
index 32f6c130325740..db8c270fb35de4 100644
--- a/clang/test/Analysis/embed.c
+++ b/clang/test/Analysis/embed.c
@@ -8,5 +8,5 @@ int main() {
         #embed "embed.c"
     };
     clang_analyzer_dump_ptr(SelfBytes); // expected-warning {{&Element{SelfBytes,0 S64b,unsigned char}}}
-    clang_analyzer_dump(SelfBytes[0]); // expected-warning {{Unknown}} FIXME: This should be the `/` character.
+    clang_analyzer_dump(SelfBytes[0]); // expected-warning {{47 U8b}}
 }


        


More information about the cfe-commits mailing list