diff --git a/include/boost/dll/detail/demangling/msvc.hpp b/include/boost/dll/detail/demangling/msvc.hpp index ff9ac03b..9e455a71 100644 --- a/include/boost/dll/detail/demangling/msvc.hpp +++ b/include/boost/dll/detail/demangling/msvc.hpp @@ -84,21 +84,20 @@ void mangled_storage_impl::trim_typename(std::string & val) } } - namespace parser { - inline bool consume_string(boost::core::string_view& s, boost::core::string_view str) { - const bool result = s.starts_with(str); + inline bool try_consume_prefix(boost::core::string_view& s, boost::core::string_view prefix) { + const bool result = s.starts_with(prefix); if (result) { - s.remove_prefix(str.size()); + s.remove_prefix(prefix.size()); } return result; } inline void consume_ptrs(boost::core::string_view& s) { do { - while (parser::consume_string(s, " ")) {} - } while (parser::consume_string(s, "__ptr32") || parser::consume_string(s, "__ptr64")); + while (parser::try_consume_prefix(s, " ")) {} + } while (parser::try_consume_prefix(s, "__ptr32") || parser::try_consume_prefix(s, "__ptr64")); } inline bool ignore_ptrs(boost::core::string_view& s) { @@ -106,46 +105,46 @@ namespace parser { return true; } - inline bool consume_visibility(boost::core::string_view& s) { - return parser::consume_string(s, "public:") - || parser::consume_string(s, "protected:") - || parser::consume_string(s, "private:"); + inline bool try_consume_visibility(boost::core::string_view& s) { + return parser::try_consume_prefix(s, "public:") + || parser::try_consume_prefix(s, "protected:") + || parser::try_consume_prefix(s, "private:"); } template - bool consume_type(boost::core::string_view& s, const mangled_storage_impl& ms) { + bool try_consume_type(boost::core::string_view& s, const mangled_storage_impl& ms) { if (std::is_void::value) { - return parser::consume_string(s, "void"); + return parser::try_consume_prefix(s, "void"); } - parser::consume_string(s, "class "); - parser::consume_string(s, "struct "); + parser::try_consume_prefix(s, "class "); + parser::try_consume_prefix(s, "struct "); const auto& mangled_name = ms.get_name(); - if (!parser::consume_string(s, mangled_name)) { + if (!parser::try_consume_prefix(s, mangled_name)) { return false; } if (std::is_const::type>::value) { - if (!parser::consume_string(s, " const")) { + if (!parser::try_consume_prefix(s, " const")) { return false; } } if (std::is_volatile::type>::value) { - if (!parser::consume_string(s, " volatile")) { + if (!parser::try_consume_prefix(s, " volatile")) { return false; } } if (std::is_rvalue_reference::value) { - if (!parser::consume_string(s, " &&")) { + if (!parser::try_consume_prefix(s, " &&")) { return false; } } if (std::is_lvalue_reference::value) { - if (!parser::consume_string(s, " &")) { + if (!parser::try_consume_prefix(s, " &")) { return false; } } @@ -153,28 +152,28 @@ namespace parser { return parser::ignore_ptrs(s); } - inline bool consume_thiscall(boost::core::string_view& s) { - parser::consume_string(s, " "); - return parser::consume_string(s, "__cdecl ") // Win 64bit - || parser::consume_string(s, "__thiscall "); // Win 32bit + inline bool try_consume_thiscall(boost::core::string_view& s) { + parser::try_consume_prefix(s, " "); + return parser::try_consume_prefix(s, "__cdecl ") // Win 64bit + || parser::try_consume_prefix(s, "__thiscall "); // Win 32bit } template - bool consume_arg_list(boost::core::string_view& s, const mangled_storage_impl& ms, Return (*)(Arg)) { - return parser::consume_type(s, ms); + bool try_consume_arg_list(boost::core::string_view& s, const mangled_storage_impl& ms, Return (*)(Arg)) { + return parser::try_consume_type(s, ms); } template - bool consume_arg_list(boost::core::string_view& s, const mangled_storage_impl& ms, Return (*)(First, Second, Args...)) { + bool try_consume_arg_list(boost::core::string_view& s, const mangled_storage_impl& ms, Return (*)(First, Second, Args...)) { using next_type = Return (*)(Second, Args...); - return parser::consume_type(s, ms) - && parser::consume_string(s, ",") - && parser::consume_arg_list(s, ms, next_type()); + return parser::try_consume_type(s, ms) + && parser::try_consume_prefix(s, ",") + && parser::try_consume_arg_list(s, ms, next_type()); } template - bool consume_arg_list(boost::core::string_view& s, const mangled_storage_impl& ms, Return (*)()) { - return parser::consume_type(s, ms); + bool try_consume_arg_list(boost::core::string_view& s, const mangled_storage_impl& ms, Return (*)()) { + return parser::try_consume_type(s, ms); } class is_destructor_with_name { @@ -185,13 +184,13 @@ namespace parser { : dtor_name_(dtor_name) {} inline bool operator()(boost::core::string_view s) const { - if (!parser::consume_visibility(s)) { + if (!parser::try_consume_visibility(s)) { return false; } - parser::consume_string(s, " virtual"); + parser::try_consume_prefix(s, " virtual"); - return parser::consume_thiscall(s) - && parser::consume_string(s, dtor_name_) + return parser::try_consume_thiscall(s) + && parser::try_consume_prefix(s, dtor_name_) && parser::ignore_ptrs(s) && s.empty(); } @@ -211,12 +210,12 @@ namespace parser { : variable_name_(variable_name), ms_(ms) {} inline bool operator()(boost::core::string_view s) const { - if (parser::consume_visibility(s) && !parser::consume_string(s, " static ")) { + if (parser::try_consume_visibility(s) && !parser::try_consume_prefix(s, " static ")) { return false; } - return parser::consume_type(s, ms_) - && parser::consume_string(s, variable_name_) + return parser::try_consume_type(s, ms_) + && parser::try_consume_prefix(s, variable_name_) && s.empty(); } @@ -235,12 +234,12 @@ namespace parser { : ctor_name_(ctor_name), ms_(ms) {} inline bool operator()(boost::core::string_view s) const { - return parser::consume_visibility(s) - && parser::consume_thiscall(s) - && parser::consume_string(s, ctor_name_) - && parser::consume_string(s, "(") - && parser::consume_arg_list(s, ms_, Signature()) - && parser::consume_string(s, ")") + return parser::try_consume_visibility(s) + && parser::try_consume_thiscall(s) + && parser::try_consume_prefix(s, ctor_name_) + && parser::try_consume_prefix(s, "(") + && parser::try_consume_arg_list(s, ms_, Signature()) + && parser::try_consume_prefix(s, ")") && parser::ignore_ptrs(s) && s.empty(); } @@ -263,21 +262,21 @@ namespace parser { : function_name_(function_name), ms_(ms) {} inline bool operator()(boost::core::string_view s) const { - if (parser::consume_visibility(s) && !parser::consume_string(s, " static ")) { + if (parser::try_consume_visibility(s) && !parser::try_consume_prefix(s, " static ")) { return false; } - if (!parser::consume_type(s, ms_)) { + if (!parser::try_consume_type(s, ms_)) { return false; } - parser::consume_string(s, " "); + parser::try_consume_prefix(s, " "); using Signature = Result(*)(Args...); - return parser::consume_string(s, "__cdecl ") - && parser::consume_string(s, function_name_) - && parser::consume_string(s, "(") - && parser::consume_arg_list(s, ms_, Signature()) - && parser::consume_string(s, ")") + return parser::try_consume_prefix(s, "__cdecl ") + && parser::try_consume_prefix(s, function_name_) + && parser::try_consume_prefix(s, "(") + && parser::try_consume_arg_list(s, ms_, Signature()) + && parser::try_consume_prefix(s, ")") && parser::ignore_ptrs(s) && s.empty(); } @@ -300,57 +299,38 @@ namespace parser { : function_name_(function_name), ms_(ms) {} inline bool operator()(boost::core::string_view s) const { - if (!parser::consume_visibility(s)) { - return false; - } - parser::consume_string(s, " virtual"); - - if (!parser::consume_string(s, " ")) { - return false; - } - - if (!parser::consume_type(s, ms_)) { - return false; - } - - if (!parser::consume_thiscall(s)) { - return false; - } - if (!parser::consume_type::type>(s, ms_)) { - return false; - } - if (!parser::consume_string(s, "::")) { - return false; - } - if (!parser::consume_string(s, function_name_)) { - return false; - } - if (!parser::consume_string(s, "(")) { + if (!parser::try_consume_visibility(s)) { return false; } + parser::try_consume_prefix(s, " virtual"); using Signature = Result(*)(Args...); - if (!parser::consume_arg_list(s, ms_, Signature())) { - return false; - } - - if (!parser::consume_string(s, ")")) { + const bool is_name_and_args_ok = parser::try_consume_prefix(s, " ") + && parser::try_consume_type(s, ms_) + && parser::try_consume_thiscall(s) + && parser::try_consume_type::type>(s, ms_) + && parser::try_consume_prefix(s, "::") + && parser::try_consume_prefix(s, function_name_) + && parser::try_consume_prefix(s, "(") + && parser::try_consume_arg_list(s, ms_, Signature()) + && parser::try_consume_prefix(s, ")"); + if (!is_name_and_args_ok) { return false; } if (std::is_const::value) { - if (!parser::consume_string(s, "const ")) { + if (!parser::try_consume_prefix(s, "const ")) { return false; } } if (std::is_volatile::value) { - if (!parser::consume_string(s, "volatile ")) { + if (!parser::try_consume_prefix(s, "volatile ")) { return false; } } - parser::consume_ptrs(s); + parser::ignore_ptrs(s); return s.empty(); } @@ -358,8 +338,7 @@ namespace parser { return (*this)(boost::core::string_view(e.demangled.data(), e.demangled.size())); } }; -} - +} // namespace parser template std::string mangled_storage_impl::get_variable(const std::string &name) const { @@ -478,7 +457,6 @@ std::vector mangled_storage_impl::get_related() const { return ret; } - }}} #endif /* BOOST_DLL_DETAIL_DEMANGLING_MSVC_HPP_ */ diff --git a/test/cpp_mangling.cpp b/test/cpp_mangling.cpp index 749d543c..34ba9901 100644 --- a/test/cpp_mangling.cpp +++ b/test/cpp_mangling.cpp @@ -48,31 +48,31 @@ int main(int argc, char* argv[]) { void(*ptr0)(int) = nullptr; boost::core::string_view s = "integer"; - BOOST_TEST(parser::consume_arg_list(s, ms, ptr0)); + BOOST_TEST(parser::try_consume_arg_list(s, ms, ptr0)); BOOST_TEST_EQ(s, "eger"); } { void(*ptr1)(int) = nullptr; boost::core::string_view s = "int"; - BOOST_TEST(parser::consume_arg_list(s, ms, ptr1)); + BOOST_TEST(parser::try_consume_arg_list(s, ms, ptr1)); BOOST_TEST(s.empty()); } { void(*ptr2)() = nullptr; boost::core::string_view s = "void"; - BOOST_TEST(parser::consume_arg_list(s, ms, ptr2)); + BOOST_TEST(parser::try_consume_arg_list(s, ms, ptr2)); BOOST_TEST(s.empty()); } { void(*ptr3)(int,int) = nullptr; boost::core::string_view s = "int,int"; - BOOST_TEST(parser::consume_arg_list(s, ms, ptr3)); + BOOST_TEST(parser::try_consume_arg_list(s, ms, ptr3)); BOOST_TEST(s.empty()); } { void(*ptr4)(int,int,int) = nullptr; boost::core::string_view s = "int,int,int"; - BOOST_TEST(parser::consume_arg_list(s, ms, ptr4)); + BOOST_TEST(parser::try_consume_arg_list(s, ms, ptr4)); BOOST_TEST(s.empty()); }