From 4e4997c2b2aba52767bdcec5bd7dd0324581b9ea Mon Sep 17 00:00:00 2001 From: firewave Date: Thu, 6 Feb 2025 02:47:29 +0100 Subject: [PATCH] fixed #13607 - print float values as such in valueflow debug output --- Makefile | 6 +++++- lib/vfvalue.cpp | 3 ++- test/cli/other_test.py | 8 +++---- test/testrunner.vcxproj | 1 + test/testvfvalue.cpp | 48 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 test/testvfvalue.cpp diff --git a/Makefile b/Makefile index ef76dfed835..5cdce7a9256 100644 --- a/Makefile +++ b/Makefile @@ -335,7 +335,8 @@ TESTOBJ = test/fixture.o \ test/testutils.o \ test/testvaarg.o \ test/testvalueflow.o \ - test/testvarid.o + test/testvarid.o \ + test/testvfvalue.o .PHONY: run-dmake tags @@ -900,6 +901,9 @@ test/testvalueflow.o: test/testvalueflow.cpp externals/simplecpp/simplecpp.h lib test/testvarid.o: test/testvarid.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testvarid.cpp +test/testvfvalue.o: test/testvfvalue.cpp lib/addoninfo.h lib/check.h lib/checkers.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/standards.h lib/suppressions.h lib/utils.h lib/vfvalue.h test/fixture.h + $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testvfvalue.cpp + externals/simplecpp/simplecpp.o: externals/simplecpp/simplecpp.cpp externals/simplecpp/simplecpp.h $(CXX) $(CPPFLAGS) $(CXXFLAGS) -w -c -o $@ externals/simplecpp/simplecpp.cpp diff --git a/lib/vfvalue.cpp b/lib/vfvalue.cpp index 8e694c6eeb7..9dd1d423faa 100644 --- a/lib/vfvalue.cpp +++ b/lib/vfvalue.cpp @@ -19,6 +19,7 @@ #include "vfvalue.h" #include "errortypes.h" +#include "mathlib.h" #include "token.h" #include @@ -58,7 +59,7 @@ namespace ValueFlow { ss << this->tokvalue->str(); break; case ValueType::FLOAT: - ss << this->floatValue; + ss << MathLib::toString(this->floatValue); break; case ValueType::MOVED: ss << toString(this->moveKind); diff --git a/test/cli/other_test.py b/test/cli/other_test.py index 1bbf11ba2ab..6d54c3a8724 100644 --- a/test/cli/other_test.py +++ b/test/cli/other_test.py @@ -3072,12 +3072,12 @@ def test_debug_valueflow(tmp_path): '##Value flow', 'File {}'.format(str(test_file).replace('\\', '/')), 'Line 3', - ' = always 2', - ' 1.0 always 1', - ' / always 2', + ' = always 2.0', + ' 1.0 always 1.0', + ' / always 2.0', ' 0.5 always 0.5', 'Line 4', - ' d always {symbolic=(1.0/0.5),2}' + ' d always {symbolic=(1.0/0.5),2.0}' ] diff --git a/test/testrunner.vcxproj b/test/testrunner.vcxproj index 02054bbbb2d..144a324163e 100755 --- a/test/testrunner.vcxproj +++ b/test/testrunner.vcxproj @@ -111,6 +111,7 @@ + diff --git a/test/testvfvalue.cpp b/test/testvfvalue.cpp new file mode 100644 index 00000000000..5326c476ec5 --- /dev/null +++ b/test/testvfvalue.cpp @@ -0,0 +1,48 @@ +/* + * 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 "fixture.h" +#include "vfvalue.h" + +class TestValueFlowValue : public TestFixture { +public: + TestValueFlowValue() : TestFixture("TestValueFlowValue") {} + +private: + void run() override { + TEST_CASE(toString); + } + + void toString() const { + { + ValueFlow::Value v; + ASSERT_EQUALS("0", v.toString()); + v.intvalue = -1; + ASSERT_EQUALS("-1", v.toString()); + } + { + ValueFlow::Value v; + v.valueType = ValueFlow::Value::ValueType::FLOAT; + ASSERT_EQUALS("0.0", v.toString()); + v.floatValue = 0.0000000000001; + ASSERT_EQUALS("1e-13", v.toString()); + } + } +}; + +REGISTER_TEST(TestValueFlowValue)