From f6e2524db62292c7c4b53c4559f81612e15c9a42 Mon Sep 17 00:00:00 2001 From: Fabio Oberhofer Date: Mon, 8 May 2023 15:20:19 +0200 Subject: [PATCH] CppParser: fix for std::function parameter The parameter was previously seen as a function because of it's brackets. --- CppParser/src/Symbol.cpp | 23 +++++++++++++++++++++++ CppParser/testsuite/src/CppParserTest.cpp | 16 ++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/CppParser/src/Symbol.cpp b/CppParser/src/Symbol.cpp index ad2f94f3b8..b55433521f 100644 --- a/CppParser/src/Symbol.cpp +++ b/CppParser/src/Symbol.cpp @@ -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 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) { diff --git a/CppParser/testsuite/src/CppParserTest.cpp b/CppParser/testsuite/src/CppParserTest.cpp index 7a38b7166b..35ce2d5ec2 100644 --- a/CppParser/testsuite/src/CppParserTest.cpp +++ b/CppParser/testsuite/src/CppParserTest.cpp @@ -77,6 +77,22 @@ void CppParserTest::testExtractName() decl = "void func(int arg1, int arg2)"; name = Symbol::extractName(decl); assertTrue (name == "func"); + + decl = "std::function func"; + name = Symbol::extractName(decl); + assertTrue (name == "func"); + + decl = "std::function func"; + name = Symbol::extractName(decl); + assertTrue (name == "func"); + + decl = "std::function(std::vector)> func"; + name = Symbol::extractName(decl); + assertTrue (name == "func"); + + decl = "std::function)> func"; + name = Symbol::extractName(decl); + assertTrue (name == "func"); decl = "const std::vector* var"; name = Symbol::extractName(decl);