Skip to content

Commit 23d77e4

Browse files
committed
Merge pull request #97 from geky/compiler-polyfill
Add useful attributes supported by supported compilers
2 parents 02a23c2 + 018e257 commit 23d77e4

File tree

4 files changed

+406
-7
lines changed

4 files changed

+406
-7
lines changed

hal/TESTS/api/toolchain/attributes.c

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include "toolchain.h"
2+
3+
#include <stdio.h>
4+
#include <stdint.h>
5+
6+
7+
typedef struct {
8+
char a;
9+
int x;
10+
} PACKED TestAttrPackedStruct;
11+
12+
int testPacked() {
13+
int failed = 0;
14+
15+
if (sizeof(TestAttrPackedStruct) != sizeof(int) + sizeof(char)) {
16+
failed++;
17+
}
18+
19+
return failed;
20+
}
21+
22+
23+
int testAlign() {
24+
int failed = 0;
25+
26+
ALIGN(8) char a;
27+
ALIGN(8) char b;
28+
ALIGN(16) char c;
29+
ALIGN(8) char d;
30+
ALIGN(16) char e;
31+
32+
if(((uintptr_t)&a) & 0x7){
33+
failed++;
34+
}
35+
if(((uintptr_t)&b) & 0x7){
36+
failed++;
37+
}
38+
if(((uintptr_t)&c) & 0xf){
39+
failed++;
40+
}
41+
if(((uintptr_t)&d) & 0x7){
42+
failed++;
43+
}
44+
if(((uintptr_t)&e) & 0xf){
45+
failed++;
46+
}
47+
48+
return failed;
49+
}
50+
51+
52+
int testUnused1(UNUSED int arg) {
53+
return 0;
54+
}
55+
56+
int testUnused() {
57+
return testUnused1(0);
58+
}
59+
60+
61+
int testWeak1();
62+
int testWeak2();
63+
64+
WEAK int testWeak1() {
65+
return 1;
66+
}
67+
68+
int testWeak2() {
69+
return 0;
70+
}
71+
72+
int testWeak() {
73+
return testWeak1() | testWeak2();
74+
}
75+
76+
77+
PURE int testPure1() {
78+
return 0;
79+
}
80+
81+
int testPure() {
82+
return testPure1();
83+
}
84+
85+
86+
FORCEINLINE int testForceInline1() {
87+
return 0;
88+
}
89+
90+
int testForceInline() {
91+
return testForceInline1();
92+
}
93+
94+
95+
NORETURN int testNoReturn1() {
96+
while (1) {}
97+
}
98+
99+
int testNoReturn() {
100+
if (0) {
101+
testNoReturn1();
102+
}
103+
return 0;
104+
}
105+
106+
107+
int testUnreachable1(int i) {
108+
switch (i) {
109+
case 0:
110+
return 0;
111+
}
112+
113+
UNREACHABLE;
114+
}
115+
116+
int testUnreachable() {
117+
return testUnreachable1(0);
118+
}
119+
120+
121+
DEPRECATED("this message should not be displayed")
122+
void testDeprecatedUnused();
123+
void testDeprecatedUnused() { }
124+
125+
DEPRECATED("this message should be displayed")
126+
int testDeprecatedUsed();
127+
int testDeprecatedUsed() {
128+
return 0;
129+
}
130+
131+
int testDeprecated() {
132+
return testDeprecatedUsed();
133+
}
134+

hal/TESTS/api/toolchain/main.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <stdio.h>
2+
#include <stdint.h>
3+
4+
#include "toolchain.h"
5+
#include "test_env.h"
6+
#include "unity.h"
7+
#include "utest.h"
8+
9+
using namespace utest::v1;
10+
11+
12+
// Test functions declared as C functions to avoid issues with name mangling
13+
extern "C" {
14+
int testPacked();
15+
int testAlign();
16+
int testUnused();
17+
int testWeak();
18+
int testPure();
19+
int testForceInline();
20+
int testNoReturn();
21+
int testUnreachable();
22+
int testDeprecated();
23+
}
24+
25+
26+
// Test wrapper and test cases for utest
27+
template <int (*F)()>
28+
void test_wrapper() {
29+
TEST_ASSERT_UNLESS(F());
30+
}
31+
32+
status_t test_setup(const size_t number_of_cases) {
33+
GREENTEA_SETUP(40, "default_auto");
34+
return verbose_test_setup_handler(number_of_cases);
35+
}
36+
37+
Case cases[] = {
38+
Case("Testing PACKED attribute", test_wrapper<testPacked>),
39+
Case("Testing ALIGN attribute", test_wrapper<testAlign>),
40+
Case("Testing UNUSED attribute", test_wrapper<testUnused>),
41+
Case("Testing WEAK attribute", test_wrapper<testWeak>),
42+
Case("Testing PURE attribute", test_wrapper<testPure>),
43+
Case("Testing FORCEINLINE attribute", test_wrapper<testForceInline>),
44+
Case("Testing NORETURN attribute", test_wrapper<testNoReturn>),
45+
Case("Testing UNREACHABLE attribute", test_wrapper<testUnreachable>),
46+
Case("Testing DEPRECATED attribute", test_wrapper<testDeprecated>),
47+
};
48+
49+
Specification specification(test_setup, cases);
50+
51+
int main() {
52+
return !Harness::run(specification);
53+
}

hal/TESTS/api/toolchain/weak.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "toolchain.h"
2+
3+
int testWeak1() {
4+
return 0;
5+
}
6+
7+
WEAK int testWeak2() {
8+
return 1;
9+
}
10+

0 commit comments

Comments
 (0)