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; 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) Library::Error Library::load(const tinyxml2::XMLDocument &doc)
{ {
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement(); 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 char exename[], const char path[]);
Error load(const tinyxml2::XMLDocument &doc); Error load(const tinyxml2::XMLDocument &doc);
/** this is used for unit tests */
bool loadxmldata(const char xmldata[], std::size_t len);
struct AllocFunc { struct AllocFunc {
int groupId; int groupId;
int arg; int arg;
@ -134,32 +131,6 @@ public:
return af ? af->groupId : 0; 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); static bool isCompliantValidationExpression(const char* p);
/** is allocation type memory? */ /** is allocation type memory? */

View File

@ -46,7 +46,7 @@ private:
"<def>\n" "<def>\n"
" <function name=\"bar\"> <pure/> </function>\n" " <function name=\"bar\"> <pure/> </function>\n"
"</def>"; "</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(assignAndCompare); // assignment and comparison don't match
TEST_CASE(mismatchingBitAnd); // overlapping bitmasks TEST_CASE(mismatchingBitAnd); // overlapping bitmasks

View File

@ -44,27 +44,29 @@ private:
Settings settings; Settings settings;
void run() override { 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" const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n" "<def>\n"
" <podtype name=\"uint8_t\" sign=\"u\" size=\"1\"/>\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>"; "</def>";
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata))); settings = settingsBuilder(settings).libraryxml(xmldata, sizeof(xmldata)).build();
// Assign // Assign
TEST_CASE(assign1); TEST_CASE(assign1);

View File

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

View File

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