@@ -16,31 +16,67 @@ import cpp
1616import codingstandards.c.misra
1717
1818/**
19- * An initialized definition of a variable, for example:
19+ * Gets the outermost macro invocation that expands `element`, if any, such as
20+ * `DEFINE_OBJECT(0, x)` for `x` in `#define DEFINE_OBJECT(value, name) int name = value`.
21+ */
22+ MacroInvocation getOutermostMacroInvocation ( Locatable element ) {
23+ element = result .getAnExpandedElement ( ) and
24+ not exists ( result .getParentInvocation ( ) )
25+ }
26+
27+ /**
28+ * Holds if `invocation` is an outermost macro invocation that expands `entry` together with at
29+ * least part of `initializer`, such as in `#define DEFINE_OBJECT(value, name) int name = value`.
30+ */
31+ predicate hasMacroInitializer (
32+ VariableDeclarationEntry entry , MacroInvocation invocation , Initializer initializer
33+ ) {
34+ entry .isDefinition ( ) and
35+ initializer = entry .getVariable ( ) .getInitializer ( ) and
36+ invocation = getOutermostMacroInvocation ( entry ) and
37+ invocation = getOutermostMacroInvocation ( initializer .getExpr ( ) .getAChild * ( ) )
38+ }
39+
40+ /**
41+ * Holds if `initializer` follows `entry` in source order in every macro expansion at
42+ * `initializer`'s location without an intervening definition of the same variable, such as in
43+ * `int global_object = 1;`.
44+ */
45+ predicate hasSourceOrderInitializer ( VariableDeclarationEntry entry , Initializer initializer ) {
46+ entry .isDefinition ( ) and
47+ initializer = entry .getVariable ( ) .getInitializer ( ) and
48+ not hasMacroInitializer ( _, _, initializer ) and
49+ entry .getLocation ( ) .isBefore ( initializer .getLocation ( ) , _) and
50+ not exists ( VariableDeclarationEntry later |
51+ later .getVariable ( ) = entry .getVariable ( ) and
52+ later .isDefinition ( ) and
53+ entry .getLocation ( ) .isBefore ( later .getLocation ( ) , _) and
54+ later .getLocation ( ) .isBefore ( initializer .getLocation ( ) , _)
55+ ) and
56+ not exists ( MacroInvocation initMacro , MacroInvocation otherMacro |
57+ initMacro = getOutermostMacroInvocation ( initializer .getExpr ( ) ) and
58+ otherMacro .getLocation ( ) = initMacro .getLocation ( ) and
59+ not exists ( otherMacro .getParentInvocation ( ) ) and
60+ not exists ( Initializer otherInitializer |
61+ otherInitializer = entry .getVariable ( ) .getInitializer ( ) and
62+ otherMacro = getOutermostMacroInvocation ( otherInitializer .getExpr ( ) )
63+ )
64+ )
65+ }
66+
67+ /**
68+ * A variable declaration entry that defines and initializes a variable in every expansion, for
69+ * example:
2070 * ```c
2171 * int global_object = 1;
2272 * ```
2373 */
2474class InitializedDefinition extends VariableDeclarationEntry {
2575 InitializedDefinition ( ) {
26- exists ( Initializer initializer |
27- this .isDefinition ( ) and
28- initializer = this .getVariable ( ) .getInitializer ( ) and
29- (
30- initializer .getLocation ( ) .subsumes ( this .getLocation ( ) )
31- or
32- // Initializers follow their own declarators, not definitions in other translation units.
33- this .getLocation ( ) .isBefore ( initializer .getLocation ( ) , _) and
34- not exists ( VariableDeclarationEntry later |
35- later .getVariable ( ) = this .getVariable ( ) and
36- later .isDefinition ( ) and
37- this .getLocation ( ) .isBefore ( later .getLocation ( ) , _) and
38- (
39- later .getLocation ( ) .isBefore ( initializer .getLocation ( ) , _) or
40- initializer .getLocation ( ) .subsumes ( later .getLocation ( ) )
41- )
42- )
43- )
76+ hasSourceOrderInitializer ( this , _)
77+ or
78+ forex ( MacroInvocation invocation | invocation = getOutermostMacroInvocation ( this ) |
79+ hasMacroInitializer ( this , invocation , _)
4480 )
4581 }
4682}
0 commit comments