Skip to content

Commit 03ae161

Browse files
Fznamznonyuxuanchen1997
authored andcommitted
[clang] Fix underlying type of EmbedExpr (#99050)
Summary: This patch makes remaining cases of #embed to emit int type since there is an agreement to do that for C. C++ is being discussed, but in general we don't want to produce different types for C and C++. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251342
1 parent 17043c5 commit 03ae161

File tree

5 files changed

+18
-6
lines changed

5 files changed

+18
-6
lines changed

clang/lib/AST/Expr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2376,7 +2376,7 @@ APValue SourceLocExpr::EvaluateInContext(const ASTContext &Ctx,
23762376
EmbedExpr::EmbedExpr(const ASTContext &Ctx, SourceLocation Loc,
23772377
EmbedDataStorage *Data, unsigned Begin,
23782378
unsigned NumOfElements)
2379-
: Expr(EmbedExprClass, Ctx.UnsignedCharTy, VK_PRValue, OK_Ordinary),
2379+
: Expr(EmbedExprClass, Ctx.IntTy, VK_PRValue, OK_Ordinary),
23802380
EmbedKeywordLoc(Loc), Ctx(&Ctx), Data(Data), Begin(Begin),
23812381
NumOfElements(NumOfElements) {
23822382
setDependence(ExprDependence::None);

clang/lib/Sema/SemaInit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,7 @@ canInitializeArrayWithEmbedDataString(ArrayRef<Expr *> ExprList,
20162016
if (InitType->isArrayType()) {
20172017
const ArrayType *InitArrayType = InitType->getAsArrayTypeUnsafe();
20182018
QualType InitElementTy = InitArrayType->getElementType();
2019-
QualType EmbedExprElementTy = EE->getType();
2019+
QualType EmbedExprElementTy = EE->getDataStringLiteral()->getType();
20202020
const bool TypesMatch =
20212021
Context.typesAreCompatible(InitElementTy, EmbedExprElementTy) ||
20222022
(InitElementTy->isCharType() && EmbedExprElementTy->isCharType());
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

clang/test/Preprocessor/embed_codegen.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ int ca[] = {
1414
};
1515

1616
// CHECK: %arrayinit.element = getelementptr inbounds i32, ptr %notca, i64 1
17-
// CHECK: store i8 106, ptr %arrayinit.element, align 4
17+
// CHECK: store i32 106, ptr %arrayinit.element, align 4
1818
// CHECK: %arrayinit.element1 = getelementptr inbounds i32, ptr %notca, i64 2
19-
// CHECK: store i8 107, ptr %arrayinit.element1, align 4
19+
// CHECK: store i32 107, ptr %arrayinit.element1, align 4
2020
int notca[] = {
2121
a
2222
#embed <jk.txt> prefix(,)
@@ -75,9 +75,9 @@ constexpr struct T t[] = {
7575
// CHECK: %arrayinit.element7 = getelementptr inbounds %struct.T, ptr %tnonc, i64 1
7676
// CHECK: call void @llvm.memset.p0.i64(ptr align 4 %arrayinit.element7, i8 0, i64 20, i1 false)
7777
// CHECK: %arr8 = getelementptr inbounds %struct.T, ptr %arrayinit.element7, i32 0, i32 0
78-
// CHECK: store i8 106, ptr %arr8, align 4
78+
// CHECK: store i32 106, ptr %arr8, align 4
7979
// CHECK: %arrayinit.element9 = getelementptr inbounds i32, ptr %arr8, i64 1
80-
// CHECK: store i8 107, ptr %arrayinit.element9, align 4
80+
// CHECK: store i32 107, ptr %arrayinit.element9, align 4
8181
struct T tnonc[] = {
8282
a, 300, 1, 2, 3
8383
#embed <jk.txt> prefix(,)

clang/test/Preprocessor/embed_weird.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,14 @@ void f1() {
115115
};
116116
}
117117
#endif
118+
119+
struct HasChar {
120+
signed char ch;
121+
};
122+
123+
constexpr struct HasChar c = {
124+
#embed "Inputs/big_char.txt" // cxx-error {{constant expression evaluates to 255 which cannot be narrowed to type 'signed char'}} \
125+
cxx-note {{insert an explicit cast to silence this issue}} \
126+
c-error {{constexpr initializer evaluates to 255 which is not exactly representable in type 'signed char'}}
127+
128+
};

0 commit comments

Comments
 (0)