Skip to content

Commit a75dc0a

Browse files
Address feedback
1 parent 8c31c8c commit a75dc0a

20 files changed

+84
-268
lines changed

c/misra/src/rules/RULE-2-8/UnusedObjectDefinition.ql

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import cpp
1616
import codingstandards.c.misra
1717
import codingstandards.cpp.deadcode.UnusedObjects
1818

19-
from ReportDeadObjectAtDefinition report
19+
from ReportDeadObject report
2020
where
2121
not isExcluded(report.getPrimaryElement(), DeadCode2Package::unusedObjectDefinitionQuery()) and
2222
not report.hasAttrUnused()

c/misra/src/rules/RULE-2-8/UnusedObjectDefinitionInMacro.ql

-24
This file was deleted.

c/misra/src/rules/RULE-2-8/UnusedObjectDefinitionInMacroStrict.ql

-27
This file was deleted.

c/misra/src/rules/RULE-2-8/UnusedObjectDefinitionStrict.ql

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import cpp
1818
import codingstandards.c.misra
1919
import codingstandards.cpp.deadcode.UnusedObjects
2020

21-
from ReportDeadObjectAtDefinition report
21+
from ReportDeadObject report
2222
where
2323
not isExcluded(report.getPrimaryElement(), DeadCode2Package::unusedObjectDefinitionStrictQuery()) and
2424
report.hasAttrUnused()
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
| test.c:6:5:6:6 | definition of g2 | Unused object definition 'g2'. | test.c:6:5:6:6 | test.c:6:5:6:6 | |
2-
| test.c:9:5:9:6 | definition of g3 | Unused object definition 'g3'. | test.c:9:5:9:6 | test.c:9:5:9:6 | |
3-
| test.c:20:7:20:8 | definition of l2 | Unused object definition 'l2'. | test.c:20:7:20:8 | test.c:20:7:20:8 | |
4-
| test.c:27:7:27:8 | definition of l5 | Unused object definition 'l5'. | test.c:27:7:27:8 | test.c:27:7:27:8 | |
5-
| test.c:37:10:37:11 | definition of g5 | Unused object definition 'g5'. | test.c:37:10:37:11 | test.c:37:10:37:11 | |
6-
| test.c:45:9:45:10 | definition of g6 | Unused object definition 'g6'. | test.c:45:9:45:10 | test.c:45:9:45:10 | |
7-
| test.c:51:5:51:6 | definition of g7 | Unused object definition 'g7'. | test.c:51:5:51:6 | test.c:51:5:51:6 | |
8-
| test.c:64:3:64:18 | ONLY_DEF_VAR(x) | Unused object definition 'l2' from macro '$@'. | test.c:60:1:60:34 | test.c:60:1:60:34 | ONLY_DEF_VAR |
9-
| test.c:117:11:117:13 | definition of g10 | Unused object definition 'g10'. | test.c:117:11:117:13 | test.c:117:11:117:13 | |
10-
| test.c:122:13:122:14 | definition of l2 | Unused object definition 'l2'. | test.c:122:13:122:14 | test.c:122:13:122:14 | |
1+
| test.c:6:5:6:6 | definition of g2 | Unused object 'g2'. | test.c:6:5:6:6 | test.c:6:5:6:6 | (ignored) |
2+
| test.c:9:5:9:6 | definition of g3 | Unused object 'g3'. | test.c:9:5:9:6 | test.c:9:5:9:6 | (ignored) |
3+
| test.c:20:7:20:8 | definition of l2 | Unused object 'l2'. | test.c:20:7:20:8 | test.c:20:7:20:8 | (ignored) |
4+
| test.c:27:7:27:8 | definition of l5 | Unused object 'l5'. | test.c:27:7:27:8 | test.c:27:7:27:8 | (ignored) |
5+
| test.c:37:10:37:11 | definition of g5 | Unused object 'g5'. | test.c:37:10:37:11 | test.c:37:10:37:11 | (ignored) |
6+
| test.c:45:9:45:10 | definition of g6 | Unused object 'g6'. | test.c:45:9:45:10 | test.c:45:9:45:10 | (ignored) |
7+
| test.c:51:5:51:6 | definition of g7 | Unused object 'g7'. | test.c:51:5:51:6 | test.c:51:5:51:6 | (ignored) |
8+
| test.c:64:3:64:18 | ONLY_DEF_VAR(x) | Invocation of macro '$@' defines unused object 'l2'. | test.c:60:1:60:34 | test.c:60:1:60:34 | ONLY_DEF_VAR |
9+
| test.c:68:1:71:5 | #define ALSO_DEF_VAR(x) int x = 0; while (1) ; | Macro 'ALSO_DEF_VAR' defines unused object with an invocation-dependent name, for example, '$@'. | test.c:73:16:73:17 | test.c:73:16:73:17 | l1 |
10+
| test.c:77:1:82:3 | #define DEF_UNUSED_INNER_VAR() { int _v = 0; while (1) ; } | Macro 'DEF_UNUSED_INNER_VAR' defines unused object '_v'. | test.c:77:1:82:3 | test.c:77:1:82:3 | (ignored) |
11+
| test.c:119:11:119:13 | definition of g10 | Unused object 'g10'. | test.c:119:11:119:13 | test.c:119:11:119:13 | (ignored) |
12+
| test.c:124:13:124:14 | definition of l2 | Unused object 'l2'. | test.c:124:13:124:14 | test.c:124:13:124:14 | (ignored) |

