[PATCH] D131314: [clang] format string checks for `InitListExpr`

YingChi Long via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 6 01:18:37 PDT 2022


inclyc created this revision.
Herald added a project: All.
inclyc added reviewers: aaron.ballman, rsmith, mizvekov, tbaeder.
inclyc added a project: clang.
inclyc added a subscriber: clang.
inclyc published this revision for review.
Herald added a subscriber: cfe-commits.

this patch enhances clang check format strings defined in list
initialization expressions. Before this patch clang just thought these
expressions are not string literal.

Example:

  constexpr const char *foo() { return "%s %d"; }
  
  struct Bar {
    static constexpr char value[] = {'%', 's', '%', 'd', '\0'};
  };
  
  constexpr const char *foobar() { return Bar::value; }
  
  constexpr const char *foooobar() { return foobar(); }
  
  int main() {
    printf(foo(), "abc", "def");
    printf(Bar::value, "abc", "def");
    printf(foobar(), "abc", "def");
    printf(foooobar(), "abc", "def");
    return 0;
  }

Diagnostics after this patch:

  <source>:14:24: warning: format specifies type 'int' but the argument has type 'const char *' [-Wformat]
    printf(foo(), "abc", "def");
           ~~~~~         ^~~~~
  <source>:3:42: note: format string is defined here
  constexpr const char *foo() { return "%s %d"; }
                                           ^~
                                           %s
  <source>:15:29: warning: format specifies type 'int' but the argument has type 'const char *' [-Wformat]
    printf(Bar::value, "abc", "def");
                              ^~~~~
  <source>:16:27: warning: format specifies type 'int' but the argument has type 'const char *' [-Wformat]
    printf(foobar(), "abc", "def");
                            ^~~~~
  <source>:17:29: warning: format specifies type 'int' but the argument has type 'const char *' [-Wformat]
    printf(foooobar(), "abc", "def");
                              ^~~~~
  4 warnings generated.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131314

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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131314.450491.patch
Type: text/x-patch
Size: 32329 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220806/e37af873/attachment-0001.bin>


More information about the cfe-commits mailing list