Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/checkunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer)
! Token::simpleMatch(tok2, ") const {") &&
! Token::simpleMatch(tok2, ") const throw ( ) {") &&
! Token::simpleMatch(tok2, ") throw ( ) {"))
funcname = NULL;
funcname = 0;
break;
}
}
Expand All @@ -83,10 +83,14 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer)
{
FunctionUsage &func = _functions[ funcname->str()];

if(!func.lineNumber)
func.lineNumber = funcname->linenr();

// No filename set yet..
if (func.filename.empty())
{
func.filename = tokenizer.getFiles()->at(0);

}
// Multiple files => filename = "+"
else if (func.filename != tokenizer.getFiles()->at(0))
{
Expand Down Expand Up @@ -141,7 +145,6 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer)

if (func.filename.empty() || func.filename == "+")
func.usedOtherFile = true;

else
func.usedSameFile = true;
}
Expand All @@ -167,7 +170,7 @@ void CheckUnusedFunctions::check(ErrorLogger * const errorLogger)
filename = "";
else
filename = func.filename;
unusedFunctionError(errorLogger, filename, it->first);
unusedFunctionError(errorLogger, filename, func.lineNumber, it->first);
}
else if (! func.usedOtherFile)
{
Expand All @@ -181,14 +184,16 @@ void CheckUnusedFunctions::check(ErrorLogger * const errorLogger)
}
}

void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger, const std::string &filename, const std::string &funcname)
void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger,
const std::string &filename, unsigned int lineNumber,
const std::string &funcname)
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
if (!filename.empty())
{
ErrorLogger::ErrorMessage::FileLocation fileLoc;
fileLoc.setfile(filename);
fileLoc.line = 1;
fileLoc.line = lineNumber;
locationList.push_back(fileLoc);
}

Expand Down
9 changes: 6 additions & 3 deletions lib/checkunusedfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ class CheckUnusedFunctions: public Check
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
CheckUnusedFunctions c(0, settings, errorLogger);
c.unusedFunctionError(errorLogger, "", "funcName");
c.unusedFunctionError(errorLogger, "", 0, "funcName");
}

/**
* Dummy implementation, just to provide error for --errorlist
*/
void unusedFunctionError(ErrorLogger * const errorLogger, const std::string &filename, const std::string &funcname);
void unusedFunctionError(ErrorLogger * const errorLogger,
const std::string &filename, unsigned int lineNumber,
const std::string &funcname);

/**
* Dummy implementation, just to provide error for --errorlist
Expand All @@ -82,10 +84,11 @@ class CheckUnusedFunctions: public Check
class FunctionUsage
{
public:
FunctionUsage() : usedSameFile(false), usedOtherFile(false)
FunctionUsage() : lineNumber(0), usedSameFile(false), usedOtherFile(false)
{ }

std::string filename;
unsigned int lineNumber;
bool usedSameFile;
bool usedOtherFile;
};
Expand Down
11 changes: 11 additions & 0 deletions test/testunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class TestUnusedFunctions : public TestFixture
TEST_CASE(initializationIsNotAFunction);

TEST_CASE(multipleFiles); // same function name in multiple files

TEST_CASE(lineNumber); // Ticket 3059
}

void check(const char code[])
Expand Down Expand Up @@ -216,6 +218,15 @@ class TestUnusedFunctions : public TestFixture

ASSERT_EQUALS("[test1.cpp:1]: (style) The function 'f' is never used\n",errout.str());
}

void lineNumber()
{
check("void foo() {}\n"
"void bar() {}\n"
"int main()\n");
ASSERT_EQUALS("[test.cpp:2]: (style) The function 'bar' is never used\n"
"[test.cpp:1]: (style) The function 'foo' is never used\n", errout.str());
}
};

REGISTER_TEST(TestUnusedFunctions)
Expand Down