c/misra/test/rules/RULE-2-8/UnusedObjectDefinitionInMacro.expected

-2
This file was deleted.

c/misra/test/rules/RULE-2-8/UnusedObjectDefinitionInMacro.qlref

-1
This file was deleted.

c/misra/test/rules/RULE-2-8/UnusedObjectDefinitionInMacroStrict.expected

-2
This file was deleted.

c/misra/test/rules/RULE-2-8/UnusedObjectDefinitionInMacroStrict.qlref

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
| test.c:87:29:87:30 | definition of g8 | Unused object definition 'g8'. | test.c:87:29:87:30 | test.c:87:29:87:30 | |
2-
| test.c:90:3:90:30 | ONLY_DEF_ATTR_UNUSED_VAR(x) | Unused object definition 'l2' from macro '$@'. | test.c:88:1:88:70 | test.c:88:1:88:70 | ONLY_DEF_ATTR_UNUSED_VAR |
1+
| test.c:87:29:87:30 | definition of g8 | Unused object 'g8'. | test.c:87:29:87:30 | test.c:87:29:87:30 | (ignored) |
2+
| test.c:92:3:92:30 | ONLY_DEF_ATTR_UNUSED_VAR(x) | Invocation of macro '$@' defines unused object 'l2'. | test.c:88:1:88:70 | test.c:88:1:88:70 | ONLY_DEF_ATTR_UNUSED_VAR |
3+
| test.c:96:1:99:5 | #define ALSO_DEF_ATTR_UNUSED_VAR(x) __attribute__((unused)) int x = 0; while (1) ; | Macro 'ALSO_DEF_ATTR_UNUSED_VAR' defines unused object with an invocation-dependent name, for example, '$@'. | test.c:101:28:101:29 | test.c:101:28:101:29 | l1 |
4+
| test.c:106:1:111:3 | #define DEF_ATTR_UNUSED_INNER_VAR() { __attribute__((unused)) int _v = 0; while (1) ; } | Macro 'DEF_ATTR_UNUSED_INNER_VAR' defines unused object '_v'. | test.c:106:1:111:3 | test.c:106:1:111:3 | (ignored) |

