cppcheck/tools/matchcompiler.py

798 lines
26 KiB
Python
Raw Normal View History

2017-06-04 22:51:48 +02:00
#!/usr/bin/env python
2013-01-10 11:10:45 +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-10 11:10:45 +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 io
import os
import sys
import re
import glob
2013-01-09 19:41:03 +01:00
import argparse
import errno
tokTypes = {
'+': ['eArithmeticalOp'],
'-': ['eArithmeticalOp'],
'*': ['eArithmeticalOp'],
'/': ['eArithmeticalOp'],
'%': ['eArithmeticalOp'],
'>>': ['eArithmeticalOp'],
'<<': ['eArithmeticalOp'],
'=': ['eAssignmentOp'],
'+=': ['eAssignmentOp'],
'-=': ['eAssignmentOp'],
'*=': ['eAssignmentOp'],
'/=': ['eAssignmentOp'],
'%=': ['eAssignmentOp'],
'&=': ['eAssignmentOp'],
'|=': ['eAssignmentOp'],
'^=': ['eAssignmentOp'],
'&': ['eBitOp'],
'^': ['eBitOp'],
'~': ['eBitOp'],
'true': ['eBoolean'],
'false': ['eBoolean'],
'{': ['eBracket'],
'}': ['eBracket'],
'<': ['eBracket', 'eComparisonOp'],
'>': ['eBracket', 'eComparisonOp'],
'==': ['eComparisonOp'],
'!=': ['eComparisonOp'],
'<=': ['eComparisonOp'],
'>=': ['eComparisonOp'],
'<=>': ['eComparisonOp'],
'...': ['eEllipsis'],
',': ['eExtendedOp'],
'?': ['eExtendedOp'],
':': ['eExtendedOp'],
'(': ['eExtendedOp'],
')': ['eExtendedOp'],
'[': ['eExtendedOp', 'eLambda'],
']': ['eExtendedOp', 'eLambda'],
'++': ['eIncDecOp'],
'--': ['eIncDecOp'],
'asm': ['eKeyword'],
'auto': ['eKeyword', 'eType'],
'break': ['eKeyword'],
'case': ['eKeyword'],
'const': ['eKeyword'],
'continue': ['eKeyword'],
'default': ['eKeyword'],
'do': ['eKeyword'],
'else': ['eKeyword'],
'enum': ['eKeyword'],
'extern': ['eKeyword'],
'for': ['eKeyword'],
'goto': ['eKeyword'],
'if': ['eKeyword'],
'inline': ['eKeyword'],
'register': ['eKeyword'],
'restrict': ['eKeyword'],
'return': ['eKeyword'],
'sizeof': ['eKeyword'],
'static': ['eKeyword'],
'struct': ['eKeyword'],
'switch': ['eKeyword'],
'typedef': ['eKeyword'],
'union': ['eKeyword'],
'volatile': ['eKeyword'],
'while': ['eKeyword'],
'void': ['eKeyword', 'eType'],
'&&': ['eLogicalOp'],
'!': ['eLogicalOp']
}
2013-03-02 16:45:26 +01:00
class MatchCompiler:
2013-10-13 11:01:50 +02:00
def __init__(self, verify_mode=False, show_skipped=False):
self._verifyMode = verify_mode
self._showSkipped = show_skipped
self._reset()
def _reset(self):
self._rawMatchFunctions = []
self._matchFunctionCache = {}
2017-07-22 11:05:50 +02:00
@staticmethod
2013-10-13 11:01:50 +02:00
def _generateCacheSignature(
2017-07-22 11:05:50 +02:00
pattern, endToken=None, varId=None, isFindMatch=False):
sig = pattern
if endToken:
sig += '|ENDTOKEN'
else:
sig += '|NO-ENDTOKEN'
if varId:
sig += '|VARID'
else:
sig += '|NO-VARID'
if isFindMatch:
sig += '|ISFINDMATCH'
else:
sig += '|NORMALMATCH'
return sig
2013-10-13 11:01:50 +02:00
def _lookupMatchFunctionId(
self, pattern, endToken=None, varId=None, isFindMatch=False):
signature = self._generateCacheSignature(
pattern, endToken, varId, isFindMatch)
if signature in self._matchFunctionCache:
return self._matchFunctionCache[signature]
return None
2013-10-13 11:01:50 +02:00
def _insertMatchFunctionId(
self, id, pattern, endToken=None, varId=None, isFindMatch=False):
signature = self._generateCacheSignature(
pattern, endToken, varId, isFindMatch)
# function signature should not be in the cache
2013-10-13 11:01:50 +02:00
assert(
self._lookupMatchFunctionId(
pattern,
endToken,
varId,
isFindMatch) is None)
self._matchFunctionCache[signature] = id
2017-07-22 11:05:50 +02:00
@staticmethod
def _compileCmd(tok):
if tok == '%any%':
return 'true'
elif tok == '%assign%':
return 'tok->isAssignmentOp()'
elif tok == '%bool%':
return 'tok->isBoolean()'
elif tok == '%char%':
return '(tok->tokType() == Token::eChar)'
elif tok == '%comp%':
return 'tok->isComparisonOp()'
elif tok == '%num%':
return 'tok->isNumber()'
2013-03-01 11:52:44 +01:00
elif tok == '%cop%':
return 'tok->isConstOp()'
elif tok == '%op%':
return 'tok->isOp()'
elif tok == '%or%':
return '(tok->tokType() == Token::eBitOp && tok->str() == MatchCompiler::makeConstString("|") )'
elif tok == '%oror%':
return '(tok->tokType() == Token::eLogicalOp && tok->str() == MatchCompiler::makeConstString("||"))'
elif tok == '%str%':
return '(tok->tokType() == Token::eString)'
elif tok == '%type%':
return '(tok->isName() && tok->varId() == 0U && (tok->str() != MatchCompiler::makeConstString("delete") || !tok->isKeyword()))'
elif tok == '%name%':
return 'tok->isName()'
elif tok == '%var%':
return '(tok->varId() != 0)'
elif tok == '%varid%':
return '(tok->isName() && tok->varId() == varid)'
2013-03-02 16:45:26 +01:00
elif (len(tok) > 2) and (tok[0] == "%"):
2015-03-20 01:31:41 +01:00
print("unhandled:" + tok)
elif tok in tokTypes:
cond = ' || '.join(['tok->tokType() == Token::{}'.format(tokType) for tokType in tokTypes[tok]])
return '(({cond}) && tok->str() == MatchCompiler::makeConstString("{tok}"))'.format(cond=cond, tok=tok)
2013-10-13 11:01:50 +02:00
return (
'(tok->str() == MatchCompiler::makeConstString("' + tok + '"))'
2013-10-13 11:01:50 +02:00
)
2013-10-13 11:01:50 +02:00
def _compilePattern(self, pattern, nr, varid,
isFindMatch=False, tokenType="const Token"):
if isFindMatch:
ret = '\n ' + tokenType + ' * tok = start_tok;\n'
returnStatement = 'continue;\n'
else:
arg2 = ''
if varid:
2019-07-15 13:49:35 +02:00
arg2 = ', const int varid'
ret = '// pattern: ' + pattern + '\n'
ret += 'static inline bool match' + \
2013-10-13 11:01:50 +02:00
str(nr) + '(' + tokenType + '* tok' + arg2 + ') {\n'
returnStatement = 'return false;\n'
tokens = pattern.split(' ')
gotoNextToken = ''
checked_varid = False
for tok in tokens:
if tok == '':
continue
ret += gotoNextToken
gotoNextToken = ' tok = tok->next();\n'
# if varid is provided, check that it's non-zero on first use
2017-06-05 13:23:00 +02:00
if varid and '%varid%' in tok and not checked_varid:
ret += ' if (varid==0U)\n'
2017-06-04 22:51:48 +02:00
ret += ' throw InternalError(tok, "Internal error. Token::Match called with varid 0. ' +\
'Please report this to Cppcheck developers");\n'
checked_varid = True
# [abc]
if (len(tok) > 2) and (tok[0] == '[') and (tok[-1] == ']'):
ret += ' if (!tok || tok->str().size() != 1U || !strchr("' + tok[1:-1] + '", tok->str()[0]))\n'
ret += ' ' + returnStatement
# a|b|c
2017-06-05 13:41:38 +02:00
elif tok.find('|') > 0:
tokens2 = tok.split('|')
2016-11-26 16:08:14 +01:00
logicalOp = ' || '
if "" in tokens2:
ret += ' if (tok && ('
else:
ret += ' if (!tok || !('
first = True
for tok2 in tokens2:
if tok2 == '':
continue
if not first:
ret += logicalOp
first = False
ret += self._compileCmd(tok2)
2016-11-26 16:08:14 +01:00
ret += '))\n'
if "" in tokens2:
ret += ' tok = tok->next();\n'
gotoNextToken = ''
else:
ret += ' ' + returnStatement
# !!a
elif tok[0:2] == "!!":
ret += ' if (tok && tok->str() == MatchCompiler::makeConstString("' + tok[2:] + '"))\n'
ret += ' ' + returnStatement
gotoNextToken = ' tok = tok ? tok->next() : nullptr;\n'
else:
2017-06-04 22:51:48 +02:00
negatedTok = "!" + self._compileCmd(tok)
# fold !true => false ; !false => true
# this avoids cppcheck warnings about condition always being true/false
2017-07-22 11:05:50 +02:00
if negatedTok == "!false":
negatedTok = "true"
2017-07-22 11:05:50 +02:00
elif negatedTok == "!true":
negatedTok = "false"
ret += ' if (!tok || ' + negatedTok + ')\n'
ret += ' ' + returnStatement
if isFindMatch:
ret += ' return start_tok;\n'
else:
ret += ' return true;\n'
ret += '}\n'
return ret
def _compileFindPattern(self, pattern, findmatchnr, endToken, varId):
more_args = ''
endCondition = ''
if endToken:
more_args += ', const Token * end'
endCondition = ' && start_tok != end'
if varId:
2019-07-15 13:49:35 +02:00
more_args += ', int varid'
ret = '// pattern: ' + pattern + '\n'
ret += 'template<class T> static inline T * findmatch' + \
2013-10-13 11:01:50 +02:00
str(findmatchnr) + '(T * start_tok' + more_args + ') {\n'
ret += ' for (; start_tok' + endCondition + \
'; start_tok = start_tok->next()) {\n'
ret += self._compilePattern(pattern, -1, varId, True, 'T')
ret += ' }\n'
ret += ' return nullptr;\n}\n'
return ret
2017-07-22 11:05:50 +02:00
@staticmethod
def parseMatch(line, pos1):
parlevel = 0
args = []
argstart = 0
pos = pos1
inString = False
while pos < len(line):
if inString:
if line[pos] == '\\':
pos += 1
elif line[pos] == '"':
inString = False
elif line[pos] == '"':
inString = True
elif line[pos] == '(':
parlevel += 1
if parlevel == 1:
argstart = pos + 1
elif line[pos] == ')':
parlevel -= 1
if parlevel == 0:
2017-07-22 11:05:50 +02:00
ret = [line[pos1:pos + 1]]
ret.extend(args)
ret.append(line[argstart:pos])
return ret
elif line[pos] == ',' and parlevel == 1:
args.append(line[argstart:pos])
argstart = pos + 1
pos += 1
return None
2017-07-22 11:05:50 +02:00
@staticmethod
def _isInString(line, pos1):
pos = 0
inString = False
while pos != pos1:
2016-11-26 16:08:14 +01:00
if line[pos] == '\\':
pos += 1
elif line[pos] == '"':
inString = not inString
pos += 1
2015-08-21 10:55:19 +02:00
return inString
2017-07-22 11:05:50 +02:00
@staticmethod
def _parseStringComparison(line, pos1):
startPos = 0
pos = pos1
inString = False
while pos < len(line):
if inString:
if line[pos] == '\\':
pos += 1
elif line[pos] == '"':
inString = False
2013-10-13 11:01:50 +02:00
endPos = pos + 1
2017-07-22 11:05:50 +02:00
return startPos, endPos
elif line[pos] == '"':
startPos = pos
inString = True
pos += 1
return None
2017-07-22 11:05:50 +02:00
@staticmethod
2013-10-13 11:01:50 +02:00
def _compileVerifyTokenMatch(
2017-07-22 11:05:50 +02:00
is_simplematch, verifyNumber, pattern, patternNumber, varId):
more_args = ''
if varId:
2019-07-15 13:49:35 +02:00
more_args = ', const int varid'
ret = 'static inline bool match_verify' + \
2013-10-13 11:01:50 +02:00
str(verifyNumber) + '(const Token *tok' + more_args + ') {\n'
origMatchName = 'Match'
if is_simplematch:
origMatchName = 'simpleMatch'
2013-10-13 11:01:50 +02:00
assert(varId is None)
2013-10-13 11:01:50 +02:00
ret += ' bool res_compiled_match = match' + \
str(patternNumber) + '(tok'
if varId:
ret += ', varid'
ret += ');\n'
2013-10-13 11:01:50 +02:00
ret += ' bool res_parsed_match = Token::' + \
origMatchName + '(tok, "' + pattern + '"'
if varId:
ret += ', varid'
ret += ');\n'
ret += '\n'
# Don't use assert() here, it's disabled for optimized builds.
# We also need to verify builds in 'release' mode
ret += ' if (res_parsed_match != res_compiled_match) {\n'
2017-06-04 22:51:48 +02:00
# ret += ' std::cout << "res_parsed_match' + str(verifyNumber) +\
# ': " << res_parsed_match << ", res_compiled_match: " << res_compiled_match << "\\n";\n'
# ret += ' if (tok)\n'
# ret += ' std::cout << "tok: " << tok->str();\n'
# ret += ' if (tok->next())\n'
# ret += ' std::cout << "tok next: " << tok->next()->str();\n'
ret += ' throw InternalError(tok, "Internal error. ' +\
'Compiled match returned different result than parsed match: ' + pattern + '");\n'
ret += ' }\n'
ret += ' return res_compiled_match;\n'
ret += '}\n'
return ret
2013-10-13 11:01:50 +02:00
def _replaceSpecificTokenMatch(
self, is_simplematch, line, start_pos, end_pos, pattern, tok, varId):
more_args = ''
if varId:
more_args = ',' + varId
# Compile function or use previously compiled one
2013-10-13 11:01:50 +02:00
patternNumber = self._lookupMatchFunctionId(
pattern, None, varId, False)
2013-03-02 16:45:26 +01:00
if patternNumber is None:
patternNumber = len(self._rawMatchFunctions) + 1
2013-10-13 11:01:50 +02:00
self._insertMatchFunctionId(
patternNumber,
pattern,
None,
varId,
False)
self._rawMatchFunctions.append(
self._compilePattern(pattern, patternNumber, varId))
functionName = "match"
if self._verifyMode:
verifyNumber = len(self._rawMatchFunctions) + 1
2013-10-13 11:01:50 +02:00
self._rawMatchFunctions.append(
self._compileVerifyTokenMatch(
is_simplematch,
verifyNumber,
pattern,
patternNumber,
varId))
# inject verify function
functionName = "match_verify"
patternNumber = verifyNumber
2013-10-13 11:01:50 +02:00
return (
line[:start_pos] + functionName + str(
patternNumber) + '(' + tok + more_args + ')' + line[start_pos + end_pos:]
)
def _replaceTokenMatch(self, line, linenr, filename):
for func in ('Match', 'simpleMatch'):
is_simplematch = func == 'simpleMatch'
pattern_start = 0
while True:
# skip comments
if line.strip().startswith('//'):
break
pos1 = line.find('Token::' + func + '(', pattern_start)
if pos1 == -1:
break
res = self.parseMatch(line, pos1)
if res is None:
break
# assert that Token::Match has either 2 or 3 arguments
assert(len(res) == 3 or len(res) == 4)
end_pos = len(res[0])
tok = res[1]
raw_pattern = res[2]
varId = None
if len(res) == 4:
varId = res[3]
pattern_start = pos1 + end_pos
res = re.match(r'\s*"((?:.|\\")*?)"\s*$', raw_pattern)
if res is None:
if self._showSkipped:
print(filename + ":" + str(linenr) + " skipping match pattern:" + raw_pattern)
continue # Non-const pattern - bailout
pattern = res.group(1)
orig_len = len(line)
line = self._replaceSpecificTokenMatch(
is_simplematch,
line,
pos1,
end_pos,
pattern,
tok,
varId)
pattern_start += len(line) - orig_len
return line
2017-07-22 11:05:50 +02:00
@staticmethod
2013-10-13 11:01:50 +02:00
def _compileVerifyTokenFindMatch(
2017-07-22 11:05:50 +02:00
is_findsimplematch, verifyNumber, pattern, patternNumber, endToken, varId):
more_args = ''
if endToken:
more_args += ', const Token * endToken'
if varId:
2019-07-15 13:49:35 +02:00
more_args += ', const int varid'
ret = 'template < class T > static inline T * findmatch_verify' + \
2013-10-13 11:01:50 +02:00
str(verifyNumber) + '(T * tok' + more_args + ') {\n'
origFindMatchName = 'findmatch'
if is_findsimplematch:
origFindMatchName = 'findsimplematch'
2013-10-13 11:01:50 +02:00
assert(varId is None)
2013-10-13 11:01:50 +02:00
ret += ' T * res_compiled_findmatch = findmatch' + \
str(patternNumber) + '(tok'
if endToken:
ret += ', endToken'
if varId:
ret += ', varid'
ret += ');\n'
2013-10-13 11:01:50 +02:00
ret += ' T * res_parsed_findmatch = Token::' + \
origFindMatchName + '(tok, "' + pattern + '"'
if endToken:
ret += ', endToken'
if varId:
ret += ', varid'
ret += ');\n'
ret += '\n'
# Don't use assert() here, it's disabled for optimized builds.
# We also need to verify builds in 'release' mode
ret += ' if (res_parsed_findmatch != res_compiled_findmatch) {\n'
2017-06-04 22:51:48 +02:00
ret += ' throw InternalError(tok, "Internal error. ' +\
'Compiled findmatch returned different result than parsed findmatch: ' + pattern + '");\n'
ret += ' }\n'
ret += ' return res_compiled_findmatch;\n'
ret += '}\n'
return ret
2013-10-13 11:01:50 +02:00
def _replaceSpecificFindTokenMatch(
self, is_findsimplematch, line, start_pos, end_pos, pattern, tok, endToken, varId):
more_args = ''
if endToken:
more_args += ',' + endToken
if varId:
more_args += ',' + varId
# Compile function or use previously compiled one
2013-10-13 11:01:50 +02:00
findMatchNumber = self._lookupMatchFunctionId(
pattern, endToken, varId, True)
2013-03-02 16:45:26 +01:00
if findMatchNumber is None:
findMatchNumber = len(self._rawMatchFunctions) + 1
2013-10-13 11:01:50 +02:00
self._insertMatchFunctionId(
findMatchNumber,
pattern,
endToken,
varId,
True)
self._rawMatchFunctions.append(
self._compileFindPattern(
pattern,
findMatchNumber,
endToken,
varId))
functionName = "findmatch"
if self._verifyMode:
verifyNumber = len(self._rawMatchFunctions) + 1
2013-10-13 11:01:50 +02:00
self._rawMatchFunctions.append(
self._compileVerifyTokenFindMatch(
is_findsimplematch,
verifyNumber,
pattern,
findMatchNumber,
endToken,
varId))
# inject verify function
functionName = "findmatch_verify"
findMatchNumber = verifyNumber
2013-10-13 11:01:50 +02:00
return (
line[:start_pos] + functionName + str(
findMatchNumber) + '(' + tok + more_args + ') ' + line[start_pos + end_pos:]
2013-10-13 11:01:50 +02:00
)
def _replaceTokenFindMatch(self, line, linenr, filename):
while True:
is_findsimplematch = True
pos1 = line.find('Token::findsimplematch(')
if pos1 == -1:
is_findsimplematch = False
pos1 = line.find('Token::findmatch(')
if pos1 == -1:
break
res = self.parseMatch(line, pos1)
2013-03-02 16:45:26 +01:00
if res is None:
break
2013-01-13 20:17:30 +01:00
2017-06-04 22:51:48 +02:00
# assert that Token::find(simple)match has either 2, 3 or 4 arguments
2013-10-13 11:01:50 +02:00
assert(len(res) >= 3 or len(res) < 6)
2013-01-13 20:17:30 +01:00
g0 = res[0]
tok = res[1]
2013-01-13 20:17:30 +01:00
pattern = res[2]
# Check for varId
varId = None
2017-06-05 13:23:00 +02:00
if not is_findsimplematch and "%varid%" in g0:
2013-01-13 20:17:30 +01:00
if len(res) == 5:
varId = res[4]
else:
2013-01-13 20:17:30 +01:00
varId = res[3]
# endToken support. We resolve the overloaded type by checking if varId is used or not.
2018-02-04 20:53:43 +01:00
# Function prototypes:
2013-01-13 20:17:30 +01:00
# Token *findsimplematch(const Token *tok, const char pattern[]);
# Token *findsimplematch(const Token *tok, const char pattern[], const Token *end);
2019-07-15 13:49:35 +02:00
# Token *findmatch(const Token *tok, const char pattern[], int varId = 0);
2013-10-13 11:01:50 +02:00
# Token *findmatch(const Token *tok, const char pattern[], const
2019-07-15 13:49:35 +02:00
# Token *end, int varId = 0);
2013-01-13 20:17:30 +01:00
endToken = None
2016-11-26 16:08:14 +01:00
if ((is_findsimplematch and len(res) == 4) or
(not is_findsimplematch and varId and (len(res) == 5)) or
(not is_findsimplematch and varId is None and len(res) == 4)):
2013-01-13 20:17:30 +01:00
endToken = res[3]
res = re.match(r'\s*"((?:.|\\")*?)"\s*$', pattern)
2013-03-02 16:45:26 +01:00
if res is None:
if self._showSkipped:
2017-06-04 22:51:48 +02:00
print(filename + ":" + str(linenr) + " skipping findmatch pattern:" + pattern)
2013-01-13 20:17:30 +01:00
break # Non-const pattern - bailout
pattern = res.group(1)
2013-10-13 11:01:50 +02:00
line = self._replaceSpecificFindTokenMatch(
is_findsimplematch,
line,
pos1,
len(g0),
pattern,
tok,
endToken,
varId)
return line
def _replaceCStrings(self, line):
while True:
match = re.search('(==|!=) *"', line)
if not match:
break
if self._isInString(line, match.start()):
break
res = self._parseStringComparison(line, match.start())
2013-03-02 16:45:26 +01:00
if res is None:
break
startPos = res[0]
endPos = res[1]
2013-10-13 11:01:50 +02:00
text = line[startPos + 1:endPos - 1]
2017-06-04 22:51:48 +02:00
line = line[:startPos] + 'MatchCompiler::makeConstStringBegin' +\
text + 'MatchCompiler::makeConstStringEnd' + line[endPos:]
line = line.replace('MatchCompiler::makeConstStringBegin', 'MatchCompiler::makeConstString("')
line = line.replace('MatchCompiler::makeConstStringEnd', '")')
return line
2015-07-23 10:49:53 +02:00
def convertFile(self, srcname, destname, line_directive):
self._reset()
fin = io.open(srcname, "rt", encoding="utf-8")
srclines = fin.readlines()
fin.close()
code = u''
modified = False
linenr = 0
for line in srclines:
if not modified:
line_orig = line
linenr += 1
# Compile Token::Match and Token::simpleMatch
line = self._replaceTokenMatch(line, linenr, srcname)
# Compile Token::findsimplematch
line = self._replaceTokenFindMatch(line, linenr, srcname)
# Cache plain C-strings in C++ strings
line = self._replaceCStrings(line)
if not modified and not line_orig == line:
modified = True
code += line
# Compute matchFunctions
strFunctions = u''
for function in self._rawMatchFunctions:
strFunctions += function
lineno = u''
2015-07-23 10:49:53 +02:00
if line_directive:
lineno = u'#line 1 "' + srcname + '"\n'
header = u'#include "matchcompiler.h"\n'
header += u'#include <string>\n'
header += u'#include <cstring>\n'
if len(self._rawMatchFunctions):
header += u'#include "errorlogger.h"\n'
header += u'#include "token.h"\n'
2015-07-23 10:49:53 +02:00
fout = io.open(destname, 'wt', encoding="utf-8")
if modified or len(self._rawMatchFunctions):
fout.write(header)
fout.write(strFunctions)
fout.write(lineno)
fout.write(code)
fout.close()
2013-03-02 16:45:26 +01:00
2013-01-11 16:21:17 +01:00
def main():
# Main program
# Argument handling
2013-10-13 11:01:50 +02:00
parser = argparse.ArgumentParser(
description='Compile Token::Match() calls into native C++ code')
2013-01-11 16:21:17 +01:00
parser.add_argument('--verify', action='store_true', default=False,
2013-03-02 16:45:26 +01:00
help='verify compiled matches against on-the-fly parser. Slow!')
parser.add_argument('--show-skipped', action='store_true', default=False,
help='show skipped (non-static) patterns')
2015-08-21 10:55:19 +02:00
parser.add_argument('--read-dir', default="lib",
2015-07-23 10:49:53 +02:00
help='directory from which files are read')
parser.add_argument('--write-dir', default="build",
help='directory into which files are written')
parser.add_argument('--prefix', default="",
help='prefix for build files')
parser.add_argument('--line', action='store_true', default=False,
help='add line directive to input files into build files')
parser.add_argument('file', nargs='*',
2019-01-06 17:15:57 +01:00
help='file to compile')
2013-01-11 16:21:17 +01:00
args = parser.parse_args()
2015-07-23 10:49:53 +02:00
lib_dir = args.read_dir
build_dir = args.write_dir
line_directive = args.line
files = args.file
# Check if we are invoked from the right place
if not os.path.exists(lib_dir):
print('Directory "' + lib_dir + '"not found.')
sys.exit(-1)
# Create build directory if needed
try:
2015-07-23 10:49:53 +02:00
os.makedirs(build_dir)
except OSError as e:
# due to race condition in case of parallel build,
# makedirs may fail. Ignore that; if there's actual
# problem with directory creation, it'll be caught
# by the following isdir check
if e.errno != errno.EEXIST:
raise
2015-07-23 10:49:53 +02:00
if not os.path.isdir(build_dir):
raise Exception(build_dir + ' is not a directory')
2013-01-11 16:21:17 +01:00
mc = MatchCompiler(verify_mode=args.verify,
show_skipped=args.show_skipped)
2013-01-11 16:21:17 +01:00
2015-07-23 10:49:53 +02:00
if not files:
# select all *.cpp files in lib_dir
for f in glob.glob(lib_dir + '/*.cpp'):
2015-08-21 10:55:19 +02:00
files.append(f[len(lib_dir) + 1:])
2015-07-23 10:49:53 +02:00
# convert files
for fi in files:
pi = lib_dir + '/' + fi
fo = args.prefix + fi
po = build_dir + '/' + fo
print(pi + ' => ' + po)
mc.convertFile(pi, po, line_directive)
2013-01-11 16:21:17 +01:00
if __name__ == '__main__':
main()