Add preliminary support for removing static funcs

This commit is contained in:
Joey Yakimowich-Payne 2018-07-07 19:16:16 +09:00
commit b32d39ad08

View file

@ -25,7 +25,7 @@ var
type
c2nimConfigObj = object
flags, ppflags: string
recurse, inline, preprocess, ctags, defines: bool
recurse, inline, preprocess, ctags, defines, remove_static: bool
dynlib, compile, pragma: seq[string]
const DOC = """
@ -92,7 +92,9 @@ proc execProc(cmd: string): string =
proc extractZip(zipfile: string) =
var cmd = "unzip -o $#"
if defined(Windows):
cmd = "powershell -nologo -noprofile -command \"& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('$#', '.'); }\""
cmd = "powershell -nologo -noprofile -command \"& { Add-Type -A " &
"'System.IO.Compression.FileSystem'; " &
"[IO.Compression.ZipFile]::ExtractToDirectory('$#', '.'); }\""
setCurrentDir(gOutput)
defer: setCurrentDir(gProjectDir)
@ -322,6 +324,67 @@ proc comment(file: string, pattern: string, numlines: string) =
idx += 1
break
proc removeStatic(filename: string) =
if not fileExists(filename):
echo "Missing file: " & filename
return
# This function should ideally be a regex. However, I could not
# get it to work as intended with the current re implementation
# in Nim.
#
# withFile(filename):
# content = content.replacef(
# re"(static inline.*?\))([ \r\n]*?\{([ \r\n]*?.*?)*[\n\r]\})", "$1;")
# )
#
# This currently won't even run, but if the replacef function is modified
# to not have nil checks, it will run on 1/3 of the input file. Maybe there's
# a buffer length issue.
var
file = open(filename)
stack: seq[string] = @[]
foundBrace = false
foundStatic = false
writeOutput = true
output = newStringofCap(getFileSize(filename))
for line in file.lines():
var modLine = line
if not foundStatic:
writeOutput = true
if line.startswith("static inline"):
foundStatic = true
let index = modLine.find("{")
if index != -1:
foundBrace = true
modLine.setLen(index)
elif not foundBrace:
writeOutput = true
if modLine.strip().startswith("{"):
foundBrace = true
writeOutput = false
else:
if modLine.startswith("}"):
foundBrace = false
foundStatic = false
output[^1] = ';'
output &= "\n"
if writeOutput:
output &= modLine
output &= "\n"
writeOutput = false
file.close()
var f = open(filename, fmWrite)
write(f, output)
f.close()
proc rename(file: string, renfile: string) =
if file.splitFile().ext == ".nim":
return
@ -519,6 +582,9 @@ proc c2nim(fl, outfile: string, c2nimConfig: c2nimConfigObj) =
if c2nimConfig.defines and (c2nimConfig.preprocess or c2nimConfig.ctags):
prepend(cfile, getDefines(file, c2nimConfig.inline))
if c2nimConfig.remove_static:
removeStatic(cfile)
var
extflags = ""
passC = ""
@ -697,6 +763,8 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
c2nimConfig.ctags = true
elif action == "defines":
c2nimConfig.defines = true
elif act == "remove_static":
c2nimConfig.remove_static = true
elif action == "noprocess":
noprocess = true
elif action == "flags":