From b9b851f7a1db0c3d5cc4698cd10b2d652b6659a9 Mon Sep 17 00:00:00 2001 From: firewave Date: Sun, 26 May 2024 22:51:59 +0200 Subject: [PATCH] ValueFlow: extracted valueFlowDebug() into separate file --- Makefile | 6 ++++- lib/cppcheck.vcxproj | 2 ++ lib/lib.pri | 2 ++ lib/valueflow.cpp | 21 +----------------- lib/vf_analyze.h | 1 + lib/vf_debug.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++ lib/vf_debug.h | 31 ++++++++++++++++++++++++++ oss-fuzz/Makefile | 6 ++++- 8 files changed, 100 insertions(+), 22 deletions(-) create mode 100644 lib/vf_debug.cpp create mode 100644 lib/vf_debug.h diff --git a/Makefile b/Makefile index efed24445b1b..863aae8bf7ab 100644 --- a/Makefile +++ b/Makefile @@ -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 \ @@ -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 @@ -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 diff --git a/lib/cppcheck.vcxproj b/lib/cppcheck.vcxproj index 65300a9c81ab..78171d08a454 100644 --- a/lib/cppcheck.vcxproj +++ b/lib/cppcheck.vcxproj @@ -90,6 +90,7 @@ + @@ -182,6 +183,7 @@ + diff --git a/lib/lib.pri b/lib/lib.pri index e5b07f526b73..b2bcdf5c70fc 100644 --- a/lib/lib.pri +++ b/lib/lib.pri @@ -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 \ @@ -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 \ diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index e5835479d45e..ea6b019bab7e 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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()) { @@ -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)), }); } diff --git a/lib/vf_analyze.h b/lib/vf_analyze.h index 83b4428bac89..6ad7b910f2b0 100644 --- a/lib/vf_analyze.h +++ b/lib/vf_analyze.h @@ -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 diff --git a/lib/vf_debug.cpp b/lib/vf_debug.cpp new file mode 100644 index 000000000000..9bc54af31b81 --- /dev/null +++ b/lib/vf_debug.cpp @@ -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 . + */ + +#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 +#include + +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}); + } + } + } +} diff --git a/lib/vf_debug.h b/lib/vf_debug.h new file mode 100644 index 000000000000..8e5de01db1ab --- /dev/null +++ b/lib/vf_debug.h @@ -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 . + */ + +#ifndef vfDebugH +#define vfDebugH + +class TokenList; +class ErrorLogger; +class Settings; + +namespace ValueFlow +{ + void analyzeDebug(TokenList& tokenlist, ErrorLogger& errorLogger, const Settings& settings); +} + +#endif // vfDebugH diff --git a/oss-fuzz/Makefile b/oss-fuzz/Makefile index 963cabb38604..7076a6a018cf 100644 --- a/oss-fuzz/Makefile +++ b/oss-fuzz/Makefile @@ -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 \ @@ -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 @@ -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