aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2019-01-31 21:15:05 -0600
committerGanesh Viswanathan <dev@genotrance.com>2019-01-31 21:15:05 -0600
commit1807580c71b28f76ded313bb3227833bc6081d22 (patch)
tree81d5d5c6dcc3e6077e7a273f5b184745533cc56e
parent4152564b398d1cc9214281d274e332d8e4d48012 (diff)
downloadnimterop-1807580c71b28f76ded313bb3227833bc6081d22.tar.gz
nimterop-1807580c71b28f76ded313bb3227833bc6081d22.zip
Fix #96 - run nim check on bad codegen
-rw-r--r--nimterop/cimport.nim35
1 files changed, 27 insertions, 8 deletions
diff --git a/nimterop/cimport.nim b/nimterop/cimport.nim
index 1dd97aa..60e0781 100644
--- a/nimterop/cimport.nim
+++ b/nimterop/cimport.nim
@@ -77,11 +77,27 @@ proc getToastError(output: string): string =
# Filter out preprocessor errors
for line in output.splitLines():
if "fatal error:" in line.toLowerAscii:
- result &= "\nERROR:$1\n" % line.split("fatal error:")[1]
+ result &= "\n\nERROR:$1\n" % line.split("fatal error:")[1]
# Toast error
if result.len == 0:
- result = output
+ result = "\n\n" & output
+
+proc getNimCheckError(output: string): tuple[tmpFile, errors: string] =
+ let
+ hash = output.hash().abs()
+
+ result.tmpFile = getTempDir() / "nimterop_" & $hash & ".nim"
+
+ if not fileExists(result.tmpFile) or gStateCT.nocache or compileOption("forceBuild"):
+ writeFile(result.tmpFile, output)
+
+ doAssert fileExists(result.tmpFile), "Bad codegen - unable to write to TEMP: " & result.tmpFile
+
+ let
+ (check, ret) = gorgeEx("nim check " & result.tmpFile)
+
+ result.errors = "\n\n" & check
proc getToast(fullpath: string, recurse: bool = false): string =
var
@@ -433,11 +449,14 @@ macro cImport*(filename: static string, recurse: static bool = false): untyped =
output = getToast(fullpath, recurse)
try:
- result.add parseStmt(output)
+ let body = parseStmt(output)
+
+ result.add body
+
+ if gStateCT.debug:
+ echo result.repr
except:
- echo output
- echo "Failed to import generated nim"
- result.add parseStmt(output)
+ let
+ (tmpFile, errors) = getNimCheckError(output)
+ doAssert false, errors & "Nimterop codegen limitation or error - review 'nim check' output above generated for " & tmpFile
- if gStateCT.debug:
- echo result.repr