[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 20 09:39:33 PST 2025
================
@@ -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<CXXMethodDecl>(FDecl)) {
+ const CXXRecordDecl *RDecl = MDecl->getParent();
+ if (RDecl->getName() != "numeric_limits")
+ return false;
+
+ if (const NamespaceDecl *NSDecl =
+ dyn_cast<NamespaceDecl>(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
More information about the cfe-commits
mailing list