|
| 1 | +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -emit-cir %s -o %t.cir |
| 2 | +// RUN: FileCheck --input-file=%t.cir %s |
| 3 | + |
| 4 | +// Test __builtin_LINE |
| 5 | +int test_builtin_LINE() { |
| 6 | + // CHECK-LABEL: cir.func {{.*}}@{{.*}}test_builtin_LINE |
| 7 | + // CHECK: %{{.*}} = cir.const #cir.int<8> : !u32i |
| 8 | + return __builtin_LINE(); |
| 9 | +} |
| 10 | + |
| 11 | +// Test __builtin_FILE |
| 12 | +const char* test_builtin_FILE() { |
| 13 | + // CHECK-LABEL: cir.func {{.*}}@{{.*}}test_builtin_FILE |
| 14 | + // CHECK: %{{.*}} = cir.const #cir.global_view<@".str{{.*}}"> : !cir.ptr<!s8i> |
| 15 | + return __builtin_FILE(); |
| 16 | +} |
| 17 | + |
| 18 | +// Test __builtin_FUNCTION |
| 19 | +const char* test_builtin_FUNCTION() { |
| 20 | + // CHECK-LABEL: cir.func {{.*}}@{{.*}}test_builtin_FUNCTION |
| 21 | + // CHECK: %{{.*}} = cir.const #cir.global_view<@".str{{.*}}"> : !cir.ptr<!s8i> |
| 22 | + return __builtin_FUNCTION(); |
| 23 | +} |
| 24 | + |
| 25 | +// Test __builtin_COLUMN |
| 26 | +int test_builtin_COLUMN() { |
| 27 | + // CHECK-LABEL: cir.func {{.*}}@{{.*}}test_builtin_COLUMN |
| 28 | + // The column number is the position of '__builtin_COLUMN' |
| 29 | + // CHECK: %{{.*}} = cir.const #cir.int<10> : !u32i |
| 30 | + return __builtin_COLUMN(); |
| 31 | +} |
| 32 | + |
| 33 | +// Test in global context |
| 34 | +#line 100 "test_file.cpp" |
| 35 | +int global_line = __builtin_LINE(); |
| 36 | +// CHECK: cir.global external @global_line = #cir.int<100> : !s32i |
| 37 | + |
| 38 | +// Test default argument |
| 39 | +int get_line(int l = __builtin_LINE()) { |
| 40 | + return l; |
| 41 | +} |
| 42 | + |
| 43 | +void test_default_arg() { |
| 44 | + // CHECK-LABEL: cir.func {{.*}}@{{.*}}test_default_arg |
| 45 | + // The LINE should be from the call site, not the default argument definition |
| 46 | + #line 111 |
| 47 | + int x = get_line(); |
| 48 | + // CHECK: %{{.*}} = cir.const #cir.int<111> : !u32i |
| 49 | + // CHECK: %{{.*}} = cir.call @{{.*}}get_line{{.*}}({{.*}}) : |
| 50 | +} |
| 51 | + |
| 52 | +#line 200 "lambda-test.cpp" |
| 53 | +// Test in lambda (this tests that source location correctly captures context) |
| 54 | +void test_in_lambda() { |
| 55 | + // CHECK-LABEL: cir.func {{.*}}@{{.*}}test_in_lambda |
| 56 | + auto lambda = []() { |
| 57 | + return __builtin_LINE(); |
| 58 | + }; |
| 59 | + int x = lambda(); |
| 60 | +} |
| 61 | + |
| 62 | +#line 214 "combined-test.cpp" |
| 63 | +// Test multiple builtins in one expression |
| 64 | +void test_combined() { |
| 65 | + // CHECK-LABEL: cir.func {{.*}}@{{.*}}test_combined |
| 66 | + const char* file = __builtin_FILE(); |
| 67 | + int line = __builtin_LINE(); |
| 68 | + const char* func = __builtin_FUNCTION(); |
| 69 | + // All should produce constants |
| 70 | + // CHECK: cir.const |
| 71 | + // CHECK: cir.const |
| 72 | + // CHECK: cir.const |
| 73 | +} |
0 commit comments