fixed #10760 - added file name to ValueFlow `--debug` output (#5655)

This commit is contained in:
Oliver Stöneberg 2023-11-16 13:44:50 +01:00 committed by GitHub
parent 8b69ccadd4
commit 682901ac2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 4 deletions

View File

@ -1701,6 +1701,7 @@ void Token::printValueFlow(bool xml, std::ostream &out) const
{ {
std::string outs; std::string outs;
int fileIndex = -1;
int line = 0; int line = 0;
if (xml) if (xml)
outs += " <valueflow>\n"; outs += " <valueflow>\n";
@ -1718,11 +1719,20 @@ void Token::printValueFlow(bool xml, std::ostream &out) const
outs += "\">"; outs += "\">";
outs += '\n'; outs += '\n';
} }
else if (line != tok->linenr()) { 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 += "Line ";
outs += std::to_string(tok->linenr()); outs += std::to_string(tok->linenr());
outs += '\n'; outs += '\n';
} }
}
fileIndex = tok->fileIndex();
line = tok->linenr(); line = tok->linenr();
if (!xml) { if (!xml) {
ValueFlow::Value::ValueKind valueKind = values->front().valueKind; ValueFlow::Value::ValueKind valueKind = values->front().valueKind;

View File

@ -2,6 +2,7 @@
# python -m pytest test-other.py # python -m pytest test-other.py
import os import os
import sys
import pytest import pytest
from testutils import cppcheck, assert_cppcheck from testutils import cppcheck, assert_cppcheck
@ -724,3 +725,89 @@ def test_markup_j(tmpdir):
'Checking {} ...'.format(test_file_3) 'Checking {} ...'.format(test_file_3)
] ]
assert stderr == '' 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 == ''