blob: d663c0fb35e1385e34a2c9fce1a5af11971d80c6 [file] [log] [blame]
ZijunZhaoCCK73ed2152024-04-22 10:17:12 -07001// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-pedantic %s
2// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits -Wformat-pedantic %s 2>&1 | FileCheck %s
3// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits -Wformat %s -verify=okay
4// okay-no-diagnostics
Alex Brachet563a23c2023-07-14 16:23:22 +00005
6extern "C" int printf(const char *, ...);
Shoaib Meenai61c5ad82023-09-20 17:32:35 -07007#define LOG(...) printf(__VA_ARGS__)
Alex Brachet563a23c2023-07-14 16:23:22 +00008
9namespace N {
10 enum class E { One };
11}
12
Shoaib Meenai61c5ad82023-09-20 17:32:35 -070013struct S {
14 N::E Type;
15};
16
Shoaib Meenai0b07b062023-10-02 11:32:54 -070017using uint32_t = unsigned;
18enum class FixedE : uint32_t { Two };
19
Shoaib Meenai61c5ad82023-09-20 17:32:35 -070020void a(N::E NEVal, S *SPtr, S &SRef) {
Alex Brachet563a23c2023-07-14 16:23:22 +000021 printf("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
22 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast<int>("
23 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:25-[[@LINE-2]]:25}:")"
24
Shoaib Meenai61c5ad82023-09-20 17:32:35 -070025 printf("%hd", N::E::One); // expected-warning{{format specifies type 'short' but the argument has type 'N::E'}}
Shoaib Meenai0b07b062023-10-02 11:32:54 -070026 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%d"
27 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:17-[[@LINE-2]]:17}:"static_cast<int>("
28 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")"
Alex Brachet563a23c2023-07-14 16:23:22 +000029
Shoaib Meenai61c5ad82023-09-20 17:32:35 -070030 printf("%hu", N::E::One); // expected-warning{{format specifies type 'unsigned short' but the argument has type 'N::E'}}
Shoaib Meenai0b07b062023-10-02 11:32:54 -070031 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%d"
32 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:17-[[@LINE-2]]:17}:"static_cast<int>("
33 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")"
Shoaib Meenai61c5ad82023-09-20 17:32:35 -070034
35 LOG("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
36 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:"static_cast<int>("
37 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:22-[[@LINE-2]]:22}:")"
38
Shoaib Meenai0b07b062023-10-02 11:32:54 -070039 LOG("%s", N::E::One); // expected-warning{{format specifies type 'char *' but the argument has type 'N::E'}}
40 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:8-[[@LINE-1]]:10}:"%d"
41 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"static_cast<int>("
42 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:22-[[@LINE-3]]:22}:")"
43
Shoaib Meenai61c5ad82023-09-20 17:32:35 -070044 printf("%d", NEVal); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
45 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast<int>("
46 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:21-[[@LINE-2]]:21}:")"
47
48 LOG("%d", NEVal); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
49 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:"static_cast<int>("
50 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:18-[[@LINE-2]]:18}:")"
51
52 printf(
53 "%d",
54 SPtr->Type // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
55 );
56 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:7-[[@LINE-2]]:7}:"static_cast<int>("
57 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:17}:")"
58
59 LOG( // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
60 "%d",
61 SPtr->Type
62 );
63 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:7-[[@LINE-2]]:7}:"static_cast<int>("
64 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:17}:")"
65
66 printf("%d",
67 SRef.Type); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
68 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:7}:"static_cast<int>("
69 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:")"
70
71 LOG("%d", // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
72 SRef.Type);
73 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:7}:"static_cast<int>("
74 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:")"
Shoaib Meenai0b07b062023-10-02 11:32:54 -070075
76 printf("%u", FixedE::Two); //expected-warning{{format specifies type 'unsigned int' but the argument has type 'FixedE'}}
77 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast<uint32_t>("
78 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:27-[[@LINE-2]]:27}:")"
Alex Brachet563a23c2023-07-14 16:23:22 +000079}