Skip to content
17 changes: 13 additions & 4 deletions include/cppast/cpp_type_alias.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,35 @@ class cpp_type_alias final : public cpp_entity

/// \returns A newly created and registered type alias.
static std::unique_ptr<cpp_type_alias> build(const cpp_entity_index& idx, cpp_entity_id id,
std::string name, std::unique_ptr<cpp_type> type);
std::string name, std::unique_ptr<cpp_type> type,
bool use_c_style = false);

/// \returns A newly created type alias that isn't registered.
/// \notes This function is intendend for templated type aliases.
static std::unique_ptr<cpp_type_alias> build(std::string name, std::unique_ptr<cpp_type> type);
static std::unique_ptr<cpp_type_alias> build(std::string name, std::unique_ptr<cpp_type> type,
bool use_c_style = false);

/// \returns A reference to the aliased [cppast::cpp_type]().
const cpp_type& underlying_type() const noexcept
{
return *type_;
}

/// \returns Whether to generate C-style typedefs instead of C++ using statements
bool use_c_style() const noexcept
{
return use_c_style_;
}

private:
cpp_type_alias(std::string name, std::unique_ptr<cpp_type> type)
: cpp_entity(std::move(name)), type_(std::move(type))
cpp_type_alias(std::string name, std::unique_ptr<cpp_type> type, bool use_c_style)
: cpp_entity(std::move(name)), type_(std::move(type)), use_c_style_(use_c_style)
{}

cpp_entity_kind do_get_entity_kind() const noexcept override;

std::unique_ptr<cpp_type> type_;
bool use_c_style_;
};
} // namespace cppast

Expand Down
24 changes: 18 additions & 6 deletions src/code_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,25 @@ bool generate_type_alias(code_generator& generator, const cpp_type_alias& alias,
code_generator::output output(type_safe::ref(generator), type_safe::ref(alias), cur_access);
if (output)
{
output << keyword("using") << whitespace << identifier(alias.name()) << operator_ws
<< punctuation("=") << operator_ws;
if (output.options() & code_generator::exclude_target)
output.excluded(alias);
if (alias.use_c_style())
{
output << keyword("typedef") << whitespace;
if (output.options() & code_generator::exclude_target)
output.excluded(alias);
else
detail::write_type(output, alias.underlying_type(), "");
output << whitespace << identifier(alias.name()) << punctuation(";") << newl;
}
else
detail::write_type(output, alias.underlying_type(), "");
output << punctuation(";") << newl;
{
output << keyword("using") << whitespace << identifier(alias.name()) << operator_ws
<< punctuation("=") << operator_ws;
if (output.options() & code_generator::exclude_target)
output.excluded(alias);
else
detail::write_type(output, alias.underlying_type(), "");
output << punctuation(";") << newl;
}
}
return static_cast<bool>(output);
}
Expand Down
10 changes: 6 additions & 4 deletions src/cpp_type_alias.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ cpp_entity_kind cpp_type_alias::kind() noexcept

std::unique_ptr<cpp_type_alias> cpp_type_alias::build(const cpp_entity_index& idx, cpp_entity_id id,
std::string name,
std::unique_ptr<cpp_type> type)
std::unique_ptr<cpp_type> type,
bool use_c_style)
{
auto result = build(std::move(name), std::move(type));
auto result = build(std::move(name), std::move(type), use_c_style);
idx.register_forward_declaration(std::move(id), type_safe::cref(*result)); // not a definition
return result;
}

std::unique_ptr<cpp_type_alias> cpp_type_alias::build(std::string name,
std::unique_ptr<cpp_type> type)
std::unique_ptr<cpp_type> type,
bool use_c_style)
{
return std::unique_ptr<cpp_type_alias>(new cpp_type_alias(std::move(name), std::move(type)));
return std::unique_ptr<cpp_type_alias>(new cpp_type_alias(std::move(name), std::move(type), use_c_style));
}

cpp_entity_kind cpp_type_alias::do_get_entity_kind() const noexcept
Expand Down
1 change: 1 addition & 0 deletions src/libclang/libclang_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@ try
type_safe::ref(logger()),
type_safe::ref(idx),
detail::comment_context(preprocessed.comments),
c.use_c(),
false};
detail::visit_tu(tu, path.c_str(), [&](const CXCursor& cur) {
if (clang_getCursorKind(cur) == CXCursor_InclusionDirective)
Expand Down
1 change: 1 addition & 0 deletions src/libclang/parse_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace detail
type_safe::object_ref<const diagnostic_logger> logger;
type_safe::object_ref<const cpp_entity_index> idx;
comment_context comments;
bool use_c_style;
mutable bool error;
};

Expand Down
4 changes: 2 additions & 2 deletions src/libclang/type_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,11 +705,11 @@ std::unique_ptr<cpp_entity> detail::parse_cpp_type_alias(const detail::parse_con

std::unique_ptr<cpp_type_alias> result;
if (!clang_Cursor_isNull(template_cur))
result = cpp_type_alias::build(name.c_str(), std::move(type));
result = cpp_type_alias::build(name.c_str(), std::move(type), context.use_c_style);
else
{
result = cpp_type_alias::build(*context.idx, get_entity_id(cur), name.c_str(),
std::move(type));
std::move(type), context.use_c_style);
context.comments.match(*result, cur);
}

Expand Down