Skip to content

Commit

Permalink
unused function check did not store file location in build dir
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Oct 20, 2024
1 parent b7ad05b commit d99efad
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/checkunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,15 @@ void CheckUnusedFunctions::unusedFunctionError(ErrorLogger& errorLogger,
}

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

std::string CheckUnusedFunctions::analyzerInfo() const
{
std::ostringstream ret;
for (const FunctionDecl &functionDecl : mFunctionDecl) {
ret << " <functiondecl"
<< " file=\"" << ErrorLogger::toxml(functionDecl.fileName) << '\"'
<< " functionName=\"" << ErrorLogger::toxml(functionDecl.functionName) << '\"'
<< " lineNumber=\"" << functionDecl.lineNumber << "\"/>\n";
}
Expand Down Expand Up @@ -456,10 +457,11 @@ void CheckUnusedFunctions::analyseWholeProgram(const Settings &settings, ErrorLo
continue;
}
if (std::strcmp(name,"functiondecl") == 0) {
const char* file = e2->Attribute("file");
const char* lineNumber = e2->Attribute("lineNumber");
if (lineNumber) {
// cppcheck-suppress templateInstantiation - TODO: fix this - see #11631
decls[functionName] = Location(sourcefile, strToInt<int>(lineNumber));
decls[functionName] = Location(file ? file : sourcefile, strToInt<int>(lineNumber));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/checkunusedfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class CPPCHECKLIB CheckUnusedFunctions {
public:
explicit FunctionDecl(const Function *f);
std::string functionName;
std::string fileName;
unsigned int lineNumber;
};
std::list<FunctionDecl> mFunctionDecl;
Expand Down
4 changes: 4 additions & 0 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2662,3 +2662,7 @@ bool Token::isC() const
{
return mTokensFrontBack.list.isC();
}

const std::string& Token::fileName() const {
return mTokensFrontBack.list.getFiles()[mImpl->mFileIndex];
}
2 changes: 2 additions & 0 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,8 @@ class CPPCHECKLIB Token {
static int multiCompare(const Token *tok, const char *haystack, nonneg int varid);

public:
const std::string& fileName() const;

nonneg int fileIndex() const {
return mImpl->mFileIndex;
}
Expand Down
23 changes: 21 additions & 2 deletions test/cli/other_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ def test_addon_result(tmpdir):

# TODO: test with -j2
# #11483
def test_unused_function_include(tmpdir):
def __test_unused_function_include(tmpdir, extra_args):
test_cpp_file = os.path.join(tmpdir, 'test.cpp')
with open(test_cpp_file, 'wt') as f:
f.write("""
Expand All @@ -802,12 +802,31 @@ class A {
};
""")

args = ['--enable=unusedFunction', '--inline-suppr', '--template=simple', '-j1', test_cpp_file]
args = [
'--enable=unusedFunction',
'--inline-suppr',
'--template=simple',
'-j1',
test_cpp_file
]

args += extra_args

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


def test_unused_function_include(tmpdir):
__test_unused_function_include(tmpdir, [])


# TODO: remove when we inject builddir
def test_unused_function_include_builddir(tmpdir):
builddir = os.path.join(tmpdir, 'injected')
os.makedirs(builddir)
__test_unused_function_include(tmpdir, ['--cppcheck-build-dir={}'.format(builddir)])


# TODO: test with all other types
def test_showtime_top5_file(tmpdir):
test_file = os.path.join(tmpdir, 'test.cpp')
Expand Down

0 comments on commit d99efad

Please sign in to comment.