Skip to content

Commit

Permalink
[clang] Fix underlying type of EmbedExpr (#99050)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Fznamznon authored and yuxuanchen1997 committed Jul 25, 2024
1 parent 17043c5 commit 03ae161
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2376,7 +2376,7 @@ APValue SourceLocExpr::EvaluateInContext(const ASTContext &Ctx,
EmbedExpr::EmbedExpr(const ASTContext &Ctx, SourceLocation Loc,
EmbedDataStorage *Data, unsigned Begin,
unsigned NumOfElements)
: Expr(EmbedExprClass, Ctx.UnsignedCharTy, VK_PRValue, OK_Ordinary),
: Expr(EmbedExprClass, Ctx.IntTy, VK_PRValue, OK_Ordinary),
EmbedKeywordLoc(Loc), Ctx(&Ctx), Data(Data), Begin(Begin),
NumOfElements(NumOfElements) {
setDependence(ExprDependence::None);
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,7 @@ canInitializeArrayWithEmbedDataString(ArrayRef<Expr *> ExprList,
if (InitType->isArrayType()) {
const ArrayType *InitArrayType = InitType->getAsArrayTypeUnsafe();
QualType InitElementTy = InitArrayType->getElementType();
QualType EmbedExprElementTy = EE->getType();
QualType EmbedExprElementTy = EE->getDataStringLiteral()->getType();
const bool TypesMatch =
Context.typesAreCompatible(InitElementTy, EmbedExprElementTy) ||
(InitElementTy->isCharType() && EmbedExprElementTy->isCharType());
Expand Down
1 change: 1 addition & 0 deletions clang/test/Preprocessor/Inputs/big_char.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8 changes: 4 additions & 4 deletions clang/test/Preprocessor/embed_codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ int ca[] = {
};

// CHECK: %arrayinit.element = getelementptr inbounds i32, ptr %notca, i64 1
// CHECK: store i8 106, ptr %arrayinit.element, align 4
// CHECK: store i32 106, ptr %arrayinit.element, align 4
// CHECK: %arrayinit.element1 = getelementptr inbounds i32, ptr %notca, i64 2
// CHECK: store i8 107, ptr %arrayinit.element1, align 4
// CHECK: store i32 107, ptr %arrayinit.element1, align 4
int notca[] = {
a
#embed <jk.txt> prefix(,)
Expand Down Expand Up @@ -75,9 +75,9 @@ constexpr struct T t[] = {
// CHECK: %arrayinit.element7 = getelementptr inbounds %struct.T, ptr %tnonc, i64 1
// CHECK: call void @llvm.memset.p0.i64(ptr align 4 %arrayinit.element7, i8 0, i64 20, i1 false)
// CHECK: %arr8 = getelementptr inbounds %struct.T, ptr %arrayinit.element7, i32 0, i32 0
// CHECK: store i8 106, ptr %arr8, align 4
// CHECK: store i32 106, ptr %arr8, align 4
// CHECK: %arrayinit.element9 = getelementptr inbounds i32, ptr %arr8, i64 1
// CHECK: store i8 107, ptr %arrayinit.element9, align 4
// CHECK: store i32 107, ptr %arrayinit.element9, align 4
struct T tnonc[] = {
a, 300, 1, 2, 3
#embed <jk.txt> prefix(,)
Expand Down
11 changes: 11 additions & 0 deletions clang/test/Preprocessor/embed_weird.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,14 @@ void f1() {
};
}
#endif

struct HasChar {
signed char ch;
};

constexpr struct HasChar c = {
#embed "Inputs/big_char.txt" // cxx-error {{constant expression evaluates to 255 which cannot be narrowed to type 'signed char'}} \
cxx-note {{insert an explicit cast to silence this issue}} \
c-error {{constexpr initializer evaluates to 255 which is not exactly representable in type 'signed char'}}

};

0 comments on commit 03ae161

Please sign in to comment.