From bb3253220bec0aeb30562822ec260bb1c2e6223a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Sat, 27 May 2017 11:11:54 +0200 Subject: [PATCH 1/2] Add formatting options to code generation --- include/cppast/code_generator.hpp | 53 ++++++++++-- src/code_generator.cpp | 133 ++++++++++++++++++------------ src/cpp_type.cpp | 73 +++++++++++----- test/code_generator.cpp | 67 +++++++++++++-- 4 files changed, 238 insertions(+), 88 deletions(-) diff --git a/include/cppast/code_generator.hpp b/include/cppast/code_generator.hpp index d0cd653..a224119 100644 --- a/include/cppast/code_generator.hpp +++ b/include/cppast/code_generator.hpp @@ -119,6 +119,27 @@ namespace cppast { } whitespace{}; + /// Flags that control the code formatting. + /// + /// If a flag is set, it adds additional whitespace. + /// If no flags are set, it will only add the whitespace necessary to separate tokens. + enum class formatting_flags + { + brace_nl, //< Set to put the opening braces on a new line. + brace_ws, //< Set to put the opening brace at the end of the line after whitespace. + + ptr_ref_var, //< Set to put pointers and references at the variable name (default is type). + + comma_ws, //< Set to put whitespace after a comma. + bracket_ws, //< Set to put whitespace inside brackets. + operator_ws, //< Set to put whitespace around operators. + + _flag_set_size, //< \exclude + }; + + /// A set of formatting flags. + using formatting = type_safe::flag_set; + /// Base class to control the code generation. /// /// Inherit from it to customize how a [cppast::cpp_entity]() is printed @@ -127,8 +148,10 @@ namespace cppast { public: code_generator(const code_generator&) = delete; + code_generator& operator=(const code_generator&) = delete; - virtual ~code_generator() noexcept = default; + + virtual ~code_generator() noexcept = default; /// Flags that control the generation. enum generation_flags @@ -178,6 +201,7 @@ namespace cppast } output(const output&) = delete; + output& operator=(const output&) = delete; /// \returns Whether or not the `on_XXX` function returned something other than `exclude`. @@ -189,17 +213,23 @@ namespace cppast } /// \returns The generation options. - generation_options options() const noexcept + generation_options options() const { return options_; } /// \returns The generation options for the given entity. - generation_options options(const cpp_entity& e) const noexcept + generation_options options(const cpp_entity& e) const { return gen_->do_get_options(e); } + /// \returns The formatting. + cppast::formatting formatting() const + { + return gen_->do_get_formatting(); + } + /// \returns Whether or not the definition should be generated as well. bool generate_definition() const noexcept { @@ -230,6 +260,13 @@ namespace cppast gen_->do_unindent(); } + /// \effects Calls `func(*this)`. + const output& operator<<(void (*func)(const output&)) const + { + func(*this); + return *this; + } + /// \effects Calls `do_write_keyword()`. const output& operator<<(const keyword& k) const { @@ -337,6 +374,13 @@ namespace cppast code_generator() noexcept = default; private: + /// \returns The formatting options that should be used. + /// The base class version has no flags set. + virtual formatting do_get_formatting() const + { + return {}; + } + /// \returns The generation options for that entity. /// The base class version always returns no special options. virtual generation_options do_get_options(const cpp_entity& e) @@ -451,9 +495,6 @@ namespace cppast /// Generates code for the given entity. /// /// How the code is generated is customized by the generator. - /// The implementation will write whitespace only where necessary, - /// but a newline after each entity. - /// This allows customization of formatting. /// /// \returns Whether or not any code was actually written. bool generate_code(code_generator& generator, const cpp_entity& e); diff --git a/src/code_generator.cpp b/src/code_generator.cpp index 17fd928..8945cb5 100644 --- a/src/code_generator.cpp +++ b/src/code_generator.cpp @@ -28,16 +28,32 @@ using namespace cppast; namespace { - template - auto write_sep(code_generator::output& output, Sep s) -> decltype(output << s) + void opening_brace(const code_generator::output& output) { - return output << s; + if (output.formatting().is_set(formatting_flags::brace_nl)) + output << newl; + else if (output.formatting().is_set(formatting_flags::brace_ws)) + output << whitespace; + output << punctuation("{"); } - template - auto write_sep(code_generator::output& output, Sep s) -> decltype(s(output)) + void comma(const code_generator::output& output) { - return s(output); + output << punctuation(","); + if (output.formatting().is_set(formatting_flags::comma_ws)) + output << whitespace; + } + + void bracket_ws(const code_generator::output& output) + { + if (output.formatting().is_set(formatting_flags::bracket_ws)) + output << whitespace; + } + + void operator_ws(const code_generator::output& output) + { + if (output.formatting().is_set(formatting_flags::operator_ws)) + output << whitespace; } template @@ -47,7 +63,7 @@ namespace for (auto& child : cont) { if (need_sep) - write_sep(output, s); + output << s; need_sep = generate_code(*output.generator(), child); } return need_sep; @@ -73,7 +89,8 @@ namespace { output << preprocessor_token("#define") << whitespace << identifier(def.name()); if (def.is_function_like()) - output << preprocessor_token("(") << preprocessor_token(def.parameters().value()) + output << preprocessor_token("(") << bracket_ws + << preprocessor_token(def.parameters().value()) << bracket_ws << preprocessor_token(")"); if (!def.replacement().empty()) output << whitespace << preprocessor_token(def.replacement()) << newl; @@ -111,7 +128,7 @@ namespace output << keyword("extern") << whitespace << string_literal(linkage.name()); if (linkage.is_block()) { - output << punctuation("{"); + output << opening_brace; output.indent(); write_container(output, linkage, newl); @@ -136,7 +153,7 @@ namespace if (ns.is_inline()) output << keyword("inline") << whitespace; output << keyword("namespace") << whitespace << identifier(ns.name()); - output << punctuation("{"); + output << opening_brace; output.indent(); write_container(output, ns, newl); @@ -152,8 +169,8 @@ namespace code_generator::output output(type_safe::ref(generator), type_safe::ref(alias), false); if (output) { - output << keyword("namespace") << whitespace << identifier(alias.name()) - << punctuation("="); + output << keyword("namespace") << whitespace << identifier(alias.name()) << operator_ws + << punctuation("=") << operator_ws; if (output.options() & code_generator::exclude_target) output.excluded(alias); else @@ -188,8 +205,8 @@ namespace code_generator::output output(type_safe::ref(generator), type_safe::ref(alias), false); if (output) { - output << keyword("using") << whitespace << identifier(alias.name()) - << punctuation("="); + output << keyword("using") << whitespace << identifier(alias.name()) << operator_ws + << punctuation("=") << operator_ws; if (output.options() & code_generator::exclude_target) output.excluded(alias); else @@ -207,7 +224,7 @@ namespace output << identifier(value.name()); if (value.value()) { - output << punctuation("="); + output << operator_ws << punctuation("=") << operator_ws; detail:: write_expression(output, value.value() @@ -228,16 +245,16 @@ namespace output << whitespace << identifier(e.semantic_scope()) << identifier(e.name()); if (e.has_explicit_type()) { - output << newl << punctuation(":"); + output << newl << punctuation(":") << operator_ws; detail::write_type(output, e.underlying_type(), ""); } if (output.generate_definition() && e.is_definition()) { - output << punctuation("{"); + output << opening_brace; output.indent(); - auto need_sep = write_container(output, e, [](code_generator::output& out) { + auto need_sep = write_container(output, e, [](const code_generator::output& out) { out << punctuation(",") << newl; }); if (need_sep) @@ -298,7 +315,8 @@ namespace if (spec.arguments_exposed()) detail::write_template_arguments(output, spec.arguments()); else if (!spec.unexposed_arguments().empty()) - output << punctuation("<") << token_seq(spec.unexposed_arguments()) << punctuation(">"); + output << punctuation("<") << bracket_ws << token_seq(spec.unexposed_arguments()) + << bracket_ws << punctuation(">"); } void write_bases(code_generator& generator, code_generator::output& output, const cpp_class& c) @@ -310,10 +328,10 @@ namespace if (first && !output.options(base).is_set(code_generator::exclude)) { first = false; - output << newl << punctuation(":"); + output << newl << punctuation(":") << operator_ws; } else if (need_sep) - output << punctuation(","); + output << comma; need_sep = generate_base_class(generator, base); } } @@ -345,7 +363,7 @@ namespace else { write_bases(generator, output, c); - output << punctuation("{"); + output << opening_brace; output.indent(); auto need_sep = false; @@ -388,7 +406,7 @@ namespace if (var.default_value()) { - output << punctuation("="); + output << operator_ws << punctuation("=") << operator_ws; detail::write_expression(output, var.default_value().value()); } return static_cast(output); @@ -441,7 +459,8 @@ namespace if (var.is_mutable()) output << keyword("mutable") << whitespace; write_variable_base(output, var, var.name()); - output << punctuation(":") << int_literal(std::to_string(var.no_bits())); + output << operator_ws << punctuation(":") << operator_ws + << int_literal(std::to_string(var.no_bits())); output << punctuation(";") << newl; } return static_cast(output); @@ -457,15 +476,15 @@ namespace void write_function_parameters(code_generator::output& output, const cpp_function_base& base) { - output << punctuation("("); - auto need_sep = write_container(output, base.parameters(), punctuation(",")); + output << punctuation("(") << bracket_ws; + auto need_sep = write_container(output, base.parameters(), comma); if (base.is_variadic()) { if (need_sep) - output << punctuation(","); + output << comma; output << punctuation("..."); } - output << punctuation(")"); + output << bracket_ws << punctuation(")"); } void write_noexcept(code_generator::output& output, const cpp_function_base& base, bool need_ws) @@ -481,9 +500,9 @@ namespace output << keyword("noexcept"); else { - output << keyword("noexcept") << punctuation("("); + output << keyword("noexcept") << punctuation("(") << bracket_ws; detail::write_expression(output, cond); - output << punctuation(")"); + output << bracket_ws << punctuation(")"); } } @@ -495,15 +514,17 @@ namespace case cpp_function_declaration: case cpp_function_definition: if (is_pure_virtual) - output << punctuation("=") << int_literal("0"); + output << operator_ws << punctuation("=") << operator_ws << int_literal("0"); output << punctuation(";") << newl; break; case cpp_function_defaulted: - output << punctuation("=") << keyword("default") << punctuation(";") << newl; + output << operator_ws << punctuation("=") << operator_ws << keyword("default") + << punctuation(";") << newl; break; case cpp_function_deleted: - output << punctuation("=") << keyword("delete") << punctuation(";") << newl; + output << operator_ws << punctuation("=") << operator_ws << keyword("delete") + << punctuation(";") << newl; break; } } @@ -543,7 +564,7 @@ namespace if (!(output.options() & code_generator::exclude_return) && detail::is_complex_type(func.return_type())) { - output << punctuation("->"); + output << operator_ws << punctuation("->") << operator_ws; detail::write_type(output, func.return_type(), ""); } write_function_body(output, func, false); @@ -591,11 +612,11 @@ namespace case cpp_ref_none: break; case cpp_ref_lvalue: - output << punctuation("&"); + output << operator_ws << punctuation("&") << operator_ws; need_ws = false; break; case cpp_ref_rvalue: - output << punctuation("&&"); + output << operator_ws << punctuation("&&") << operator_ws; need_ws = false; break; } @@ -642,7 +663,7 @@ namespace if (!(output.options() & code_generator::exclude_return) && detail::is_complex_type(func.return_type())) { - output << punctuation("->"); + output << operator_ws << punctuation("->") << operator_ws; detail::write_type(output, func.return_type(), ""); } @@ -775,12 +796,12 @@ namespace { output << keyword(to_string(param.keyword())); if (param.is_variadic()) - output << punctuation("..."); + output << operator_ws << punctuation("..."); if (!param.name().empty()) output << whitespace << identifier(param.name()); if (param.default_type()) { - output << punctuation("="); + output << operator_ws << punctuation("=") << operator_ws; detail::write_type(output, param.default_type().value(), ""); } } @@ -796,7 +817,7 @@ namespace detail::write_type(output, param.type(), param.name(), param.is_variadic()); if (param.default_value()) { - output << punctuation("="); + output << operator_ws << punctuation("=") << operator_ws; detail::write_expression(output, param.default_value().value()); } } @@ -809,14 +830,16 @@ namespace code_generator::output output(type_safe::ref(generator), type_safe::ref(param), true); if (output) { - output << keyword("template") << punctuation("<"); + output << keyword("template") << operator_ws << punctuation("<") << bracket_ws; write_container(output, param.parameters(), punctuation(",")); - output << punctuation(">") << keyword(to_string(param.keyword())) << whitespace; + output << bracket_ws << punctuation(">") << operator_ws + << keyword(to_string(param.keyword())); if (param.is_variadic()) - output << punctuation("..."); - output << identifier(param.name()); + output << operator_ws << punctuation("..."); + output << whitespace << identifier(param.name()); if (param.default_template()) - output << punctuation("=") << param.default_template().value(); + output << operator_ws << punctuation("=") << operator_ws + << param.default_template().value(); } return static_cast(output); } @@ -825,7 +848,7 @@ namespace bool hide_if_empty) { if (!hide_if_empty) - output << keyword("template") << punctuation("<"); + output << keyword("template") << operator_ws << punctuation("<") << bracket_ws; auto need_sep = false; auto first = hide_if_empty; @@ -835,15 +858,15 @@ namespace && !output.options(*templ.parameters().begin()).is_set(code_generator::exclude)) { first = false; - output << keyword("template") << punctuation("<"); + output << keyword("template") << operator_ws << punctuation("<") << bracket_ws; } else if (need_sep) - output << punctuation(","); + output << comma; need_sep = generate_code(*output.generator(), param); } if (!hide_if_empty || need_sep) - output << punctuation(">") << newl; + output << bracket_ws << punctuation(">") << newl; } bool generate_alias_template(code_generator& generator, const cpp_alias_template& alias) @@ -922,10 +945,10 @@ namespace code_generator::output output(type_safe::ref(generator), type_safe::ref(assert), false); if (output) { - output << keyword("static_assert") << punctuation("("); + output << keyword("static_assert") << punctuation("(") << bracket_ws; detail::write_expression(output, assert.expression()); - output << punctuation(",") << string_literal('"' + assert.message() + '"'); - output << punctuation(");") << newl; + output << comma << string_literal('"' + assert.message() + '"'); + output << bracket_ws << punctuation(");") << newl; } return static_cast(output); } @@ -1012,12 +1035,12 @@ void detail::write_template_arguments(code_generator::output& if (arguments.size() == 0u) return; - output << punctuation("<"); + output << punctuation("<") << bracket_ws; auto need_sep = false; for (auto& arg : arguments) { if (need_sep) - output << punctuation(","); + output << comma; else need_sep = true; @@ -1030,5 +1053,5 @@ void detail::write_template_arguments(code_generator::output& else DEBUG_UNREACHABLE(detail::assert_handler{}); } - output << punctuation(">"); + output << bracket_ws << punctuation(">"); } diff --git a/src/cpp_type.cpp b/src/cpp_type.cpp index f675ac9..efd8d7a 100644 --- a/src/cpp_type.cpp +++ b/src/cpp_type.cpp @@ -190,6 +190,25 @@ bool detail::is_complex_type(const cpp_type& type) noexcept namespace { + void comma(const code_generator::output& output) + { + output << punctuation(","); + if (output.formatting().is_set(formatting_flags::comma_ws)) + output << whitespace; + } + + void bracket_ws(const code_generator::output& output) + { + if (output.formatting().is_set(formatting_flags::bracket_ws)) + output << whitespace; + } + + void operator_ws(const code_generator::output& output) + { + if (output.formatting().is_set(formatting_flags::operator_ws)) + output << whitespace; + } + void write_builtin(code_generator::output& output, const cpp_builtin_type& type) { output << keyword(to_string(type.builtin_type_kind())); @@ -207,14 +226,15 @@ namespace void write_decltype(code_generator::output& output, const cpp_decltype_type& type) { - output << keyword("decltype") << punctuation("("); + output << keyword("decltype") << punctuation("(") << bracket_ws; detail::write_expression(output, type.expression()); - output << punctuation(")"); + output << bracket_ws << punctuation(")"); } void write_decltype_auto(code_generator::output& output, const cpp_decltype_auto_type&) { - output << keyword("decltype") << punctuation("(") << keyword("auto") << punctuation(")"); + output << keyword("decltype") << punctuation("(") << bracket_ws << keyword("auto") + << bracket_ws << punctuation(")"); } void write_cv_qualified_prefix(code_generator::output& output, @@ -223,7 +243,7 @@ namespace detail::write_type_prefix(output, type.type()); if (is_direct_complex(type.type())) - output << punctuation("("); + output << punctuation("(") << bracket_ws; if (is_const(type.cv_qualifier())) output << whitespace << keyword("const"); @@ -235,7 +255,7 @@ namespace const cpp_cv_qualified_type& type) { if (is_direct_complex(type.type())) - output << punctuation(")"); + output << bracket_ws << punctuation(")"); detail::write_type_suffix(output, type.type()); } @@ -248,8 +268,11 @@ namespace void write_pointer_prefix(code_generator::output& output, const cpp_pointer_type& type) { detail::write_type_prefix(output, type.pointee()); + if (pointer_requires_paren(type)) - output << punctuation("("); + output << punctuation("(") << bracket_ws; + else if (output.formatting().is_set(formatting_flags::ptr_ref_var)) + output << whitespace; output << punctuation("*"); } @@ -257,15 +280,18 @@ namespace void write_pointer_suffix(code_generator::output& output, const cpp_pointer_type& type) { if (pointer_requires_paren(type)) - output << punctuation(")"); + output << bracket_ws << punctuation(")"); detail::write_type_suffix(output, type.pointee()); } void write_reference_prefix(code_generator::output& output, const cpp_reference_type& type) { detail::write_type_prefix(output, type.referee()); + if (is_direct_complex(type.referee())) - output << punctuation("("); + output << punctuation("(") << bracket_ws; + else if (output.formatting().is_set(formatting_flags::ptr_ref_var)) + output << whitespace; if (type.reference_kind() == cpp_ref_lvalue) output << punctuation("&"); @@ -278,7 +304,7 @@ namespace void write_reference_suffix(code_generator::output& output, const cpp_reference_type& type) { if (is_direct_complex(type.referee())) - output << punctuation(")"); + output << bracket_ws << punctuation(")"); detail::write_type_suffix(output, type.referee()); } @@ -291,7 +317,11 @@ namespace { output << punctuation("["); if (type.size()) + { + output << bracket_ws; detail::write_expression(output, type.size().value()); + output << bracket_ws; + } output << punctuation("]"); detail::write_type_suffix(output, type.value_type()); } @@ -304,13 +334,13 @@ namespace template void write_parameters(code_generator::output& output, const T& type) { - output << punctuation("("); + output << punctuation("(") << bracket_ws; auto need_sep = false; for (auto& param : type.parameter_types()) { if (need_sep) - output << punctuation(","); + output << comma; else need_sep = true; detail::write_type_prefix(output, param); @@ -319,11 +349,11 @@ namespace if (type.is_variadic()) { if (need_sep) - output << punctuation(","); + output << comma; output << punctuation("..."); } - output << punctuation(")"); + output << bracket_ws << punctuation(")"); } void write_function_suffix(code_generator::output& output, const cpp_function_type& type) @@ -361,7 +391,7 @@ namespace { detail::write_type_prefix(output, type.return_type()); - output << punctuation("("); + output << punctuation("(") << bracket_ws; detail::write_type_prefix(output, strip_class_type(type.class_type(), nullptr, nullptr)); output << punctuation("::"); } @@ -369,7 +399,7 @@ namespace void write_member_function_suffix(code_generator::output& output, const cpp_member_function_type& type) { - output << punctuation(")"); + output << bracket_ws << punctuation(")"); write_parameters(output, type); auto cv = cpp_cv_none; @@ -384,9 +414,9 @@ namespace output << keyword("volatile"); if (ref == cpp_ref_lvalue) - output << punctuation("&"); + output << operator_ws << punctuation("&") << operator_ws; else if (ref == cpp_ref_rvalue) - output << punctuation("&&"); + output << operator_ws << punctuation("&&") << operator_ws; detail::write_type_suffix(output, type.return_type()); } @@ -395,7 +425,7 @@ namespace const cpp_member_object_type& type) { detail::write_type_prefix(output, type.object_type()); - output << punctuation("("); + output << punctuation("(") << bracket_ws; DEBUG_ASSERT(!detail::is_complex_type(type.class_type()), detail::assert_handler{}); detail::write_type_prefix(output, type.class_type()); output << punctuation("::"); @@ -403,7 +433,7 @@ namespace void write_member_object_suffix(code_generator::output& output, const cpp_member_object_type&) { - output << punctuation(")"); + output << bracket_ws << punctuation(")"); } void write_template_parameter(code_generator::output& output, @@ -419,7 +449,8 @@ namespace if (type.arguments_exposed()) detail::write_template_arguments(output, type.arguments()); else - output << punctuation("<") << token_seq(type.unexposed_arguments()) << punctuation(">"); + output << punctuation("<") << bracket_ws << token_seq(type.unexposed_arguments()) + << bracket_ws << punctuation(">"); } void write_dependent(code_generator::output& output, const cpp_dependent_type& type) @@ -511,6 +542,6 @@ void detail::write_type(code_generator::output& output, const cpp_type& type, st if (!name.empty()) output << whitespace << identifier(name); if (is_variadic) - output << punctuation("..."); + output << operator_ws << punctuation("...") << operator_ws; write_type_suffix(output, type); } diff --git a/test/code_generator.cpp b/test/code_generator.cpp index 8a63411..35513d9 100644 --- a/test/code_generator.cpp +++ b/test/code_generator.cpp @@ -10,11 +10,14 @@ using namespace cppast; TEST_CASE("code_generator") { - SECTION("basic") - { - // no need to check much here, as each entity check separately - // only write some file with equivalent code and synopsis - auto code = R"(using type=int; + // no need to check much here, as each entity check separately + auto code = R"(using type=int; + +type* var; + +templateclass... T> +struct templated{ +}; struct foo{ int a; @@ -36,10 +39,62 @@ enum class bar void func(int(*)(int)); extern void(* ptr)(int(*)(int))=&func;)"; + auto file = parse({}, "code_generator.cpp", code); - auto file = parse({}, "code_generator.cpp", code); + SECTION("basic") + { REQUIRE(get_code(*file) == code); } + SECTION("formatting") + { + auto synopsis = R"(using type = int; + +type* var; + +template