c/misra/test/rules/RULE-2-8/test.c

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ void f6() {
8787
__attribute__((unused)) int g8 = 1; // NON-COMPLIANT
8888
#define ONLY_DEF_ATTR_UNUSED_VAR(x) __attribute__((unused)) int x = 0;
8989
void f7() {
90+
ONLY_DEF_ATTR_UNUSED_VAR(l1); // COMPLIANT
91+
l1;
9092
ONLY_DEF_ATTR_UNUSED_VAR(l2); // NON-COMPLIANT
9193
}
9294

cpp/common/src/codingstandards/cpp/alertreporting/DeduplicateMacroResults.qll

+44-11
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ signature module MacroReportConfigSig<ResultType ResultElement> {
2929
/* Create a message to describe this macro, using '$@' to describe an example `ResultElement`. */
3030
string getMessageVariedResultInAllExpansions(Macro m);
3131

32-
/*
32+
/**
3333
* Create a message to describe this macro expansion which produces a `ResultElement`, using '$@'
3434
* to describe the relevant macro.
3535
*/
36-
3736
string getMessageResultInIsolatedExpansion(ResultElement element);
37+
38+
/**
39+
* Create a message to describe a `ResultElement` which is not generated by a macro expansion.
40+
*/
41+
string getMessageNotInMacro(ResultElement element);
3842
}
3943

4044
/**
@@ -88,7 +92,7 @@ signature module MacroReportConfigSig<ResultType ResultElement> {
8892
* ## Generating Report Objects
8993
*
9094
* This module also can be used to more easily report issues across these cases, by implementing
91-
* `MacroReportConfigSig` and importing `DeduplicateMacroResults::Report::ReportResultInMacro`.
95+
* `MacroReportConfigSig` and importing `DeduplicateMacroResults::Report::ReportResult`.
9296
*
9397
* ```
9498
* module InvalidFooInMacroReportConfig implements MacroReportConfigSig<InvalidFoo> {
@@ -106,11 +110,15 @@ signature module MacroReportConfigSig<ResultType ResultElement> {
106110
* string getMessageResultInIsolatedExpansion(InvalidFoo foo) {
107111
* result = "Invocation of macro $@ has invalid foo '" + foo.getName() + "'."
108112
* }
113+
*
114+
* string getMessageNotInMacro(ResultElement element) {
115+
* result = "Invalid foo '" + element.getName() + "'."
116+
* }
109117
* }
110118
*
111119
* import DeduplicateFooInMacros::Report<InvalidFooInMacroReportConfig> as Report;
112120
*
113-
* from Report::ReportResultInMacro report
121+
* from Report::ReportResult report
114122
* where not excluded(report.getPrimaryElement(), ...)
115123
* select report.getPrimaryElement(), report.getMessage(), report.getOptionalPlaceholderLocation(),
116124
* report.getOptionalPlaceholderMessage()
@@ -234,11 +242,10 @@ module DeduplicateMacroResults<
234242
}
235243
}
236244

237-
/*
245+
/**
238246
* Convenience predicate to know when invalid macro expansions have been reported at their macro
239247
* definition.
240248
*/
241-
242249
private predicate reported(Macro macro) {
243250
macro instanceof PrimaryMacroSameResultElementInAllInvocations or
244251
macro instanceof PrimaryMacroDifferentResultElementInAllInvocations
@@ -248,7 +255,7 @@ module DeduplicateMacroResults<
248255
* A macro invocation for which the target macro does not always produce a `ResultElement`, but
249256
* this specific invocation of it does.
250257
*
251-
* This is "primary" / most specific macro for these result elements. It will also does not match
258+
* This is the "primary" / most specific macro for these result elements. It also does not match
252259
* `MacroInvocation`s inside of a `MacroInvocation` of a `Macro` which always produces a
253260
* `ResultElement`, indicating that the real problem lies with that other `Macro` instead of with
254261
* this particular invocation.
@@ -274,10 +281,15 @@ module DeduplicateMacroResults<
274281
* See the doc comment for the `DeduplicateMacroResults` module for more info.
275282
*/
276283
module Report<MacroReportConfigSig<ResultElement> ReportConfig> {
277-
newtype TReportResultInMacro =
284+
newtype TReportResult =
278285
TReportMacroResultWithSameName(PrimaryMacroSameResultElementInAllInvocations def) or
279286
TReportMacroResultWithVariedName(PrimaryMacroDifferentResultElementInAllInvocations def) or
280-
TReportIsolatedMacroResult(IsolatedMacroExpansionWithResultElement def)
287+
TReportIsolatedMacroResult(IsolatedMacroExpansionWithResultElement def) or
288+
TReportNotInMacro(ResultElement def) {
289+
not exists (ResultMacroExpansion macroExpansion |
290+
macroExpansion.getResultElement() = def
291+
)
292+
}
281293

282294
/**
283295
* An instance of a `ResultElement` to be reported to a user.
@@ -291,7 +303,7 @@ module DeduplicateMacroResults<
291303
* The values returned by these methods are configured by the `MacroReportConfigSig`
292304
* signature parameter.
293305
*/
294-
class ReportResultInMacro extends TReportResultInMacro {
306+
class ReportResult extends TReportResult {
295307
string toString() { result = getMessage() }
296308

297309
string getMessage() {
@@ -310,6 +322,11 @@ module DeduplicateMacroResults<
310322
this = TReportIsolatedMacroResult(def) and
311323
result = ReportConfig::getMessageResultInIsolatedExpansion(def.getResultElement())
312324
)
325+
or
326+
exists(ResultElement def |
327+
this = TReportNotInMacro(def) and
328+
result = ReportConfig::getMessageNotInMacro(def)
329+
)
313330
}
314331

315332
Element getPrimaryElement() {
@@ -318,6 +335,8 @@ module DeduplicateMacroResults<
318335
this = TReportMacroResultWithVariedName(result)
319336
or
320337
this = TReportIsolatedMacroResult(result)
338+
or
339+
this = TReportNotInMacro(result)
321340
}
322341

323342
Location getOptionalPlaceholderLocation() {
@@ -335,6 +354,11 @@ module DeduplicateMacroResults<
335354
this = TReportIsolatedMacroResult(def) and
336355
result = def.getMacro().getLocation()
337356
)
357+
or
358+
exists(ResultElement def |
359+
this = TReportNotInMacro(def) and
360+
result = def.getLocation()
361+
)
338362
}
339363

340364
string getOptionalPlaceholderMessage() {
@@ -343,7 +367,10 @@ module DeduplicateMacroResults<
343367
result = Config::describe(def.getExampleResultElement())
344368
)
345369
or
346-
this = TReportMacroResultWithSameName(_) and
370+
(
371+
this = TReportMacroResultWithSameName(_)
372+
or this = TReportNotInMacro(_)
373+
) and
347374
result = "(ignored)"
348375
or
349376
this = TReportIsolatedMacroResult(_) and
@@ -374,6 +401,12 @@ module DeduplicateMacroResults<
374401
or
375402
this = TReportIsolatedMacroResult(result)
376403
}
404+
405+
ResultElement getAResultElement() {
406+
result = getAResultMacroExpansion().getResultElement()
407+
or
408+
this = TReportNotInMacro(result)
409+
}
377410
}
378411
}
379412
}

0 commit comments

Comments
 (0)