aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/SemaChecking.cpp
diff options
context:
space:
mode:
authorAlex Brachet <abrachet@google.com>2023-07-14 16:22:06 +0000
committerAlex Brachet <abrachet@google.com>2023-07-14 16:22:06 +0000
commit3c0a136ce4b724221a7f75b656b9f7af3de6b64c (patch)
treeb4ace9f2818493301c7dd1371cf05fe7937c84f8 /clang/lib/Sema/SemaChecking.cpp
parent424392b6960b934bba2687024cf968e0729220b4 (diff)
downloadllvm-3c0a136ce4b724221a7f75b656b9f7af3de6b64c.zip
llvm-3c0a136ce4b724221a7f75b656b9f7af3de6b64c.tar.gz
llvm-3c0a136ce4b724221a7f75b656b9f7af3de6b64c.tar.bz2
[clang][Sema] Suggest static_cast in C++ code
This patch changes the -Wformat diagnostic to suggest static_cast over a C-style cast for {,Objective}C++ when recommending the argument be casted rather than changing the format string. Before: ``` clang/test/FixIt/format.mm:11:16: warning: format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'wchar_t' [-Wformat] 11 | NSLog(@"%C", wchar_data); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'wchar_t'}} | ~~ ^~~~~~~~~~ | (unsigned short) ``` After: ``` clang/test/FixIt/format.mm:11:16: warning: format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'wchar_t' [-Wformat] 11 | NSLog(@"%C", wchar_data); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'wchar_t'}} | ~~ ^~~~~~~~~~ | static_cast<unsigned short>( ) ``` Differential Revision: https://reviews.llvm.org/D153622
Diffstat (limited to 'clang/lib/Sema/SemaChecking.cpp')
-rw-r--r--clang/lib/Sema/SemaChecking.cpp6
1 files changed, 3 insertions, 3 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index f9a50f6..66a1a8f 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -11184,9 +11184,9 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
// if necessary).
SmallString<16> CastBuf;
llvm::raw_svector_ostream CastFix(CastBuf);
- CastFix << "(";
+ CastFix << (S.LangOpts.CPlusPlus ? "static_cast<" : "(");
IntendedTy.print(CastFix, S.Context.getPrintingPolicy());
- CastFix << ")";
+ CastFix << (S.LangOpts.CPlusPlus ? ">" : ")");
SmallVector<FixItHint,4> Hints;
if (!AT.matchesType(S.Context, IntendedTy) || ShouldNotPrintDirectly)
@@ -11197,7 +11197,7 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc());
Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str()));
- } else if (!requiresParensToAddCast(E)) {
+ } else if (!requiresParensToAddCast(E) && !S.LangOpts.CPlusPlus) {
// If the expression has high enough precedence,
// just write the C-style cast.
Hints.push_back(