Skip to content

Commit 65eaed7

Browse files
authored
[CIR] Handle character literal values (#144141)
This change adds a handler for emitting a cir.constant op when a character literal is encountered outside an initializer expression.
1 parent b7cb348 commit 65eaed7

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
162162
builder.getAttr<cir::FPAttr>(type, e->getValue()));
163163
}
164164

165+
mlir::Value VisitCharacterLiteral(const CharacterLiteral *e) {
166+
mlir::Type ty = cgf.convertType(e->getType());
167+
auto init = cir::IntAttr::get(ty, e->getValue());
168+
return builder.create<cir::ConstantOp>(cgf.getLoc(e->getExprLoc()), init);
169+
}
170+
165171
mlir::Value VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *e) {
166172
return builder.getBool(e->getValue(), cgf.getLoc(e->getExprLoc()));
167173
}

clang/test/CIR/CodeGen/basic.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,17 @@ size_type max_size(void) {
309309
// CHECK: %6 = cir.load{{.*}} %0 : !cir.ptr<!u64i>, !u64i
310310
// CHECK: cir.return %6 : !u64i
311311
// CHECK: }
312+
313+
void test_char_literal() {
314+
char c;
315+
c = 'X';
316+
}
317+
318+
// CIR: cir.func @test_char_literal
319+
// CIR: cir.const #cir.int<88>
320+
321+
// LLVM: define void @test_char_literal()
322+
// LLVM: store i8 88, ptr %{{.*}}, align 1
323+
324+
// OGCG: define{{.*}} void @test_char_literal()
325+
// OGCG: store i8 88, ptr %{{.*}}, align 1

0 commit comments

Comments
 (0)