Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Language 3 #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
"Lambdas",
"Language1",
"Language2",
"Language3",
"Literals",
"Loops",
"Macros",
Expand Down
117 changes: 117 additions & 0 deletions 117 c/common/src/codingstandards/c/Extensions.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import cpp
import codingstandards.cpp.Extensions

/**
* Common base class for modeling compiler extensions.
*/
abstract class CCompilerExtension extends CompilerExtension { }

// Reference: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#Other-Builtins
abstract class CConditionalDefineExtension extends CCompilerExtension, PreprocessorIfdef {
CConditionalDefineExtension() {
exists(toString().indexOf("__has_builtin")) or
exists(toString().indexOf("__has_constexpr_builtin")) or
exists(toString().indexOf("__has_feature")) or
exists(toString().indexOf("__has_extension")) or
exists(toString().indexOf("__has_attribute")) or
exists(toString().indexOf("__has_declspec_attribute")) or
exists(toString().indexOf("__is_identifier")) or
exists(toString().indexOf("__has_include")) or
exists(toString().indexOf("__has_include_next")) or
exists(toString().indexOf("__has_warning"))
}
}

// Reference: https://clang.llvm.org/docs/LanguageExtensions.html#builtin-macros
class CMacroBasedExtension extends CCompilerExtension, Macro {
CMacroBasedExtension() {
getBody() in [
"__BASE_FILE__", "__FILE_NAME__", "__COUNTER__", "__INCLUDE_LEVEL__", "_TIMESTAMP__",
"__clang__", "__clang_major__", "__clang_minor__", "__clang_patchlevel__",
"__clang_version__", "__clang_literal_encoding__", "__clang_wide_literal_encoding__"
]
}
}

// Reference: https://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#Variable-Attributes
class CAttributeExtension extends CCompilerExtension, Attribute {
CAttributeExtension() {
getName() in [
"ext_vector_type", "vector_size", "access", "aligned", "deprecated", "cold", "unused",
"fallthrough", "read_only", "alias"
]
}
}

// Reference: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html#g_t_005f_005fsync-Builtins
class CFunctionExtension extends CCompilerExtension, FunctionCall {
CFunctionExtension() {
// these must be somewhat broad because of how they vary
// in implementation / naming
getTarget().getName().indexOf("__sync_fetch") = 0 or
getTarget().getName().indexOf("__sync_add") = 0 or
getTarget().getName().indexOf("__sync_sub") = 0 or
getTarget().getName().indexOf("__sync_or") = 0 or
getTarget().getName().indexOf("__sync_and") = 0 or
getTarget().getName().indexOf("__sync_xor") = 0 or
getTarget().getName().indexOf("__sync_nand") = 0 or
getTarget().getName().indexOf("__sync_bool") = 0 or
getTarget().getName().indexOf("__sync_val") = 0 or
getTarget().getName().indexOf("__sync_lock") = 0 or
// the built-in extensions
getTarget().getName().indexOf("__builtin_") = 0
}
}

// Reference: https://gcc.gnu.org/onlinedocs/gcc/Alignment.html#Alignment
class CFunctionLikeExtension extends CCompilerExtension, AlignofExprOperator {
CFunctionLikeExtension() { exists(getValueText().indexOf("__alignof__")) }
}

// Reference: https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html#Statement-Exprs
class CStmtExprExtension extends CCompilerExtension, StmtExpr { }

// Use of ternary like the following: `int a = 0 ?: 0;` where the
// one of the branches is omitted
// Reference: https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html#Conditionals
class CTerseTernaryExtension extends CCompilerExtension, ConditionalExpr {
CTerseTernaryExtension() { getCondition() = getElse() or getCondition() = getThen() }
}

// Reference: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fint128.html#g_t_005f_005fint128
// Reference: https://gcc.gnu.org/onlinedocs/gcc/Decimal-Float.html#Decimal-Float
class CRealTypeExtensionExtension extends CCompilerExtension, DeclarationEntry {
CRealTypeExtensionExtension() {
getType() instanceof Decimal128Type or
getType() instanceof Decimal32Type or
getType() instanceof Decimal64Type or
getType() instanceof Float128Type
}
}

// Reference: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fint128.html#g_t_005f_005fint128
class CIntegerTypeExtension extends CCompilerExtension, DeclarationEntry {
CIntegerTypeExtension() { getType() instanceof Int128Type }
}

// Reference: https://gcc.gnu.org/onlinedocs/gcc/Long-Long.html#Long-Long
class CLongLongType extends CCompilerExtension, DeclarationEntry {
CLongLongType() { getType() instanceof LongLongType }
}

