1919#include " color.h"
2020#include " cppcheck.h"
2121#include " errorlogger.h"
22+ #include " filesettings.h"
2223#include " fixture.h"
24+ #include " helpers.h"
2325
2426#include < algorithm>
2527#include < list>
@@ -34,30 +36,37 @@ class TestCppcheck : public TestFixture {
3436
3537 class ErrorLogger2 : public ErrorLogger {
3638 public:
37- std::list<std::string> id;
39+ std::list<std::string> ids;
40+ std::list<ErrorMessage> errmsgs;
3841
42+ private:
3943 void reportOut (const std::string & /* outmsg*/ , Color /* c*/ = Color::Reset) override {}
4044
4145 void reportErr (const ErrorMessage &msg) override {
42- id.push_back (msg.id );
46+ ids.push_back (msg.id );
47+ errmsgs.push_back (msg);
4348 }
4449 };
4550
4651 void run () override {
4752 TEST_CASE (getErrorMessages);
53+ TEST_CASE (checkWithFile);
54+ TEST_CASE (checkWithFS);
55+ TEST_CASE (suppress_error_library);
56+ TEST_CASE (unique_errors);
4857 }
4958
5059 void getErrorMessages () const {
5160 ErrorLogger2 errorLogger;
5261 CppCheck::getErrorMessages (errorLogger);
53- ASSERT (!errorLogger.id .empty ());
62+ ASSERT (!errorLogger.ids .empty ());
5463
5564 // Check if there are duplicate error ids in errorLogger.id
5665 std::string duplicate;
57- for (std::list<std::string>::const_iterator it = errorLogger.id .cbegin ();
58- it != errorLogger.id .cend ();
66+ for (std::list<std::string>::const_iterator it = errorLogger.ids .cbegin ();
67+ it != errorLogger.ids .cend ();
5968 ++it) {
60- if (std::find (errorLogger.id .cbegin (), it, *it) != it) {
69+ if (std::find (errorLogger.ids .cbegin (), it, *it) != it) {
6170 duplicate = " Duplicate ID: " + *it;
6271 break ;
6372 }
@@ -67,7 +76,7 @@ class TestCppcheck : public TestFixture {
6776 // Check for error ids from this class.
6877 bool foundPurgedConfiguration = false ;
6978 bool foundTooManyConfigs = false ;
70- for (const std::string & it : errorLogger.id ) {
79+ for (const std::string & it : errorLogger.ids ) {
7180 if (it == " purgedConfiguration" )
7281 foundPurgedConfiguration = true ;
7382 else if (it == " toomanyconfigs" )
@@ -76,6 +85,102 @@ class TestCppcheck : public TestFixture {
7685 ASSERT (foundPurgedConfiguration);
7786 ASSERT (foundTooManyConfigs);
7887 }
88+
89+ void checkWithFile () const
90+ {
91+ ScopedFile file (" test.cpp" ,
92+ " int main()\n "
93+ " {\n "
94+ " int i = *((int*)0);\n "
95+ " return 0;\n "
96+ " }" );
97+
98+ ErrorLogger2 errorLogger;
99+ CppCheck cppcheck (errorLogger, false , {});
100+ ASSERT_EQUALS (1 , cppcheck.check (file.path ()));
101+ // TODO: how to properly disable these warnings?
102+ errorLogger.ids .erase (std::remove_if (errorLogger.ids .begin (), errorLogger.ids .end (), [](const std::string& id) {
103+ return id == " logChecker" ;
104+ }), errorLogger.ids .end ());
105+ ASSERT_EQUALS (1 , errorLogger.ids .size ());
106+ ASSERT_EQUALS (" nullPointer" , *errorLogger.ids .cbegin ());
107+ }
108+
109+ void checkWithFS () const
110+ {
111+ ScopedFile file (" test.cpp" ,
112+ " int main()\n "
113+ " {\n "
114+ " int i = *((int*)0);\n "
115+ " return 0;\n "
116+ " }" );
117+
118+ ErrorLogger2 errorLogger;
119+ CppCheck cppcheck (errorLogger, false , {});
120+ FileSettings fs;
121+ fs.filename = file.path ();
122+ ASSERT_EQUALS (1 , cppcheck.check (fs));
123+ // TODO: how to properly disable these warnings?
124+ errorLogger.ids .erase (std::remove_if (errorLogger.ids .begin (), errorLogger.ids .end (), [](const std::string& id) {
125+ return id == " logChecker" ;
126+ }), errorLogger.ids .end ());
127+ ASSERT_EQUALS (1 , errorLogger.ids .size ());
128+ ASSERT_EQUALS (" nullPointer" , *errorLogger.ids .cbegin ());
129+ }
130+
131+ void suppress_error_library () const
132+ {
133+ ScopedFile file (" test.cpp" ,
134+ " int main()\n "
135+ " {\n "
136+ " int i = *((int*)0);\n "
137+ " return 0;\n "
138+ " }" );
139+
140+ ErrorLogger2 errorLogger;
141+ CppCheck cppcheck (errorLogger, false , {});
142+ const char xmldata[] = R"( <def format="2"><markup ext=".cpp" reporterrors="false"/></def>)" ;
143+ const Settings s = settingsBuilder ().libraryxml (xmldata, sizeof (xmldata)).build ();
144+ cppcheck.settings () = s;
145+ ASSERT_EQUALS (0 , cppcheck.check (file.path ()));
146+ // TODO: how to properly disable these warnings?
147+ errorLogger.ids .erase (std::remove_if (errorLogger.ids .begin (), errorLogger.ids .end (), [](const std::string& id) {
148+ return id == " logChecker" ;
149+ }), errorLogger.ids .end ());
150+ ASSERT_EQUALS (0 , errorLogger.ids .size ());
151+ }
152+
153+ // TODO: hwo to actually get duplicated findings
154+ void unique_errors () const
155+ {
156+ ScopedFile file (" inc.h" ,
157+ " inline void f()\n "
158+ " {\n "
159+ " (void)*((int*)0);\n "
160+ " }" );
161+ ScopedFile test_file_a (" a.cpp" ,
162+ " #include \" inc.h\" " );
163+ ScopedFile test_file_b (" b.cpp" ,
164+ " #include \" inc.h\" " );
165+
166+ ErrorLogger2 errorLogger;
167+ CppCheck cppcheck (errorLogger, false , {});
168+ ASSERT_EQUALS (1 , cppcheck.check (test_file_a.path ()));
169+ ASSERT_EQUALS (1 , cppcheck.check (test_file_b.path ()));
170+ // TODO: how to properly disable these warnings?
171+ errorLogger.errmsgs .erase (std::remove_if (errorLogger.errmsgs .begin (), errorLogger.errmsgs .end (), [](const ErrorMessage& errmsg) {
172+ return errmsg.id == " logChecker" ;
173+ }), errorLogger.errmsgs .end ());
174+ // the internal errorlist is cleared after each check() call
175+ ASSERT_EQUALS (2 , errorLogger.errmsgs .size ());
176+ auto it = errorLogger.errmsgs .cbegin ();
177+ ASSERT_EQUALS (" nullPointer" , it->id );
178+ ++it;
179+ ASSERT_EQUALS (" nullPointer" , it->id );
180+ }
181+
182+ // TODO: test suppressions
183+ // TODO: test all with FS
79184};
80185
81186REGISTER_TEST (TestCppcheck)
0 commit comments