aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2020-03-12 18:36:38 -0500
committerGanesh Viswanathan <dev@genotrance.com>2020-03-12 18:36:38 -0500
commit4dd15aa7d7c2aae0e4d96e4ac0a4c1423afd396e (patch)
treeef8931b3bee0cfed69b6a965373d43b5a599d535
parentd6e69dbf84e3f2c85e24a62c7bd3bb56090ca2da (diff)
downloadnimterop-issue169.tar.gz
nimterop-issue169.zip
Fix #169 - header pragma omitted by defaultissue169
-rw-r--r--nimterop/ast.nim4
-rw-r--r--nimterop/getters.nim22
-rw-r--r--nimterop/globals.nim2
-rw-r--r--nimterop/grammar.nim4
-rw-r--r--nimterop/toast.nim13
-rw-r--r--tests/include/test2.hpp10
6 files changed, 26 insertions, 29 deletions
diff --git a/nimterop/ast.nim b/nimterop/ast.nim
index a61e65a..e28241f 100644
--- a/nimterop/ast.nim
+++ b/nimterop/ast.nim
@@ -178,7 +178,7 @@ proc printNim*(gState: State, fullpath: string, root: TSNode, astTable: AstTable
nimState.impShort = nimState.currentHeader.replace("header", "imp")
nimState.sourceFile = fullpath
- if nimState.gState.dynlib.Bl:
+ if nimState.gState.dynlib.Bl and nimState.gState.includeHeader:
nimState.constStr &= &"\n {nimState.currentHeader} {{.used.}} = \"{fp}\""
root.searchAst(astTable, nimState)
@@ -191,7 +191,7 @@ proc printNim*(gState: State, fullpath: string, root: TSNode, astTable: AstTable
necho &"const{nimState.constStr}\n"
necho &"""
-{{.pragma: {nimState.impShort}, importc{nimState.getHeader()}.}}
+{{.pragma: {nimState.impShort}, importc{nimState.getHeaderPragma()}.}}
{{.pragma: {nimState.impShort}C, {nimState.impShort}, cdecl{nimState.getDynlib()}.}}
"""
diff --git a/nimterop/getters.nim b/nimterop/getters.nim
index 4cd8c04..93c4000 100644
--- a/nimterop/getters.nim
+++ b/nimterop/getters.nim
@@ -346,8 +346,7 @@ proc printLisp*(gState: State, root: TSNode): string =
while true:
if not node.isNil() and depth > -1:
- if gState.pretty:
- result &= spaces(depth)
+ result &= spaces(depth)
let
(line, col) = gState.getLineCol(node)
result &= &"({$node.tsNodeType()} {line} {col} {node.tsNodeEndByte() - node.tsNodeStartByte()}"
@@ -359,15 +358,11 @@ proc printLisp*(gState: State, root: TSNode): string =
break
if node.tsNodeNamedChildCount() != 0:
- if gState.pretty:
- result &= "\n"
+ result &= "\n"
nextnode = node.tsNodeNamedChild(0)
depth += 1
else:
- if gState.pretty:
- result &= ")\n"
- else:
- result &= ")"
+ result &= ")\n"
nextnode = node.tsNodeNextNamedSibling()
if nextnode.isNil():
@@ -376,10 +371,7 @@ proc printLisp*(gState: State, root: TSNode): string =
depth -= 1
if depth == -1:
break
- if gState.pretty:
- result &= spaces(depth) & ")\n"
- else:
- result &= ")"
+ result &= spaces(depth) & ")\n"
if node == root:
break
if not node.tsNodeNextNamedSibling().isNil():
@@ -656,9 +648,9 @@ proc getSplitComma*(joined: seq[string]): seq[string] =
for i in joined:
result = result.concat(i.split(","))
-proc getHeader*(nimState: NimState): string =
+proc getHeaderPragma*(nimState: NimState): string =
result =
- if nimState.gState.dynlib.Bl:
+ if nimState.gState.dynlib.Bl and nimState.gState.includeHeader:
&", header: {nimState.currentHeader}"
else:
""
@@ -672,7 +664,7 @@ proc getDynlib*(nimState: NimState): string =
proc getImportC*(nimState: NimState, origName, nimName: string): string =
if nimName != origName:
- result = &"importc: \"{origName}\"{nimState.getHeader()}"
+ result = &"importc: \"{origName}\"{nimState.getHeaderPragma()}"
else:
result = nimState.impShort
diff --git a/nimterop/globals.nim b/nimterop/globals.nim
index b234c1f..35a5575 100644
--- a/nimterop/globals.nim
+++ b/nimterop/globals.nim
@@ -57,7 +57,7 @@ type
State = ref object
compile*, defines*, headers*, includeDirs*, searchDirs*, prefix*, suffix*, symOverride*: seq[string]
- nocache*, nocomments*, debug*, past*, preprocess*, pnim*, pretty*, recurse*: bool
+ debug*, includeHeader*, nocache*, nocomments*, past*, preprocess*, pnim*, recurse*: bool
code*, dynlib*, mode*, nim*, overrides*, pluginSource*, pluginSourcePath*: string
diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim
index c76844c..f15c2f8 100644
--- a/nimterop/grammar.nim
+++ b/nimterop/grammar.nim
@@ -200,7 +200,7 @@ proc initGrammar(): Grammar =
nname = nimState.getIdentifier(name, nskType)
i += 1
- if nimState.gState.dynlib.Bl:
+ if nimState.gState.dynlib.Bl and nimState.gState.includeHeader:
pragmas.add nimState.getImportC(name, nname)
let
@@ -316,7 +316,7 @@ proc initGrammar(): Grammar =
else:
var
pragmas: seq[string] = @[]
- if nimState.gState.dynlib.Bl:
+ if nimState.gState.dynlib.Bl and nimState.gState.includeHeader:
pragmas.add nimState.getImportC(prefix & name, nname)
pragmas.add "bycopy"
if union.nBl:
diff --git a/nimterop/toast.nim b/nimterop/toast.nim
index a4415d2..323b4ec 100644
--- a/nimterop/toast.nim
+++ b/nimterop/toast.nim
@@ -58,6 +58,7 @@ proc main(
defines: seq[string] = @[],
dynlib: string = "",
feature: seq[Feature] = @[],
+ includeHeader = false,
includeDirs: seq[string] = @[],
mode = modeDefault,
nim: string = "nim",
@@ -82,6 +83,7 @@ proc main(
defines: defines,
dynlib: dynlib,
feature: feature,
+ includeHeader: includeHeader,
includeDirs: includeDirs,
mode: mode,
nim: nim,
@@ -91,7 +93,6 @@ proc main(
pnim: pnim,
prefix: prefix,
preprocess: preprocess,
- pretty: true,
recurse: recurse,
suffix: suffix,
symOverride: symOverride
@@ -190,8 +191,9 @@ when isMainModule:
"check": "check generated wrapper with compiler",
"debug": "enable debug output",
"defines": "definitions to pass to preprocessor",
- "dynlib": "Import symbols from library in specified Nim string",
+ "dynlib": "import symbols from library in specified Nim string",
"feature": "flags to enable experimental features",
+ "includeHeader": "add {.header.} pragma to wrapper",
"includeDirs": "include directory to pass to preprocessor",
"mode": "language parser: c or cpp",
"nim": "use a particular Nim executable (default: $PATH/nim)",
@@ -199,14 +201,14 @@ when isMainModule:
"output": "file to output content - default stdout",
"past": "print AST output",
"pgrammar": "print grammar",
- "pluginSourcePath": "Nim file to build and load as a plugin",
+ "pluginSourcePath": "nim file to build and load as a plugin",
"pnim": "print Nim output",
"preprocess": "run preprocessor on header",
"recurse": "process #include files",
"source" : "C/C++ source/header",
- "prefix": "Strip prefix from identifiers",
+ "prefix": "strip prefix from identifiers",
"stub": "stub out undefined type references as objects",
- "suffix": "Strip suffix from identifiers",
+ "suffix": "strip suffix from identifiers",
"symOverride": "skip generating specified symbols"
}, short = {
"check": 'k',
@@ -214,6 +216,7 @@ when isMainModule:
"defines": 'D',
"dynlib": 'l',
"feature": 'f',
+ "includeHeader": 'H',
"includeDirs": 'I',
"nocomments": 'c',
"output": 'o',
diff --git a/tests/include/test2.hpp b/tests/include/test2.hpp
index 0fd90ee..2f9a281 100644
--- a/tests/include/test2.hpp
+++ b/tests/include/test2.hpp
@@ -4,11 +4,13 @@
#define TEST_FLOAT 5.12
#define TEST_HEX 0x512
-int test_call_int();
+extern "C" {
+ int test_call_int();
-struct Foo{
- int bar;
-};
+ struct Foo{
+ int bar;
+ };
+}
class Foo1{
int bar1;