Skip to content

Commit 98ac18e

Browse files
authored
pass Settings by reference in checks (#8593)
1 parent f1379d6 commit 98ac18e

63 files changed

Lines changed: 791 additions & 791 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/check.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class CPPCHECKLIB Check {
6565
virtual void runChecks(const Tokenizer &, ErrorLogger *) = 0;
6666

6767
/** get error messages */
68-
virtual void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const = 0;
68+
virtual void getErrorMessages(ErrorLogger *errorLogger, const Settings &settings) const = 0;
6969

7070
/** class name, used to generate documentation */
7171
const std::string& name() const {

lib/check64bit.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636
// CWE ids used
3737
static const CWE CWE758(758U); // Reliance on Undefined, Unspecified, or Implementation-Defined Behavior
3838

39-
static bool is32BitIntegerReturn(const Function* func, const Settings* settings)
39+
static bool is32BitIntegerReturn(const Function* func, const Settings& settings)
4040
{
41-
if (settings->platform.sizeof_pointer != 8)
41+
if (settings.platform.sizeof_pointer != 8)
4242
return false;
4343
const ValueType* vt = func->arg->valueType();
44-
return vt && vt->pointer == 0 && vt->isIntegral() && vt->getSizeOf(*settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer) == 4;
44+
return vt && vt->pointer == 0 && vt->isIntegral() && vt->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer) == 4;
4545
}
4646

4747
static bool isFunctionPointer(const Token* tok)
@@ -53,7 +53,7 @@ static bool isFunctionPointer(const Token* tok)
5353

5454
void Check64BitPortabilityImpl::pointerassignment()
5555
{
56-
if (!mSettings->severity.isEnabled(Severity::portability))
56+
if (!mSettings.severity.isEnabled(Severity::portability))
5757
return;
5858

5959
logChecker("Check64BitPortability::pointerassignment"); // portability
@@ -185,11 +185,11 @@ void Check64BitPortabilityImpl::returnIntegerError(const Token *tok)
185185

186186
void Check64BitPortability::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
187187
{
188-
Check64BitPortabilityImpl check64BitPortability(&tokenizer, &tokenizer.getSettings(), errorLogger);
188+
Check64BitPortabilityImpl check64BitPortability(&tokenizer, tokenizer.getSettings(), errorLogger);
189189
check64BitPortability.pointerassignment();
190190
}
191191

192-
void Check64BitPortability::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const
192+
void Check64BitPortability::getErrorMessages(ErrorLogger *errorLogger, const Settings &settings) const
193193
{
194194
Check64BitPortabilityImpl c(nullptr, settings, errorLogger);
195195
c.assignmentAddressToIntegerError(nullptr);

lib/check64bit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class CPPCHECKLIB Check64BitPortability : public Check {
5151
/** @brief Run checks against the normal token list */
5252
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override;
5353

54-
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override;
54+
void getErrorMessages(ErrorLogger *errorLogger, const Settings &settings) const override;
5555

5656
std::string classInfo() const override {
5757
return "Check if there is 64-bit portability issues:\n"
@@ -63,7 +63,7 @@ class CPPCHECKLIB Check64BitPortability : public Check {
6363
class CPPCHECKLIB Check64BitPortabilityImpl : public CheckImpl {
6464
public:
6565
/** This constructor is used when running checks. */
66-
Check64BitPortabilityImpl(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
66+
Check64BitPortabilityImpl(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger)
6767
: CheckImpl(tokenizer, settings, errorLogger) {}
6868

6969
/** Check for pointer assignment */

lib/checkassert.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static const CWE CWE398(398U); // Indicator of Poor Code Quality
4141

4242
void CheckAssertImpl::assertWithSideEffects()
4343
{
44-
if (!mSettings->severity.isEnabled(Severity::warning))
44+
if (!mSettings.severity.isEnabled(Severity::warning))
4545
return;
4646

4747
logChecker("CheckAssert::assertWithSideEffects"); // warning
@@ -58,7 +58,7 @@ void CheckAssertImpl::assertWithSideEffects()
5858
checkVariableAssignment(tmp, tok->scope());
5959

6060
if (tmp->tokType() != Token::eFunction) {
61-
if (const Library::Function* f = mSettings->library.getFunction(tmp)) {
61+
if (const Library::Function* f = mSettings.library.getFunction(tmp)) {
6262
if (f->isconst || f->ispure)
6363
continue;
6464
if (Library::getContainerYield(tmp->next()) != Library::Container::Yield::NO_YIELD) // bailout, assume read access
@@ -73,7 +73,7 @@ void CheckAssertImpl::assertWithSideEffects()
7373
f->containerYield == Library::Container::Yield::END_ITERATOR ||
7474
f->containerYield == Library::Container::Yield::ITERATOR)
7575
continue;
76-
sideEffectInAssertError(tmp, mSettings->library.getFunctionName(tmp));
76+
sideEffectInAssertError(tmp, mSettings.library.getFunctionName(tmp));
7777
}
7878
continue;
7979
}
@@ -180,11 +180,11 @@ bool CheckAssertImpl::inSameScope(const Token* returnTok, const Token* assignTok
180180

181181
void CheckAssert::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
182182
{
183-
CheckAssertImpl checkAssert(&tokenizer, &tokenizer.getSettings(), errorLogger);
183+
CheckAssertImpl checkAssert(&tokenizer, tokenizer.getSettings(), errorLogger);
184184
checkAssert.assertWithSideEffects();
185185
}
186186

187-
void CheckAssert::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const
187+
void CheckAssert::getErrorMessages(ErrorLogger *errorLogger, const Settings &settings) const
188188
{
189189
CheckAssertImpl c(nullptr, settings, errorLogger);
190190
c.sideEffectInAssertError(nullptr, "function");

lib/checkassert.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class CPPCHECKLIB CheckAssert : public Check {
4848
private:
4949
/** run checks, the token list is not simplified */
5050
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override;
51-
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override;
51+
void getErrorMessages(ErrorLogger *errorLogger, const Settings &settings) const override;
5252

5353
std::string classInfo() const override {
5454
return "Warn if there are side effects in assert statements (since this cause different behaviour in debug/release builds).\n";
@@ -57,7 +57,7 @@ class CPPCHECKLIB CheckAssert : public Check {
5757

5858
class CPPCHECKLIB CheckAssertImpl : public CheckImpl {
5959
public:
60-
CheckAssertImpl(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
60+
CheckAssertImpl(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger)
6161
: CheckImpl(tokenizer, settings, errorLogger) {}
6262

6363
void assertWithSideEffects();

lib/checkautovariables.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ static bool variableIsUsedInScope(const Token* start, nonneg int varId, const Sc
204204

205205
void CheckAutoVariablesImpl::assignFunctionArg()
206206
{
207-
const bool printStyle = mSettings->severity.isEnabled(Severity::style);
208-
const bool printWarning = mSettings->severity.isEnabled(Severity::warning);
209-
if (!printStyle && !printWarning && !mSettings->isPremiumEnabled("uselessAssignmentPtrArg"))
207+
const bool printStyle = mSettings.severity.isEnabled(Severity::style);
208+
const bool printWarning = mSettings.severity.isEnabled(Severity::warning);
209+
if (!printStyle && !printWarning && !mSettings.isPremiumEnabled("uselessAssignmentPtrArg"))
210210
return;
211211

212212
logChecker("CheckAutoVariables::assignFunctionArg"); // style,warning
@@ -284,7 +284,7 @@ void CheckAutoVariablesImpl::autoVariables()
284284
{
285285
logChecker("CheckAutoVariables::autoVariables");
286286

287-
const bool printInconclusive = mSettings->certainty.isEnabled(Certainty::inconclusive);
287+
const bool printInconclusive = mSettings.certainty.isEnabled(Certainty::inconclusive);
288288
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
289289
for (const Scope * scope : symbolDatabase->functionScopes) {
290290
for (const Token *tok = scope->bodyStart; tok && tok != scope->bodyEnd; tok = tok->next()) {
@@ -295,27 +295,27 @@ void CheckAutoVariablesImpl::autoVariables()
295295
}
296296
// Critical assignment
297297
const Token* rhs{};
298-
if (Token::Match(tok, "[;{}] %var% =") && isRefPtrArg(tok->next()) && isAutoVariableRHS(tok->tokAt(2)->astOperand2(), *mSettings)) {
298+
if (Token::Match(tok, "[;{}] %var% =") && isRefPtrArg(tok->next()) && isAutoVariableRHS(tok->tokAt(2)->astOperand2(), mSettings)) {
299299
checkAutoVariableAssignment(tok->next(), false);
300-
} else if (Token::Match(tok, "[;{}] * %var% =") && isPtrArg(tok->tokAt(2)) && isAutoVariableRHS(tok->tokAt(3)->astOperand2(), *mSettings)) {
300+
} else if (Token::Match(tok, "[;{}] * %var% =") && isPtrArg(tok->tokAt(2)) && isAutoVariableRHS(tok->tokAt(3)->astOperand2(), mSettings)) {
301301
const Token* lhs = tok->tokAt(2);
302302
bool inconclusive = false;
303303
if (!hasOverloadedAssignment(lhs, inconclusive) || (printInconclusive && inconclusive))
304304
checkAutoVariableAssignment(tok->next(), inconclusive);
305305
tok = tok->tokAt(4);
306-
} else if (isMemberAssignment(tok, rhs, *mSettings)) {
306+
} else if (isMemberAssignment(tok, rhs, mSettings)) {
307307
const Token* lhs = tok->tokAt(3);
308308
bool inconclusive = false;
309309
if (!hasOverloadedAssignment(lhs, inconclusive) || (printInconclusive && inconclusive))
310310
checkAutoVariableAssignment(tok->next(), inconclusive);
311311
tok = rhs;
312312
} else if (Token::Match(tok, "[;{}] %var% [") && Token::simpleMatch(tok->linkAt(2), "] =") &&
313-
(isPtrArg(tok->next()) || isArrayArg(tok->next(), *mSettings)) &&
314-
isAutoVariableRHS(tok->linkAt(2)->next()->astOperand2(), *mSettings)) {
313+
(isPtrArg(tok->next()) || isArrayArg(tok->next(), mSettings)) &&
314+
isAutoVariableRHS(tok->linkAt(2)->next()->astOperand2(), mSettings)) {
315315
errorAutoVariableAssignment(tok->next(), false);
316316
}
317317
// Invalid pointer deallocation
318-
else if ((Token::Match(tok, "%name% ( %var%|%str% ) ;") && mSettings->library.getDeallocFuncInfo(tok)) ||
318+
else if ((Token::Match(tok, "%name% ( %var%|%str% ) ;") && mSettings.library.getDeallocFuncInfo(tok)) ||
319319
(tok->isCpp() && Token::Match(tok, "delete [| ]| (| %var%|%str% !!["))) {
320320
tok = Token::findmatch(tok->next(), "%var%|%str%");
321321
if (Token::simpleMatch(tok->astParent(), "."))
@@ -333,7 +333,7 @@ void CheckAutoVariablesImpl::autoVariables()
333333
}
334334
}
335335
}
336-
} else if ((Token::Match(tok, "%name% ( & %var% ) ;") && mSettings->library.getDeallocFuncInfo(tok)) ||
336+
} else if ((Token::Match(tok, "%name% ( & %var% ) ;") && mSettings.library.getDeallocFuncInfo(tok)) ||
337337
(tok->isCpp() && Token::Match(tok, "delete [| ]| (| & %var% !!["))) {
338338
tok = Token::findmatch(tok->next(), "%var%");
339339
if (isAutoVar(tok))
@@ -564,7 +564,7 @@ static bool isAssignedToNonLocal(const Token* tok)
564564

565565
void CheckAutoVariablesImpl::checkVarLifetimeScope(const Token * start, const Token * end)
566566
{
567-
const bool printInconclusive = mSettings->certainty.isEnabled(Certainty::inconclusive);
567+
const bool printInconclusive = mSettings.certainty.isEnabled(Certainty::inconclusive);
568568
if (!start)
569569
return;
570570
const Scope * scope = start->scope();
@@ -577,7 +577,7 @@ void CheckAutoVariablesImpl::checkVarLifetimeScope(const Token * start, const To
577577
for (const Token *tok = start; tok && tok != end; tok = tok->next()) {
578578
// Return reference from function
579579
if (returnRef && Token::simpleMatch(tok->astParent(), "return")) {
580-
for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(tok, *mSettings, true)) {
580+
for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(tok, mSettings, true)) {
581581
if (!printInconclusive && lt.inconclusive)
582582
continue;
583583
const Variable* var = lt.token->variable();
@@ -586,7 +586,7 @@ void CheckAutoVariablesImpl::checkVarLifetimeScope(const Token * start, const To
586586
errorReturnReference(tok, lt.errorPath, lt.inconclusive);
587587
break;
588588
}
589-
if (isDeadTemporary(lt.token, nullptr, mSettings->library)) {
589+
if (isDeadTemporary(lt.token, nullptr, mSettings.library)) {
590590
errorReturnTempReference(tok, lt.errorPath, lt.inconclusive);
591591
break;
592592
}
@@ -597,18 +597,18 @@ void CheckAutoVariablesImpl::checkVarLifetimeScope(const Token * start, const To
597597
tok->variable()->declarationId() == tok->varId() && tok->variable()->isStatic() &&
598598
!tok->variable()->isArgument()) {
599599
ErrorPath errorPath;
600-
const Variable *var = ValueFlow::getLifetimeVariable(tok, errorPath, *mSettings);
600+
const Variable *var = ValueFlow::getLifetimeVariable(tok, errorPath, mSettings);
601601
if (var && isInScope(var->nameToken(), tok->scope())) {
602602
errorDanglingReference(tok, var, std::move(errorPath));
603603
continue;
604604
}
605605
// Reference to temporary
606606
} else if (tok->variable() && (tok->variable()->isReference() || tok->variable()->isRValueReference())) {
607-
for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(getParentLifetime(tok), *mSettings)) {
607+
for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(getParentLifetime(tok), mSettings)) {
608608
if (!printInconclusive && lt.inconclusive)
609609
continue;
610610
const Token * tokvalue = lt.token;
611-
if (isDeadTemporary(tokvalue, tok, mSettings->library)) {
611+
if (isDeadTemporary(tokvalue, tok, mSettings.library)) {
612612
errorDanglingTempReference(tok, lt.errorPath, lt.inconclusive);
613613
break;
614614
}
@@ -622,23 +622,23 @@ void CheckAutoVariablesImpl::checkVarLifetimeScope(const Token * start, const To
622622
continue;
623623
if (!printInconclusive && val.isInconclusive())
624624
continue;
625-
const Token* parent = getParentLifetime(val.tokvalue, mSettings->library);
625+
const Token* parent = getParentLifetime(val.tokvalue, mSettings.library);
626626
if (!exprs.insert(parent).second)
627627
continue;
628-
for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(parent, *mSettings, escape || isAssignedToNonLocal(tok))) {
628+
for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(parent, mSettings, escape || isAssignedToNonLocal(tok))) {
629629
const Token * tokvalue = lt.token;
630630
if (val.isLocalLifetimeValue()) {
631631
if (escape) {
632632
if (getPointerDepth(tok) < getPointerDepth(tokvalue))
633633
continue;
634-
if (!ValueFlow::isLifetimeBorrowed(tok, *mSettings))
634+
if (!ValueFlow::isLifetimeBorrowed(tok, mSettings))
635635
continue;
636636
if (tokvalue->exprId() == tok->exprId() && !(tok->variable() && tok->variable()->isArray()) &&
637637
!astIsContainerView(tok->astParent()))
638638
continue;
639639
if ((tokvalue->variable() && !isEscapedReference(tokvalue->variable()) &&
640640
isInScope(tokvalue->variable()->nameToken(), scope)) ||
641-
isDeadTemporary(tokvalue, nullptr, mSettings->library)) {
641+
isDeadTemporary(tokvalue, nullptr, mSettings.library)) {
642642
if (!diag(tokvalue))
643643
errorReturnDanglingLifetime(tok, &val);
644644
break;
@@ -647,7 +647,7 @@ void CheckAutoVariablesImpl::checkVarLifetimeScope(const Token * start, const To
647647
errorInvalidLifetime(tok, &val);
648648
break;
649649
} else if (!tokvalue->variable() &&
650-
isDeadTemporary(tokvalue, tok, mSettings->library)) {
650+
isDeadTemporary(tokvalue, tok, mSettings.library)) {
651651
if (!diag(tokvalue))
652652
errorDanglingTemporaryLifetime(tok, &val, tokvalue);
653653
break;
@@ -665,7 +665,7 @@ void CheckAutoVariablesImpl::checkVarLifetimeScope(const Token * start, const To
665665
} else if (tok->variable() && tok->variable()->declarationId() == tok->varId()) {
666666
var = tok->variable();
667667
}
668-
if (!ValueFlow::isLifetimeBorrowed(tok, *mSettings))
668+
if (!ValueFlow::isLifetimeBorrowed(tok, mSettings))
669669
continue;
670670
const Token* nextTok = nextAfterAstRightmostLeaf(tok->astTop());
671671
if (!nextTok)
@@ -677,7 +677,7 @@ void CheckAutoVariablesImpl::checkVarLifetimeScope(const Token * start, const To
677677
var->valueType() ? var->valueType()->pointer : 0,
678678
var->declarationId(),
679679
var->isGlobal(),
680-
*mSettings)) {
680+
mSettings)) {
681681
if (!diag(tok2))
682682
errorDanglngLifetime(tok2, &val, var->isLocal());
683683
break;
@@ -819,13 +819,13 @@ void CheckAutoVariablesImpl::errorInvalidDeallocation(const Token *tok, const Va
819819

820820
void CheckAutoVariables::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
821821
{
822-
CheckAutoVariablesImpl checkAutoVariables(&tokenizer, &tokenizer.getSettings(), errorLogger);
822+
CheckAutoVariablesImpl checkAutoVariables(&tokenizer, tokenizer.getSettings(), errorLogger);
823823
checkAutoVariables.assignFunctionArg();
824824
checkAutoVariables.autoVariables();
825825
checkAutoVariables.checkVarLifetime();
826826
}
827827

828-
void CheckAutoVariables::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const
828+
void CheckAutoVariables::getErrorMessages(ErrorLogger *errorLogger, const Settings &settings) const
829829
{
830830
CheckAutoVariablesImpl c(nullptr,settings,errorLogger);
831831
c.errorAutoVariableAssignment(nullptr, false);

lib/checkautovariables.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class CPPCHECKLIB CheckAutoVariables : public Check {
5454
/** @brief Run checks against the normal token list */
5555
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override;
5656

57-
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override;
57+
void getErrorMessages(ErrorLogger *errorLogger, const Settings &settings) const override;
5858

5959
std::string classInfo() const override {
6060
return "A pointer to a variable is only valid as long as the variable is in scope.\n"
@@ -72,7 +72,7 @@ class CPPCHECKLIB CheckAutoVariablesImpl : public CheckImpl
7272
{
7373
public:
7474
/** This constructor is used when running checks. */
75-
CheckAutoVariablesImpl(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
75+
CheckAutoVariablesImpl(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger)
7676
: CheckImpl(tokenizer, settings, errorLogger) {}
7777

7878
/** assign function argument */

0 commit comments

Comments
 (0)