Skip to content

Commit 7aabe01

Browse files
hchokshimeta-codesync[bot]
authored andcommitted
Migrate service include/type properties and cpp_includes to Whisker
Summary: Migrate several mstch service and program properties to Whisker: - `service:include_prefix` → `service:program.include_prefix` - `service:thrift_includes` → `service:program.thrift_includes` - `service:cpp_includes` → `service:program.cpp_includes` - `service:thrift_uri_or_service_name` → inline conditional with `string.empty?` - `program:cpp_includes` split into `program:cpp_includes` (IDL) + `program:extra_cpp_includes` (compiler options), to allow migrating `service:cpp_includes` (which only takes IDL includes) to use the `program` prototype Reviewed By: vitaut Differential Revision: D95884142 fbshipit-source-id: bb01d3892cdc772d15c4ece0dfee751b0075a117
1 parent 336c01a commit 7aabe01

14 files changed

+98
-108
lines changed

thrift/compiler/generate/t_mstch_cpp2_generator.cc

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,6 @@ class t_mstch_cpp2_generator : public t_mstch_generator {
563563
void generate_program() override;
564564
void fill_validator_visitors(ast_validator&) const override;
565565
static std::string get_cpp2_namespace(const t_program* program);
566-
static mstch::array cpp_includes(const t_program* program);
567566
static std::string include_prefix(
568567
const t_program* program, const compiler_options_map& options);
569568

@@ -796,6 +795,36 @@ class t_mstch_cpp2_generator : public t_mstch_generator {
796795
return to_array(
797796
program.get_includes_for_codegen(), proto.of<t_program>());
798797
});
798+
def.property("cpp_includes", [](const t_program& program) {
799+
// C++ includes from IDL file
800+
whisker::array::raw includes;
801+
if (program.language_includes().count("cpp")) {
802+
for (std::string include : program.language_includes().at("cpp")) {
803+
if (include.at(0) != '<') {
804+
include = fmt::format("\"{}\"", include);
805+
}
806+
includes.emplace_back(std::move(include));
807+
}
808+
}
809+
return whisker::make::array(std::move(includes));
810+
});
811+
def.property("extra_cpp_includes", [this](const t_program&) {
812+
// C++ includes from compiler options
813+
std::optional<std::string_view> extra_includes_option =
814+
get_compiler_option("includes");
815+
if (extra_includes_option.value_or("").empty()) {
816+
return whisker::make::array();
817+
}
818+
std::vector<std::string> extra_includes;
819+
boost::split(extra_includes, extra_includes_option.value(), [](char c) {
820+
return c == ':';
821+
});
822+
whisker::array::raw result;
823+
for (std::string& s : extra_includes) {
824+
result.emplace_back(std::move(s));
825+
}
826+
return whisker::make::array(std::move(result));
827+
});
799828
return std::move(def).make();
800829
}
801830

