[clang] [Clang] Fix warning for non std functions with name `infinity` (PR #123417)
Zahira Ammarguellat via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 20 12:44:01 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())
----------------
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
More information about the cfe-commits
mailing list