Refactoring/small corrections to fix warnings from clang -fsanitize=undefined 'member call on null pointer'

This commit is contained in:
Alexander Mai 2014-05-20 21:55:08 +02:00
parent ac8d283afb
commit be9a566d48
8 changed files with 30 additions and 22 deletions

View File

@ -1129,7 +1129,7 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens)
bool TemplateSimplifier::simplifyTemplateInstantiations( bool TemplateSimplifier::simplifyTemplateInstantiations(
TokenList& tokenlist, TokenList& tokenlist,
ErrorLogger& errorlogger, ErrorLogger* errorlogger,
const Settings *_settings, const Settings *_settings,
const Token *tok, const Token *tok,
std::list<Token *> &templateInstantiations, std::list<Token *> &templateInstantiations,
@ -1154,9 +1154,9 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
int namepos = TemplateSimplifier::getTemplateNamePosition(tok); int namepos = TemplateSimplifier::getTemplateNamePosition(tok);
if (namepos == -1) { if (namepos == -1) {
// debug message that we bail out.. // debug message that we bail out..
if (_settings->debugwarnings) { if (_settings->debugwarnings && errorlogger) {
std::list<const Token *> callstack(1, tok); std::list<const Token *> callstack(1, tok);
errorlogger.reportErr(ErrorLogger::ErrorMessage(callstack, &tokenlist, Severity::debug, "debug", "simplifyTemplates: bailing out", false)); errorlogger->reportErr(ErrorLogger::ErrorMessage(callstack, &tokenlist, Severity::debug, "debug", "simplifyTemplates: bailing out", false));
} }
return false; return false;
} }
@ -1238,9 +1238,9 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
const std::string typeForNewName(typeForNewNameStr); const std::string typeForNewName(typeForNewNameStr);
if (typeForNewName.empty() || typeParametersInDeclaration.size() != typesUsedInTemplateInstantiation.size()) { if (typeForNewName.empty() || typeParametersInDeclaration.size() != typesUsedInTemplateInstantiation.size()) {
if (_settings->debugwarnings) { if (_settings->debugwarnings && errorlogger) {
std::list<const Token *> callstack(1, tok); std::list<const Token *> callstack(1, tok);
errorlogger.reportErr(ErrorLogger::ErrorMessage(callstack, &tokenlist, Severity::debug, "debug", errorlogger->reportErr(ErrorLogger::ErrorMessage(callstack, &tokenlist, Severity::debug, "debug",
"Failed to instantiate template. The checking continues anyway.", false)); "Failed to instantiate template. The checking continues anyway.", false));
} }
if (typeForNewName.empty()) if (typeForNewName.empty())
@ -1317,7 +1317,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
void TemplateSimplifier::simplifyTemplates( void TemplateSimplifier::simplifyTemplates(
TokenList& tokenlist, TokenList& tokenlist,
ErrorLogger& errorlogger, ErrorLogger* errorlogger,
const Settings *_settings, const Settings *_settings,
bool &_codeWithTemplates bool &_codeWithTemplates
) )

View File

@ -130,7 +130,7 @@ public:
*/ */
static bool simplifyTemplateInstantiations( static bool simplifyTemplateInstantiations(
TokenList& tokenlist, TokenList& tokenlist,
ErrorLogger& errorlogger, ErrorLogger* errorlogger,
const Settings *_settings, const Settings *_settings,
const Token *tok, const Token *tok,
std::list<Token *> &templateInstantiations, std::list<Token *> &templateInstantiations,
@ -145,7 +145,7 @@ public:
*/ */
static void simplifyTemplates( static void simplifyTemplates(
TokenList& tokenlist, TokenList& tokenlist,
ErrorLogger& errorlogger, ErrorLogger* errorlogger,
const Settings *_settings, const Settings *_settings,
bool &_codeWithTemplates); bool &_codeWithTemplates);

View File

@ -1285,3 +1285,12 @@ const ValueFlow::Value * Token::getValueGE(const MathLib::bigint val, const Sett
return ret; return ret;
} }
void Token::assignProgressValues(Token *tok)
{
unsigned int total_count = 0;
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
++total_count;
unsigned int count = 0;
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
tok2->_progressValue = count++ * 100 / total_count;
}

View File

@ -591,14 +591,7 @@ public:
} }
/** Calculate progress values for all tokens */ /** Calculate progress values for all tokens */
void assignProgressValues() { static void assignProgressValues(Token *tok);
unsigned int total_count = 0;
for (Token *tok = this; tok; tok = tok->next())
++total_count;
unsigned int count = 0;
for (Token *tok = this; tok; tok = tok->next())
tok->_progressValue = count++ * 100 / total_count;
}
/** /**
* @return the first token of the next argument. Does only work on argument * @return the first token of the next argument. Does only work on argument

View File

@ -1518,6 +1518,8 @@ void Tokenizer::simplifyTypedef()
void Tokenizer::simplifyMulAndParens() void Tokenizer::simplifyMulAndParens()
{ {
if (!list.front())
return;
for (Token *tok = list.front()->tokAt(3); tok; tok = tok->next()) { for (Token *tok = list.front()->tokAt(3); tok; tok = tok->next()) {
if (tok->isName()) { if (tok->isName()) {
//fix ticket #2784 - improved by ticket #3184 //fix ticket #2784 - improved by ticket #3184
@ -2243,7 +2245,7 @@ void Tokenizer::simplifyTemplates()
TemplateSimplifier::simplifyTemplates( TemplateSimplifier::simplifyTemplates(
list, list,
*_errorLogger, _errorLogger,
_settings, _settings,
_codeWithTemplates); _codeWithTemplates);
} }
@ -3474,7 +3476,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
simplifyArrayAccessSyntax(); simplifyArrayAccessSyntax();
list.front()->assignProgressValues(); Token::assignProgressValues(list.front());
removeRedundantSemicolons(); removeRedundantSemicolons();
@ -3687,7 +3689,7 @@ bool Tokenizer::simplifyTokenList2()
validate(); validate();
list.front()->assignProgressValues(); Token::assignProgressValues(list.front());
// Create symbol database and then remove const keywords // Create symbol database and then remove const keywords
createSymbolDatabase(); createSymbolDatabase();

View File

@ -800,7 +800,7 @@ private:
const Settings * _settings; const Settings * _settings;
/** errorlogger */ /** errorlogger */
ErrorLogger * const _errorLogger; ErrorLogger* const _errorLogger;
/** Symbol database that all checks etc can use */ /** Symbol database that all checks etc can use */
SymbolDatabase *_symbolDatabase; SymbolDatabase *_symbolDatabase;

View File

@ -360,7 +360,7 @@ bool TokenList::createTokens(std::istream &code, const std::string& file0)
addtoken(CurrentToken, lineno, FileIndex, true); addtoken(CurrentToken, lineno, FileIndex, true);
if (!CurrentToken.empty()) if (!CurrentToken.empty())
_back->isExpandedMacro(expandedMacro); _back->isExpandedMacro(expandedMacro);
_front->assignProgressValues(); Token::assignProgressValues(_front);
for (unsigned int i = 1; i < _files.size(); i++) for (unsigned int i = 1; i < _files.size(); i++)
_files[i] = Path::getRelativePath(_files[i], _settings->_basePaths); _files[i] = Path::getRelativePath(_files[i], _settings->_basePaths);

View File

@ -25,6 +25,7 @@
#include "preprocessor.h" // usually tests here should not use preprocessor... #include "preprocessor.h" // usually tests here should not use preprocessor...
#include <cstring> #include <cstring>
#include <stack> #include <stack>
#include <cassert>
extern std::ostringstream errout; extern std::ostringstream errout;
class TestTokenizer : public TestFixture { class TestTokenizer : public TestFixture {
@ -611,7 +612,10 @@ private:
errout << line << "\n"; errout << line << "\n";
} }
return tokenizer.tokens()->stringifyList(false, expand, false, true, false, 0, 0); if (tokenizer.tokens())
return tokenizer.tokens()->stringifyList(false, expand, false, true, false, 0, 0);
else
return "";
} }