[PATCH] D132568: [clang][Sema] check default argument promotions for printf

YingChi Long via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 24 08:51:26 PDT 2022


inclyc created this revision.
inclyc added a reviewer: aaron.ballman.
Herald added a subscriber: emaste.
Herald added a project: All.
inclyc published this revision for review.
inclyc added a comment.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

https://reviews.llvm.org/D132266

This patch seems to ignore this scenario:

  printf("%hhf", [int])

Where we have length modifier `h`, and the argument type is `int`. Causes clang to suppress warnings about type mismatches between floats and integers.



================
Comment at: clang/test/FixIt/format.mm:10
 
   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'}}
----------------
The language built-in `wchar_t` will be properly diagnosed.


================
Comment at: clang/test/SemaObjC/format-strings-objc.m:194
   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'}}
 }
----------------
This `wchar_t` is defined as `typedef __WCHAR_TYPE__ wchar_t;`. Actually it is `int` on my machine, I'm not sure if we should still report this warning as before, because it is just `int`, not a real `"wchar_t"`.


The main focus of this patch is to make ArgType::matchesType check for
possible default parameter promotions when the argType is not a pointer.
If so, no warning will be given for `int`, `unsigned int` types as
corresponding arguments to %hhd and %hd. However, the usage of %hhd
corresponding to short is relatively rare, and it is more likely to be a
misuse. This patch keeps the original behavior of clang like this as
much as possible, while making it more convenient to consider the
default arguments promotion.

Fixes https://github.com/llvm/llvm-project/issues/57102


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132568

Files:
  clang/include/clang/AST/FormatString.h
  clang/lib/AST/FormatString.cpp
  clang/lib/Sema/SemaChecking.cpp
  clang/test/FixIt/format.m
  clang/test/FixIt/format.mm
  clang/test/Sema/format-strings-freebsd.c
  clang/test/Sema/format-strings-scanf.c
  clang/test/Sema/format-strings.c
  clang/test/SemaObjC/format-strings-objc.m

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132568.455235.patch
Type: text/x-patch
Size: 19430 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220824/8bc788cc/attachment-0001.bin>


More information about the cfe-commits mailing list