Skip to content

Commit 8ec3b83

Browse files
committed
checkpoint
1 parent 422b00b commit 8ec3b83

File tree

9 files changed

+437
-1
lines changed

9 files changed

+437
-1
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
"Iterators",
222222
"Lambdas",
223223
"Language1",
224+
"Language2",
224225
"Literals",
225226
"Loops",
226227
"Macros",
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @id c/misra/language-extensions-should-not-be-used
3+
* @name RULE-1-2: Language extensions should not be used
4+
* @description Language extensions can have inconsistent behavior and should not be used.
5+
* @kind problem
6+
* @precision high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-1-2
9+
* maintainability
10+
* readability
11+
* external/misra/obligation/advisory
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
17+
from
18+
where
19+
not isExcluded(x, Language2Package::languageExtensionsShouldNotBeUsedQuery()) and
20+
select
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
No expected results have yet been specified
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-1-2/LanguageExtensionsShouldNotBeUsed.ql

c/misra/test/rules/RULE-1-2/test.c

Lines changed: 361 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,361 @@
1+
#include <stdbool.h>
2+
#include <stdio.h>
3+
// Note: Clang aims to support both clang and gcc extensions.
4+
// This test case has been designed using lists compiled from:
5+
// - https://clang.llvm.org/docs/LanguageExtensions.html
6+
// - https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html
7+
8+
#ifdef __has_builtin // NON_COMPLIANT
9+
#endif
10+
#ifdef __has_constexpr_builtin // NON_COMPLIANT
11+
#endif
12+
#ifdef __has_feature // NON_COMPLIANT
13+
#endif
14+
#ifdef __has_extension // NON_COMPLIANT
15+
#endif
16+
#ifdef __has_c_attribute // NON_COMPLIANT
17+
#endif
18+
#ifdef __has_attribute // NON_COMPLIANT
19+
#endif
20+
#ifdef __has_declspec_attribute // NON_COMPLIANT
21+
#endif
22+
#ifdef __is_identifier // NON_COMPLIANT
23+
#endif
24+
#ifdef __has_include // NON_COMPLIANT
25+
#endif
26+
#ifdef __has_include_next // NON_COMPLIANT
27+
#endif
28+
#ifdef __has_warning // NON_COMPLIANT
29+
#endif
30+
31+
#define A __BASE_FILE__ // NON_COMPLIANT
32+
#define B __FILE_NAME__ // NON_COMPLIANT
33+
#define C __COUNTER__ // NON_COMPLIANT
34+
#define D __INCLUDE_LEVEL__ // NON_COMPLIANT
35+
#define E__TIMESTAMP__ // NON_COMPLIANT
36+
#define F __clang__ // NON_COMPLIANT
37+
#define G __clang_major__ // NON_COMPLIANT
38+
#define H __clang_minor__ // NON_COMPLIANT
39+
#define I __clang_patchlevel__ // NON_COMPLIANT
40+
#define J __clang_version__ // NON_COMPLIANT
41+
#define K __clang_literal_encoding__ // NON_COMPLIANT
42+
#define L __clang_wide_literal_encoding__ // NON_COMPLIANT
43+
44+
typedef float float4 __attribute__((ext_vector_type(4))); // NON_COMPLIANT
45+
typedef float float2 __attribute__((ext_vector_type(2))); // NON_COMPLIANT
46+
47+
// Requires additional compiler flags to change the architecture
48+
// typedef __attribute__((neon_vector_type(8))) int8_t int8x8_t;
49+
// typedef __attribute__((neon_polyvector_type(16))) poly8_t poly8x16_t;
50+
51+
typedef int int4 __attribute__((vector_size(4 * sizeof(int)))); // NON_COMPLIANT
52+
53+
typedef int v4si __attribute__((__vector_size__(16))); // NON_COMPLIANT
54+
typedef float float4 __attribute__((ext_vector_type(4))); // NON_COMPLIANT
55+
typedef float float2 __attribute__((ext_vector_type(2))); // NON_COMPLIANT
56+
57+
//// GCC features
58+
void gf1() {
59+
({
60+
int y = 1;
61+
int z; // NON_COMPLIANT
62+
if (y > 0)
63+
z = y;
64+
else
65+
z = -y;
66+
z;
67+
});
68+
}
69+
70+
void gf2() {
71+
// __label__ found; -- local labels not supported by clang
72+
}
73+
74+
void gf3() {
75+
void *ptr;
76+
// goto *ptr; -- not supported in clang
77+
}
78+
79+
void gf4() {
80+
// void gf4a(){ -- not supported in clang
81+
//
82+
// }
83+
}
84+
85+
void gf5() {
86+
__builtin_setjmp(0); // NON_COMPLIANT
87+
__builtin_longjmp(0, 1); // NON_COMPLIANT
88+
}
89+
90+
void gf6() {
91+
// not supported by clang
92+
93+
//__builtin_apply_args();
94+
//__builtin_apply(0, 0, 0);
95+
//__builtin_return(0);
96+
//__builtin_va_arg_pack();
97+
//__builtin_va_arg_pack_len();
98+
}
99+
100+
void gf7() {
101+
int a = 0 ?: 0; // NON_COMPLIANT
102+
}
103+
104+
void gf8() {
105+
typeof(int *); // NON_COMPLIANT
106+
}
107+
108+
void gf9() {
109+
__int128 a; // NON_COMPLIANT
110+
}
111+
112+
void gf10() {
113+
long long int a; // NON_COMPLIANT
114+
}
115+
116+
void gf11() {
117+
__real__(0); // NON_COMPLIANT
118+
__imag__(0); // NON_COMPLIANT
119+
}
120+
121+
void gf12() {}
122+
123+
void gf13() {
124+
// not supported on clang
125+
126+
//_Decimal32 a;
127+
//_Decimal64 b;
128+
//_Decimal128 c;
129+
}
130+
131+
void gf14() {
132+
// Not sure how to get this to work.
133+
// typedef _Complex float __attribute__((mode(TC))) _Complex128;
134+
// typedef _Complex float __attribute__((mode(XC))) _Complex80;
135+
}
136+
137+
void gf15() {
138+
float f = 0x1.fp3; // NON_COMPLIANT
139+
}
140+
141+
void gf16() {
142+
char contents[0]; // NON_COMPLIANT
143+
}
144+
145+
void gf17() {
146+
// const __flash char ** p; // not supported in clang
147+
}
148+
149+
void gf18() {
150+
// not supported by extractor - checked by looking for flags.
151+
152+
// short _Fract, _Fract;
153+
// long _Fract;
154+
}
155+
156+
struct gf19 {}; // NON_COMPLIANT
157+
158+
void gf20(int n) {
159+
// struct S { int x[n]; }; // will never be supported in clang
160+
}
161+
162+
#define gf21(format, args...) \
163+
printf(format, args) // NON_COMPLIANT -- note the issue here is explicitly
164+
// naming the arguments.
165+
#define gf21a(format, ...) printf(format, __VA_ARGS__) // COMPLIANT
166+
167+
#define gf22 \
168+
"a" \
169+
\
170+
"b" // NON_COMPLIANT - additional spaces after a backslash
171+
#define gf22a \
172+
"a" \
173+
"b" // COMPLIANT
174+
175+
struct gf23s {
176+
int a[1];
177+
};
178+
struct gf23s gf23f();
179+
void gf23() {
180+
gf23f().a[0]; // NON_COMPLIANT in C90
181+
}
182+
183+
void gf24(int f, int g) {
184+
float beat_freqs[2] = {f - g, f + g}; // NON_COMPLIANT
185+
}
186+
187+
void gf25t(int N, int M, double out[M][N], const double in[N][M]);
188+
void gf25() {
189+
double x[3][2];
190+
double y[2][3];
191+
gf25t(3, 2, y,
192+
x); // NON_COMPLIANT - in ISO C the const qualifier is formally attached
193+
// to the element type of the array and not the array itself
194+
}
195+
196+
struct gf26t {
197+
int a;
198+
char b[2];
199+
} gf26v;
200+
void gf26(int x, int y) {
201+
gf26v = ((struct gf26t){x + y, 'z', 0}); // NON_COMPLIANT - compound literal
202+
}
203+
204+
void gf27() {
205+
int a[6] = {[4] = 29, [2] = 15}; // NON_COMPLIANT in C90.
206+
}
207+
208+
void gf28() {
209+
int a;
210+
211+
// switch(a){
212+
// case: 0 ... 5: // Not supported in clang.
213+
// ;;
214+
// break;
215+
// default:
216+
// ;;
217+
// break;
218+
// }
219+
}
220+
221+
union gf29u {
222+
int i;
223+
double j;
224+
};
225+
226+
void gf29() {
227+
int x;
228+
int y;
229+
union gf29u z;
230+
z = (union gf29u)x; // NON_COMPLIANT
231+
z = (union gf29u)y; // NON_COMPLIANT
232+
}
233+
234+
__attribute__((access(read_only, 1))) int
235+
gf30(const char *); // NON_COMPLIANT -- attributes are not portable.
236+
237+
extern int __attribute__((alias("var_target")))
238+
gf31; // NON_COMPLIANT -- attributes are not portable.
239+
240+
struct __attribute__((aligned(8))) gf32 {
241+
short f[3];
242+
}; // NON_COMPLIANT -- attributes are not portable.
243+
244+
void gf33() {
245+
gf33l:
246+
__attribute__((cold, unused)); // NON_COMPLIANT
247+
return;
248+
}
249+
250+
enum gf34 {
251+
oldval __attribute__((deprecated)), // NON_COMPLIANT
252+
newval
253+
};
254+
255+
void gf35() {
256+
int x;
257+
// __attribute__((assume(x == 42))); - Not supported in clang
258+
259+
switch (x) {
260+
case 1:
261+
printf("");
262+
__attribute__((fallthrough)); // NON_COMPLIANT
263+
case 2:
264+
break;
265+
}
266+
}
267+
268+
// Not supported in clang.
269+
// int gf36 (uid_t);
270+
271+
// int
272+
// gf36 (int x)
273+
// {
274+
// return x == 0;
275+
// }
276+
277+
void gf37() {
278+
int a$1; // NON_COMPLIANT
279+
}
280+
281+
void gf38() {
282+
const char *c = "test\e"; // NON_COMPLIANT
283+
}
284+
285+
struct gf39s {
286+
int x;
287+
char y;
288+
} gf39v;
289+
290+
void gf39() {
291+
__alignof__(gf39v.x); // NON_COMPLIANT
292+
}
293+
294+
// enum gf40 {}; // not supported in clang
295+
296+
void gf41() {
297+
printf("__FUNCTION__ = %s\n", __FUNCTION__); // NON_COMPLIANT
298+
printf("__PRETTY_FUNCTION__ = %s\n", __PRETTY_FUNCTION__); // NON_COMPLIANT
299+
}
300+
301+
void gf42() {
302+
__builtin_extract_return_addr(0);
303+
__builtin_frob_return_addr(0);
304+
__builtin_frame_address(0);
305+
}
306+
307+
struct gf43s {
308+
int x;
309+
char y;
310+
} gf43v;
311+
312+
void gf43() {
313+
__builtin_offsetof(struct gf43s, x); // NON_COMPLIANT
314+
}
315+
316+
struct gf44s {
317+
int x;
318+
char y;
319+
} gf44v;
320+
321+
void gf44() {
322+
int i;
323+
__sync_fetch_and_add(&i, 0); // NON_COMPLIANT
324+
__sync_fetch_and_sub(&i, 0); // NON_COMPLIANT
325+
__sync_fetch_and_or(&i, 0); // NON_COMPLIANT
326+
__sync_fetch_and_and(&i, 0); // NON_COMPLIANT
327+
__sync_fetch_and_xor(&i, 0); // NON_COMPLIANT
328+
__sync_fetch_and_nand(&i, 0); // NON_COMPLIANT
329+
__sync_add_and_fetch(&i, 0); // NON_COMPLIANT
330+
__sync_sub_and_fetch(&i, 0); // NON_COMPLIANT
331+
__sync_or_and_fetch(&i, 0); // NON_COMPLIANT
332+
__sync_and_and_fetch(&i, 0); // NON_COMPLIANT
333+
__sync_xor_and_fetch(&i, 0); // NON_COMPLIANT
334+
__sync_nand_and_fetch(&i, 0); // NON_COMPLIANT
335+
336+
__sync_bool_compare_and_swap(&i, 0, 0);
337+
__sync_val_compare_and_swap(&i, 0, 0);
338+
__sync_lock_test_and_set(&i, 0, 0);
339+
__sync_lock_release(&i, 0);
340+
}
341+
342+
void gf45() {
343+
int i = 0b101010; // NON_COMPLIANT
344+
}
345+
346+
__thread int gf46; // NON_COMPLIANT
347+
348+
void gf47() { // NON_COMPLIANT in versions < C11.
349+
struct {
350+
int a;
351+
union {
352+
int b;
353+
float c;
354+
};
355+
int d;
356+
} f;
357+
}
358+
359+
void gf48(){
360+
__builtin_alloca(0); // NON_COMPLIANT (all __builtin functions are non-compliant.)
361+
}

0 commit comments

Comments
 (0)