Files
clang-p2996/clang/test/SemaCXX/warn-new-overaligned.cpp
Akira Hatanaka adaf62ced2 [Sema] Reject array element types whose sizes aren't a multiple of their
alignments

In the following code, the first element is aligned on a 16-byte
boundary, but the remaining elements aren't:

```
typedef char int8_a16 __attribute__((aligned(16)));
int8_a16 array[4];
```

Currently clang doesn't reject the code, but it should since it can
cause crashes at runtime. This patch also fixes assertion failures in
CodeGen caused by the changes in https://reviews.llvm.org/D123649.

Differential Revision: https://reviews.llvm.org/D133711
2022-09-21 09:15:03 -07:00

81 lines
2.3 KiB
C++

// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -Wover-aligned -verify=precxx17 %std_cxx98-14 %s
// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -Wover-aligned -verify=cxx17 %std_cxx17- %s
namespace test1 {
struct Test {
template <typename T>
struct SeparateCacheLines {
T data;
} __attribute__((aligned(256)));
SeparateCacheLines<int> high_contention_data[10];
};
void helper() {
Test t;
new Test; // precxx17-warning {{type 'Test' requires 256 bytes of alignment and the default allocator only guarantees}}
new Test[10]; // precxx17-warning {{type 'Test' requires 256 bytes of alignment and the default allocator only guarantees}}
}
}
namespace test2 {
struct S {
char c[256];
};
class Test {
typedef S __attribute__((aligned(256))) alignedS;
alignedS high_contention_data[10];
};
void helper() {
Test t;
new Test; // precxx17-warning {{type 'Test' requires 256 bytes of alignment and the default allocator only guarantees}}
new Test[10]; // precxx17-warning {{type 'Test' requires 256 bytes of alignment and the default allocator only guarantees}}
}
}
namespace test3 {
struct Test {
template <typename T>
struct SeparateCacheLines {
T data;
} __attribute__((aligned(256)));
void* operator new(unsigned long) {
return 0; // precxx17-warning {{'operator new' should not return a null pointer unless it is declared 'throw()'}} \
cxx17-warning {{'operator new' should not return a null pointer unless it is declared 'throw()' or 'noexcept'}}
}
SeparateCacheLines<int> high_contention_data[10];
};
void helper() {
Test t;
new Test;
new Test[10]; // precxx17-warning {{type 'Test' requires 256 bytes of alignment and the default allocator only guarantees}}
}
}
namespace test4 {
struct Test {
template <typename T>
struct SeparateCacheLines {
T data;
} __attribute__((aligned(256)));
void* operator new[](unsigned long) {
return 0; // precxx17-warning {{'operator new[]' should not return a null pointer unless it is declared 'throw()'}} \
cxx17-warning {{'operator new[]' should not return a null pointer unless it is declared 'throw()' or 'noexcept'}}
}
SeparateCacheLines<int> high_contention_data[10];
};
void helper() {
Test t;
new Test; // precxx17-warning {{type 'Test' requires 256 bytes of alignment and the default allocator only guarantees}}
new Test[10];
}
}