Skip to content
Closed
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
1 change: 1 addition & 0 deletions cli/cmdlineparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class CmdLineParser {
/**
* Return if help is shown to user.
*/
// cppcheck-suppress unusedFunction - only used in unit tests
bool getShowHelp() const {
return mShowHelp;
}
Expand Down
10 changes: 0 additions & 10 deletions externals/tinyxml2/tinyxml2.h
Original file line number Diff line number Diff line change
Expand Up @@ -1141,16 +1141,6 @@ class TINYXML2_LIB XMLAttribute
return _next;
}

/** IntValue interprets the attribute as an integer, and returns the value.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not change external files. Put it in the suppression file.

If the value isn't an integer, 0 will be returned. There is no error checking;
use QueryIntValue() if you need error checking.
*/
int IntValue() const {
int i = 0;
QueryIntValue(&i);
return i;
}

int64_t Int64Value() const {
int64_t i = 0;
QueryInt64Value(&i);
Expand Down
20 changes: 12 additions & 8 deletions lib/checkunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
// Function declarations..
for (const Scope* scope : symbolDatabase->functionScopes) {
const Function* func = scope->function;
if (!func || !func->token || scope->bodyStart->fileIndex() != 0)
if (!func || !func->token)
continue;

// Don't warn about functions that are marked by __attribute__((constructor)) or __attribute__((destructor))
Expand All @@ -93,12 +93,15 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
if (!usage.lineNumber)
usage.lineNumber = func->token->linenr();

usage.fileIndex = func->token->fileIndex();
const std::string& fileName = tokenizer.list.file(func->token);

// No filename set yet..
if (usage.filename.empty()) {
usage.filename = tokenizer.list.getSourceFilePath();
usage.filename = fileName;
}
// Multiple files => filename = "+"
else if (usage.filename != tokenizer.list.getSourceFilePath()) {
else if (usage.filename != fileName) {
//func.filename = "+";
usage.usedOtherFile |= usage.usedSameFile;
}
Expand Down Expand Up @@ -308,7 +311,7 @@ static bool isOperatorFunction(const std::string & funcName)

bool CheckUnusedFunctions::check(ErrorLogger * const errorLogger, const Settings& settings) const
{
using ErrorParams = std::tuple<std::string, unsigned int, std::string>;
using ErrorParams = std::tuple<std::string, unsigned int, unsigned int, std::string>;
std::vector<ErrorParams> errors; // ensure well-defined order

for (std::unordered_map<std::string, FunctionUsage>::const_iterator it = mFunctions.cbegin(); it != mFunctions.cend(); ++it) {
Expand All @@ -323,7 +326,7 @@ bool CheckUnusedFunctions::check(ErrorLogger * const errorLogger, const Settings
std::string filename;
if (func.filename != "+")
filename = func.filename;
errors.emplace_back(filename, func.lineNumber, it->first);
errors.emplace_back(filename, func.fileIndex, func.lineNumber, it->first);
} else if (!func.usedOtherFile) {
/** @todo add error message "function is only used in <file> it can be static" */
/*
Expand All @@ -336,17 +339,18 @@ bool CheckUnusedFunctions::check(ErrorLogger * const errorLogger, const Settings
}
std::sort(errors.begin(), errors.end());
for (const auto& e : errors)
unusedFunctionError(errorLogger, std::get<0>(e), std::get<1>(e), std::get<2>(e));
unusedFunctionError(errorLogger, std::get<0>(e), std::get<1>(e), std::get<2>(e), std::get<3>(e));
return !errors.empty();
}

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

const ErrorMessage errmsg(locationList, emptyString, Severity::style, "$symbol:" + funcname + "\nThe function '$symbol' is never used.", "unusedFunction", CWE561, Certainty::normal);
Expand Down Expand Up @@ -456,7 +460,7 @@ void CheckUnusedFunctions::analyseWholeProgram(const Settings &settings, ErrorLo

if (calls.find(functionName) == calls.end() && !isOperatorFunction(functionName)) {
const Location &loc = decl->second;
unusedFunctionError(errorLogger, loc.fileName, loc.lineNumber, functionName);
unusedFunctionError(errorLogger, loc.fileName, /*fileIndex*/ 0, loc.lineNumber, functionName);
}
}
}
5 changes: 3 additions & 2 deletions lib/checkunusedfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class CPPCHECKLIB CheckUnusedFunctions : public Check {
private:

void getErrorMessages(ErrorLogger *errorLogger, const Settings * /*settings*/) const override {
CheckUnusedFunctions::unusedFunctionError(errorLogger, emptyString, 0, "funcName");
CheckUnusedFunctions::unusedFunctionError(errorLogger, emptyString, 0, 0, "funcName");
}

void runChecks(const Tokenizer * /*tokenizer*/, const Settings * /*settings*/, ErrorLogger * /*errorLogger*/) override {}
Expand All @@ -90,7 +90,7 @@ class CPPCHECKLIB CheckUnusedFunctions : public Check {
* Dummy implementation, just to provide error for --errorlist
*/
static void unusedFunctionError(ErrorLogger * const errorLogger,
const std::string &filename, unsigned int lineNumber,
const std::string &filename, unsigned int fileIndex, unsigned int lineNumber,
const std::string &funcname);

static std::string myName() {
Expand All @@ -107,6 +107,7 @@ class CPPCHECKLIB CheckUnusedFunctions : public Check {

std::string filename;
unsigned int lineNumber;
unsigned int fileIndex{};
bool usedSameFile;
bool usedOtherFile;
};
Expand Down
4 changes: 0 additions & 4 deletions lib/cppcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
void tooManyConfigsError(const std::string &file, const int numberOfConfigurations);
void purgedConfigurationMessage(const std::string &file, const std::string& configuration);

void dontSimplify() {
mSimplify = false;
}

/** Analyse whole program, run this after all TUs has been scanned.
* This is deprecated and the plan is to remove this when
* .analyzeinfo is good enough.
Expand Down
5 changes: 5 additions & 0 deletions lib/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class CPPCHECKLIB Library {
}

/** get allocation id for function by name (deprecated, use other alloc) */
// cppcheck-suppress unusedFunction - only used in unit tests

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would drop the mention of unit tests for obvious helper functions like this. Same for the other methods in this file.

I will review this in more detail later on though.

int allocId(const char name[]) const {
const AllocFunc* af = getAllocDealloc(mAlloc, name);
return af ? af->groupId : 0;
Expand All @@ -130,23 +131,27 @@ class CPPCHECKLIB Library {
}

/** set allocation id for function */
// cppcheck-suppress unusedFunction - only used in unit tests
void setalloc(const std::string &functionname, int id, int arg) {
mAlloc[functionname].groupId = id;
mAlloc[functionname].arg = arg;
}

// cppcheck-suppress unusedFunction - only used in unit tests
void setdealloc(const std::string &functionname, int id, int arg) {
mDealloc[functionname].groupId = id;
mDealloc[functionname].arg = arg;
}

// cppcheck-suppress unusedFunction - only used in unit tests
void setrealloc(const std::string &functionname, int id, int arg, int reallocArg = 1) {
mRealloc[functionname].groupId = id;
mRealloc[functionname].arg = arg;
mRealloc[functionname].reallocArg = reallocArg;
}

/** add noreturn function setting */
// cppcheck-suppress unusedFunction - only used in unit tests
void setnoreturn(const std::string& funcname, bool noreturn) {
mNoReturn[funcname] = noreturn ? FalseTrueMaybe::True : FalseTrueMaybe::False;
}
Expand Down
4 changes: 0 additions & 4 deletions lib/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,6 @@ class CPPCHECKLIB Preprocessor {

static void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings);

void setFile0(const std::string &f) {
mFile0 = f;
}

/**
* dump all directives present in source file
*/
Expand Down
6 changes: 0 additions & 6 deletions lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ class SimpleEnableGroup {
void fill() {
mFlags = 0xFFFFFFFF;
}
void setEnabledAll(bool enabled) {
if (enabled)
fill();
else
clear();
}
bool isEnabled(T flag) const {
return (mFlags & (1U << (uint32_t)flag)) != 0;
}
Expand Down
4 changes: 0 additions & 4 deletions lib/symboldatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1409,10 +1409,6 @@ class CPPCHECKLIB SymbolDatabase {
return const_cast<Scope *>(this->findScope(tok, const_cast<const Scope *>(startScope)));
}

bool isVarId(nonneg int varid) const {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep this for now.

return varid < mVariableList.size();
}

const Variable *getVariableFromVarId(nonneg int varId) const {
return mVariableList.at(varId);
}
Expand Down
5 changes: 1 addition & 4 deletions lib/templatesimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3679,9 +3679,7 @@ void TemplateSimplifier::printOut(const std::string & text) const
}
}

void TemplateSimplifier::simplifyTemplates(
const std::time_t maxtime,
bool &codeWithTemplates)
void TemplateSimplifier::simplifyTemplates(const std::time_t maxtime)
{
// convert "sizeof ..." to "sizeof..."
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
Expand Down Expand Up @@ -3754,7 +3752,6 @@ void TemplateSimplifier::simplifyTemplates(
const bool hasTemplates = getTemplateDeclarations();

if (passCount == 0) {
codeWithTemplates = hasTemplates;
mDump.clear();
for (const TokenAndName& t: mTemplateDeclarations)
mDump += t.dump(mTokenizer.list.getFiles());
Expand Down
4 changes: 1 addition & 3 deletions lib/templatesimplifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,9 @@ class CPPCHECKLIB TemplateSimplifier {
/**
* Simplify templates
* @param maxtime time when the simplification should be stopped
* @param codeWithTemplates output parameter that is set if code contains templates
*/
void simplifyTemplates(
const std::time_t maxtime,
bool &codeWithTemplates);
const std::time_t maxtime);

/**
* Simplify constant calculations such as "1+2" => "3"
Expand Down
15 changes: 2 additions & 13 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,6 @@ class CPPCHECKLIB Token {
void isSigned(const bool sign) {
setFlag(fIsSigned, sign);
}
bool isPointerCompare() const {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep this even if it isn't used. Same for the other methods in this file.

return getFlag(fIsPointerCompare);
}
void isPointerCompare(const bool b) {
setFlag(fIsPointerCompare, b);
}
bool isLong() const {
return getFlag(fIsLong);
}
Expand Down Expand Up @@ -562,9 +556,6 @@ class CPPCHECKLIB Token {
bool getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint &value) const {
return mImpl->getCppcheckAttribute(type, value);
}
bool hasCppcheckAttributes() const {
return nullptr != mImpl->mCppcheckAttributes;
}
bool isControlFlowKeyword() const {
return getFlag(fIsControlFlowKeyword);
}
Expand Down Expand Up @@ -690,9 +681,6 @@ class CPPCHECKLIB Token {
setFlag(fIsFinalType, b);
}

bool isBitfield() const {
return mImpl->mBits > 0;
}
unsigned char bits() const {
return mImpl->mBits;
}
Expand Down Expand Up @@ -936,6 +924,7 @@ class CPPCHECKLIB Token {
options.files = true;
return options;
}
// cppcheck-suppress unusedFunction - only used in unit tests
static stringifyOptions forDebugVarId() {
stringifyOptions options = forDebug();
options.varid = true;
Expand Down Expand Up @@ -1267,7 +1256,7 @@ class CPPCHECKLIB Token {
enum : uint64_t {
fIsUnsigned = (1 << 0),
fIsSigned = (1 << 1),
fIsPointerCompare = (1 << 2),

fIsLong = (1 << 3),
fIsStandardType = (1 << 4),
fIsExpandedMacro = (1 << 5),
Expand Down
5 changes: 1 addition & 4 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger, const P
mTemplateSimplifier(new TemplateSimplifier(*this)),
mVarId(0),
mUnnamedCount(0),
mCodeWithTemplates(false), //is there any templates?
mTimerResults(nullptr),
mPreprocessor(preprocessor)
{
Expand Down Expand Up @@ -3939,9 +3938,7 @@ void Tokenizer::simplifyTemplates()
return;

const std::time_t maxTime = mSettings->templateMaxTime > 0 ? std::time(nullptr) + mSettings->templateMaxTime : 0;
mTemplateSimplifier->simplifyTemplates(
maxTime,
mCodeWithTemplates);
mTemplateSimplifier->simplifyTemplates(maxTime);
}
//---------------------------------------------------------------------------

Expand Down
11 changes: 0 additions & 11 deletions lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,6 @@ class CPPCHECKLIB Tokenizer {

public:

/** Was there templates in the code? */
bool codeWithTemplates() const {
return mCodeWithTemplates;
}

const SymbolDatabase *getSymbolDatabase() const {
return mSymbolDatabase;
}
Expand Down Expand Up @@ -732,12 +727,6 @@ class CPPCHECKLIB Tokenizer {
/** unnamed count "Unnamed0", "Unnamed1", "Unnamed2", ... */
nonneg int mUnnamedCount;

/**
* was there any templates? templates that are "unused" are
* removed from the token list
*/
bool mCodeWithTemplates;

/**
* TimerResults
*/
Expand Down