class CZeroLengthArraysExtension extends CCompilerExtension, DeclarationEntry {
CZeroLengthArraysExtension() { getType().(ArrayType).getArraySize() = 0 }
}

// Reference: https://gcc.gnu.org/onlinedocs/gcc/Empty-Structures.html#Empty-Structures
class CEmptyStructExtension extends CCompilerExtension, Struct {
CEmptyStructExtension() { not exists(getAMember(_)) }
}

// Reference: https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html#Variable-Length
class CVariableLengthArraysExtension extends CCompilerExtension, DeclarationEntry {
CVariableLengthArraysExtension() {
getType() instanceof ArrayType and
not getType().(ArrayType).hasArraySize()
}
}
28 changes: 28 additions & 0 deletions 28 c/common/src/codingstandards/c/UndefinedBehavior.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import cpp
import codingstandards.cpp.UndefinedBehavior

/**
* Library for modeling undefined behavior.
*/
abstract class CUndefinedBehavior extends UndefinedBehavior { }

class C99MainFunction extends Function {
C99MainFunction() {
this.getNumberOfParameters() = 2 and
this.getType() instanceof IntType and
this.getParameter(0).getType() instanceof IntType and
this.getParameter(1).getType().(PointerType).getBaseType().(PointerType).getBaseType()
instanceof CharType
or
this.getNumberOfParameters() = 0 and
this.getType() instanceof VoidType
}
}

class CUndefinedMainDefinition extends CUndefinedBehavior, Function {
CUndefinedMainDefinition() {
// for testing purposes, we use the prefix ____codeql_coding_standards`
(this.getName() = "main" or this.getName().indexOf("____codeql_coding_standards") = 0) and
not this instanceof C99MainFunction
}
}
20 changes: 20 additions & 0 deletions 20 c/misra/src/rules/RULE-1-2/LanguageExtensionsShouldNotBeUsed.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @id c/misra/language-extensions-should-not-be-used
* @name RULE-1-2: Language extensions should not be used
* @description Language extensions are not portable to other compilers and should not be used.
* @kind problem
* @precision high
* @problem.severity error
* @tags external/misra/id/rule-1-2
* maintainability
* readability
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.c.misra
import codingstandards.c.Extensions

from CCompilerExtension e
where not isExcluded(e, Language3Package::languageExtensionsShouldNotBeUsedQuery())
select e, "Is a compiler extension and is not portable to other compilers."
20 changes: 20 additions & 0 deletions 20 c/misra/src/rules/RULE-1-3/OccurrenceOfUndefinedBehavior.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @id c/misra/occurrence-of-undefined-behavior
* @name RULE-1-3: There shall be no occurrence of undefined or critical unspecified behavior
* @description Relying on undefined or unspecified behavior can result in unreliable programs.
* @kind problem
* @precision high
* @problem.severity error
* @tags external/misra/id/rule-1-3
* maintainability
* readability
* external/misra/obligation/required
*/

import cpp
import codingstandards.c.misra
import codingstandards.c.UndefinedBehavior

