Debian Codegraph @

clang/test/CodeGen/attr-counted-by-void-ptr-gnu.c

raw
1 // RUN: %clang_cc1 -std=gnu11 -triple x86_64-unknown-linux-gnu -O2 -emit-llvm -o - %s | FileCheck %s
2
3 // Test that counted_by on void* in GNU mode treats void as having size 1 (byte count)
4
5 #define __counted_by(f) __attribute__((counted_by(f)))
6 #define __sized_by(f) __attribute__((sized_by(f)))
7
8 struct with_counted_by_void {
9 int count;
10 void* buf __counted_by(count);
11 };
12
13 struct with_sized_by_void {
14 int size;
15 void* buf __sized_by(size);
16 };
17
18 struct with_counted_by_int {
19 int count;
20 int* buf __counted_by(count);
21 };
22
23 // CHECK-LABEL: define dso_local {{.*}}@test_counted_by_void(
24 // CHECK: %[[COUNT:.*]] = load i32, ptr %s
25 // CHECK: %[[NARROW:.*]] = tail call i32 @llvm.smax.i32(i32 %[[COUNT]], i32 0)
26 // CHECK: %[[ZEXT:.*]] = zext nneg i32 %[[NARROW]] to i64
27 // CHECK: ret i64 %[[ZEXT]]
28 //
29 // Verify: counted_by on void* returns the count directly (count * 1 byte)
30 long long test_counted_by_void(struct with_counted_by_void *s) {
31 return __builtin_dynamic_object_size(s->buf, 0);
32 }
33
34 // CHECK-LABEL: define dso_local {{.*}}@test_sized_by_void(
35 // CHECK: %[[SIZE:.*]] = load i32, ptr %s
36 // CHECK: %[[NARROW:.*]] = tail call i32 @llvm.smax.i32(i32 %[[SIZE]], i32 0)
37 // CHECK: %[[ZEXT:.*]] = zext nneg i32 %[[NARROW]] to i64
38 // CHECK: ret i64 %[[ZEXT]]
39 //
40 // Verify: sized_by on void* returns the size directly
41 long long test_sized_by_void(struct with_sized_by_void *s) {
42 return __builtin_dynamic_object_size(s->buf, 0);
43 }
44
45 // CHECK-LABEL: define dso_local {{.*}}@test_counted_by_int(
46 // CHECK: %[[COUNT:.*]] = load i32, ptr %s
47 // CHECK: %[[SEXT:.*]] = sext i32 %[[COUNT]] to i64
48 // CHECK: %[[SIZE:.*]] = shl nsw i64 %[[SEXT]], 2
49 // CHECK: ret i64
50 //
51 // Verify: counted_by on int* returns count * sizeof(int) = count * 4
52 long long test_counted_by_int(struct with_counted_by_int *s) {
53 return __builtin_dynamic_object_size(s->buf, 0);
54 }
55
56 // CHECK-LABEL: define dso_local ptr @test_void_ptr_arithmetic(
57 // CHECK: %[[BUF:.*]] = load ptr, ptr
58 // CHECK: %[[EXT:.*]] = sext i32 %offset to i64
59 // CHECK: %[[PTR:.*]] = getelementptr inbounds i8, ptr %[[BUF]], i64 %[[EXT]]
60 // CHECK: ret ptr %[[PTR]]
61 //
62 // Verify: pointer arithmetic on void* uses i8 (byte offsets), not i32 or other sizes
63 void* test_void_ptr_arithmetic(struct with_counted_by_void *s, int offset) {
64 return s->buf + offset; // GNU extension: void* arithmetic
65 }