Skip to content

Commit 5b61245

Browse files
committed
[Clang][Sema] Process warnings conditionally
There are a few functions that emit warnings related to positional arguments in format strings. These functions use `getLocationOfByte()` which has O(n) complexity and may lead to silent hang of compilation in some cases. But such warnings is not widely used and actually don't emit if user didn't pass the appropriate `-W...` flag, so if the flag is not passed dont make the call to `EmitFormatDiagnostic` for such diags. Fix #120462
1 parent 6f8afaf commit 5b61245

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

clang/lib/Sema/SemaChecking.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6612,27 +6612,33 @@ void CheckFormatHandler::HandleNonStandardConversionSpecifier(
66126612

66136613
void CheckFormatHandler::HandlePosition(const char *startPos,
66146614
unsigned posLen) {
6615-
EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg),
6616-
getLocationOfByte(startPos),
6617-
/*IsStringLocation*/true,
6618-
getSpecifierRange(startPos, posLen));
6615+
if (!S.getDiagnostics().isIgnored(diag::warn_format_non_standard_positional_arg, SourceLocation())) {
6616+
EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg),
6617+
getLocationOfByte(startPos),
6618+
/*IsStringLocation*/true,
6619+
getSpecifierRange(startPos, posLen));
6620+
}
66196621
}
66206622

66216623
void CheckFormatHandler::HandleInvalidPosition(
66226624
const char *startSpecifier, unsigned specifierLen,
66236625
analyze_format_string::PositionContext p) {
6624-
EmitFormatDiagnostic(
6625-
S.PDiag(diag::warn_format_invalid_positional_specifier) << (unsigned)p,
6626-
getLocationOfByte(startSpecifier), /*IsStringLocation*/ true,
6627-
getSpecifierRange(startSpecifier, specifierLen));
6626+
if (!S.getDiagnostics().isIgnored(diag::warn_format_invalid_positional_specifier, SourceLocation())) {
6627+
EmitFormatDiagnostic(
6628+
S.PDiag(diag::warn_format_invalid_positional_specifier) << (unsigned)p,
6629+
getLocationOfByte(startSpecifier), /*IsStringLocation*/ true,
6630+
getSpecifierRange(startSpecifier, specifierLen));
6631+
}
66286632
}
66296633

66306634
void CheckFormatHandler::HandleZeroPosition(const char *startPos,
66316635
unsigned posLen) {
6632-
EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier),
6633-
getLocationOfByte(startPos),
6634-
/*IsStringLocation*/true,
6635-
getSpecifierRange(startPos, posLen));
6636+
if (!S.getDiagnostics().isIgnored(diag::warn_format_zero_positional_specifier, SourceLocation())) {
6637+
EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier),
6638+
getLocationOfByte(startPos),
6639+
/*IsStringLocation*/true,
6640+
getSpecifierRange(startPos, posLen));
6641+
}
66366642
}
66376643

66386644
void CheckFormatHandler::HandleNullChar(const char *nullCharacter) {

0 commit comments

Comments
 (0)