warn-missing-variable-declarations.cpp
2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-variable-declarations -std=c++17 %s
// Variable declarations that should trigger a warning.
int vbad1; // expected-warning{{no previous extern declaration for non-static variable 'vbad1'}}
// expected-note@-1{{declare 'static' if the variable is not intended to be used outside of this translation unit}}
int vbad2 = 10; // expected-warning{{no previous extern declaration for non-static variable 'vbad2'}}
// expected-note@-1{{declare 'static' if the variable is not intended to be used outside of this translation unit}}
namespace x {
int vbad3; // expected-warning{{no previous extern declaration for non-static variable 'vbad3'}}
// expected-note@-1{{declare 'static' if the variable is not intended to be used outside of this translation unit}}
}
// Variable declarations that should not trigger a warning.
static int vgood1;
extern int vgood2;
int vgood2;
static struct {
int mgood1;
} vgood3;
// Functions should never trigger a warning.
void fgood1(void);
void fgood2(void) {
int lgood1;
static int lgood2;
}
static void fgood3(void) {
int lgood3;
static int lgood4;
}
// Structures, namespaces and classes should be unaffected.
struct sgood1 {
int mgood2;
};
struct {
int mgood3;
} sgood2;
class CGood1 {
static int MGood1;
};
int CGood1::MGood1;
namespace {
int mgood4;
}
class C {
void test() {
static int x = 0; // no-warn
}
};
// There is also no need to use static in anonymous namespaces.
namespace {
int vgood4;
}
inline int inline_var = 0;
const int const_var = 0;
constexpr int constexpr_var = 0;
inline constexpr int inline_constexpr_var = 0;
extern const int extern_const_var = 0; // expected-warning {{no previous extern declaration}}
// expected-note@-1{{declare 'static' if the variable is not intended to be used outside of this translation unit}}
extern constexpr int extern_constexpr_var = 0; // expected-warning {{no previous extern declaration}}
// expected-note@-1{{declare 'static' if the variable is not intended to be used outside of this translation unit}}
template<typename> int var_template = 0;
template<typename> constexpr int const_var_template = 0;
template<typename> static int static_var_template = 0;
template<typename T> int var_template<T*>;
template int var_template<int[1]>;
int use_var_template() { return var_template<int[2]>; }
template int var_template<int[3]>;
extern template int var_template<int[4]>;
template<> int var_template<int[5]>; // expected-warning {{no previous extern declaration}}
// expected-note@-1{{declare 'static' if the variable is not intended to be used outside of this translation unit}}
// FIXME: We give this specialization internal linkage rather than inheriting
// the linkage from the template! We should not warn here.
template<> int static_var_template<int[5]>; // expected-warning {{no previous extern declaration}}
// expected-note@-1{{declare 'static' if the variable is not intended to be used outside of this translation unit}}