From 5b4f543c3b80bc86d051522b27d09cf8438e65b9 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Fri, 9 Jan 2015 22:39:25 +0100 Subject: [PATCH] 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. --- tools/matchcompiler.py | 4 ++-- tools/test_matchcompiler.py | 41 +++++++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/tools/matchcompiler.py b/tools/matchcompiler.py index 725df4916..c7a508885 100755 --- a/tools/matchcompiler.py +++ b/tools/matchcompiler.py @@ -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 diff --git a/tools/test_matchcompiler.py b/tools/test_matchcompiler.py index 8f1d552ee..f15744459 100755 --- a/tools/test_matchcompiler.py +++ b/tools/test_matchcompiler.py @@ -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")) {'