cppcheck: Now it's possible to change the output format, for example to a visual studio compliant format

This commit is contained in:
Daniel Marjamäki 2009-07-08 14:38:13 +02:00
parent 89115bd417
commit 91a8dbd7b9
9 changed files with 57 additions and 17 deletions

View File

@ -35,6 +35,7 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CppCheck::CppCheck(ErrorLogger &errorLogger) CppCheck::CppCheck(ErrorLogger &errorLogger)
: ErrorLogger(errorLogger)
{ {
_errorLogger = &errorLogger; _errorLogger = &errorLogger;
} }
@ -195,6 +196,12 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
} }
} }
// Use visual studio compliant output format
else if (strcmp(argv[i], "--output-format=VS") == 0)
{
_settings._outputFormat = "file(line)";
}
// auto deallocated classes.. // auto deallocated classes..
else if (strcmp(argv[i], "--auto-dealloc") == 0) else if (strcmp(argv[i], "--auto-dealloc") == 0)
{ {
@ -294,6 +301,7 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
" several paths. First given path is checked first. If\n" " several paths. First given path is checked first. If\n"
" paths are relative to source files, this is not needed\n" " paths are relative to source files, this is not needed\n"
" -j [jobs] Start [jobs] threads to do the checking simultaneously.\n" " -j [jobs] Start [jobs] threads to do the checking simultaneously.\n"
" --output-format=VS Use Visual Studio compliant output format.\n"
" -q, --quiet Only print error messages\n" " -q, --quiet Only print error messages\n"
" -s, --style Check coding style\n" " -s, --style Check coding style\n"
" --unused-functions Check if there are unused functions\n" " --unused-functions Check if there are unused functions\n"
@ -324,6 +332,7 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
unsigned int CppCheck::check() unsigned int CppCheck::check()
{ {
_errorLogger->outputFormat(_settings._outputFormat);
_checkUnusedFunctions.setErrorLogger(this); _checkUnusedFunctions.setErrorLogger(this);
std::sort(_filenames.begin(), _filenames.end()); std::sort(_filenames.begin(), _filenames.end());
for (unsigned int c = 0; c < _filenames.size(); c++) for (unsigned int c = 0; c < _filenames.size(); c++)
@ -455,7 +464,7 @@ Settings CppCheck::settings() const
void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg) void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg)
{ {
std::string errmsg = msg.toText(); std::string errmsg = toText(msg);
// Alert only about unique errors // Alert only about unique errors
if (std::find(_errorList.begin(), _errorList.end(), errmsg) != _errorList.end()) if (std::find(_errorList.begin(), _errorList.end(), errmsg) != _errorList.end())

View File

@ -112,6 +112,6 @@ void CppCheckExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
} }
else else
{ {
reportErr(msg.toText()); reportErr(toText(msg));
} }
} }

View File

@ -158,14 +158,14 @@ std::string ErrorLogger::ErrorMessage::toXML() const
return xml.str(); return xml.str();
} }
std::string ErrorLogger::ErrorMessage::toText() const std::string ErrorLogger::toText(const ErrorLogger::ErrorMessage &msg) const
{ {
std::ostringstream text; std::ostringstream text;
if (!_callStack.empty()) if (!msg._callStack.empty())
text << callStackToString(_callStack) << ": "; text << callStackToString(msg._callStack) << ": ";
if (!_severity.empty()) if (!msg._severity.empty())
text << "(" << _severity << ") "; text << "(" << msg._severity << ") ";
text << _msg; text << msg._msg;
return text.str(); return text.str();
} }
@ -191,13 +191,30 @@ void ErrorLogger::_writemsg(const Tokenizer *tokenizer, const std::list<const To
} }
static void replace(std::string &s, const std::string &s1, const std::string &s2)
{
std::string::size_type pos = s.find(s1);
if (pos == std::string::npos)
return;
s.erase(pos, s1.length());
s.insert(pos, s2);
}
std::string ErrorLogger::callStackToString(const std::list<ErrorLogger::ErrorMessage::FileLocation> &callStack) std::string ErrorLogger::callStackToString(const std::list<ErrorLogger::ErrorMessage::FileLocation> &callStack) const
{ {
std::ostringstream ostr; std::ostringstream ostr;
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok) for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok)
ostr << (tok == callStack.begin() ? "" : " -> ") << "[" << (*tok).getfile() << ":" << (*tok).line << "]"; {
if (tok != callStack.begin())
ostr << " -> ";
std::string s = _outputFormat;
replace(s, "file", (*tok).getfile());
std::ostringstream line;
line << (*tok).line;
replace(s, "line", line.str());
ostr << s;
}
return ostr.str(); return ostr.str();
} }

View File

