[PATCH] D130906: [clang] format string checking for conpile-time evaluated str literal

YingChi Long via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 1 08:19:02 PDT 2022


inclyc created this revision.
Herald added a project: All.
inclyc edited the summary of this revision.
inclyc updated this revision to Diff 449023.
inclyc added a comment.
inclyc added reviewers: mizvekov, rsmith, aaron.ballman.
inclyc added a project: clang.
inclyc added a subscriber: clang.
inclyc published this revision for review.
Herald added a subscriber: cfe-commits.

add newline


This patch enhances clang's ability to check compile-time determinable
string literals as format strings, and can give FixIt hints at literals
(unlike gcc). Issue https://github.com/llvm/llvm-project/issues/55805
mentiond two compile-time string cases. And this patch partially fixes
one.

  constexpr const char* foo() {
    return "%s %d";
  }
  int main() {
     printf(foo(), "abc", "def");
     return 0;
  }

This patch enables clang check format string for this:

  <source>:4:24: warning: format specifies type 'int' but the argument has type 'const char *' [-Wformat]
    printf(foo(), "abc", "def");
           ~~~~~         ^~~~~
  <source>:2:42: note: format string is defined here
  constexpr const char *foo() { return "%s %d"; }
                                           ^~
                                           %s
  1 warning generated.

Signed-off-by: YingChi Long <me at inclyc.cn>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D130906

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/Sema/format-strings-scanf.c
  clang/test/Sema/format-strings.c
  clang/test/SemaCXX/format-strings.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130906.449023.patch
Type: text/x-patch
Size: 5425 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220801/752b2d35/attachment.bin>


More information about the cfe-commits mailing list