UninitVar: Better checking in whole program analysis

This commit is contained in:
Daniel Marjamäki 2018-01-25 15:56:46 +01:00
parent c04557eb73
commit b2bdc2687b
3 changed files with 8 additions and 10 deletions

View File

@ -937,10 +937,10 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al
} }
if (Token::Match(vartok->previous(), "++|--|%cop%")) { if (Token::Match(vartok->previous(), "++|--|%cop%")) {
if (_tokenizer->isCPP() && alloc == ARRAY && Token::Match(vartok->tokAt(-4), "& %var% =|( *")) if ((!_tokenizer || _tokenizer->isCPP()) && alloc == ARRAY && Token::Match(vartok->tokAt(-4), "& %var% =|( *"))
return false; return false;
if (_tokenizer->isCPP() && Token::Match(vartok->previous(), ">>|<<")) { if (_tokenizer && _tokenizer->isCPP() && Token::Match(vartok->previous(), ">>|<<")) {
const Token* tok2 = vartok->previous(); const Token* tok2 = vartok->previous();
if (Token::simpleMatch(tok2->astOperand1(), ">>")) if (Token::simpleMatch(tok2->astOperand1(), ">>"))
return false; // Looks like stream operator, initializes the variable return false; // Looks like stream operator, initializes the variable
@ -1012,7 +1012,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al
return true; return true;
} }
if (_tokenizer->isCPP() && Token::Match(vartok->next(), "<<|>>")) { if (_tokenizer && _tokenizer->isCPP() && Token::Match(vartok->next(), "<<|>>")) {
// Is this calculation done in rhs? // Is this calculation done in rhs?
const Token *tok = vartok; const Token *tok = vartok;
while (Token::Match(tok, "%name%|.|::")) while (Token::Match(tok, "%name%|.|::"))
@ -1320,7 +1320,7 @@ CheckUninitVar::MyFileInfo::FunctionArg::FunctionArg(const Tokenizer *tokenizer,
location.linenr = tok->linenr(); location.linenr = tok->linenr();
} }
static bool isUnsafeFunction(const Scope *scope, int argnr, const Token **tok) bool CheckUninitVar::isUnsafeFunction(const Scope *scope, int argnr, const Token **tok) const
{ {
const Variable * const argvar = scope->function->getArgumentVar(argnr); const Variable * const argvar = scope->function->getArgumentVar(argnr);
if (!argvar->isPointer()) if (!argvar->isPointer())
@ -1328,11 +1328,7 @@ static bool isUnsafeFunction(const Scope *scope, int argnr, const Token **tok)
for (const Token *tok2 = scope->classStart; tok2 != scope->classEnd; tok2 = tok2->next()) { for (const Token *tok2 = scope->classStart; tok2 != scope->classEnd; tok2 = tok2->next()) {
if (tok2->variable() != argvar) if (tok2->variable() != argvar)
continue; continue;
if (!Token::Match(tok2->astParent(), "*|[")) if (!isVariableUsage(tok2, true, Alloc::ARRAY))
return false;
while (Token::Match(tok2->astParent(), "*|["))
tok2 = tok2->astParent();
if (!Token::Match(tok2->astParent(),"%cop%"))
return false; return false;
*tok = tok2; *tok = tok2;
return true; return true;

View File

@ -155,6 +155,8 @@ public:
void uninitStructMemberError(const Token *tok, const std::string &membername); void uninitStructMemberError(const Token *tok, const std::string &membername);
private: private:
bool isUnsafeFunction(const Scope *scope, int argnr, const Token **tok) const;
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const { void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
CheckUninitVar c(nullptr, settings, errorLogger); CheckUninitVar c(nullptr, settings, errorLogger);

View File

@ -3996,7 +3996,7 @@ private:
void ctu() { void ctu() {
ctu("void f(int *p) {\n" ctu("void f(int *p) {\n"
" a = *p + 3;\n" " a = *p;\n"
"}\n" "}\n"
"int main() {\n" "int main() {\n"
" int x;\n" " int x;\n"