Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #10854: False positive: misra-c2012-9.2: inner union #5693

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions addons/cppcheckdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ class Scope:
function = None
nestedInId = None
nestedIn = None
nestedList = None
type = None
isExecutable = None
varlistId = None
Expand All @@ -583,6 +584,7 @@ def __init__(self, element):
self.bodyEnd = None
self.nestedInId = element.get('nestedIn')
self.nestedIn = None
self.nestedList = list()
self.type = element.get('type')
self.definedType = element.get('definedType')
self.isExecutable = (self.type in ('Function', 'If', 'Else', 'For', 'While', 'Do',
Expand All @@ -603,6 +605,8 @@ def setId(self, IdMap):
self.bodyStart = IdMap[self.bodyStartId]
self.bodyEnd = IdMap[self.bodyEndId]
self.nestedIn = IdMap[self.nestedInId]
if self.nestedIn:
self.nestedIn.nestedList.append(self)
self.function = IdMap[self.functionId]
for v in self.varlistId:
value = IdMap.get(v)
Expand Down
20 changes: 19 additions & 1 deletion addons/misra_9.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,29 @@ def createRecordChildrenDefs(ed, var):
child = ElementDef("pointer", var.nameToken, var.nameToken.valueType)
ed.addChild(child)
return
child_dict = {}
for variable in valueType.typeScope.varlist:
if variable is var:
continue
child = getElementDef(variable.nameToken)
ed.addChild(child)
child_dict[variable.nameToken] = child
for scopes in valueType.typeScope.nestedList:
varscope = False
if scopes.nestedIn == valueType.typeScope:
for variable in valueType.typeScope.varlist:
if variable.nameToken and variable.nameToken.valueType and variable.nameToken.valueType.typeScope == scopes:
varscope = True
break
if not varscope:
ed1 = ElementDef("record", scopes.Id, valueType)
for variable in scopes.varlist:
child = getElementDef(variable.nameToken)
ed1.addChild(child)
child_dict[scopes.bodyStart] = ed1
sorted_keys = sorted(list(child_dict.keys()), key=lambda k: "%s %s %s" % (k.file, k.linenr, k.column))
for _key in sorted_keys:
ed.addChild(child_dict[_key])


def getElementByDesignator(ed, token):
if not token.str in [ '.', '[' ]:
Expand Down
15 changes: 15 additions & 0 deletions addons/test/misra/misra-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,21 @@ static void misra_8_14(char * restrict str) {(void)str;} // 8.14
struct S_9_3 { struct S_9_3* p; int x; };
struct S_9_3* s_9_3_array[] = { x, NULL }; // 8.4

// #10854
struct Entry_9_2{
union{ // 19.2
const int *p;
int x;
};
int y;
};

static void misra_9_2_10854(void){
struct Entry_9_2 e1[] =
{
{{ .x = 1 }, .y = 2 }
};
}
static void misra_9_empty_or_zero_initializers(void) {
int a[2] = {}; // 9.2
int b[2][2] = {}; // 9.2
Expand Down
Loading