/test/testconstructors.cpp

https://github.com/joshbeck/cppcheck · C++ · 1048 lines · 916 code · 109 blank · 23 comment · 0 complexity · ab4d504a98e6cea6552e9564634d87d6 MD5 · raw file

  1. /*
  2. * Cppcheck - A tool for static C/C++ code analysis
  3. * Copyright (C) 2007-2011 Daniel Marjamäki and Cppcheck team.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "tokenize.h"
  19. #include "checkclass.h"
  20. #include "testsuite.h"
  21. #include <sstream>
  22. extern std::ostringstream errout;
  23. class TestConstructors : public TestFixture {
  24. public:
  25. TestConstructors() : TestFixture("TestConstructors")
  26. { }
  27. private:
  28. void check(const char code[], bool showAll = false) {
  29. // Clear the error buffer..
  30. errout.str("");
  31. Settings settings;
  32. settings.inconclusive = showAll;
  33. settings.addEnabled("style");
  34. // Tokenize..
  35. Tokenizer tokenizer(&settings, this);
  36. std::istringstream istr(code);
  37. tokenizer.tokenize(istr, "test.cpp");
  38. tokenizer.simplifyTokenList();
  39. // Check class constructors..
  40. CheckClass checkClass(&tokenizer, &settings, this);
  41. checkClass.constructors();
  42. }
  43. void run() {
  44. TEST_CASE(simple1);
  45. TEST_CASE(simple2);
  46. TEST_CASE(simple3);
  47. TEST_CASE(simple4);
  48. TEST_CASE(simple5); // ticket #2560
  49. TEST_CASE(initvar_with_this); // BUG 2190300
  50. TEST_CASE(initvar_if); // BUG 2190290
  51. TEST_CASE(initvar_operator_eq1); // BUG 2190376
  52. TEST_CASE(initvar_operator_eq2); // BUG 2190376
  53. TEST_CASE(initvar_operator_eq3);
  54. TEST_CASE(initvar_operator_eq4); // ticket #2204
  55. TEST_CASE(initvar_same_classname); // BUG 2208157
  56. TEST_CASE(initvar_chained_assign); // BUG 2270433
  57. TEST_CASE(initvar_2constructors); // BUG 2270353
  58. TEST_CASE(initvar_constvar);
  59. TEST_CASE(initvar_staticvar);
  60. TEST_CASE(initvar_union);
  61. TEST_CASE(initvar_private_constructor); // BUG 2354171 - private constructor
  62. TEST_CASE(initvar_copy_constructor); // ticket #1611
  63. TEST_CASE(initvar_nested_constructor); // ticket #1375
  64. TEST_CASE(initvar_nocopy1); // ticket #2474
  65. TEST_CASE(initvar_nocopy2); // ticket #2484
  66. TEST_CASE(initvar_destructor); // No variables need to be initialized in a destructor
  67. TEST_CASE(operatorEqSTL);
  68. }
  69. void simple1() {
  70. check("class Fred\n"
  71. "{\n"
  72. "public:\n"
  73. " int i;\n"
  74. "};\n");
  75. ASSERT_EQUALS("", errout.str());
  76. check("class Fred\n"
  77. "{\n"
  78. "private:\n"
  79. " int i;\n"
  80. "};\n");
  81. ASSERT_EQUALS("[test.cpp:1]: (style) The class 'Fred' does not have a constructor.\n", errout.str());
  82. check("struct Fred\n"
  83. "{\n"
  84. " int i;\n"
  85. "};\n");
  86. ASSERT_EQUALS("", errout.str());
  87. check("struct Fred\n"
  88. "{\n"
  89. "private:\n"
  90. " int i;\n"
  91. "};\n");
  92. ASSERT_EQUALS("[test.cpp:1]: (style) The struct 'Fred' does not have a constructor.\n", errout.str());
  93. }
  94. void simple2() {
  95. check("class Fred\n"
  96. "{\n"
  97. "public:\n"
  98. " Fred() : i(0) { }\n"
  99. " int i;\n"
  100. "};\n");
  101. ASSERT_EQUALS("", errout.str());
  102. check("class Fred\n"
  103. "{\n"
  104. "public:\n"
  105. " Fred() { i = 0; }\n"
  106. " int i;\n"
  107. "};\n");
  108. ASSERT_EQUALS("", errout.str());
  109. check("class Fred\n"
  110. "{\n"
  111. "public:\n"
  112. " Fred() { }\n"
  113. " int i;\n"
  114. "};\n");
  115. ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'Fred::i' is not initialized in the constructor.\n", errout.str());
  116. check("struct Fred\n"
  117. "{\n"
  118. " Fred() : i(0) { }\n"
  119. " int i;\n"
  120. "};\n");
  121. ASSERT_EQUALS("", errout.str());
  122. check("struct Fred\n"
  123. "{\n"
  124. " Fred() { i = 0; }\n"
  125. " int i;\n"
  126. "};\n");
  127. ASSERT_EQUALS("", errout.str());
  128. check("struct Fred\n"
  129. "{\n"
  130. " Fred() { }\n"
  131. " int i;\n"
  132. "};\n");
  133. ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Fred::i' is not initialized in the constructor.\n", errout.str());
  134. }
  135. void simple3() {
  136. check("class Fred\n"
  137. "{\n"
  138. "public:\n"
  139. " Fred();\n"
  140. " int i;\n"
  141. "};\n"
  142. "Fred::Fred() :i(0)\n"
  143. "{ }\n");
  144. ASSERT_EQUALS("", errout.str());
  145. check("class Fred\n"
  146. "{\n"
  147. "public:\n"
  148. " Fred();\n"
  149. " int i;\n"
  150. "};\n"
  151. "Fred::Fred()\n"
  152. "{ i = 0; }\n");
  153. ASSERT_EQUALS("", errout.str());
  154. check("class Fred\n"
  155. "{\n"
  156. "public:\n"
  157. " Fred();\n"
  158. " int i;\n"
  159. "};\n"
  160. "Fred::Fred()\n"
  161. "{ }\n");
  162. ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable 'Fred::i' is not initialized in the constructor.\n", errout.str());
  163. check("struct Fred\n"
  164. "{\n"
  165. " Fred();\n"
  166. " int i;\n"
  167. "};\n"
  168. "Fred::Fred() :i(0)\n"
  169. "{ }\n");
  170. ASSERT_EQUALS("", errout.str());
  171. check("struct Fred\n"
  172. "{\n"
  173. " Fred();\n"
  174. " int i;\n"
  175. "};\n"
  176. "Fred::Fred()\n"
  177. "{ i = 0; }\n");
  178. ASSERT_EQUALS("", errout.str());
  179. check("struct Fred\n"
  180. "{\n"
  181. " Fred();\n"
  182. " int i;\n"
  183. "};\n"
  184. "Fred::Fred()\n"
  185. "{ }\n");
  186. ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'Fred::i' is not initialized in the constructor.\n", errout.str());
  187. }
  188. void simple4() {
  189. check("class Fred\n"
  190. "{\n"
  191. "public:\n"
  192. " Fred();\n"
  193. " Fred(int _i);\n"
  194. " int i;\n"
  195. "};\n"
  196. "Fred::Fred()\n"
  197. "{ }\n"
  198. "Fred::Fred(int _i)\n"
  199. "{\n"
  200. " i = _i;\n"
  201. "}\n");
  202. ASSERT_EQUALS("[test.cpp:8]: (warning) Member variable 'Fred::i' is not initialized in the constructor.\n", errout.str());
  203. check("struct Fred\n"
  204. "{\n"
  205. " Fred();\n"
  206. " Fred(int _i);\n"
  207. " int i;\n"
  208. "};\n"
  209. "Fred::Fred()\n"
  210. "{ }\n"
  211. "Fred::Fred(int _i)\n"
  212. "{\n"
  213. " i = _i;\n"
  214. "}\n");
  215. ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable 'Fred::i' is not initialized in the constructor.\n", errout.str());
  216. }
  217. void simple5() { // ticket #2560
  218. check("namespace Nsp\n"
  219. "{\n"
  220. " class B { };\n"
  221. "}\n"
  222. "class Altren : public Nsp::B\n"
  223. "{\n"
  224. "public:\n"
  225. " Altren () : Nsp::B(), mValue(0)\n"
  226. " {\n"
  227. " }\n"
  228. "private:\n"
  229. " int mValue;\n"
  230. "};");
  231. ASSERT_EQUALS("", errout.str());
  232. }
  233. void initvar_with_this() {
  234. check("class Fred\n"
  235. "{\n"
  236. "public:\n"
  237. " Fred()\n"
  238. " { this->i = 0; }\n"
  239. " int i;\n"
  240. "};\n");
  241. ASSERT_EQUALS("", errout.str());
  242. check("struct Fred\n"
  243. "{\n"
  244. " Fred()\n"
  245. " { this->i = 0; }\n"
  246. " int i;\n"
  247. "};\n");
  248. ASSERT_EQUALS("", errout.str());
  249. }
  250. void initvar_if() {
  251. check("class Fred\n"
  252. "{\n"
  253. "public:\n"
  254. " Fred()\n"
  255. " {\n"
  256. " if (true)\n"
  257. " i = 0;\n"
  258. " else\n"
  259. " i = 1;\n"
  260. " }\n"
  261. " int i;\n"
  262. "};\n");
  263. ASSERT_EQUALS("", errout.str());
  264. check("struct Fred\n"
  265. "{\n"
  266. "public:\n"
  267. " Fred()\n"
  268. " {\n"
  269. " if (true)\n"
  270. " i = 0;\n"
  271. " else\n"
  272. " i = 1;\n"
  273. " }\n"
  274. " int i;\n"
  275. "};\n");
  276. ASSERT_EQUALS("", errout.str());
  277. }
  278. void initvar_operator_eq1() {
  279. // Bug 2190376 - False positive, Uninitialized member variable with operator=
  280. check("class Fred\n"
  281. "{\n"
  282. "private:\n"
  283. " int i;\n"
  284. "\n"
  285. "public:\n"
  286. " Fred()\n"
  287. " { i = 0; }\n"
  288. "\n"
  289. " Fred(const Fred &fred)\n"
  290. " { *this = fred; }\n"
  291. "\n"
  292. " const Fred & operator=(const Fred &fred)\n"
  293. " { i = fred.i; return *this; }\n"
  294. "};\n");
  295. ASSERT_EQUALS("", errout.str());
  296. check("class A\n"
  297. "{\n"
  298. " public:\n"
  299. " A() : i(0), j(0) {}\n"
  300. "\n"
  301. " A &operator=(const int &value)\n"
  302. " {\n"
  303. " i = value;\n"
  304. " return (*this);\n"
  305. " }\n"
  306. "\n"
  307. " int i;\n"
  308. " int j;\n"
  309. "};\n"
  310. "\n"
  311. "int main() {}\n");
  312. ASSERT_EQUALS("", errout.str());
  313. check("struct Fred\n"
  314. "{\n"
  315. " int i;\n"
  316. "\n"
  317. " Fred()\n"
  318. " { i = 0; }\n"
  319. "\n"
  320. " Fred(const Fred &fred)\n"
  321. " { *this = fred; }\n"
  322. "\n"
  323. " const Fred & operator=(const Fred &fred)\n"
  324. " { i = fred.i; return *this; }\n"
  325. "};\n");
  326. ASSERT_EQUALS("", errout.str());
  327. check("struct A\n"
  328. "{\n"
  329. " A() : i(0), j(0) {}\n"
  330. "\n"
  331. " A &operator=(const int &value)\n"
  332. " {\n"
  333. " i = value;\n"
  334. " return (*this);\n"
  335. " }\n"
  336. "\n"
  337. " int i;\n"
  338. " int j;\n"
  339. "};\n"
  340. "\n"
  341. "int main() {}\n");
  342. ASSERT_EQUALS("", errout.str());
  343. }
  344. void initvar_operator_eq2() {
  345. check("class Fred\n"
  346. "{\n"
  347. "public:\n"
  348. " Fred() { i = 0; }\n"
  349. " void operator=(const Fred &fred) { }\n"
  350. " int i;\n"
  351. "};\n");
  352. ASSERT_EQUALS("[test.cpp:5]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
  353. check("struct Fred\n"
  354. "{\n"
  355. " Fred() { i = 0; }\n"
  356. " void operator=(const Fred &fred) { }\n"
  357. " int i;\n"
  358. "};\n");
  359. ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
  360. }
  361. void initvar_operator_eq3() {
  362. check("class Fred\n"
  363. "{\n"
  364. "public:\n"
  365. " Fred() { Init(); }\n"
  366. " void operator=(const Fred &fred) { Init(); }\n"
  367. "private:\n"
  368. " void Init() { i = 0; }\n"
  369. " int i;\n"
  370. "};\n");
  371. ASSERT_EQUALS("", errout.str());
  372. check("struct Fred\n"
  373. "{\n"
  374. " Fred() { Init(); }\n"
  375. " void operator=(const Fred &fred) { Init(); }\n"
  376. "private:\n"
  377. " void Init() { i = 0; }\n"
  378. " int i;\n"
  379. "};\n");
  380. ASSERT_EQUALS("", errout.str());
  381. }
  382. void initvar_operator_eq4() {
  383. check("class Fred\n"
  384. "{\n"
  385. " int i;\n"
  386. "public:\n"
  387. " Fred() : i(5) { }\n"
  388. " Fred & operator=(const Fred &fred)\n"
  389. " {\n"
  390. " if (&fred != this)\n"
  391. " {\n"
  392. " }\n"
  393. " return *this\n"
  394. " }\n"
  395. "};\n");
  396. ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
  397. check("class Fred\n"
  398. "{\n"
  399. " int * i;\n"
  400. "public:\n"
  401. " Fred() : i(NULL) { }\n"
  402. " Fred & operator=(const Fred &fred)\n"
  403. " {\n"
  404. " if (&fred != this)\n"
  405. " {\n"
  406. " }\n"
  407. " return *this\n"
  408. " }\n"
  409. "};\n");
  410. ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
  411. check("class Fred\n"
  412. "{\n"
  413. " const int * i;\n"
  414. "public:\n"
  415. " Fred() : i(NULL) { }\n"
  416. " Fred & operator=(const Fred &fred)\n"
  417. " {\n"
  418. " if (&fred != this)\n"
  419. " {\n"
  420. " }\n"
  421. " return *this\n"
  422. " }\n"
  423. "};\n");
  424. ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
  425. check("class Fred\n"
  426. "{\n"
  427. " const int i;\n"
  428. "public:\n"
  429. " Fred() : i(5) { }\n"
  430. " Fred & operator=(const Fred &fred)\n"
  431. " {\n"
  432. " if (&fred != this)\n"
  433. " {\n"
  434. " }\n"
  435. " return *this\n"
  436. " }\n"
  437. "};\n");
  438. ASSERT_EQUALS("", errout.str());
  439. }
  440. void initvar_same_classname() {
  441. // Bug 2208157 - False positive: Uninitialized variable, same class name
  442. check("void func1()\n"
  443. "{\n"
  444. " class Fred\n"
  445. " {\n"
  446. " int a;\n"
  447. " Fred() { a = 0; }\n"
  448. " };\n"
  449. "}\n"
  450. "\n"
  451. "void func2()\n"
  452. "{\n"
  453. " class Fred\n"
  454. " {\n"
  455. " int b;\n"
  456. " Fred() { b = 0; }\n"
  457. " };\n"
  458. "}\n");
  459. ASSERT_EQUALS("", errout.str());
  460. check("void func1()\n"
  461. "{\n"
  462. " struct Fred\n"
  463. " {\n"
  464. " int a;\n"
  465. " Fred() { a = 0; }\n"
  466. " };\n"
  467. "}\n"
  468. "\n"
  469. "void func2()\n"
  470. "{\n"
  471. " class Fred\n"
  472. " {\n"
  473. " int b;\n"
  474. " Fred() { b = 0; }\n"
  475. " };\n"
  476. "}\n");
  477. ASSERT_EQUALS("", errout.str());
  478. check("void func1()\n"
  479. "{\n"
  480. " struct Fred\n"
  481. " {\n"
  482. " int a;\n"
  483. " Fred() { a = 0; }\n"
  484. " };\n"
  485. "}\n"
  486. "\n"
  487. "void func2()\n"
  488. "{\n"
  489. " struct Fred\n"
  490. " {\n"
  491. " int b;\n"
  492. " Fred() { b = 0; }\n"
  493. " };\n"
  494. "}\n");
  495. ASSERT_EQUALS("", errout.str());
  496. check("class Foo {\n"
  497. " void func1()\n"
  498. " {\n"
  499. " struct Fred\n"
  500. " {\n"
  501. " int a;\n"
  502. " Fred() { a = 0; }\n"
  503. " };\n"
  504. " }\n"
  505. "\n"
  506. " void func2()\n"
  507. " {\n"
  508. " struct Fred\n"
  509. " {\n"
  510. " int b;\n"
  511. " Fred() { b = 0; }\n"
  512. " };\n"
  513. " }\n"
  514. "};\n");
  515. ASSERT_EQUALS("", errout.str());
  516. check("class Foo {\n"
  517. " void func1()\n"
  518. " {\n"
  519. " struct Fred\n"
  520. " {\n"
  521. " int a;\n"
  522. " Fred() { }\n"
  523. " };\n"
  524. " }\n"
  525. "\n"
  526. " void func2()\n"
  527. " {\n"
  528. " struct Fred\n"
  529. " {\n"
  530. " int b;\n"
  531. " Fred() { }\n"
  532. " };\n"
  533. " }\n"
  534. "};\n");
  535. ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable 'Fred::a' is not initialized in the constructor.\n"
  536. "[test.cpp:16]: (warning) Member variable 'Fred::b' is not initialized in the constructor.\n", errout.str());
  537. }
  538. void initvar_chained_assign() {
  539. // Bug 2270433 - Uninitialized variable false positive on chained assigns
  540. check("class c\n"
  541. "{\n"
  542. " c();\n"
  543. "\n"
  544. " int m_iMyInt1;\n"
  545. " int m_iMyInt2;\n"
  546. "}\n"
  547. "\n"
  548. "c::c()\n"
  549. "{\n"
  550. " m_iMyInt1 = m_iMyInt2 = 0;\n"
  551. "}\n");
  552. ASSERT_EQUALS("", errout.str());
  553. check("struct c\n"
  554. "{\n"
  555. " c();\n"
  556. "\n"
  557. " int m_iMyInt1;\n"
  558. " int m_iMyInt2;\n"
  559. "}\n"
  560. "\n"
  561. "c::c()\n"
  562. "{\n"
  563. " m_iMyInt1 = m_iMyInt2 = 0;\n"
  564. "}\n");
  565. ASSERT_EQUALS("", errout.str());
  566. }
  567. void initvar_2constructors() {
  568. check("class c\n"
  569. "{\n"
  570. " c();\n"
  571. " c(bool b);"
  572. "\n"
  573. " void InitInt();\n"
  574. "\n"
  575. " int m_iMyInt;\n"
  576. " int m_bMyBool;\n"
  577. "}\n"
  578. "\n"
  579. "c::c()\n"
  580. "{\n"
  581. " m_bMyBool = false;\n"
  582. " InitInt();"
  583. "}\n"
  584. "\n"
  585. "c::c(bool b)\n"
  586. "{\n"
  587. " m_bMyBool = b;\n"
  588. " InitInt();\n"
  589. "}\n"
  590. "\n"
  591. "void c::InitInt()\n"
  592. "{\n"
  593. " m_iMyInt = 0;\n"
  594. "}\n");
  595. ASSERT_EQUALS("", errout.str());
  596. check("struct c\n"
  597. "{\n"
  598. " c();\n"
  599. " c(bool b);"
  600. "\n"
  601. " void InitInt();\n"
  602. "\n"
  603. " int m_iMyInt;\n"
  604. " int m_bMyBool;\n"
  605. "}\n"
  606. "\n"
  607. "c::c()\n"
  608. "{\n"
  609. " m_bMyBool = false;\n"
  610. " InitInt();"
  611. "}\n"
  612. "\n"
  613. "c::c(bool b)\n"
  614. "{\n"
  615. " m_bMyBool = b;\n"
  616. " InitInt();\n"
  617. "}\n"
  618. "\n"
  619. "void c::InitInt()\n"
  620. "{\n"
  621. " m_iMyInt = 0;\n"
  622. "}\n");
  623. ASSERT_EQUALS("", errout.str());
  624. }
  625. void initvar_constvar() {
  626. check("class Fred\n"
  627. "{\n"
  628. "public:\n"
  629. " const char *s;\n"
  630. " Fred();\n"
  631. "};\n"
  632. "Fred::Fred() : s(NULL)\n"
  633. "{ }");
  634. ASSERT_EQUALS("", errout.str());
  635. check("class Fred\n"
  636. "{\n"
  637. "public:\n"
  638. " const char *s;\n"
  639. " Fred();\n"
  640. "};\n"
  641. "Fred::Fred()\n"
  642. "{ s = NULL; }");
  643. ASSERT_EQUALS("", errout.str());
  644. check("class Fred\n"
  645. "{\n"
  646. "public:\n"
  647. " const char *s;\n"
  648. " Fred();\n"
  649. "};\n"
  650. "Fred::Fred()\n"
  651. "{ }");
  652. ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable 'Fred::s' is not initialized in the constructor.\n", errout.str());
  653. check("struct Fred\n"
  654. "{\n"
  655. " const char *s;\n"
  656. " Fred();\n"
  657. "};\n"
  658. "Fred::Fred() : s(NULL)\n"
  659. "{ }");
  660. ASSERT_EQUALS("", errout.str());
  661. check("struct Fred\n"
  662. "{\n"
  663. " const char *s;\n"
  664. " Fred();\n"
  665. "};\n"
  666. "Fred::Fred()\n"
  667. "{ s = NULL; }");
  668. ASSERT_EQUALS("", errout.str());
  669. check("struct Fred\n"
  670. "{\n"
  671. " const char *s;\n"
  672. " Fred();\n"
  673. "};\n"
  674. "Fred::Fred()\n"
  675. "{ }");
  676. ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'Fred::s' is not initialized in the constructor.\n", errout.str());
  677. }
  678. void initvar_staticvar() {
  679. check("class Fred\n"
  680. "{\n"
  681. "public:\n"
  682. " Fred() { }\n"
  683. " static void *p;\n"
  684. "};\n");
  685. ASSERT_EQUALS("", errout.str());
  686. }
  687. void initvar_union() {
  688. check("class Fred\n"
  689. "{\n"
  690. " union\n"
  691. " {\n"
  692. " int a;\n"
  693. " char b[4];\n"
  694. " } U;\n"
  695. "public:\n"
  696. " Fred()\n"
  697. " {\n"
  698. " U.a = 0;\n"
  699. " }\n"
  700. "};\n");
  701. ASSERT_EQUALS("", errout.str());
  702. check("class Fred\n"
  703. "{\n"
  704. " union\n"
  705. " {\n"
  706. " int a;\n"
  707. " char b[4];\n"
  708. " } U;\n"
  709. "public:\n"
  710. " Fred()\n"
  711. " {\n"
  712. " }\n"
  713. "};\n");
  714. ASSERT_EQUALS("[test.cpp:9]: (warning) Member variable 'Fred::U' is not initialized in the constructor.\n", errout.str());
  715. }
  716. void initvar_private_constructor() {
  717. check("class Fred\n"
  718. "{\n"
  719. "private:\n"
  720. " int var;\n"
  721. " Fred();\n"
  722. "};\n"
  723. "Fred::Fred()\n"
  724. "{ }");
  725. ASSERT_EQUALS("", errout.str());
  726. }
  727. void initvar_copy_constructor() { // ticket #1611
  728. check("class Fred\n"
  729. "{\n"
  730. "private:\n"
  731. " std::string var;\n"
  732. "public:\n"
  733. " Fred() { };\n"
  734. " Fred(const Fred &) { };\n"
  735. "};");
  736. ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable 'Fred::var' is not initialized in the constructor.\n", errout.str());
  737. check("class Fred\n"
  738. "{\n"
  739. "private:\n"
  740. " std::string var;\n"
  741. "public:\n"
  742. " Fred();\n"
  743. " Fred(const Fred &);\n"
  744. "};\n"
  745. "Fred::Fred() { };\n"
  746. "Fred::Fred(const Fred &) { };\n");
  747. ASSERT_EQUALS("[test.cpp:10]: (warning) Member variable 'Fred::var' is not initialized in the constructor.\n", errout.str());
  748. }
  749. void initvar_nested_constructor() { // ticket #1375
  750. check("class A {\n"
  751. "public:\n"
  752. " A();\n"
  753. " struct B {\n"
  754. " B(int x);\n"
  755. " struct C {\n"
  756. " C(int y);\n"
  757. " struct D {\n"
  758. " int d;\n"
  759. " D(int z);\n"
  760. " };\n"
  761. " int c;\n"
  762. " };\n"
  763. " int b;\n"
  764. " };\n"
  765. "private:\n"
  766. " int a;\n"
  767. " B b;\n"
  768. "};\n"
  769. "A::A(){}\n"
  770. "A::B::B(int x){}\n"
  771. "A::B::C::C(int y){}\n"
  772. "A::B::C::D::D(int z){}\n");
  773. ASSERT_EQUALS("[test.cpp:20]: (warning) Member variable 'A::a' is not initialized in the constructor.\n"
  774. "[test.cpp:20]: (warning) Member variable 'A::b' is not initialized in the constructor.\n"
  775. "[test.cpp:21]: (warning) Member variable 'B::b' is not initialized in the constructor.\n"
  776. "[test.cpp:22]: (warning) Member variable 'C::c' is not initialized in the constructor.\n"
  777. "[test.cpp:23]: (warning) Member variable 'D::d' is not initialized in the constructor.\n", errout.str());
  778. check("class A {\n"
  779. "public:\n"
  780. " A();\n"
  781. " struct B {\n"
  782. " B(int x);\n"
  783. " struct C {\n"
  784. " C(int y);\n"
  785. " struct D {\n"
  786. " D(const D &);\n"
  787. " int d;\n"
  788. " };\n"
  789. " int c;\n"
  790. " };\n"
  791. " int b;\n"
  792. " };\n"
  793. "private:\n"
  794. " int a;\n"
  795. " B b;\n"
  796. "};\n"
  797. "A::A(){}\n"
  798. "A::B::B(int x){}\n"
  799. "A::B::C::C(int y){}\n"
  800. "A::B::C::D::D(const A::B::C::D & d){}\n");
  801. ASSERT_EQUALS("[test.cpp:20]: (warning) Member variable 'A::a' is not initialized in the constructor.\n"
  802. "[test.cpp:20]: (warning) Member variable 'A::b' is not initialized in the constructor.\n"
  803. "[test.cpp:21]: (warning) Member variable 'B::b' is not initialized in the constructor.\n"
  804. "[test.cpp:22]: (warning) Member variable 'C::c' is not initialized in the constructor.\n"
  805. "[test.cpp:23]: (warning) Member variable 'D::d' is not initialized in the constructor.\n", errout.str());
  806. check("class A {\n"
  807. "public:\n"
  808. " A();\n"
  809. " struct B {\n"
  810. " B(int x);\n"
  811. " struct C {\n"
  812. " C(int y);\n"
  813. " struct D {\n"
  814. " struct E { int e; };\n"
  815. " struct E d;\n"
  816. " D(const E &);\n"
  817. " };\n"
  818. " int c;\n"
  819. " };\n"
  820. " int b;\n"
  821. " };\n"
  822. "private:\n"
  823. " int a;\n"
  824. " B b;\n"
  825. "};\n"
  826. "A::A(){}\n"
  827. "A::B::B(int x){}\n"
  828. "A::B::C::C(int y){}\n"
  829. "A::B::C::D::D(const A::B::C::D::E & e){}\n");
  830. ASSERT_EQUALS("[test.cpp:21]: (warning) Member variable 'A::a' is not initialized in the constructor.\n"
  831. "[test.cpp:21]: (warning) Member variable 'A::b' is not initialized in the constructor.\n"
  832. "[test.cpp:22]: (warning) Member variable 'B::b' is not initialized in the constructor.\n"
  833. "[test.cpp:23]: (warning) Member variable 'C::c' is not initialized in the constructor.\n"
  834. "[test.cpp:24]: (warning) Member variable 'D::d' is not initialized in the constructor.\n", errout.str());
  835. }
  836. void initvar_nocopy1() { // ticket #2474
  837. check("class B\n"
  838. "{\n"
  839. " B (const B & Var);\n"
  840. " B & operator= (const B & Var);\n"
  841. "};\n"
  842. "class A\n"
  843. "{\n"
  844. " B m_SemVar;\n"
  845. "public:\n"
  846. " A(){}\n"
  847. " A(const A&){}\n"
  848. " const A& operator=(const A&){return *this;}\n"
  849. "};\n");
  850. ASSERT_EQUALS("", errout.str());
  851. check("class B\n"
  852. "{\n"
  853. "public:\n"
  854. " B (const B & Var);\n"
  855. " B & operator= (const B & Var);\n"
  856. "};\n"
  857. "class A\n"
  858. "{\n"
  859. " B m_SemVar;\n"
  860. "public:\n"
  861. " A(){}\n"
  862. " A(const A&){}\n"
  863. " const A& operator=(const A&){return *this;}\n"
  864. "};\n");
  865. ASSERT_EQUALS("[test.cpp:12]: (warning) Member variable 'A::m_SemVar' is not initialized in the constructor.\n"
  866. "[test.cpp:13]: (warning) Member variable 'A::m_SemVar' is not assigned a value in 'A::operator='\n", errout.str());
  867. check("class A\n"
  868. "{\n"
  869. " B m_SemVar;\n"
  870. "public:\n"
  871. " A(){}\n"
  872. " A(const A&){}\n"
  873. " const A& operator=(const A&){return *this;}\n"
  874. "};\n");
  875. ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'A::m_SemVar' is not initialized in the constructor.\n"
  876. "[test.cpp:7]: (warning) Member variable 'A::m_SemVar' is not assigned a value in 'A::operator='\n", errout.str());
  877. }
  878. void initvar_nocopy2() { // ticket #2484
  879. check("class B\n"
  880. "{\n"
  881. " B (B & Var);\n"
  882. " B & operator= (const B & Var);\n"
  883. "};\n"
  884. "class A\n"
  885. "{\n"
  886. " B m_SemVar;\n"
  887. "public:\n"
  888. " A(){}\n"
  889. " A(const A&){}\n"
  890. " const A& operator=(const A&){return *this;}\n"
  891. "};\n");
  892. ASSERT_EQUALS("", errout.str());
  893. check("class B\n"
  894. "{\n"
  895. "public:\n"
  896. " B (B & Var);\n"
  897. " B & operator= (const B & Var);\n"
  898. "};\n"
  899. "class A\n"
  900. "{\n"
  901. " B m_SemVar;\n"
  902. "public:\n"
  903. " A(){}\n"
  904. " A(const A&){}\n"
  905. " const A& operator=(const A&){return *this;}\n"
  906. "};\n");
  907. ASSERT_EQUALS("[test.cpp:12]: (warning) Member variable 'A::m_SemVar' is not initialized in the constructor.\n"
  908. "[test.cpp:13]: (warning) Member variable 'A::m_SemVar' is not assigned a value in 'A::operator='\n", errout.str());
  909. }
  910. void initvar_destructor() {
  911. check("class Fred\n"
  912. "{\n"
  913. "private:\n"
  914. " int var;\n"
  915. "public:\n"
  916. " Fred() : var(0) {}\n"
  917. " ~Fred() {}\n"
  918. "};\n");
  919. ASSERT_EQUALS("", errout.str());
  920. }
  921. void operatorEqSTL() {
  922. check("class Fred\n"
  923. "{\n"
  924. "private:\n"
  925. " std::vector<int> ints;\n"
  926. "public:\n"
  927. " Fred();\n"
  928. " void operator=(const Fred &f);\n"
  929. "};\n"
  930. "\n"
  931. "Fred::Fred()\n"
  932. "{ }\n"
  933. "\n"
  934. "void Fred::operator=(const Fred &f)\n"
  935. "{ }", true);
  936. ASSERT_EQUALS("[test.cpp:13]: (warning) Member variable 'Fred::ints' is not assigned a value in 'Fred::operator='\n", errout.str());
  937. }
  938. };
  939. REGISTER_TEST(TestConstructors)