Skip to content

Commit 56b7b6a

Browse files
authored
got rid of global CTU::maxCtuDepth (danmar#6892)
1 parent 89465bc commit 56b7b6a

File tree

10 files changed

+46
-26
lines changed

10 files changed

+46
-26
lines changed

cli/cmdlineparser.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,14 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
903903

904904
// max ctu depth
905905
else if (std::strncmp(argv[i], "--max-ctu-depth=", 16) == 0) {
906-
if (!parseNumberArg(argv[i], 16, mSettings.maxCtuDepth))
906+
int temp = 0;
907+
if (!parseNumberArg(argv[i], 16, temp))
907908
return Result::Fail;
909+
if (temp > 10) {
910+
mLogger.printMessage("--max-ctu-depth is being capped at 10. This limitation will be removed in a future Cppcheck version.");
911+
temp = 10;
912+
}
913+
mSettings.maxCtuDepth = temp;
908914
}
909915

910916
else if (std::strcmp(argv[i], "--no-cpp-header-probe") == 0) {

lib/checkbufferoverrun.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,6 @@ bool CheckBufferOverrun::analyseWholeProgram(const CTU::FileInfo *ctu, const std
987987
if (!ctu)
988988
return false;
989989
bool foundErrors = false;
990-
(void)settings; // This argument is unused
991990

992991
CheckBufferOverrun dummy(nullptr, &settings, &errorLogger);
993992
dummy.
@@ -1000,14 +999,14 @@ bool CheckBufferOverrun::analyseWholeProgram(const CTU::FileInfo *ctu, const std
1000999
if (!fi)
10011000
continue;
10021001
for (const CTU::FileInfo::UnsafeUsage &unsafeUsage : fi->unsafeArrayIndex)
1003-
foundErrors |= analyseWholeProgram1(callsMap, unsafeUsage, 1, errorLogger);
1002+
foundErrors |= analyseWholeProgram1(callsMap, unsafeUsage, 1, errorLogger, settings.maxCtuDepth);
10041003
for (const CTU::FileInfo::UnsafeUsage &unsafeUsage : fi->unsafePointerArith)
1005-
foundErrors |= analyseWholeProgram1(callsMap, unsafeUsage, 2, errorLogger);
1004+
foundErrors |= analyseWholeProgram1(callsMap, unsafeUsage, 2, errorLogger, settings.maxCtuDepth);
10061005
}
10071006
return foundErrors;
10081007
}
10091008

1010-
bool CheckBufferOverrun::analyseWholeProgram1(const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> &callsMap, const CTU::FileInfo::UnsafeUsage &unsafeUsage, int type, ErrorLogger &errorLogger)
1009+
bool CheckBufferOverrun::analyseWholeProgram1(const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> &callsMap, const CTU::FileInfo::UnsafeUsage &unsafeUsage, int type, ErrorLogger &errorLogger, int maxCtuDepth)
10111010
{
10121011
const CTU::FileInfo::FunctionCall *functionCall = nullptr;
10131012

@@ -1017,7 +1016,8 @@ bool CheckBufferOverrun::analyseWholeProgram1(const std::map<std::string, std::l
10171016
callsMap,
10181017
"Using argument ARG",
10191018
&functionCall,
1020-
false);
1019+
false,
1020+
maxCtuDepth);
10211021
if (locationList.empty())
10221022
return false;
10231023

lib/checkbufferoverrun.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class CPPCHECKLIB CheckBufferOverrun : public Check {
135135
static bool isCtuUnsafePointerArith(const Settings &settings, const Token *argtok, MathLib::bigint *offset);
136136

137137
Check::FileInfo * loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const override;
138-
static bool analyseWholeProgram1(const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> &callsMap, const CTU::FileInfo::UnsafeUsage &unsafeUsage, int type, ErrorLogger &errorLogger);
138+
static bool analyseWholeProgram1(const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> &callsMap, const CTU::FileInfo::UnsafeUsage &unsafeUsage, int type, ErrorLogger &errorLogger, int maxCtuDepth);
139139

140140

141141
static std::string myName() {

lib/checknullpointer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,8 @@ bool CheckNullPointer::analyseWholeProgram(const CTU::FileInfo *ctu, const std::
637637
callsMap,
638638
"Dereferencing argument ARG that is null",
639639
nullptr,
640-
warning);
640+
warning,
641+
settings.maxCtuDepth);
641642
if (locationList.empty())
642643
continue;
643644

lib/checkuninitvar.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,8 @@ bool CheckUninitVar::analyseWholeProgram(const CTU::FileInfo *ctu, const std::li
17471747
callsMap,
17481748
"Using argument ARG",
17491749
&functionCall,
1750-
false);
1750+
false,
1751+
settings.maxCtuDepth);
17511752
if (locationList.empty())
17521753
continue;
17531754

lib/cppcheck.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,8 +1807,6 @@ void CppCheck::analyseClangTidy(const FileSettings &fileSettings)
18071807
bool CppCheck::analyseWholeProgram()
18081808
{
18091809
bool errors = false;
1810-
// Init CTU
1811-
CTU::maxCtuDepth = mSettings.maxCtuDepth;
18121810
// Analyse the tokens
18131811
CTU::FileInfo ctu;
18141812
if (mSettings.useSingleJob() || !mSettings.buildDir.empty())
@@ -1881,9 +1879,6 @@ unsigned int CppCheck::analyseWholeProgram(const std::string &buildDir, const st
18811879
}
18821880
}
18831881

1884-
// Set CTU max depth
1885-
CTU::maxCtuDepth = mSettings.maxCtuDepth;
1886-
18871882
// Analyse the tokens
18881883
// cppcheck-suppress shadowFunction - TODO: fix this
18891884
for (Check *check : Check::instances())

lib/ctu.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ static constexpr char ATTR_MY_ARGNR[] = "my-argnr";
5555
static constexpr char ATTR_MY_ARGNAME[] = "my-argname";
5656
static constexpr char ATTR_VALUE[] = "value";
5757

58-
int CTU::maxCtuDepth = 2;
59-
6058
std::string CTU::getFunctionId(const Tokenizer &tokenizer, const Function *function)
6159
{
6260
return tokenizer.list.file(function->tokenDef) + ':' + std::to_string(function->tokenDef->linenr()) + ':' + std::to_string(function->tokenDef->column());
@@ -502,10 +500,11 @@ static bool findPath(const std::string &callId,
502500
const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> &callsMap,
503501
const CTU::FileInfo::CallBase *path[10],
504502
int index,
505-
bool warning)
503+
bool warning,
504+
int maxCtuDepth)
506505
{
507-
if (index >= CTU::maxCtuDepth || index >= 10)
508-
return false;
506+
if (index >= maxCtuDepth)
507+
return false; // TODO: add bailout message?
509508

510509
const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>>::const_iterator it = callsMap.find(callId);
511510
if (it == callsMap.end())
@@ -543,7 +542,7 @@ static bool findPath(const std::string &callId,
543542
if (!nestedCall)
544543
continue;
545544

546-
if (findPath(nestedCall->myId, nestedCall->myArgNr, unsafeValue, invalidValue, callsMap, path, index + 1, warning)) {
545+
if (findPath(nestedCall->myId, nestedCall->myArgNr, unsafeValue, invalidValue, callsMap, path, index + 1, warning, maxCtuDepth)) {
547546
path[index] = nestedCall;
548547
return true;
549548
}
@@ -557,13 +556,14 @@ std::list<ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(InvalidValueTy
557556
const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> &callsMap,
558557
const char info[],
559558
const FunctionCall ** const functionCallPtr,
560-
bool warning)
559+
bool warning,
560+
int maxCtuDepth)
561561
{
562562
std::list<ErrorMessage::FileLocation> locationList;
563563

564564
const CTU::FileInfo::CallBase *path[10] = {nullptr};
565565

566-
if (!findPath(unsafeUsage.myId, unsafeUsage.myArgNr, unsafeUsage.value, invalidValue, callsMap, path, 0, warning))
566+
if (!findPath(unsafeUsage.myId, unsafeUsage.myArgNr, unsafeUsage.value, invalidValue, callsMap, path, 0, warning, maxCtuDepth))
567567
return locationList;
568568

569569
const std::string value1 = (invalidValue == InvalidValueType::null) ? "null" : "uninitialized";

lib/ctu.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,10 @@ namespace CTU {
135135
const std::map<std::string, std::list<const CallBase *>> &callsMap,
136136
const char info[],
137137
const FunctionCall ** functionCallPtr,
138-
bool warning);
138+
bool warning,
139+
int maxCtuDepth);
139140
};
140141

141-
extern int maxCtuDepth;
142-
143142
CPPCHECKLIB std::string toString(const std::list<FileInfo::UnsafeUsage> &unsafeUsage);
144143

145144
CPPCHECKLIB std::string getFunctionId(const Tokenizer &tokenizer, const Function *function);

releasenotes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Changed interface:
1616

1717
Deprecations:
1818
- The previously deprecated support for Python 2.7 has been removed. please use Python 3 instead.
19+
- The maximum value for --max-ctu-depth is currently capped at 10. This limitation will be removed in a future release.
1920
-
2021

2122
Other:

test/testcmdlineparser.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ class TestCmdlineParser : public TestFixture {
323323
TEST_CASE(loadAverageNotSupported);
324324
#endif
325325
TEST_CASE(maxCtuDepth);
326+
TEST_CASE(maxCtuDepth2);
327+
TEST_CASE(maxCtuDepthLimit);
326328
TEST_CASE(maxCtuDepthInvalid);
327329
TEST_CASE(performanceValueflowMaxTime);
328330
TEST_CASE(performanceValueflowMaxTimeInvalid);
@@ -2110,10 +2112,25 @@ class TestCmdlineParser : public TestFixture {
21102112
#endif
21112113

21122114
void maxCtuDepth() {
2115+
REDIRECT;
2116+
const char * const argv[] = {"cppcheck", "--max-ctu-depth=5", "file.cpp"};
2117+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
2118+
ASSERT_EQUALS(5, settings->maxCtuDepth);
2119+
}
2120+
2121+
void maxCtuDepth2() {
2122+
REDIRECT;
2123+
const char * const argv[] = {"cppcheck", "--max-ctu-depth=10", "file.cpp"};
2124+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
2125+
ASSERT_EQUALS(10, settings->maxCtuDepth);
2126+
}
2127+
2128+
void maxCtuDepthLimit() {
21132129
REDIRECT;
21142130
const char * const argv[] = {"cppcheck", "--max-ctu-depth=12", "file.cpp"};
21152131
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
2116-
ASSERT_EQUALS(12, settings->maxCtuDepth);
2132+
ASSERT_EQUALS(10, settings->maxCtuDepth);
2133+
ASSERT_EQUALS("cppcheck: --max-ctu-depth is being capped at 10. This limitation will be removed in a future Cppcheck version.\n", logger->str());
21172134
}
21182135

21192136
void maxCtuDepthInvalid() {

0 commit comments

Comments
 (0)