xref: /llvm-project/clang/test/FixIt/format.cpp (revision 73ed2153beb529973741344874c0084d24c2f268)
1*73ed2153SZijunZhaoCCK // RUN: %clang_cc1 -fsyntax-only -verify -Wformat-pedantic %s
2*73ed2153SZijunZhaoCCK // RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits -Wformat-pedantic %s 2>&1 | FileCheck %s
3*73ed2153SZijunZhaoCCK // RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits -Wformat %s -verify=okay
4*73ed2153SZijunZhaoCCK // okay-no-diagnostics
5563a23c8SAlex Brachet 
6563a23c8SAlex Brachet extern "C" int printf(const char *, ...);
761c5ad88SShoaib Meenai #define LOG(...) printf(__VA_ARGS__)
8563a23c8SAlex Brachet 
9563a23c8SAlex Brachet namespace N {
10563a23c8SAlex Brachet   enum class E { One };
11563a23c8SAlex Brachet }
12563a23c8SAlex Brachet 
1361c5ad88SShoaib Meenai struct S {
1461c5ad88SShoaib Meenai   N::E Type;
1561c5ad88SShoaib Meenai };
1661c5ad88SShoaib Meenai 
170b07b06eSShoaib Meenai using uint32_t = unsigned;
180b07b06eSShoaib Meenai enum class FixedE : uint32_t { Two };
190b07b06eSShoaib Meenai 
a(N::E NEVal,S * SPtr,S & SRef)2061c5ad88SShoaib Meenai void a(N::E NEVal, S *SPtr, S &SRef) {
21563a23c8SAlex Brachet   printf("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
22563a23c8SAlex Brachet   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast<int>("
23563a23c8SAlex Brachet   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:25-[[@LINE-2]]:25}:")"
24563a23c8SAlex Brachet 
2561c5ad88SShoaib Meenai   printf("%hd", N::E::One); // expected-warning{{format specifies type 'short' but the argument has type 'N::E'}}
260b07b06eSShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%d"
270b07b06eSShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:17-[[@LINE-2]]:17}:"static_cast<int>("
280b07b06eSShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")"
29563a23c8SAlex Brachet 
3061c5ad88SShoaib Meenai   printf("%hu", N::E::One); // expected-warning{{format specifies type 'unsigned short' but the argument has type 'N::E'}}
310b07b06eSShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%d"
320b07b06eSShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:17-[[@LINE-2]]:17}:"static_cast<int>("
330b07b06eSShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")"
3461c5ad88SShoaib Meenai 
3561c5ad88SShoaib Meenai   LOG("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
3661c5ad88SShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:"static_cast<int>("
3761c5ad88SShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:22-[[@LINE-2]]:22}:")"
3861c5ad88SShoaib Meenai 
390b07b06eSShoaib Meenai   LOG("%s", N::E::One); // expected-warning{{format specifies type 'char *' but the argument has type 'N::E'}}
400b07b06eSShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:8-[[@LINE-1]]:10}:"%d"
410b07b06eSShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"static_cast<int>("
420b07b06eSShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:22-[[@LINE-3]]:22}:")"
430b07b06eSShoaib Meenai 
4461c5ad88SShoaib Meenai   printf("%d", NEVal); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
4561c5ad88SShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast<int>("
4661c5ad88SShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:21-[[@LINE-2]]:21}:")"
4761c5ad88SShoaib Meenai 
4861c5ad88SShoaib Meenai   LOG("%d", NEVal); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
4961c5ad88SShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:"static_cast<int>("
5061c5ad88SShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:18-[[@LINE-2]]:18}:")"
5161c5ad88SShoaib Meenai 
5261c5ad88SShoaib Meenai   printf(
5361c5ad88SShoaib Meenai       "%d",
5461c5ad88SShoaib Meenai       SPtr->Type // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
5561c5ad88SShoaib Meenai   );
5661c5ad88SShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:7-[[@LINE-2]]:7}:"static_cast<int>("
5761c5ad88SShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:17}:")"
5861c5ad88SShoaib Meenai 
5961c5ad88SShoaib Meenai   LOG( // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
6061c5ad88SShoaib Meenai       "%d",
6161c5ad88SShoaib Meenai       SPtr->Type
6261c5ad88SShoaib Meenai   );
6361c5ad88SShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:7-[[@LINE-2]]:7}:"static_cast<int>("
6461c5ad88SShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:17}:")"
6561c5ad88SShoaib Meenai 
6661c5ad88SShoaib Meenai   printf("%d",
6761c5ad88SShoaib Meenai       SRef.Type); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
6861c5ad88SShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:7}:"static_cast<int>("
6961c5ad88SShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:")"
7061c5ad88SShoaib Meenai 
7161c5ad88SShoaib Meenai   LOG("%d", // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
7261c5ad88SShoaib Meenai       SRef.Type);
7361c5ad88SShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:7}:"static_cast<int>("
7461c5ad88SShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:")"
750b07b06eSShoaib Meenai 
760b07b06eSShoaib Meenai   printf("%u", FixedE::Two); //expected-warning{{format specifies type 'unsigned int' but the argument has type 'FixedE'}}
770b07b06eSShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast<uint32_t>("
780b07b06eSShoaib Meenai   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:27-[[@LINE-2]]:27}:")"
79563a23c8SAlex Brachet }
80