Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueFlow: extracted valueFlowSwitchVariable() into separate file #6783

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,12 @@ LIBOBJ = $(libcppdir)/valueflow.o \
$(libcppdir)/vf_iterators.o \
$(libcppdir)/vf_number.o \
$(libcppdir)/vf_pointeralias.o \
$(libcppdir)/vf_reverse.o \
$(libcppdir)/vf_rightshift.o \
$(libcppdir)/vf_sameexpressions.o \
$(libcppdir)/vf_settokenvalue.o \
$(libcppdir)/vf_string.o \
$(libcppdir)/vf_switchvariable.o \
$(libcppdir)/vf_symbolicinfer.o \
$(libcppdir)/vf_unknownfunctionreturn.o \
$(libcppdir)/vfvalue.o
Expand Down Expand Up @@ -485,7 +487,7 @@ validateRules:

###### Build

$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.h lib/check.h lib/checkuninitvar.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/findtoken.h lib/forwardanalyzer.h lib/infer.h lib/library.h lib/mathlib.h lib/platform.h lib/programmemory.h lib/reverseanalyzer.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vf_analyze.h lib/vf_analyzers.h lib/vf_array.h lib/vf_arraybool.h lib/vf_arrayelement.h lib/vf_bailout.h lib/vf_bitand.h lib/vf_common.h lib/vf_debug.h lib/vf_enumvalue.h lib/vf_functionreturn.h lib/vf_globalconstvar.h lib/vf_globalstaticvar.h lib/vf_impossiblevalues.h lib/vf_infercondition.h lib/vf_iteratorinfer.h lib/vf_iterators.h lib/vf_number.h lib/vf_pointeralias.h lib/vf_rightshift.h lib/vf_sameexpressions.h lib/vf_settokenvalue.h lib/vf_string.h lib/vf_symbolicinfer.h lib/vf_unknownfunctionreturn.h lib/vfvalue.h
$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.h lib/check.h lib/checkuninitvar.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/findtoken.h lib/forwardanalyzer.h lib/infer.h lib/library.h lib/mathlib.h lib/platform.h lib/programmemory.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vf_analyze.h lib/vf_analyzers.h lib/vf_array.h lib/vf_arraybool.h lib/vf_arrayelement.h lib/vf_bailout.h lib/vf_bitand.h lib/vf_common.h lib/vf_debug.h lib/vf_enumvalue.h lib/vf_functionreturn.h lib/vf_globalconstvar.h lib/vf_globalstaticvar.h lib/vf_impossiblevalues.h lib/vf_infercondition.h lib/vf_iteratorinfer.h lib/vf_iterators.h lib/vf_number.h lib/vf_pointeralias.h lib/vf_reverse.h lib/vf_rightshift.h lib/vf_sameexpressions.h lib/vf_settokenvalue.h lib/vf_string.h lib/vf_switchvariable.h lib/vf_symbolicinfer.h lib/vf_unknownfunctionreturn.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/valueflow.cpp

$(libcppdir)/tokenize.o: lib/tokenize.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/astutils.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/summaries.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/vfvalue.h
Expand Down Expand Up @@ -725,6 +727,9 @@ $(libcppdir)/vf_number.o: lib/vf_number.cpp lib/config.h lib/errortypes.h lib/li
$(libcppdir)/vf_pointeralias.o: lib/vf_pointeralias.cpp lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vf_pointeralias.h lib/vf_settokenvalue.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_pointeralias.cpp

$(libcppdir)/vf_reverse.o: lib/vf_reverse.cpp lib/addoninfo.h lib/analyzer.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/reverseanalyzer.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueptr.h lib/vf_analyzers.h lib/vf_common.h lib/vf_reverse.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_reverse.cpp

$(libcppdir)/vf_rightshift.o: lib/vf_rightshift.cpp lib/addoninfo.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vf_rightshift.h lib/vf_settokenvalue.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_rightshift.cpp

Expand All @@ -737,6 +742,9 @@ $(libcppdir)/vf_settokenvalue.o: lib/vf_settokenvalue.cpp lib/addoninfo.h lib/as
$(libcppdir)/vf_string.o: lib/vf_string.cpp lib/config.h lib/errortypes.h lib/mathlib.h lib/sourcelocation.h lib/standards.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vf_settokenvalue.h lib/vf_string.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_string.cpp

$(libcppdir)/vf_switchvariable.o: lib/vf_switchvariable.cpp lib/addoninfo.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/vf_bailout.h lib/vf_reverse.h lib/vf_switchvariable.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_switchvariable.cpp

$(libcppdir)/vf_symbolicinfer.o: lib/vf_symbolicinfer.cpp lib/astutils.h lib/config.h lib/errortypes.h lib/infer.h lib/library.h lib/mathlib.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueptr.h lib/vf_settokenvalue.h lib/vf_symbolicinfer.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_symbolicinfer.cpp

