diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -415,6 +415,8 @@ source:1:6: note: candidate function not viable: no known conversion from 'const char[4]' to 'int' for 2nd argument void func(int aa, int bb); ^ ~~~~~~ +- ``-Wformat`` cast fix-its will now suggest ``static_cast`` instead of C-style casts + for C++ code. Bug Fixes in This Version ------------------------- diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -11184,9 +11184,9 @@ // 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 Hints; if (!AT.matchesType(S.Context, IntendedTy) || ShouldNotPrintDirectly) @@ -11197,7 +11197,7 @@ 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( diff --git a/clang/test/FixIt/format.mm b/clang/test/FixIt/format.mm --- a/clang/test/FixIt/format.mm +++ b/clang/test/FixIt/format.mm @@ -9,22 +9,22 @@ const wchar_t wchar_data = L'a'; NSLog(@"%C", wchar_data); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'wchar_t'}} - // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"(unsigned short)" + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast( NSLog(@"%C", 0x260300); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'int'}} // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%d" - // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unsigned short)" + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"static_cast( typedef unsigned short unichar; NSLog(@"%C", wchar_data); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'wchar_t'}} - // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"(unichar)" + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast( NSLog(@"%C", 0x260300); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'int'}} // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%d" - // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unichar)" + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"static_cast( NSLog(@"%C", 0.0); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'double'}} // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%f" - // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unichar)" + // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"static_cast( }