Skip to content

Commit

Permalink
fixed #10760 - added file name to ValueFlow --debug output (#5655)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Nov 16, 2023
1 parent 8b69cca commit 682901a
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 += " <valueflow>\n";
Expand All @@ -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;
Expand Down
87 changes: 87 additions & 0 deletions test/cli/test-other.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# python -m pytest test-other.py

import os
import sys
import pytest

from testutils import cppcheck, assert_cppcheck
Expand Down Expand Up @@ -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 == ''

0 comments on commit 682901a

Please sign in to comment.