Expand Down
4 changes: 4 additions & 0 deletions lib/cppcheck.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@
<ClCompile Include="vf_iterators.cpp" />
<ClCompile Include="vf_number.cpp" />
<ClCompile Include="vf_pointeralias.cpp" />
<ClCompile Include="vf_reverse.cpp" />
<ClCompile Include="vf_rightshift.cpp" />
<ClCompile Include="vf_sameexpressions.cpp" />
<ClCompile Include="vf_settokenvalue.cpp" />
<ClCompile Include="vf_string.cpp" />
<ClCompile Include="vf_switchvariable.cpp" />
<ClCompile Include="vf_symbolicinfer.cpp" />
<ClCompile Include="vf_unknownfunctionreturn.cpp" />
<ClCompile Include="vfvalue.cpp" />
Expand Down Expand Up @@ -211,10 +213,12 @@
<ClInclude Include="vf_iterators.h" />
<ClInclude Include="vf_number.h" />
<ClInclude Include="vf_pointeralias.h" />
<ClInclude Include="vf_reverse.h" />
<ClInclude Include="vf_rightshift.h" />
<ClInclude Include="vf_sameexpressions.h" />
<ClInclude Include="vf_settokenvalue.h" />
<ClInclude Include="vf_string.h" />
<ClInclude Include="vf_switchvariable.h" />
<ClInclude Include="vf_symbolicinfer.h" />
<ClInclude Include="vf_unknownfunctionreturn.h" />
<ClInclude Include="vfvalue.h" />
Expand Down
4 changes: 4 additions & 0 deletions lib/lib.pri
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ HEADERS += $${PWD}/addoninfo.h \
$${PWD}/vf_iterators.h \
$${PWD}/vf_number.h \
$${PWD}/vf_pointeralias.h \
$${PWD}/vf_reverse.h \
$${PWD}/vf_rightshift.h \
$${PWD}/vf_sameexpressions.h \
$${PWD}/vf_settokenvalue.h \
$${PWD}/vf_string.h \
$${PWD}/vf_switchvariable.h \
$${PWD}/vf_symbolicinfer.h \
$${PWD}/vf_unknownfunctionreturn.h \
$${PWD}/vfvalue.h \
Expand Down Expand Up @@ -187,10 +189,12 @@ SOURCES += $${PWD}/valueflow.cpp \
$${PWD}/vf_iterators.cpp \
$${PWD}/vf_number.cpp \
$${PWD}/vf_pointeralias.cpp \
$${PWD}/vf_reverse.cpp \
$${PWD}/vf_rightshift.cpp \
$${PWD}/vf_sameexpressions.cpp \
$${PWD}/vf_settokenvalue.cpp \
$${PWD}/vf_string.cpp \
$${PWD}/vf_switchvariable.cpp \
$${PWD}/vf_symbolicinfer.cpp \
$${PWD}/vf_unknownfunctionreturn.cpp \
$${PWD}/vfvalue.cpp
104 changes: 2 additions & 102 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
#include "mathlib.h"
#include "platform.h"
#include "programmemory.h"
#include "reverseanalyzer.h"
#include "settings.h"
#include "smallvector.h"
#include "sourcelocation.h"
Expand All @@ -107,6 +106,7 @@
#include "vf_analyzers.h"
#include "vf_bailout.h"
#include "vf_common.h"
#include "vf_reverse.h"
#include "vf_settokenvalue.h"

#include <algorithm>
Expand Down Expand Up @@ -593,34 +593,6 @@ static Analyzer::Result valueFlowForwardRecursive(Token* top,
return result;
}

static void valueFlowReverse(Token* tok,
const Token* const endToken,
const Token* const varToken,
std::list<ValueFlow::Value> values,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc = SourceLocation::current())
{
for (ValueFlow::Value& v : values) {
if (settings.debugnormal)
setSourceLocation(v, loc, tok);
valueFlowGenericReverse(tok, endToken, makeReverseAnalyzer(varToken, std::move(v), settings), tokenlist, errorLogger, settings);
}
}

// Deprecated
static void valueFlowReverse(const TokenList& tokenlist,
Token* tok,
const Token* const varToken,
ValueFlow::Value val,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc = SourceLocation::current())
{
valueFlowReverse(tok, nullptr, varToken, {std::move(val)}, tokenlist, errorLogger, settings, loc);
}

static bool isConditionKnown(const Token* tok, bool then)
{
const char* op = "||";
Expand Down Expand Up @@ -4552,78 +4524,6 @@ static void valueFlowInjectParameter(const TokenList& tokenlist,
settings);
}

