https://github.com/zahiraam updated https://github.com/llvm/llvm-project/pull/99672
>From 452bc424b7e6d63c50af1ed8f3623ad84e9b091c Mon Sep 17 00:00:00 2001 From: Zahira Ammarguellat <zahira.ammarguel...@intel.com> Date: Fri, 19 Jul 2024 10:31:18 -0700 Subject: [PATCH 1/4] Fix issue https://github.com/llvm/llvm-project/issues/98018. --- clang/lib/Sema/SemaChecking.cpp | 3 +- clang/test/Headers/float.c | 3 +- .../Sema/warn-infinity-nan-disabled-lnx.cpp | 28 +++++++++++++------ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 45b9bbb23dbf7..4bf115d6efb8b 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -8208,7 +8208,8 @@ void Sema::CheckInfNaNFunction(const CallExpr *Call, << 1 << 0 << Call->getSourceRange(); else if ((IsStdFunction(FDecl, "isinf") || (IsStdFunction(FDecl, "isfinite") || - (FDecl->getIdentifier() && FDecl->getName() == "infinity"))) && + (FDecl->getIdentifier() && FDecl->getName() == "infinity") || + (Call->getBuiltinCallee() == Builtin::BI__builtin_inff))) && FPO.getNoHonorInfs()) Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) << 0 << 0 << Call->getSourceRange(); diff --git a/clang/test/Headers/float.c b/clang/test/Headers/float.c index d524d0e53f3fd..7e156e7dd4a34 100644 --- a/clang/test/Headers/float.c +++ b/clang/test/Headers/float.c @@ -224,7 +224,8 @@ #error "Mandatory macro NAN is missing." #endif // FIXME: the NAN diagnostic should only be issued once, not twice. - _Static_assert(_Generic(INFINITY, float : 1, default : 0), ""); // finite-warning {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} + _Static_assert(_Generic(INFINITY, float : 1, default : 0), ""); // finite-warning {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} \ + finite-warning {{use of infinity is undefined behavior due to the currently enabled floating-point options}} _Static_assert(_Generic(NAN, float : 1, default : 0), ""); // finite-warning {{use of NaN is undefined behavior due to the currently enabled floating-point options}} \ finite-warning {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}} diff --git a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp index 03a432e05851d..d18aaad0e6925 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp +++ b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp @@ -87,11 +87,15 @@ class numeric_limits<double> { 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-no-nan-warning@+4 {{use of infinity 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 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@+2 {{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 infinity 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 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; @@ -107,11 +111,15 @@ int compareit(float a, float b) { // 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-no-nan-warning@+4 {{use of infinity 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 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@+2 {{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 infinity 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 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; @@ -192,7 +200,9 @@ int compareit(float a, float b) { // 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-no-nan-warning@+4 {{use of infinity 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 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); @@ -204,9 +214,11 @@ int compareit(float a, float b) { // 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-inf-no-nan-warning@+6 {{use of infinity 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-inf-warning@+2 {{use of infinity 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); >From 523f73d029f83b6454ec656042a3fc2c97e72c05 Mon Sep 17 00:00:00 2001 From: Zahira Ammarguellat <zahira.ammarguel...@intel.com> Date: Mon, 22 Jul 2024 13:47:26 -0700 Subject: [PATCH 2/4] Addressed review comments. --- clang/lib/Sema/SemaChecking.cpp | 47 ++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 4bf115d6efb8b..6126c2f97cfed 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -8198,21 +8198,48 @@ static bool IsStdFunction(const FunctionDecl *FDecl, return true; } +enum class MathCheck { NaN, Inf }; +static bool IsSpecialMathFunction(StringRef calleeName, MathCheck Check) { + auto MatchesAny = [&](std::initializer_list<llvm::StringRef> names) { + return std::any_of(names.begin(), names.end(), [&](llvm::StringRef name) { + return calleeName == name; + }); + }; + + switch (Check) { + case MathCheck::NaN: + return MatchesAny({"__builtin_nan", "__builtin_nanf", "__builtin_nanl", + "__builtin_nanf16", "__builtin_nanf128"}); + case MathCheck::Inf: + return MatchesAny({"__builtin_inf", "__builtin_inff", "__builtin_infl", + "__builtin_inff16", "__builtin_inff128"}); + default: + return false; + } +} + void Sema::CheckInfNaNFunction(const CallExpr *Call, const FunctionDecl *FDecl) { FPOptions FPO = Call->getFPFeaturesInEffect(getLangOpts()); - if ((IsStdFunction(FDecl, "isnan") || IsStdFunction(FDecl, "isunordered") || - (Call->getBuiltinCallee() == Builtin::BI__builtin_nanf)) && - FPO.getNoHonorNaNs()) + bool HasIdentifier = FDecl->getIdentifier() != nullptr; + bool IsNaNOrIsUnordered = + IsStdFunction(FDecl, "isnan") || IsStdFunction(FDecl, "isunordered"); + bool IsSpecialNaN = + HasIdentifier && IsSpecialMathFunction(FDecl->getName(), MathCheck::NaN); + if ((IsNaNOrIsUnordered || IsSpecialNaN) && FPO.getNoHonorNaNs()) { Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) << 1 << 0 << Call->getSourceRange(); - else if ((IsStdFunction(FDecl, "isinf") || - (IsStdFunction(FDecl, "isfinite") || - (FDecl->getIdentifier() && FDecl->getName() == "infinity") || - (Call->getBuiltinCallee() == Builtin::BI__builtin_inff))) && - FPO.getNoHonorInfs()) - Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) - << 0 << 0 << Call->getSourceRange(); + } else { + bool IsInfOrIsFinite = + IsStdFunction(FDecl, "isinf") || IsStdFunction(FDecl, "isfinite"); + bool IsInfinityOrIsSpecialInf = + HasIdentifier && + ((FDecl->getName() == "infinity") || + IsSpecialMathFunction(FDecl->getName(), MathCheck::Inf)); + if ((IsInfOrIsFinite || IsInfinityOrIsSpecialInf) && FPO.getNoHonorInfs()) + Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) + << 0 << 0 << Call->getSourceRange(); + } } void Sema::CheckAbsoluteValueFunction(const CallExpr *Call, >From 36094bf2cdfb6941c03cde099514fc4a55c8e8ce Mon Sep 17 00:00:00 2001 From: Zahira Ammarguellat <zahira.ammarguel...@intel.com> Date: Mon, 22 Jul 2024 13:58:44 -0700 Subject: [PATCH 3/4] Fix format. --- clang/lib/Sema/SemaChecking.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 6126c2f97cfed..adff2567ad20f 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -8229,7 +8229,7 @@ void Sema::CheckInfNaNFunction(const CallExpr *Call, if ((IsNaNOrIsUnordered || IsSpecialNaN) && FPO.getNoHonorNaNs()) { Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) << 1 << 0 << Call->getSourceRange(); - } else { + } else { bool IsInfOrIsFinite = IsStdFunction(FDecl, "isinf") || IsStdFunction(FDecl, "isfinite"); bool IsInfinityOrIsSpecialInf = >From 24b1d4cfc266d98c9da4466c22be7db902680a8d Mon Sep 17 00:00:00 2001 From: Ammarguellat <zahira.ammarguel...@intel.com> Date: Wed, 24 Jul 2024 09:49:18 -0700 Subject: [PATCH 4/4] Addressed review comments. --- clang/lib/Sema/SemaChecking.cpp | 9 ++++----- clang/test/Headers/float.c | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index adff2567ad20f..3fccef4aed675 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -8199,7 +8199,7 @@ static bool IsStdFunction(const FunctionDecl *FDecl, } enum class MathCheck { NaN, Inf }; -static bool IsSpecialMathFunction(StringRef calleeName, MathCheck Check) { +static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) { auto MatchesAny = [&](std::initializer_list<llvm::StringRef> names) { return std::any_of(names.begin(), names.end(), [&](llvm::StringRef name) { return calleeName == name; @@ -8225,7 +8225,7 @@ void Sema::CheckInfNaNFunction(const CallExpr *Call, bool IsNaNOrIsUnordered = IsStdFunction(FDecl, "isnan") || IsStdFunction(FDecl, "isunordered"); bool IsSpecialNaN = - HasIdentifier && IsSpecialMathFunction(FDecl->getName(), MathCheck::NaN); + HasIdentifier && IsInfOrNanFunction(FDecl->getName(), MathCheck::NaN); if ((IsNaNOrIsUnordered || IsSpecialNaN) && FPO.getNoHonorNaNs()) { Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) << 1 << 0 << Call->getSourceRange(); @@ -8233,9 +8233,8 @@ void Sema::CheckInfNaNFunction(const CallExpr *Call, bool IsInfOrIsFinite = IsStdFunction(FDecl, "isinf") || IsStdFunction(FDecl, "isfinite"); bool IsInfinityOrIsSpecialInf = - HasIdentifier && - ((FDecl->getName() == "infinity") || - IsSpecialMathFunction(FDecl->getName(), MathCheck::Inf)); + HasIdentifier && ((FDecl->getName() == "infinity") || + IsInfOrNanFunction(FDecl->getName(), MathCheck::Inf)); if ((IsInfOrIsFinite || IsInfinityOrIsSpecialInf) && FPO.getNoHonorInfs()) Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) << 0 << 0 << Call->getSourceRange(); diff --git a/clang/test/Headers/float.c b/clang/test/Headers/float.c index 7e156e7dd4a34..5d3461aa65939 100644 --- a/clang/test/Headers/float.c +++ b/clang/test/Headers/float.c @@ -223,7 +223,6 @@ #ifndef NAN #error "Mandatory macro NAN is missing." #endif - // FIXME: the NAN diagnostic should only be issued once, not twice. _Static_assert(_Generic(INFINITY, float : 1, default : 0), ""); // finite-warning {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}} \ finite-warning {{use of infinity is undefined behavior due to the currently enabled floating-point options}} _Static_assert(_Generic(NAN, float : 1, default : 0), ""); // finite-warning {{use of NaN is undefined behavior due to the currently enabled floating-point options}} \ _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits