Merge pull request #4027 from Crafty-Codes/fix/function-parameter

CppParser: fix for std::function<void()> parameter
This commit is contained in:
Günter Obiltschnig 2023-07-11 06:42:56 +02:00 committed by GitHub
commit 6356cd22ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 0 deletions

View File

@ -121,6 +121,29 @@ std::string Symbol::extractName(const std::string& decl)
return "operator []";
std::string::size_type pos = decl.find('(');
if (pos != std::string::npos) {
// special case std::function<void(int)> a
// ^ ^^
// In case the marked patterns are found,
// reset pos to npos to make sure "(" is not seen as the beginning of a function
int bracket = 1;
std::string::size_type i = pos + 1;
while (i < decl.size() && bracket != 0){
if (decl[i] == '('){
bracket++;
} else if (decl[i] == ')'){
bracket--;
}
i++;
}
while (i < decl.size() && std::isspace(decl[i])) i++;
if (i < decl.size() && decl[i] == '>') {
pos = std::string::npos;
}
}
// another special case: function pointer
if (pos != std::string::npos && pos < decl.size() - 1)
{

View File

@ -77,6 +77,22 @@ void CppParserTest::testExtractName()
decl = "void func(int arg1, int arg2)";
name = Symbol::extractName(decl);
assertTrue (name == "func");
decl = "std::function<bool> func";
name = Symbol::extractName(decl);
assertTrue (name == "func");
decl = "std::function<void(bool)> func";
name = Symbol::extractName(decl);
assertTrue (name == "func");
decl = "std::function<std::vector<int>(std::vector<bool>)> func";
name = Symbol::extractName(decl);
assertTrue (name == "func");
decl = "std::function<void*(std::function<const int*(void)>)> func";
name = Symbol::extractName(decl);
assertTrue (name == "func");
decl = "const std::vector<NS::MyType>* var";
name = Symbol::extractName(decl);