static void valueFlowSwitchVariable(const TokenList &tokenlist, const SymbolDatabase& symboldatabase, ErrorLogger &errorLogger, const Settings &settings)
{
for (const Scope &scope : symboldatabase.scopeList) {
if (scope.type != Scope::ScopeType::eSwitch)
continue;
if (!Token::Match(scope.classDef, "switch ( %var% ) {"))
continue;
const Token *vartok = scope.classDef->tokAt(2);
const Variable *var = vartok->variable();
if (!var)
continue;

// bailout: global non-const variables
if (!(var->isLocal() || var->isArgument()) && !var->isConst()) {
if (settings.debugwarnings)
bailout(tokenlist, errorLogger, vartok, "switch variable " + var->name() + " is global");
continue;
}

for (const Token *tok = scope.bodyStart->next(); tok != scope.bodyEnd; tok = tok->next()) {
if (tok->str() == "{") {
tok = tok->link();
continue;
}
if (Token::Match(tok, "case %num% :")) {
std::list<ValueFlow::Value> values;
values.emplace_back(MathLib::toBigNumber(tok->strAt(1)));
values.back().condition = tok;
values.back().errorPath.emplace_back(tok, "case " + tok->strAt(1) + ": " + vartok->str() + " is " + tok->strAt(1) + " here.");
bool known = false;
if ((Token::simpleMatch(tok->previous(), "{") || Token::simpleMatch(tok->tokAt(-2), "break ;")) && !Token::Match(tok->tokAt(3), ";| case"))
known = true;
while (Token::Match(tok->tokAt(3), ";| case %num% :")) {
known = false;
tok = tok->tokAt(3);
if (!tok->isName())
tok = tok->next();
values.emplace_back(MathLib::toBigNumber(tok->strAt(1)));
values.back().condition = tok;
values.back().errorPath.emplace_back(tok, "case " + tok->strAt(1) + ": " + vartok->str() + " is " + tok->strAt(1) + " here.");
}
for (auto val = values.cbegin(); val != values.cend(); ++val) {
valueFlowReverse(tokenlist,
const_cast<Token*>(scope.classDef),
vartok,
*val,
errorLogger,
settings);
}
if (vartok->variable()->scope()) {
if (known)
values.back().setKnown();

// FIXME We must check if there is a return. See #9276
/*
valueFlowForwardVariable(tok->tokAt(3),
vartok->variable()->scope()->bodyEnd,
vartok->variable(),
vartok->varId(),
values,
values.back().isKnown(),
false,
tokenlist,
errorLogger,
settings);
*/
}
}
}
}
}

static std::list<ValueFlow::Value> getFunctionArgumentValues(const Token *argtok)
{
std::list<ValueFlow::Value> argvalues(argtok->values());
Expand Down Expand Up @@ -6122,7 +6022,7 @@ void ValueFlow::setValues(TokenList& tokenlist,
VFA_CPP(valueFlowAfterSwap(tokenlist, symboldatabase, errorLogger, settings)),
VFA(valueFlowCondition(SimpleConditionHandler{}, tokenlist, symboldatabase, errorLogger, settings, skippedFunctions)),
VFA(analyzeInferCondition(tokenlist, settings)),
VFA(valueFlowSwitchVariable(tokenlist, symboldatabase, errorLogger, settings)),
VFA(analyzeSwitchVariable(tokenlist, symboldatabase, errorLogger, settings)),
VFA(valueFlowForLoop(tokenlist, symboldatabase, errorLogger, settings)),
VFA(valueFlowSubFunction(tokenlist, symboldatabase, errorLogger, settings)),
VFA(analyzeFunctionReturn(tokenlist, errorLogger, settings)),
Expand Down
1 change: 1 addition & 0 deletions lib/vf_analyze.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "vf_rightshift.h" // IWYU pragma: export
#include "vf_sameexpressions.h" // IWYU pragma: export
#include "vf_string.h" // IWYU pragma: export
#include "vf_switchvariable.h" // IWYU pragma: export
#include "vf_symbolicinfer.h" // IWYU pragma: export
#include "vf_unknownfunctionreturn.h" // IWYU pragma: export

Expand Down
47 changes: 47 additions & 0 deletions lib/vf_reverse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2024 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 "vf_reverse.h"

#include "reverseanalyzer.h"
#include "settings.h"
#include "vfvalue.h"

#include "vf_analyzers.h"
#include "vf_common.h"

#include <utility>

namespace ValueFlow
{
void valueFlowReverse(Token* tok,
const Token* const endToken,
const Token* const varToken,
std::list<Value> values,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc)
{
for (Value& v : values) {
if (settings.debugnormal)
setSourceLocation(v, loc, tok);
valueFlowGenericReverse(tok, endToken, makeReverseAnalyzer(varToken, std::move(v), settings), tokenlist, errorLogger, settings);
}
}
}
47 changes: 47 additions & 0 deletions lib/vf_reverse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* -*- C++ -*-
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2024 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 vfReverseeH
#define vfReverseeH

#include "sourcelocation.h"

#include <list>

class TokenList;
class ErrorLogger;
class Settings;
class Token;
namespace ValueFlow
{
class Value;
}

namespace ValueFlow {
void valueFlowReverse(Token* tok,
const Token* const endToken,
const Token* const varToken,
std::list<ValueFlow::Value> values,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc = SourceLocation::current());

}

#endif // vfReverseeH
Loading
Loading