From 69109784e858dc941388b553afc0086dc61a9d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 20 Jan 2014 06:49:45 +0100 Subject: [PATCH] value flow: cleanup usage of valueflow. utility function Token::getValue was added. --- lib/checknullpointer.cpp | 11 +---------- lib/checkother.cpp | 15 ++++++--------- lib/token.h | 10 ++++++++++ 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index df3402a1a..be5aa1d10 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -752,21 +752,12 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec() void CheckNullPointer::nullPointerByDeRefAndChec() { for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if (!tok->isName() || tok->values.empty()) - continue; - const Variable *var = tok->variable(); if (!var || !var->isPointer() || tok == var->nameToken()) continue; // Can pointer be NULL? - const ValueFlow::Value *value = 0; - for (std::list::const_iterator it = tok->values.begin(); it != tok->values.end(); ++it) { - if (it->intvalue == 0) { - value = &(*it); - break; - } - } + const ValueFlow::Value *value = tok->getValue(0); if (!value) continue; diff --git a/lib/checkother.cpp b/lib/checkother.cpp index fe4273eb0..547497e27 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2185,15 +2185,12 @@ void CheckOther::checkZeroDivision() zerodivError(tok); } else if (Token::Match(tok, "[/%]") && tok->astOperand2() && !tok->astOperand2()->values.empty()) { // Value flow.. - const std::list &values = tok->astOperand2()->values; - std::list::const_iterator it; - for (it = values.begin(); it != values.end(); ++it) { - if (it->intvalue == 0) { - if (it->condition == NULL) - zerodivError(tok); - else if (_settings->isEnabled("warning")) - zerodivcondError(it->condition,tok); - } + const ValueFlow::Value *value = tok->astOperand2()->getValue(0LL); + if (value) { + if (value->condition == NULL) + zerodivError(tok); + else if (_settings->isEnabled("warning")) + zerodivcondError(value->condition,tok); } } } diff --git a/lib/token.h b/lib/token.h index 766844069..bb0aa3c56 100644 --- a/lib/token.h +++ b/lib/token.h @@ -27,6 +27,7 @@ #include #include "config.h" #include "valueflow.h" +#include "mathlib.h" class Scope; class Function; @@ -580,6 +581,15 @@ public: /** Values of token */ std::list values; + const ValueFlow::Value * getValue(const MathLib::bigint val) const { + std::list::const_iterator it; + for (it = values.begin(); it != values.end(); ++it) { + if (it->intvalue == val) + return &(*it); + } + return NULL; + } + private: void next(Token *nextToken) { _next = nextToken;