#include file, static line, nostderr, improve preprocessing

This commit is contained in:
Ganesh Viswanathan 2019-01-11 12:22:49 -06:00
commit 88005fa984
6 changed files with 59 additions and 21 deletions

View file

@ -21,12 +21,15 @@ proc findPath(path: string, fail = true): string =
else:
return ""
proc getToast(fullpath: string): string =
proc getToast(fullpath: string, recurse: bool = false): string =
var
cmd = when defined(Windows): "cmd /c " else: ""
cmd &= "toast --pnim --preprocess "
if recurse:
cmd.add "--recurse "
for i in gStateCT.defines:
cmd.add &"--defines+={i.quoteShell} "
@ -156,7 +159,7 @@ macro cCompile*(path: static string): untyped =
if gStateCT.debug:
echo result.repr
macro cImport*(filename: static string): untyped =
macro cImport*(filename: static string, recurse: static bool = false): untyped =
result = newNimNode(nnkStmtList)
let
@ -165,7 +168,7 @@ macro cImport*(filename: static string): untyped =
echo "Importing " & fullpath
let
output = getToast(fullpath)
output = getToast(fullpath, recurse)
try:
result.add parseStmt(output)

View file

@ -112,6 +112,20 @@ proc getGccPaths*(mode = "c"): string =
(result, ret) = gorgeEx("gcc -Wp,-v -x" & mmode & " " & nul)
proc removeStatic*(content: string): string =
## Replace static function bodies with a semicolon and commented
## out body
return content.replace(
re"(?ms)static inline(.*?\))(\s*\{(\s*?.*?$)*?[\n\r]\})",
proc (m: RegexMatch, s: string): string =
let funcDecl = s[m.group(0)[0]]
let body = s[m.group(1)[0]].strip()
result = ""
result.add("$#;" % [funcDecl])
result.add(body.replace(re"(?m)^(.*\n?)", "//$1"))
)
proc getPreprocessor*(fullpath: string, mode = "cpp"): string =
var
mmode = if mode == "cpp": "c++" else: mode
@ -130,20 +144,36 @@ proc getPreprocessor*(fullpath: string, mode = "cpp"): string =
cmd &= &"\"{fullpath}\""
# Include content only from file
for line in execAction(cmd).splitLines():
for line in execAction(cmd, true).splitLines():
if line.strip() != "":
if line[0] == '#' and not line.contains("#pragma") and not line.contains("define"):
if line.len > 1 and line[0 .. 1] == "# ":
start = false
if sfile in line.sanitizePath:
let
saniLine = line.sanitizePath
if sfile in saniLine:
start = true
if not ("\\" in line) and not ("/" in line) and extractFilename(sfile) in line:
elif not ("\\" in line) and not ("/" in line) and extractFilename(sfile) in line:
start = true
elif gStateRT.recurse:
if sfile.parentDir() in saniLine:
start = true
else:
for inc in gStateRT.includeDirs:
if inc.absolutePath().sanitizePath in saniLine:
start = true
break
if start:
rdata.add(&"// {line}")
else:
if start:
if "#undef" in line:
continue
rdata.add(
line.replace(re"__attribute__[ ]*\(\(.*?\)\) ", "")
line.
replace("__restrict", "").
replace(re"__attribute__[ ]*\(\(.*?\)\)([ ,;])", "$1")
)
return rdata.join("\n")
return rdata.join("\n").removeStatic()
converter toString*(kind: Kind): string =
return case kind:

View file

@ -2,7 +2,7 @@ import macros, os, osproc, regex, strformat, strutils
import globals
proc execAction*(cmd: string): string =
proc execAction*(cmd: string, nostderr=false): string =
var
ccmd = ""
ret = 0
@ -14,7 +14,10 @@ proc execAction*(cmd: string): string =
when nimvm:
(result, ret) = gorgeEx(ccmd)
else:
(result, ret) = execCmdEx(ccmd)
if nostderr:
(result, ret) = execCmdEx(ccmd, {poUsePath})
else:
(result, ret) = execCmdEx(ccmd)
if ret != 0:
echo "Command failed: " & $ret
echo ccmd
@ -66,7 +69,7 @@ macro gitCheckout*(file, outdir: static string): untyped =
macro gitPull*(url: static string, outdirN = "", plistN = "", checkoutN = ""): untyped =
let
outdir = getProjectPath()/outdirN.strVal()
outdir = if outdirN.strVal().isAbsolute(): outdirN.strVal() else: getProjectPath()/outdirN.strVal()
plist = plistN.strVal()
checkout = checkoutN.strVal()

View file

@ -21,7 +21,7 @@ type
State* = object
compile*, defines*, headers*, includeDirs*, searchDirs*: seq[string]
debug*, past*, preprocess*, pnim*, pretty*: bool
debug*, past*, preprocess*, pnim*, pretty*, recurse*: bool
consts*, enums*, procs*, types*: seq[string]