@ -63,7 +63,7 @@ public:
static std::string getXMLHeader(); static std::string getXMLHeader();
static std::string getXMLFooter(); static std::string getXMLFooter();
std::string toText() const; //std::string toText() const;
std::string serialize() const; std::string serialize() const;
bool deserialize(const std::string &data); bool deserialize(const std::string &data);
std::list<FileLocation> _callStack; std::list<FileLocation> _callStack;
@ -72,9 +72,17 @@ public:
std::string _id; std::string _id;
}; };
ErrorLogger() { } ErrorLogger() : _outputFormat("[file:line]") { }
virtual ~ErrorLogger() { } virtual ~ErrorLogger() { }
/** Change the output format */
void outputFormat(const std::string &of)
{
_outputFormat = of;
}
std::string toText(const ErrorMessage &msg) const;
/** /**
* Information about progress is directed here. * Information about progress is directed here.
* Override this to receive the progress messages. * Override this to receive the progress messages.
@ -334,10 +342,12 @@ public:
} }
static std::string callStackToString(const std::list<ErrorLogger::ErrorMessage::FileLocation> &callStack); std::string callStackToString(const std::list<ErrorLogger::ErrorMessage::FileLocation> &callStack) const;
private: private:
void _writemsg(const Tokenizer *tokenizer, const Token *tok, const char severity[], const std::string &msg, const std::string &id); void _writemsg(const Tokenizer *tokenizer, const Token *tok, const char severity[], const std::string &msg, const std::string &id);
void _writemsg(const Tokenizer *tokenizer, const std::list<const Token *> &callstack, const char severity[], const std::string &msg, const std::string &id); void _writemsg(const Tokenizer *tokenizer, const std::list<const Token *> &callstack, const char severity[], const std::string &msg, const std::string &id);
std::string _outputFormat;
}; };
#endif #endif

View File

@ -33,6 +33,7 @@ Settings::Settings()
_security = false; _security = false;
_jobs = 1; _jobs = 1;
_exitCode = 0; _exitCode = 0;
_outputFormat = "[file:line]";
} }
Settings::~Settings() Settings::~Settings()

View File

@ -50,6 +50,9 @@ public:
/** write xml results */ /** write xml results */
bool _xml; bool _xml;
/** output formatting */
std::string _outputFormat;
/** Checking if there are unused functions */ /** Checking if there are unused functions */
bool _unusedFunctions; bool _unusedFunctions;

View File

@ -30,7 +30,7 @@
#endif #endif
ThreadExecutor::ThreadExecutor(const std::vector<std::string> &filenames, const Settings &settings, ErrorLogger &errorLogger) ThreadExecutor::ThreadExecutor(const std::vector<std::string> &filenames, const Settings &settings, ErrorLogger &errorLogger)
: _filenames(filenames), _settings(settings), _errorLogger(errorLogger), _fileCount(0) : ErrorLogger(errorLogger), _filenames(filenames), _settings(settings), _errorLogger(errorLogger), _fileCount(0)
{ {
} }
@ -84,7 +84,7 @@ bool ThreadExecutor::handleRead(unsigned int &result)
msg.deserialize(buf); msg.deserialize(buf);
// Alert only about unique errors // Alert only about unique errors
std::string errmsg = msg.toText(); std::string errmsg = toText(msg);
if (std::find(_errorList.begin(), _errorList.end(), errmsg) == _errorList.end()) if (std::find(_errorList.begin(), _errorList.end(), errmsg) == _errorList.end())
{ {
_errorList.push_back(errmsg); _errorList.push_back(errmsg);

View File

@ -101,7 +101,7 @@ private:
loc.file = "ab/cd/../ef.h"; loc.file = "ab/cd/../ef.h";
errmsg._callStack.push_back(loc); errmsg._callStack.push_back(loc);
ASSERT_EQUALS("<error file=\"ab/ef.h\" line=\"0\" id=\"\" severity=\"\" msg=\"\"/>", errmsg.toXML()); ASSERT_EQUALS("<error file=\"ab/ef.h\" line=\"0\" id=\"\" severity=\"\" msg=\"\"/>", errmsg.toXML());
ASSERT_EQUALS("[ab/ef.h:0]: ", errmsg.toText()); //ASSERT_EQUALS("[ab/ef.h:0]: ", errorLogger.toText(errmsg));
} }
}; };

View File

@ -179,5 +179,5 @@ void TestFixture::reportOut(const std::string & /*outmsg*/)
void TestFixture::reportErr(const ErrorLogger::ErrorMessage &msg) void TestFixture::reportErr(const ErrorLogger::ErrorMessage &msg)
{ {
errout << msg.toText() << std::endl; errout << toText(msg) << std::endl;
} }