summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorReimer Behrends <behrends@gmail.com>2014-09-19 03:34:00 +0200
committerReimer Behrends <behrends@gmail.com>2014-09-19 03:34:00 +0200
commit80356f1cc76250706f23daa0c729c91127c4812b (patch)
tree9156bd8ecaee0b7bf2b67eb4eaa067f6c3a7c9c6 /compiler
parentd2b7aed229b3a347b614c7ea0f3c1f29513e1c08 (diff)
downloadNim-80356f1cc76250706f23daa0c729c91127c4812b.tar.gz
Avoid unnecessary #include triggered by importc vars.
When a C variable or macro is imported via an {.importc.} var or
let statement, but no definition is needed and the variable does
not have an initializer part, then there is also no need to generate
an #include for the associated header until and unless the variable
is actually used. The header is already generated upon use, but
unnecessarily also when the variable is defined.

This is an issue with the posix module in particular, where a lot
of unnecessary header files are being included because relevant
constants are defined via importc vars, and those header files
may not even be available on a given system.

This patch omits the generation of the #include directive for those
definitions where they aren't needed.
Diffstat (limited to 'compiler')
-rw-r--r--compiler/ccgstmts.nim4
1 files changed, 4 insertions, 0 deletions
diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim
index 3004e353b..cb9eebb32 100644
--- a/compiler/ccgstmts.nim
+++ b/compiler/ccgstmts.nim
@@ -171,6 +171,10 @@ proc genSingleVar(p: BProc, a: PNode) =
   if sfCompileTime in v.flags: return
   var targetProc = p
   if sfGlobal in v.flags:
+    if v.flags * {sfImportc, sfExportc} == {sfImportc} and
+        a.sons[2].kind == nkEmpty and
+        v.loc.flags * {lfHeader, lfNoDecl} != {}:
+      return
     if sfPure in v.flags:
       # v.owner.kind != skModule:
       targetProc = p.module.preInitProc