From 28ad69e4b3d6ea8c3f979b99f305f22d43784082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 2 Jun 2010 17:53:45 +0200 Subject: [PATCH] Refactoring of CheckOther::nullConstantDereference --- lib/checkother.cpp | 48 +++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index f6b25f855..711409960 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2201,27 +2201,49 @@ void CheckOther::nullPointer() /** Derefencing null constant (simplified token list) */ void CheckOther::nullConstantDereference() { + // this is kept at 0 for all scopes that are not executing + unsigned int indentlevel = 0; + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if (tok->str() == "(" && Token::simpleMatch(tok->previous(), "sizeof")) - tok = tok->link(); + // start of executable scope.. + if (indentlevel == 0 && Token::Match(tok, ") const| {")) + indentlevel = 1; - else if (Token::simpleMatch(tok, "exit ( )")) + else if (indentlevel >= 1) { - while (tok && tok->str() != "}") + if (tok->str() == "{") + ++indentlevel; + + else if (tok->str() == "}") { - if (tok->str() == "{") - tok = tok->link(); - tok = tok->next(); + if (indentlevel <= 2) + indentlevel = 0; + else + --indentlevel; } - } - else if (Token::simpleMatch(tok, "* 0")) - { - if (Token::Match(tok->previous(), "[;{}=+-/(,]") || - Token::Match(tok->previous(), "return|<<")) + if (tok->str() == "(" && Token::simpleMatch(tok->previous(), "sizeof")) + tok = tok->link(); + + else if (Token::simpleMatch(tok, "exit ( )")) { - nullPointerError(tok); + // Goto end of scope + while (tok && tok->str() != "}") + { + if (tok->str() == "{") + tok = tok->link(); + tok = tok->next(); + } + } + + else if (Token::simpleMatch(tok, "* 0")) + { + if (Token::Match(tok->previous(), "[;{}=+-/(,]") || + Token::Match(tok->previous(), "return|<<")) + { + nullPointerError(tok); + } } } }