1// RUN: %check_clang_tidy %s readability-redundant-declaration %t -- \
2// RUN: -config="{CheckOptions: \
3// RUN: {readability-redundant-declaration.IgnoreMacros: \
4// RUN: false}}"
5//
6// With -fms-compatibility and -DEXTERNINLINE, the extern inline shouldn't
7// produce additional diagnostics, so same check suffix as before:
8// RUN: %check_clang_tidy %s readability-redundant-declaration %t -- \
9// RUN: -config="{CheckOptions: \
10// RUN: {readability-redundant-declaration.IgnoreMacros: \
11// RUN: false}}" -- -fms-compatibility -DEXTERNINLINE
12//
13// With -fno-ms-compatibility, DEXTERNINLINE causes additional output.
14// (The leading ',' means "default checks in addition to NOMSCOMPAT checks.)
15// RUN: %check_clang_tidy -check-suffix=,NOMSCOMPAT \
16// RUN: %s readability-redundant-declaration %t -- \
17// RUN: -config="{CheckOptions: \
18// RUN: {readability-redundant-declaration.IgnoreMacros: \
19// RUN: false}}" -- -fno-ms-compatibility -DEXTERNINLINE
20
21extern int Xyz;
22extern int Xyz; // Xyz
23// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant 'Xyz' declaration [readability-redundant-declaration]
24// CHECK-FIXES: {{^}}// Xyz{{$}}
25int Xyz = 123;
26
27extern int A;
28extern int A, B;
29// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant 'A' declaration
30// CHECK-FIXES: {{^}}extern int A, B;{{$}}
31
32extern int Buf[10];
33extern int Buf[10]; // Buf[10]
34// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant 'Buf' declaration
35// CHECK-FIXES: {{^}}// Buf[10]{{$}}
36
37static int f();
38static int f(); // f
39// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant 'f' declaration
40// CHECK-FIXES: {{^}}// f{{$}}
41static int f() {}
42
43// Original check crashed for the code below.
44namespace std {
45typedef decltype(sizeof(0)) size_t;
46}
47void *operator new(std::size_t) __attribute__((__externally_visible__));
48void *operator new[](std::size_t) __attribute__((__externally_visible__));
49
50// Don't warn about static member definition.
51struct C {
52 static int I;
53};
54int C::I;
55
56template <class T>
57struct C2 {
58 C2();
59};
60
61template <class T>
62C2<T>::C2() = default;
63
64void best_friend();
65
66struct Friendly {
67 friend void best_friend();
68 friend void enemy();
69};
70
71void enemy();
72
73template <typename>
74struct TemplateFriendly {
75 template <typename T>
76 friend void generic_friend();
77};
78
79template <typename T>
80void generic_friend() {}
81
82TemplateFriendly<int> template_friendly;
83
84template <typename>
85struct TemplateFriendly2 {
86 template <typename T>
87 friend void generic_friend2() {}
88};
89
90template <typename T>
91void generic_friend2();
92
93void generic_friend_caller() {
94 TemplateFriendly2<int> f;
95 generic_friend2<int>();
96}
97
98
99namespace macros {
100#define DECLARE(x) extern int x
101#define DEFINE(x) extern int x; int x = 42
102DECLARE(test);
103DEFINE(test);
104// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant 'test' declaration
105// CHECK-FIXES: {{^}}#define DECLARE(x) extern int x{{$}}
106// CHECK-FIXES: {{^}}#define DEFINE(x) extern int x; int x = 42{{$}}
107// CHECK-FIXES: {{^}}DECLARE(test);{{$}}
108// CHECK-FIXES: {{^}}DEFINE(test);{{$}}
109
110} // namespace macros
111
112inline void g() {}
113
114inline void g(); // g
115// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant 'g' declaration
116// CHECK-FIXES: {{^}}// g{{$}}
117
118#if defined(EXTERNINLINE)
119extern inline void g(); // extern g
120// CHECK-MESSAGES-NOMSCOMPAT: :[[@LINE-1]]:20: warning: redundant 'g' declaration
121// CHECK-FIXES-NOMSCOMPAT: {{^}}// extern g{{$}}
122#endif
123
124// PR42068
125extern "C" int externX;
126int dummyBeforeBegin;extern "C" int externX;int dummyAfterEnd;
127// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: redundant 'externX' declaration [readability-redundant-declaration]
128// CHECK-FIXES: {{^}}int dummyBeforeBegin;int dummyAfterEnd;{{$}}
129

source code of clang-tools-extra/test/clang-tidy/checkers/readability/redundant-declaration.cpp