Skip to content

Commit 58383ae

Browse files
committed
work
1 parent 293450f commit 58383ae

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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, RealNumberType {
84+
CRealTypeExtensionExtension() {
85+
this instanceof Decimal128Type or
86+
this instanceof Decimal32Type or
87+
this instanceof Decimal64Type or
88+
this instanceof Float128Type
89+
}
90+
}
91+
// Reference: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fint128.html#g_t_005f_005fint128
92+
class CIntegerTypeExtension extends CCompilerExtension, Int128Type {}
93+
94+
class CZeroLengthArraysExtension extends CCompilerExtension, DeclarationEntry {
95+
CZeroLengthArraysExtension() { getType().(ArrayType).getArraySize() = 0 }
96+
}
97+
98+
// Reference: https://gcc.gnu.org/onlinedocs/gcc/Empty-Structures.html#Empty-Structures
99+
class CEmptyStructExtension extends CCompilerExtension, Struct {
100+
CEmptyStructExtension() { not exists(getAMember(_)) }
101+
}
102+
103+
// Reference: https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html#Variable-Length
104+
class CVariableLengthArraysExtension extends CCompilerExtension, DeclarationEntry {
105+
CVariableLengthArraysExtension() { not getType().(ArrayType).hasArraySize() }
106+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import cpp
2+
3+
abstract class CompilerExtension extends Locatable {}
4+
abstract class CPPCompilerExtension extends CompilerExtension {}

0 commit comments

Comments
 (0)