Skip to content

Commit b0e343f

Browse files
authored
Merge pull request #1034 from github/michaelrfairhurst/package-deadcode-6
Implement DeadCode6, RULE-0-1-2, shared with A0-1-2.
2 parents b007dcd + 3be7984 commit b0e343f

File tree

19 files changed

+164
-108
lines changed

19 files changed

+164
-108
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `A0-1-2` - `UnusedReturnValue.ql`:
2+
- Refactors the rule implementation into a shared library for usage in MISRA C++ ruleset. No externally visible changes expected.

cpp/autosar/src/rules/A0-1-2/UnusedReturnValue.ql

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,10 @@
1616

1717
import cpp
1818
import codingstandards.cpp.autosar
19-
import codingstandards.cpp.Operator
20-
import cpp
19+
import codingstandards.cpp.rules.unusedreturnvalueshared.UnusedReturnValueShared
2120

22-
/*
23-
* This query performs a simple syntactic check to ensure that the return value of the function is
24-
* not completely ignored. This matches the examples given in the rule, although the text itself is
25-
* not entirely clear. This means it will not find cases where something is done with the return
26-
* value, but it is not meaningfully read. For example: `int ret_val = f();`, with no subsequent
27-
* access of `ret_val`. However, such a case _would_ be flagged by A0-1-1 - Useless assignment.
28-
*/
21+
module UnusedReturnValueConfig implements UnusedReturnValueSharedConfigSig {
22+
Query getQuery() { result = DeadCodePackage::unusedReturnValueQuery() }
23+
}
2924

30-
from FunctionCall fc, Function f
31-
where
32-
not isExcluded(fc, DeadCodePackage::unusedReturnValueQuery()) and
33-
// Find function calls in `ExprStmt`s, which indicate the return value is ignored
34-
fc.getParent() instanceof ExprStmt and
35-
// Ignore calls to void functions, which don't return values
36-
not fc.getUnderlyingType() instanceof VoidType and
37-
// Get the function target
38-
f = fc.getTarget() and
39-
// Overloaded (i.e. user defined) operators should behave in the same way as built-in operators,
40-
// so the rule does not require the use of the return value
41-
not f instanceof Operator and
42-
// Exclude cases where the function call is generated within a macro, as the user of the macro is
43-
// not necessarily able to address those results
44-
not fc.isAffectedByMacro() and
45-
// Rule allows disabling this rule where a static_cast<void> or a C-style cast to void is applied
46-
not exists(Cast cast | cast instanceof StaticCast or cast instanceof CStyleCast |
47-
fc.getExplicitlyConverted() = cast and
48-
cast.getActualType() instanceof VoidType
49-
)
50-
select fc, "Return value from call to $@ is unused.", f, f.getName()
25+
import UnusedReturnValueShared<UnusedReturnValueConfig>

cpp/autosar/test/rules/A0-1-2/UnusedReturnValue.expected

Lines changed: 0 additions & 1 deletion
This file was deleted.

cpp/autosar/test/rules/A0-1-2/UnusedReturnValue.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/unusedreturnvalueshared/UnusedReturnValueShared.ql
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype DeadCode6Query = TUnusedReturnValueMisraCppQuery()
7+
8+
predicate isDeadCode6QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `unusedReturnValueMisraCpp` query
11+
DeadCode6Package::unusedReturnValueMisraCppQuery() and
12+
queryId =
13+
// `@id` for the `unusedReturnValueMisraCpp` query
14+
"cpp/misra/unused-return-value-misra-cpp" and
15+
ruleId = "RULE-0-1-2" and
16+
category = "required"
17+
}
18+
19+
module DeadCode6Package {
20+
Query unusedReturnValueMisraCppQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `unusedReturnValueMisraCpp` query
24+
TQueryCPP(TDeadCode6PackageQuery(TUnusedReturnValueMisraCppQuery()))
25+
}
26+
}

cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Conversions2
1919
import DeadCode
2020
import DeadCode3
2121
import DeadCode4
22+
import DeadCode6
2223
import Declarations
2324
import ExceptionSafety
2425
import Exceptions1
@@ -91,6 +92,7 @@ newtype TCPPQuery =
9192
TDeadCodePackageQuery(DeadCodeQuery q) or
9293
TDeadCode3PackageQuery(DeadCode3Query q) or
9394
TDeadCode4PackageQuery(DeadCode4Query q) or
95+
TDeadCode6PackageQuery(DeadCode6Query q) or
9496
TDeclarationsPackageQuery(DeclarationsQuery q) or
9597
TExceptionSafetyPackageQuery(ExceptionSafetyQuery q) or
9698
TExceptions1PackageQuery(Exceptions1Query q) or
@@ -163,6 +165,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
163165
isDeadCodeQueryMetadata(query, queryId, ruleId, category) or
164166
isDeadCode3QueryMetadata(query, queryId, ruleId, category) or
165167
isDeadCode4QueryMetadata(query, queryId, ruleId, category) or
168+
isDeadCode6QueryMetadata(query, queryId, ruleId, category) or
166169
isDeclarationsQueryMetadata(query, queryId, ruleId, category) or
167170
isExceptionSafetyQueryMetadata(query, queryId, ruleId, category) or
168171
isExceptions1QueryMetadata(query, queryId, ruleId, category) or
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Provides a configurable module UnusedReturnValue with a `problems` predicate
3+
* for the following issue:
4+
* The value returned by a function having a non-void return type that is not an
5+
* overloaded operator shall be used.
6+
*/
7+
8+
import cpp
9+
import codingstandards.cpp.Customizations
10+
import codingstandards.cpp.Exclusions
11+
12+
signature module UnusedReturnValueSharedConfigSig {
13+
Query getQuery();
14+
}
15+
16+
module UnusedReturnValueShared<UnusedReturnValueSharedConfigSig Config> {
17+
/*
18+
* This query performs a simple syntactic check to ensure that the return value of the function is
19+
* not completely ignored. This matches the examples given in the rule, although the text itself is
20+
* not entirely clear. This means it will not find cases where something is done with the return
21+
* value, but it is not meaningfully read. For example: `int ret_val = f();`, with no subsequent
22+
* access of `ret_val`. However, such a case _would_ be flagged by A0-1-1 - Useless assignment.
23+
*/
24+
25+
query predicate problems(FunctionCall fc, string message, Function f, string funcName) {
26+
not isExcluded(fc, Config::getQuery()) and
27+
message = "Return value from call to $@ is unused." and
28+
funcName = f.getName() and
29+
// Find function calls in `ExprStmt`s, which indicate the return value is ignored
30+
fc.getParent() instanceof ExprStmt and
31+
// Ignore calls to void functions, which don't return values
32+
not fc.getUnderlyingType() instanceof VoidType and
33+
// Get the function target
34+
f = fc.getTarget() and
35+
// Overloaded (i.e. user defined) operators should behave in the same way as built-in operators,
36+
// so the rule does not require the use of the return value
37+
not f instanceof Operator and
38+
// Exclude cases where the function call is generated within a macro, as the user of the macro is
39+
// not necessarily able to address those results
40+
not fc.isAffectedByMacro() and
41+
// Rule allows disabling this rule where a static_cast<void> or a C-style cast to void is applied
42+
not exists(Cast cast | cast instanceof StaticCast or cast instanceof CStyleCast |
43+
fc.getExplicitlyConverted() = cast and
44+
cast.getActualType() instanceof VoidType
45+
)
46+
}
47+
}

cpp/common/test/deviations/deviation_permits_basic_test/UnusedReturnValue.ql

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,11 @@
1717
import cpp
1818
import codingstandards.cpp.CodingStandards
1919
import codingstandards.cpp.exclusions.cpp.RuleMetadata
20+
import codingstandards.cpp.rules.unusedreturnvalueshared.UnusedReturnValueShared
2021

