Skip to content

misra.py: Add R18.7 check#1934

Merged
danmar merged 4 commits into
cppcheck-opensource:masterfrom
jubnzv:misra-18-7
Jul 1, 2019
Merged

misra.py: Add R18.7 check#1934
danmar merged 4 commits into
cppcheck-opensource:masterfrom
jubnzv:misra-18-7

Conversation

@jubnzv

@jubnzv jubnzv commented Jun 29, 2019

Copy link
Copy Markdown
Contributor

No description provided.

@jubnzv jubnzv changed the title misra.py: Add 18.7 check misra.py: Add R18.7 check Jun 29, 2019

@danmar danmar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking at this!

Comment thread addons/misra.py Outdated
continue
structScopes.append(token.valueType.typeScope)

for scope in structScopes:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that you remove the structScopes above and change this loop:

for scope in data.scopes:
    if scope.type != 'Struct':
        continue

Comment thread addons/misra.py Outdated
token = scope.bodyStart.next
while token != scope.bodyEnd and token is not None:
# Handle nested structures to not duplicate an error.
if token.str == 'struct':

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this work?

if token.str == '{':
    token = token.link

@jubnzv jubnzv Jun 30, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, no. I need a struct token to get its typeScope. This is necessary because I want to iterate over this scope ignoring possible nested structs like this:

struct {
  struct {
    struct {
      uint8_t a3[42];
    } s3;
  } s2;
} s1;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes with my code you would ignore all nested scopes.

Comment thread addons/misra.py Outdated
token = nestedStructScope.bodyEnd.next.next.next
continue

if token.str == '[' and token.next.str == ']':

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm.. you can use cppcheckdata.simpleMatch(token, "[ ]")

Comment thread addons/test/test-misra.py Outdated
assert("(style)" in captured)


def test_misra_18_7(checker):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant .. Travis executes this command:

 ~/cppcheck/cppcheck --dump misra-test.c && python ../misra.py -verify misra-test.c.dump

And this command will verify that the addon outputs an error report on every marked line and nowhere else.

@jubnzv

jubnzv commented Jun 30, 2019

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I corrected it.

@danmar

danmar commented Jun 30, 2019

Copy link
Copy Markdown
Collaborator

I checked out your code and changed it:

def misra_18_7(self, data):
    for scope in data.scopes:
        if scope.type != 'Struct':
            continue

        token = scope.bodyStart.next
        while token != scope.bodyEnd and token is not None:
            # Handle nested structures to not duplicate an error.
            if token.str == '{':
                token = token.link

            if cppcheckdata.simpleMatch(token, "[ ]"):
                self.reportError(token, 18, 7)
                break
            token = token.next

This code works fine as far as I see.

Comment thread addons/misra.py Outdated
return False


def isStruct(token):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the isStruct can be removed, I believe

@danmar
danmar merged commit f8c350f into cppcheck-opensource:master Jul 1, 2019
@danmar

danmar commented Jul 1, 2019

Copy link
Copy Markdown
Collaborator

Thanks!

jubnzv added a commit to jubnzv/cppcheck that referenced this pull request Nov 13, 2019
* misra.py: Add 18.7 check

* Simplify R18.7 check

* use token.link

* Remove isStruct method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants