fixed #11183 (checkLibraryFunction with parameter to "std::string()") / added rudimentary tests for `CheckFunctions::checkLibraryMatchFunctions()` / added test for #10105 (#4265)

This commit is contained in:
Oliver Stöneberg 2022-07-12 17:39:01 +02:00 committed by GitHub
parent aea4fd1068
commit 22be5f29aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 1 deletions

View File

@ -624,7 +624,19 @@ void CheckFunctions::checkLibraryMatchFunctions()
continue;
const std::string &functionName = mSettings->library.getFunctionName(tok);
if (functionName.empty() || mSettings->library.functions.find(functionName) != mSettings->library.functions.end())
if (functionName.empty())
continue;
if (mSettings->library.functions.find(functionName) != mSettings->library.functions.end())
continue;
if (mSettings->library.smartPointers.find(functionName) != mSettings->library.smartPointers.end())
continue;
const Token* start = tok;
while (Token::Match(start->tokAt(-2), "%name% ::"))
start = start->tokAt(-2);
if (mSettings->library.detectContainerOrIterator(start))
continue;
reportError(tok,

View File

@ -99,6 +99,8 @@ private:
TEST_CASE(returnLocalStdMove5);
TEST_CASE(negativeMemoryAllocationSizeError); // #389
TEST_CASE(checkLibraryMatchFunctions);
}
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
@ -1769,6 +1771,51 @@ private:
ASSERT_EQUALS("[test.cpp:3]: (warning) Obsolete function 'alloca' called.\n"
"[test.cpp:3]: (error) Invalid alloca() argument nr 1. The value is -10 but the valid values are '0:'.\n", errout.str());
}
void checkLibraryMatchFunctions() {
settings.checkLibrary = true;
auto severity_old = settings.severity;
settings.severity.enable(Severity::information);
check("void f() {\n"
" lib_func();"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(void* v) {\n"
" lib_func(v);"
"}");
ASSERT_EQUALS("[test.cpp:2]: (information) --check-library: There is no matching configuration for function lib_func()\n", errout.str());
// #10105
check("class TestFixture {\n"
"protected:\n"
" bool prepareTest(const char testname[]);\n"
"};\n"
"\n"
"class TestMemleak : private TestFixture {\n"
" void run() {\n"
" do { prepareTest(\"testFunctionReturnType\"); } while (false);\n"
" }\n"
"\n"
" void testFunctionReturnType() {\n"
" }\n"
"};");
ASSERT_EQUALS("", errout.str());
// #11183
check("#include <string>\n"
"\n"
"extern void cb(const std::string&);\n"
"\n"
"void f() {\n"
" cb(std::string(\"\"));\n"
"}");
TODO_ASSERT_EQUALS("", "[test.cpp:6]: (information) --check-library: There is no matching configuration for function cb()\n", errout.str());
settings.severity = severity_old;
settings.checkLibrary = false;
}
};
REGISTER_TEST(TestFunctions)