Skip to content

Commit 17a10d0

Browse files
authored
fixed #13246/#13247 - unused function check did not store file location in build dir (#6937)
1 parent 32d0c22 commit 17a10d0

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

Diff for: lib/checkunusedfunctions.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,15 @@ void CheckUnusedFunctions::unusedFunctionError(ErrorLogger& errorLogger,
387387
}
388388

389389
CheckUnusedFunctions::FunctionDecl::FunctionDecl(const Function *f)
390-
: functionName(f->name()), lineNumber(f->token->linenr())
390+
: functionName(f->name()), fileName(f->token->fileName()), lineNumber(f->token->linenr())
391391
{}
392392

393393
std::string CheckUnusedFunctions::analyzerInfo() const
394394
{
395395
std::ostringstream ret;
396396
for (const FunctionDecl &functionDecl : mFunctionDecl) {
397397
ret << " <functiondecl"
398+
<< " file=\"" << ErrorLogger::toxml(functionDecl.fileName) << '\"'
398399
<< " functionName=\"" << ErrorLogger::toxml(functionDecl.functionName) << '\"'
399400
<< " lineNumber=\"" << functionDecl.lineNumber << "\"/>\n";
400401
}
@@ -458,8 +459,9 @@ void CheckUnusedFunctions::analyseWholeProgram(const Settings &settings, ErrorLo
458459
if (std::strcmp(name,"functiondecl") == 0) {
459460
const char* lineNumber = e2->Attribute("lineNumber");
460461
if (lineNumber) {
462+
const char* file = e2->Attribute("file");
461463
// cppcheck-suppress templateInstantiation - TODO: fix this - see #11631
462-
decls[functionName] = Location(sourcefile, strToInt<int>(lineNumber));
464+
decls[functionName] = Location(file ? file : sourcefile, strToInt<int>(lineNumber));
463465
}
464466
}
465467
}

Diff for: lib/checkunusedfunctions.h

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class CPPCHECKLIB CheckUnusedFunctions {
8484
public:
8585
explicit FunctionDecl(const Function *f);
8686
std::string functionName;
87+
std::string fileName;
8788
unsigned int lineNumber;
8889
};
8990
std::list<FunctionDecl> mFunctionDecl;

Diff for: lib/token.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -2662,3 +2662,7 @@ bool Token::isC() const
26622662
{
26632663
return mTokensFrontBack.list.isC();
26642664
}
2665+
2666+
const std::string& Token::fileName() const {
2667+
return mTokensFrontBack.list.getFiles()[mImpl->mFileIndex];
2668+
}

Diff for: lib/token.h

+2
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,8 @@ class CPPCHECKLIB Token {
873873
static int multiCompare(const Token *tok, const char *haystack, nonneg int varid);
874874

875875
public:
876+
const std::string& fileName() const;
877+
876878
nonneg int fileIndex() const {
877879
return mImpl->mFileIndex;
878880
}

Diff for: test/cli/other_test.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ def test_addon_result(tmpdir):
784784

785785
# TODO: test with -j2
786786
# #11483
787-
def test_unused_function_include(tmpdir):
787+
def __test_unused_function_include(tmpdir, extra_args):
788788
test_cpp_file = os.path.join(tmpdir, 'test.cpp')
789789
with open(test_cpp_file, 'wt') as f:
790790
f.write("""
@@ -802,12 +802,31 @@ class A {
802802
};
803803
""")
804804

805-
args = ['--enable=unusedFunction', '--inline-suppr', '--template=simple', '-j1', test_cpp_file]
805+
args = [
806+
'--enable=unusedFunction',
807+
'--inline-suppr',
808+
'--template=simple',
809+
'-j1',
810+
test_cpp_file
811+
]
812+
813+
args += extra_args
806814

807815
_, _, stderr = cppcheck(args)
808816
assert stderr == "{}:4:0: style: The function 'f' is never used. [unusedFunction]\n".format(test_h_file)
809817

810818

819+
def test_unused_function_include(tmpdir):
820+
__test_unused_function_include(tmpdir, [])
821+
822+
823+
# TODO: remove when we inject builddir
824+
def test_unused_function_include_builddir(tmpdir):
825+
builddir = os.path.join(tmpdir, 'injected')
826+
os.makedirs(builddir)
827+
__test_unused_function_include(tmpdir, ['--cppcheck-build-dir={}'.format(builddir)])
828+
829+
811830
# TODO: test with all other types
812831
def test_showtime_top5_file(tmpdir):
813832
test_file = os.path.join(tmpdir, 'test.cpp')

0 commit comments

Comments
 (0)