[clang] [Clang][Sema] Process warnings conditionally (PR #120591)

Dmitry Chestnykh via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 19 07:37:05 PST 2024


https://github.com/chestnykh created https://github.com/llvm/llvm-project/pull/120591

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

>From 5b61245bbdc9d1874684f29bba880f6b5a4cfe82 Mon Sep 17 00:00:00 2001
From: Dmitry Chestnykh <dm.chestnykh at gmail.com>
Date: Thu, 19 Dec 2024 18:35:35 +0300
Subject: [PATCH] [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
---
 clang/lib/Sema/SemaChecking.cpp | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index be5d3694aec152..a745250988feeb 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -6612,27 +6612,33 @@ void CheckFormatHandler::HandleNonStandardConversionSpecifier(
 
 void CheckFormatHandler::HandlePosition(const char *startPos,
                                         unsigned posLen) {
-  EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg),
-                               getLocationOfByte(startPos),
-                               /*IsStringLocation*/true,
-                               getSpecifierRange(startPos, posLen));
+  if (!S.getDiagnostics().isIgnored(diag::warn_format_non_standard_positional_arg, SourceLocation())) {
+    EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg),
+                                getLocationOfByte(startPos),
+                                /*IsStringLocation*/true,
+                                getSpecifierRange(startPos, posLen));
+  }
 }
 
 void CheckFormatHandler::HandleInvalidPosition(
     const char *startSpecifier, unsigned specifierLen,
     analyze_format_string::PositionContext p) {
-  EmitFormatDiagnostic(
-      S.PDiag(diag::warn_format_invalid_positional_specifier) << (unsigned)p,
-      getLocationOfByte(startSpecifier), /*IsStringLocation*/ true,
-      getSpecifierRange(startSpecifier, specifierLen));
+  if (!S.getDiagnostics().isIgnored(diag::warn_format_invalid_positional_specifier, SourceLocation())) {
+    EmitFormatDiagnostic(
+        S.PDiag(diag::warn_format_invalid_positional_specifier) << (unsigned)p,
+        getLocationOfByte(startSpecifier), /*IsStringLocation*/ true,
+        getSpecifierRange(startSpecifier, specifierLen));
+  }
 }
 
 void CheckFormatHandler::HandleZeroPosition(const char *startPos,
                                             unsigned posLen) {
-  EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier),
-                               getLocationOfByte(startPos),
-                               /*IsStringLocation*/true,
-                               getSpecifierRange(startPos, posLen));
+  if (!S.getDiagnostics().isIgnored(diag::warn_format_zero_positional_specifier, SourceLocation())) {
+    EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier),
+                                getLocationOfByte(startPos),
+                                /*IsStringLocation*/true,
+                                getSpecifierRange(startPos, posLen));
+  }
 }
 
 void CheckFormatHandler::HandleNullChar(const char *nullCharacter) {



More information about the cfe-commits mailing list