got rid of test-only `Library` functions / avoid some more direct modifications of library data structures in test (#5468)

This commit is contained in:
Oliver Stöneberg 2023-09-25 13:37:24 +02:00 committed by GitHub
parent e6e273645d
commit 3979ade9ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 58 deletions

View File

@ -187,13 +187,6 @@ Library::Container::Action Library::Container::actionFrom(const std::string& act
return Container::Action::NO_ACTION;
}
// cppcheck-suppress unusedFunction - only used in unit tests
bool Library::loadxmldata(const char xmldata[], std::size_t len)
{
tinyxml2::XMLDocument doc;
return (tinyxml2::XML_SUCCESS == doc.Parse(xmldata, len)) && (load(doc).errorcode == ErrorCode::OK);
}
Library::Error Library::load(const tinyxml2::XMLDocument &doc)
{
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();

View File

@ -75,9 +75,6 @@ public:
Error load(const char exename[], const char path[]);
Error load(const tinyxml2::XMLDocument &doc);
/** this is used for unit tests */
bool loadxmldata(const char xmldata[], std::size_t len);
struct AllocFunc {
int groupId;
int arg;
@ -134,32 +131,6 @@ public:
return af ? af->groupId : 0;
}
/** set allocation id for function */
// cppcheck-suppress unusedFunction - test-only
void setalloc(const std::string &functionname, int id, int arg) {
mAlloc[functionname].groupId = id;
mAlloc[functionname].arg = arg;
}
// cppcheck-suppress unusedFunction - test-only
void setdealloc(const std::string &functionname, int id, int arg) {
mDealloc[functionname].groupId = id;
mDealloc[functionname].arg = arg;
}
// cppcheck-suppress unusedFunction - test-only
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 - test-only
void setnoreturn(const std::string& funcname, bool noreturn) {
mNoReturn[funcname] = noreturn ? FalseTrueMaybe::True : FalseTrueMaybe::False;
}
static bool isCompliantValidationExpression(const char* p);
/** is allocation type memory? */

View File

@ -46,7 +46,7 @@ private:
"<def>\n"
" <function name=\"bar\"> <pure/> </function>\n"
"</def>";
ASSERT(settings1.library.loadxmldata(cfg, sizeof(cfg)));
settings1 = settingsBuilder(settings1).libraryxml(cfg, sizeof(cfg)).build();
TEST_CASE(assignAndCompare); // assignment and comparison don't match
TEST_CASE(mismatchingBitAnd); // overlapping bitmasks

View File

@ -44,27 +44,29 @@ private:
Settings settings;
void run() override {
int id = 0;
while (!Library::ismemory(++id));
settings.library.setalloc("malloc", id, -1);
settings.library.setrealloc("realloc", id, -1);
settings.library.setdealloc("free", id, 1);
while (!Library::isresource(++id));
settings.library.setalloc("socket", id, -1);
settings.library.setdealloc("close", id, 1);
while (!Library::isresource(++id));
settings.library.setalloc("fopen", id, -1);
settings.library.setrealloc("freopen", id, -1, 3);
settings.library.setdealloc("fclose", id, 1);
settings.library.smartPointers["std::shared_ptr"];
settings.library.smartPointers["std::unique_ptr"];
settings.library.smartPointers["std::unique_ptr"].unique = true;
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <podtype name=\"uint8_t\" sign=\"u\" size=\"1\"/>\n"
" <memory>\n"
" <alloc>malloc</alloc>\n"
" <realloc>realloc</realloc>\n"
" <dealloc>free</dealloc>\n"
" </memory>\n"
" <resource>\n"
" <alloc>socket</alloc>\n"
" <dealloc>close</dealloc>\n"
" </resource>\n"
" <resource>\n"
" <alloc>fopen</alloc>\n"
" <realloc realloc-arg=\"3\">freopen</realloc>\n"
" <dealloc>fclose</dealloc>\n"
" </resource>\n"
" <smart-pointer class-name=\"std::shared_ptr\"/>\n"
" <smart-pointer class-name=\"std::unique_ptr\">\n"
" <unique/>\n"
" </smart-pointer>\n"
"</def>";
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
settings = settingsBuilder(settings).libraryxml(xmldata, sizeof(xmldata)).build();
// Assign
TEST_CASE(assign1);

View File

@ -4681,9 +4681,15 @@ private:
"}", nullptr, false, false);
ASSERT_EQUALS("[test.cpp:3]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary.\n", errout.str());
Settings settings;
settings.library.setnoreturn("exit", true);
settings.library.functions["exit"].argumentChecks[1] = Library::ArgumentChecks();
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"exit\">\n"
" <noreturn>true</noreturn>\n"
" <arg nr=\"1\"/>\n"
" </function>\n"
"</def>";
Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
check("void foo() {\n"
" exit(0);\n"
" break;\n"

View File

@ -55,7 +55,7 @@ private:
" <function name=\"strcpy\"> <arg nr=\"1\"><not-null/></arg> </function>\n"
" <function name=\"abort\"> <noreturn>true</noreturn> </function>\n" // abort is a noreturn function
"</def>";
ASSERT_EQUALS(true, settings.library.loadxmldata(cfg, sizeof(cfg)));
settings = settingsBuilder(settings).libraryxml(cfg, sizeof(cfg)).build();
TEST_CASE(valueFlowNumber);
TEST_CASE(valueFlowString);