Skip to content

Commit ebf2d15

Browse files
committed
[clang][AVR] Fix basic type size/alignment values to match avr-gcc.
Closes #102172.
1 parent fb0ef6b commit ebf2d15

File tree

4 files changed

+60
-7
lines changed

4 files changed

+60
-7
lines changed

clang/include/clang/Basic/TargetInfo.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ enum class FloatModeKind {
8787
struct TransferrableTargetInfo {
8888
unsigned char PointerWidth, PointerAlign;
8989
unsigned char BoolWidth, BoolAlign;
90+
unsigned char ShortWidth, ShortAlign;
9091
unsigned char IntWidth, IntAlign;
9192
unsigned char HalfWidth, HalfAlign;
9293
unsigned char BFloat16Width, BFloat16Align;
@@ -497,13 +498,10 @@ class TargetInfo : public TransferrableTargetInfo,
497498
unsigned getCharWidth() const { return 8; } // FIXME
498499
unsigned getCharAlign() const { return 8; } // FIXME
499500

500-
/// Return the size of 'signed short' and 'unsigned short' for this
501-
/// target, in bits.
502-
unsigned getShortWidth() const { return 16; } // FIXME
503-
504-
/// Return the alignment of 'signed short' and 'unsigned short' for
505-
/// this target.
506-
unsigned getShortAlign() const { return 16; } // FIXME
501+
/// getShortWidth/Align - Return the size of 'signed short' and
502+
/// 'unsigned short' for this target, in bits.
503+
unsigned getShortWidth() const { return ShortWidth; }
504+
unsigned getShortAlign() const { return ShortAlign; }
507505

508506
/// getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for
509507
/// this target, in bits.

clang/lib/Basic/TargetInfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ TargetInfo::TargetInfo(const llvm::Triple &T) : Triple(T) {
7070
HasStrictFP = false;
7171
PointerWidth = PointerAlign = 32;
7272
BoolWidth = BoolAlign = 8;
73+
ShortWidth = ShortAlign = 16;
7374
IntWidth = IntAlign = 32;
7475
LongWidth = LongAlign = 32;
7576
LongLongWidth = LongLongAlign = 64;
@@ -437,6 +438,7 @@ void TargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) {
437438
// what these normally are for the target.
438439
// We also define long long and long double here, although the
439440
// OpenCL standard only mentions these as "reserved".
441+
ShortWidth = ShortAlign = 16;
440442
IntWidth = IntAlign = 32;
441443
LongWidth = LongAlign = 64;
442444
LongLongWidth = LongLongAlign = 128;

clang/lib/Basic/Targets/AVR.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class LLVM_LIBRARY_VISIBILITY AVRTargetInfo : public TargetInfo {
2929
TLSSupported = false;
3030
PointerWidth = 16;
3131
PointerAlign = 8;
32+
ShortWidth = 16;
33+
ShortAlign = 8;
3234
IntWidth = 16;
3335
IntAlign = 8;
3436
LongWidth = 32;
@@ -65,6 +67,8 @@ class LLVM_LIBRARY_VISIBILITY AVRTargetInfo : public TargetInfo {
6567
return std::nullopt;
6668
}
6769

70+
bool allowsLargerPreferedTypeAlignment() const override { return false; }
71+
6872
BuiltinVaListKind getBuiltinVaListKind() const override {
6973
return TargetInfo::VoidPtrBuiltinVaList;
7074
}

clang/test/Sema/avr-size-align.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// RUN: %clang_cc1 %s -triple avr -fsyntax-only
2+
3+
_Static_assert(sizeof(char) == 1, "sizeof(char) == 1");
4+
_Static_assert(_Alignof(char) == 1, "_Alignof(char) == 1");
5+
_Static_assert(__alignof(char) == 1, "__alignof(char) == 1");
6+
7+
_Static_assert(sizeof(short) == 2, "sizeof(short) == 2");
8+
_Static_assert(_Alignof(short) == 1, "_Alignof(short) == 1");
9+
_Static_assert(__alignof(short) == 1, "__alignof(short) == 1");
10+
11+
_Static_assert(sizeof(unsigned short) == 2, "sizeof(unsigned short) == 2");
12+
_Static_assert(_Alignof(unsigned short) == 1, "_Alignof(unsigned short) == 1");
13+
_Static_assert(__alignof(unsigned short) == 1, "__alignof(unsigned short) == 1");
14+
15+
_Static_assert(sizeof(int) == 2, "sizeof(int) == 2");
16+
_Static_assert(_Alignof(int) == 1, "_Alignof(int) == 1");
17+
_Static_assert(__alignof(int) == 1, "__alignof(int) == 1");
18+
19+
_Static_assert(sizeof(unsigned int) == 2, "sizeof(unsigned int) == 2");
20+
_Static_assert(_Alignof(unsigned int) == 1, "_Alignof(unsigned int) == 1");
21+
_Static_assert(__alignof(unsigned int) == 1, "__alignof(unsigned int) == 1");
22+
23+
_Static_assert(sizeof(long) == 4, "sizeof(long) == 4");
24+
_Static_assert(_Alignof(long) == 1, "_Alignof(long) == 1");
25+
_Static_assert(__alignof(long) == 1, "__alignof(long) == 1");
26+
27+
_Static_assert(sizeof(unsigned long) == 4, "sizeof(unsigned long) == 4");
28+
_Static_assert(_Alignof(unsigned long) == 1, "_Alignof(unsigned long) == 1");
29+
_Static_assert(__alignof(unsigned long) == 1, "__alignof(unsigned long) == 1");
30+
31+
_Static_assert(sizeof(long long) == 8, "sizeof(long long) == 8");
32+
_Static_assert(_Alignof(long long) == 1, "_Alignof(long long) == 1");
33+
_Static_assert(__alignof(long long) == 1, "__alignof(long long) == 1");
34+
35+
_Static_assert(sizeof(unsigned long long) == 8, "sizeof(unsigned long long) == 8");
36+
_Static_assert(_Alignof(unsigned long long) == 1, "_Alignof(unsigned long long) == 1");
37+
_Static_assert(__alignof(unsigned long long) == 1, "__alignof(unsigned long long) == 1");
38+
39+
_Static_assert(sizeof(float) == 4, "sizeof(float) == 4");
40+
_Static_assert(_Alignof(float) == 1, "_Alignof(float) == 1");
41+
_Static_assert(__alignof(float) == 1, "__alignof(float) == 1");
42+
43+
_Static_assert(sizeof(double) == 4, "sizeof(double) == 4");
44+
_Static_assert(_Alignof(double) == 1, "_Alignof(double) == 1");
45+
_Static_assert(__alignof(double) == 1, "__alignof(double) == 1");
46+
47+
_Static_assert(sizeof(long double) == 4, "sizeof(long double) == 4");
48+
_Static_assert(_Alignof(long double) == 1, "_Alignof(long double) == 1");
49+
_Static_assert(__alignof(long double) == 1, "__alignof(long double) == 1");

0 commit comments

Comments
 (0)