| 1 |
// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s |
| 2 |
// RUN: %clang_cc1 %s -emit-llvm -triple i686-unknown-unknown -o - | FileCheck %s |
| 3 |
// RUN: %clang_cc1 %s -emit-llvm -triple x86_64-unknown-unknown -o - | FileCheck %s |
| 4 |
|
| 5 |
#include <stdint.h> |
| 6 |
|
| 7 |
// This test is meant to verify code that handles the 'p = nullptr + n' idiom |
| 8 |
// used by some versions of glibc and gcc. This is undefined behavior but |
| 9 |
// it is intended there to act like a conversion from a pointer-sized integer |
| 10 |
// to a pointer, and we would like to tolerate that. |
| 11 |
|
| 12 |
#define NULLPTRI8 ((int8_t*)0) |
| 13 |
|
| 14 |
// This should get the inttoptr instruction. |
| 15 |
int8_t *test1(intptr_t n) { |
| 16 |
return NULLPTRI8 + n; |
| 17 |
} |
| 18 |
// CHECK-LABEL: test1 |
| 19 |
// CHECK: inttoptr |
| 20 |
// CHECK-NOT: getelementptr |
| 21 |
|
| 22 |
// This doesn't meet the idiom because the element type is larger than a byte. |
| 23 |
int16_t *test2(intptr_t n) { |
| 24 |
return (int16_t*)0 + n; |
| 25 |
} |
| 26 |
// CHECK-LABEL: test2 |
| 27 |
// CHECK: getelementptr |
| 28 |
// CHECK-NOT: inttoptr |
| 29 |
|
| 30 |
// This doesn't meet the idiom because the offset is subtracted. |
| 31 |
int8_t* test3(intptr_t n) { |
| 32 |
return NULLPTRI8 - n; |
| 33 |
} |
| 34 |
// CHECK-LABEL: test3 |
| 35 |
// CHECK: getelementptr |
| 36 |
// CHECK-NOT: inttoptr |
| 37 |
|
| 38 |
// This checks the case where the offset isn't pointer-sized. |
| 39 |
// The front end will implicitly cast the offset to an integer, so we need to |
| 40 |
// make sure that doesn't cause problems on targets where integers and pointers |
| 41 |
// are not the same size. |
| 42 |
int8_t *test4(int8_t b) { |
| 43 |
return NULLPTRI8 + b; |
| 44 |
} |
| 45 |
// CHECK-LABEL: test4 |
| 46 |
// CHECK: inttoptr |
| 47 |
// CHECK-NOT: getelementptr |