Skip to content

Commit 6a81cc7

Browse files
author
Andy C
committed
[translation] Avoid name conflict with C macros isinf(), isnan()
This is issue #2326 Moving the functions to mylib should also help with translation to other languages. Unrelated: [metrics] Compare against nosouffle build
1 parent 3edd658 commit 6a81cc7

File tree

9 files changed

+32
-31
lines changed

9 files changed

+32
-31
lines changed

build/doc.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ code:not(pre code) {
307307
}
308308
</style>
309309
310-
This doc mirrors the `--help` for the 3 shell tools in the build sytsem:
310+
This doc mirrors the `--help` for the 3 shell tools in the build system:
311311
312312
1. `configure` - Detect system features
313313
1. `_build/oils.sh` - Compile `oils-for-unix` source into an executable

cpp/stdlib.cc

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,6 @@
2020

2121
using error::e_die;
2222

23-
namespace math {
24-
25-
bool isinf(double f) {
26-
return ::isinf(f);
27-
}
28-
29-
bool isnan(double f) {
30-
return ::isnan(f);
31-
}
32-
33-
} // namespace math
34-
3523
namespace fcntl_ {
3624

3725
int fcntl(int fd, int cmd) {

cpp/stdlib.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@
99

1010
#include "mycpp/runtime.h"
1111

12-
namespace math {
13-
14-
bool isinf(double f);
15-
bool isnan(double f);
16-
17-
} // namespace math
18-
1912
namespace fcntl_ {
2013

2114
// for F_GETFD

data_lang/j8.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
- NIL8 at least has no commas for [1 2 "hi"]
2424
"""
2525

26-
import math
27-
2826
from _devbuild.gen.id_kind_asdl import Id, Id_t, Id_str
2927
from _devbuild.gen.nil8_asdl import (nvalue, nvalue_t)
3028
from _devbuild.gen.runtime_asdl import error_code_e
@@ -38,7 +36,7 @@
3836
from frontend import match
3937
from mycpp import mops
4038
from mycpp import mylib
41-
from mycpp.mylib import tagswitch, iteritems, NewDict, log
39+
from mycpp.mylib import tagswitch, iteritems, NewDict, log, isinf_, isnan_
4240

4341
import fastfunc
4442

@@ -500,14 +498,14 @@ def Print(self, val, level=0):
500498
val = cast(value.Float, UP_val)
501499

502500
fl = val.f
503-
if math.isinf(fl):
501+
if isinf_(fl):
504502
if self.options & INF_NAN_ARE_NULL:
505503
s = 'null' # negative infinity is null too
506504
else:
507505
s = 'INFINITY'
508506
if fl < 0:
509507
s = '-' + s
510-
elif math.isnan(fl):
508+
elif isnan_(fl):
511509
if self.options & INF_NAN_ARE_NULL:
512510
# JavaScript JSON lib behavior: Inf and NaN are null
513511
# Python has a bug in the encoder by default, and then

display/pp_value.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
Render Oils value_t -> doc_t, so it can be pretty printed
55
"""
66

7-
import math
8-
97
from _devbuild.gen.pretty_asdl import (doc, Measure, MeasuredDoc)
108
from _devbuild.gen.runtime_asdl import error_code_e
119
from _devbuild.gen.value_asdl import Obj, value, value_e, value_t, value_str
@@ -17,7 +15,7 @@
1715
from display.pretty import _Break, _Concat, AsciiText
1816
from frontend import match
1917
from mycpp import mops
20-
from mycpp.mylib import log, tagswitch, iteritems
18+
from mycpp.mylib import log, tagswitch, iteritems, isinf_, isnan_
2119
from typing import cast, List, Dict
2220

2321
import libc
@@ -35,11 +33,11 @@ def FloatString(fl):
3533
# type: (float) -> str
3634

3735
# Print in YSH syntax, similar to data_lang/j8.py
38-
if math.isinf(fl):
36+
if isinf_(fl):
3937
s = 'INFINITY'
4038
if fl < 0:
4139
s = '-' + s
42-
elif math.isnan(fl):
40+
elif isnan_(fl):
4341
s = 'NAN'
4442
else:
4543
s = str(fl)

metrics/native-code.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ compare-gcc-clang() {
126126

127127
local -a targets=(
128128
_bin/{clang,cxx}-opt/bin/oils_for_unix.mycpp.stripped
129-
_bin/{clang,cxx}-opt/bin/oils_for_unix.mycpp-souffle.stripped
129+
_bin/{clang,cxx}-opt/bin/oils_for_unix.mycpp-nosouffle.stripped
130130
_bin/cxx-{opt+bumpleak,opt+bumproot,opt+bigint}/bin/oils_for_unix.mycpp.stripped
131131
_bin/{clang,cxx}-opt/yaks/yaks_main.mycpp.stripped
132132
#_bin/cxx-{opt+bumpleak,opt+bumproot}/yaks/yaks_main.mycpp.stripped

mycpp/gc_mylib.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
#include "mycpp/gc_mylib.h"
22

33
#include <errno.h>
4+
#include <math.h>
45
#include <stdio.h>
56
#include <unistd.h> // isatty
67

78
#include "mycpp/gc_iolib.h"
89

910
namespace mylib {
1011

12+
bool isinf_(double f) {
13+
return ::isinf(f);
14+
}
15+
16+
bool isnan_(double f) {
17+
return ::isnan(f);
18+
}
19+
1120
void InitCppOnly() {
1221
// We don't seem need this now that we have ctx_FlushStdout().
1322
// setvbuf(stdout, 0, _IONBF, 0);

mycpp/gc_mylib.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class Dict;
1313

1414
namespace mylib {
1515

16+
bool isinf_(double f);
17+
bool isnan_(double f);
18+
1619
void InitCppOnly();
1720

1821
// Wrappers around our C++ APIs

mycpp/mylib.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
cStringIO = None
1313
import io
1414

15+
import math
1516
import sys
1617

1718
from pylib import collections_
@@ -35,6 +36,17 @@
3536
STDIN_FILENO = 0
3637

3738

39+
# Avoid name conflicts with C Macros
40+
def isinf_(x):
41+
# type: (float) -> bool
42+
return math.isinf(x)
43+
44+
45+
def isnan_(x):
46+
# type: (float) -> bool
47+
return math.isnan(x)
48+
49+
3850
def MaybeCollect():
3951
# type: () -> None
4052
pass

0 commit comments

Comments
 (0)