Matchcompiler parse all Match|simpleMatch on the same line (#2993)

* MatchCompiler: Neaten error messages

Especially the added space makes it a little more readable.

* MatchCompiler: Add spaces between operators

* Matchcompiler: Don't bailout if non-const pattern

If matchcompiler found a call to Token::Match() or Token::simpleMatch()
with an unknown string argument, subsequent calls to Token::Match() or
Token::simpleMatch() on the same line would not be processed by
matchcompiler.

To fix this, keep track of the last index we found a match, and update
it accordingly when the line is modified. To avoid having to keep track
of if "Match" or "simpleMatch" is the first match we find, just make a
loop over them.
This commit is contained in:
Rikard Falkeborn 2020-12-31 19:28:06 +01:00 committed by GitHub
parent e469436fe1
commit 8dc8aa0459
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 44 deletions

View File

@ -331,7 +331,7 @@ class MatchCompiler:
# ret += ' if (tok->next())\n'
# ret += ' std::cout << "tok next: " << tok->next()->str();\n'
ret += ' throw InternalError(tok, "Internal error. ' +\
'compiled match returned different result than parsed match: ' + pattern + '");\n'
'Compiled match returned different result than parsed match: ' + pattern + '");\n'
ret += ' }\n'
ret += ' return res_compiled_match;\n'
ret += '}\n'
@ -380,12 +380,11 @@ class MatchCompiler:
)
def _replaceTokenMatch(self, line, linenr, filename):
for func in ('Match', 'simpleMatch'):
is_simplematch = func == 'simpleMatch'
pattern_start = 0
while True:
is_simplematch = False
pos1 = line.find('Token::Match(')
if pos1 == -1:
is_simplematch = True
pos1 = line.find('Token::simpleMatch(')
pos1 = line.find('Token::' + func + '(', pattern_start)
if pos1 == -1:
break
@ -403,13 +402,15 @@ class MatchCompiler:
if len(res) == 4:
varId = res[3]
pattern_start = pos1 + end_pos
res = re.match(r'\s*"((?:.|\\")*?)"\s*$', raw_pattern)
if res is None:
if self._showSkipped:
print(filename + ":" + str(linenr) + " skipping match pattern:" + raw_pattern)
break # Non-const pattern - bailout
continue # Non-const pattern - bailout
pattern = res.group(1)
orig_len = len(line)
line = self._replaceSpecificTokenMatch(
is_simplematch,
line,
@ -418,6 +419,7 @@ class MatchCompiler:
pattern,
tok,
varId)
pattern_start += len(line) - orig_len
return line
@ -459,7 +461,7 @@ class MatchCompiler:
# We also need to verify builds in 'release' mode
ret += ' if (res_parsed_findmatch != res_compiled_findmatch) {\n'
ret += ' throw InternalError(tok, "Internal error. ' +\
'compiled findmatch returned different result than parsed findmatch: ' + pattern + '");\n'
'Compiled findmatch returned different result than parsed findmatch: ' + pattern + '");\n'
ret += ' }\n'
ret += ' return res_compiled_findmatch;\n'
ret += '}\n'

View File

@ -39,6 +39,10 @@ class MatchCompilerTest(unittest.TestCase):
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
self.assertEqual(output, 'if (match1(tok)) {')
input = 'if (Token::simpleMatch(tok, "foobar")) {'
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
self.assertEqual(output, 'if (match1(tok)) {')
input = 'if (Token::Match(tok->next()->next(), "foobar %type% %num%")) {'
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
self.assertEqual(output, 'if (match2(tok->next()->next())) {')
@ -60,6 +64,25 @@ class MatchCompilerTest(unittest.TestCase):
self.assertEqual(
output, 'if (Token::Match(tok, "extern \"C\" " + varname)) {')
# test that multiple patterns on the same line are replaced
input = 'if (Token::Match(tok, "foo") && Token::Match(tok->next(), "baz")) {'
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
self.assertEqual(output, 'if (match4(tok) && match5(tok->next())) {')
# test that second pattern is replaced, even if there is a bailout on the first pattern
input = 'if (Token::Match(tok, foo) && Token::Match(tok->next(), "foobaz")) {'
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
self.assertEqual(output, 'if (Token::Match(tok, foo) && match6(tok->next())) {')
# test mixing Match and simpleMatch on the same line
input = 'if (Token::Match(tok, "a") && Token::simpleMatch(tok->next(), "b")) {'
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
self.assertEqual(output, 'if (match7(tok) && match8(tok->next())) {')
input = 'if (Token::simpleMatch(tok, "a") && Token::Match(tok->next(), "b")) {'
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
self.assertEqual(output, 'if (match7(tok) && match8(tok->next())) {')
def test_replaceTokenMatchWithVarId(self):
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")