Skip to content

Commit dc8df30

Browse files
authored
Merge pull request #264 from github/jsinglet/language3
Language 3
2 parents 1c3ad1c + 8bfc092 commit dc8df30

File tree

17 files changed

+795
-0
lines changed

17 files changed

+795
-0
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@
236236
"Lambdas",
237237
"Language1",
238238
"Language2",
239+
"Language3",
239240
"Literals",
240241
"Loops",
241242
"Macros",
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import cpp
2+
import codingstandards.cpp.Extensions
3+
4+
/**
5+
* Common base class for modeling compiler extensions.
6+
*/
7+
abstract class CCompilerExtension extends CompilerExtension { }
8+
9+
// Reference: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#Other-Builtins
10+
abstract class CConditionalDefineExtension extends CCompilerExtension, PreprocessorIfdef {
11+
CConditionalDefineExtension() {
12+
exists(toString().indexOf("__has_builtin")) or
13+
exists(toString().indexOf("__has_constexpr_builtin")) or
14+
exists(toString().indexOf("__has_feature")) or
15+
exists(toString().indexOf("__has_extension")) or
16+
exists(toString().indexOf("__has_attribute")) or
17+
exists(toString().indexOf("__has_declspec_attribute")) or
18+
exists(toString().indexOf("__is_identifier")) or
19+
exists(toString().indexOf("__has_include")) or
20+
exists(toString().indexOf("__has_include_next")) or
21+
exists(toString().indexOf("__has_warning"))
22+
}
23+
}
24+
25+
// Reference: https://clang.llvm.org/docs/LanguageExtensions.html#builtin-macros
26+
class CMacroBasedExtension extends CCompilerExtension, Macro {
27+
CMacroBasedExtension() {
28+
getBody() in [
29+
"__BASE_FILE__", "__FILE_NAME__", "__COUNTER__", "__INCLUDE_LEVEL__", "_TIMESTAMP__",
30+
"__clang__", "__clang_major__", "__clang_minor__", "__clang_patchlevel__",
31+
"__clang_version__", "__clang_literal_encoding__", "__clang_wide_literal_encoding__"
32+
]
33+
}
34+
}
35+
36+
// Reference: https://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#Variable-Attributes
37+
class CAttributeExtension extends CCompilerExtension, Attribute {
38+
CAttributeExtension() {
39+
getName() in [
40+
"ext_vector_type", "vector_size", "access", "aligned", "deprecated", "cold", "unused",
41+
"fallthrough", "read_only", "alias"
42+
]
43+
}
44+
}
45+
46+
// Reference: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html#g_t_005f_005fsync-Builtins
47+
class CFunctionExtension extends CCompilerExtension, FunctionCall {
48+
CFunctionExtension() {
49+
// these must be somewhat broad because of how they vary
50+
// in implementation / naming
51+
getTarget().getName().indexOf("__sync_fetch") = 0 or
52+
getTarget().getName().indexOf("__sync_add") = 0 or
53+
getTarget().getName().indexOf("__sync_sub") = 0 or
54+
getTarget().getName().indexOf("__sync_or") = 0 or
55+
getTarget().getName().indexOf("__sync_and") = 0 or
56+
getTarget().getName().indexOf("__sync_xor") = 0 or
57+
getTarget().getName().indexOf("__sync_nand") = 0 or
58+
getTarget().getName().indexOf("__sync_bool") = 0 or
59+
getTarget().getName().indexOf("__sync_val") = 0 or
60+
getTarget().getName().indexOf("__sync_lock") = 0 or
61+
// the built-in extensions
62+
getTarget().getName().indexOf("__builtin_") = 0
63+
}
64+
}
65+
66+
// Reference: https://gcc.gnu.org/onlinedocs/gcc/Alignment.html#Alignment
67+
class CFunctionLikeExtension extends CCompilerExtension, AlignofExprOperator {
68+
CFunctionLikeExtension() { exists(getValueText().indexOf("__alignof__")) }
69+
}
70+
71+
// Reference: https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html#Statement-Exprs
72+
class CStmtExprExtension extends CCompilerExtension, StmtExpr { }
73+
74+
// Use of ternary like the following: `int a = 0 ?: 0;` where the
75+
// one of the branches is omitted
76+
// Reference: https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html#Conditionals
77+
class CTerseTernaryExtension extends CCompilerExtension, ConditionalExpr {
78+
CTerseTernaryExtension() { getCondition() = getElse() or getCondition() = getThen() }
79+
}
80+
81+
// Reference: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fint128.html#g_t_005f_005fint128
82+
// Reference: https://gcc.gnu.org/onlinedocs/gcc/Decimal-Float.html#Decimal-Float
83+
class CRealTypeExtensionExtension extends CCompilerExtension, DeclarationEntry {
84+
CRealTypeExtensionExtension() {
85+
getType() instanceof Decimal128Type or
86+
getType() instanceof Decimal32Type or
87+
getType() instanceof Decimal64Type or
88+
getType() instanceof Float128Type
89+
}
90+
}
91+
92+
// Reference: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fint128.html#g_t_005f_005fint128
93+
class CIntegerTypeExtension extends CCompilerExtension, DeclarationEntry {
94+
CIntegerTypeExtension() { getType() instanceof Int128Type }
95+
}
96+
97+
// Reference: https://gcc.gnu.org/onlinedocs/gcc/Long-Long.html#Long-Long
98+
class CLongLongType extends CCompilerExtension, DeclarationEntry {
99+
CLongLongType() { getType() instanceof LongLongType }
100+
}
101+
102+
class CZeroLengthArraysExtension extends CCompilerExtension, DeclarationEntry {
103+
CZeroLengthArraysExtension() { getType().(ArrayType).getArraySize() = 0 }
104+
}
105+
106+
// Reference: https://gcc.gnu.org/onlinedocs/gcc/Empty-Structures.html#Empty-Structures
107+
class CEmptyStructExtension extends CCompilerExtension, Struct {
108+
CEmptyStructExtension() { not exists(getAMember(_)) }
109+
}
110+
111+
// Reference: https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html#Variable-Length
112+
class CVariableLengthArraysExtension extends CCompilerExtension, DeclarationEntry {
113+
CVariableLengthArraysExtension() {
114+
getType() instanceof ArrayType and
115+
not getType().(ArrayType).hasArraySize()
116+
}
117+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import cpp
2+
import codingstandards.cpp.UndefinedBehavior
3+
4+
/**
5+
* Library for modeling undefined behavior.
6+
*/
7+
abstract class CUndefinedBehavior extends UndefinedBehavior { }
8+
9+
class C99MainFunction extends Function {
10+
C99MainFunction() {
11+
this.getNumberOfParameters() = 2 and
12+
this.getType() instanceof IntType and
13+
this.getParameter(0).getType() instanceof IntType and
14+
this.getParameter(1).getType().(PointerType).getBaseType().(PointerType).getBaseType()
15+
instanceof CharType
16+
or
17+
this.getNumberOfParameters() = 0 and
18+
this.getType() instanceof VoidType
19+
}
20+
}
21+
22+
class CUndefinedMainDefinition extends CUndefinedBehavior, Function {
23+
CUndefinedMainDefinition() {
24+
// for testing purposes, we use the prefix ____codeql_coding_standards`
25+
(this.getName() = "main" or this.getName().indexOf("____codeql_coding_standards") = 0) and
26+
not this instanceof C99MainFunction
27+
}
28+
}
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 are not portable to other compilers 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+
import codingstandards.c.Extensions
17+
18+
from CCompilerExtension e
19+
where not isExcluded(e, Language3Package::languageExtensionsShouldNotBeUsedQuery())
20+
select e, "Is a compiler extension and is not portable to other compilers."
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @id c/misra/occurrence-of-undefined-behavior
3+
* @name RULE-1-3: There shall be no occurrence of undefined or critical unspecified behavior
4+
* @description Relying on undefined or unspecified behavior can result in unreliable programs.
5+
* @kind problem
6+
* @precision high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-1-3
9+
* maintainability
10+
* readability
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.c.UndefinedBehavior
17+
18+
from CUndefinedBehavior c
19+
where not isExcluded(c, Language3Package::occurrenceOfUndefinedBehaviorQuery())
20+
select c, "May result in undefined behavior."
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
| test.c:34:1:34:23 | #define A __BASE_FILE__ | Is a compiler extension and is not portable to other compilers. |
2+
| test.c:35:1:35:23 | #define B __FILE_NAME__ | Is a compiler extension and is not portable to other compilers. |
3+
| test.c:36:1:36:21 | #define C __COUNTER__ | Is a compiler extension and is not portable to other compilers. |
4+
| test.c:37:1:37:27 | #define D __INCLUDE_LEVEL__ | Is a compiler extension and is not portable to other compilers. |
5+
| test.c:39:1:39:19 | #define F __clang__ | Is a compiler extension and is not portable to other compilers. |
6+
| test.c:40:1:40:25 | #define G __clang_major__ | Is a compiler extension and is not portable to other compilers. |
7+
| test.c:41:1:41:25 | #define H __clang_minor__ | Is a compiler extension and is not portable to other compilers. |
8+
| test.c:42:1:42:30 | #define I __clang_patchlevel__ | Is a compiler extension and is not portable to other compilers. |
9+
| test.c:43:1:43:27 | #define J __clang_version__ | Is a compiler extension and is not portable to other compilers. |
10+
| test.c:44:1:44:36 | #define K __clang_literal_encoding__ | Is a compiler extension and is not portable to other compilers. |
11+
| test.c:45:1:45:41 | #define L __clang_wide_literal_encoding__ | Is a compiler extension and is not portable to other compilers. |
12+
| test.c:53:33:53:43 | vector_size | Is a compiler extension and is not portable to other compilers. |
13+
| test.c:54:33:54:47 | vector_size | Is a compiler extension and is not portable to other compilers. |
14+
| test.c:55:37:55:51 | ext_vector_type | Is a compiler extension and is not portable to other compilers. |
15+
| test.c:56:37:56:51 | ext_vector_type | Is a compiler extension and is not portable to other compilers. |
16+
| test.c:61:3:69:4 | (statement expression) | Is a compiler extension and is not portable to other compilers. |
17+
| test.c:96:3:96:18 | call to __builtin_setjmp | Is a compiler extension and is not portable to other compilers. |
18+
| test.c:97:3:97:19 | call to __builtin_longjmp | Is a compiler extension and is not portable to other compilers. |
19+
| test.c:113:11:113:16 | ... ? ... : ... | Is a compiler extension and is not portable to other compilers. |
20+
| test.c:124:12:124:12 | definition of a | Is a compiler extension and is not portable to other compilers. |
21+
| test.c:128:17:128:17 | definition of a | Is a compiler extension and is not portable to other compilers. |
22+
| test.c:165:8:165:15 | definition of contents | Is a compiler extension and is not portable to other compilers. |
23+
| test.c:182:8:182:11 | gf19 | Is a compiler extension and is not portable to other compilers. |
24+
| test.c:214:33:214:35 | declaration of out | Is a compiler extension and is not portable to other compilers. |
25+
| test.c:215:25:215:26 | declaration of in | Is a compiler extension and is not portable to other compilers. |
26+
| test.c:268:16:268:21 | access | Is a compiler extension and is not portable to other compilers. |
27+
| test.c:271:27:271:31 | alias | Is a compiler extension and is not portable to other compilers. |
28+
| test.c:274:23:274:29 | aligned | Is a compiler extension and is not portable to other compilers. |
29+
| test.c:285:25:285:34 | deprecated | Is a compiler extension and is not portable to other compilers. |
30+
| test.c:297:20:297:30 | fallthrough | Is a compiler extension and is not portable to other compilers. |
31+
| test.c:321:3:321:22 | alignof(<expr>) | Is a compiler extension and is not portable to other compilers. |
32+
| test.c:340:3:340:31 | call to __builtin_extract_return_addr | Is a compiler extension and is not portable to other compilers. |
33+
| test.c:341:3:341:28 | call to __builtin_frob_return_addr | Is a compiler extension and is not portable to other compilers. |
34+
| test.c:342:3:342:25 | call to __builtin_frame_address | Is a compiler extension and is not portable to other compilers. |
35+
| test.c:363:3:363:22 | call to __sync_fetch_and_add_4 | Is a compiler extension and is not portable to other compilers. |
36+
| test.c:364:3:364:22 | call to __sync_fetch_and_sub_4 | Is a compiler extension and is not portable to other compilers. |
37+
| test.c:365:3:365:21 | call to __sync_fetch_and_or_4 | Is a compiler extension and is not portable to other compilers. |
38+
| test.c:366:3:366:22 | call to __sync_fetch_and_and_4 | Is a compiler extension and is not portable to other compilers. |
39+
| test.c:367:3:367:22 | call to __sync_fetch_and_xor_4 | Is a compiler extension and is not portable to other compilers. |
40+
| test.c:368:3:368:23 | call to __sync_fetch_and_nand_4 | Is a compiler extension and is not portable to other compilers. |
41+
| test.c:369:3:369:22 | call to __sync_add_and_fetch_4 | Is a compiler extension and is not portable to other compilers. |
42+
| test.c:370:3:370:22 | call to __sync_sub_and_fetch_4 | Is a compiler extension and is not portable to other compilers. |
43+
| test.c:371:3:371:21 | call to __sync_or_and_fetch_4 | Is a compiler extension and is not portable to other compilers. |
44+
| test.c:372:3:372:22 | call to __sync_and_and_fetch_4 | Is a compiler extension and is not portable to other compilers. |
45+
| test.c:373:3:373:22 | call to __sync_xor_and_fetch_4 | Is a compiler extension and is not portable to other compilers. |
46+
| test.c:374:3:374:23 | call to __sync_nand_and_fetch_4 | Is a compiler extension and is not portable to other compilers. |
47+
| test.c:376:3:376:30 | call to __sync_bool_compare_and_swap_4 | Is a compiler extension and is not portable to other compilers. |
48+
| test.c:377:3:377:29 | call to __sync_val_compare_and_swap_4 | Is a compiler extension and is not portable to other compilers. |
49+
| test.c:378:3:378:26 | call to __sync_lock_test_and_set_4 | Is a compiler extension and is not portable to other compilers. |
50+
| test.c:379:3:379:21 | call to __sync_lock_release_4 | Is a compiler extension and is not portable to other compilers. |
51+
| test.c:407:3:407:18 | call to __builtin_alloca | Is a compiler extension and is not portable to other compilers. |
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/options

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options:--clang -fhonor-infinity -std=c11 --edg --diag_error=implicit_func_decl -nostdinc -I../../../../common/test/includes/standard-library

0 commit comments

Comments
 (0)