diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index c70930fc8..8feebe7f5 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -250,6 +250,19 @@ unsigned int CppCheck::processFile() // Exception was thrown when checking this file.. const std::string fixedpath = Path::toNativeSeparators(_filename); _errorLogger.reportOut("Bailing out from checking " + fixedpath + ": " + e.what()); + } catch (ErrorLogger::ErrorMessage &err) { + // Catch exception from Token class + const std::string fixedpath = Path::toNativeSeparators(_filename); + if( err._callStack.empty() ){ + ErrorLogger::ErrorMessage::FileLocation loc; + loc.setfile(fixedpath); + err._callStack.push_back(loc); + } + else{ + err._callStack.begin()->setfile(fixedpath); + } + + _errorLogger.reportErr(err ); } if (!_settings._errorsOnly) diff --git a/lib/token.cpp b/lib/token.cpp index cda6b018f..7f660f42b 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -542,7 +542,7 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid) "Internal error. Token::Match called with varid 0.", "cppcheckError", false); - Check::reportError(errmsg); + throw errmsg; } if (tok->varId() != varid) diff --git a/test/testtoken.cpp b/test/testtoken.cpp index d7b3a1b5e..cbfe840fe 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -277,7 +277,9 @@ private: void matchVarid() { givenACodeSampleToTokenize var("int a ; int b ;"); - ASSERT_EQUALS(false, Token::Match(var.tokens(), "%type% %varid% ; %type% %var%", 0)); + + // Varid == 0 should throw exception + ASSERT_THROW(Token::Match(var.tokens(), "%type% %varid% ; %type% %var%", 0),ErrorLogger::ErrorMessage ); ASSERT_EQUALS(true, Token::Match(var.tokens(), "%type% %varid% ; %type% %var%", 1)); ASSERT_EQUALS(true, Token::Match(var.tokens(), "%type% %var% ; %type% %varid%", 2));