marzer / tomlplusplus

Header-only TOML config file parser and serializer for C++17.
https://marzer.github.io/tomlplusplus/
MIT License
1.59k stars 152 forks source link

Warnings when compiling with -ffinite-math-only on Linux with Clang v19.1 #241

Open theakman2 opened 2 weeks ago

theakman2 commented 2 weeks ago

You can run the following command on the generated all-in-one toml.hpp file using Clang 19.1:

$ clang++ -ffinite-math-only -fsyntax-only toml.hpp

When compiling, you get the following warnings:

a@a:/nt$ clang++ -ffinite-math-only -fsyntax-only toml.hpp
/nt/vendor/toml++/toml.hpp:13012:30: warning: use of infinity is undefined behavior due to the currently enabled floating-point options [-Wnan-infinity-disabled]
 13012 |                         return inf ? (negative ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity())
       |                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/nt/vendor/toml++/toml.hpp:13012:72: warning: use of infinity is undefined behavior due to the currently enabled floating-point options [-Wnan-infinity-disabled]
 13012 |                         return inf ? (negative ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity())
       |                                                                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 warnings generated.

Ideally, the parser should generate a parse error on encountering an Inf/NaN when compiling with -ffinite-math-only.

theakman2 commented 2 weeks ago

I started fixing the issue, but it looks like a lot of unit tests need to be updated to check whether finite-math-only is enabled. Unfortunately I don't have enough time to make these changes, but hopefully my fork is helpful if you decide to address this.

marzer commented 2 weeks ago

Ideally, the parser should generate a parse error on encountering an Inf/NaN when compiling with -ffinite-math-only.

Hmm, maybe. The problem with doing this is you can no-longer round-trip a config document that contains NaN/Inf if you all you're doing is modifying some other non-float field.

The TOML spec mandates that floating points are IEEE 754 format, and the library is already capable of detecting these regardless of the compilers attitude towards them (ref). All that's missing here is the other side of the round-trip.