2012-12-02 12:36:55 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import re
|
|
|
|
import glob
|
|
|
|
|
|
|
|
def compileCmd(tok):
|
|
|
|
if tok == '%any%':
|
|
|
|
return 'true'
|
|
|
|
elif tok == '%bool%':
|
|
|
|
return 'tok->isBoolean()'
|
|
|
|
elif tok == '%char%':
|
|
|
|
return '(tok->type()==Token::eChar)'
|
|
|
|
elif tok == '%comp%':
|
|
|
|
return 'tok->isComparisonOp()'
|
|
|
|
elif tok == '%num%':
|
|
|
|
return 'tok->isNumber()'
|
|
|
|
elif tok == '%op%':
|
|
|
|
return 'tok->isOp()'
|
|
|
|
elif tok == '%or%':
|
|
|
|
return '(tok->str()=="|")'
|
|
|
|
elif tok == '%oror%':
|
|
|
|
return '(tok->str()=="||")'
|
|
|
|
elif tok == '%num%':
|
|
|
|
return 'tok->isNumber()'
|
|
|
|
elif tok == '%str%':
|
|
|
|
return '(tok->type()==Token::eString)'
|
|
|
|
elif tok == '%type%':
|
|
|
|
return '(tok->isName() && tok->varId()==0U && tok->str() != "delete")'
|
|
|
|
elif tok == '%var%':
|
|
|
|
return 'tok->isName()'
|
2012-12-14 16:59:37 +01:00
|
|
|
elif tok == '%varid%':
|
|
|
|
return '(tok->isName() && tok->varId()==varid)'
|
2012-12-02 12:36:55 +01:00
|
|
|
elif (len(tok)>2) and (tok[0]=="%"):
|
|
|
|
print "unhandled:" + tok
|
|
|
|
return '(tok->str()=="'+tok+'")'
|
|
|
|
|
2012-12-14 16:59:37 +01:00
|
|
|
def compilePattern(pattern, nr, varid):
|
|
|
|
arg2 = ''
|
|
|
|
if varid:
|
|
|
|
arg2 = ', const unsigned int varid'
|
2012-12-02 12:36:55 +01:00
|
|
|
ret = '// ' + pattern + '\n'
|
2012-12-14 16:59:37 +01:00
|
|
|
ret = ret + 'static bool match' + str(nr) + '(const Token *tok'+arg2+') {\n'
|
2012-12-19 06:45:05 +01:00
|
|
|
if varid:
|
|
|
|
# if varid is provided, check that it's non-zero
|
|
|
|
ret = ret + ' if (varid==0U)\n'
|
|
|
|
ret = ret + ' throw InternalError(tok, "Internal error. Token::Match called with varid 0. Please report this to Cppcheck developers");\n'
|
|
|
|
|
2012-12-02 12:36:55 +01:00
|
|
|
tokens = pattern.split(' ')
|
|
|
|
gotoNextToken = ''
|
|
|
|
for tok in tokens:
|
|
|
|
if tok == '':
|
|
|
|
continue
|
|
|
|
ret = ret + gotoNextToken
|
|
|
|
gotoNextToken = ' tok = tok->next();\n'
|
|
|
|
|
|
|
|
# [abc]
|
|
|
|
if (len(tok) > 2) and (tok[0] == '[') and (tok[-1] == ']'):
|
|
|
|
ret = ret + ' if (!tok || tok->str().size()!=1U || !strchr("'+tok[1:-1]+'", tok->str()[0]))\n'
|
|
|
|
ret = ret + ' return false;\n'
|
|
|
|
|
|
|
|
# a|b|c
|
|
|
|
elif tok.find('|') > 0:
|
|
|
|
tokens2 = tok.split('|')
|
|
|
|
logicalOp = None
|
|
|
|
neg = None
|
|
|
|
if "" in tokens2:
|
|
|
|
ret = ret + ' if (tok && ('
|
|
|
|
logicalOp = ' || '
|
|
|
|
neg = ''
|
|
|
|
else:
|
|
|
|
ret = ret + ' if (!tok || !('
|
|
|
|
logicalOp = ' || '
|
|
|
|
neg = ''
|
|
|
|
first = True
|
|
|
|
for tok2 in tokens2:
|
|
|
|
if tok2 == '':
|
|
|
|
continue
|
|
|
|
if not first:
|
|
|
|
ret = ret + logicalOp
|
|
|
|
first = False
|
|
|
|
ret = ret + neg + compileCmd(tok2)
|
|
|
|
|
|
|
|
if "" in tokens2:
|
|
|
|
ret = ret + '))\n'
|
|
|
|
ret = ret + ' tok = tok->next();\n'
|
|
|
|
gotoNextToken = ''
|
|
|
|
else:
|
|
|
|
ret = ret + '))\n'
|
|
|
|
ret = ret + ' return false;\n'
|
|
|
|
|
|
|
|
# !!a
|
|
|
|
elif tok[0:2]=="!!":
|
|
|
|
ret = ret + ' if (tok && tok->str() == "' + tok[2:] + '")\n'
|
|
|
|
ret = ret + ' return false;\n'
|
|
|
|
gotoNextToken = ' tok = tok ? tok->next() : NULL;\n'
|
|
|
|
|
|
|
|
else:
|
|
|
|
ret = ret + ' if (!tok || !' + compileCmd(tok) + ')\n'
|
|
|
|
ret = ret + ' return false;\n'
|
|
|
|
ret = ret + ' return true;\n}\n'
|
|
|
|
return ret
|
|
|
|
|
2012-12-10 20:28:11 +01:00
|
|
|
def parseMatch(line, pos1):
|
2012-12-08 19:29:16 +01:00
|
|
|
parlevel = 0
|
|
|
|
args = []
|
|
|
|
argstart = 0
|
|
|
|
pos = pos1
|
|
|
|
inString = False
|
|
|
|
while pos < len(line):
|
|
|
|
if inString:
|
|
|
|
if line[pos] == '\\':
|
|
|
|
pos = pos + 1
|
|
|
|
elif line[pos] == '"':
|
|
|
|
inString = False
|
|
|
|
elif line[pos] == '"':
|
|
|
|
inString = True
|
|
|
|
elif line[pos] == '(':
|
|
|
|
parlevel = parlevel + 1
|
|
|
|
if parlevel == 1:
|
|
|
|
argstart = pos + 1
|
|
|
|
elif line[pos] == ')':
|
|
|
|
parlevel = parlevel - 1
|
|
|
|
if parlevel == 0:
|
|
|
|
ret = []
|
|
|
|
ret.append(line[pos1:pos+1])
|
|
|
|
for arg in args:
|
|
|
|
ret.append(arg)
|
|
|
|
ret.append(line[argstart:pos])
|
|
|
|
return ret
|
|
|
|
elif line[pos] == ',' and parlevel == 1:
|
|
|
|
args.append(line[argstart:pos])
|
|
|
|
argstart = pos + 1
|
|
|
|
pos = pos + 1
|
|
|
|
|
|
|
|
return None
|
2012-12-02 13:39:59 +01:00
|
|
|
|
2012-12-02 12:36:55 +01:00
|
|
|
def convertFile(srcname, destname):
|
|
|
|
fin = open(srcname, "rt")
|
|
|
|
srclines = fin.readlines()
|
|
|
|
fin.close()
|
|
|
|
|
|
|
|
matchfunctions = ''
|
|
|
|
matchfunctions = matchfunctions + '#include "token.h"\n'
|
2012-12-19 06:45:05 +01:00
|
|
|
matchfunctions = matchfunctions + '#include "errorlogger.h"\n'
|
2012-12-02 12:36:55 +01:00
|
|
|
matchfunctions = matchfunctions + '#include <string>\n'
|
|
|
|
matchfunctions = matchfunctions + '#include <cstring>\n'
|
|
|
|
code = ''
|
|
|
|
|
|
|
|
patternNumber = 1
|
|
|
|
for line in srclines:
|
2012-12-10 20:28:11 +01:00
|
|
|
while True:
|
|
|
|
pos1 = line.find('Token::Match(')
|
|
|
|
if pos1 == -1:
|
|
|
|
pos1 = line.find('Token::simpleMatch(')
|
|
|
|
if pos1 == -1:
|
|
|
|
break
|
|
|
|
|
|
|
|
res = parseMatch(line, pos1)
|
2012-12-08 19:29:16 +01:00
|
|
|
if res == None:
|
2012-12-10 20:28:11 +01:00
|
|
|
break
|
2012-12-08 19:29:16 +01:00
|
|
|
else:
|
2012-12-14 16:59:37 +01:00
|
|
|
assert(len(res)==3 or len(res)==4) # assert that Token::Match has either 2 or 3 arguments
|
|
|
|
|
2012-12-10 20:28:11 +01:00
|
|
|
g0 = res[0]
|
|
|
|
arg1 = res[1]
|
|
|
|
arg2 = res[2]
|
2012-12-14 16:59:37 +01:00
|
|
|
arg3 = None
|
|
|
|
if len(res) == 4:
|
|
|
|
arg3 = res[3]
|
2012-12-10 20:28:11 +01:00
|
|
|
|
2012-12-14 16:59:37 +01:00
|
|
|
res = re.match(r'\s*"([^"]*)"\s*$', arg2)
|
2012-12-10 20:28:11 +01:00
|
|
|
if res == None:
|
|
|
|
break # Non-const pattern - bailout
|
|
|
|
else:
|
|
|
|
arg2 = res.group(1)
|
2012-12-14 16:59:37 +01:00
|
|
|
a3 = ''
|
|
|
|
if arg3:
|
|
|
|
a3 = ',' + arg3
|
|
|
|
line = line[:pos1]+'match'+str(patternNumber)+'('+arg1+a3+')'+line[pos1+len(g0):]
|
|
|
|
matchfunctions = matchfunctions + compilePattern(arg2, patternNumber, arg3)
|
2012-12-10 20:28:11 +01:00
|
|
|
patternNumber = patternNumber + 1
|
|
|
|
|
|
|
|
code = code + line
|
2012-12-02 12:36:55 +01:00
|
|
|
|
|
|
|
fout = open(destname, 'wt')
|
|
|
|
fout.write(matchfunctions+code)
|
|
|
|
fout.close()
|
|
|
|
|
2012-12-04 22:57:52 +01:00
|
|
|
# selftests..
|
2012-12-10 20:28:11 +01:00
|
|
|
def assertEquals(actual,expected):
|
|
|
|
if actual!=expected:
|
|
|
|
print 'Assertion failed:'
|
|
|
|
print actual
|
|
|
|
print expected
|
|
|
|
assert actual == expected
|
|
|
|
assertEquals(parseMatch(' Token::Match(tok, ";") ',2), ['Token::Match(tok, ";")','tok',' ";"'])
|
|
|
|
assertEquals(parseMatch(' Token::Match(tok,', 2), None) # multiline Token::Match is not supported yet
|
|
|
|
assertEquals(parseMatch(' Token::Match(Token::findsimplematch(tok,")"), ";")', 2), ['Token::Match(Token::findsimplematch(tok,")"), ";")', 'Token::findsimplematch(tok,")")', ' ";"']) # inner function call
|
2012-12-02 12:36:55 +01:00
|
|
|
|
2012-12-04 22:57:52 +01:00
|
|
|
# convert all lib/*.cpp files
|
2012-12-02 12:36:55 +01:00
|
|
|
for f in glob.glob('lib/*.cpp'):
|
|
|
|
print f + ' => build/' + f[4:]
|
|
|
|
convertFile(f, 'build/'+f[4:])
|
|
|
|
|