Refactoring: Move some AST related functions to distinct source file

This commit is contained in:
Alexander Mai 2015-08-02 21:57:32 +02:00
parent ea62ab337b
commit 96c3c111fc
7 changed files with 146 additions and 87 deletions

View File

@ -115,7 +115,8 @@ MAN_SOURCE=man/cppcheck.1.xml
###### Object Files
LIBOBJ = $(SRCDIR)/check.o \
LIBOBJ = $(SRCDIR)/astutils.o \
$(SRCDIR)/check.o \
$(SRCDIR)/check64bit.o \
$(SRCDIR)/checkassert.o \
$(SRCDIR)/checkautovariables.o \
@ -282,6 +283,9 @@ endif
###### Build
$(SRCDIR)/astutils.o: lib/astutils.cpp lib/cxx11emu.h lib/astutils.h lib/token.h lib/config.h lib/valueflow.h lib/mathlib.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CFG) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -std=c++0x -c -o $(SRCDIR)/astutils.o $(SRCDIR)/astutils.cpp
$(SRCDIR)/check.o: lib/check.cpp lib/cxx11emu.h lib/check.h lib/config.h lib/token.h lib/valueflow.h lib/mathlib.h lib/tokenize.h lib/errorlogger.h lib/suppressions.h lib/tokenlist.h lib/settings.h lib/library.h lib/standards.h lib/timer.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CFG) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -std=c++0x -c -o $(SRCDIR)/check.o $(SRCDIR)/check.cpp
@ -333,7 +337,7 @@ $(SRCDIR)/checknullpointer.o: lib/checknullpointer.cpp lib/cxx11emu.h lib/checkn
$(SRCDIR)/checkobsolescentfunctions.o: lib/checkobsolescentfunctions.cpp lib/cxx11emu.h lib/checkobsolescentfunctions.h lib/config.h lib/check.h lib/token.h lib/valueflow.h lib/mathlib.h lib/tokenize.h lib/errorlogger.h lib/suppressions.h lib/tokenlist.h lib/settings.h lib/library.h lib/standards.h lib/timer.h lib/symboldatabase.h lib/utils.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CFG) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -std=c++0x -c -o $(SRCDIR)/checkobsolescentfunctions.o $(SRCDIR)/checkobsolescentfunctions.cpp
$(SRCDIR)/checkother.o: lib/checkother.cpp lib/cxx11emu.h lib/checkother.h lib/config.h lib/check.h lib/token.h lib/valueflow.h lib/mathlib.h lib/tokenize.h lib/errorlogger.h lib/suppressions.h lib/tokenlist.h lib/settings.h lib/library.h lib/standards.h lib/timer.h lib/symboldatabase.h lib/utils.h
$(SRCDIR)/checkother.o: lib/checkother.cpp lib/cxx11emu.h lib/checkother.h lib/config.h lib/check.h lib/token.h lib/valueflow.h lib/mathlib.h lib/tokenize.h lib/errorlogger.h lib/suppressions.h lib/tokenlist.h lib/settings.h lib/library.h lib/standards.h lib/timer.h lib/astutils.h lib/symboldatabase.h lib/utils.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CFG) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -std=c++0x -c -o $(SRCDIR)/checkother.o $(SRCDIR)/checkother.cpp
$(SRCDIR)/checkpostfixoperator.o: lib/checkpostfixoperator.cpp lib/cxx11emu.h lib/checkpostfixoperator.h lib/config.h lib/check.h lib/token.h lib/valueflow.h lib/mathlib.h lib/tokenize.h lib/errorlogger.h lib/suppressions.h lib/tokenlist.h lib/settings.h lib/library.h lib/standards.h lib/timer.h lib/symboldatabase.h lib/utils.h

102
lib/astutils.cpp Normal file
View File

@ -0,0 +1,102 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2015 Daniel Marjamäki and Cppcheck team.
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
#include "astutils.h"
bool astIsIntegral(const Token *tok, bool unknown)
{
// TODO: handle arrays
if (tok->isNumber())
return MathLib::isInt(tok->str());
if (tok->isName()) {
if (tok->variable())
return tok->variable()->isIntegralType();
return unknown;
}
if (tok->str() == "(") {
// cast
if (Token::Match(tok, "( const| float|double )"))
return false;
// Function call
if (tok->previous()->function()) {
if (Token::Match(tok->previous()->function()->retDef, "float|double"))
return false;
else if (Token::Match(tok->previous()->function()->retDef, "bool|char|short|int|long"))
return true;
}
if (tok->strAt(-1) == "sizeof")
return true;
return unknown;
}
if (tok->astOperand2() && (tok->str() == "." || tok->str() == "::"))
return astIsIntegral(tok->astOperand2(), unknown);
if (tok->astOperand1() && tok->str() != "?")
return astIsIntegral(tok->astOperand1(), unknown);
return unknown;
}
bool astIsFloat(const Token *tok, bool unknown)
{
// TODO: handle arrays
if (tok->isNumber())
return MathLib::isFloat(tok->str());
if (tok->isName()) {
if (tok->variable())
return tok->variable()->isFloatingType();
return unknown;
}
if (tok->str() == "(") {
// cast
if (Token::Match(tok, "( const| float|double )"))
return true;
// Function call
if (tok->previous()->function())
return Token::Match(tok->previous()->function()->retDef, "float|double");
if (tok->strAt(-1) == "sizeof")
return false;
return unknown;
}
if (tok->astOperand2() && (tok->str() == "." || tok->str() == "::"))
return astIsFloat(tok->astOperand2(), unknown);
if (tok->astOperand1() && tok->str() != "?" && astIsFloat(tok->astOperand1(), unknown))
return true;
if (tok->astOperand2() && astIsFloat(tok->astOperand2(), unknown))
return true;
if (tok->isOp())
return false;
return unknown;
}

