diff --git a/src/cppcheck.cpp b/src/cppcheck.cpp index 7ab820ecd..9f767c759 100644 --- a/src/cppcheck.cpp +++ b/src/cppcheck.cpp @@ -35,6 +35,7 @@ //--------------------------------------------------------------------------- CppCheck::CppCheck(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.. 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" " paths are relative to source files, this is not needed\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" " -s, --style Check coding style\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() { + _errorLogger->outputFormat(_settings._outputFormat); _checkUnusedFunctions.setErrorLogger(this); std::sort(_filenames.begin(), _filenames.end()); for (unsigned int c = 0; c < _filenames.size(); c++) @@ -455,7 +464,7 @@ Settings CppCheck::settings() const void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg) { - std::string errmsg = msg.toText(); + std::string errmsg = toText(msg); // Alert only about unique errors if (std::find(_errorList.begin(), _errorList.end(), errmsg) != _errorList.end()) diff --git a/src/cppcheckexecutor.cpp b/src/cppcheckexecutor.cpp index 3707b807d..f436804a2 100644 --- a/src/cppcheckexecutor.cpp +++ b/src/cppcheckexecutor.cpp @@ -112,6 +112,6 @@ void CppCheckExecutor::reportErr(const ErrorLogger::ErrorMessage &msg) } else { - reportErr(msg.toText()); + reportErr(toText(msg)); } } diff --git a/src/errorlogger.cpp b/src/errorlogger.cpp index e5c316075..f56998a1e 100644 --- a/src/errorlogger.cpp +++ b/src/errorlogger.cpp @@ -158,14 +158,14 @@ std::string ErrorLogger::ErrorMessage::toXML() const return xml.str(); } -std::string ErrorLogger::ErrorMessage::toText() const +std::string ErrorLogger::toText(const ErrorLogger::ErrorMessage &msg) const { std::ostringstream text; - if (!_callStack.empty()) - text << callStackToString(_callStack) << ": "; - if (!_severity.empty()) - text << "(" << _severity << ") "; - text << _msg; + if (!msg._callStack.empty()) + text << callStackToString(msg._callStack) << ": "; + if (!msg._severity.empty()) + text << "(" << msg._severity << ") "; + text << msg._msg; return text.str(); } @@ -191,13 +191,30 @@ void ErrorLogger::_writemsg(const Tokenizer *tokenizer, const std::list &callStack) +std::string ErrorLogger::callStackToString(const std::list &callStack) const { std::ostringstream ostr; for (std::list::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(); } diff --git a/src/errorlogger.h b/src/errorlogger.h index 8232118ca..d6a81bbaa 100644 --- a/src/errorlogger.h +++ b/src/errorlogger.h @@ -63,7 +63,7 @@ public: static std::string getXMLHeader(); static std::string getXMLFooter(); - std::string toText() const; + //std::string toText() const; std::string serialize() const; bool deserialize(const std::string &data); std::list _callStack; @@ -72,9 +72,17 @@ public: std::string _id; }; - ErrorLogger() { } + ErrorLogger() : _outputFormat("[file:line]") { } 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. * Override this to receive the progress messages. @@ -334,10 +342,12 @@ public: } - static std::string callStackToString(const std::list &callStack); + std::string callStackToString(const std::list &callStack) const; 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 std::list &callstack, const char severity[], const std::string &msg, const std::string &id); + + std::string _outputFormat; }; #endif diff --git a/src/settings.cpp b/src/settings.cpp index 087019b83..e21c04290 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -33,6 +33,7 @@ Settings::Settings() _security = false; _jobs = 1; _exitCode = 0; + _outputFormat = "[file:line]"; } Settings::~Settings() diff --git a/src/settings.h b/src/settings.h index 2d5549c0a..2d569d667 100644 --- a/src/settings.h +++ b/src/settings.h @@ -50,6 +50,9 @@ public: /** write xml results */ bool _xml; + /** output formatting */ + std::string _outputFormat; + /** Checking if there are unused functions */ bool _unusedFunctions; diff --git a/src/threadexecutor.cpp b/src/threadexecutor.cpp index 3a4c83493..dbbfba4d4 100644 --- a/src/threadexecutor.cpp +++ b/src/threadexecutor.cpp @@ -30,7 +30,7 @@ #endif ThreadExecutor::ThreadExecutor(const std::vector &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); // 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()) { _errorList.push_back(errmsg); diff --git a/test/testcppcheck.cpp b/test/testcppcheck.cpp index c6edb17bc..643f3df95 100644 --- a/test/testcppcheck.cpp +++ b/test/testcppcheck.cpp @@ -101,7 +101,7 @@ private: loc.file = "ab/cd/../ef.h"; errmsg._callStack.push_back(loc); ASSERT_EQUALS("", errmsg.toXML()); - ASSERT_EQUALS("[ab/ef.h:0]: ", errmsg.toText()); + //ASSERT_EQUALS("[ab/ef.h:0]: ", errorLogger.toText(errmsg)); } }; diff --git a/test/testsuite.cpp b/test/testsuite.cpp index 04c5d01ea..8144013d5 100644 --- a/test/testsuite.cpp +++ b/test/testsuite.cpp @@ -179,5 +179,5 @@ void TestFixture::reportOut(const std::string & /*outmsg*/) void TestFixture::reportErr(const ErrorLogger::ErrorMessage &msg) { - errout << msg.toText() << std::endl; + errout << toText(msg) << std::endl; }