2017-06-04 22:51:48 +02:00
|
|
|
#!/usr/bin/env python
|
2013-01-13 20:03:55 +01:00
|
|
|
#
|
|
|
|
# Cppcheck - A tool for static C/C++ code analysis
|
2022-01-28 18:30:12 +01:00
|
|
|
# Copyright (C) 2007-2021 Cppcheck team.
|
2013-01-13 20:03:55 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import matchcompiler
|
|
|
|
|
2013-03-02 16:45:26 +01:00
|
|
|
|
2013-01-13 20:03:55 +01:00
|
|
|
class MatchCompilerTest(unittest.TestCase):
|
2013-10-18 17:35:59 +02:00
|
|
|
|
2013-01-13 20:03:55 +01:00
|
|
|
def setUp(self):
|
|
|
|
self.mc = matchcompiler.MatchCompiler(verify_mode=False)
|
|
|
|
|
|
|
|
def test_parseMatch(self):
|
2013-10-18 17:35:59 +02:00
|
|
|
self.assertEqual(self.mc.parseMatch(' Token::Match(tok, ";") ', 2), [
|
|
|
|
'Token::Match(tok, ";")', 'tok', ' ";"'])
|
|
|
|
self.assertEqual(self.mc.parseMatch(' Token::Match(tok,', 2), None)
|
2015-01-09 20:20:14 +01:00
|
|
|
# multiline Token::Match is not supported yet
|
2013-10-18 17:35:59 +02:00
|
|
|
self.assertEqual(self.mc.parseMatch(' Token::Match(Token::findsimplematch(tok,")"), ";")', 2), [
|
2017-06-04 22:51:48 +02:00
|
|
|
'Token::Match(Token::findsimplematch(tok,")"), ";")',
|
|
|
|
'Token::findsimplematch(tok,")")', ' ";"']) # inner function call
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
def test_replaceTokenMatch(self):
|
|
|
|
input = 'if (Token::Match(tok, "foobar")) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
|
2013-01-13 20:11:28 +01:00
|
|
|
self.assertEqual(output, 'if (match1(tok)) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
2020-12-31 19:28:06 +01:00
|
|
|
input = 'if (Token::simpleMatch(tok, "foobar")) {'
|
|
|
|
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
|
|
|
|
self.assertEqual(output, 'if (match1(tok)) {')
|
|
|
|
|
2013-01-13 20:03:55 +01:00
|
|
|
input = 'if (Token::Match(tok->next()->next(), "foobar %type% %num%")) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
|
2013-01-13 20:11:28 +01:00
|
|
|
self.assertEqual(output, 'if (match2(tok->next()->next())) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
input = 'if (Token::Match(tok, "foo\"special\"bar %num%")) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
|
2013-10-18 17:35:59 +02:00
|
|
|
self.assertEqual(
|
2015-01-09 22:39:25 +01:00
|
|
|
output, 'if (match3(tok)) {')
|
|
|
|
|
|
|
|
# test that non-static patterns get passed on unmatched
|
|
|
|
input = 'if (Token::Match(tok, "struct " + varname)) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
|
2015-01-09 22:39:25 +01:00
|
|
|
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)) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
|
2015-01-09 22:39:25 +01:00
|
|
|
self.assertEqual(
|
|
|
|
output, 'if (Token::Match(tok, "extern \"C\" " + varname)) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
2020-12-31 19:28:06 +01:00
|
|
|
# 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())) {')
|
|
|
|
|
2013-01-13 20:03:55 +01:00
|
|
|
def test_replaceTokenMatchWithVarId(self):
|
|
|
|
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
|
2013-01-13 20:11:28 +01:00
|
|
|
self.assertEqual(output, 'if (match1(tok, 123)) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
input = 'if (Token::Match(tok->next()->next(), "%varid% foobar", tok->varId())) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
|
2013-10-18 17:35:59 +02:00
|
|
|
self.assertEqual(
|
|
|
|
output, 'if (match2(tok->next()->next(), tok->varId())) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
input = 'if (Token::Match(tok, "foo\"special\"bar %type% %varid%", my_varid_cache)) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
|
2013-10-18 17:35:59 +02:00
|
|
|
self.assertEqual(
|
2015-01-09 22:39:25 +01:00
|
|
|
output, 'if (match3(tok, my_varid_cache)) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
# test caching: reuse existing matchX()
|
|
|
|
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
|
2013-01-13 20:11:28 +01:00
|
|
|
self.assertEqual(output, 'if (match1(tok, 123)) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
# two in one line
|
|
|
|
input = 'if (Token::Match(tok, "foobar2 %varid%", 123) || Token::Match(tok, "%type% %varid%", 123)) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
|
2015-01-09 22:39:25 +01:00
|
|
|
self.assertEqual(output, 'if (match4(tok, 123) || match5(tok, 123)) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
def test_replaceTokenSimpleMatch(self):
|
|
|
|
input = 'if (Token::simpleMatch(tok, "foobar")) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
|
2013-01-13 20:11:28 +01:00
|
|
|
self.assertEqual(output, 'if (match1(tok)) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
input = 'if (Token::simpleMatch(tok->next()->next(), "foobar")) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
|
2013-01-13 20:11:28 +01:00
|
|
|
self.assertEqual(output, 'if (match1(tok->next()->next())) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
input = 'if (Token::simpleMatch(tok, "foo\"special\"bar")) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenMatch(input, 0, "foo.cpp")
|
2013-10-18 17:35:59 +02:00
|
|
|
self.assertEqual(
|
2015-01-09 22:39:25 +01:00
|
|
|
output, 'if (match2(tok)) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
def test_replaceTokenFindSimpleMatch(self):
|
|
|
|
input = 'if (Token::findsimplematch(tok, "foobar")) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenFindMatch(input, 0, "foo.cpp")
|
2015-06-07 20:24:33 +02:00
|
|
|
self.assertEqual(output, 'if (findmatch1(tok) ) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
input = 'if (Token::findsimplematch(tok->next()->next(), "foobar", tok->link())) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenFindMatch(input, 0, "foo.cpp")
|
2013-10-18 17:35:59 +02:00
|
|
|
self.assertEqual(
|
2015-06-07 20:24:33 +02:00
|
|
|
output, 'if (findmatch2(tok->next()->next(), tok->link()) ) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
input = 'if (Token::findsimplematch(tok, "foo\"special\"bar")) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenFindMatch(input, 0, "foo.cpp")
|
2013-10-18 17:35:59 +02:00
|
|
|
self.assertEqual(
|
2015-06-07 20:24:33 +02:00
|
|
|
output, 'if (findmatch3(tok) ) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
def test_replaceTokenFindMatch(self):
|
|
|
|
input = 'if (Token::findmatch(tok, "foobar")) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenFindMatch(input, 0, "foo.cpp")
|
2015-06-07 20:24:33 +02:00
|
|
|
self.assertEqual(output, 'if (findmatch1(tok) ) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
# findmatch with varid
|
|
|
|
input = 'if (Token::findmatch(tok, "foobar %varid%", tok->varId())) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenFindMatch(input, 0, "foo.cpp")
|
2015-06-07 20:24:33 +02:00
|
|
|
self.assertEqual(output, 'if (findmatch2(tok, tok->varId()) ) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
# findmatch with end token
|
|
|
|
input = 'if (Token::findmatch(tok->next()->next(), "foobar %type%", tok->link())) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenFindMatch(input, 0, "foo.cpp")
|
2013-10-18 17:35:59 +02:00
|
|
|
self.assertEqual(
|
2015-06-07 20:24:33 +02:00
|
|
|
output, 'if (findmatch3(tok->next()->next(), tok->link()) ) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
|
|
|
# findmatch with end token and varid
|
|
|
|
input = 'if (Token::findmatch(tok->next()->next(), "foobar %type% %varid%", tok->link(), 123)) {'
|
2016-11-24 00:06:42 +01:00
|
|
|
output = self.mc._replaceTokenFindMatch(input, 0, "foo.cpp")
|
2013-10-18 17:35:59 +02:00
|
|
|
self.assertEqual(
|
2015-06-07 20:24:33 +02:00
|
|
|
output, 'if (findmatch4(tok->next()->next(), tok->link(), 123) ) {')
|
2013-01-13 20:03:55 +01:00
|
|
|
|
2015-01-09 20:20:14 +01:00
|
|
|
def test_parseStringComparison(self):
|
|
|
|
input = 'str == "abc"'
|
2015-08-21 10:55:19 +02:00
|
|
|
# offset '5' is chosen as an abritary start offset to look for
|
|
|
|
res = self.mc._parseStringComparison(input, 5)
|
2015-01-09 20:20:14 +01:00
|
|
|
self.assertEqual(2, len(res))
|
2017-06-04 22:51:48 +02:00
|
|
|
self.assertEqual('str == MatchCompiler::makeConstString("abc")', input[:res[0]] +
|
|
|
|
"MatchCompiler::makeConstString(" + input[res[0]:res[1]] + ")" + input[res[1]:])
|
2015-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
input = 'str == "a\\"b\\"c"'
|
|
|
|
res = self.mc._parseStringComparison(input, 5)
|
|
|
|
self.assertEqual(2, len(res))
|
2017-06-04 22:51:48 +02:00
|
|
|
self.assertEqual('str == MatchCompiler::makeConstString("a\\"b\\"c")', input[:res[0]] +
|
|
|
|
"MatchCompiler::makeConstString(" + input[res[0]:res[1]] + ")" + input[res[1]:])
|
2015-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
def test_replaceCStrings(self):
|
|
|
|
# str() ==
|
|
|
|
input = 'if (tok2->str() == "abc") {'
|
|
|
|
output = self.mc._replaceCStrings(input)
|
2015-06-07 20:24:33 +02:00
|
|
|
self.assertEqual('if (tok2->str() == MatchCompiler::makeConstString("abc")) {', output)
|
2015-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
# str() !=
|
|
|
|
input = 'if (tok2->str() != "xyz") {'
|
|
|
|
output = self.mc._replaceCStrings(input)
|
2015-06-07 20:24:33 +02:00
|
|
|
self.assertEqual('if (tok2->str() != MatchCompiler::makeConstString("xyz")) {', output)
|
2015-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
# strAt()
|
|
|
|
input = 'if (match16(parent->tokAt(-3)) && tok->strAt(1) == ")")'
|
|
|
|
output = self.mc._replaceCStrings(input)
|
2017-06-04 22:51:48 +02:00
|
|
|
self.assertEqual(
|
|
|
|
'if (match16(parent->tokAt(-3)) && tok->strAt(1) == MatchCompiler::makeConstString(")"))',
|
|
|
|
output)
|
2015-01-09 20:20:14 +01:00
|
|
|
|
2013-01-13 20:03:55 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|