[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 22:42:24 PDT 2022


inclyc updated this revision to Diff 455474.
inclyc added a comment.

Add tests for character literals

I've noticed that Linus mentioned the following code triggered warning by
clang. (With a little modifications shown below)

Link: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=21f9c8a13bb2a0c24d9c6b86bc0896542a28c197

  printf("%hhd", 'a');
  printf("%hd", 'a');

character literals are `int` in C. So clang-14 generates warnings like this:

  local/format.c:9:20: warning: format specifies type 'char' but the argument has type 'int' [-Wformat]
      printf("%hhd", 'a');
              ~~~~   ^~~
              %d
  local/format.c:10:19: warning: format specifies type 'short' but the argument has type 'char' [-Wformat]
      printf("%hd", 'a');
              ~~~   ^~~
              %hhd
  2 warnings generated.

Ironically, we advise our users to change their source code, which leads to
another warning. I've added tests for this case, after this patch, only (%hd,
"char") will cause warnings. And clang will generate warning like this:

  local/format.c:10:19: warning: format specifies type 'short' but the argument has type 'char' [-Wformat]
      printf("%hd", 'a');
              ~~~   ^~~
              %hhd
  1 warning generated.

Use `%hd` with `char` is not reasonable. (Probabaly another misuse) So I kept
this warning as-is.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D132568/new/

https://reviews.llvm.org/D132568

Files:
  clang/docs/ReleaseNotes.rst
  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
  clang/www/c_status.html

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132568.455474.patch
Type: text/x-patch
Size: 24101 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220825/62ada256/attachment-0001.bin>


More information about the cfe-commits mailing list