Skip to content

Commit da98fe3

Browse files
committed
Match C++ semantics closer
1 parent b14b932 commit da98fe3

2 files changed

Lines changed: 44 additions & 14 deletions

File tree

‎addons/cppcheckdata.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,14 +1702,16 @@ def compile_atom(cls, p):
17021702
if p in patterns:
17031703
return patterns[p]
17041704
prefix, word = p[:2], p[2:]
1705-
# a '!!' or '**' prefix applies to the whole word only when the word
1706-
# is a literal: '!!|=' is "not |=", but '!!a|b' is "not a, or b"
1707-
if '|' in p and not (prefix in ('!!', '**') and word in cls._literal_atoms):
1708-
return cls._match_alternatives([cls.compile_atom(x) for x in p.split('|')])
17091705
if prefix == '!!':
1710-
return cls._match_negation(cls.compile_atom(word))
1706+
# as in the C++ Token::Match, '!!' is checked before alternation
1707+
# and the rest is compared as a whole: '!!a|b' is "not the token a|b"
1708+
inner = cls._match_literal(word) if '|' in word else cls.compile_atom(word)
1709+
return cls._match_negation(inner)
17111710
if prefix == '**':
1711+
# '**a|b' searches ahead for a or b
17121712
return cls._match_forward(cls.compile_atom(word))
1713+
if '|' in p:
1714+
return cls._match_alternatives([cls.compile_atom(x) for x in p.split('|')])
17131715
return cls._match_literal(p)
17141716

17151717
@staticmethod

‎test/addon/test_cppcheckdata.py‎

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -722,28 +722,56 @@ def test_negation_literal_or_assign(self, match_cfg):
722722
assert cppcheckdata.match(bit_or.previous, '%var% !!|=')
723723
assert cppcheckdata.match(find_token(match_cfg, '=').previous, '%var% !!|=')
724724

725+
def test_negation_literal_bitwise_or(self, match_cfg):
726+
bit_or = find_token(match_cfg, '|')
727+
assert not cppcheckdata.match(bit_or.previous, '%var% !!|')
728+
assert cppcheckdata.match(find_token(match_cfg, '||').previous, '%var% !!|')
729+
assert cppcheckdata.match(find_token(match_cfg, '|=').previous, '%var% !!|')
730+
731+
def test_negation_literal_logical_or(self, match_cfg):
732+
log_or = find_token(match_cfg, '||')
733+
assert not cppcheckdata.match(log_or.previous, '%var% !!||')
734+
assert cppcheckdata.match(find_token(match_cfg, '|').previous, '%var% !!||')
735+
assert cppcheckdata.match(find_token(match_cfg, '|=').previous, '%var% !!||')
736+
725737
def test_negation_alternatives(self, match_cfg):
726-
# '!!a|b' is "not a, or b", as in the C++ Token::Match
738+
# as in the C++ Token::Match, the word after '!!' is not split into
739+
# alternatives: '!!a|b' is "not the token a|b"
727740
struct_tok = find_token(match_cfg, 'struct')
728-
assert not cppcheckdata.match(struct_tok, 'struct !!Point|Foo')
741+
assert cppcheckdata.match(struct_tok, 'struct !!Point|Foo')
729742
assert cppcheckdata.match(struct_tok, 'struct !!Foo|Point')
730743
assert cppcheckdata.match(struct_tok, 'struct !!Foo|Bar')
744+
assert cppcheckdata.match(struct_tok, 'struct !!%name%|;')
731745

732746
def test_find_forward_alternatives(self, match_cfg):
733-
# '**a|b' is "a somewhere ahead, or b here"
747+
# '**a|b' searches ahead for the first token that is a or b
734748
calc_def = find_token(match_cfg, 'calc')
735749
res = cppcheckdata.match(calc_def, 'calc **{|nosuchtoken')
736750
assert res
737751
assert res.end.str == '{'
738-
res = cppcheckdata.match(calc_def, 'calc **nosuchtoken|(')
752+
res = cppcheckdata.match(calc_def, 'calc **nosuchtoken|{')
753+
assert res
754+
assert res.end.str == '{'
755+
res = cppcheckdata.match(calc_def, 'calc **{|(')
739756
assert res
740757
assert res.end.str == '('
741-
assert not cppcheckdata.match(calc_def, 'calc **nosuchtoken|{')
742-
# but a literal word is searched for as a whole
743-
arr_decl = find_token(match_cfg, 'arr')
744-
res = cppcheckdata.match(arr_decl, 'arr **|=')
758+
res = cppcheckdata.match(calc_def, 'calc **%op%|{')
745759
assert res
746-
assert res.end.str == '|='
760+
assert res.end.str == '{'
761+
assert not cppcheckdata.match(calc_def, 'calc **nosuchtoken|nosuchtoken2')
762+
763+
def test_find_forward_literal_or_operators(self, match_cfg):
764+
# '**|', '**||' and '**|=' search for the operator as a whole
765+
bit_or_decl = find_token(match_cfg, 'bit_or')
766+
for op in ('|', '||', '|='):
767+
res = cppcheckdata.match(bit_or_decl, 'bit_or **' + op)
768+
assert res
769+
assert res.end.str == op
770+
# past the last '|' and '||' only the '|=' is left to find
771+
arr_decl = find_token(match_cfg, 'arr')
772+
assert not cppcheckdata.match(arr_decl, 'arr **|')
773+
assert not cppcheckdata.match(arr_decl, 'arr **||')
774+
assert cppcheckdata.match(arr_decl, 'arr **|=')
747775

748776
@pytest.mark.xfail(strict=False,
749777
reason='in the C++ Token::Match a negation also matches when there is no token, '

0 commit comments

Comments
 (0)