@@ -1703,8 +1732,7 @@ class cpp_mstch_program : public mstch_program {
17031732
sm_(sm) {
17041733
register_methods(
17051734
this,
1706-
{{"program:cpp_includes", &cpp_mstch_program::cpp_includes},
1707-
{"program:transitive_schema_initializers",
1735+
{{"program:transitive_schema_initializers",
17081736
&cpp_mstch_program::transitive_schema_initializers},
17091737
{"program:num_transitive_thrift_includes",
17101738
&cpp_mstch_program::num_transitive_thrift_includes},
@@ -1718,19 +1746,6 @@ class cpp_mstch_program : public mstch_program {
17181746
std::string get_program_namespace(const t_program* program) override {
17191747
return t_mstch_cpp2_generator::get_cpp2_namespace(program);
17201748
}
1721-
1722-
mstch::node cpp_includes() {
1723-
mstch::array includes = t_mstch_cpp2_generator::cpp_includes(program_);
1724-
if (auto it = context_.options->find("includes");
1725-
it != context_.options->end()) {
1726-
std::vector<std::string> extra_includes;
1727-
boost::split(extra_includes, it->second, [](char c) { return c == ':'; });
1728-
for (auto& include : extra_includes) {
1729-
includes.emplace_back(std::move(include));
1730-
}
1731-
}
1732-
return includes;
1733-
}
17341749
/**
17351750
* To reduce build time, the generated constants code only includes the
17361751
* headers for direct thrift includes in the .cpp file and not in the .h
@@ -1908,46 +1923,23 @@ class cpp_mstch_service : public mstch_service {
19081923
register_methods(
19091924
this,
19101925
{
1911-
{"service:include_prefix", &cpp_mstch_service::include_prefix},
1912-
{"service:thrift_includes", &cpp_mstch_service::thrift_includes},
1913-
{"service:cpp_includes", &cpp_mstch_service::cpp_includes},
19141926
{"service:parent_service_cpp_name",
19151927
&cpp_mstch_service::parent_service_cpp_name},
19161928
{"service:parent_service_qualified_name",
19171929
&cpp_mstch_service::parent_service_qualified_name},
1918-
{"service:thrift_uri_or_service_name",
1919-
&cpp_mstch_service::thrift_uri_or_service_name},
19201930
});
19211931

19221932
const auto all_functions = mstch_service::get_functions();
19231933
for (size_t id = split_id; id < all_functions.size(); id += split_count) {
19241934
split_functions_.push_back(all_functions[id]);
19251935
}
19261936
}
1927-
mstch::node cpp_includes() {
1928-
return t_mstch_cpp2_generator::cpp_includes(service_->program());
1929-
}
1930-
mstch::node include_prefix() {
1931-
return t_mstch_cpp2_generator::include_prefix(
1932-
service_->program(), *context_.options);
1933-
}
1934-
mstch::node thrift_includes() {
1935-
mstch::array a;
1936-
for (const auto* program :
1937-
service_->program()->get_includes_for_codegen()) {
1938-
a.emplace_back(make_mstch_program_cached(program, context_));
1939-
}
1940-
return a;
1941-
}
19421937
mstch::node parent_service_cpp_name() {
19431938
return cpp2::get_name(parent_service());
19441939
}
19451940
mstch::node parent_service_qualified_name() {
19461941
return cpp2::get_service_qualified_name(*parent_service());
19471942
}
1948-
mstch::node thrift_uri_or_service_name() {
1949-
return service_->uri().empty() ? parent_service_name() : service_->uri();
1950-
}
19511943

19521944
private:
19531945
const std::vector<const t_function*>& get_functions() const override {
@@ -2455,19 +2447,6 @@ std::string t_mstch_cpp2_generator::get_cpp2_namespace(
24552447
return cpp2::get_gen_namespace(*program);
24562448
}
24572449

2458-
mstch::array t_mstch_cpp2_generator::cpp_includes(const t_program* program) {
2459-
mstch::array a;
2460-
if (program->language_includes().count("cpp")) {
2461-
for (auto include : program->language_includes().at("cpp")) {
2462-
if (include.at(0) != '<') {
2463-
include = fmt::format("\"{}\"", include);
2464-
}
2465-
a.emplace_back(std::move(include));
2466-
}
2467-
}
2468-
return a;
2469-
}
2470-
24712450
std::string t_mstch_cpp2_generator::include_prefix(
24722451
const t_program* program, const compiler_options_map& options) {
24732452
const std::string& prefix = program->include_prefix();

thrift/compiler/generate/templates/cpp2/ServiceAsyncClient.cpp.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}}
1818
{{> Autogen}}
1919

20-
#include "{{service:include_prefix}}{{service:name}}AsyncClient.h"
20+
#include "{{service:program.include_prefix}}{{service:name}}AsyncClient.h"
2121

2222
#include <thrift/lib/cpp2/gen/client_cpp.h>
2323

thrift/compiler/generate/templates/cpp2/ServiceAsyncClient.h.mustache

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,31 @@
2020

2121
#include <thrift/lib/cpp2/gen/client_h.h>
2222

23-
#include "{{service:include_prefix}}{{service:program.name}}_types.h"
23+
#include "{{service:program.include_prefix}}{{service:program.name}}_types.h"
2424
{{#service:extends}}
25-
#if __has_include("{{service:include_prefix}}{{service:name}}AsyncClient.h")
26-
#include "{{service:include_prefix}}{{service:name}}AsyncClient.h"
25+
#if __has_include("{{service:program.include_prefix}}{{service:name}}AsyncClient.h")
26+
#include "{{service:program.include_prefix}}{{service:name}}AsyncClient.h"
2727
#else
28-
#include "{{service:include_prefix}}{{service:program.name}}_clients.h"
28+
#include "{{service:program.include_prefix}}{{service:program.name}}_clients.h"
2929
#endif
3030
{{/service:extends}}
31-
{{#service:thrift_includes}}
32-
#include "{{program:include_prefix}}{{program:name}}_types.h"
33-
{{/service:thrift_includes}}
34-
{{#each service:cpp_includes as |cpp_include|}}
31+
{{#each service:program.thrift_includes as |included|}}
32+
#include "{{included.include_prefix}}{{included.name}}_types.h"
33+
{{/each}}
34+
{{#each service:program.cpp_includes as |cpp_include|}}
3535
#include {{cpp_include}}
36-
{{/each}}
37-
{{#if (or service:streams? service:interactions?)}}
36+
{{/each}}
37+
{{#if (or service:streams? service:interactions?)}}
3838
#include <thrift/lib/cpp2/async/ClientBufferedStream.h>
3939
#include <thrift/lib/cpp2/async/ClientStreamInterceptorContext.h>
40-
{{/if (or service:streams? service:interactions?)}}
41-
{{#if (or service:sinks? service:interactions?)}}
40+
{{/if (or service:streams? service:interactions?)}}
41+
{{#if (or service:sinks? service:interactions?)}}
4242
#include <thrift/lib/cpp2/async/ClientSinkBridge.h>
4343
#include <thrift/lib/cpp2/async/Sink.h>
44-
{{/if (or service:sinks? service:interactions?)}}
45-
{{#if (or service:self.has_bidirectional_streams? service:interactions?)}}
44+
{{/if (or service:sinks? service:interactions?)}}
45+
{{#if (or service:self.has_bidirectional_streams? service:interactions?)}}
4646
#include <thrift/lib/cpp2/async/BiDiStream.h>
47-
{{/if (or service:self.has_bidirectional_streams? service:interactions?)}}
47+
{{/if (or service:self.has_bidirectional_streams? service:interactions?)}}
4848

4949
namespace apache { namespace thrift {
5050
class Cpp2RequestContext;

thrift/compiler/generate/templates/cpp2/module_clients.h.mustache

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@
2626
#include "{{program:include_prefix}}{{program:name}}_clients.h"
2727
{{/program:thrift_includes}}
2828

29-
{{#each program:cpp_includes as |cpp_include|}}
29+
{{#each program:cpp_includes as |cpp_include|}}
3030
// cpp_include's
3131
#include {{cpp_include}}
32-
{{/each}}
32+
{{/each}}
33+
{{#each program:extra_cpp_includes as |cpp_include|}}
34+
// cpp_include's
35+
#include {{cpp_include}}
36+
{{/each}}
3337
{{/program}}
3438

3539
{{#if (or any_sinks? any_interactions?)}}

thrift/compiler/generate/templates/cpp2/module_clients_out_of_line.h.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
#pragma once
2020

2121
{{#services}}
22-
#include "{{service:include_prefix}}{{service:name}}AsyncClient.h"
22+
#include "{{service:program.include_prefix}}{{service:name}}AsyncClient.h"
2323
{{/services}}

thrift/compiler/generate/templates/cpp2/module_handlers.h.mustache

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@
2626
#include "{{program:include_prefix}}{{program:name}}_handlers.h"
2727
{{/program:thrift_includes}}
2828

29-
{{#each program:cpp_includes as |cpp_include|}}
29+
{{#each program:cpp_includes as |cpp_include|}}
3030
// cpp_include's
3131
#include {{cpp_include}}
32-
{{/each}}
32+
{{/each}}
33+
{{#each program:extra_cpp_includes as |cpp_include|}}
34+
// cpp_include's
35+
#include {{cpp_include}}
36+
{{/each}}
3337
{{/program}}
3438

3539
{{#if (or any_sinks? any_interactions?)}}

thrift/compiler/generate/templates/cpp2/module_handlers_out_of_line.h.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
#pragma once
2020

2121
{{#services}}
22-
#include "{{service:include_prefix}}{{service:name}}.h"
22+
#include "{{service:program.include_prefix}}{{service:name}}.h"
2323
{{/services}}

thrift/compiler/generate/templates/cpp2/module_types.h.mustache

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@
3131
#include "{{program:include_prefix}}{{program:name}}_constants.h"
3232
{{/if program:const_referenced_in_field_default?}}
3333
{{/program:thrift_includes}}
34-
{{#each program:cpp_includes as |cpp_include|}}
34+
{{#each program:cpp_includes as |cpp_include|}}
3535
#include {{cpp_include}}
36-
{{/each}}
36+
{{/each}}
37+
{{#each program:extra_cpp_includes as |cpp_include|}}
38+
#include {{cpp_include}}
39+
{{/each}}
3740

3841
{{#program:structs?}}
3942
namespace apache::thrift {

thrift/compiler/generate/templates/cpp2/service.cpp.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
{{#service}}
1919
{{> Autogen}}
2020

21-
#include "{{service:include_prefix}}{{service:name}}.h"
22-
#include "{{service:include_prefix}}{{service:name}}.tcc"
23-
#include "{{service:include_prefix}}{{service:program.name}}_types.h"
21+
#include "{{service:program.include_prefix}}{{service:name}}.h"
22+
#include "{{service:program.include_prefix}}{{service:name}}.tcc"
23+
#include "{{service:program.include_prefix}}{{service:program.name}}_types.h"
2424
{{#service:has_service_schema}}
25-
#include "{{service:include_prefix}}{{service:program.name}}_constants.h"
25+
#include "{{service:program.include_prefix}}{{service:program.name}}_constants.h"
2626
{{/service:has_service_schema}}
2727
#include <thrift/lib/cpp2/gen/service_cpp.h>
2828

thrift/compiler/generate/templates/cpp2/service.h.mustache

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,34 @@
2020

2121
#include <thrift/lib/cpp2/gen/service_h.h>
2222

23-
#include "{{service:include_prefix}}{{service:name}}AsyncClient.h"
24-
#include "{{service:include_prefix}}{{service:program.name}}_types.h"
23+
#include "{{service:program.include_prefix}}{{service:name}}AsyncClient.h"
24+
#include "{{service:program.include_prefix}}{{service:program.name}}_types.h"
2525
{{#service:extends}}
26-
#if __has_include("{{service:include_prefix}}{{service:name}}.h")
27-
#include "{{service:include_prefix}}{{service:name}}.h"
26+
#if __has_include("{{service:program.include_prefix}}{{service:name}}.h")
27+
#include "{{service:program.include_prefix}}{{service:name}}.h"
2828
#else
29-
#include "{{service:include_prefix}}{{service:program.name}}_handlers.h"
29+
#include "{{service:program.include_prefix}}{{service:program.name}}_handlers.h"
3030
#endif
3131
{{/service:extends}}
32-
{{#service:thrift_includes}}
33-
#include "{{program:include_prefix}}{{program:name}}_types.h"
34-
{{/service:thrift_includes}}
35-
{{#each service:cpp_includes as |cpp_include|}}
32+
{{#each service:program.thrift_includes as |included|}}
33+
#include "{{included.include_prefix}}{{included.name}}_types.h"
34+
{{/each}}
35+
{{#each service:program.cpp_includes as |cpp_include|}}
3636
#include {{cpp_include}}
37-
{{/each}}
38-
{{#if (or service:streams? service:interactions?)}}
37+
{{/each}}
38+
{{#if (or service:streams? service:interactions?)}}
3939
#include <thrift/lib/cpp2/async/ServerStream.h>
40-
{{/if (or service:streams? service:interactions?)}}
41-
{{#if (or service:sinks? service:interactions?)}}
40+
{{/if (or service:streams? service:interactions?)}}
41+
{{#if (or service:sinks? service:interactions?)}}
4242
#include <thrift/lib/cpp2/async/Sink.h>
43-
{{/if (or service:sinks? service:interactions?)}}
44-
{{#if (or service:self.has_bidirectional_streams? service:interactions?)}}
43+
{{/if (or service:sinks? service:interactions?)}}
44+
{{#if (or service:self.has_bidirectional_streams? service:interactions?)}}
4545
#include <thrift/lib/cpp2/async/BiDiStream.h>
4646
#include <thrift/lib/cpp2/async/ServerBiDiStreamFactory.h>
47-
{{/if (or service:self.has_bidirectional_streams? service:interactions?)}}
48-
{{#service:cpp_requires_method_decorator?}}
47+
{{/if (or service:self.has_bidirectional_streams? service:interactions?)}}
48+
{{#if service:cpp_requires_method_decorator?}}
4949
#include <thrift/lib/cpp2/gen/module_method_decorator_h.h>
50-
{{/service:cpp_requires_method_decorator?}}
50+
{{/if service:cpp_requires_method_decorator?}}
5151

5252
namespace folly {
5353
class IOBuf;

0 commit comments

Comments
 (0)