Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit46f16cc

Browse files
authored
Merge pull request#224 from kraiouchkine/Memory1
Implement C Memory1 package
2 parents0e9bcdd +ca169de commit46f16cc

File tree

25 files changed

+640
-46
lines changed

25 files changed

+640
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| test.c:35:20:35:23 | {...} | Missing braces on aggregate literal of type int[2]$@ which is assigned to index 0 in $@. | file://:0:0:0:0 | int[2] | int[2] | test.c:35:18:35:42 | {...} | array of type int[4][2] |
2+
| test.c:35:26:35:29 | {...} | Missing braces on aggregate literal of type int[2]$@ which is assigned to index 1 in $@. | file://:0:0:0:0 | int[2] | int[2] | test.c:35:18:35:42 | {...} | array of type int[4][2] |
3+
| test.c:35:32:35:35 | {...} | Missing braces on aggregate literal of type int[2]$@ which is assigned to index 2 in $@. | file://:0:0:0:0 | int[2] | int[2] | test.c:35:18:35:42 | {...} | array of type int[4][2] |
4+
| test.c:35:38:35:41 | {...} | Missing braces on aggregate literal of type int[2]$@ which is assigned to index 3 in $@. | file://:0:0:0:0 | int[2] | int[2] | test.c:35:18:35:42 | {...} | array of type int[4][2] |
5+
| test.c:41:34:41:34 | {...} | Missing braces on aggregate literal of type int[2]$@ which is assigned to index 1 in $@. | file://:0:0:0:0 | int[2] | int[2] | test.c:41:18:41:35 | {...} | array of type int[2][2] |
6+
| test.c:47:26:47:29 | {...} | Missing braces on aggregate literal of type $@ which is assigned to field $@. | test.c:4:10:4:10 | struct <unnamed> | struct <unnamed> | test.c:7:5:7:8 | m_s1 | m_s1 |
7+
| test.c:47:26:47:29 | {...} | Missing braces on aggregate literal of type $@ which is assigned to field $@. | test.c:11:10:11:10 | struct <unnamed> | struct <unnamed> | test.c:7:5:7:8 | m_s1 | m_s1 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.useinitializerbracestomatchaggregatetypestructure.UseInitializerBracesToMatchAggregateTypeStructure
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
structFoo {
2+
intm_i1;
3+
intm_i2;
4+
struct {
5+
intm_s1_i1;
6+
intm_s1_i2;
7+
}m_s1;
8+
};
9+
10+
structBar {
11+
struct {
12+
intm_s1_i1;
13+
intm_s1_i2;
14+
}m_s1;
15+
intm_i1;
16+
intm_i2;
17+
};
18+
19+
structBaz {
20+
intm_baz_i1;
21+
intm_baz_i2;
22+
structFoof;
23+
};
24+
25+
structStructNested {
26+
intm_nested_i1;
27+
int*m_nested_i2;
28+
structBazm_baz;
29+
intm_array[10];
30+
};
31+
32+
voidtest() {
33+
intl01[4]= {1,2,3,4};// COMPLIANT
34+
intl02[4][2]= {{1,2}, {3,4}, {3,4}, {3,4}};// COMPLIANT
35+
intl03[4][2]= {1,2,3,4,3,4,3,4};// NON_COMPLIANT - implied braces
36+
intl04[4][2]= {0};// COMPLIANT
37+
intl06[4][2]= {{0}, {0}, {0}, {0}};// COMPLIANT, nested zero initializer
38+
intl08[4]= {1,2};// COMPLIANT, but missing explicit init
39+
intl09[2][2]= {{1,2}};// COMPLIANT, but missing explicit init
40+
intl10[2][2]= {{1,2}, [1]= {0}};// COMPLIANT
41+
intl11[2][2]= {{1,2}, [1]=0};// NON_COMPLIANT - implied braces
42+
intl12[2][2]= {{1,2}, [1][0]=0, [1][1]=0};// COMPLIANT
43+
intl13[2][2]= {{0}, [1][0]=0};// COMPLIANT
44+
intl14[2][2]= {
45+
{0}, [1][0]=0,0};// NON_COMPLIANT[FALSE_NEGATIVE] - not all elements
46+
// initialized with designated initializer
47+
structFoof1= {1,2,3,4};// NON_COMPLIANT - implied braces
48+
structFoof2= {1,2, {3,4}};// COMPLIANT
49+
structFoof3= {0};// COMPLIANT
50+
structFoof4= {0,2};// COMPLIANT, but missing explicit init
51+
structFoof5= {0,2, {0}};// COMPLIANT
52+
structBarb1= {0};// COMPLIANT
53+
structBarb2= {{0}};// COMPLIANT, but missing explicit init
54+
structStructNestedn= {0};// COMPLIANT
55+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @id c/misra/initializer-for-aggregate-or-union-not-enclosed-in-braces
3+
* @name RULE-9-2: The initializer for an aggregate or union shall be enclosed in braces
4+
* @description Using braces in initializers of objects and subobjects improves code readability and
5+
* clarifies intent.
6+
* @kind problem
7+
* @precision high
8+
* @problem.severity recommendation
9+
* @tags external/misra/id/rule-9-2
10+
* maintainability
11+
* readability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.cpp.rules.useinitializerbracestomatchaggregatetypestructure.UseInitializerBracesToMatchAggregateTypeStructure
18+
19+
classInitializerForAggregateOrUnionNotEnclosedInBracesQueryextendsUseInitializerBracesToMatchAggregateTypeStructureSharedQuery{
20+
InitializerForAggregateOrUnionNotEnclosedInBracesQuery(){
21+
this= Memory1Package::initializerForAggregateOrUnionNotEnclosedInBracesQuery()
22+
}
23+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* @id c/misra/partially-initialized-array-with-explicit-initializers
3+
* @name RULE-9-3: Arrays shall not be partially initialized
4+
* @description An array object or a subobject of an array shall be explicitly initialized if any
5+
* other object in that array is explicitly initialized.
6+
* @kind problem
7+
* @precision high
8+
* @problem.severity warning
9+
* @tags external/misra/id/rule-9-3
10+
* maintainability
11+
* readability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.cpp.enhancements.AggregateLiteralEnhancements
18+
19+
/**
20+
* Holds if the aggregate literal has at least one explicit initializer, and at least one
21+
* _missing_ explicit initializer, and not _only_ designated initializers.
22+
*/
23+
predicateisMissingExplicitInitializers(AggregateLiteralal){
24+
notal.isCompilerGenerated()and
25+
notal.isAffectedByMacro()and
26+
// Partially initialized, but not initialized with a leading zero (which is permitted)
27+
isPartiallyValueInitialized(al)and
28+
notisLeadingZeroInitialized(al)
29+
}
30+
31+
// note: this query is similar to M8-5-2: MissingExplicitInitializers.ql
32+
// but, pursuant to Rule 9.3, only covers array initializers rather than all aggregates
33+
fromAggregateLiteralal,TypeaggType,ElementexplanationElement,stringexplanationDescription
34+
where
35+
notisExcluded(al, Memory1Package::partiallyInitializedArrayWithExplicitInitializersQuery())and
36+
// The aggregate literal is missing at least one explicit initializer
37+
isMissingExplicitInitializers(al)and
38+
// Missing array initializer
39+
exists(intarraySize,intminIndex,intmaxIndex|
40+
// Identify the size of the array with a missing initializer
41+
arraySize=al.getType().getUnspecifiedType().(ArrayType).getArraySize()and
42+
// Identify the smallest index missing an initialzer
43+
minIndex=
44+
min(intindex|
45+
index=[0 ..arraySize-1]and ArrayAggregateLiterals::isValueInitialized(al,index)
46+
|
47+
index
48+
)and
49+
// Identify the largest index missing an initialzer
50+
maxIndex=
51+
max(intindex|
52+
index=[0 ..arraySize-1]and ArrayAggregateLiterals::isValueInitialized(al,index)
53+
|
54+
index
55+
)and
56+
// Ensure that the maxIndex is the last array entry. If it's not, something is up with this
57+
// database, and so we shouldn't report it (because you can only initialize trailing array
58+
// values)
59+
maxIndex=(arraySize-1)and
60+
// Nothing useful to point to as the explanation element, so let's just set it to the parent
61+
// array
62+
explanationElement=aland
63+
(
64+
ifminIndex=maxIndex
65+
then
66+
// Only one element missing
67+
explanationDescription="the element at index "+minIndex
68+
else
69+
// Multiple elements missing
70+
explanationDescription="the elements in the index range "+minIndex+" to "+maxIndex
71+
)
72+
)
73+
selectal,
74+
"Aggregate literal for "+getAggregateTypeDescription(al,aggType)+
75+
" is missing an explicit initializer for $@.",aggType,aggType.getName(),explanationElement,
76+
explanationDescription
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* @id c/misra/repeated-initialization-of-aggregate-object-element
3+
* @name RULE-9-4: An element of an object shall not be initialized more than once
4+
* @description Repeated initialization of an element in an object can lead to side-effects or may
5+
* signal a logic error.
6+
* @kind problem
7+
* @precision high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-9-4
10+
* correctness
11+
* maintainability
12+
* readability
13+
* external/misra/obligation/required
14+
*/
15+
16+
import cpp
17+
import codingstandards.c.misra
18+
import codingstandards.cpp.enhancements.AggregateLiteralEnhancements
19+
20+
/**
21+
* Gets the `n`th parent of `e`.
22+
* If `n` is zero, the result is `e`.
23+
*/
24+
ExprgetNthParent(Expre,intn){
25+
ifn=0thenresult=eelseresult=getNthParent(e.getParent(),n-1)
26+
}
27+
28+
/**
29+
* Returns a string representation of the index of `e` relative
30+
* to the nested array aggregate literal structure it is contained in.
31+
*/
32+
stringgetNestedArrayIndexString(Expre){
33+
result=
34+
concat(intdepth|
35+
depth=[0 ..getMaxDepth(getRootAggregate(e.getParent()))]
36+
|
37+
"["+
38+
any(intelementIndex|
39+
exists(ArrayAggregateLiteralparent|
40+
parent=getNthParent(e,pragma[only_bind_into](depth+1))and
41+
parent.getElementExpr(elementIndex)=getNthParent(e,pragma[only_bind_into](depth))
42+
)
43+
|
44+
elementIndex
45+
).toString()+"]"
46+
orderby
47+
depthdesc
48+
)
49+
}
50+
51+
/**
52+
* Returns the number of levels of nested `ArrayAggregateLiteral`s in `al`.
53+
* If there are no nested array aggregate literals, the max depth of the `ArrayAggregateLiteral` is `0`.
54+
*/
55+
language[monotonicAggregates]
56+
intgetMaxDepth(ArrayAggregateLiteralal){
57+
ifnotexists(al.getElementExpr(_).(ArrayAggregateLiteral))
58+
thenresult=0
59+
elseresult=1+max(Exprchild|child=al.getElementExpr(_)|getMaxDepth(child))
60+
}
61+
62+
// internal recursive predicate for `hasMultipleInitializerExprsForSameIndex`
63+
predicatehasMultipleInitializerExprsForSameIndexInternal(
64+
ArrayAggregateLiteralal1,ArrayAggregateLiteralal2,Exprout_al1_expr,Exprout_al2_expr
65+
){
66+
exists(intshared_index,Expral1_expr,Expral2_expr|
67+
// an `Expr` initializing an element of the same index in both `al1` and `al2`
68+
shared_index=[0 ..al1.getArraySize()-1]and
69+
al1_expr=al1.getElementExpr(shared_index)and
70+
al2_expr=al2.getElementExpr(shared_index)and
71+
// but not the same `Expr`
72+
notal1_expr=al2_exprand
73+
(
74+
// case A - the children are not aggregate literals
75+
// holds if `al1` and `al2` both hold for .getElement[sharedIndex]
76+
notal1_exprinstanceofArrayAggregateLiteraland
77+
out_al1_expr=al1_exprand
78+
out_al2_expr=al2_expr
79+
or
80+
// case B - `al1` and `al2` both have an aggregate literal child at the same index, so recurse
81+
hasMultipleInitializerExprsForSameIndexInternal(al1_expr,al2_expr,out_al1_expr,out_al2_expr)
82+
)
83+
)
84+
}
85+
86+
/**
87+
* Holds if `expr1` and `expr2` both initialize the same array element of `root`.
88+
*/
89+
predicatehasMultipleInitializerExprsForSameIndex(ArrayAggregateLiteralroot,Exprexpr1,Exprexpr2){
90+
hasMultipleInitializerExprsForSameIndexInternal(root,root,expr1,expr2)
91+
}
92+
93+
/**
94+
* Holds if `expr1` and `expr2` both initialize the same field of `root`.
95+
*
96+
* The dbschema keyset for `aggregate_field_init` prevents referencing multiple `Expr`
97+
* that initialize the same Field and are part of the same `ClassAggregateLiteral`.
98+
* This predicate is therefore unable to distinguish the individual duplicate expressions.
99+
*/
100+
predicatehasMultipleInitializerExprsForSameField(ClassAggregateLiteralroot,Fieldf){
101+
count(root.getFieldExpr(f))>1
102+
}
103+
104+
from
105+
AggregateLiteralroot,Expre1,Expre2,stringelementDescription,stringrootType,
106+
stringclarification
107+
where
108+
notisExcluded(e1, Memory1Package::repeatedInitializationOfAggregateObjectElementQuery())and
109+
exists(Initializerinit|init.getExpr()=root)and
110+
(
111+
hasMultipleInitializerExprsForSameIndex(root,e1,e2)and
112+
elementDescription=getNestedArrayIndexString(e1)and
113+
rootType="Array aggregate literal"and
114+
clarification=", which is already initialized $@."
115+
or
116+
exists(Fieldf|
117+
// we cannot distinguish between different aggregate field init expressions.
118+
// therefore, we only report the root aggregate rather than any child init expr.
119+
// see `hasMultipleInitializerExprsForSameField` documentation.
120+
hasMultipleInitializerExprsForSameField(root,f)and
121+
e1=rootand
122+
e2=rootand
123+
elementDescription=f.getQualifiedName()and
124+
rootType="Structure aggregate literal"and
125+
clarification="."
126+
)
127+
)and
128+
// de-duplicate the results by excluding permutations of `e1` and `e2`
129+
e1.getLocation().toString()<=e2.getLocation().toString()
130+
selecte1,"$@ repeats initialization of element "+elementDescription+clarification,root,
131+
rootType,e2,"here"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/common/test/rules/useinitializerbracestomatchaggregatetypestructure/UseInitializerBracesToMatchAggregateTypeStructure.ql
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
| test.c:7:15:7:21 | {...} | Aggregate literal for type int[4]$@ is missing an explicit initializer for $@. | file://:0:0:0:0 | int[4] | int[4] | test.c:7:15:7:21 | {...} | the elements in the index range 2 to 3 |
2+
| test.c:8:18:8:26 | {...} | Aggregate literal for type int[2][2]$@ is missing an explicit initializer for $@. | file://:0:0:0:0 | int[2][2] | int[2][2] | test.c:8:18:8:26 | {...} | the element at index 1 |
3+
| test.c:12:18:12:35 | {...} | Aggregate literal for type int[2][2]$@ is missing an explicit initializer for $@. | file://:0:0:0:0 | int[2][2] | int[2][2] | test.c:12:18:12:35 | {...} | the element at index 1 |
4+
| test.c:14:18:15:25 | {...} | Aggregate literal for type int[2][2]$@ is missing an explicit initializer for $@. | file://:0:0:0:0 | int[2][2] | int[2][2] | test.c:14:18:15:25 | {...} | the element at index 1 |
5+
| test.c:20:18:20:32 | {...} | Aggregate literal for type int[2][2]$@ is missing an explicit initializer for $@. | file://:0:0:0:0 | int[2][2] | int[2][2] | test.c:20:18:20:32 | {...} | the element at index 1 |
6+
| test.c:31:43:31:43 | {...} | Aggregate literal for type int[4]$@ is missing an explicit initializer for $@. | file://:0:0:0:0 | int[4] | int[4] | test.c:31:43:31:43 | {...} | the elements in the index range 1 to 3 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-9-3/PartiallyInitializedArrayWithExplicitInitializers.ql

‎c/misra/test/rules/RULE-9-3/test.c‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
voidtest() {
2+
intl01[4]= {1,2,3,4};// COMPLIANT
3+
intl02[4][2]= {{1,2}, {3,4}, {3,4}, {3,4}};// COMPLIANT
4+
intl03[4][2]= {1,2,3,4,3,4,3,4};// COMPLIANT
5+
intl04[4][2]= {0};// COMPLIANT
6+
intl06[4][2]= {{0}, {0}, {0}, {0}};// COMPLIANT
7+
intl08[4]= {1,2};// NON_COMPLIANT
8+
intl09[2][2]= {{1,2}};// NON_COMPLIANT
9+
intl10[2][2]= {{1,2}, [1]= {0}};// COMPLIANT
10+
intl11[2][2]= {{1,2}, [1]=0};// COMPLIANT
11+
intl12[2][2]= {{1,2}, [1][0]=0, [1][1]=0};// COMPLIANT
12+
intl13[2][2]= {{0}, [1][0]=0};// NON_COMPLIANT - not all elements
13+
// initialized with designated initializer
14+
intl14[2][2]= {
15+
{0}, [1][0]=0,0};// NON_COMPLIANT - not all elements
16+
// initialized with designated initializer
17+
18+
intl15[2]= {[1]=0};// COMPILANT - sparse matrix initialized with
19+
// designated initializer
20+
intl16[2][2]= {[0]= {0,1}};// NON_COMPLIANT - sub-elements not
21+
// initialized with designated initializer
22+
23+
intl17[4][4]= {
24+
[0][0]=0, [0][1]=0, [0][2]=0, [0][3]=0, [2][0]=0,
25+
[2][1]=0, [2][2]=0, [2][3]=0};// COMPLIANT - sparse matrix
26+
// initialized with designated
27+
// initializer
28+
29+
intl18[4][4]= {
30+
[0][0]=0, [0][1]=0, [0][2]=0, [0][3]=0, [2][0]=0,
31+
[2][1]=0, [2][2]=0, [2][3]=0,2};// NON_COMPLIANT - not all
32+
// elements initialized with
33+
// designated initializer
34+
35+
charstr1[4]="abc";// COMPLIANT
36+
charstr2[5]="abc";// COMPLIANT - array initialized by string literal
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp