From 729b2c1706f6829c18d84e3165d66f34d2dc8114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 21 Mar 2009 18:36:41 +0100 Subject: [PATCH] Fixed 188 (Return of auto variable address), applied patched submitted by gscacco --- Makefile | 4 ++++ src/checkautovariables.cpp | 30 +++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index bf9638169..c7c9c42df 100644 --- a/Makefile +++ b/Makefile @@ -32,6 +32,7 @@ TESTOBJ = test/testbufferoverrun.o \ test/testconstructors.o \ test/testcppcheck.o \ test/testdangerousfunctions.o \ + test/testautovariables.o \ test/testdivision.o \ test/testfilelister.o \ test/testfunctionusage.o \ @@ -180,6 +181,9 @@ test/testcppcheck.o: test/testcppcheck.cpp test/testsuite.h src/errorlogger.h sr test/testdangerousfunctions.o: test/testdangerousfunctions.cpp src/tokenize.h src/settings.h src/errorlogger.h src/token.h src/checkdangerousfunctions.h src/check.h test/testsuite.h $(CXX) $(CXXFLAGS) -c -o test/testdangerousfunctions.o test/testdangerousfunctions.cpp +test/testautovariables.o: test/testautovariables.cpp src/tokenize.h src/settings.h src/errorlogger.h src/token.h src/checkdangerousfunctions.h src/check.h test/testsuite.h + $(CXX) $(CXXFLAGS) -c -o test/testautovariables.o test/testautovariables.cpp + test/testdivision.o: test/testdivision.cpp src/tokenize.h src/settings.h src/errorlogger.h src/token.h src/checkother.h src/check.h test/testsuite.h $(CXX) $(CXXFLAGS) -c -o test/testdivision.o test/testdivision.cpp diff --git a/src/checkautovariables.cpp b/src/checkautovariables.cpp index fe4e4d97c..a1737f782 100644 --- a/src/checkautovariables.cpp +++ b/src/checkautovariables.cpp @@ -1,7 +1,7 @@ /* * Cppcheck - A tool for static C/C++ code analysis * Copyright (C) 2007-2009 Daniel Marjamäki, Reijo Tomperi, Nicolas Le Cam, - * Leandro Penz, Kimmo Varis, Vesa Pikki + * Leandro Penz, Kimmo Varis, Vesa Pikki, Gianluca Scacco * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -111,11 +111,26 @@ bool isTypeName(const Token *tok) ret |= (_str == type[i]); return !ret; } +bool isStatic(const Token *tok) +{ + bool res = false; + + if (Token::Match(tok->tokAt(-1), "static ")) + res = true; + else if (Token::Match(tok->tokAt(-2), "static ")) + res = true; + else if (Token::Match(tok->tokAt(-3), "static ")) + res = true; + + //std::cout << __PRETTY_FUNCTION__ << " " << tok->str() << " " << res << std::endl; + return res; + +} void CheckAutoVariables::addVD(const Token* tok) { std::string var_name; var_name = tok->str(); - //cout << "VD " << tok->linenr() << " " << var_name << endl; + //std::cout << "VD " << tok->linenr() << " " << var_name << std::endl; vd_list.push_back(var_name); } void CheckAutoVariables::autoVariables() @@ -155,20 +170,17 @@ void CheckAutoVariables::autoVariables() { bindent--; } - else if (bindent > 0 && Token::Match(tok, "%type% :: %any%")) //Inside a function + else if (bindent > 0 && Token::Match(tok, "%type% :: %any%") && !isStatic(tok)) //Inside a function { - std::string var_name; - //print(tok,5); - var_name = tok->tokAt(2)->str(); - vd_list.push_back(var_name); + addVD(tok->tokAt(2)); } - else if (bindent > 0 && Token::Match(tok, "%var% %var% ;")) //Inside a function + else if (bindent > 0 && Token::Match(tok, "%var% %var% ;") && !isStatic(tok)) //Inside a function { if (!isTypeName(tok)) continue; addVD(tok->tokAt(1)); } - else if (bindent > 0 && Token::Match(tok, "const %var% %var% ;")) //Inside a function + else if (bindent > 0 && Token::Match(tok, "const %var% %var% ;") && !isStatic(tok)) //Inside a function { if (!isTypeName(tok->tokAt(1))) continue;