Skip to content

Commit

Permalink
ValueFlow: extracted valueFlowDebug() into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Jul 1, 2024
1 parent 178fb4c commit b9b851f
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 22 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
$(libcppdir)/vf_array.o \
$(libcppdir)/vf_bitand.o \
$(libcppdir)/vf_common.o \
$(libcppdir)/vf_debug.o \
$(libcppdir)/vf_enumvalue.o \
$(libcppdir)/vf_globalconstvar.o \
$(libcppdir)/vf_globalstaticvar.o \
Expand Down Expand Up @@ -472,7 +473,7 @@ validateRules:

###### Build

$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.h lib/calculate.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/path.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_array.h lib/vf_bitand.h lib/vf_common.h lib/vf_enumvalue.h lib/vf_globalconstvar.h lib/vf_globalstaticvar.h lib/vf_number.h lib/vf_pointeralias.h lib/vf_sameexpressions.h lib/vf_settokenvalue.h lib/vf_string.h lib/vf_unknownfunctionreturn.h lib/vfvalue.h
$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.h lib/calculate.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/path.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_array.h lib/vf_bitand.h lib/vf_common.h lib/vf_debug.h lib/vf_enumvalue.h lib/vf_globalconstvar.h lib/vf_globalstaticvar.h lib/vf_number.h lib/vf_pointeralias.h lib/vf_sameexpressions.h lib/vf_settokenvalue.h lib/vf_string.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 @@ -667,6 +668,9 @@ $(libcppdir)/vf_bitand.o: lib/vf_bitand.cpp lib/config.h lib/errortypes.h lib/ma
$(libcppdir)/vf_common.o: lib/vf_common.cpp lib/addoninfo.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.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/valueflow.h lib/vf_common.h lib/vf_settokenvalue.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_common.cpp

$(libcppdir)/vf_debug.o: lib/vf_debug.cpp lib/addoninfo.h lib/color.h lib/config.h lib/errorlogger.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_common.h lib/vf_debug.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_debug.cpp

$(libcppdir)/vf_enumvalue.o: lib/vf_enumvalue.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/utils.h lib/valueflow.h lib/vf_enumvalue.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_enumvalue.cpp

Expand Down
2 changes: 2 additions & 0 deletions lib/cppcheck.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<ClCompile Include="vf_array.cpp" />
<ClCompile Include="vf_bitand.cpp" />
<ClCompile Include="vf_common.cpp" />
<ClCompile Include="vf_debug.cpp" />
<ClCompile Include="vf_enumvalue.cpp" />
<ClCompile Include="vf_globalconstvar.cpp" />
<ClCompile Include="vf_globalstaticvar.cpp" />
Expand Down Expand Up @@ -182,6 +183,7 @@
<ClInclude Include="vf_array.h" />
<ClInclude Include="vf_bitand.h" />
<ClInclude Include="vf_common.h" />
<ClInclude Include="vf_debug.h" />
<ClInclude Include="vf_enumvalue.h" />
<ClInclude Include="vf_globalconstvar.h" />
<ClInclude Include="vf_globalstaticvar.h" />
Expand Down
2 changes: 2 additions & 0 deletions lib/lib.pri
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ HEADERS += $${PWD}/addoninfo.h \
$${PWD}/vf_array.h \
$${PWD}/vf_bitand.h \
$${PWD}/vf_common.h \
$${PWD}/vf_debug.h \
$${PWD}/vf_enumvalue.h \
$${PWD}/vf_globalconstvar.h \
$${PWD}/vf_globalstaticvar.h \
Expand Down Expand Up @@ -158,6 +159,7 @@ SOURCES += $${PWD}/valueflow.cpp \
$${PWD}/vf_array.cpp \
$${PWD}/vf_bitand.cpp \
$${PWD}/vf_common.cpp \
$${PWD}/vf_debug.cpp \
$${PWD}/vf_enumvalue.cpp \
$${PWD}/vf_globalconstvar.cpp \
$${PWD}/vf_globalstaticvar.cpp \
Expand Down
21 changes: 1 addition & 20 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8094,25 +8094,6 @@ static void valueFlowSafeFunctions(const TokenList& tokenlist, const SymbolDatab
}
}

static void valueFlowDebug(TokenList& tokenlist, ErrorLogger& errorLogger, const Settings& settings)
{
if (!settings.debugnormal && !settings.debugwarnings)
return;
for (Token* tok = tokenlist.front(); tok; tok = tok->next()) {
if (tok->getTokenDebug() != TokenDebug::ValueFlow)
continue;
if (tok->astParent() && tok->astParent()->getTokenDebug() == TokenDebug::ValueFlow)
continue;
for (const ValueFlow::Value& v : tok->values()) {
std::string msg = "The value is " + debugString(v);
ErrorPath errorPath = v.errorPath;
errorPath.insert(errorPath.end(), v.debugPath.cbegin(), v.debugPath.cend());
errorPath.emplace_back(tok, "");
errorLogger.reportErr({errorPath, &tokenlist, Severity::debug, "valueFlow", msg, CWE{0}, Certainty::normal});
}
}
}

