Skip to content

Commit 5eaf437

Browse files
authored
misra.py: Fix R5.4 false positives with C99 (#2516)
* parser: Parse standards node at start event This required, because we can loose data at the end event. * misra.py: Fix 5.4 standard-dependent error By default Cppcheck use C11 standard, so this change fix false positives for rule 5.4 with C99. * travis: force --std=c89 for misra.py
1 parent fb38e87 commit 5eaf437

3 files changed

Lines changed: 27 additions & 13 deletions

File tree

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ matrix:
126126
- cd ../../
127127
# check addons/misra.py
128128
- cd addons/test
129-
- ${CPPCHECK} --dump misra/misra-test.c
129+
# We'll force C89 standard to enable an additional verification for
130+
# rules 5.4 and 5.5 which have standard-dependent options.
131+
- ${CPPCHECK} --dump misra/misra-test.c --std=c89
130132
- python3 ../misra.py -verify misra/misra-test.c.dump
131133
- ${CPPCHECK} --dump misra/misra-test.cpp
132134
- python3 ../misra.py -verify misra/misra-test.cpp.dump

addons/cppcheckdata.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ class Configuration:
656656
functions = []
657657
variables = []
658658
valueflow = []
659-
standards = []
659+
standards = None
660660

661661
def __init__(self, name):
662662
self.name = name
@@ -666,7 +666,7 @@ def __init__(self, name):
666666
self.functions = []
667667
self.variables = []
668668
self.valueflow = []
669-
self.standards = []
669+
self.standards = Standards()
670670

671671
def set_tokens_links(self):
672672
"""Set next/previous links between tokens."""
@@ -762,10 +762,18 @@ class Standards:
762762
posix If Posix was used
763763
"""
764764

765-
def __init__(self, standardsnode):
766-
self.c = standardsnode.find("c").get("version")
767-
self.cpp = standardsnode.find("cpp").get("version")
768-
self.posix = standardsnode.find("posix") is not None
765+
c = ""
766+
cpp = ""
767+
posix = False
768+
769+
def set_c(self, node):
770+
self.c = node.get("version")
771+
772+
def set_cpp(self, node):
773+
self.cpp = node.get("version")
774+
775+
def set_posix(self, node):
776+
self.posix = node.get("posix") is not None
769777

770778
def __repr__(self):
771779
attrs = ["c", "cpp", "posix"]
@@ -896,11 +904,15 @@ def iterconfigurations(self):
896904
cfg = None
897905
cfg_arguments = []
898906

899-
# Parse nested elemenets of configuration node
907+
# Parse standards
900908
elif node.tag == "standards" and event == 'start':
901909
continue
902-
elif node.tag == "standards" and event == 'end':
903-
cfg.standards = Standards(node)
910+
elif node.tag == 'c' and event == 'start':
911+
cfg.standards.set_c(node)
912+
elif node.tag == 'cpp' and event == 'start':
913+
cfg.standards.set_cpp(node)
914+
elif node.tag == 'posix' and event == 'start':
915+
cfg.standards.set_posix(node)
904916

905917
# Parse directives list
906918
elif node.tag == 'directive' and event == 'start':

addons/misra.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,10 +806,10 @@ def __repr__(self):
806806
)
807807

808808
def get_num_significant_naming_chars(self, cfg):
809-
if cfg.standards and cfg.standards.c == "c99":
810-
return 63
811-
else:
809+
if cfg.standards and cfg.standards.c == "c89":
812810
return 31
811+
else:
812+
return 63
813813

814814
def misra_2_7(self, data):
815815
for func in data.functions:

0 commit comments

Comments
 (0)