Skip to content

Commit 98e709f

Browse files
committed
Fix return type having same name as function
1 parent 03495fb commit 98e709f

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

‎include/cppast/diagnostic.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace cppast
5050
/// \returns A source location where entity and file name is available.
5151
static source_location make_entity(std::string entity, std::string file)
5252
{
53-
return {std::move(entity), std::move(file), type_safe::nullopt};
53+
return {std::move(entity), std::move(file), type_safe::nullopt, type_safe::nullopt};
5454
}
5555

5656
/// \returns A possible string representation of the source location.

‎src/libclang/function_parser.cpp‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,19 @@ namespace
229229
bool is_friend = false;
230230
};
231231

232-
bool prefix_end(detail::cxtoken_stream& stream, const char* name, bool is_ctor)
232+
bool prefix_end(detail::cxtoken_stream& stream, const char* name, bool is_ctor_dtor)
233233
{
234234
auto cur = stream.cur();
235235
// name can have multiple tokens if it is an operator
236236
if (!detail::skip_if(stream, name, true))
237237
return false;
238238
else if (stream.peek() == "," || stream.peek() == ">" || stream.peek() == ">>")
239+
{
239240
// argument to template parameters
241+
stream.set_cur(cur);
240242
return false;
241-
else if (is_ctor)
243+
}
244+
else if (is_ctor_dtor)
242245
{
243246
// need to make sure it is not actually a class name
244247
if (stream.peek() == "::")
@@ -269,15 +272,22 @@ namespace
269272
else
270273
return true;
271274
}
275+
else if (std::strcmp(name, "operator") != 0 && stream.peek().kind() == CXToken_Identifier)
276+
{
277+
// can't be function name
278+
stream.set_cur(cur);
279+
return false;
280+
}
272281
else
273282
return true;
274283
}
275284

276-
prefix_info parse_prefix_info(detail::cxtoken_stream& stream, const char* name, bool is_ctor)
285+
prefix_info parse_prefix_info(detail::cxtoken_stream& stream, const char* name,
286+
bool is_ctor_dtor)
277287
{
278288
prefix_info result;
279289

280-
while (!stream.done() && !prefix_end(stream, name, is_ctor))
290+
while (!stream.done() && !prefix_end(stream, name, is_ctor_dtor))
281291
{
282292
if (detail::skip_if(stream, "constexpr"))
283293
result.is_constexpr = true;
@@ -811,7 +821,7 @@ std::unique_ptr<cpp_entity> detail::parse_cpp_destructor(const detail::parse_con
811821
detail::cxtokenizer tokenizer(context.tu, context.file, cur);
812822
detail::cxtoken_stream stream(tokenizer, cur);
813823

814-
auto prefix_info = parse_prefix_info(stream, "~", false);
824+
auto prefix_info = parse_prefix_info(stream, "~", true);
815825
DEBUG_ASSERT(!prefix_info.is_constexpr && !prefix_info.is_explicit, detail::assert_handler{});
816826

817827
auto name = std::string("~") + stream.get().c_str();

‎test/cpp_function.cpp‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ namespace ns
4848
4949
/// void l();
5050
void l();
51+
52+
using m = int;
5153
}
5254
5355
/// void ns::l();
@@ -56,6 +58,9 @@ void ns::l()
5658
// might confuse parser
5759
auto b = noexcept(g());
5860
}
61+
62+
/// ns::m m();
63+
ns::m m();
5964
)";
6065

6166
auto check_body = [](const cpp_function& func, cpp_function_body_kind kind) {
@@ -223,10 +228,22 @@ void ns::l()
223228
check_body(func, cpp_function_declaration);
224229
}
225230
}
231+
else if (func.name() == "m")
232+
{
233+
REQUIRE(equal_types(idx, func.return_type(),
234+
*cpp_user_defined_type::build(
235+
cpp_type_ref(cpp_entity_id(""), "ns::m"))));
236+
REQUIRE(count_children(func.parameters()) == 0u);
237+
REQUIRE(!func.is_variadic());
238+
REQUIRE(!func.noexcept_condition());
239+
REQUIRE(!func.is_constexpr());
240+
REQUIRE(func.storage_class() == cpp_storage_class_none);
241+
check_body(func, cpp_function_declaration);
242+
}
226243
else
227244
REQUIRE(false);
228245
});
229-
REQUIRE(count == 13u);
246+
REQUIRE(count == 14u);
230247
}
231248

232249
TEST_CASE("static cpp_function")

0 commit comments

Comments
 (0)