From 529879c78ae4c1bf4d04110b543f37c1d1af20f7 Mon Sep 17 00:00:00 2001 From: Ammarguellat Date: Thu, 1 Feb 2024 05:26:00 -0800 Subject: [PATCH 1/6] Fix INF/NAN warning. --- .../clang/Basic/DiagnosticCommonKinds.td | 4 +- clang/include/clang/Lex/Preprocessor.h | 21 ++- clang/lib/Lex/PPDirectives.cpp | 2 +- clang/lib/Lex/Preprocessor.cpp | 6 +- clang/lib/Sema/SemaChecking.cpp | 10 +- .../Sema/warn-infinity-nan-disabled-lnx.cpp | 172 +++++++++++------- .../Sema/warn-infinity-nan-disabled-win.cpp | 171 ++++++++++------- 7 files changed, 241 insertions(+), 145 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticCommonKinds.td b/clang/include/clang/Basic/DiagnosticCommonKinds.td index b1bada65cb6b2..ccae70a0fdbac 100644 --- a/clang/include/clang/Basic/DiagnosticCommonKinds.td +++ b/clang/include/clang/Basic/DiagnosticCommonKinds.td @@ -72,7 +72,9 @@ def warn_pragma_debug_unexpected_argument : Warning< def warn_fp_nan_inf_when_disabled : Warning< "use of %select{infinity|NaN}0%select{| via a macro}1 is undefined behavior " - "due to the currently enabled floating-point options">, + "due to the currently enabled floating-point options%select{|; mix of safe " + "and unsafe math options are used. Check the order of you command line " + "arguments}2">, InGroup>; } diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index 9d0d53129a12d..dac5a46f73749 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -2838,7 +2838,8 @@ class Preprocessor { return AnnotationInfos.find(II)->second; } - void emitMacroExpansionWarnings(const Token &Identifier) const { + void emitMacroExpansionWarnings(const Token &Identifier, + bool IsIfnDef = false) const { IdentifierInfo *Info = Identifier.getIdentifierInfo(); if (Info->isDeprecatedMacro()) emitMacroDeprecationWarning(Identifier); @@ -2847,12 +2848,15 @@ class Preprocessor { !SourceMgr.isInMainFile(Identifier.getLocation())) emitRestrictExpansionWarning(Identifier); - if (Info->getName() == "INFINITY") - if (getLangOpts().NoHonorInfs) - emitRestrictInfNaNWarning(Identifier, 0); - if (Info->getName() == "NAN") - if (getLangOpts().NoHonorNaNs) - emitRestrictInfNaNWarning(Identifier, 1); + if (!IsIfnDef) { + bool UnsafeMath = getLangOpts().UnsafeFPMath; + if (Info->getName() == "INFINITY") + if (getLangOpts().NoHonorInfs) + emitRestrictInfNaNWarning(Identifier, 0, UnsafeMath); + if (Info->getName() == "NAN") + if (getLangOpts().NoHonorNaNs) + emitRestrictInfNaNWarning(Identifier, 1, UnsafeMath); + } } static void processPathForFileMacro(SmallVectorImpl &Path, @@ -2869,7 +2873,8 @@ class Preprocessor { void emitRestrictExpansionWarning(const Token &Identifier) const; void emitFinalMacroWarning(const Token &Identifier, bool IsUndef) const; void emitRestrictInfNaNWarning(const Token &Identifier, - unsigned DiagSelection) const; + unsigned DiagSelection, + bool UnsafeMath) const; /// This boolean state keeps track if the current scanned token (by this PP) /// is in an "-Wunsafe-buffer-usage" opt-out region. Assuming PP scans a diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 9f82a6d073e3b..17a59677e2de4 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -3288,7 +3288,7 @@ void Preprocessor::HandleIfdefDirective(Token &Result, return; } - emitMacroExpansionWarnings(MacroNameTok); + emitMacroExpansionWarnings(MacroNameTok, true); // Check to see if this is the last token on the #if[n]def line. CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp index 031ed1e16bb8f..635d052070146 100644 --- a/clang/lib/Lex/Preprocessor.cpp +++ b/clang/lib/Lex/Preprocessor.cpp @@ -1466,8 +1466,10 @@ void Preprocessor::emitRestrictExpansionWarning(const Token &Identifier) const { } void Preprocessor::emitRestrictInfNaNWarning(const Token &Identifier, - unsigned DiagSelection) const { - Diag(Identifier, diag::warn_fp_nan_inf_when_disabled) << DiagSelection << 1; + unsigned DiagSelection, + bool UnsafeMath) const { + Diag(Identifier, diag::warn_fp_nan_inf_when_disabled) + << DiagSelection << 1 << UnsafeMath; } void Preprocessor::emitFinalMacroWarning(const Token &Identifier, diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 7b877da1a928f..62a0dbd6d7ea2 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -9132,7 +9132,7 @@ bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall, unsigned BuiltinID) { if (BuiltinID == Builtin::BI__builtin_isunordered && TheCall->getFPFeaturesInEffect(getLangOpts()).getNoHonorNaNs()) Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) - << 1 << 0 << TheCall->getSourceRange(); + << 1 << 0 << 0; ExprResult OrigArg0 = TheCall->getArg(0); ExprResult OrigArg1 = TheCall->getArg(1); @@ -9178,12 +9178,12 @@ bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs, BuiltinID == Builtin::BI__builtin_isinf || BuiltinID == Builtin::BI__builtin_isinf_sign)) Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) - << 0 << 0 << TheCall->getSourceRange(); + << 0 << 0 << 0; if (FPO.getNoHonorNaNs() && (BuiltinID == Builtin::BI__builtin_isnan || BuiltinID == Builtin::BI__builtin_isunordered)) Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) - << 1 << 0 << TheCall->getSourceRange(); + << 1 << 0 << 0; bool IsFPClass = NumArgs == 2; @@ -12938,13 +12938,13 @@ void Sema::CheckInfNaNFunction(const CallExpr *Call, (Call->getBuiltinCallee() == Builtin::BI__builtin_nanf)) && FPO.getNoHonorNaNs()) Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) - << 1 << 0 << Call->getSourceRange(); + << 1 << 0 << 0; else if ((IsStdFunction(FDecl, "isinf") || (IsStdFunction(FDecl, "isfinite") || (FDecl->getIdentifier() && FDecl->getName() == "infinity"))) && FPO.getNoHonorInfs()) Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) - << 0 << 0 << Call->getSourceRange(); + << 0 << 0 << 0; } // Warn when using the wrong abs() function. diff --git a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp index 8a610fa0e737e..be29f38e3b43e 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp +++ b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp @@ -9,8 +9,12 @@ // RUN: %clang_cc1 -x c++ -verify=no-nan -triple powerpc64le-unknown-unknown %s \ // RUN: -menable-no-nans +// RUN: %clang_cc1 -x c++ -verify=no-inf-no-nan-mix-mode -triple powerpc64le-unknown-unknown %s \ +// RUN: -menable-no-infs -menable-no-nans -funsafe-math-optimizations + // no-fast-no-diagnostics + int isunorderedf (float x, float y); extern "C++" { namespace std __attribute__((__visibility__("default"))) { @@ -69,108 +73,148 @@ class numeric_limits { int compareit(float a, float b) { volatile int i, j, k, l, m, n, o, p; -// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} i = a == INFINITY; -// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} j = INFINITY == a; -// no-inf-no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+6 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+5 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} i = a == NAN; -// no-inf-no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+5 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} j = NAN == a; -// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} j = INFINITY <= a; -// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} j = INFINITY < a; -// no-inf-no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+5 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} j = a > NAN; -// no-inf-no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+5 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} j = a >= NAN; -// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} k = std::isinf(a); -// no-inf-no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point option}} -// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point option}} +// no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} l = std::isnan(a); -// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} o = std::isfinite(a); -// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} m = __builtin_isinf(a); -// no-inf-no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} n = __builtin_isnan(a); -// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} p = __builtin_isfinite(a); - // These should NOT warn, since they are not using NaN or infinity. - j = a > 1.1; - j = b < 1.1; - j = a >= 1.1; - j = b <= 1.1; - j = isunorderedf(a, b); - -// no-inf-no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+5 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} j = isunorderedf(a, NAN); -// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} j = isunorderedf(a, INFINITY); -// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+5 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+9 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+8 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+7 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+5 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} i = std::isunordered(a, NAN); -// no-inf-no-nan-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+7 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+6 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+5 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-mix-mode-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} i = std::isunordered(a, INFINITY); -// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} double y = i * numeric_limits::infinity(); -// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} j = numeric_limits::infinity(); - return 0; + // These should NOT warn, since they are not using NaN or infinity. + j = a > 1.1; + j = b < 1.1; + j = a >= 1.1; + j = b <= 1.1; + j = isunorderedf(a, b); +#ifndef INFINITY + j = a; +#endif +#ifndef NAN + j = b; +#endif +#ifdef INFINITY + j = a; +#endif +#ifdef NAN + j = b; +#endif + return 0; } diff --git a/clang/test/Sema/warn-infinity-nan-disabled-win.cpp b/clang/test/Sema/warn-infinity-nan-disabled-win.cpp index 19a575386e329..31fb55635af6e 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-win.cpp +++ b/clang/test/Sema/warn-infinity-nan-disabled-win.cpp @@ -12,6 +12,9 @@ // RUN: %clang_cc1 -x c++ -verify=no-nan -triple powerpc64le-unknown-unknown %s \ // RUN: -menable-no-nans +// RUN: %clang_cc1 -x c++ -verify=no-inf-no-nan-mix-mode -triple powerpc64le-unknown-unknown %s \ +// RUN: -menable-no-infs -menable-no-nans -funsafe-math-optimizations + // no-fast-no-diagnostics int isunorderedf (float x, float y); @@ -72,108 +75,148 @@ class numeric_limits { int compareit(float a, float b) { volatile int i, j, k, l, m, n, o, p; -// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} i = a == INFINITY; -// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} j = INFINITY == a; -// no-inf-no-nan-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+6 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+5 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} i = a == NAN; -// no-inf-no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+5 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} j = NAN == a; -// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} j = INFINITY <= a; -// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} - j = INFINITY < a; - -// no-inf-no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} // no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} // no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} + j = INFINITY < a; + +// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+5 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} j = a > NAN; -// no-inf-no-nan-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+6 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+5 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} j = a >= NAN; -// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} k = std::isinf(a); -// no-inf-no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} l = std::isnan(a); -// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} o = std::isfinite(a); -// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} m = __builtin_isinf(a); -// no-inf-no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} n = __builtin_isnan(a); -// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} p = __builtin_isfinite(a); - // These should NOT warn, since they are not using NaN or infinity. - j = a > 1.1; - j = b < 1.1; - j = a >= 1.1; - j = b <= 1.1; - j = isunorderedf(a, b); - -// no-inf-no-nan-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point option}} -// no-inf-no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+6 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point option}} +// no-inf-no-nan-warning@+5 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+2{{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} j = isunorderedf(a, NAN); -// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} j = isunorderedf(a, INFINITY); -// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+5 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+9 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+8 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+7 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+6 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+5 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +//no-inf-no-nan-mix-mode-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} i = std::isunordered(a, NAN); -// no-inf-no-nan-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+6 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+5 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +//no-inf-no-nan-mix-mode-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} i = std::isunordered(a, INFINITY); -// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} double y = i * numeric_limits::infinity(); -// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} j = numeric_limits::infinity(); - return 0; + // These should NOT warn, since they are not using NaN or infinity. + j = a > 1.1; + j = b < 1.1; + j = a >= 1.1; + j = b <= 1.1; + j = isunorderedf(a, b); + +#ifndef INFINITY + j = a; +#endif +#ifndef NAN + j = b; +#endif +#ifdef INFINITY + j = a; +#endif +#ifdef NAN + j = b; +#endif + return 0; } From 0deb2b61c8669053693dd8750f4a2d21abee76a3 Mon Sep 17 00:00:00 2001 From: Ammarguellat Date: Thu, 1 Feb 2024 05:48:19 -0800 Subject: [PATCH 2/6] Fix format. --- clang/include/clang/Lex/Preprocessor.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index dac5a46f73749..de5597cc7f6fa 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -2873,8 +2873,7 @@ class Preprocessor { void emitRestrictExpansionWarning(const Token &Identifier) const; void emitFinalMacroWarning(const Token &Identifier, bool IsUndef) const; void emitRestrictInfNaNWarning(const Token &Identifier, - unsigned DiagSelection, - bool UnsafeMath) const; + unsigned DiagSelection, bool UnsafeMath) const; /// This boolean state keeps track if the current scanned token (by this PP) /// is in an "-Wunsafe-buffer-usage" opt-out region. Assuming PP scans a From d33ec0e039a78c10eac9f751534ae15f29922c64 Mon Sep 17 00:00:00 2001 From: Ammarguellat Date: Thu, 1 Feb 2024 12:29:09 -0800 Subject: [PATCH 3/6] Addressed review comments. --- .../clang/Basic/DiagnosticCommonKinds.td | 6 +- clang/include/clang/Basic/DiagnosticDocs.td | 7 + clang/include/clang/Lex/Preprocessor.h | 13 +- clang/lib/Lex/PPDirectives.cpp | 2 +- clang/lib/Lex/PPExpressions.cpp | 4 +- clang/lib/Lex/Preprocessor.cpp | 6 +- clang/lib/Sema/SemaChecking.cpp | 10 +- .../Sema/warn-infinity-nan-disabled-lnx.cpp | 196 ++++++++---------- .../Sema/warn-infinity-nan-disabled-win.cpp | 193 ++++++++--------- 9 files changed, 200 insertions(+), 237 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticCommonKinds.td b/clang/include/clang/Basic/DiagnosticCommonKinds.td index ccae70a0fdbac..a9779992a49c2 100644 --- a/clang/include/clang/Basic/DiagnosticCommonKinds.td +++ b/clang/include/clang/Basic/DiagnosticCommonKinds.td @@ -72,10 +72,8 @@ def warn_pragma_debug_unexpected_argument : Warning< def warn_fp_nan_inf_when_disabled : Warning< "use of %select{infinity|NaN}0%select{| via a macro}1 is undefined behavior " - "due to the currently enabled floating-point options%select{|; mix of safe " - "and unsafe math options are used. Check the order of you command line " - "arguments}2">, - InGroup>; + "due to the currently enabled floating-point options">, + InGroup>; } // Parse && Sema diff --git a/clang/include/clang/Basic/DiagnosticDocs.td b/clang/include/clang/Basic/DiagnosticDocs.td index e9862422b4997..df153bae4567d 100644 --- a/clang/include/clang/Basic/DiagnosticDocs.td +++ b/clang/include/clang/Basic/DiagnosticDocs.td @@ -87,3 +87,10 @@ program by treating all string literals as having type ``const char *`` instead of ``char *``. This can cause unexpected behaviors with type-sensitive constructs like ``_Generic``. }]; + +defvar NanInfDisabledDocs = [{ +This warning is enabled when source code using the macros INFINITY and/or NAN +is compiled with floating-point options preventing these two values. This can +lead to undefined behavior. Some command line combinations order and pragmas +may have an impact and a warning can be generated when not expected. +}]; diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index de5597cc7f6fa..0836b7d439bb0 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -2849,13 +2849,10 @@ class Preprocessor { emitRestrictExpansionWarning(Identifier); if (!IsIfnDef) { - bool UnsafeMath = getLangOpts().UnsafeFPMath; - if (Info->getName() == "INFINITY") - if (getLangOpts().NoHonorInfs) - emitRestrictInfNaNWarning(Identifier, 0, UnsafeMath); - if (Info->getName() == "NAN") - if (getLangOpts().NoHonorNaNs) - emitRestrictInfNaNWarning(Identifier, 1, UnsafeMath); + if (Info->getName() == "INFINITY" && getLangOpts().NoHonorInfs) + emitRestrictInfNaNWarning(Identifier, 0); + if (Info->getName() == "NAN" && getLangOpts().NoHonorNaNs) + emitRestrictInfNaNWarning(Identifier, 1); } } @@ -2873,7 +2870,7 @@ class Preprocessor { void emitRestrictExpansionWarning(const Token &Identifier) const; void emitFinalMacroWarning(const Token &Identifier, bool IsUndef) const; void emitRestrictInfNaNWarning(const Token &Identifier, - unsigned DiagSelection, bool UnsafeMath) const; + unsigned DiagSelection) const; /// This boolean state keeps track if the current scanned token (by this PP) /// is in an "-Wunsafe-buffer-usage" opt-out region. Assuming PP scans a diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 17a59677e2de4..a980f4bcbae12 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -3288,7 +3288,7 @@ void Preprocessor::HandleIfdefDirective(Token &Result, return; } - emitMacroExpansionWarnings(MacroNameTok, true); + emitMacroExpansionWarnings(MacroNameTok, /*IsIfnDef=*/true); // Check to see if this is the last token on the #if[n]def line. CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); diff --git a/clang/lib/Lex/PPExpressions.cpp b/clang/lib/Lex/PPExpressions.cpp index 1feb0eb18d71e..8f25c67ec9dfb 100644 --- a/clang/lib/Lex/PPExpressions.cpp +++ b/clang/lib/Lex/PPExpressions.cpp @@ -133,7 +133,9 @@ static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT, Result.Val.setIsUnsigned(false); // Result is signed intmax_t. DT.IncludedUndefinedIds = !Macro; - PP.emitMacroExpansionWarnings(PeekTok); + PP.emitMacroExpansionWarnings( + PeekTok, + (II->getName() == "INFINITY" || II->getName() == "NAN") ? true : false); // If there is a macro, mark it used. if (Result.Val != 0 && ValueLive) diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp index 635d052070146..031ed1e16bb8f 100644 --- a/clang/lib/Lex/Preprocessor.cpp +++ b/clang/lib/Lex/Preprocessor.cpp @@ -1466,10 +1466,8 @@ void Preprocessor::emitRestrictExpansionWarning(const Token &Identifier) const { } void Preprocessor::emitRestrictInfNaNWarning(const Token &Identifier, - unsigned DiagSelection, - bool UnsafeMath) const { - Diag(Identifier, diag::warn_fp_nan_inf_when_disabled) - << DiagSelection << 1 << UnsafeMath; + unsigned DiagSelection) const { + Diag(Identifier, diag::warn_fp_nan_inf_when_disabled) << DiagSelection << 1; } void Preprocessor::emitFinalMacroWarning(const Token &Identifier, diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 62a0dbd6d7ea2..7b877da1a928f 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -9132,7 +9132,7 @@ bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall, unsigned BuiltinID) { if (BuiltinID == Builtin::BI__builtin_isunordered && TheCall->getFPFeaturesInEffect(getLangOpts()).getNoHonorNaNs()) Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) - << 1 << 0 << 0; + << 1 << 0 << TheCall->getSourceRange(); ExprResult OrigArg0 = TheCall->getArg(0); ExprResult OrigArg1 = TheCall->getArg(1); @@ -9178,12 +9178,12 @@ bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs, BuiltinID == Builtin::BI__builtin_isinf || BuiltinID == Builtin::BI__builtin_isinf_sign)) Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) - << 0 << 0 << 0; + << 0 << 0 << TheCall->getSourceRange(); if (FPO.getNoHonorNaNs() && (BuiltinID == Builtin::BI__builtin_isnan || BuiltinID == Builtin::BI__builtin_isunordered)) Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) - << 1 << 0 << 0; + << 1 << 0 << TheCall->getSourceRange(); bool IsFPClass = NumArgs == 2; @@ -12938,13 +12938,13 @@ void Sema::CheckInfNaNFunction(const CallExpr *Call, (Call->getBuiltinCallee() == Builtin::BI__builtin_nanf)) && FPO.getNoHonorNaNs()) Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) - << 1 << 0 << 0; + << 1 << 0 << Call->getSourceRange(); else if ((IsStdFunction(FDecl, "isinf") || (IsStdFunction(FDecl, "isfinite") || (FDecl->getIdentifier() && FDecl->getName() == "infinity"))) && FPO.getNoHonorInfs()) Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) - << 0 << 0 << 0; + << 0 << 0 << Call->getSourceRange(); } // Warn when using the wrong abs() function. diff --git a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp index be29f38e3b43e..67a2611374a13 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp +++ b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp @@ -1,20 +1,22 @@ -// RUN: %clang_cc1 -x c++ -verify=no-inf-no-nan -triple powerpc64le-unknown-unknown %s \ -// RUN: -menable-no-infs -menable-no-nans +// RUN: %clang_cc1 -x c++ -verify=no-inf-no-nan \ +// RUN: -triple powerpc64le-unknown-unknown %s -menable-no-infs \ +// RUN: -menable-no-nans -std=c++23 -// RUN: %clang_cc1 -x c++ -verify=no-fast -triple powerpc64le-unknown-unknown %s +// RUN: %clang_cc1 -x c++ -verify=no-inf-no-nan \ +// RUN: -triple powerpc64le-unknown-unknown %s -menable-no-infs \ +// RUN: -menable-no-nans -funsafe-math-optimizations -std=c++23 + +// RUN: %clang_cc1 -x c++ -verify=no-fast -triple powerpc64le-unknown-unknown \ +// RUN: %s -std=c++23 // RUN: %clang_cc1 -x c++ -verify=no-inf -triple powerpc64le-unknown-unknown %s \ -// RUN: -menable-no-infs +// RUN: -menable-no-infs -std=c++23 // RUN: %clang_cc1 -x c++ -verify=no-nan -triple powerpc64le-unknown-unknown %s \ -// RUN: -menable-no-nans - -// RUN: %clang_cc1 -x c++ -verify=no-inf-no-nan-mix-mode -triple powerpc64le-unknown-unknown %s \ -// RUN: -menable-no-infs -menable-no-nans -funsafe-math-optimizations +// RUN: -menable-no-nans -std=c++23 // no-fast-no-diagnostics - int isunorderedf (float x, float y); extern "C++" { namespace std __attribute__((__visibility__("default"))) { @@ -73,137 +75,77 @@ class numeric_limits { int compareit(float a, float b) { volatile int i, j, k, l, m, n, o, p; -// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} i = a == INFINITY; -// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} j = INFINITY == a; -// no-inf-no-nan-warning@+6 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+5 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} i = a == NAN; -// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+5 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} j = NAN == a; -// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} j = INFINITY <= a; -// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} j = INFINITY < a; -// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+5 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} j = a > NAN; -// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+5 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} j = a >= NAN; -// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} k = std::isinf(a); -// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point option}} -// no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point option}} +// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} l = std::isnan(a); -// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} o = std::isfinite(a); -// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} m = __builtin_isinf(a); -// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} n = __builtin_isnan(a); -// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} p = __builtin_isfinite(a); -// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+5 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} - j = isunorderedf(a, NAN); - -// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} - j = isunorderedf(a, INFINITY); - -// no-inf-no-nan-warning@+9 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+8 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+7 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+5 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} -// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} - i = std::isunordered(a, NAN); - -// no-inf-no-nan-warning@+7 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+6 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+5 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-mix-mode-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} -// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} - i = std::isunordered(a, INFINITY); - -// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} - double y = i * numeric_limits::infinity(); - -// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} - j = numeric_limits::infinity(); - - // These should NOT warn, since they are not using NaN or infinity. +// These should NOT warn, since they are not using NaN or infinity. j = a > 1.1; j = b < 1.1; j = a >= 1.1; j = b <= 1.1; j = isunorderedf(a, b); + #ifndef INFINITY j = a; #endif @@ -216,5 +158,43 @@ int compareit(float a, float b) { #ifdef NAN j = b; #endif +#if defined(INFINITY) + j = a; +#elifndef(INFINITY) + j = b; +#endif + +// no-inf-no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} + j = isunorderedf(a, NAN); + +// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} + j = isunorderedf(a, INFINITY); + +// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+5 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} + i = std::isunordered(a, NAN); + +// no-inf-no-nan-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} + i = std::isunordered(a, INFINITY); + +// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} + double y = i * numeric_limits::infinity(); + +// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} + j = numeric_limits::infinity(); return 0; -} + +} diff --git a/clang/test/Sema/warn-infinity-nan-disabled-win.cpp b/clang/test/Sema/warn-infinity-nan-disabled-win.cpp index 31fb55635af6e..a904cfbd04f75 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-win.cpp +++ b/clang/test/Sema/warn-infinity-nan-disabled-win.cpp @@ -1,19 +1,22 @@ // Use of NAN macro will trigger a warning "infinity defined in macro" because // on Windows the NAN macro is defined using INFINITY. See below. -// RUN: %clang_cc1 -x c++ -verify=no-inf-no-nan -triple powerpc64le-unknown-unknown %s \ -// RUN: -menable-no-infs -menable-no-nans +// RUN: %clang_cc1 -x c++ -verify=no-inf-no-nan \ +// RUN: -triple powerpc64le-unknown-unknown %s -menable-no-infs \ +// RUN: -menable-no-nans -std=c++23 -// RUN: %clang_cc1 -x c++ -verify=no-fast -triple powerpc64le-unknown-unknown %s +// RUN: %clang_cc1 -x c++ -verify=no-inf-no-nan \ +// RUN: -triple powerpc64le-unknown-unknown %s -menable-no-infs \ +// RUN: -menable-no-nans -funsafe-math-optimizations -std=c++23 + +// RUN: %clang_cc1 -x c++ -verify=no-fast -triple powerpc64le-unknown-unknown \ +// RUN: %s -std=c++23 // RUN: %clang_cc1 -x c++ -verify=no-inf -triple powerpc64le-unknown-unknown %s \ -// RUN: -menable-no-infs +// RUN: -menable-no-infs -std=c++23 // RUN: %clang_cc1 -x c++ -verify=no-nan -triple powerpc64le-unknown-unknown %s \ -// RUN: -menable-no-nans - -// RUN: %clang_cc1 -x c++ -verify=no-inf-no-nan-mix-mode -triple powerpc64le-unknown-unknown %s \ -// RUN: -menable-no-infs -menable-no-nans -funsafe-math-optimizations +// RUN: -menable-no-nans -std=c++23 // no-fast-no-diagnostics @@ -75,131 +78,71 @@ class numeric_limits { int compareit(float a, float b) { volatile int i, j, k, l, m, n, o, p; -// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} i = a == INFINITY; -// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} j = INFINITY == a; -// no-inf-no-nan-warning@+6 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+5 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} i = a == NAN; -// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+5 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} - j = NAN == a; - +// no-inf-no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} // no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} // no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} + j = NAN == a; + +// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} j = INFINITY <= a; -// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} j = INFINITY < a; -// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+5 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} j = a > NAN; -// no-inf-no-nan-warning@+6 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+5 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} +// no-inf-no-nan-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} j = a >= NAN; -// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} k = std::isinf(a); -// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} l = std::isnan(a); -// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} o = std::isfinite(a); -// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} m = __builtin_isinf(a); -// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} n = __builtin_isnan(a); -// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} p = __builtin_isfinite(a); -// no-inf-no-nan-warning@+6 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point option}} -// no-inf-no-nan-warning@+5 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+2{{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} - j = isunorderedf(a, NAN); - -// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} - j = isunorderedf(a, INFINITY); - -// no-inf-no-nan-warning@+9 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+8 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+7 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+6 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+5 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} -//no-inf-no-nan-mix-mode-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} - i = std::isunordered(a, NAN); - -// no-inf-no-nan-warning@+6 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-warning@+5 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} -// no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} -//no-inf-no-nan-mix-mode-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options; mix of safe and unsafe math options are used. Check the order of you command line arguments}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} - i = std::isunordered(a, INFINITY); - -// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} - double y = i * numeric_limits::infinity(); - -// no-inf-no-nan-warning@+3 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} -// no-inf-no-nan-mix-mode-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} - j = numeric_limits::infinity(); - - // These should NOT warn, since they are not using NaN or infinity. +// These should NOT warn, since they are not using NaN or infinity. j = a > 1.1; j = b < 1.1; j = a >= 1.1; @@ -218,5 +161,43 @@ int compareit(float a, float b) { #ifdef NAN j = b; #endif +#if defined(INFINITY) + j = a; +#elifndef(INFINITY) + j = b; +#endif + +// no-inf-no-nan-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point option}} +// no-inf-no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} + j = isunorderedf(a, NAN); + +// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} + j = isunorderedf(a, INFINITY); + +// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+5 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} + i = std::isunordered(a, NAN); + +// no-inf-no-nan-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} +// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} + i = std::isunordered(a, INFINITY); + +// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} + double y = i * numeric_limits::infinity(); + +// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} +// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} + j = numeric_limits::infinity(); return 0; -} + +} From ad10f4a4bcc9fd1129a8b08973aed7766b381e22 Mon Sep 17 00:00:00 2001 From: Ammarguellat Date: Mon, 5 Feb 2024 10:57:38 -0800 Subject: [PATCH 4/6] Fixed diag dog comment and added a few lines in the LIT tests. --- clang/include/clang/Basic/DiagnosticCommonKinds.td | 2 +- clang/include/clang/Basic/DiagnosticDocs.td | 8 +++++--- clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp | 10 ++++++++++ clang/test/Sema/warn-infinity-nan-disabled-win.cpp | 10 ++++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticCommonKinds.td b/clang/include/clang/Basic/DiagnosticCommonKinds.td index a9779992a49c2..08bb1d81ba29f 100644 --- a/clang/include/clang/Basic/DiagnosticCommonKinds.td +++ b/clang/include/clang/Basic/DiagnosticCommonKinds.td @@ -73,7 +73,7 @@ def warn_pragma_debug_unexpected_argument : Warning< def warn_fp_nan_inf_when_disabled : Warning< "use of %select{infinity|NaN}0%select{| via a macro}1 is undefined behavior " "due to the currently enabled floating-point options">, - InGroup>; + InGroup>; } // Parse && Sema diff --git a/clang/include/clang/Basic/DiagnosticDocs.td b/clang/include/clang/Basic/DiagnosticDocs.td index df153bae4567d..8c024b5cad740 100644 --- a/clang/include/clang/Basic/DiagnosticDocs.td +++ b/clang/include/clang/Basic/DiagnosticDocs.td @@ -89,8 +89,10 @@ constructs like ``_Generic``. }]; defvar NanInfDisabledDocs = [{ -This warning is enabled when source code using the macros INFINITY and/or NAN +This warning is enabled when source code using the macros ``INFINITY`` or ``NAN`` is compiled with floating-point options preventing these two values. This can -lead to undefined behavior. Some command line combinations order and pragmas -may have an impact and a warning can be generated when not expected. +lead to undefined behavior. Check the order of command line arguments that modify +this behavior, such as ``-ffast-math``, ``-fhonor-infinities``, and +``-fhonor-nans`` (etc), as well as ``#pragma`` directives if this diagnostic is +generated unexpectedly. }]; diff --git a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp index 67a2611374a13..de66176563303 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp +++ b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp @@ -163,6 +163,16 @@ int compareit(float a, float b) { #elifndef(INFINITY) j = b; #endif +#if defined(INFINITY) + j = a; +#elifndef(NAN) + j = b; +#endif +#if defined(NAN) + j = a; +#elifndef(INFINITY) + j = b; +#endif // no-inf-no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} // no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}} diff --git a/clang/test/Sema/warn-infinity-nan-disabled-win.cpp b/clang/test/Sema/warn-infinity-nan-disabled-win.cpp index a904cfbd04f75..01da6c1e43078 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-win.cpp +++ b/clang/test/Sema/warn-infinity-nan-disabled-win.cpp @@ -166,6 +166,16 @@ int compareit(float a, float b) { #elifndef(INFINITY) j = b; #endif +#if defined(INFINITY) + j = a; +#elifndef(NAN) + j = b; +#endif +#if defined(NAN) + j = a; +#elifndef(INFINITY) + j = b; +#endif // no-inf-no-nan-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point option}} // no-inf-no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} From 76568789d12428ff44b6230a539a9f8f727ff789 Mon Sep 17 00:00:00 2001 From: Ammarguellat Date: Mon, 5 Feb 2024 11:16:33 -0800 Subject: [PATCH 5/6] Added 2 additional run lines to the tests. --- clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp | 6 ++++++ clang/test/Sema/warn-infinity-nan-disabled-win.cpp | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp index de66176563303..25ba110893a83 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp +++ b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp @@ -12,9 +12,15 @@ // RUN: %clang_cc1 -x c++ -verify=no-inf -triple powerpc64le-unknown-unknown %s \ // RUN: -menable-no-infs -std=c++23 +// RUN: %clang_cc1 -x c++ -verify=no-inf -triple powerpc64le-unknown-unknown %s \ +// RUN: -menable-no-infs -funsafe-math-optimizations -std=c++23 + // RUN: %clang_cc1 -x c++ -verify=no-nan -triple powerpc64le-unknown-unknown %s \ // RUN: -menable-no-nans -std=c++23 +// RUN: %clang_cc1 -x c++ -verify=no-nan -triple powerpc64le-unknown-unknown %s \ +// RUN: -funsafe-math-optimizations -menable-no-nans -std=c++23 + // no-fast-no-diagnostics int isunorderedf (float x, float y); diff --git a/clang/test/Sema/warn-infinity-nan-disabled-win.cpp b/clang/test/Sema/warn-infinity-nan-disabled-win.cpp index 01da6c1e43078..ec5126f804712 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-win.cpp +++ b/clang/test/Sema/warn-infinity-nan-disabled-win.cpp @@ -15,9 +15,15 @@ // RUN: %clang_cc1 -x c++ -verify=no-inf -triple powerpc64le-unknown-unknown %s \ // RUN: -menable-no-infs -std=c++23 +// RUN: %clang_cc1 -x c++ -verify=no-inf -triple powerpc64le-unknown-unknown %s \ +// RUN: -menable-no-infs -funsafe-math-optimizations -std=c++23 + // RUN: %clang_cc1 -x c++ -verify=no-nan -triple powerpc64le-unknown-unknown %s \ // RUN: -menable-no-nans -std=c++23 +// RUN: %clang_cc1 -x c++ -verify=no-nan -triple powerpc64le-unknown-unknown %s \ +// RUN: -funsafe-math-optimizations -menable-no-nans -std=c++23 + // no-fast-no-diagnostics int isunorderedf (float x, float y); From 33fb1038c6f8cd064d178128c74a5ca3f164513e Mon Sep 17 00:00:00 2001 From: Ammarguellat Date: Mon, 5 Feb 2024 11:57:37 -0800 Subject: [PATCH 6/6] Added 2 additional run lines requested from reviewer. --- clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp | 6 ++++++ clang/test/Sema/warn-infinity-nan-disabled-win.cpp | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp index 25ba110893a83..03a432e05851d 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp +++ b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp @@ -21,6 +21,12 @@ // RUN: %clang_cc1 -x c++ -verify=no-nan -triple powerpc64le-unknown-unknown %s \ // RUN: -funsafe-math-optimizations -menable-no-nans -std=c++23 +// RUN: %clang_cc1 -x c++ -verify=no-fast -triple powerpc64le-unknown-unknown \ +// RUN: %s -Wno-nan-infinity-disabled -menable-no-infs -std=c++23 + +// RUN: %clang_cc1 -x c++ -verify=no-fast -triple powerpc64le-unknown-unknown \ +// RUN: %s -Wno-nan-infinity-disabled -menable-no-nans -std=c++23 + // no-fast-no-diagnostics int isunorderedf (float x, float y); diff --git a/clang/test/Sema/warn-infinity-nan-disabled-win.cpp b/clang/test/Sema/warn-infinity-nan-disabled-win.cpp index ec5126f804712..51f9d325619ba 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-win.cpp +++ b/clang/test/Sema/warn-infinity-nan-disabled-win.cpp @@ -24,6 +24,12 @@ // RUN: %clang_cc1 -x c++ -verify=no-nan -triple powerpc64le-unknown-unknown %s \ // RUN: -funsafe-math-optimizations -menable-no-nans -std=c++23 +// RUN: %clang_cc1 -x c++ -verify=no-fast -triple powerpc64le-unknown-unknown \ +// RUN: %s -Wno-nan-infinity-disabled -menable-no-infs -std=c++23 + +// RUN: %clang_cc1 -x c++ -verify=no-fast -triple powerpc64le-unknown-unknown \ +// RUN: %s -Wno-nan-infinity-disabled -menable-no-nans -std=c++23 + // no-fast-no-diagnostics int isunorderedf (float x, float y);