4 ms·
Compiler Warnings
- deogeo 8y ago> If a warning message is considered not fixable, or not desirable to fix, it’s preferable to remove the associated flag from the build chain. I like to use diagnostic pragmas (https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html) in the vanishingly rare cases where I can't fix the cause of a warning.
- berti 8y agoThis unfortunately gets messy when you're targeting multiple compilers, and in some cases multiple versions of the same compiler.
- deogeo 8y agoAt least clang has it covered: https://clang.llvm.org/docs/UsersManual.html#pragma-gcc-diagnostic https://clang.llvm.org/docs/UsersManual.html#pragma-gcc-diag... Don't know what the situation is on MS's and Intel's compilers.
- raptorfactor 8y agoThe same functionality is available: https://docs.microsoft.com/en-us/cpp/preprocessor/warning?view=vs-2017 https://docs.microsoft.com/en-us/cpp/preprocessor/warning?vi...
- MaulingMonkey 8y agoAnd you can use _Pragma / __pragma to hide them inside (conditional) macros without #ifdefing up every call site. There are still edge cases where you can't sanely scope them relating to when templates are evaluated, being unable to use the pragmas at arbitrary points of an expression, warnings that don't respect the warning flags properly, etc. - but it works most of the time. That said, for warnings with high false positives and low value true positives, I'll just globally suppress the warning.
- loeg 8y agoWorks well if your only targets are GCC and Clang. For building across several platforms with other compilers, you could just manipulate the flags globally on those other compilers without giving up high levels and specificity of warnings on gcc/clang.
- kazinator 8y ago> -Wdeclaration-after-statement : this flag is useful for C90 compatibility. This option is rather for developers who want to avoid declarations after statements in C dialects that support them. If you want to enforce actual C90 compatibility use -ansi or -std=c90. Otherwise you just have partial compatibility which is actually incompatibility.
- loeg 8y agoSome context: the author is Yann Collet, the principal author of zstd, lz4, and xxHash. In my experience he is an extremely talented and friendly engineer.
- olliej 8y agoAll of this seems fine, except for the floating point but. Floating point is completely deterministic, and has a very clearly defined set of behaviours.
- wyattpeak 8y agoI don't know much about the C standard, but the article states that implementation details are platform-specific. If that's the case, it's a very sensible suggestion. Sure, if the same system runs the same operation twice, the two values will be equal. But what if you're comparing to a stored value generated on a different system?
- gizmo686 8y agoIt doesn't even need to be a different system. Different compilations of the same program on the same system can result in different floating point results (either because of internal high-precision floating point registers being utilized differently, or the compiler making algebraically valid re-arrangements that change the floating point result). Additionally, many usages of == assume basic algebraic properties of numbers to hold. It is possible to use equality with floats, but require far more care than is typically worth it.
- MaulingMonkey 8y agoOne option is to have an ExactlyEquals function or macro that wraps the == in pragmas to supress the warning for the scope. At work some of our types don't even define == just to force the issue.
- olliej 8y agoIf the optimization level changes the behaviour the compiler is broken. “Optimizing” by assuming commutivity or associativity of floating point is no less incorrect than optimizing by assuming string addition was commutativeand associative.
- 8y ago