-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathtest.cpp
More file actions
160 lines (133 loc) · 3.35 KB
/
test.cpp
File metadata and controls
160 lines (133 loc) · 3.35 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/** Static functions with internal linkage */
static void f1() {} // NON_COMPLIANT - never used
static int f2() { return 0; } // COMPLIANT - used in an initializer
static void f3() {} // COMPLIANT - explicitly called in another function
static void f5(); // ignore declarations
static void f4() { // COMPLIANT - dead code cycle, but is called statically and
// explicitly as required by the rule
f5();
}
static void f5() { // COMPLIANT - dead code cycle, but is called statically and
// explicitly as required by the rule
f4();
}
static int foo = f2(); // Consider any variable initializer calling a function
// to be a sufficient use
void test_use_static() { f3(); }
template <class T> static T getAT() { // NON_COMPLIANT - never used
T t;
return t;
}
template <class T> static void setT(T t) { // COMPLIANT - used with setT<Foo>
}
class Foo {};
void test_template() {
Foo foo;
setT(foo);
}
/** Private member functions */
class A {
public:
void g1() {
g3();
g4();
}
private:
void g2() {} // NON_COMPLIANT - never called
void g3() {} // COMPLIANT - called by g1
virtual void g4() {} // COMPLIANT - called by g1
};
class B : A {
private:
virtual void g4() {} // COMPLIANT - potentially called by g1
};
template <class T> class C {
public:
void setT(T t) { _setT(t); }
private:
T getAT() { // NON_COMPLIANT - never used
T t;
return t;
}
void
_setT(T t) { // COMPLIANT - used in C<A>, which should make C<B>.setT live
}
};
void test_template_class() { // Ensure the templates are populated
C<A> c1;
A a;
c1.setT(a);
C<B> c;
}
/** Anonymous namespace functions */
namespace {
void h1() { // COMPLIANT - used below "statically and explicitly" as
// required by the rule
}
void h2() { h1(); } // NON_COMPLIANT
namespace foo {
namespace bar {
void h3() {} // NON_COMPLIANT
} // namespace bar
} // namespace foo
} // namespace
static int unevaluatedContextFn(int x) {
x++;
return x;
} // COMPLIANT - called in an unevaluated context.
#include <typeinfo>
static int unevalContextCaller() // COMPLIANT - address taken
{
typeid(unevaluatedContextFn(0));
sizeof(unevaluatedContextFn(1));
noexcept(unevaluatedContextFn(2));
decltype(unevaluatedContextFn(2)) n = 42;
return 0;
}
int (*ptr_unevalContextCaller)(void) = unevalContextCaller;
class X {
private:
[[maybe_unused]] void maybeUnused();
void deleted() = delete; // COMPLIANT - Deleted Function
};
void X::maybeUnused() {} // COMPLIANT - [[maybe_unused]]
static int overload1(int c) // COMPLIANT - called
{
return ++c;
}
static int overload1(int c, int d) // COMPLIANT - overload1(int) is called.
{
return c + d;
}
int overload = overload1(5);
class classWithOverloads {
public:
int caller(int x) { return overloadMember(x, 0); }
private:
int overloadMember(int c) // COMPLIANT - overloadMember(int, int) is called.
{
return ++c;
}
int overloadMember(int c, int d) // COMPLIANT - called.
{
return c + d;
}
};
namespace {
class C1 {
public:
void f() {} // NON_COMPLIANT - never used, internal linkage
};
namespace named {
class C2 {
public:
void f() {} // NON_COMPLIANT - never used, internal linkage
};
} // namespace named
} // namespace
namespace N1 {
class C3 {
public:
void f() {} // COMPLIANT - public external linkage
};
} // namespace N1