|
| 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 | +} |
0 commit comments