[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -216,11 +244,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); + + j = numeric_limits::infinity(); // expected-no-diagnostics + + y = infinity(); // expected-no-diagnostics + AmrDeveloper wrote: > None I can think of at the moment unfortunately. If i run across one, I'll > bring it up. Thank you, I created a PR for the test cases, and I will continue searching for a good function or a place so I can create a one https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -216,11 +244,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); + + j = numeric_limits::infinity(); // expected-no-diagnostics + + y = infinity(); // expected-no-diagnostics + erichkeane wrote: None I can think of at the moment unfortunately. If i run across one, I'll bring it up. https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -216,11 +244,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); + + j = numeric_limits::infinity(); // expected-no-diagnostics + + y = infinity(); // expected-no-diagnostics + AmrDeveloper wrote: Do we have a similar case with a place that covers most cases of using CallExpr, I found a function like `CheckInvalidBuiltinCountedByRef`, but it does not cover all cases @erichkeane https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -216,11 +244,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); + + j = numeric_limits::infinity(); // expected-no-diagnostics + + y = infinity(); // expected-no-diagnostics + AmrDeveloper wrote: Cool, I will understand the Sema structure more and move it :D Thanks https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -216,11 +244,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); + + j = numeric_limits::infinity(); // expected-no-diagnostics + + y = infinity(); // expected-no-diagnostics + erichkeane wrote: We cannot really, since there is no such thing as 'phase 2' for most of the program (non templates). Typically if you want something ONLY during phase 2, you do it during tree-transform or during the calls to the 'Rebuild' functions. So you ALSO have to do it during phase-1, but the phase-2 work can suppress its diagnostic if it has already been done (assuming you are close enough to rebuild to do that). The fix is likely that you'll have to mvoe where this is diagnosed to somewhere that isn't currently being called by the template instantiation, and re-add it then. https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/AmrDeveloper edited https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -216,11 +244,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); + + j = numeric_limits::infinity(); // expected-no-diagnostics + + y = infinity(); // expected-no-diagnostics + AmrDeveloper wrote: Yes, exactly. Can we just enabled it only on phase 2 🤔, Not sure if we have a way to know in which phase we are? https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -216,11 +244,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); + + j = numeric_limits::infinity(); // expected-no-diagnostics + + y = infinity(); // expected-no-diagnostics + erichkeane wrote: Ah, in that case it is that the same checking code is being called during instantiation, which it almost definitely has to be (to catch the dependent case). So what you're getting is: ``` std::numeric_limits::infinity(); << Diagnosed ONLY during "Phase 2" of template instantiation. std::numeric_limits::infinity(); << Diagnosed BOTH during Phase1 and Phase2. ``` Unfortunately we don't have a GREAT way to suppress warnings in these cases if you don't have access to the actual TreeTransform calls. Typically we'd try to put the diagnostic somewhere this is ONLY called in Phase1, plus do it again ONLY if transformation happened during Phase 2. https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -216,11 +244,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); + + j = numeric_limits::infinity(); // expected-no-diagnostics + + y = infinity(); // expected-no-diagnostics + AmrDeveloper wrote: Yes I expected two too but what I got is two same warns on `std::numeric_limits::infinity();` and one on `td::numeric_limits::infinity()`, I am trying to understand from where the extra one come from https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -216,11 +244,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); + + j = numeric_limits::infinity(); // expected-no-diagnostics + + y = infinity(); // expected-no-diagnostics + erichkeane wrote: Sorry, I'm not getting your point in the previous message. Can you explain it more for me? >From my example, I would expect _TWO_ diagnostics (one per use). But I don't >want 3 or 4 diagnostics. https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -216,11 +244,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); + + j = numeric_limits::infinity(); // expected-no-diagnostics + + y = infinity(); // expected-no-diagnostics + AmrDeveloper wrote: The same problem will happen with `isinf`, `isnan` and `isunordered` functions because the diagnostic will be reported for every generated function, we need to think of a way to process it once @erichkeane https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -216,11 +244,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); + + j = numeric_limits::infinity(); // expected-no-diagnostics + + y = infinity(); // expected-no-diagnostics + AmrDeveloper wrote: I found that we got `use of infinity is undefined behavior due to the currently enabled floating-point options` for `std::numeric_limits::infinity();` for each copy generated from `foo` function, I will check it more and handle it in different pr https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -216,11 +244,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); + + j = numeric_limits::infinity(); // expected-no-diagnostics + + y = infinity(); // expected-no-diagnostics + erichkeane wrote: I would like a dependent test or two, to make sure we don't get this wrong/double-diagnose : ``` template void foo() { std::numeric_limits::infinity(); std::numeric_limits::infinity(); } foo(); ``` https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -216,11 +244,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); AmrDeveloper wrote: > So what happens if we are using `using namespace std` and use > `numeric_limits<...>::infinity()` do we expect a diagnostic? Yes should expect a diagnostic because we check Decl name space not the call expr, I will create a pr to cover this case and update minor comment related to namespace I will tag you as reviewer once I push it Thanks https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -48,24 +48,49 @@ namespace std __attribute__((__visibility__("default"))) { isnan(double __x); bool isnan(long double __x); -bool + bool isfinite(float __x); bool isfinite(double __x); bool isfinte(long double __x); - bool + bool isunordered(float __x, float __y); bool isunordered(double __x, double __y); bool isunordered(long double __x, long double __y); + +template +class numeric_limits { +public: +[[nodiscard]] static constexpr _Ty infinity() noexcept { +return _Ty(); +} +}; + } // namespace ) } shafik wrote: we should annotate the std namespace as well `// std` https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/shafik edited https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/shafik edited https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/shafik commented: quick questions https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -216,11 +244,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); shafik wrote: So what happens if we are using `using namespace std` and use `numeric_limits<...>::infinity()` do we expect a diagnostic? https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/AmrDeveloper closed https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -8454,26 +8454,43 @@ static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) { llvm_unreachable("unknown MathCheck"); } +static bool IsInfinityFunction(const FunctionDecl *FDecl) { + if (FDecl->getName() != "infinity") +return false; + + if (const CXXMethodDecl *MDecl = dyn_cast(FDecl)) { +const CXXRecordDecl *RDecl = MDecl->getParent(); +if (RDecl->getName() != "numeric_limits") + return false; + +if (const NamespaceDecl *NSDecl = +dyn_cast(RDecl->getDeclContext())) + return NSDecl->isStdNamespace(); + } + + return false; +} + void Sema::CheckInfNaNFunction(const CallExpr *Call, const FunctionDecl *FDecl) { + if (!FDecl->getIdentifier()) AmrDeveloper wrote: Thank you https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/zahiraam approved this pull request. LGTM. Thanks. https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -8454,26 +8454,43 @@ static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) { llvm_unreachable("unknown MathCheck"); } +static bool IsInfinityFunction(const FunctionDecl *FDecl) { + if (FDecl->getName() != "infinity") +return false; + + if (const CXXMethodDecl *MDecl = dyn_cast(FDecl)) { +const CXXRecordDecl *RDecl = MDecl->getParent(); +if (RDecl->getName() != "numeric_limits") + return false; + +if (const NamespaceDecl *NSDecl = +dyn_cast(RDecl->getDeclContext())) + return NSDecl->isStdNamespace(); + } + + return false; +} + void Sema::CheckInfNaNFunction(const CallExpr *Call, const FunctionDecl *FDecl) { + if (!FDecl->getIdentifier()) zahiraam wrote: Oh! right, That makes sense! https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/AmrDeveloper edited https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/AmrDeveloper edited https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/AmrDeveloper edited https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/AmrDeveloper edited https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -8454,26 +8454,43 @@ static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) { llvm_unreachable("unknown MathCheck"); } +static bool IsInfinityFunction(const FunctionDecl *FDecl) { + if (FDecl->getName() != "infinity") +return false; + + if (const CXXMethodDecl *MDecl = dyn_cast(FDecl)) { +const CXXRecordDecl *RDecl = MDecl->getParent(); +if (RDecl->getName() != "numeric_limits") + return false; + +if (const NamespaceDecl *NSDecl = +dyn_cast(RDecl->getDeclContext())) + return NSDecl->isStdNamespace(); + } + + return false; +} + void Sema::CheckInfNaNFunction(const CallExpr *Call, const FunctionDecl *FDecl) { + if (!FDecl->getIdentifier()) AmrDeveloper wrote: The problem with this is in case of `FPO.getNoHonorNaNs() == false` we performed un needed 3 function call, and in case of it's true we performed 3 more un needed too `IsInfOrIsFinite`, `IsInfinityOrIsSpecialInf` ```call bool IsNaNOrIsUnordered = IsStdFunction(FDecl, "isnan") || IsStdFunction(FDecl, "isunordered"); bool IsSpecialNaN = IsInfOrNanFunction(FDecl->getName(), MathCheck::NaN); if ((IsNaNOrIsUnordered || IsSpecialNaN) && false) { <--- Condition will evaluated to false } ``` https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -8454,26 +8454,43 @@ static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) { llvm_unreachable("unknown MathCheck"); } +static bool IsInfinityFunction(const FunctionDecl *FDecl) { + if (FDecl->getName() != "infinity") +return false; + + if (const CXXMethodDecl *MDecl = dyn_cast(FDecl)) { +const CXXRecordDecl *RDecl = MDecl->getParent(); +if (RDecl->getName() != "numeric_limits") + return false; + +if (const NamespaceDecl *NSDecl = +dyn_cast(RDecl->getDeclContext())) + return NSDecl->isStdNamespace(); + } + + return false; +} + void Sema::CheckInfNaNFunction(const CallExpr *Call, const FunctionDecl *FDecl) { + if (!FDecl->getIdentifier()) zahiraam wrote: Wouldn't something like this work? ```suggestion if (!FDecl->getIdentifier()) return; FPOptions FPO = Call->getFPFeaturesInEffect(getLangOpts()); bool IsNaNOrIsUnordered = IsStdFunction(FDecl, "isnan") || IsStdFunction(FDecl, "isunordered"); bool IsSpecialNaN = IsInfOrNanFunction(FDecl->getName(), MathCheck::NaN); bool IsInfOrIsFinite = IsStdFunction(FDecl, "isinf") || IsStdFunction(FDecl, "isfinite"); bool IsInfinityOrIsSpecialInf = IsInfOrNanFunction(FDecl->getName(), MathCheck::Inf); if ((IsNaNOrIsUnordered || IsSpecialNaN) && FPO.getNoHonorNaNs()) { Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) << 1 << 0 << Call->getSourceRange(); return; } if ((IsInfOrIsFinite || IsInfinityFunction(FDecl) || IsInfinityOrIsSpecialInf) && FPO.getNoHonorInfs()) { Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) << 0 << 0 << Call->getSourceRange(); } ``` https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -8454,26 +8454,43 @@ static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) { llvm_unreachable("unknown MathCheck"); } +static bool IsInfinityFunction(const FunctionDecl *FDecl) { + if (FDecl->getName() != "infinity") +return false; + + if (const CXXMethodDecl *MDecl = dyn_cast(FDecl)) { +const CXXRecordDecl *RDecl = MDecl->getParent(); +if (RDecl->getName() != "numeric_limits") + return false; + +if (const NamespaceDecl *NSDecl = +dyn_cast(RDecl->getDeclContext())) + return NSDecl->isStdNamespace(); + } + + return false; +} + void Sema::CheckInfNaNFunction(const CallExpr *Call, const FunctionDecl *FDecl) { + if (!FDecl->getIdentifier()) +return; + FPOptions FPO = Call->getFPFeaturesInEffect(getLangOpts()); - bool HasIdentifier = FDecl->getIdentifier() != nullptr; - bool IsNaNOrIsUnordered = - IsStdFunction(FDecl, "isnan") || IsStdFunction(FDecl, "isunordered"); - bool IsSpecialNaN = - HasIdentifier && IsInfOrNanFunction(FDecl->getName(), MathCheck::NaN); - if ((IsNaNOrIsUnordered || IsSpecialNaN) && FPO.getNoHonorNaNs()) { + if (FPO.getNoHonorNaNs() && + (IsStdFunction(FDecl, "isnan") || IsStdFunction(FDecl, "isunordered") || + IsInfOrNanFunction(FDecl->getName(), MathCheck::NaN))) { AmrDeveloper wrote: I don't mind but my point was that functions are readable for example, for the first boolean, we can easily know the meaning from the condition itself. ```cpp bool IsInfOrIsFinite = IsStdFunction(FDecl, "isinf") || IsStdFunction(FDecl, "isfinite"); bool IsInfinityOrIsSpecialInf = HasIdentifier && ((FDecl->getName() == "infinity") || IsInfOrNanFunction(FDecl->getName(), MathCheck::Inf)); ``` Also I am thinking of performing the checking only if `FPO.getNoHonorInfs()` is true, so to do this with variables either I need to introduce another variable for each case to perform other conditions then if it true, or merge it with `IsInfOrIsFinite` so it will be like `IsNoHonorInfsAndIsInfOrIsFinite` and `IsNoHonorInfsAndIsInfinityOrIsSpecialInf` then in the if condition makes sure one of them is true and I think that's less readable that the current code. What do you think? @zahiraam https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -8454,26 +8454,43 @@ static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) { llvm_unreachable("unknown MathCheck"); } +static bool IsInfinityFunction(const FunctionDecl *FDecl) { + if (FDecl->getName() != "infinity") +return false; + + if (const CXXMethodDecl *MDecl = dyn_cast(FDecl)) { +const CXXRecordDecl *RDecl = MDecl->getParent(); +if (RDecl->getName() != "numeric_limits") + return false; + +if (const NamespaceDecl *NSDecl = +dyn_cast(RDecl->getDeclContext())) + return NSDecl->isStdNamespace(); + } + + return false; +} + void Sema::CheckInfNaNFunction(const CallExpr *Call, const FunctionDecl *FDecl) { + if (!FDecl->getIdentifier()) +return; + FPOptions FPO = Call->getFPFeaturesInEffect(getLangOpts()); - bool HasIdentifier = FDecl->getIdentifier() != nullptr; - bool IsNaNOrIsUnordered = - IsStdFunction(FDecl, "isnan") || IsStdFunction(FDecl, "isunordered"); - bool IsSpecialNaN = - HasIdentifier && IsInfOrNanFunction(FDecl->getName(), MathCheck::NaN); - if ((IsNaNOrIsUnordered || IsSpecialNaN) && FPO.getNoHonorNaNs()) { + if (FPO.getNoHonorNaNs() && + (IsStdFunction(FDecl, "isnan") || IsStdFunction(FDecl, "isunordered") || + IsInfOrNanFunction(FDecl->getName(), MathCheck::NaN))) { zahiraam wrote: I think for readability the intermediate `bool` variables can be kept. Same for the condition below. https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
AmrDeveloper wrote: > LGTM with nits Thank you, I addressed the nits https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/123417 >From 51dd53feb7a35d4fac648dd165fb5b99f73c6c24 Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Fri, 17 Jan 2025 23:32:48 +0100 Subject: [PATCH 1/4] [Clang] Fix invalid use of infinity warning --- clang/lib/Sema/SemaChecking.cpp | 22 +- .../Sema/warn-infinity-nan-disabled-lnx.cpp | 42 -- .../Sema/warn-infinity-nan-disabled-win.cpp | 43 +-- 3 files changed, 98 insertions(+), 9 deletions(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 881907ac311a30..3d2b2e1e96d2dd 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -8454,6 +8454,26 @@ static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) { llvm_unreachable("unknown MathCheck"); } +static bool IsInfinityFunction(const FunctionDecl *FDecl) { + if (FDecl->getName() != "infinity") { +return false; + } + + if (const CXXMethodDecl *MDecl = dyn_cast(FDecl)) { +const CXXRecordDecl *RDecl = MDecl->getParent(); +if (RDecl->getName() != "numeric_limits") { + return false; +} + +if (const NamespaceDecl *NSDecl = +dyn_cast(RDecl->getDeclContext())) { + return NSDecl->isStdNamespace(); +} + } + + return false; +} + void Sema::CheckInfNaNFunction(const CallExpr *Call, const FunctionDecl *FDecl) { FPOptions FPO = Call->getFPFeaturesInEffect(getLangOpts()); @@ -8469,7 +8489,7 @@ void Sema::CheckInfNaNFunction(const CallExpr *Call, bool IsInfOrIsFinite = IsStdFunction(FDecl, "isinf") || IsStdFunction(FDecl, "isfinite"); bool IsInfinityOrIsSpecialInf = -HasIdentifier && ((FDecl->getName() == "infinity") || +HasIdentifier && (IsInfinityFunction(FDecl) || IsInfOrNanFunction(FDecl->getName(), MathCheck::Inf)); if ((IsInfOrIsFinite || IsInfinityOrIsSpecialInf) && FPO.getNoHonorInfs()) Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) diff --git a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp index 357c9e5b641073..4f46b777c88742 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp +++ b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp @@ -45,24 +45,48 @@ namespace std __attribute__((__visibility__("default"))) { isnan(double __x); bool isnan(long double __x); -bool + bool isfinite(float __x); bool isfinite(double __x); bool isfinte(long double __x); - bool + bool isunordered(float __x, float __y); bool isunordered(double __x, double __y); bool isunordered(long double __x, long double __y); + +template +class numeric_limits { +public: +[[nodiscard]] static constexpr _Ty infinity() noexcept { +return _Ty(); +} +}; } // namespace ) } #define NAN (__builtin_nanf("")) #define INFINITY (__builtin_inff()) +template <> +class std::numeric_limits { +public: +[[nodiscard]] static constexpr float infinity() noexcept { +return __builtin_huge_val(); +} +}; + +template <> +class std::numeric_limits { +public: +[[nodiscard]] static constexpr double infinity() noexcept { +return __builtin_huge_val(); +} +}; + template class numeric_limits { public: @@ -78,6 +102,7 @@ class numeric_limits { return __builtin_huge_val(); } }; + template <> class numeric_limits { public: @@ -86,6 +111,8 @@ class numeric_limits { } }; +double infinity() { return 0; } + int compareit(float a, float b) { volatile int i, j, k, l, m, n, o, p; // no-inf-no-nan-warning@+4 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} @@ -225,11 +252,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); + + j = numeric_limits::infinity(); // expected-no-diagnostics + + y = infinity(); // expected-no-diagnostics + 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 ee4eb33a16e449..655024f5909b33 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-win.cpp +++ b/clang/test/Sem
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/cor3ntin edited https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -8454,26 +8454,47 @@ static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) { llvm_unreachable("unknown MathCheck"); } +static bool IsInfinityFunction(const FunctionDecl *FDecl) { + if (FDecl->getName() != "infinity") { +return false; + } + + if (const CXXMethodDecl *MDecl = dyn_cast(FDecl)) { +const CXXRecordDecl *RDecl = MDecl->getParent(); +if (RDecl->getName() != "numeric_limits") { + return false; +} + +if (const NamespaceDecl *NSDecl = +dyn_cast(RDecl->getDeclContext())) { + return NSDecl->isStdNamespace(); +} + } + + return false; +} + void Sema::CheckInfNaNFunction(const CallExpr *Call, const FunctionDecl *FDecl) { + if (!FDecl->getIdentifier()) { +return; + } + cor3ntin wrote: ```suggestion if (!FDecl->getIdentifier()) return; ``` https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -8454,26 +8454,47 @@ static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) { llvm_unreachable("unknown MathCheck"); } +static bool IsInfinityFunction(const FunctionDecl *FDecl) { + if (FDecl->getName() != "infinity") { +return false; + } + + if (const CXXMethodDecl *MDecl = dyn_cast(FDecl)) { +const CXXRecordDecl *RDecl = MDecl->getParent(); +if (RDecl->getName() != "numeric_limits") { + return false; +} + +if (const NamespaceDecl *NSDecl = +dyn_cast(RDecl->getDeclContext())) { + return NSDecl->isStdNamespace(); +} + } + + return false; +} + cor3ntin wrote: ```suggestion if (FDecl->getName() != "infinity") return false; if (const CXXMethodDecl *MDecl = dyn_cast(FDecl)) { const CXXRecordDecl *RDecl = MDecl->getParent(); if (RDecl->getName() != "numeric_limits") return false; if (const NamespaceDecl *NSDecl = dyn_cast(RDecl->getDeclContext())) return NSDecl->isStdNamespace(); } return false; } ``` https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/cor3ntin approved this pull request. LGTM with nits https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
AmrDeveloper wrote: > Can you add a changelog entry? Thanks Done, thanks https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/123417 >From 51dd53feb7a35d4fac648dd165fb5b99f73c6c24 Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Fri, 17 Jan 2025 23:32:48 +0100 Subject: [PATCH 1/3] [Clang] Fix invalid use of infinity warning --- clang/lib/Sema/SemaChecking.cpp | 22 +- .../Sema/warn-infinity-nan-disabled-lnx.cpp | 42 -- .../Sema/warn-infinity-nan-disabled-win.cpp | 43 +-- 3 files changed, 98 insertions(+), 9 deletions(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 881907ac311a30..3d2b2e1e96d2dd 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -8454,6 +8454,26 @@ static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) { llvm_unreachable("unknown MathCheck"); } +static bool IsInfinityFunction(const FunctionDecl *FDecl) { + if (FDecl->getName() != "infinity") { +return false; + } + + if (const CXXMethodDecl *MDecl = dyn_cast(FDecl)) { +const CXXRecordDecl *RDecl = MDecl->getParent(); +if (RDecl->getName() != "numeric_limits") { + return false; +} + +if (const NamespaceDecl *NSDecl = +dyn_cast(RDecl->getDeclContext())) { + return NSDecl->isStdNamespace(); +} + } + + return false; +} + void Sema::CheckInfNaNFunction(const CallExpr *Call, const FunctionDecl *FDecl) { FPOptions FPO = Call->getFPFeaturesInEffect(getLangOpts()); @@ -8469,7 +8489,7 @@ void Sema::CheckInfNaNFunction(const CallExpr *Call, bool IsInfOrIsFinite = IsStdFunction(FDecl, "isinf") || IsStdFunction(FDecl, "isfinite"); bool IsInfinityOrIsSpecialInf = -HasIdentifier && ((FDecl->getName() == "infinity") || +HasIdentifier && (IsInfinityFunction(FDecl) || IsInfOrNanFunction(FDecl->getName(), MathCheck::Inf)); if ((IsInfOrIsFinite || IsInfinityOrIsSpecialInf) && FPO.getNoHonorInfs()) Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) diff --git a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp index 357c9e5b641073..4f46b777c88742 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp +++ b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp @@ -45,24 +45,48 @@ namespace std __attribute__((__visibility__("default"))) { isnan(double __x); bool isnan(long double __x); -bool + bool isfinite(float __x); bool isfinite(double __x); bool isfinte(long double __x); - bool + bool isunordered(float __x, float __y); bool isunordered(double __x, double __y); bool isunordered(long double __x, long double __y); + +template +class numeric_limits { +public: +[[nodiscard]] static constexpr _Ty infinity() noexcept { +return _Ty(); +} +}; } // namespace ) } #define NAN (__builtin_nanf("")) #define INFINITY (__builtin_inff()) +template <> +class std::numeric_limits { +public: +[[nodiscard]] static constexpr float infinity() noexcept { +return __builtin_huge_val(); +} +}; + +template <> +class std::numeric_limits { +public: +[[nodiscard]] static constexpr double infinity() noexcept { +return __builtin_huge_val(); +} +}; + template class numeric_limits { public: @@ -78,6 +102,7 @@ class numeric_limits { return __builtin_huge_val(); } }; + template <> class numeric_limits { public: @@ -86,6 +111,8 @@ class numeric_limits { } }; +double infinity() { return 0; } + int compareit(float a, float b) { volatile int i, j, k, l, m, n, o, p; // no-inf-no-nan-warning@+4 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} @@ -225,11 +252,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); + + j = numeric_limits::infinity(); // expected-no-diagnostics + + y = infinity(); // expected-no-diagnostics + 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 ee4eb33a16e449..655024f5909b33 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-win.cpp +++ b/clang/test/Sem
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/cor3ntin commented: Can you add a changelog entry? Thanks https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -8469,7 +8489,7 @@ void Sema::CheckInfNaNFunction(const CallExpr *Call, bool IsInfOrIsFinite = AmrDeveloper wrote: I think it's better now, early return if there is no identifier and check `NoHonor.. `first before checking for function name https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/123417 >From 51dd53feb7a35d4fac648dd165fb5b99f73c6c24 Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Fri, 17 Jan 2025 23:32:48 +0100 Subject: [PATCH 1/2] [Clang] Fix invalid use of infinity warning --- clang/lib/Sema/SemaChecking.cpp | 22 +- .../Sema/warn-infinity-nan-disabled-lnx.cpp | 42 -- .../Sema/warn-infinity-nan-disabled-win.cpp | 43 +-- 3 files changed, 98 insertions(+), 9 deletions(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 881907ac311a30..3d2b2e1e96d2dd 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -8454,6 +8454,26 @@ static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) { llvm_unreachable("unknown MathCheck"); } +static bool IsInfinityFunction(const FunctionDecl *FDecl) { + if (FDecl->getName() != "infinity") { +return false; + } + + if (const CXXMethodDecl *MDecl = dyn_cast(FDecl)) { +const CXXRecordDecl *RDecl = MDecl->getParent(); +if (RDecl->getName() != "numeric_limits") { + return false; +} + +if (const NamespaceDecl *NSDecl = +dyn_cast(RDecl->getDeclContext())) { + return NSDecl->isStdNamespace(); +} + } + + return false; +} + void Sema::CheckInfNaNFunction(const CallExpr *Call, const FunctionDecl *FDecl) { FPOptions FPO = Call->getFPFeaturesInEffect(getLangOpts()); @@ -8469,7 +8489,7 @@ void Sema::CheckInfNaNFunction(const CallExpr *Call, bool IsInfOrIsFinite = IsStdFunction(FDecl, "isinf") || IsStdFunction(FDecl, "isfinite"); bool IsInfinityOrIsSpecialInf = -HasIdentifier && ((FDecl->getName() == "infinity") || +HasIdentifier && (IsInfinityFunction(FDecl) || IsInfOrNanFunction(FDecl->getName(), MathCheck::Inf)); if ((IsInfOrIsFinite || IsInfinityOrIsSpecialInf) && FPO.getNoHonorInfs()) Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) diff --git a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp index 357c9e5b641073..4f46b777c88742 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp +++ b/clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp @@ -45,24 +45,48 @@ namespace std __attribute__((__visibility__("default"))) { isnan(double __x); bool isnan(long double __x); -bool + bool isfinite(float __x); bool isfinite(double __x); bool isfinte(long double __x); - bool + bool isunordered(float __x, float __y); bool isunordered(double __x, double __y); bool isunordered(long double __x, long double __y); + +template +class numeric_limits { +public: +[[nodiscard]] static constexpr _Ty infinity() noexcept { +return _Ty(); +} +}; } // namespace ) } #define NAN (__builtin_nanf("")) #define INFINITY (__builtin_inff()) +template <> +class std::numeric_limits { +public: +[[nodiscard]] static constexpr float infinity() noexcept { +return __builtin_huge_val(); +} +}; + +template <> +class std::numeric_limits { +public: +[[nodiscard]] static constexpr double infinity() noexcept { +return __builtin_huge_val(); +} +}; + template class numeric_limits { public: @@ -78,6 +102,7 @@ class numeric_limits { return __builtin_huge_val(); } }; + template <> class numeric_limits { public: @@ -86,6 +111,8 @@ class numeric_limits { } }; +double infinity() { return 0; } + int compareit(float a, float b) { volatile int i, j, k, l, m, n, o, p; // no-inf-no-nan-warning@+4 {{use of infinity is undefined behavior due to the currently enabled floating-point options}} @@ -225,11 +252,18 @@ int compareit(float a, float b) { // 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(); + double y = i * std::numeric_limits::infinity(); + + y = i * numeric_limits::infinity(); // expected-no-diagnostics // 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(); + j = std::numeric_limits::infinity(); + + j = numeric_limits::infinity(); // expected-no-diagnostics + + y = infinity(); // expected-no-diagnostics + 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 ee4eb33a16e449..655024f5909b33 100644 --- a/clang/test/Sema/warn-infinity-nan-disabled-win.cpp +++ b/clang/test/Sem
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -8469,7 +8489,7 @@ void Sema::CheckInfNaNFunction(const CallExpr *Call, bool IsInfOrIsFinite = AmrDeveloper wrote: Yes, I agree, i am thinking to change the structure of the function `CheckInfNaNFunction ` too, because if `HasIdentifier` is false that's means `IsStdFunction`, `IsInfOrNanFunction` and `IsInfinityFunction` will return false, so maybe we can just eliminate some un needed function calls by early return. I will try and update you with result :D https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
@@ -8469,7 +8489,7 @@ void Sema::CheckInfNaNFunction(const CallExpr *Call, bool IsInfOrIsFinite = cor3ntin wrote: As an aside, this is called on every function call, which seems widely inficient. can we do something like ```cpp FPOptions FPO = Call->getFPFeaturesInEffect(getLangOpts()); if(FPO.getNoHonorInfs()) return; ``` (and remove `FPO.getNoHonorInfs()` in the rest of the function) https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
https://github.com/AmrDeveloper edited https://github.com/llvm/llvm-project/pull/123417 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits