[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 23:41:50 PDT 2022
inclyc added inline comments.
================
Comment at: clang/test/Sema/format-strings-scanf.c:236
+ scanf(1 ? "%s" : "%d", i); // expected-warning{{format specifies type 'char *'}} \
+ // expected-note{{format string is defined here}}
scanf(0 ? "%d %d" : "%d", i); // no warning
----------------
```
// long macro
#include <cstdio>
#define SOME_STRANGE_MACRO(a, b) ((0) ? (a) : (b))
int main() {
int *i;
scanf(SOME_STRANGE_MACRO("%d", "%d %s"), i);
}
```
previous:
```
sample.cpp:7:39: warning: more '%' conversions than data arguments [-Wformat-insufficient-args]
scanf(SOME_STRANGE_MACRO("%d", "%d %s"), i);
~^
sample.cpp:3:48: note: expanded from macro 'SOME_STRANGE_MACRO'
#define SOME_STRANGE_MACRO(a, b) ((0) ? (a) : (b))
^
1 warning generated.
```
now:
```
sample.cpp:7:9: warning: more '%' conversions than data arguments [-Wformat-insufficient-args]
scanf(SOME_STRANGE_MACRO("%d", "%d %s"), i);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sample.cpp:3:34: note: expanded from macro 'SOME_STRANGE_MACRO'
#define SOME_STRANGE_MACRO(a, b) ((0) ? (a) : (b))
^~~~~~~~~~~~~~~~~
sample.cpp:7:39: note: format string is defined here
scanf(SOME_STRANGE_MACRO("%d", "%d %s"), i);
~^
sample.cpp:3:48: note: expanded from macro 'SOME_STRANGE_MACRO'
#define SOME_STRANGE_MACRO(a, b) ((0) ? (a) : (b))
^
1 warning generated.
```
I think it is better to underline the buggy expression, if this sucks maybe we can check if this is constexpr **after** checking conditional operator ` ? :` or somehow other statement classes ?
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D130906/new/
https://reviews.llvm.org/D130906
More information about the cfe-commits
mailing list