Skip to content

Commit

Permalink
got rid of global CTU::maxCtuDepth
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Oct 12, 2024
1 parent 6650bce commit 307909f
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 22 deletions.
8 changes: 7 additions & 1 deletion cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,8 +903,14 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a

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

else if (std::strcmp(argv[i], "--no-cpp-header-probe") == 0) {
Expand Down
3 changes: 2 additions & 1 deletion lib/checkbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,8 @@ bool CheckBufferOverrun::analyseWholeProgram1(const std::map<std::string, std::l
callsMap,
"Using argument ARG",
&functionCall,
false);
false,
mSettings->maxCtuDepth);
if (locationList.empty())
return false;

Expand Down
2 changes: 1 addition & 1 deletion lib/checkbufferoverrun.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class CPPCHECKLIB CheckBufferOverrun : public Check {
static bool isCtuUnsafePointerArith(const Settings &settings, const Token *argtok, MathLib::bigint *offset);

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


static std::string myName() {
Expand Down
3 changes: 2 additions & 1 deletion lib/checknullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,8 @@ bool CheckNullPointer::analyseWholeProgram(const CTU::FileInfo *ctu, const std::
callsMap,
"Dereferencing argument ARG that is null",
nullptr,
warning);
warning,
mSettings->maxCtuDepth);
if (locationList.empty())
continue;

Expand Down
3 changes: 2 additions & 1 deletion lib/checkuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,8 @@ bool CheckUninitVar::analyseWholeProgram(const CTU::FileInfo *ctu, const std::li
callsMap,
"Using argument ARG",
&functionCall,
false);
false,
mSettings->maxCtuDepth);
if (locationList.empty())
continue;

Expand Down
5 changes: 0 additions & 5 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1807,8 +1807,6 @@ void CppCheck::analyseClangTidy(const FileSettings &fileSettings)
bool CppCheck::analyseWholeProgram()
{
bool errors = false;
// Init CTU
CTU::maxCtuDepth = mSettings.maxCtuDepth;
// Analyse the tokens
CTU::FileInfo ctu;
if (mSettings.useSingleJob() || !mSettings.buildDir.empty())
Expand Down Expand Up @@ -1881,9 +1879,6 @@ unsigned int CppCheck::analyseWholeProgram(const std::string &buildDir, const st
}
}

// Set CTU max depth
CTU::maxCtuDepth = mSettings.maxCtuDepth;

// Analyse the tokens
// cppcheck-suppress shadowFunction - TODO: fix this
for (Check *check : Check::instances())
Expand Down
16 changes: 8 additions & 8 deletions lib/ctu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ static constexpr char ATTR_MY_ARGNR[] = "my-argnr";
static constexpr char ATTR_MY_ARGNAME[] = "my-argname";
static constexpr char ATTR_VALUE[] = "value";

int CTU::maxCtuDepth = 2;

std::string CTU::getFunctionId(const Tokenizer &tokenizer, const Function *function)
{
return tokenizer.list.file(function->tokenDef) + ':' + std::to_string(function->tokenDef->linenr()) + ':' + std::to_string(function->tokenDef->column());
Expand Down Expand Up @@ -502,10 +500,11 @@ static bool findPath(const std::string &callId,
const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> &callsMap,
const CTU::FileInfo::CallBase *path[10],
int index,
bool warning)
bool warning,
int maxCtuDepth)
{
if (index >= CTU::maxCtuDepth || index >= 10)
return false;
if (index >= maxCtuDepth)
return false; // TODO: add bailout message?

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

if (findPath(nestedCall->myId, nestedCall->myArgNr, unsafeValue, invalidValue, callsMap, path, index + 1, warning)) {
if (findPath(nestedCall->myId, nestedCall->myArgNr, unsafeValue, invalidValue, callsMap, path, index + 1, warning, maxCtuDepth)) {
path[index] = nestedCall;
return true;
}
Expand All @@ -557,13 +556,14 @@ std::list<ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(InvalidValueTy
const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> &callsMap,
const char info[],
const FunctionCall ** const functionCallPtr,
bool warning)
bool warning,
int maxCtuDepth)
{
std::list<ErrorMessage::FileLocation> locationList;

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

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

const std::string value1 = (invalidValue == InvalidValueType::null) ? "null" : "uninitialized";
Expand Down
5 changes: 2 additions & 3 deletions lib/ctu.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,10 @@ namespace CTU {
const std::map<std::string, std::list<const CallBase *>> &callsMap,
const char info[],
const FunctionCall ** functionCallPtr,
bool warning);
bool warning,
int maxCtuDepth);
};

extern int maxCtuDepth;

CPPCHECKLIB std::string toString(const std::list<FileInfo::UnsafeUsage> &unsafeUsage);

CPPCHECKLIB std::string getFunctionId(const Tokenizer &tokenizer, const Function *function);
Expand Down
1 change: 1 addition & 0 deletions releasenotes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Changed interface:

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

Other:
Expand Down
19 changes: 18 additions & 1 deletion test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(loadAverageNotSupported);
#endif
TEST_CASE(maxCtuDepth);
TEST_CASE(maxCtuDepth2);
TEST_CASE(maxCtuDepthLimit);
TEST_CASE(maxCtuDepthInvalid);
TEST_CASE(performanceValueflowMaxTime);
TEST_CASE(performanceValueflowMaxTimeInvalid);
Expand Down Expand Up @@ -2110,10 +2112,25 @@ class TestCmdlineParser : public TestFixture {
#endif

void maxCtuDepth() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--max-ctu-depth=5", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT_EQUALS(5, settings->maxCtuDepth);
}

void maxCtuDepth2() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--max-ctu-depth=10", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT_EQUALS(10, settings->maxCtuDepth);
}

void maxCtuDepthLimit() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--max-ctu-depth=12", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT_EQUALS(12, settings->maxCtuDepth);
ASSERT_EQUALS(10, settings->maxCtuDepth);
ASSERT_EQUALS("cppcheck: --max-ctu-depth is being capped at 10. This limitation will be removed in a future Cppcheck version.\n", logger->str());
}

void maxCtuDepthInvalid() {
Expand Down

0 comments on commit 307909f

Please sign in to comment.