https://github.com/amanmaurya92 created https://github.com/llvm/llvm-project/pull/239291
This patch adds tests for calling static member functions through instance and pointer expressions in ClangIR (`clang/test/CIR/CodeGenCXX/static-member-call.cpp`). Under standard C++ Sema, calling a static member function via an object instance (`s.static_func()`) or pointer (`p->static_func()`) is lowered into a standard `CallExpr` with a `MemberExpr` callee. This patch adds test coverage verifying that: 1. Direct instance calls (`s.static_func()`) emit expected CIR and OGCG LLVM IR. 2. Pointer arrow calls (`p->static_func()`) emit expected CIR and OGCG LLVM IR. 3. Temporary base object expressions are evaluated for side effects (`get_s().static_func()`). 4. Mutating pointer expressions are evaluated for side effects (`(p++)->static_func()`). Split from #237420 as requested by @Men-cotton. >From 84ae12a44d60bb35cdcc306d8544a64983df63cc Mon Sep 17 00:00:00 2001 From: amanmaurya92 <[email protected]> Date: Thu, 5 Nov 2026 22:13:28 +0530 Subject: [PATCH] [CIR] Add test coverage for static member call side effects Add tests in clang/test/CIR/CodeGenCXX/static-member-call.cpp to verify that invoking a static member function through an instance or pointer expression emits the expected CIR and evaluates base expressions for side effects. --- .../CIR/CodeGenCXX/static-member-call.cpp | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 clang/test/CIR/CodeGenCXX/static-member-call.cpp diff --git a/clang/test/CIR/CodeGenCXX/static-member-call.cpp b/clang/test/CIR/CodeGenCXX/static-member-call.cpp new file mode 100644 index 0000000000000..a6495c8974095 --- /dev/null +++ b/clang/test/CIR/CodeGenCXX/static-member-call.cpp @@ -0,0 +1,68 @@ +// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -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 -emit-llvm -disable-llvm-passes %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG + +struct S { + static int static_func(int a); +}; + +S get_s(); + +// CIR-LABEL: cir.func {{.*}} @_Z18test_instance_call1S +// OGCG-LABEL: define {{.*}} @_Z18test_instance_call1S +int test_instance_call(S s) { + // CIR: %[[ARG:.*]] = cir.const #cir.int<42> : !s32i + // CIR: %[[CALL:.*]] = cir.call @_ZN1S11static_funcEi(%[[ARG]]) : (!s32i {{.*}}) -> (!s32i {{.*}}) + // CIR: cir.store %[[CALL]], %[[RET:.*]] : !s32i, !cir.ptr<!s32i> + // CIR: %[[RES:.*]] = cir.load %[[RET]] : !cir.ptr<!s32i>, !s32i + // CIR: cir.return %[[RES]] : !s32i + + // OGCG: %[[CALL_OGCG:.*]] = call noundef i32 @_ZN1S11static_funcEi(i32 noundef 42) + // OGCG: ret i32 %[[CALL_OGCG]] + return s.static_func(42); +} + +// CIR-LABEL: cir.func {{.*}} @_Z15test_arrow_callP1S +// OGCG-LABEL: define {{.*}} @_Z15test_arrow_callP1S +int test_arrow_call(S *p) { + // CIR: %[[ARG:.*]] = cir.const #cir.int<100> : !s32i + // CIR: %[[CALL:.*]] = cir.call @_ZN1S11static_funcEi(%[[ARG]]) : (!s32i {{.*}}) -> (!s32i {{.*}}) + // CIR: cir.store %[[CALL]], %[[RET:.*]] : !s32i, !cir.ptr<!s32i> + // CIR: %[[RES:.*]] = cir.load %[[RET]] : !cir.ptr<!s32i>, !s32i + // CIR: cir.return %[[RES]] : !s32i + + // OGCG: %[[CALL_OGCG:.*]] = call noundef i32 @_ZN1S11static_funcEi(i32 noundef 100) + // OGCG: ret i32 %[[CALL_OGCG]] + return p->static_func(100); +} + +// Verify that the instance expression is evaluated for side effects: +// CIR-LABEL: cir.func {{.*}} @_Z16test_side_effectv +// OGCG-LABEL: define {{.*}} @_Z16test_side_effectv +int test_side_effect() { + // CIR: cir.call @_Z5get_sv() + // CIR: %[[ARG:.*]] = cir.const #cir.int<42> : !s32i + // CIR: %[[CALL:.*]] = cir.call @_ZN1S11static_funcEi(%[[ARG]]) : (!s32i {{.*}}) -> (!s32i {{.*}}) + // CIR: cir.return %{{.*}} : !s32i + + // OGCG: call void @_Z5get_sv() + // OGCG: %[[CALL_OGCG:.*]] = call noundef i32 @_ZN1S11static_funcEi(i32 noundef 42) + // OGCG: ret i32 %[[CALL_OGCG]] + return get_s().static_func(42); +} + +// Verify that pointer expressions are evaluated for side effects: +// CIR-LABEL: cir.func {{.*}} @_Z20test_ptr_side_effectP1S +// OGCG-LABEL: define {{.*}} @_Z20test_ptr_side_effectP1S +int test_ptr_side_effect(S *p) { + // CIR: cir.ptr_stride + // CIR: %[[ARG:.*]] = cir.const #cir.int<100> : !s32i + // CIR: %[[CALL:.*]] = cir.call @_ZN1S11static_funcEi(%[[ARG]]) : (!s32i {{.*}}) -> (!s32i {{.*}}) + // CIR: cir.return %{{.*}} : !s32i + + // OGCG: getelementptr + // OGCG: %[[CALL_OGCG:.*]] = call noundef i32 @_ZN1S11static_funcEi(i32 noundef 100) + // OGCG: ret i32 %[[CALL_OGCG]] + return (p++)->static_func(100); +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
