analyzer: add heuristics for switch on enum type [PR105273]
[platform/upstream/gcc.git] / gcc / testsuite / gcc.dg / analyzer / switch-enum-pr105273-git-vreportf-2.c
1 /* Currently the warning only fires at -O0
2    (needs to inline the call without optimizing the
3    implicit default of the switch).  */
4
5 /* { dg-additional-options "-O0" } */
6
7 typedef __SIZE_TYPE__ size_t;
8 int snprintf(char *str, size_t size, const char *format, ...);
9
10 enum usage_kind {
11         USAGE_ERROR,
12         USAGE_BUG,
13 };
14
15 static void __analyzer_vreportf(enum usage_kind kind)
16 {
17         char buf[256];
18         const char *pfx;
19
20         switch (kind) { /* { dg-message "following 'default:' branch" } */
21         case USAGE_ERROR:
22                 pfx = "error: ";
23                 break;
24         case USAGE_BUG:
25                 pfx = "BUG: ";
26                 break;
27         }
28
29         if (kind == USAGE_BUG)
30                 snprintf(buf, sizeof(buf), "%s%s:%d: ", pfx, "file", 123);
31         else
32                 snprintf(buf, sizeof(buf), "%s", pfx); /* { dg-warning "uninitialized" } */
33 }
34
35 int main(void)
36 {
37         __analyzer_vreportf(42);
38
39         return 0;
40 }