[clang] [CIR] Add support for SourceLocExpr (PR #171492)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 9 11:42:53 PST 2025
https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/171492
>From 04fcc40e7be832d6a6964b1994a57c6a4c4d5696 Mon Sep 17 00:00:00 2001
From: Amr Hesham <amr96 at programmer.net>
Date: Tue, 9 Dec 2025 20:28:13 +0100
Subject: [PATCH] [CIR] Add support for SourceLocExpr
---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 11 +++-
clang/lib/CIR/CodeGen/CIRGenModule.cpp | 6 ++-
clang/test/CIR/CodeGen/source-loc.cpp | 58 ++++++++++++++++++++++
3 files changed, 72 insertions(+), 3 deletions(-)
create mode 100644 clang/test/CIR/CodeGen/source-loc.cpp
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 6820e2a403288..0027db74e6479 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//
+#include "CIRGenConstantEmitter.h"
#include "CIRGenFunction.h"
#include "CIRGenValue.h"
@@ -813,8 +814,14 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
return {};
}
mlir::Value VisitSourceLocExpr(SourceLocExpr *e) {
- cgf.cgm.errorNYI(e->getSourceRange(), "ScalarExprEmitter: source loc");
- return {};
+ ASTContext &ctx = cgf.getContext();
+ APValue evaluated =
+ e->EvaluateInContext(ctx, cgf.curSourceLocExprScope.getDefaultExpr());
+ mlir::Attribute attribute = ConstantEmitter(cgf).emitAbstract(
+ e->getLocation(), evaluated, e->getType());
+ mlir::TypedAttr typedAttr = mlir::cast<mlir::TypedAttr>(attribute);
+ return cir::ConstantOp::create(builder, cgf.getLoc(e->getExprLoc()),
+ typedAttr);
}
mlir::Value VisitCXXDefaultArgExpr(CXXDefaultArgExpr *dae) {
CIRGenFunction::CXXDefaultArgExprScope scope(cgf, dae);
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index e1894c040dd53..7c11b2129f2f5 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -1412,7 +1412,11 @@ cir::GlobalOp CIRGenModule::getGlobalForStringLiteral(const StringLiteral *s,
// Unlike LLVM IR, CIR doesn't automatically unique names for globals, so
// we need to do that explicitly.
std::string uniqueName = getUniqueGlobalName(name.str());
- mlir::Location loc = getLoc(s->getSourceRange());
+ // Synthetic string literals (e.g., from SourceLocExpr) may not have valid
+ // source locations. Use unknown location in those cases.
+ mlir::Location loc = s->getBeginLoc().isValid()
+ ? getLoc(s->getSourceRange())
+ : builder.getUnknownLoc();
auto typedC = llvm::cast<mlir::TypedAttr>(c);
gv = generateStringLiteral(loc, typedC,
cir::GlobalLinkageKind::PrivateLinkage, *this,
diff --git a/clang/test/CIR/CodeGen/source-loc.cpp b/clang/test/CIR/CodeGen/source-loc.cpp
new file mode 100644
index 0000000000000..fc8ab76a9bbb8
--- /dev/null
+++ b/clang/test/CIR/CodeGen/source-loc.cpp
@@ -0,0 +1,58 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
+
+void line_column() {
+ unsigned int a = __builtin_LINE();
+ unsigned int b = __builtin_COLUMN();
+}
+
+// CIR: %[[A_ADDR:.*]] = cir.alloca !u32i, !cir.ptr<!u32i>, ["a", init]
+// CIR: %[[B_ADDR:.*]] = cir.alloca !u32i, !cir.ptr<!u32i>, ["b", init]
+// CIR: %[[CONST_9:.*]] = cir.const #cir.int<9> : !u32i
+// CIR: cir.store {{.*}} %[[CONST_9]], %[[A_ADDR]] : !u32i, !cir.ptr<!u32i>
+// CIR: %[[CONST_20:.*]] = cir.const #cir.int<20> : !u32i
+// CIR: cir.store {{.*}} %[[CONST_20]], %[[B_ADDR]] : !u32i, !cir.ptr<!u32i>
+
+// LLVM: %[[A_ADDR:.*]] = alloca i32, i64 1, align 4
+// LLVM: %[[B_ADDR:.*]] = alloca i32, i64 1, align 4
+// LLVM: store i32 9, ptr %[[A_ADDR]], align 4
+// LLVM: store i32 20, ptr %[[B_ADDR]], align 4
+
+// OGCG: %[[A_ADDR:.*]] = alloca i32, align 4
+// OGCG: %[[B_ADDR:.*]] = alloca i32, align 4
+// OGCG: store i32 9, ptr %[[A_ADDR]], align 4
+// OGCG: store i32 20, ptr %[[B_ADDR]], align 4
+
+void function_file() {
+ const char *a = __builtin_FUNCTION();
+ const char *b = __builtin_FILE();
+ const char *c = __builtin_FILE_NAME();
+}
+
+// CIR: %[[A_ADDR:.*]] = cir.alloca !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>>, ["a", init]
+// CIR: %[[B_ADDR:.*]] = cir.alloca !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>>, ["b", init]
+// CIR: %[[C_ADDR:.*]] = cir.alloca !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>>, ["c", init]
+// CIR: %[[FUNC__GV:.*]] = cir.const #cir.global_view<@".str"> : !cir.ptr<!s8i>
+// CIR: cir.store {{.*}} %[[FUNC__GV]], %[[A_ADDR]] : !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>>
+// CIR: %[[FILE_PATH_GV:.*]] = cir.const #cir.global_view<@".str.1"> : !cir.ptr<!s8i>
+// CIR: cir.store {{.*}} %[[FILE_PATH_GV]], %[[B_ADDR]] : !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>>
+// CIR: %[[FILE_GV:.*]] = cir.const #cir.global_view<@".str.2"> : !cir.ptr<!s8i>
+// CIR: cir.store {{.*}} %[[FILE_GV]], %[[C_ADDR]] : !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>>
+
+// LLVM: %[[A_ADDR:.*]] = alloca ptr, i64 1, align 8
+// LLVM: %[[B_ADDR:.*]] = alloca ptr, i64 1, align 8
+// LLVM: %[[C_ADDR:.*]] = alloca ptr, i64 1, align 8
+// LLVM: store ptr @.str, ptr %[[A_ADDR]], align 8
+// LLVM: store ptr @.str.1, ptr %[[B_ADDR]], align 8
+// LLVM: store ptr @.str.2, ptr %[[C_ADDR]], align 8
+
+// OGCG: %[[A_ADDR:.*]] = alloca ptr, align 8
+// OGCG: %[[B_ADDR:.*]] = alloca ptr, align 8
+// OGCG: %[[C_ADDR:.*]] = alloca ptr, align 8
+// OGCG: store ptr @.str, ptr %[[A_ADDR]], align 8
+// OGCG: store ptr @.str.1, ptr %[[B_ADDR]], align 8
+// OGCG: store ptr @.str.2, ptr %[[C_ADDR]], align 8
More information about the cfe-commits
mailing list