forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testclass.cpp
7501 lines (6729 loc) · 304 KB
/
testclass.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2021 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <tinyxml2.h>
#include "checkclass.h"
#include "library.h"
#include "settings.h"
#include "testsuite.h"
#include "tokenize.h"
class TestClass : public TestFixture {
public:
TestClass() : TestFixture("TestClass") {}
private:
Settings settings0;
Settings settings1;
void run() OVERRIDE {
settings0.severity.enable(Severity::style);
settings1.severity.enable(Severity::warning);
// Load std.cfg configuration
{
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <memory>\n"
" <alloc init=\"false\">malloc</alloc>\n"
" <dealloc>free</dealloc>\n"
" </memory>\n"
" <smart-pointer class-name=\"std::shared_ptr\"/>\n"
"</def>";
tinyxml2::XMLDocument doc;
doc.Parse(xmldata, sizeof(xmldata));
settings0.library.load(doc);
settings1.library.load(doc);
}
TEST_CASE(virtualDestructor1); // Base class not found => no error
TEST_CASE(virtualDestructor2); // Base class doesn't have a destructor
TEST_CASE(virtualDestructor3); // Base class has a destructor, but it's not virtual
TEST_CASE(virtualDestructor4); // Derived class doesn't have a destructor => no error
TEST_CASE(virtualDestructor5); // Derived class has empty destructor => no error
TEST_CASE(virtualDestructor6); // only report error if base class pointer that points at derived class is deleted
TEST_CASE(virtualDestructorProtected);
TEST_CASE(virtualDestructorInherited);
TEST_CASE(virtualDestructorTemplate);
TEST_CASE(virtualDestructorInconclusive); // ticket # 5807
TEST_CASE(copyConstructor1);
TEST_CASE(copyConstructor2); // ticket #4458
TEST_CASE(copyConstructor3); // defaulted/deleted
TEST_CASE(copyConstructor4); // base class with private constructor
TEST_CASE(copyConstructor5); // multiple inheritance
TEST_CASE(noOperatorEq); // class with memory management should have operator eq
TEST_CASE(noDestructor); // class with memory management should have destructor
TEST_CASE(operatorEqRetRefThis1);
TEST_CASE(operatorEqRetRefThis2); // ticket #1323
TEST_CASE(operatorEqRetRefThis3); // ticket #1405
TEST_CASE(operatorEqRetRefThis4); // ticket #1451
TEST_CASE(operatorEqRetRefThis5); // ticket #1550
TEST_CASE(operatorEqRetRefThis6); // ticket #2479
TEST_CASE(operatorEqRetRefThis7); // ticket #5782 endless recursion
TEST_CASE(operatorEqToSelf1); // single class
TEST_CASE(operatorEqToSelf2); // nested class
TEST_CASE(operatorEqToSelf3); // multiple inheritance
TEST_CASE(operatorEqToSelf4); // nested class with multiple inheritance
TEST_CASE(operatorEqToSelf5); // ticket # 1233
TEST_CASE(operatorEqToSelf6); // ticket # 1550
TEST_CASE(operatorEqToSelf7);
TEST_CASE(operatorEqToSelf8); // ticket #2179
TEST_CASE(operatorEqToSelf9); // ticket #2592
TEST_CASE(memsetOnStruct);
TEST_CASE(memsetVector);
TEST_CASE(memsetOnClass);
TEST_CASE(memsetOnInvalid); // Ticket #5425: Crash upon invalid
TEST_CASE(memsetOnStdPodType); // Ticket #5901 - std::uint8_t
TEST_CASE(memsetOnFloat); // Ticket #5421
TEST_CASE(memsetOnUnknown); // Ticket #7183
TEST_CASE(mallocOnClass);
TEST_CASE(this_subtraction); // warn about "this-x"
// can member function be made const
TEST_CASE(const1);
TEST_CASE(const2);
TEST_CASE(const3);
TEST_CASE(const4);
TEST_CASE(const5); // ticket #1482
TEST_CASE(const6); // ticket #1491
TEST_CASE(const7);
TEST_CASE(const8); // ticket #1517
TEST_CASE(const9); // ticket #1515
TEST_CASE(const10); // ticket #1522
TEST_CASE(const11); // ticket #1529
TEST_CASE(const12); // ticket #1552
TEST_CASE(const13); // ticket #1519
TEST_CASE(const14);
TEST_CASE(const15);
TEST_CASE(const16); // ticket #1551
TEST_CASE(const17); // ticket #1552
TEST_CASE(const18);
TEST_CASE(const19); // ticket #1612
TEST_CASE(const20); // ticket #1602
TEST_CASE(const21); // ticket #1683
TEST_CASE(const22);
TEST_CASE(const23); // ticket #1699
TEST_CASE(const24); // ticket #1708
TEST_CASE(const25); // ticket #1724
TEST_CASE(const26); // ticket #1847
TEST_CASE(const27); // ticket #1882
TEST_CASE(const28); // ticket #1883
TEST_CASE(const29); // ticket #1922
TEST_CASE(const30);
TEST_CASE(const31);
TEST_CASE(const32); // ticket #1905 - member array is assigned
TEST_CASE(const33);
TEST_CASE(const34); // ticket #1964
TEST_CASE(const35); // ticket #2001
TEST_CASE(const36); // ticket #2003
TEST_CASE(const37); // ticket #2081 and #2085
TEST_CASE(const38); // ticket #2135
TEST_CASE(const39);
TEST_CASE(const40); // ticket #2228
TEST_CASE(const41); // ticket #2255
TEST_CASE(const42); // ticket #2282
TEST_CASE(const43); // ticket #2377
TEST_CASE(const44); // ticket #2595
TEST_CASE(const45); // ticket #2664
TEST_CASE(const46); // ticket #2636
TEST_CASE(const47); // ticket #2670
TEST_CASE(const48); // ticket #2672
TEST_CASE(const49); // ticket #2795
TEST_CASE(const50); // ticket #2943
TEST_CASE(const51); // ticket #3040
TEST_CASE(const52); // ticket #3048
TEST_CASE(const53); // ticket #3049
TEST_CASE(const54); // ticket #3052
TEST_CASE(const55);
TEST_CASE(const56); // ticket #3149
TEST_CASE(const57); // tickets #2669 and #2477
TEST_CASE(const58); // ticket #2698
TEST_CASE(const59); // ticket #4646
TEST_CASE(const60); // ticket #3322
TEST_CASE(const61); // ticket #5606
TEST_CASE(const62); // ticket #5701
TEST_CASE(const63); // ticket #5983
TEST_CASE(const64); // ticket #6268
TEST_CASE(const65); // ticket #8693
TEST_CASE(const66); // ticket #7714
TEST_CASE(const67); // ticket #9193
TEST_CASE(const68); // ticket #6471
TEST_CASE(const69); // ticket #9806
TEST_CASE(const70); // variadic template can receive more arguments than in its definition
TEST_CASE(const_handleDefaultParameters);
TEST_CASE(const_passThisToMemberOfOtherClass);
TEST_CASE(assigningPointerToPointerIsNotAConstOperation);
TEST_CASE(assigningArrayElementIsNotAConstOperation);
TEST_CASE(constoperator1); // operator< can often be const
TEST_CASE(constoperator2); // operator<<
TEST_CASE(constoperator3);
TEST_CASE(constoperator4);
TEST_CASE(constoperator5); // ticket #3252
TEST_CASE(constoperator6); // ticket #8669
TEST_CASE(constincdec); // increment/decrement => non-const
TEST_CASE(constassign1);
TEST_CASE(constassign2);
TEST_CASE(constincdecarray); // increment/decrement array element => non-const
TEST_CASE(constassignarray);
TEST_CASE(constReturnReference);
TEST_CASE(constDelete); // delete member variable => not const
TEST_CASE(constLPVOID); // a function that returns LPVOID can't be const
TEST_CASE(constFunc); // a function that calls const functions can be const
TEST_CASE(constVirtualFunc);
TEST_CASE(constIfCfg); // ticket #1881 - fp when there are #if
TEST_CASE(constFriend); // ticket #1921 - fp for friend function
TEST_CASE(constUnion); // ticket #2111 - fp when there is a union
TEST_CASE(constArrayOperator); // #4406
TEST_CASE(constRangeBasedFor); // #5514
TEST_CASE(const_shared_ptr);
TEST_CASE(constPtrToConstPtr);
TEST_CASE(constTrailingReturnType);
TEST_CASE(initializerListOrder);
TEST_CASE(initializerListUsage);
TEST_CASE(selfInitialization);
TEST_CASE(virtualFunctionCallInConstructor);
TEST_CASE(pureVirtualFunctionCall);
TEST_CASE(pureVirtualFunctionCallOtherClass);
TEST_CASE(pureVirtualFunctionCallWithBody);
TEST_CASE(pureVirtualFunctionCallPrevented);
TEST_CASE(duplInheritedMembers);
TEST_CASE(explicitConstructors);
TEST_CASE(copyCtorAndEqOperator);
TEST_CASE(override1);
TEST_CASE(overrideCVRefQualifiers);
TEST_CASE(checkThisUseAfterFree);
TEST_CASE(unsafeClassRefMember);
TEST_CASE(ctuOneDefinitionRule);
TEST_CASE(getFileInfo);
}
void checkCopyCtorAndEqOperator(const char code[]) {
// Clear the error log
errout.str("");
Settings settings;
settings.severity.enable(Severity::warning);
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Check..
CheckClass checkClass(&tokenizer, &settings, this);
checkClass.checkCopyCtorAndEqOperator();
}
void copyCtorAndEqOperator() {
checkCopyCtorAndEqOperator("class A\n"
"{\n"
" A(const A& other) { }\n"
" A& operator=(const A& other) { return *this; }\n"
"};");
ASSERT_EQUALS("", errout.str());
checkCopyCtorAndEqOperator("class A\n"
"{\n"
"};");
ASSERT_EQUALS("", errout.str());
checkCopyCtorAndEqOperator("class A\n"
"{\n"
" A(const A& other) { }\n"
"};");
ASSERT_EQUALS("", errout.str());
checkCopyCtorAndEqOperator("class A\n"
"{\n"
" A& operator=(const A& other) { return *this; }\n"
"};");
ASSERT_EQUALS("", errout.str());
checkCopyCtorAndEqOperator("class A\n"
"{\n"
" A(const A& other) { }\n"
" int x;\n"
"};");
TODO_ASSERT_EQUALS("[test.cpp:1]: (warning) The class 'A' has 'copy constructor' but lack of 'operator='.\n", "", errout.str());
// TODO the error message should be clarified. It should say something like 'copy constructor is empty and will not assign i and therefore the behaviour is different to the default assignment operator'
checkCopyCtorAndEqOperator("class A\n"
"{\n"
" A& operator=(const A& other) { return *this; }\n"
" int x;\n"
"};");
TODO_ASSERT_EQUALS("[test.cpp:1]: (warning) The class 'A' has 'operator=' but lack of 'copy constructor'.\n", "", errout.str());
// TODO the error message should be clarified. It should say something like 'assignment operator does not assign i and therefore the behaviour is different to the default copy constructor'
checkCopyCtorAndEqOperator("class A\n"
"{\n"
" A& operator=(const int &x) { this->x = x; return *this; }\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout.str());
checkCopyCtorAndEqOperator("class A {\n"
"public:\n"
" A() : x(0) { }\n"
" A(const A & a) { x = a.x; }\n"
" A & operator = (const A & a) {\n"
" x = a.x;\n"
" return *this;\n"
" }\n"
"private:\n"
" int x;\n"
"};\n"
"class B : public A {\n"
"public:\n"
" B() { }\n"
" B(const B & b) :A(b) { }\n"
"private:\n"
" static int i;\n"
"};");
ASSERT_EQUALS("", errout.str());
// #7987 - Don't show warning when there is a move constructor
checkCopyCtorAndEqOperator("struct S {\n"
" std::string test;\n"
" S(S&& s) : test(std::move(s.test)) { }\n"
" S& operator = (S &&s) {\n"
" test = std::move(s.test);\n"
" return *this;\n"
" }\n"
"};");
ASSERT_EQUALS("", errout.str());
// #8337 - False positive in copy constructor detection
checkCopyCtorAndEqOperator("struct StaticListNode {\n"
" StaticListNode(StaticListNode*& prev) : m_next(0) {}\n"
" StaticListNode* m_next;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void checkExplicitConstructors(const char code[]) {
// Clear the error log
errout.str("");
// Tokenize..
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Check..
CheckClass checkClass(&tokenizer, &settings0, this);
checkClass.checkExplicitConstructors();
}
void explicitConstructors() {
checkExplicitConstructors("class Class {\n"
" Class() = delete;\n"
" Class(const Class& other) { }\n"
" Class(Class&& other) { }\n"
" explicit Class(int i) { }\n"
" explicit Class(const std::string&) { }\n"
" Class(int a, int b) { }\n"
"};");
ASSERT_EQUALS("", errout.str());
checkExplicitConstructors("class Class {\n"
" Class() = delete;\n"
" explicit Class(const Class& other) { }\n"
" explicit Class(Class&& other) { }\n"
" virtual int i() = 0;\n"
"};");
ASSERT_EQUALS("", errout.str());
checkExplicitConstructors("class Class {\n"
" Class() = delete;\n"
" Class(const Class& other) = delete;\n"
" Class(Class&& other) = delete;\n"
" virtual int i() = 0;\n"
"};");
ASSERT_EQUALS("", errout.str());
checkExplicitConstructors("class Class {\n"
" Class(int i) { }\n"
"};");
ASSERT_EQUALS("[test.cpp:2]: (style) Class 'Class' has a constructor with 1 argument that is not explicit.\n", errout.str());
checkExplicitConstructors("class Class {\n"
" Class(const Class& other) { }\n"
" virtual int i() = 0;\n"
"};");
ASSERT_EQUALS("", errout.str());
checkExplicitConstructors("class Class {\n"
" Class(Class&& other) { }\n"
" virtual int i() = 0;\n"
"};");
ASSERT_EQUALS("", errout.str());
// #6585
checkExplicitConstructors("class Class {\n"
" private: Class(const Class&);\n"
" virtual int i() = 0;\n"
"};");
ASSERT_EQUALS("", errout.str());
checkExplicitConstructors("class Class {\n"
" public: Class(const Class&);\n"
" virtual int i() = 0;\n"
"};");
ASSERT_EQUALS("", errout.str());
// #7465: Error properly reported in templates
checkExplicitConstructors("template <class T> struct Test {\n"
" Test(int) : fData(0) {}\n"
" T fData;\n"
"};\n"
"int main() {\n"
" Test <int> test;\n"
" return 0;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Struct 'Test < int >' has a constructor with 1 argument that is not explicit.\n", errout.str());
// #7465: No error for copy or move constructors
checkExplicitConstructors("template <class T> struct Test {\n"
" Test() : fData(0) {}\n"
" Test (const Test<T>& aOther) : fData(aOther.fData) {}\n"
" Test (Test<T>&& aOther) : fData(std::move(aOther.fData)) {}\n"
" T fData;\n"
"};\n"
"int main() {\n"
" Test <int> test;\n"
" return 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
// #8600
checkExplicitConstructors("struct A { struct B; };\n"
"struct A::B {\n"
" B() = default;\n"
" B(const B&) {}\n"
"};");
ASSERT_EQUALS("", errout.str());
checkExplicitConstructors("struct A{"
" A(int, int y=2) {}"
"};");
ASSERT_EQUALS("[test.cpp:1]: (style) Struct 'A' has a constructor with 1 argument that is not explicit.\n", errout.str());
}
void checkDuplInheritedMembers(const char code[]) {
// Clear the error log
errout.str("");
// Tokenize..
Tokenizer tokenizer(&settings1, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Check..
CheckClass checkClass(&tokenizer, &settings1, this);
checkClass.checkDuplInheritedMembers();
}
void duplInheritedMembers() {
checkDuplInheritedMembers("class Base {\n"
" int x;\n"
"};\n"
"struct Derived : Base {\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout.str());
checkDuplInheritedMembers("class Base {\n"
" protected:\n"
" int x;\n"
"};\n"
"struct Derived : Base {\n"
" int x;\n"
"};");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (warning) The struct 'Derived' defines member variable with name 'x' also defined in its parent class 'Base'.\n", errout.str());
checkDuplInheritedMembers("class Base {\n"
" protected:\n"
" int x;\n"
"};\n"
"struct Derived : public Base {\n"
" int x;\n"
"};");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (warning) The struct 'Derived' defines member variable with name 'x' also defined in its parent class 'Base'.\n", errout.str());
checkDuplInheritedMembers("class Base0 {\n"
" int x;\n"
"};\n"
"class Base1 {\n"
" int x;\n"
"};\n"
"struct Derived : Base0, Base1 {\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout.str());
checkDuplInheritedMembers("class Base0 {\n"
" protected:\n"
" int x;\n"
"};\n"
"class Base1 {\n"
" int x;\n"
"};\n"
"struct Derived : Base0, Base1 {\n"
" int x;\n"
"};");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:9]: (warning) The struct 'Derived' defines member variable with name 'x' also defined in its parent class 'Base0'.\n", errout.str());
checkDuplInheritedMembers("class Base0 {\n"
" protected:\n"
" int x;\n"
"};\n"
"class Base1 {\n"
" public:\n"
" int x;\n"
"};\n"
"struct Derived : Base0, Base1 {\n"
" int x;\n"
"};");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:10]: (warning) The struct 'Derived' defines member variable with name 'x' also defined in its parent class 'Base0'.\n"
"[test.cpp:7] -> [test.cpp:10]: (warning) The struct 'Derived' defines member variable with name 'x' also defined in its parent class 'Base1'.\n", errout.str());
checkDuplInheritedMembers("class Base {\n"
" int x;\n"
"};\n"
"struct Derived : Base {\n"
" int y;\n"
"};");
ASSERT_EQUALS("", errout.str());
checkDuplInheritedMembers("class A {\n"
" int x;\n"
"};\n"
"struct B {\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout.str());
// Unknown 'Base' class
checkDuplInheritedMembers("class Derived : public UnknownBase {\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout.str());
checkDuplInheritedMembers("class Base {\n"
" int x;\n"
"};\n"
"class Derived : public Base {\n"
"};");
ASSERT_EQUALS("", errout.str());
// #6692
checkDuplInheritedMembers("namespace test1 {\n"
" struct SWibble{};\n"
" typedef SWibble wibble;\n"
"}\n"
"namespace test2 {\n"
" struct SWibble : public test1::wibble {\n"
" int Value;\n"
" };\n"
"}");
ASSERT_EQUALS("", errout.str());
// #9957
checkDuplInheritedMembers("class Base {\n"
" public:\n"
" int i;\n"
"};\n"
"class Derived1: public Base {\n"
" public:\n"
" int j;\n"
"};\n"
"class Derived2 : public Derived1 {\n"
" int i;\n"
"};");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:10]: (warning) The class 'Derived2' defines member variable with name 'i' also defined in its parent class 'Base'.\n", errout.str());
// don't crash on recursive template
checkDuplInheritedMembers("template<size_t N>\n"
"struct BitInt : public BitInt<N+1> { };");
ASSERT_EQUALS("", errout.str());
// don't crash on recursive template
checkDuplInheritedMembers("namespace _impl {\n"
" template <typename AlwaysVoid, typename>\n"
" struct fn_traits;\n"
"}\n"
"template <typename T>\n"
"struct function_traits\n"
" : public _impl::fn_traits<void, std::remove_reference_t<T>> {};\n"
"namespace _impl {\n"
" template <typename T>\n"
" struct fn_traits<decltype(void(&T::operator())), T>\n"
" : public fn_traits<void, decltype(&T::operator())> {};\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void checkCopyConstructor(const char code[]) {
// Clear the error log
errout.str("");
// Tokenize..
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Check..
CheckClass checkClass(&tokenizer, &settings0, this);
checkClass.copyconstructors();
}
void copyConstructor1() {
checkCopyConstructor("class F\n"
"{\n"
" public:\n"
" char *c,*p,*d;\n"
" F(const F &f) : p(f.p), c(f.c)\n"
" {\n"
" p=(char *)malloc(strlen(f.p)+1);\n"
" strcpy(p,f.p);\n"
" }\n"
" F(char *str)\n"
" {\n"
" p=(char *)malloc(strlen(str)+1);\n"
" strcpy(p,str);\n"
" }\n"
" F&operator=(const F&);\n"
" ~F();\n"
"};");
TODO_ASSERT_EQUALS("[test.cpp:5]: (warning) Value of pointer 'p', which points to allocated memory, is copied in copy constructor instead of allocating new memory.\n", "", errout.str());
checkCopyConstructor("class F {\n"
" char *p;\n"
" F(const F &f) {\n"
" p = f.p;\n"
" }\n"
" F(char *str) {\n"
" p = malloc(strlen(str)+1);\n"
" }\n"
" ~F();\n"
" F& operator=(const F&f);\n"
"};");
TODO_ASSERT_EQUALS("[test.cpp:4]: (warning) Value of pointer 'p', which points to allocated memory, is copied in copy constructor instead of allocating new memory.\n"
"[test.cpp:3] -> [test.cpp:7]: (warning) Copy constructor does not allocate memory for member 'p' although memory has been allocated in other constructors.\n",
"[test.cpp:4]: (warning) Value of pointer 'p', which points to allocated memory, is copied in copy constructor instead of allocating new memory.\n"
, errout.str());
checkCopyConstructor("class F\n"
"{\n"
" public:\n"
" char *c,*p,*d;\n"
" F(const F &f) :p(f.p)\n"
" {\n"
" }\n"
" F(char *str)\n"
" {\n"
" p=(char *)malloc(strlen(str)+1);\n"
" strcpy(p,str);\n"
" }\n"
" ~F();\n"
" F& operator=(const F&f);\n"
"};");
TODO_ASSERT_EQUALS("[test.cpp:5]: (warning) Value of pointer 'p', which points to allocated memory, is copied in copy constructor instead of allocating new memory.\n"
"[test.cpp:5] -> [test.cpp:10]: (warning) Copy constructor does not allocate memory for member 'p' although memory has been allocated in other constructors.\n",
""
, errout.str());
checkCopyConstructor("class kalci\n"
"{\n"
" public:\n"
" char *c,*p,*d;\n"
" kalci()\n"
" {\n"
" p=(char *)malloc(100);\n"
" strcpy(p,\"hello\");\n"
" c=(char *)malloc(100);\n"
" strcpy(p,\"hello\");\n"
" d=(char *)malloc(100);\n"
" strcpy(p,\"hello\");\n"
" }\n"
" kalci(const kalci &f)\n"
" {\n"
" p=(char *)malloc(strlen(str)+1);\n"
" strcpy(p,f.p);\n"
" c=(char *)malloc(strlen(str)+1);\n"
" strcpy(p,f.p);\n"
" d=(char *)malloc(strlen(str)+1);\n"
" strcpy(p,f.p);\n"
" }\n"
" ~kalci();\n"
" kalci& operator=(const kalci&kalci);\n"
"};");
ASSERT_EQUALS("", errout.str());
checkCopyConstructor("class F\n"
"{\n"
" public:\n"
" char *c,*p,*d;\n"
" F(char *str,char *st,char *string)\n"
" {\n"
" p=(char *)malloc(100);\n"
" strcpy(p,str);\n"
" c=(char *)malloc(100);\n"
" strcpy(p,st);\n"
" d=(char *)malloc(100);\n"
" strcpy(p,string);\n"
" }\n"
" F(const F &f)\n"
" {\n"
" p=(char *)malloc(strlen(str)+1);\n"
" strcpy(p,f.p);\n"
" c=(char *)malloc(strlen(str)+1);\n"
" strcpy(p,f.p);\n"
" }\n"
" ~F();\n"
" F& operator=(const F&f);\n"
"};");
TODO_ASSERT_EQUALS("[test.cpp:14] -> [test.cpp:11]: (warning) Copy constructor does not allocate memory for member 'd' although memory has been allocated in other constructors.\n", "", errout.str());
checkCopyConstructor("class F {\n"
" char *c;\n"
" F(char *str,char *st,char *string) {\n"
" p=(char *)malloc(100);\n"
" }\n"
" F(const F &f)\n"
" : p(malloc(size))\n"
" {\n"
" }\n"
" ~F();\n"
" F& operator=(const F&f);\n"
"};");
ASSERT_EQUALS("", errout.str());
checkCopyConstructor("class F {\n"
" char *c;\n"
" F(char *str,char *st,char *string)\n"
" : p(malloc(size))\n"
" {\n"
" }\n"
" F(const F &f)\n"
" {\n"
" }\n"
" ~F();\n"
" F& operator=(const F&f);\n"
"};");
TODO_ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:4]: (warning) Copy constructor does not allocate memory for member 'd' although memory has been allocated in other constructors.\n", "", errout.str());
checkCopyConstructor("class F\n"
"{\n"
" public:\n"
" char *c,*p,*d;\n"
" F()\n"
" {\n"
" p=(char *)malloc(100);\n"
" c=(char *)malloc(100);\n"
" d=(char*)malloc(100);\n"
" }\n"
" ~F();\n"
" F& operator=(const F&f);\n"
"};");
TODO_ASSERT_EQUALS("[test.cpp:8]: (warning) Class 'F' does not have a copy constructor which is recommended since it has dynamic memory/resource allocation(s).\n", "", errout.str());
checkCopyConstructor("class F\n"
"{\n"
" public:\n"
" char *c;\n"
" const char *p,*d;\n"
" F(char *str,char *st,char *string)\n"
" {\n"
" p=str;\n"
" d=st;\n"
" c=(char *)malloc(strlen(string)+1);\n"
" strcpy(d,string);\n"
" }\n"
" F(const F &f)\n"
" {\n"
" p=f.p;\n"
" d=f.d;\n"
" c=(char *)malloc(strlen(str)+1);\n"
" strcpy(d,f.p);\n"
" }\n"
" ~F();\n"
" F& operator=(const F&f);\n"
"};");
ASSERT_EQUALS("", errout.str());
checkCopyConstructor("class F : E\n"
"{\n"
" char *p;\n"
" F() {\n"
" p = malloc(100);\n"
" }\n"
" ~F();\n"
" F& operator=(const F&f);\n"
"};");
ASSERT_EQUALS("", errout.str());
checkCopyConstructor("class E { E(E&); };\n" // non-copyable
"class F : E\n"
"{\n"
" char *p;\n"
" F() {\n"
" p = malloc(100);\n"
" }\n"
" ~F();\n"
" F& operator=(const F&f);\n"
"};");
ASSERT_EQUALS("", errout.str());
checkCopyConstructor("class E {};\n"
"class F : E {\n"
" char *p;\n"
" F() {\n"
" p = malloc(100);\n"
" }\n"
" ~F();\n"
" F& operator=(const F&f);\n"
"};");
ASSERT_EQUALS("[test.cpp:5]: (warning) Class 'F' does not have a copy constructor which is recommended since it has dynamic memory/resource allocation(s).\n", errout.str());
checkCopyConstructor("class F {\n"
" char *p;\n"
" F() {\n"
" p = malloc(100);\n"
" }\n"
" F(F& f);\n"
" ~F();\n"
" F& operator=(const F&f);\n"
"};");
ASSERT_EQUALS("", errout.str());
checkCopyConstructor("class F {\n"
" char *p;\n"
" F() : p(malloc(100)) {}\n"
" ~F();\n"
" F& operator=(const F&f);\n"
"};");
ASSERT_EQUALS("[test.cpp:3]: (warning) Class 'F' does not have a copy constructor which is recommended since it has dynamic memory/resource allocation(s).\n", errout.str());
// #7198
checkCopyConstructor("struct F {\n"
" static char* c;\n"
" F() {\n"
" p = malloc(100);\n"
" }\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void copyConstructor2() { // ticket #4458
checkCopyConstructor("template <class _Tp>\n"
"class Vector\n"
"{\n"
"public:\n"
" Vector() {\n"
" _M_finish = new _Tp[ 42 ];\n"
" }\n"
" Vector( const Vector<_Tp>& v ) {\n"
" }\n"
" ~Vector();\n"
" Vector& operator=(const Vector&v);\n"
" _Tp* _M_finish;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void copyConstructor3() {
checkCopyConstructor("struct F {\n"
" char* c;\n"
" F() { c = malloc(100); }\n"
" F(const F &f) = delete;\n"
" F&operator=(const F &f);\n"
" ~F();\n"
"};");
ASSERT_EQUALS("", errout.str());
checkCopyConstructor("struct F {\n"
" char* c;\n"
" F() { c = malloc(100); }\n"
" F(const F &f) = default;\n"
" F&operator=(const F &f);\n"
" ~F();\n"
"};");
ASSERT_EQUALS("[test.cpp:3]: (warning) Struct 'F' has dynamic memory/resource allocation(s). The copy constructor is explicitly defaulted but the default copy constructor does not work well. It is recommended to define or delete the copy constructor.\n", errout.str());
}
void copyConstructor4() {
checkCopyConstructor("class noncopyable {\n"
"protected:\n"
" noncopyable() {}\n"
" ~noncopyable() {}\n"
"\n"
"private:\n"
" noncopyable( const noncopyable& );\n"
" const noncopyable& operator=( const noncopyable& );\n"
"};\n"
"\n"
"class Base : private noncopyable {};\n"
"\n"
"class Foo : public Base {\n"
"public:\n"
" Foo() : m_ptr(new int) {}\n"
" ~Foo() { delete m_ptr; }\n"
"private:\n"
" int* m_ptr;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void copyConstructor5() {
checkCopyConstructor("class Copyable {};\n"
"\n"
"class Foo : public Copyable, public UnknownType {\n"
"public:\n"
" Foo() : m_ptr(new int) {}\n"
" ~Foo() { delete m_ptr; }\n"
"private:\n"
" int* m_ptr;\n"
"};");
ASSERT_EQUALS("", errout.str());
checkCopyConstructor("class Copyable {};\n"
"\n"
"class Foo : public UnknownType, public Copyable {\n"
"public:\n"
" Foo() : m_ptr(new int) {}\n"
" ~Foo() { delete m_ptr; }\n"
"private:\n"
" int* m_ptr;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void noOperatorEq() {
checkCopyConstructor("struct F {\n"
" char* c;\n"
" F() { c = malloc(100); }\n"
" F(const F &f);\n"
" ~F();\n"
"};");
ASSERT_EQUALS("[test.cpp:3]: (warning) Struct 'F' does not have a operator= which is recommended since it has dynamic memory/resource allocation(s).\n", errout.str());
// defaulted operator=
checkCopyConstructor("struct F {\n"
" char* c;\n"
" F() { c = malloc(100); }\n"
" F(const F &f);\n"
" F &operator=(const F &f) = default;\n"
" ~F();\n"
"};");
ASSERT_EQUALS("[test.cpp:3]: (warning) Struct 'F' has dynamic memory/resource allocation(s). The operator= is explicitly defaulted but the default operator= does not work well. It is recommended to define or delete the operator=.\n", errout.str());
// deleted operator=
checkCopyConstructor("struct F {\n"
" char* c;\n"
" F() { c = malloc(100); }\n"
" F(const F &f);\n"
" F &operator=(const F &f) = delete;\n"
" ~F();\n"
"};");
ASSERT_EQUALS("", errout.str());
// base class deletes operator=
checkCopyConstructor("struct F : NonCopyable {\n"
" char* c;\n"
" F() { c = malloc(100); }\n"
" F(const F &f);\n"
" ~F();\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void noDestructor() {
checkCopyConstructor("struct F {\n"
" char* c;\n"
" F() { c = malloc(100); }\n"
" F(const F &f);\n"
" F&operator=(const F&);"
"};");
ASSERT_EQUALS("[test.cpp:3]: (warning) Struct 'F' does not have a destructor which is recommended since it has dynamic memory/resource allocation(s).\n", errout.str());
checkCopyConstructor("struct F {\n"
" C* c;\n"
" F() { c = new C; }\n"
" F(const F &f);\n"
" F&operator=(const F&);"
"};");
ASSERT_EQUALS("", errout.str());
checkCopyConstructor("struct F {\n"
" int* i;\n"
" F() { i = new int(); }\n"
" F(const F &f);\n"
" F& operator=(const F&);"
"};");
ASSERT_EQUALS("[test.cpp:3]: (warning) Struct 'F' does not have a destructor which is recommended since it has dynamic memory/resource allocation(s).\n", errout.str());
checkCopyConstructor("struct Data { int x; int y; };\n"
"struct F {\n"