66 lines · c
1// RUN: %clang_cc1 -std=gnu11 -triple x86_64-unknown-linux-gnu -O2 -emit-llvm -o - %s | FileCheck %s2 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 8struct with_counted_by_void {9 int count;10 void* buf __counted_by(count);11};12 13struct with_sized_by_void {14 int size;15 void* buf __sized_by(size);16};17 18struct 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 %s25// CHECK: %[[NARROW:.*]] = tail call i32 @llvm.smax.i32(i32 %[[COUNT]], i32 0)26// CHECK: %[[ZEXT:.*]] = zext nneg i32 %[[NARROW]] to i6427// CHECK: ret i64 %[[ZEXT]]28//29// Verify: counted_by on void* returns the count directly (count * 1 byte)30long 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 %s36// CHECK: %[[NARROW:.*]] = tail call i32 @llvm.smax.i32(i32 %[[SIZE]], i32 0)37// CHECK: %[[ZEXT:.*]] = zext nneg i32 %[[NARROW]] to i6438// CHECK: ret i64 %[[ZEXT]]39//40// Verify: sized_by on void* returns the size directly41long 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 %s47// CHECK: %[[SEXT:.*]] = sext i32 %[[COUNT]] to i6448// CHECK: %[[SIZE:.*]] = shl nsw i64 %[[SEXT]], 249// CHECK: ret i6450//51// Verify: counted_by on int* returns count * sizeof(int) = count * 452long 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, ptr58// CHECK: %[[EXT:.*]] = sext i32 %offset to i6459// 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 sizes63void* test_void_ptr_arithmetic(struct with_counted_by_void *s, int offset) {64 return s->buf + offset; // GNU extension: void* arithmetic65}66