Match compiler: Support match patterns with escaped quotes

This allows us to compile patterns like 'extern "C"'.
Fixes long standing open issues in the MC unit test.
This commit is contained in:
Thomas Jarosch 2015-01-09 22:39:25 +01:00
parent d110d715ba
commit 5b4f543c3b
2 changed files with 28 additions and 17 deletions

View File

@ -396,7 +396,7 @@ class MatchCompiler:
if len(res) == 4:
varId = res[3]
res = re.match(r'\s*"([^"]*)"\s*$', raw_pattern)
res = re.match(r'\s*"((?:.|\\")*?)"\s*$', raw_pattern)
if res is None:
break # Non-const pattern - bailout
@ -550,7 +550,7 @@ class MatchCompiler:
elif varId is None and len(res) == 4:
endToken = res[3]
res = re.match(r'\s*"([^"]*)"\s*$', pattern)
res = re.match(r'\s*"((?:.|\\")*?)"\s*$', pattern)
if res is None:
break # Non-const pattern - bailout

View File

@ -47,10 +47,21 @@ class MatchCompilerTest(unittest.TestCase):
input = 'if (Token::Match(tok, "foo\"special\"bar %num%")) {'
output = self.mc._replaceTokenMatch(input)
# FIXME: Currently detected as non-static pattern
self.assertEqual(
output, 'if (Token::Match(tok, "foo"special"bar %num%")) {')
# self.assertEqual(3, len(self.mc._matchStrs))
output, 'if (match3(tok)) {')
self.assertEqual(2, len(self.mc._matchStrs))
# test that non-static patterns get passed on unmatched
input = 'if (Token::Match(tok, "struct " + varname)) {'
output = self.mc._replaceTokenMatch(input)
self.assertEqual(
output, 'if (Token::Match(tok, "struct " + varname)) {')
# test that non-static patterns get passed on unmatched
input = 'if (Token::Match(tok, "extern \"C\" " + varname)) {'
output = self.mc._replaceTokenMatch(input)
self.assertEqual(
output, 'if (Token::Match(tok, "extern \"C\" " + varname)) {')
def test_replaceTokenMatchWithVarId(self):
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
@ -66,22 +77,22 @@ class MatchCompilerTest(unittest.TestCase):
input = 'if (Token::Match(tok, "foo\"special\"bar %type% %varid%", my_varid_cache)) {'
output = self.mc._replaceTokenMatch(input)
# FIXME: Currently detected as non-static pattern
self.assertEqual(
output, 'if (Token::Match(tok, "foo"special"bar %type% %varid%", my_varid_cache)) {')
# self.assertEqual(1, len(self.mc._matchStrs))
output, 'if (match3(tok, my_varid_cache)) {')
self.assertEqual(2, len(self.mc._matchStrs))
self.assertEqual(2, self.mc._matchStrs['foo"special"bar'])
# test caching: reuse existing matchX()
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
output = self.mc._replaceTokenMatch(input)
self.assertEqual(output, 'if (match1(tok, 123)) {')
self.assertEqual(1, len(self.mc._matchStrs))
self.assertEqual(2, len(self.mc._matchStrs))
# two in one line
input = 'if (Token::Match(tok, "foobar2 %varid%", 123) || Token::Match(tok, "%type% %varid%", 123)) {'
output = self.mc._replaceTokenMatch(input)
self.assertEqual(output, 'if (match3(tok, 123) || match4(tok, 123)) {')
self.assertEqual(2, len(self.mc._matchStrs))
self.assertEqual(output, 'if (match4(tok, 123) || match5(tok, 123)) {')
self.assertEqual(3, len(self.mc._matchStrs))
def test_replaceTokenSimpleMatch(self):
input = 'if (Token::simpleMatch(tok, "foobar")) {'
@ -98,10 +109,10 @@ class MatchCompilerTest(unittest.TestCase):
input = 'if (Token::simpleMatch(tok, "foo\"special\"bar")) {'
output = self.mc._replaceTokenMatch(input)
# FIXME: Currently detected as non-static pattern
self.assertEqual(
output, 'if (Token::simpleMatch(tok, "foo\"special\"bar")) {')
self.assertEqual(1, len(self.mc._matchStrs))
output, 'if (match2(tok)) {')
self.assertEqual(2, len(self.mc._matchStrs))
self.assertEqual(2, self.mc._matchStrs['foo\"special\"bar'])
def test_replaceTokenFindSimpleMatch(self):
input = 'if (Token::findsimplematch(tok, "foobar")) {'
@ -119,10 +130,10 @@ class MatchCompilerTest(unittest.TestCase):
input = 'if (Token::findsimplematch(tok, "foo\"special\"bar")) {'
output = self.mc._replaceTokenFindMatch(input)
# FIXME: Currently detected as non-static pattern
self.assertEqual(
output, 'if (Token::findsimplematch(tok, "foo\"special\"bar")) {')
self.assertEqual(1, len(self.mc._matchStrs))
output, 'if (findmatch3(tok)) {')
self.assertEqual(2, len(self.mc._matchStrs))
self.assertEqual(2, self.mc._matchStrs['foo\"special\"bar'])
def test_replaceTokenFindMatch(self):
input = 'if (Token::findmatch(tok, "foobar")) {'