Skip to content

Commit 651db08

Browse files
authored
Merge branch 'main' into rule-17-6
2 parents bd58c29 + d3d017b commit 651db08

File tree

5 files changed

+63
-24
lines changed

5 files changed

+63
-24
lines changed

c/misra/src/rules/RULE-20-12/MacroParameterUsedAsHashOperand.ql

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import codingstandards.cpp.Macro
1919

2020
from FunctionLikeMacro m, MacroInvocation mi, int i, string expanded, string param
2121
where
22-
not isExcluded(m, Preprocessor2Package::macroParameterUsedAsHashOperandQuery()) and
22+
not isExcluded(mi, Preprocessor2Package::macroParameterUsedAsHashOperandQuery()) and
2323
mi = m.getAnInvocation() and
2424
param = m.getParameter(i) and
2525
(
@@ -31,9 +31,6 @@ where
3131
// This check ensure there is an expansion that is used.
3232
expanded = mi.getExpandedArgument(i) and
3333
not expanded = "" and
34-
exists(Macro furtherExpandedMacro |
35-
mi.getUnexpandedArgument(i).matches(furtherExpandedMacro.getName() + "%")
36-
)
34+
not mi.getUnexpandedArgument(i) = mi.getExpandedArgument(i)
3735
select m,
38-
"Macro " + m.getName() + " contains use of parameter " + m.getParameter(i) +
39-
" used in multiple contexts."
36+
"Macro " + m.getName() + " contains use of parameter " + param + " used in multiple contexts."

c/misra/src/rules/RULE-5-8/IdentifiersWithExternalLinkageNotUnique.ql

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,39 @@ import cpp
1515
import codingstandards.c.misra
1616
import codingstandards.cpp.Identifiers
1717

18-
from Declaration de, ExternalIdentifiers e
18+
/**
19+
* Holds if the `identifierName` has conflicting declarations.
20+
*/
21+
predicate isExternalIdentifierNotUnique(string identifierName) {
22+
// More than one declaration with this name
23+
count(Declaration d | d.getName() = identifierName) > 1 and
24+
// At least one declaration is an external identifier
25+
exists(ExternalIdentifiers e | e.getName() = identifierName)
26+
}
27+
28+
/**
29+
* Holds if the `Declaration` `d` is conflicting with an external identifier.
30+
*/
31+
predicate isConflictingDeclaration(Declaration d, string name) {
32+
isExternalIdentifierNotUnique(name) and
33+
d.getName() = name
34+
}
35+
36+
/**
37+
* An external identifier which is not uniquely defined in the source code.
38+
*/
39+
class NotUniqueExternalIdentifier extends ExternalIdentifiers {
40+
NotUniqueExternalIdentifier() { isExternalIdentifierNotUnique(getName()) }
41+
42+
Declaration getAConflictingDeclaration() {
43+
not result = this and
44+
isConflictingDeclaration(result, getName())
45+
}
46+
}
47+
48+
from NotUniqueExternalIdentifier e, Declaration de
1949
where
2050
not isExcluded(de, Declarations6Package::identifiersWithExternalLinkageNotUniqueQuery()) and
2151
not isExcluded(e, Declarations6Package::identifiersWithExternalLinkageNotUniqueQuery()) and
22-
not de = e and
23-
de.getName() = e.getName()
52+
de = e.getAConflictingDeclaration()
2453
select de, "Identifier conflicts with external identifier $@", e, e.getName()

c/misra/src/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.ql

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,36 @@ import codingstandards.c.misra
2020
import codingstandards.cpp.Identifiers
2121
import codingstandards.cpp.Scope
2222

23+
ExternalIdentifiers getExternalIdentifierTarget(NameQualifiableElement nqe) {
24+
result = nqe.(Access).getTarget()
25+
or
26+
result = nqe.(FunctionCall).getTarget()
27+
}
28+
2329
/**
24-
* Re-introduce function calls into access description as
25-
* "any reference"
30+
* A reference to an external identifier, either as an `Access` or a `FunctionCall`.
2631
*/
27-
class Reference extends NameQualifiableElement {
28-
Reference() {
29-
this instanceof Access or
30-
this instanceof FunctionCall
31-
}
32+
class ExternalIdentifierReference extends NameQualifiableElement {
33+
ExternalIdentifierReference() { exists(getExternalIdentifierTarget(this)) }
34+
35+
ExternalIdentifiers getExternalIdentifierTarget() { result = getExternalIdentifierTarget(this) }
36+
}
37+
38+
predicate isReferencedInTranslationUnit(
39+
ExternalIdentifiers e, ExternalIdentifierReference r, TranslationUnit t
40+
) {
41+
r.getExternalIdentifierTarget() = e and
42+
r.getFile() = t
3243
}
3344

34-
from ExternalIdentifiers e, Reference a1, TranslationUnit t1
45+
from ExternalIdentifiers e, ExternalIdentifierReference a1, TranslationUnit t1
3546
where
3647
not isExcluded(e, Declarations6Package::shouldNotBeDefinedWithExternalLinkageQuery()) and
37-
(a1.(Access).getTarget() = e or a1.(FunctionCall).getTarget() = e) and
38-
a1.getFile() = t1 and
39-
//not accessed in any other translation unit
40-
not exists(TranslationUnit t2, Reference a2 |
41-
not t1 = t2 and
42-
(a2.(Access).getTarget() = e or a2.(FunctionCall).getTarget() = e) and
43-
a2.getFile() = t2
48+
isReferencedInTranslationUnit(e, a1, t1) and
49+
// Not referenced in any other translation unit
50+
not exists(TranslationUnit t2 |
51+
isReferencedInTranslationUnit(e, _, t2) and
52+
not t1 = t2
4453
)
4554
select e, "Declaration with external linkage is accessed in only one translation unit $@.", a1,
4655
a1.toString()

change_notes/2023-03-07-20-12-perf.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* `Rule 20.12` - the performance of this rule has been improved.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* The performance of the following identifier related rules has been improved:
2+
* MISRA C 2012 `Rule 5.8`
3+
* MISRA C 2012 `Rule 8.7`

0 commit comments

Comments
 (0)