from CUndefinedBehavior c
where not isExcluded(c, Language3Package::occurrenceOfUndefinedBehaviorQuery())
select c, "May result in undefined behavior."
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
| test.c:34:1:34:23 | #define A __BASE_FILE__ | Is a compiler extension and is not portable to other compilers. |
| test.c:35:1:35:23 | #define B __FILE_NAME__ | Is a compiler extension and is not portable to other compilers. |
| test.c:36:1:36:21 | #define C __COUNTER__ | Is a compiler extension and is not portable to other compilers. |
| test.c:37:1:37:27 | #define D __INCLUDE_LEVEL__ | Is a compiler extension and is not portable to other compilers. |
| test.c:39:1:39:19 | #define F __clang__ | Is a compiler extension and is not portable to other compilers. |
| test.c:40:1:40:25 | #define G __clang_major__ | Is a compiler extension and is not portable to other compilers. |
| test.c:41:1:41:25 | #define H __clang_minor__ | Is a compiler extension and is not portable to other compilers. |
| test.c:42:1:42:30 | #define I __clang_patchlevel__ | Is a compiler extension and is not portable to other compilers. |
| test.c:43:1:43:27 | #define J __clang_version__ | Is a compiler extension and is not portable to other compilers. |
| test.c:44:1:44:36 | #define K __clang_literal_encoding__ | Is a compiler extension and is not portable to other compilers. |
| test.c:45:1:45:41 | #define L __clang_wide_literal_encoding__ | Is a compiler extension and is not portable to other compilers. |
| test.c:53:33:53:43 | vector_size | Is a compiler extension and is not portable to other compilers. |
| test.c:54:33:54:47 | vector_size | Is a compiler extension and is not portable to other compilers. |
| test.c:55:37:55:51 | ext_vector_type | Is a compiler extension and is not portable to other compilers. |
| test.c:56:37:56:51 | ext_vector_type | Is a compiler extension and is not portable to other compilers. |
| test.c:61:3:69:4 | (statement expression) | Is a compiler extension and is not portable to other compilers. |
| test.c:96:3:96:18 | call to __builtin_setjmp | Is a compiler extension and is not portable to other compilers. |
| test.c:97:3:97:19 | call to __builtin_longjmp | Is a compiler extension and is not portable to other compilers. |
| test.c:113:11:113:16 | ... ? ... : ... | Is a compiler extension and is not portable to other compilers. |
| test.c:124:12:124:12 | definition of a | Is a compiler extension and is not portable to other compilers. |
| test.c:128:17:128:17 | definition of a | Is a compiler extension and is not portable to other compilers. |
| test.c:165:8:165:15 | definition of contents | Is a compiler extension and is not portable to other compilers. |
| test.c:182:8:182:11 | gf19 | Is a compiler extension and is not portable to other compilers. |
| test.c:214:33:214:35 | declaration of out | Is a compiler extension and is not portable to other compilers. |
| test.c:215:25:215:26 | declaration of in | Is a compiler extension and is not portable to other compilers. |
| test.c:268:16:268:21 | access | Is a compiler extension and is not portable to other compilers. |
| test.c:271:27:271:31 | alias | Is a compiler extension and is not portable to other compilers. |
| test.c:274:23:274:29 | aligned | Is a compiler extension and is not portable to other compilers. |
| test.c:285:25:285:34 | deprecated | Is a compiler extension and is not portable to other compilers. |
| test.c:297:20:297:30 | fallthrough | Is a compiler extension and is not portable to other compilers. |
| test.c:321:3:321:22 | alignof(<expr>) | Is a compiler extension and is not portable to other compilers. |
| test.c:340:3:340:31 | call to __builtin_extract_return_addr | Is a compiler extension and is not portable to other compilers. |
| test.c:341:3:341:28 | call to __builtin_frob_return_addr | Is a compiler extension and is not portable to other compilers. |
| test.c:342:3:342:25 | call to __builtin_frame_address | Is a compiler extension and is not portable to other compilers. |
| test.c:363:3:363:22 | call to __sync_fetch_and_add_4 | Is a compiler extension and is not portable to other compilers. |
| test.c:364:3:364:22 | call to __sync_fetch_and_sub_4 | Is a compiler extension and is not portable to other compilers. |
| test.c:365:3:365:21 | call to __sync_fetch_and_or_4 | Is a compiler extension and is not portable to other compilers. |
| test.c:366:3:366:22 | call to __sync_fetch_and_and_4 | Is a compiler extension and is not portable to other compilers. |
| test.c:367:3:367:22 | call to __sync_fetch_and_xor_4 | Is a compiler extension and is not portable to other compilers. |
| test.c:368:3:368:23 | call to __sync_fetch_and_nand_4 | Is a compiler extension and is not portable to other compilers. |
| test.c:369:3:369:22 | call to __sync_add_and_fetch_4 | Is a compiler extension and is not portable to other compilers. |
| test.c:370:3:370:22 | call to __sync_sub_and_fetch_4 | Is a compiler extension and is not portable to other compilers. |
| test.c:371:3:371:21 | call to __sync_or_and_fetch_4 | Is a compiler extension and is not portable to other compilers. |
| test.c:372:3:372:22 | call to __sync_and_and_fetch_4 | Is a compiler extension and is not portable to other compilers. |
| test.c:373:3:373:22 | call to __sync_xor_and_fetch_4 | Is a compiler extension and is not portable to other compilers. |
| test.c:374:3:374:23 | call to __sync_nand_and_fetch_4 | Is a compiler extension and is not portable to other compilers. |
| 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. |
| 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. |
| 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. |
| test.c:379:3:379:21 | call to __sync_lock_release_4 | Is a compiler extension and is not portable to other compilers. |
| test.c:407:3:407:18 | call to __builtin_alloca | Is a compiler extension and is not portable to other compilers. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/RULE-1-2/LanguageExtensionsShouldNotBeUsed.ql
1 change: 1 addition & 0 deletions 1 c/misra/test/rules/RULE-1-2/options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
semmle-extractor-options:--clang -fhonor-infinity -std=c11 --edg --diag_error=implicit_func_decl -nostdinc -I../../../../common/test/includes/standard-library
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.