Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLANG] Fix INF/NAN warning. #80290

Merged
merged 6 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion clang/include/clang/Basic/DiagnosticCommonKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -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">,
AaronBallman marked this conversation as resolved.
Show resolved Hide resolved
InGroup<DiagGroup<"nan-infinity-disabled">>;
}

Expand Down
20 changes: 12 additions & 8 deletions clang/include/clang/Lex/Preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
bool UnsafeMath = getLangOpts().UnsafeFPMath;
if (Info->getName() == "INFINITY" && getLangOpts().NoHonorInfs)
emitRestrictInfNaNWarning(Identifier, 0, UnsafeMath);
if (Info->getName() == "NAN" && getLangOpts().NoHonorNaNs)
emitRestrictInfNaNWarning(Identifier, 1, UnsafeMath);

}
}

static void processPathForFileMacro(SmallVectorImpl<char> &Path,
Expand All @@ -2869,7 +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) 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
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Lex/PPDirectives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3288,7 +3288,7 @@ void Preprocessor::HandleIfdefDirective(Token &Result,
return;
}

emitMacroExpansionWarnings(MacroNameTok);
emitMacroExpansionWarnings(MacroNameTok, true);
AaronBallman marked this conversation as resolved.
Show resolved Hide resolved

// Check to see if this is the last token on the #if[n]def line.
CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Lex/Preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
AaronBallman marked this conversation as resolved.
Show resolved Hide resolved

ExprResult OrigArg0 = TheCall->getArg(0);
ExprResult OrigArg1 = TheCall->getArg(1);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down
Loading
Loading