forked from github/codeql-coding-standards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
189 lines (160 loc) · 3.17 KB
/
test.cpp
File metadata and controls
189 lines (160 loc) · 3.17 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
class B {
public:
int b;
};
class D {
public:
static int d;
};
class C : public D {};
class A : public C {
private:
int m;
static int m1;
public:
int f() { // NON_COMPLIANT
// can be static - only accesses own members that are statics
// can also be const for same reason
B b = B();
b.b = 1;
m1 = f3();
d = 7;
return m1;
}
int f1() { // COMPLIANT
// cannot be static -accesses non static m
return m++;
}
int f2() { // COMPLIANT
// cannot be static -accesses non static m
return m;
}
static int f3() {
// already static
// cannot be const, is static
return m1;
}
void f4() { // COMPLIANT
// cannot be static - calls non static f2
int l = f2();
}
};
class E {
public:
int m;
};
class F {
friend E &operator+(E &e, F *f);
E f1(E &e) { // COMPLIANT
// cannot be static - accesses this
// can be const though
return e + this;
}
private:
int m;
};
class G {
public:
int g;
};
class H : public G {};
class I : public H {
private:
int m;
public:
int f() { // NON_COMPLIANT
// can be const - only modifies other members
B b = B();
b.b = 1;
int l = f3();
l = g;
return m;
}
int f1() { // NON_COMPLIANT
// can be const - does not modify any member
int i = 1;
i = i + 1;
return m;
}
int f2() { // COMPLIANT
// cannot be const - modifies a member
return g++;
}
int f3() const {
// already const
return m;
}
void f4() { // COMPLIANT
// cannot be const - calls f5 which calls f2 which modifies a member
int l = f5();
}
int f5() { // COMPLIANT
// cannot be const - calls f2 which modifies a member
return f2();
}
void f6() { // COMPLIANT
// cannot be const - modifies this
I *i = new I();
*this = *i;
}
};
class Y {
public:
int y;
void setY() { // COMPLIANT
y += 1;
}
};
class X {
public:
Y y;
void callSet() { // COMPLIANT
y.setY();
}
};
class Z {
public:
int a;
virtual void f1() = 0; // COMPLIANT
void f2() {} // NON_COMPLIANT
virtual void f3() {} // NON_COMPLIANT
virtual void f4() const noexcept {} // COMPLIANT
virtual void f5() { this->a = 100; } // COMPLIANT
};
class Z1 {
public:
int a = 0;
virtual void f() = 0; // COMPLIANT
virtual void f2() { a = 100; } // COMPLIANT
virtual void f3() {} // NON_COMPLIANT
};
class Z2 : Z1 {
void f() override {} // COMPLIANT
void f2() override { this->a = 100; } // COMPLIANT
void f3() override {} // COMPLIANT
};
class Z22 : Z1 {
void f() override { this->a = 100; } // COMPLIANT
void f2() final {} // COMPLIANT
void f3() { this->a = 100; } // COMPLIANT
};
template <class T> class Array {
public:
T &back();
private:
T data[128];
unsigned int size;
};
template <class T, template <class...> class U> class Stack {
public:
T &Top() {
return this->data.back();
} // Likely NON_COMPLIANT, but cannot be determined until instantiation.
private:
U<T> data;
};
using IntVectorStack = Stack<int, Array>;
void test_template() {
IntVectorStack s;
int i = s.Top();
}