Skip to content

Add Preprocessor 4 package #17

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2022
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
4 changes: 3 additions & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,11 @@
"Pointers",
"Preprocessor1",
"Preprocessor2",
"Preprocessor3",
"Preprocessor4",
"IntegerConversion",
"Expressions",
"DeadCode"
"DeadCode",
"VirtualFunctions"
]
},
Expand Down
95 changes: 95 additions & 0 deletions c/common/src/codingstandards/c/Keywords.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import cpp

/** Module to reason about keywords in standards C90, C99 and C11. */
module Keywords {
/** Holds if `s` is a keyword. */
predicate isKeyword(string s) {
s = "auto"
or
s = "break"
or
s = "case"
or
s = "char"
or
s = "const"
or
s = "continue"
or
s = "default"
or
s = "do"
or
s = "double"
or
s = "else"
or
s = "enum"
or
s = "extern"
or
s = "float"
or
s = "for"
or
s = "goto"
or
s = "if"
or
s = "inline"
or
s = "int"
or
s = "long"
or
s = "register"
or
s = "restrict"
or
s = "return"
or
s = "short"
or
s = "signed"
or
s = "sizeof"
or
s = "static"
or
s = "struct"
or
s = "switch"
or
s = "typedef"
or
s = "union"
or
s = "unsigned"
or
s = "void"
or
s = "volatile"
or
s = "while"
or
s = "_Alignas"
or
s = "_Alignof"
or
s = "_Atomic"
or
s = "_Bool"
or
s = "_Complex"
or
s = "_Generic"
or
s = "_Imaginary"
or
s = "_Noreturn"
or
s = "_Static_assert"
or
s = "_Thread_local"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| test.c:5:3:11:3 | MACROFUNCTION(X) | Invocation of macro MACROFUNCTION includes a token "#else" that could be confused for an argument preprocessor directive. |
| test.c:5:3:11:3 | MACROFUNCTION(X) | Invocation of macro MACROFUNCTION includes a token "#endif" that could be confused for an argument preprocessor directive. |
| test.c:5:3:11:3 | MACROFUNCTION(X) | Invocation of macro MACROFUNCTION includes a token "#if NOTDEFINEDMACRO" that could be confused for an argument preprocessor directive. |
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// GENERATED FILE - DO NOT MODIFY
import codingstandards.cpp.rules.preprocessingdirectivewithinmacroargument.PreprocessingDirectiveWithinMacroArgument
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <string.h>
#define MACROFUNCTION(X) strlen(X)

void f() {
MACROFUNCTION(
#if NOTDEFINEDMACRO // NON_COMPLIANT
"longstringtest!test!"
#else // NON_COMPLIANT
"shortstring"
#endif // NON_COMPLIANT
);

MACROFUNCTION("alright"); // COMPLIANT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @id c/misra/macro-defined-with-the-same-name-as-keyword
* @name RULE-20-4: A macro shall not be defined with the same name as a keyword
* @description Redefinition of keywords is confusing and in the case where the standard library is
* included where that keyword is defined, the redefinition will result in undefined
* behaviour.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-20-4
* correctness
* readability
* maintainability
* external/misra/obligation/required
*/

import cpp
import codingstandards.c.misra
import codingstandards.c.Keywords

from Macro m, string name
where
not isExcluded(m, Preprocessor4Package::macroDefinedWithTheSameNameAsKeywordQuery()) and
m.hasName(name) and
Keywords::isKeyword(name)
select m, "Redefinition of keyword '" + name + "'."
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @id c/misra/function-like-macro-args-contain-hash-token-c-query
* @name RULE-20-6: Tokens that look like a preprocessing directive shall not occur within a macro argument
* @description Arguments to a function-like macro shall not contain tokens that look like
* pre-processing directives or else behaviour after macro expansion is unpredictable.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-20-6
* readability
* correctness
* external/misra/obligation/required
*/

import cpp
import codingstandards.c.misra
import codingstandards.cpp.rules.preprocessingdirectivewithinmacroargument.PreprocessingDirectiveWithinMacroArgument

class FunctionLikeMacroArgsContainHashTokenCQueryQuery extends PreprocessingDirectiveWithinMacroArgumentSharedQuery {
FunctionLikeMacroArgsContainHashTokenCQueryQuery() {
this = Preprocessor4Package::functionLikeMacroArgsContainHashTokenCQueryQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @id c/misra/define-and-undef-used-on-reserved-identifier-or-macro-name
* @name RULE-21-1: #define and #undef shall not be used on a reserved identifier or reserved macro name
* @description The use of #define and #undef on reserved identifiers or macro names can result in
* undefined behaviour.
* @kind problem
* @precision high
* @problem.severity warning
* @tags external/misra/id/rule-21-1
* correctness
* readability
* maintainability
* external/misra/obligation/required
*/

import cpp
import codingstandards.c.misra
import codingstandards.cpp.Naming

from PreprocessorDirective p, string name
where
not isExcluded(p, Preprocessor4Package::defineAndUndefUsedOnReservedIdentifierOrMacroNameQuery()) and
(
p.(Macro).hasName(name)
or
p.(PreprocessorUndef).getName() = name
) and
(
Naming::Cpp14::hasStandardLibraryMacroName(name)
or
Naming::Cpp14::hasStandardLibraryObjectName(name)
or
name.regexpMatch("_.*")
or
name = "defined"
)
select p, "Reserved identifier '" + name + "' has been undefined or redefined."
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| test.c:1:1:1:16 | #define int long | Redefinition of keyword 'int'. |
| test.c:2:1:2:30 | #define while (E) for (; (E);) | Redefinition of keyword 'while'. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/RULE-20-4/MacroDefinedWithTheSameNameAsKeyword.ql
4 changes: 4 additions & 0 deletions c/misra/test/rules/RULE-20-4/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#define int long // NON_COMPLIANT
#define while (E) for (; (E);) // NON_COMPLIANT
#define test(E) for (; (E);) // COMPLIANT
#define _Decimal128 long // COMPLIANT introduced in C23
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c/common/test/rules/preprocessingdirectivewithinmacroargument/PreprocessingDirectiveWithinMacroArgument.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| test.c:1:1:1:17 | #define _NOT_OKAY | Reserved identifier '_NOT_OKAY' has been undefined or redefined. |
| test.c:2:1:2:16 | #undef _NOT_OKAY | Reserved identifier '_NOT_OKAY' has been undefined or redefined. |
| test.c:4:1:4:15 | #define defined | Reserved identifier 'defined' has been undefined or redefined. |
| test.c:5:1:5:13 | #define errno | Reserved identifier 'errno' has been undefined or redefined. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/RULE-21-1/DefineAndUndefUsedOnReservedIdentifierOrMacroName.ql
7 changes: 7 additions & 0 deletions c/misra/test/rules/RULE-21-1/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#define _NOT_OKAY // NON_COMPLIANT
#undef _NOT_OKAY // NON_COMPLIANT

#define defined // NON_COMPLIANT
#define errno // NON_COMPLIANT

#define NDEBUG 1 // COMPLIANT
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,10 @@

import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.rules.preprocessingdirectivewithinmacroargument.PreprocessingDirectiveWithinMacroArgument

pragma[noinline]
predicate isMacroInvocationLocation(MacroInvocation mi, File f, int startChar, int endChar) {
mi.getActualLocation().charLoc(f, startChar, endChar)
class FunctionLikeMacroArgsContainHashTokenCQueryQuery extends PreprocessingDirectiveWithinMacroArgumentSharedQuery {
FunctionLikeMacroArgsContainHashTokenCQueryQuery() {
this = MacrosPackage::functionLikeMacroArgsContainHashTokenQuery()
}
}

pragma[noinline]
predicate isPreprocDirectiveLocation(PreprocessorDirective pd, File f, int startChar) {
pd.getLocation().charLoc(f, startChar, _)
}

from MacroInvocation m, PreprocessorDirective p
where
not isExcluded(m, MacrosPackage::functionLikeMacroArgsContainHashTokenQuery()) and
// There is not sufficient information in the database for nested macro invocations, because
// the location of nested macros and preprocessor directives are all set to the location of the
// outermost macro invocation
not exists(m.getParentInvocation()) and
exists(File f, int startChar, int endChar |
isMacroInvocationLocation(m, f, startChar, endChar) and
exists(int lStart | isPreprocDirectiveLocation(p, f, lStart) |
// If the start location of the preprocessor directive is after the start of the macro
// invocation, and before the end, it must be within the macro invocation
// Note: it's critical to use startChar < lStart, not startChar <= lStart, because the
// latter will include preprocessor directives which occur in nested macro invocations
startChar < lStart and lStart < endChar
)
)
select m,
"Invocation of macro " + m.getMacroName() +
" includes a $@ that could be confused for an argument", p, "preprocessor directive"

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp/common/test/rules/preprocessingdirectivewithinmacroargument/PreprocessingDirectiveWithinMacroArgument.ql
58 changes: 58 additions & 0 deletions cpp/common/src/codingstandards/cpp/exclusions/c/Preprocessor4.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
import cpp
import RuleMetadata
import codingstandards.cpp.exclusions.RuleMetadata

newtype Preprocessor4Query =
TMacroDefinedWithTheSameNameAsKeywordQuery() or
TFunctionLikeMacroArgsContainHashTokenCQueryQuery() or
TDefineAndUndefUsedOnReservedIdentifierOrMacroNameQuery()

predicate isPreprocessor4QueryMetadata(Query query, string queryId, string ruleId) {
query =
// `Query` instance for the `macroDefinedWithTheSameNameAsKeyword` query
Preprocessor4Package::macroDefinedWithTheSameNameAsKeywordQuery() and
queryId =
// `@id` for the `macroDefinedWithTheSameNameAsKeyword` query
"c/misra/macro-defined-with-the-same-name-as-keyword" and
ruleId = "RULE-20-4"
or
query =
// `Query` instance for the `functionLikeMacroArgsContainHashTokenCQuery` query
Preprocessor4Package::functionLikeMacroArgsContainHashTokenCQueryQuery() and
queryId =
// `@id` for the `functionLikeMacroArgsContainHashTokenCQuery` query
"c/misra/function-like-macro-args-contain-hash-token-c-query" and
ruleId = "RULE-20-6"
or
query =
// `Query` instance for the `defineAndUndefUsedOnReservedIdentifierOrMacroName` query
Preprocessor4Package::defineAndUndefUsedOnReservedIdentifierOrMacroNameQuery() and
queryId =
// `@id` for the `defineAndUndefUsedOnReservedIdentifierOrMacroName` query
"c/misra/define-and-undef-used-on-reserved-identifier-or-macro-name" and
ruleId = "RULE-21-1"
}

module Preprocessor4Package {
Query macroDefinedWithTheSameNameAsKeywordQuery() {
//autogenerate `Query` type
result =
// `Query` type for `macroDefinedWithTheSameNameAsKeyword` query
TQueryC(TPreprocessor4PackageQuery(TMacroDefinedWithTheSameNameAsKeywordQuery()))
}

Query functionLikeMacroArgsContainHashTokenCQueryQuery() {
//autogenerate `Query` type
result =
// `Query` type for `functionLikeMacroArgsContainHashTokenCQuery` query
TQueryC(TPreprocessor4PackageQuery(TFunctionLikeMacroArgsContainHashTokenCQueryQuery()))
}

Query defineAndUndefUsedOnReservedIdentifierOrMacroNameQuery() {
//autogenerate `Query` type
result =
// `Query` type for `defineAndUndefUsedOnReservedIdentifierOrMacroName` query
TQueryC(TPreprocessor4PackageQuery(TDefineAndUndefUsedOnReservedIdentifierOrMacroNameQuery()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Pointers1
import Preprocessor1
import Preprocessor2
import Preprocessor3
import Preprocessor4
import SideEffects1
import SideEffects2
import Strings1
Expand All @@ -29,6 +30,7 @@ newtype TCQuery =
TPreprocessor1PackageQuery(Preprocessor1Query q) or
TPreprocessor2PackageQuery(Preprocessor2Query q) or
TPreprocessor3PackageQuery(Preprocessor3Query q) or
TPreprocessor4PackageQuery(Preprocessor4Query q) or
TSideEffects1PackageQuery(SideEffects1Query q) or
TSideEffects2PackageQuery(SideEffects2Query q) or
TStrings1PackageQuery(Strings1Query q) or
Expand All @@ -47,6 +49,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId) {
isPreprocessor1QueryMetadata(query, queryId, ruleId) or
isPreprocessor2QueryMetadata(query, queryId, ruleId) or
isPreprocessor3QueryMetadata(query, queryId, ruleId) or
isPreprocessor4QueryMetadata(query, queryId, ruleId) or
isSideEffects1QueryMetadata(query, queryId, ruleId) or
isSideEffects2QueryMetadata(query, queryId, ruleId) or
isStrings1QueryMetadata(query, queryId, ruleId) or
Expand Down
Loading