const ValueFlow::Value *ValueFlow::valueFlowConstantFoldAST(Token *expr, const Settings &settings)
{
if (expr && expr->values().empty()) {
Expand Down Expand Up @@ -8383,7 +8364,7 @@ void ValueFlow::setValues(TokenList& tokenlist,

runner.run_once({
VFA(valueFlowDynamicBufferSize(tokenlist, symboldatabase, errorLogger, settings)),
VFA(valueFlowDebug(tokenlist, errorLogger, settings)),
VFA(analyzeDebug(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 @@ -21,6 +21,7 @@

#include "vf_array.h" // IWYU pragma: export
#include "vf_bitand.h" // IWYU pragma: export
#include "vf_debug.h" // IWYU pragma: export
#include "vf_enumvalue.h" // IWYU pragma: export
#include "vf_globalconstvar.h" // IWYU pragma: export
#include "vf_globalstaticvar.h" // IWYU pragma: export
Expand Down
53 changes: 53 additions & 0 deletions lib/vf_debug.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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_debug.h"

#include "errorlogger.h"
#include "errortypes.h"
#include "settings.h"
#include "token.h"
#include "tokenlist.h"
#include "vfvalue.h"

#include "vf_common.h"

#include <list>
#include <string>

namespace ValueFlow
{
void analyzeDebug(TokenList& tokenlist, ErrorLogger& errorLogger, const Settings& settings)
{
if (!settings.debugnormal && !settings.debugwarnings)
return;
for (Token* tok = tokenlist.front(); tok; tok = tok->next()) {
if (tok->getTokenDebug() != TokenDebug::ValueFlow)
continue;
if (tok->astParent() && tok->astParent()->getTokenDebug() == TokenDebug::ValueFlow)
continue;
for (const Value& v : tok->values()) {
std::string msg = "The value is " + debugString(v);
ErrorPath errorPath = v.errorPath;
errorPath.insert(errorPath.end(), v.debugPath.cbegin(), v.debugPath.cend());
errorPath.emplace_back(tok, "");
errorLogger.reportErr({errorPath, &tokenlist, Severity::debug, "valueFlow", msg, CWE{0}, Certainty::normal});
}
}
}
}
31 changes: 31 additions & 0 deletions lib/vf_debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 vfDebugH
#define vfDebugH

class TokenList;
class ErrorLogger;
class Settings;

namespace ValueFlow
{
void analyzeDebug(TokenList& tokenlist, ErrorLogger& errorLogger, const Settings& settings);
}

#endif // vfDebugH
6 changes: 5 additions & 1 deletion oss-fuzz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
$(libcppdir)/vf_array.o \
$(libcppdir)/vf_bitand.o \
$(libcppdir)/vf_common.o \
$(libcppdir)/vf_debug.o \
$(libcppdir)/vf_enumvalue.o \
$(libcppdir)/vf_globalconstvar.o \
$(libcppdir)/vf_globalstaticvar.o \
Expand Down Expand Up @@ -151,7 +152,7 @@ simplecpp.o: ../externals/simplecpp/simplecpp.cpp ../externals/simplecpp/simplec
tinyxml2.o: ../externals/tinyxml2/tinyxml2.cpp ../externals/tinyxml2/tinyxml2.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -w -c -o $@ ../externals/tinyxml2/tinyxml2.cpp

$(libcppdir)/valueflow.o: ../lib/valueflow.cpp ../lib/addoninfo.h ../lib/analyzer.h ../lib/astutils.h ../lib/calculate.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/path.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_array.h ../lib/vf_bitand.h ../lib/vf_common.h ../lib/vf_enumvalue.h ../lib/vf_globalconstvar.h ../lib/vf_globalstaticvar.h ../lib/vf_number.h ../lib/vf_pointeralias.h ../lib/vf_sameexpressions.h ../lib/vf_settokenvalue.h ../lib/vf_string.h ../lib/vf_unknownfunctionreturn.h ../lib/vfvalue.h
$(libcppdir)/valueflow.o: ../lib/valueflow.cpp ../lib/addoninfo.h ../lib/analyzer.h ../lib/astutils.h ../lib/calculate.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/path.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_array.h ../lib/vf_bitand.h ../lib/vf_common.h ../lib/vf_debug.h ../lib/vf_enumvalue.h ../lib/vf_globalconstvar.h ../lib/vf_globalstaticvar.h ../lib/vf_number.h ../lib/vf_pointeralias.h ../lib/vf_sameexpressions.h ../lib/vf_settokenvalue.h ../lib/vf_string.h ../lib/vf_unknownfunctionreturn.h ../lib/vfvalue.h
$(CXX) ${LIB_FUZZING_ENGINE} $(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 @@ -346,6 +347,9 @@ $(libcppdir)/vf_bitand.o: ../lib/vf_bitand.cpp ../lib/config.h ../lib/errortypes
$(libcppdir)/vf_common.o: ../lib/vf_common.cpp ../lib/addoninfo.h ../lib/config.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/path.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/valueflow.h ../lib/vf_common.h ../lib/vf_settokenvalue.h ../lib/vfvalue.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_common.cpp

$(libcppdir)/vf_debug.o: ../lib/vf_debug.cpp ../lib/addoninfo.h ../lib/color.h ../lib/config.h ../lib/errorlogger.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_common.h ../lib/vf_debug.h ../lib/vfvalue.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_debug.cpp

$(libcppdir)/vf_enumvalue.o: ../lib/vf_enumvalue.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/utils.h ../lib/valueflow.h ../lib/vf_enumvalue.h ../lib/vfvalue.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_enumvalue.cpp

Expand Down

0 comments on commit b9b851f

Please sign in to comment.