2009-02-05 21:06:39 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2009-05-30 07:48:12 +02:00
|
|
|
* Copyright (C) 2007-2009 Daniel Marjamäki and Cppcheck team.
|
2009-02-05 21:06:39 +01:00
|
|
|
*
|
|
|
|
* 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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-02-05 21:06:39 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-07-05 22:16:43 +02:00
|
|
|
// The preprocessor that Cppcheck uses is a bit special. Instead of generating
|
2009-02-05 21:06:39 +01:00
|
|
|
// the code for a known configuration, it generates the code for each configuration.
|
|
|
|
|
|
|
|
|
2009-10-25 12:49:06 +01:00
|
|
|
#include "cppcheck.h"
|
2009-02-05 21:06:39 +01:00
|
|
|
#include "testsuite.h"
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
extern std::ostringstream errout;
|
|
|
|
|
|
|
|
class TestCppcheck : public TestFixture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestCppcheck() : TestFixture("TestCppcheck")
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void check(const std::string &data)
|
|
|
|
{
|
|
|
|
errout.str("");
|
|
|
|
CppCheck cppCheck(*this);
|
|
|
|
cppCheck.addFile("file.cpp", data);
|
|
|
|
cppCheck.check();
|
|
|
|
}
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
2009-02-05 21:29:55 +01:00
|
|
|
TEST_CASE(linenumbers);
|
|
|
|
// TEST_CASE(linenumbers2);
|
2009-04-23 21:59:26 +02:00
|
|
|
|
|
|
|
TEST_CASE(xml);
|
2009-05-31 10:12:19 +02:00
|
|
|
|
|
|
|
TEST_CASE(include);
|
2009-09-05 21:01:49 +02:00
|
|
|
TEST_CASE(templateFormat);
|
2010-04-03 21:53:06 +02:00
|
|
|
TEST_CASE(getErrorMessages);
|
2009-02-05 21:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void linenumbers()
|
|
|
|
{
|
|
|
|
const char filedata[] = "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *foo = new char[10];\n"
|
|
|
|
" delete [] foo;\n"
|
|
|
|
" foo[3] = 0;\n"
|
|
|
|
"}\n";
|
|
|
|
check(filedata);
|
|
|
|
|
|
|
|
// Compare results..
|
2009-12-13 17:18:27 +01:00
|
|
|
ASSERT_EQUALS("Checking file.cpp...\n"
|
|
|
|
"[file.cpp:5]: (error) Dereferencing 'foo' after it is deallocated / released\n", errout.str());
|
2009-02-05 21:06:39 +01:00
|
|
|
}
|
2009-02-05 21:29:55 +01:00
|
|
|
|
|
|
|
void linenumbers2()
|
|
|
|
{
|
|
|
|
const char filedata[] = "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *string;\n"
|
|
|
|
" string = new char[20];\n"
|
|
|
|
" string = new char[30];\n"
|
|
|
|
" delete [] string;\n"
|
|
|
|
"}\n";
|
|
|
|
check(filedata);
|
|
|
|
|
|
|
|
// Compare results..
|
2009-02-05 22:06:32 +01:00
|
|
|
ASSERT_EQUALS("[file.cpp:5]: (error) Memory leak: string\n", errout.str());
|
2009-02-05 21:29:55 +01:00
|
|
|
}
|
2009-04-23 21:59:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
void xml()
|
|
|
|
{
|
|
|
|
// Test the errorlogger..
|
|
|
|
ErrorLogger::ErrorMessage errmsg;
|
|
|
|
errmsg._msg = "ab<cd>ef";
|
|
|
|
ASSERT_EQUALS("<error id=\"\" severity=\"\" msg=\"ab<cd>ef\"/>", errmsg.toXML());
|
|
|
|
}
|
2009-05-31 10:12:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
void include()
|
|
|
|
{
|
|
|
|
ErrorLogger::ErrorMessage errmsg;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.file = "ab/cd/../ef.h";
|
|
|
|
errmsg._callStack.push_back(loc);
|
|
|
|
ASSERT_EQUALS("<error file=\"ab/ef.h\" line=\"0\" id=\"\" severity=\"\" msg=\"\"/>", errmsg.toXML());
|
2009-07-10 22:38:26 +02:00
|
|
|
ASSERT_EQUALS("[ab/ef.h:0]: ", errmsg.toText());
|
2009-05-31 10:12:19 +02:00
|
|
|
}
|
|
|
|
|
2009-09-05 21:01:49 +02:00
|
|
|
void templateFormat()
|
|
|
|
{
|
|
|
|
ErrorLogger::ErrorMessage errmsg;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.file = "some/{file}file.cpp";
|
|
|
|
loc.line = 10;
|
|
|
|
errmsg._callStack.push_back(loc);
|
|
|
|
errmsg._id = "testId";
|
|
|
|
errmsg._severity = "testSeverity";
|
|
|
|
errmsg._msg = "long testMessage";
|
|
|
|
ASSERT_EQUALS("<error file=\"some/{file}file.cpp\" line=\"10\" id=\"testId\" severity=\"testSeverity\" msg=\"long testMessage\"/>", errmsg.toXML());
|
|
|
|
ASSERT_EQUALS("[some/{file}file.cpp:10]: (testSeverity) long testMessage", errmsg.toText());
|
|
|
|
ASSERT_EQUALS("testId-some/{file}file.cpp,testSeverity.10?{long testMessage}", errmsg.toText("{id}-{file},{severity}.{line}?{{message}}"));
|
|
|
|
}
|
2010-04-03 21:53:06 +02:00
|
|
|
|
|
|
|
void getErrorMessages()
|
|
|
|
{
|
|
|
|
errout.str("");
|
|
|
|
CppCheck cppCheck(*this);
|
|
|
|
cppCheck.getErrorMessages();
|
|
|
|
}
|
2009-02-05 21:06:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(TestCppcheck)
|