33
lib/astutils.h Normal file
View File

@ -0,0 +1,33 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2015 Daniel Marjamäki and Cppcheck team.
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
#ifndef astutilsH
#define astutilsH
//---------------------------------------------------------------------------
#include "symboldatabase.h"
#include "token.h"
/** Is expression of integral type? */
bool astIsIntegral(const Token *tok, bool unknown);
/** Is expression of floating point type? */
bool astIsFloat(const Token *tok, bool unknown);
#endif // astutilsH

View File

@ -21,6 +21,7 @@
//---------------------------------------------------------------------------
#include "checkcondition.h"
#include "astutils.h"
#include "checkother.h"
#include "symboldatabase.h"

View File

@ -19,6 +19,7 @@
//---------------------------------------------------------------------------
#include "checkother.h"
#include "astutils.h"
#include "mathlib.h"
#include "symboldatabase.h"
@ -32,86 +33,6 @@ namespace {
CheckOther instance;
}
bool astIsIntegral(const Token *tok, bool unknown)
{
// TODO: handle arrays
if (tok->isNumber())
return MathLib::isInt(tok->str());
if (tok->isName()) {
if (tok->variable())
return tok->variable()->isIntegralType();
return unknown;
}
if (tok->str() == "(") {
// cast
if (Token::Match(tok, "( const| float|double )"))
return false;
// Function call
if (tok->previous()->function()) {
if (Token::Match(tok->previous()->function()->retDef, "float|double"))
return false;
else if (Token::Match(tok->previous()->function()->retDef, "bool|char|short|int|long"))
return true;
}
if (tok->strAt(-1) == "sizeof")
return true;
return unknown;
}
if (tok->astOperand2() && (tok->str() == "." || tok->str() == "::"))
return astIsIntegral(tok->astOperand2(), unknown);
if (tok->astOperand1() && tok->str() != "?")
return astIsIntegral(tok->astOperand1(), unknown);
return unknown;
}
bool astIsFloat(const Token *tok, bool unknown)
{
// TODO: handle arrays
if (tok->isNumber())
return MathLib::isFloat(tok->str());
if (tok->isName()) {
if (tok->variable())
return tok->variable()->isFloatingType();
return unknown;
}
if (tok->str() == "(") {
// cast
if (Token::Match(tok, "( const| float|double )"))
return true;
// Function call
if (tok->previous()->function())
return Token::Match(tok->previous()->function()->retDef, "float|double");
if (tok->strAt(-1) == "sizeof")
return false;
return unknown;
}
if (tok->astOperand2() && (tok->str() == "." || tok->str() == "::"))
return astIsFloat(tok->astOperand2(), unknown);
if (tok->astOperand1() && tok->str() != "?" && astIsFloat(tok->astOperand1(), unknown))
return true;
if (tok->astOperand2() && astIsFloat(tok->astOperand2(), unknown))
return true;
if (tok->isOp())
return false;
return unknown;
}
bool isConstExpression(const Token *tok, const std::set<std::string> &constFunctions)
{

View File

@ -35,10 +35,6 @@ bool isSameExpression(const Tokenizer *tokenizer, const Token *tok1, const Token
bool isWithoutSideEffects(const Tokenizer *tokenizer, const Token* tok);
/** Is expression of floating point type? */
bool astIsFloat(const Token *tok, bool unknown);
/// @addtogroup Checks
/// @{

View File

@ -6,6 +6,7 @@ include($$PWD/../externals/tinyxml/tinyxml.pri)
BASEPATH = ../lib/
INCLUDEPATH += ../externals/tinyxml
HEADERS += $${BASEPATH}check.h \
$${BASEPATH}astutils.h \
$${BASEPATH}check.h \
$${BASEPATH}check64bit.h \
$${BASEPATH}checkassert.h \
@ -50,7 +51,8 @@ HEADERS += $${BASEPATH}check.h \
$${BASEPATH}valueflow.h \
SOURCES += $${BASEPATH}check.cpp \
SOURCES += $${BASEPATH}astutils.cpp \
$${BASEPATH}check.cpp \
$${BASEPATH}check64bit.cpp \
$${BASEPATH}checkassert.cpp \
$${BASEPATH}checkautovariables.cpp \