You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Deal better with a macro that surrounds the whole call
We don't really want to stop enclosing an entire call to a printf-like
function to stop the check from replacing the call since that makes the
check ineffective when using testing frameworks such as Catch2.
// Needs a __PRI prefix so that we get as far as looking for where the macro comes from
153
153
auto s11 = absl::StrFormat(" macro from command line %" __PRI_CMDLINE_MACRO, s);
154
154
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: unable to use 'std::format' instead of 'StrFormat' because format string contains unreplaceable macro '__PRI_CMDLINE_MACRO' [modernize-use-std-format]
155
+
156
+
// We ought to be able to fix this since the macro surrounds the whole call
157
+
// and therefore can't change the format string independently. This is
158
+
// required to be able to fix calls inside Catch2 macros for example.
159
+
#defineSURROUND_ALL(x) x
160
+
auto s12 = SURROUND_ALL(absl::StrFormat("Macro surrounding entire invocation %" PRIu64, u64));
161
+
// CHECK-MESSAGES: [[@LINE-1]]:27: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
// But having that surrounding macro shouldn't stop us ignoring an
165
+
// unreplaceable macro elsewhere.
166
+
auto s13 = SURROUND_ALL(absl::StrFormat("Macro surrounding entire invocation with unreplaceable macro %" PRI_FMT_MACRO, s));
167
+
// CHECK-MESSAGES: [[@LINE-1]]:27: warning: unable to use 'std::format' instead of 'StrFormat' because format string contains unreplaceable macro 'PRI_FMT_MACRO' [modernize-use-std-format]
168
+
169
+
// At the moment at least the check will replace occurrences where the
170
+
// function name is the result of expanding a macro.
171
+
#defineSURROUND_FUNCTION_NAME(x) absl:: x
172
+
auto s14 = SURROUND_FUNCTION_NAME(StrFormat)("Hello %d", 4442);
173
+
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
174
+
// CHECK-FIXES: auto s14 = std::format("Hello {}", 4442);
175
+
176
+
// We can't safely fix occurrences where the macro may affect the format
177
+
// string differently in different builds.
178
+
#defineSURROUND_FORMAT(x) "!" x
179
+
auto s15 = absl::StrFormat(SURROUND_FORMAT("Hello %d"), 4443);
180
+
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: unable to use 'std::format' instead of 'StrFormat' because format string contains unreplaceable macro 'SURROUND_FORMAT' [modernize-use-std-format]
// Needs a __PRI prefix so that we get as far as looking for where the macro comes from
1614
1614
printf(" macro from command line %" __PRI_CMDLINE_MACRO, s);
1615
1615
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: unable to use 'std::print' instead of 'printf' because format string contains unreplaceable macro '__PRI_CMDLINE_MACRO' [modernize-use-std-print]
1616
+
1617
+
// We ought to be able to fix this since the macro surrounds the whole call
1618
+
// and therefore can't change the format string independently. This is
1619
+
// required to be able to fix calls inside Catch2 macros for example.
// But having that surrounding macro shouldn't stop us ignoring an
1626
+
// unreplaceable macro elsewhere.
1627
+
SURROUND_ALL(printf("Macro surrounding entire invocation with unreplaceable macro %" PRI_FMT_MACRO, s));
1628
+
// CHECK-MESSAGES: [[@LINE-1]]:16: warning: unable to use 'std::print' instead of 'printf' because format string contains unreplaceable macro 'PRI_FMT_MACRO' [modernize-use-std-print]
1629
+
1630
+
// At the moment at least the check will replace occurrences where the
1631
+
// function name is the result of expanding a macro.
1632
+
#defineSURROUND_FUNCTION_NAME(x) x
1633
+
SURROUND_FUNCTION_NAME(printf)("Hello %d", 4442);
1634
+
// CHECK-MESSAGES: [[@LINE-1]]:26: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]
1635
+
// CHECK-FIXES: std::print("Hello {}", 4442);
1636
+
1637
+
// We can't safely fix occurrences where the macro may affect the format
1638
+
// string differently in different builds.
1639
+
#defineSURROUND_FORMAT(x) "!" x
1640
+
printf(SURROUND_FORMAT("Hello %d"), 4443);
1641
+
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: unable to use 'std::print' instead of 'printf' because format string contains unreplaceable macro 'SURROUND_FORMAT' [modernize-use-std-print]
0 commit comments