Preprocessor mode fixes (#176)

This commit is contained in:
awr1 2020-04-09 23:00:56 -05:00 • committed by GitHub
commit f5dd89904d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 33 deletions

View file

@ -620,6 +620,21 @@ proc make*(path, check: string, flags = "", regex = false) =
doAssert findFile(check, path, regex = regex).len != 0, "# make failed"
proc getCompilerMode*(path: string): string =
## Determines a target language mode from an input filename, if one is not already specified.
let file = path.splitFile()
if file.ext in [".hxx", ".hpp", ".hh", ".H", ".h++", ".cpp", ".cxx", ".cc", ".C", ".c++"]:
result = "cpp"
elif file.ext in [".h", ".c"]:
result = "c"
proc getGccModeArg*(mode: string): string =
## Produces a GCC argument that explicitly sets the language mode to be used by the compiler.
if mode == "cpp":
result = "-xc++"
elif mode == "c":
result = "-xc"
proc getCompiler*(): string =
var
compiler =
@ -632,13 +647,12 @@ proc getCompiler*(): string =
result = getEnv("CC", compiler)
proc getGccPaths*(mode = "c"): seq[string] =
proc getGccPaths*(mode: string): seq[string] =
var
nul = when defined(Windows): "nul" else: "/dev/null"
mmode = if mode == "cpp": "c++" else: mode
inc = false
(outp, _) = execAction(&"""{getCompiler()} -Wp,-v -x{mmode} {nul}""", die = false)
(outp, _) = execAction(&"""{getCompiler()} -Wp,-v {getGccModeArg(mode)} {nul}""", die = false)
for line in outp.splitLines():
if "#include <...> search starts here" in line:
@ -655,13 +669,12 @@ proc getGccPaths*(mode = "c"): seq[string] =
when defined(osx):
result.add(execAction("xcrun --show-sdk-path").output.strip() & "/usr/include")
proc getGccLibPaths*(mode = "c"): seq[string] =
proc getGccLibPaths*(mode: string): seq[string] =
var
nul = when defined(Windows): "nul" else: "/dev/null"
mmode = if mode == "cpp": "c++" else: mode
linker = when defined(OSX): "-Xlinker" else: ""
(outp, _) = execAction(&"""{getCompiler()} {linker} -v -x{mmode} {nul}""", die = false)
(outp, _) = execAction(&"""{getCompiler()} {linker} -v {getGccModeArg(mode)} {nul}""", die = false)
for line in outp.splitLines():
if "LIBRARY_PATH=" in line:
@ -680,14 +693,14 @@ proc getGccLibPaths*(mode = "c"): seq[string] =
when defined(osx):
result.add "/usr/lib"
proc getStdPath(header: string): string =
for inc in getGccPaths():
proc getStdPath(header, mode: string): string =
for inc in getGccPaths(mode):
result = findFile(header, inc, recurse = false, first = true)
if result.len != 0:
break
proc getStdLibPath(lname: string): string =
for lib in getGccLibPaths():
proc getStdLibPath(lname, mode: string): string =
for lib in getGccLibPaths(mode):
result = findFile(lname, lib, recurse = false, first = true, regex = true)
if result.len != 0:
break
@ -973,6 +986,7 @@ macro getHeader*(header: static[string], giturl: static[string] = "", dlurl: sta
gDefines[verStr]
else:
""
mode = getCompilerMode(header)
# Use alternate library names if specified for regex search
if altNames.len != 0:
@ -1008,9 +1022,9 @@ macro getHeader*(header: static[string], giturl: static[string] = "", dlurl: sta
# Look in standard path if requested by user
stdPath =
when `nameStd`: getStdPath(`header`) else: ""
when `nameStd`: getStdPath(`header`, `mode`) else: ""
stdLPath =
when `nameStd`: getStdLibPath(`lname`) else: ""
when `nameStd`: getStdLibPath(`lname`, `mode`) else: ""
# Look elsewhere if requested while prioritizing standard paths
prePath =

View file

@ -495,11 +495,10 @@ proc removeStatic(content: string): string =
result.add(body.replace(re"(?m)^(.*\n?)", "//$1"))
)
proc getPreprocessor*(gState: State, fullpath: string, mode = "cpp"): string =
proc getPreprocessor*(gState: State, fullpath: string): string =
var
mmode = if mode == "cpp": "c++" else: mode
cmts = if gState.nocomments: "" else: "-CC"
cmd = &"""{getCompiler()} -E {cmts} -dD -x{mmode} -w """
cmd = &"""{getCompiler()} -E {cmts} -dD {getGccModeArg(gState.mode)} -w """
rdata: seq[string] = @[]
start = false

View file

@ -98,9 +98,6 @@ type
nodeBranch*: seq[string]
CompileMode = enum
c, cpp
Feature* = enum
ast2
@ -113,11 +110,9 @@ template nBl(s: typed): untyped {.used.} =
template Bl(s: typed): untyped {.used.} =
(s.len == 0)
const modeDefault {.used.} = $cpp
when not declared(CIMPORT):
export gAtoms, gExpressions, gEnumVals, Kind, Ast, AstTable, State, NimState,
nBl, Bl, CompileMode, modeDefault
nBl, Bl
# Redirect output to file when required
template gecho*(args: string) {.dirty.} =
@ -133,4 +128,4 @@ when not declared(CIMPORT):
template decho*(str: untyped): untyped =
if nimState.gState.debug:
necho str.getCommented()
necho str.getCommented()

View file

@ -2,23 +2,18 @@ import os, osproc, strformat, strutils, times
import "."/treesitter/[api, c, cpp]
import "."/[ast, ast2, globals, getters, grammar]
import "."/[ast, ast2, globals, getters, grammar, build]
proc process(gState: State, path: string, astTable: AstTable) =
doAssert existsFile(path), &"Invalid path {path}"
var
parser = tsParserNew()
ext = path.splitFile().ext
var parser = tsParserNew()
defer:
parser.tsParserDelete()
if gState.mode.Bl:
if ext in [".h", ".c"]:
gState.mode = "c"
elif ext in [".hxx", ".hpp", ".hh", ".H", ".h++", ".cpp", ".cxx", ".cc", ".C", ".c++"]:
gState.mode = "cpp"
gState.mode = getCompilerMode(path)
if gState.preprocess:
gState.code = gState.getPreprocessor(path)
@ -61,7 +56,7 @@ proc main(
feature: seq[Feature] = @[],
includeHeader = false,
includeDirs: seq[string] = @[],
mode = modeDefault,
mode = "",
nim: string = "nim",
nocomments = false,
output = "",

View file

@ -31,8 +31,7 @@ cPlugin:
sym.name = sym.name.replace("pcre_", "")
const FLAGS {.strdefine.} = ""
cImport(pcreH, dynlib="dynpcre", flags = FLAGS)
cImport(pcreH, dynlib="dynpcre", flags="--mode=c " & FLAGS)
echo version()
when FLAGS.len != 0: