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(
TokenList& tokenlist,
ErrorLogger& errorlogger,
ErrorLogger* errorlogger,
const Settings *_settings,
const Token *tok,
std::list<Token *> &templateInstantiations,
@ -1154,9 +1154,9 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
int namepos = TemplateSimplifier::getTemplateNamePosition(tok);
if (namepos == -1) {
// debug message that we bail out..
if (_settings->debugwarnings) {
if (_settings->debugwarnings && errorlogger) {
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;
}
@ -1238,9 +1238,9 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
const std::string typeForNewName(typeForNewNameStr);
if (typeForNewName.empty() || typeParametersInDeclaration.size() != typesUsedInTemplateInstantiation.size()) {
if (_settings->debugwarnings) {
if (_settings->debugwarnings && errorlogger) {
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));
}
if (typeForNewName.empty())
@ -1317,7 +1317,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
void TemplateSimplifier::simplifyTemplates(
TokenList& tokenlist,
ErrorLogger& errorlogger,
ErrorLogger* errorlogger,
const Settings *_settings,
bool &_codeWithTemplates
)

View File

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

View File

@ -1285,3 +1285,12 @@ const ValueFlow::Value * Token::getValueGE(const MathLib::bigint val, const Sett
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 */
void assignProgressValues() {
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;
}
static void assignProgressValues(Token *tok);
/**
* @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()
{
if (!list.front())
return;
for (Token *tok = list.front()->tokAt(3); tok; tok = tok->next()) {
if (tok->isName()) {
//fix ticket #2784 - improved by ticket #3184
@ -2243,7 +2245,7 @@ void Tokenizer::simplifyTemplates()
TemplateSimplifier::simplifyTemplates(
list,
*_errorLogger,
_errorLogger,
_settings,
_codeWithTemplates);
}
@ -3474,7 +3476,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
simplifyArrayAccessSyntax();
list.front()->assignProgressValues();
Token::assignProgressValues(list.front());
removeRedundantSemicolons();
@ -3687,7 +3689,7 @@ bool Tokenizer::simplifyTokenList2()
validate();
list.front()->assignProgressValues();
Token::assignProgressValues(list.front());
// Create symbol database and then remove const keywords
createSymbolDatabase();

View File

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

View File

@ -360,7 +360,7 @@ bool TokenList::createTokens(std::istream &code, const std::string& file0)
addtoken(CurrentToken, lineno, FileIndex, true);
if (!CurrentToken.empty())
_back->isExpandedMacro(expandedMacro);
_front->assignProgressValues();
Token::assignProgressValues(_front);
for (unsigned int i = 1; i < _files.size(); i++)
_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 <cstring>
#include <stack>
#include <cassert>
extern std::ostringstream errout;
class TestTokenizer : public TestFixture {
@ -611,7 +612,10 @@ private:
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 "";
}