2122
/* This is a copy of an AUTOSAR rule, which we are using for testing purposes. */
22-
/*
23-
* This query performs a simple syntactic check to ensure that the return value of the function is
24-
* not completely ignored. This matches the examples given in the rule, although the text itself is
25-
* not entirely clear. This means it will not find cases where something is done with the return
26-
* value, but it is not meaningfully read. For example: `int ret_val = f();`, with no subsequent
27-
* access of `ret_val`. However, such a case _would_ be flagged by A0-1-1 - Useless assignment.
28-
*/
23+
module UnusedReturnValueConfig implements UnusedReturnValueSharedConfigSig {
24+
Query getQuery() { result = DeadCodePackage::unusedReturnValueQuery() }
25+
}
2926

30-
from FunctionCall fc, Function f
31-
where
32-
not isExcluded(fc, DeadCodePackage::unusedReturnValueQuery()) and
33-
// Find function calls in `ExprStmt`s, which indicate the return value is ignored
34-
fc.getParent() instanceof ExprStmt and
35-
// Ignore calls to void functions, which don't return values
36-
not fc.getType() instanceof VoidType and
37-
// Get the function target
38-
f = fc.getTarget() and
39-
// Overloaded (i.e. user defined) operators should behave in the same way as built-in operators,
40-
// so the rule does not require the use of the return value
41-
not f instanceof Operator and
42-
// Exclude cases where the function call is generated within a macro, as the user of the macro is
43-
// not necessarily able to address thoes results
44-
not fc.isAffectedByMacro() and
45-
// Rule allows disabling this rule where a static_cast<void> is applied
46-
not fc.getExplicitlyConverted().(StaticCast).getActualType() instanceof VoidType
47-
select fc, "Return value from call to $@ is unused.", f, f.getName()
27+
import UnusedReturnValueShared<UnusedReturnValueConfig>

cpp/common/test/deviations/deviations_basic_test/UnusedReturnValue.ql

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,11 @@
1717
import cpp
1818
import codingstandards.cpp.CodingStandards
1919
import codingstandards.cpp.exclusions.cpp.RuleMetadata
20+
import codingstandards.cpp.rules.unusedreturnvalueshared.UnusedReturnValueShared
2021

2122
/* This is a copy of an AUTOSAR rule, which we are using for testing purposes. */
22-
/*
23-
* This query performs a simple syntactic check to ensure that the return value of the function is
24-
* not completely ignored. This matches the examples given in the rule, although the text itself is
25-
* not entirely clear. This means it will not find cases where something is done with the return
26-
* value, but it is not meaningfully read. For example: `int ret_val = f();`, with no subsequent
27-
* access of `ret_val`. However, such a case _would_ be flagged by A0-1-1 - Useless assignment.
28-
*/
23+
module UnusedReturnValueConfig implements UnusedReturnValueSharedConfigSig {
24+
Query getQuery() { result = DeadCodePackage::unusedReturnValueQuery() }
25+
}
2926

30-
from FunctionCall fc, Function f
31-
where
32-
not isExcluded(fc, DeadCodePackage::unusedReturnValueQuery()) and
33-
// Find function calls in `ExprStmt`s, which indicate the return value is ignored
34-
fc.getParent() instanceof ExprStmt and
35-
// Ignore calls to void functions, which don't return values
36-
not fc.getType() instanceof VoidType and
37-
// Get the function target
38-
f = fc.getTarget() and
39-
// Overloaded (i.e. user defined) operators should behave in the same way as built-in operators,
40-
// so the rule does not require the use of the return value
41-
not f instanceof Operator and
42-
// Exclude cases where the function call is generated within a macro, as the user of the macro is
43-
// not necessarily able to address thoes results
44-
not fc.isAffectedByMacro() and
45-
// Rule allows disabling this rule where a static_cast<void> is applied
46-
not fc.getExplicitlyConverted().(StaticCast).getActualType() instanceof VoidType
47-
select fc, "Return value from call to $@ is unused.", f, f.getName()
27+
import UnusedReturnValueShared<UnusedReturnValueConfig>

0 commit comments

Comments
 (0)