From 682901ac2a759b03db9ef8b9b1e6f3d4da6c24f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Thu, 16 Nov 2023 13:44:50 +0100 Subject: [PATCH] fixed #10760 - added file name to ValueFlow `--debug` output (#5655) --- lib/token.cpp | 18 +++++++-- test/cli/test-other.py | 87 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 4 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index fafaba9ce..0b548aa14 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1701,6 +1701,7 @@ void Token::printValueFlow(bool xml, std::ostream &out) const { std::string outs; + int fileIndex = -1; int line = 0; if (xml) outs += " \n"; @@ -1718,11 +1719,20 @@ void Token::printValueFlow(bool xml, std::ostream &out) const outs += "\">"; outs += '\n'; } - else if (line != tok->linenr()) { - outs += "Line "; - outs += std::to_string(tok->linenr()); - outs += '\n'; + else { + if (fileIndex != tok->fileIndex()) { + outs += "File "; + outs += tok->mTokensFrontBack->list->getFiles()[tok->fileIndex()]; + outs += '\n'; + line = 0; + } + if (line != tok->linenr()) { + outs += "Line "; + outs += std::to_string(tok->linenr()); + outs += '\n'; + } } + fileIndex = tok->fileIndex(); line = tok->linenr(); if (!xml) { ValueFlow::Value::ValueKind valueKind = values->front().valueKind; diff --git a/test/cli/test-other.py b/test/cli/test-other.py index 002d4c528..95aaaa654 100644 --- a/test/cli/test-other.py +++ b/test/cli/test-other.py @@ -2,6 +2,7 @@ # python -m pytest test-other.py import os +import sys import pytest from testutils import cppcheck, assert_cppcheck @@ -724,3 +725,89 @@ def test_markup_j(tmpdir): 'Checking {} ...'.format(test_file_3) ] assert stderr == '' + + +def test_valueflow_debug(tmpdir): + test_file_cpp = os.path.join(tmpdir, 'test_1.cpp') + with open(test_file_cpp, 'wt') as f: + f.write(""" +#include "test.h" + +void f() +{ + int i = 0; +} +""" + ) + test_file_h = os.path.join(tmpdir, 'test.h') + with open(test_file_h, 'wt') as f: + f.write(""" +#include "test2.h" +inline void f1() +{ + int i = 0; +} +""" + ) + pass + test_file_h_2 = os.path.join(tmpdir, 'test2.h') + with open(test_file_h_2, 'wt') as f: + f.write(""" +inline void f2() +{ + int i = 0; +} +""" + ) + + args = ['--debug', test_file_cpp] + + exitcode, stdout, stderr = cppcheck(args) + assert exitcode == 0 + if sys.platform == "win32": + stdout = stdout.replace('/', '\\') + assert stdout == '''Checking {} ... + + +##file {} +2: void f2 ( ) +3: {{ +4: int i@var1 ; i@var1 =@expr1073741828 0 ; +5: }} + +##file {} + +1: +2: +3: void f1 ( ) +4: {{ +5: int i@var2 ; i@var2 =@expr1073741829 0 ; +6: }} + +##file {} + +1: +2: +3: +4: void f ( ) +5: {{ +6: int i@var3 ; i@var3 =@expr1073741830 0 ; +7: }} + + + +##Value flow +File {} +Line 4 + = always 0 + 0 always 0 +File {} +Line 5 + = always 0 + 0 always 0 +File {} +Line 6 + = always 0 + 0 always 0 +'''.format(test_file_cpp, test_file_h_2, test_file_h, test_file_cpp, test_file_h_2, test_file_h, test_file_cpp) + assert stderr == ''