summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2020-05-05 00:48:13 +0200
committerGitHub <noreply@github.com>2020-05-05 00:48:13 +0200
commit64e839d5fdb0a334c516f107cab91da14f7c3251 (patch)
tree159c19c4a56fc9169057e11d667a7efce7cd14a4
parente86a6d24d5ffe43e9dc7f230b80c274167376225 (diff)
downloadNim-64e839d5fdb0a334c516f107cab91da14f7c3251.tar.gz
fixes #14209 [backport:1.2] (#14213)
* fixes #14209 [backport:1.2]
* improve stability
-rw-r--r--compiler/extccomp.nim28
-rw-r--r--compiler/main.nim3
-rw-r--r--lib/system/integerops.nim2
3 files changed, 32 insertions, 1 deletions
diff --git a/compiler/extccomp.nim b/compiler/extccomp.nim
index 0faf3230b..fe48093dd 100644
--- a/compiler/extccomp.nim
+++ b/compiler/extccomp.nim
@@ -613,6 +613,34 @@ proc getCompilerExe(conf: ConfigRef; compiler: TSystemCC; cfile: AbsoluteFile):
       "Compiler '$1' doesn't support the requested target" %
       CC[compiler].name)
 
+proc ccHasSaneOverflow*(conf: ConfigRef): bool =
+  if conf.cCompiler == ccGcc:
+    result = false # assume an old or crappy GCC
+    var exe = getConfigVar(conf, conf.cCompiler, ".exe")
+    if exe.len == 0: exe = CC[conf.cCompiler].compilerExe
+    let (s, exitCode) = try: execCmdEx(exe & " --version") except: ("", 1)
+    if exitCode == 0:
+      var i = 0
+      var j = 0
+      # the version is the last part of the first line:
+      while i < s.len and s[i] != '\n':
+        if s[i] in {' ', '\t'}: j = i+1
+        inc i
+      if j > 0:
+        var major = 0
+        while j < s.len and s[j] in {'0'..'9'}:
+          major = major * 10 + (ord(s[j]) - ord('0'))
+          inc j
+        if i < s.len and s[j] == '.': inc j
+        while j < s.len and s[j] in {'0'..'9'}:
+          inc j
+        if j+1 < s.len and s[j] == '.' and s[j+1] in {'0'..'9'}:
+          # we found a third version number, chances are high
+          # we really parsed the version:
+          result = major >= 5
+  else:
+    result = conf.cCompiler == ccCLang
+
 proc getLinkerExe(conf: ConfigRef; compiler: TSystemCC): string =
   result = if CC[compiler].linkerExe.len > 0: CC[compiler].linkerExe
            elif optMixedMode in conf.globalOptions and conf.cmd != cmdCompileToCpp: CC[compiler].cppCompiler
diff --git a/compiler/main.nim b/compiler/main.nim
index a609d26bc..c94af24d5 100644
--- a/compiler/main.nim
+++ b/compiler/main.nim
@@ -95,6 +95,9 @@ proc commandCompileToC(graph: ModuleGraph) =
       graph.config.notes = graph.config.mainPackageNotes
       return
 
+  if not extccomp.ccHasSaneOverflow(conf):
+    conf.symbols.defineSymbol("nimEmulateOverflowChecks")
+
   compileProject(graph)
   if graph.config.errorCounter > 0:
     return # issue #9933
diff --git a/lib/system/integerops.nim b/lib/system/integerops.nim
index ff744edde..4ef3594f1 100644
--- a/lib/system/integerops.nim
+++ b/lib/system/integerops.nim
@@ -19,7 +19,7 @@ proc raiseDivByZero {.compilerproc, noinline.} =
 
 {.pragma: nimbaseH, importc, nodecl, noSideEffect, compilerproc.}
 
-when (defined(gcc) or defined(clang) or defined(zig)) and not defined(nimEmulateOverflowChecks):
+when not defined(nimEmulateOverflowChecks):
   # take the #define from nimbase.h
 
   proc nimAddInt(a, b: int, res: ptr int): bool {.nimbaseH.}