Fixed #6716 (Performance: Alternative String Representation in Match Compiler)
Quickfix of tools/test_matchcompiler.py for Alternative String Representation _matchStrs does not exist in Match Compiler anymore.
This commit is contained in:
parent
0e354a03bd
commit
6ba2534f32
|
@ -37,19 +37,15 @@ class MatchCompilerTest(unittest.TestCase):
|
||||||
input = 'if (Token::Match(tok, "foobar")) {'
|
input = 'if (Token::Match(tok, "foobar")) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self.assertEqual(output, 'if (match1(tok)) {')
|
self.assertEqual(output, 'if (match1(tok)) {')
|
||||||
self.assertEqual(1, len(self.mc._matchStrs))
|
|
||||||
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
|
||||||
|
|
||||||
input = 'if (Token::Match(tok->next()->next(), "foobar %type% %num%")) {'
|
input = 'if (Token::Match(tok->next()->next(), "foobar %type% %num%")) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self.assertEqual(output, 'if (match2(tok->next()->next())) {')
|
self.assertEqual(output, 'if (match2(tok->next()->next())) {')
|
||||||
self.assertEqual(1, len(self.mc._matchStrs))
|
|
||||||
|
|
||||||
input = 'if (Token::Match(tok, "foo\"special\"bar %num%")) {'
|
input = 'if (Token::Match(tok, "foo\"special\"bar %num%")) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
output, 'if (match3(tok)) {')
|
output, 'if (match3(tok)) {')
|
||||||
self.assertEqual(2, len(self.mc._matchStrs))
|
|
||||||
|
|
||||||
# test that non-static patterns get passed on unmatched
|
# test that non-static patterns get passed on unmatched
|
||||||
input = 'if (Token::Match(tok, "struct " + varname)) {'
|
input = 'if (Token::Match(tok, "struct " + varname)) {'
|
||||||
|
@ -67,134 +63,104 @@ class MatchCompilerTest(unittest.TestCase):
|
||||||
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
|
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self.assertEqual(output, 'if (match1(tok, 123)) {')
|
self.assertEqual(output, 'if (match1(tok, 123)) {')
|
||||||
self.assertEqual(1, len(self.mc._matchStrs))
|
|
||||||
|
|
||||||
input = 'if (Token::Match(tok->next()->next(), "%varid% foobar", tok->varId())) {'
|
input = 'if (Token::Match(tok->next()->next(), "%varid% foobar", tok->varId())) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
output, 'if (match2(tok->next()->next(), tok->varId())) {')
|
output, 'if (match2(tok->next()->next(), tok->varId())) {')
|
||||||
self.assertEqual(1, len(self.mc._matchStrs))
|
|
||||||
|
|
||||||
input = 'if (Token::Match(tok, "foo\"special\"bar %type% %varid%", my_varid_cache)) {'
|
input = 'if (Token::Match(tok, "foo\"special\"bar %type% %varid%", my_varid_cache)) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
output, 'if (match3(tok, my_varid_cache)) {')
|
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()
|
# test caching: reuse existing matchX()
|
||||||
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
|
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self.assertEqual(output, 'if (match1(tok, 123)) {')
|
self.assertEqual(output, 'if (match1(tok, 123)) {')
|
||||||
self.assertEqual(2, len(self.mc._matchStrs))
|
|
||||||
|
|
||||||
# two in one line
|
# two in one line
|
||||||
input = 'if (Token::Match(tok, "foobar2 %varid%", 123) || Token::Match(tok, "%type% %varid%", 123)) {'
|
input = 'if (Token::Match(tok, "foobar2 %varid%", 123) || Token::Match(tok, "%type% %varid%", 123)) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self.assertEqual(output, 'if (match4(tok, 123) || match5(tok, 123)) {')
|
self.assertEqual(output, 'if (match4(tok, 123) || match5(tok, 123)) {')
|
||||||
self.assertEqual(3, len(self.mc._matchStrs))
|
|
||||||
|
|
||||||
def test_replaceTokenSimpleMatch(self):
|
def test_replaceTokenSimpleMatch(self):
|
||||||
input = 'if (Token::simpleMatch(tok, "foobar")) {'
|
input = 'if (Token::simpleMatch(tok, "foobar")) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self.assertEqual(output, 'if (match1(tok)) {')
|
self.assertEqual(output, 'if (match1(tok)) {')
|
||||||
self.assertEqual(1, len(self.mc._matchStrs))
|
|
||||||
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
|
||||||
|
|
||||||
input = 'if (Token::simpleMatch(tok->next()->next(), "foobar")) {'
|
input = 'if (Token::simpleMatch(tok->next()->next(), "foobar")) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self.assertEqual(output, 'if (match1(tok->next()->next())) {')
|
self.assertEqual(output, 'if (match1(tok->next()->next())) {')
|
||||||
self.assertEqual(1, len(self.mc._matchStrs))
|
|
||||||
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
|
||||||
|
|
||||||
input = 'if (Token::simpleMatch(tok, "foo\"special\"bar")) {'
|
input = 'if (Token::simpleMatch(tok, "foo\"special\"bar")) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
output, 'if (match2(tok)) {')
|
output, 'if (match2(tok)) {')
|
||||||
self.assertEqual(2, len(self.mc._matchStrs))
|
|
||||||
self.assertEqual(2, self.mc._matchStrs['foo\"special\"bar'])
|
|
||||||
|
|
||||||
def test_replaceTokenFindSimpleMatch(self):
|
def test_replaceTokenFindSimpleMatch(self):
|
||||||
input = 'if (Token::findsimplematch(tok, "foobar")) {'
|
input = 'if (Token::findsimplematch(tok, "foobar")) {'
|
||||||
output = self.mc._replaceTokenFindMatch(input)
|
output = self.mc._replaceTokenFindMatch(input)
|
||||||
self.assertEqual(output, 'if (findmatch1(tok)) {')
|
self.assertEqual(output, 'if (findmatch1(tok) ) {')
|
||||||
self.assertEqual(1, len(self.mc._matchStrs))
|
|
||||||
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
|
||||||
|
|
||||||
input = 'if (Token::findsimplematch(tok->next()->next(), "foobar", tok->link())) {'
|
input = 'if (Token::findsimplematch(tok->next()->next(), "foobar", tok->link())) {'
|
||||||
output = self.mc._replaceTokenFindMatch(input)
|
output = self.mc._replaceTokenFindMatch(input)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
output, 'if (findmatch2(tok->next()->next(), tok->link())) {')
|
output, 'if (findmatch2(tok->next()->next(), tok->link()) ) {')
|
||||||
self.assertEqual(1, len(self.mc._matchStrs))
|
|
||||||
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
|
||||||
|
|
||||||
input = 'if (Token::findsimplematch(tok, "foo\"special\"bar")) {'
|
input = 'if (Token::findsimplematch(tok, "foo\"special\"bar")) {'
|
||||||
output = self.mc._replaceTokenFindMatch(input)
|
output = self.mc._replaceTokenFindMatch(input)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
output, 'if (findmatch3(tok)) {')
|
output, 'if (findmatch3(tok) ) {')
|
||||||
self.assertEqual(2, len(self.mc._matchStrs))
|
|
||||||
self.assertEqual(2, self.mc._matchStrs['foo\"special\"bar'])
|
|
||||||
|
|
||||||
def test_replaceTokenFindMatch(self):
|
def test_replaceTokenFindMatch(self):
|
||||||
input = 'if (Token::findmatch(tok, "foobar")) {'
|
input = 'if (Token::findmatch(tok, "foobar")) {'
|
||||||
output = self.mc._replaceTokenFindMatch(input)
|
output = self.mc._replaceTokenFindMatch(input)
|
||||||
self.assertEqual(output, 'if (findmatch1(tok)) {')
|
self.assertEqual(output, 'if (findmatch1(tok) ) {')
|
||||||
self.assertEqual(1, len(self.mc._matchStrs))
|
|
||||||
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
|
||||||
|
|
||||||
# findmatch with varid
|
# findmatch with varid
|
||||||
input = 'if (Token::findmatch(tok, "foobar %varid%", tok->varId())) {'
|
input = 'if (Token::findmatch(tok, "foobar %varid%", tok->varId())) {'
|
||||||
output = self.mc._replaceTokenFindMatch(input)
|
output = self.mc._replaceTokenFindMatch(input)
|
||||||
self.assertEqual(output, 'if (findmatch2(tok, tok->varId())) {')
|
self.assertEqual(output, 'if (findmatch2(tok, tok->varId()) ) {')
|
||||||
self.assertEqual(1, len(self.mc._matchStrs))
|
|
||||||
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
|
||||||
|
|
||||||
# findmatch with end token
|
# findmatch with end token
|
||||||
input = 'if (Token::findmatch(tok->next()->next(), "foobar %type%", tok->link())) {'
|
input = 'if (Token::findmatch(tok->next()->next(), "foobar %type%", tok->link())) {'
|
||||||
output = self.mc._replaceTokenFindMatch(input)
|
output = self.mc._replaceTokenFindMatch(input)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
output, 'if (findmatch3(tok->next()->next(), tok->link())) {')
|
output, 'if (findmatch3(tok->next()->next(), tok->link()) ) {')
|
||||||
self.assertEqual(1, len(self.mc._matchStrs))
|
|
||||||
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
|
||||||
|
|
||||||
# findmatch with end token and varid
|
# findmatch with end token and varid
|
||||||
input = 'if (Token::findmatch(tok->next()->next(), "foobar %type% %varid%", tok->link(), 123)) {'
|
input = 'if (Token::findmatch(tok->next()->next(), "foobar %type% %varid%", tok->link(), 123)) {'
|
||||||
output = self.mc._replaceTokenFindMatch(input)
|
output = self.mc._replaceTokenFindMatch(input)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
output, 'if (findmatch4(tok->next()->next(), tok->link(), 123)) {')
|
output, 'if (findmatch4(tok->next()->next(), tok->link(), 123) ) {')
|
||||||
self.assertEqual(1, len(self.mc._matchStrs))
|
|
||||||
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
|
||||||
|
|
||||||
def test_parseStringComparison(self):
|
def test_parseStringComparison(self):
|
||||||
input = 'str == "abc"'
|
input = 'str == "abc"'
|
||||||
res = self.mc._parseStringComparison(input, 5) # offset '5' is chosen as an abritary start offset to look for "
|
res = self.mc._parseStringComparison(input, 5) # offset '5' is chosen as an abritary start offset to look for "
|
||||||
self.assertEqual(2, len(res))
|
self.assertEqual(2, len(res))
|
||||||
self.assertEqual(7, res[0])
|
self.assertEqual('str == MatchCompiler::makeConstString("abc")', input[:res[0]] + "MatchCompiler::makeConstString(" + input[res[0]:res[1]] + ")" + input[res[1]:])
|
||||||
self.assertEqual(12, res[1])
|
|
||||||
self.assertEqual('str == matchStr', input[:res[0]] + "matchStr" + input[res[1]:])
|
|
||||||
|
|
||||||
input = 'str == "a\\"b\\"c"'
|
input = 'str == "a\\"b\\"c"'
|
||||||
res = self.mc._parseStringComparison(input, 5)
|
res = self.mc._parseStringComparison(input, 5)
|
||||||
self.assertEqual(2, len(res))
|
self.assertEqual(2, len(res))
|
||||||
self.assertEqual(7, res[0])
|
self.assertEqual('str == MatchCompiler::makeConstString("a\\"b\\"c")', input[:res[0]] + "MatchCompiler::makeConstString(" + input[res[0]:res[1]] + ")" + input[res[1]:])
|
||||||
self.assertEqual(16, res[1])
|
|
||||||
self.assertEqual('str == matchStr', input[:res[0]] + "matchStr" + input[res[1]:])
|
|
||||||
|
|
||||||
def test_replaceCStrings(self):
|
def test_replaceCStrings(self):
|
||||||
# str() ==
|
# str() ==
|
||||||
input = 'if (tok2->str() == "abc") {'
|
input = 'if (tok2->str() == "abc") {'
|
||||||
output = self.mc._replaceCStrings(input)
|
output = self.mc._replaceCStrings(input)
|
||||||
self.assertEqual("if (tok2->str() == matchStr1) {", output)
|
self.assertEqual('if (tok2->str() == MatchCompiler::makeConstString("abc")) {', output)
|
||||||
|
|
||||||
# str() !=
|
# str() !=
|
||||||
input = 'if (tok2->str() != "xyz") {'
|
input = 'if (tok2->str() != "xyz") {'
|
||||||
output = self.mc._replaceCStrings(input)
|
output = self.mc._replaceCStrings(input)
|
||||||
self.assertEqual("if (tok2->str() != matchStr2) {", output)
|
self.assertEqual('if (tok2->str() != MatchCompiler::makeConstString("xyz")) {', output)
|
||||||
|
|
||||||
# strAt()
|
# strAt()
|
||||||
input = 'if (match16(parent->tokAt(-3)) && tok->strAt(1) == ")")'
|
input = 'if (match16(parent->tokAt(-3)) && tok->strAt(1) == ")")'
|
||||||
output = self.mc._replaceCStrings(input)
|
output = self.mc._replaceCStrings(input)
|
||||||
self.assertEqual('if (match16(parent->tokAt(-3)) && tok->strAt(1) == matchStr3)', output)
|
self.assertEqual('if (match16(parent->tokAt(-3)) && tok->strAt(1) == MatchCompiler::makeConstString(")"))', output)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue