Skip to content

Commit

Permalink
Remove 17-13, simpler implementation for 17-12.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelRFairhurst committed Sep 27, 2024
1 parent 982c597 commit 722dc6a
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 317 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,70 +14,15 @@
import cpp
import codingstandards.c.misra

abstract class AddressOfFunction extends Expr {
abstract predicate isImplicitlyAddressed();

abstract string getFuncName();
}

class FunctionTypeAccess extends FunctionAccess, AddressOfFunction {

predicate isImmediatelyParenthesized() {
exists(ParenthesisExpr parens | parens.getExpr() = this)
}

predicate isExplicitlyAddressed() {
getParent() instanceof AddressOfExpr and
not isImmediatelyParenthesized()
}

override predicate isImplicitlyAddressed() {
not isExplicitlyAddressed()
}

override string getFuncName() {
result = getTarget().getName()
}
}

/*
class IndirectFunctionCall extends FunctionCall, AddressOfFunction {
override predicate isImplicitlyAddressed() {
getConversion+() instanceof ParenthesisExpr
}
override string getFuncName() {
result = getTarget().getName()
}
}
*/

class MacroArgTakesFunction extends AddressOfFunction {
MacroInvocation m;
MacroArgTakesFunction() {
m.getExpr() = this
}

override predicate isImplicitlyAddressed() {
any()
}

string getProp() {
result = m.getExpandedArgument(_)
and this.get
}

override string getFuncName() {
result = "a macro argument"
}

predicate isImplicitlyAddressed(FunctionAccess access) {
not access.getParent() instanceof AddressOfExpr or
exists(ParenthesisExpr parens | parens.getExpr() = access)
}

from AddressOfFunction funcAddr
from FunctionAccess funcAccess
where
not isExcluded(funcAddr, FunctionTypesPackage::functionAddressesShouldAddressOperatorQuery()) and
//not funcAccess.isImmediatelyCalled() and
//not funcAccess.isExplicitlyAddressed()
funcAddr.isImplicitlyAddressed()
select
funcAddr, "The address of function " + funcAddr.getFuncName() + " is taken without the & operator."
not isExcluded(funcAccess, FunctionTypesPackage::functionAddressesShouldAddressOperatorQuery()) and
isImplicitlyAddressed(funcAccess)
select funcAccess,
"The address of function " + funcAccess.getTarget().getName() +
" is taken without the & operator."
147 changes: 0 additions & 147 deletions c/misra/src/rules/RULE-17-13/DisallowedFunctionTypeQualifier.ql

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
| test.c:15:25:15:29 | func2 | The address of function func2 is taken without the & operator. |
| test.c:16:27:16:31 | func3 | The address of function func3 is taken without the & operator. |
| test.c:22:16:22:20 | func1 | The address of function func1 is taken without the & operator. |
| test.c:38:5:38:9 | func1 | The address of function func1 is taken without the & operator. |
| test.c:39:5:39:9 | func2 | The address of function func2 is taken without the & operator. |
| test.c:47:7:47:11 | func1 | The address of function func1 is taken without the & operator. |
| test.c:48:7:48:11 | func2 | The address of function func2 is taken without the & operator. |
| test.c:57:15:57:19 | func1 | The address of function func1 is taken without the & operator. |
| test.c:58:23:58:27 | func2 | The address of function func2 is taken without the & operator. |
| test.c:59:15:59:19 | func1 | The address of function func1 is taken without the & operator. |
| test.c:59:22:59:26 | func2 | The address of function func2 is taken without the & operator. |
| test.c:67:13:67:17 | func1 | The address of function func1 is taken without the & operator. |
| test.c:68:14:68:18 | func1 | The address of function func1 is taken without the & operator. |
| test.c:39:5:39:9 | func1 | The address of function func1 is taken without the & operator. |
| test.c:40:5:40:9 | func2 | The address of function func2 is taken without the & operator. |
| test.c:48:7:48:11 | func1 | The address of function func1 is taken without the & operator. |
| test.c:49:7:49:11 | func2 | The address of function func2 is taken without the & operator. |
| test.c:58:15:58:19 | func1 | The address of function func1 is taken without the & operator. |
| test.c:59:23:59:27 | func2 | The address of function func2 is taken without the & operator. |
| test.c:60:15:60:19 | func1 | The address of function func1 is taken without the & operator. |
| test.c:60:22:60:26 | func2 | The address of function func2 is taken without the & operator. |
| test.c:68:13:68:17 | func1 | The address of function func1 is taken without the & operator. |
| test.c:69:14:69:18 | func1 | The address of function func1 is taken without the & operator. |
| test.c:71:20:71:24 | func1 | The address of function func1 is taken without the & operator. |
| test.c:70:14:70:18 | func1 | The address of function func1 is taken without the & operator. |
| test.c:72:20:72:24 | func1 | The address of function func1 is taken without the & operator. |
| test.c:76:20:76:24 | func1 | The address of function func1 is taken without the & operator. |
| test.c:77:20:77:24 | func1 | The address of function func1 is taken without the & operator. |
9 changes: 6 additions & 3 deletions c/misra/test/rules/RULE-17-12/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func_ptr_t1 returns_func(int x) {
#define MACRO_IDENTITY(f) (f)
#define MACRO_INVOKE_RISKY(f) (f())
#define MACRO_INVOKE_IMPROVED(f) ((f)())
#define MACRO_INVOKE_AND_USE_AS_TOKEN(f) f(0, #f)

void test() {
func1(); // COMPLIANT
Expand Down Expand Up @@ -69,14 +70,16 @@ void test() {
((void*) func1); // NON-COMPLIANT

MACRO_IDENTITY(func1); // NON-COMPLIANT
MACRO_IDENTITY(func1)(); // NON-COMPLIANT
MACRO_IDENTITY(func1)(); // NON-COMPLIANT[FALSE NEGATIVE]
MACRO_IDENTITY(&func1); // COMPLIANT
MACRO_IDENTITY(&func1)(); // COMPLIANT

MACRO_INVOKE_RISKY(func3); // NON-COMPLIANT
MACRO_INVOKE_IMPROVED(func3); // NON-COMPLIANT
MACRO_INVOKE_RISKY(func3); // NON-COMPLIANT[FALSE NEGATIVE]
MACRO_INVOKE_IMPROVED(func3); // NON-COMPLIANT[FALSE NEGATIVE]
MACRO_INVOKE_IMPROVED(&func3); // COMPLIANT

MACRO_INVOKE_AND_USE_AS_TOKEN(func1); // COMPLIANT

// Function pointers are exempt from this rule.
func_ptr1(); // COMPLIANT
func_ptr2(1, "hello"); // COMPLIANT
Expand Down

This file was deleted.

This file was deleted.

56 changes: 0 additions & 56 deletions c/misra/test/rules/RULE-17-13/test.c

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import cpp
import RuleMetadata
import codingstandards.cpp.exclusions.RuleMetadata

newtype FunctionTypesQuery =
TFunctionAddressesShouldAddressOperatorQuery() or
TDisallowedFunctionTypeQualifierQuery()
newtype FunctionTypesQuery = TFunctionAddressesShouldAddressOperatorQuery()

predicate isFunctionTypesQueryMetadata(Query query, string queryId, string ruleId, string category) {
query =
Expand All @@ -16,15 +14,6 @@ predicate isFunctionTypesQueryMetadata(Query query, string queryId, string ruleI
"c/misra/function-addresses-should-address-operator" and
ruleId = "RULE-17-12" and
category = "advisory"
or
query =
// `Query` instance for the `disallowedFunctionTypeQualifier` query
FunctionTypesPackage::disallowedFunctionTypeQualifierQuery() and
queryId =
// `@id` for the `disallowedFunctionTypeQualifier` query
"c/misra/disallowed-function-type-qualifier" and
ruleId = "RULE-17-13" and
category = "required"
}

module FunctionTypesPackage {
Expand All @@ -34,11 +23,4 @@ module FunctionTypesPackage {
// `Query` type for `functionAddressesShouldAddressOperator` query
TQueryC(TFunctionTypesPackageQuery(TFunctionAddressesShouldAddressOperatorQuery()))
}

Query disallowedFunctionTypeQualifierQuery() {
//autogenerate `Query` type
result =
// `Query` type for `disallowedFunctionTypeQualifier` query
TQueryC(TFunctionTypesPackageQuery(TDisallowedFunctionTypeQualifierQuery()))
}
}
Loading

0 comments on commit 722dc6a

Please sign in to comment.