Skip to content

Commit

Permalink
CppParser: fix for std::function<void()> parameter
Browse files Browse the repository at this point in the history
The parameter was previously seen as a function because of it's
brackets.
  • Loading branch information
Fabio Oberhofer committed Jul 3, 2023
1 parent ead93ba commit f6e2524
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
23 changes: 23 additions & 0 deletions CppParser/src/Symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
16 changes: 16 additions & 0 deletions CppParser/testsuite/src/CppParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f6e2524

Please sign in to comment.