[all-commits] [llvm/llvm-project] 3c0a13: [clang][Sema] Suggest static_cast in C++ code

Alex Brachet via All-commits all-commits at lists.llvm.org
Fri Jul 14 09:25:46 PDT 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 3c0a136ce4b724221a7f75b656b9f7af3de6b64c
      https://github.com/llvm/llvm-project/commit/3c0a136ce4b724221a7f75b656b9f7af3de6b64c
  Author: Alex Brachet <abrachet at google.com>
  Date:   2023-07-14 (Fri, 14 Jul 2023)

  Changed paths:
    M clang/docs/ReleaseNotes.rst
    M clang/lib/Sema/SemaChecking.cpp
    M clang/test/FixIt/format.mm

  Log Message:
  -----------
  [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


  Commit: 563a23c824db22da3ea378e8179fcc7072e897ec
      https://github.com/llvm/llvm-project/commit/563a23c824db22da3ea378e8179fcc7072e897ec
  Author: Alex Brachet <abrachet at google.com>
  Date:   2023-07-14 (Fri, 14 Jul 2023)

  Changed paths:
    M clang/docs/ReleaseNotes.rst
    M clang/lib/Sema/SemaChecking.cpp
    A clang/test/FixIt/format.cpp

  Log Message:
  -----------
  [clang][Sema] Add fixit for scoped enum format error

This helps transition code bases to handle the new warning added in 3632e2f5179

Before:
```
clang/test/FixIt/format.cpp:10:16: warning: format specifies type 'int' but the argument has type 'N::E' [-Wformat]
   10 |   printf("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
      |           ~~   ^~~~~~~~~
      |           %d
```
After:
```
clang/test/FixIt/format.cpp:10:16: warning: format specifies type 'int' but the argument has type 'N::E' [-Wformat]
   10 |   printf("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
      |           ~~   ^~~~~~~~~
      |                static_cast<int>( )
```

Differential Revision: https://reviews.llvm.org/D153623


Compare: https://github.com/llvm/llvm-project/compare/424392b6960b...563a23c824db


More information about the All-commits mailing list