Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions hpb_generator/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ void WriteForwardDecls(const google::protobuf::FileDescriptor* file, Context& ct
WriteTypedefForwardingHeader(file, this_file_messages, ctx);
}

void WriteHeader(const google::protobuf::FileDescriptor* file, Context& ctx) {
void WriteHeader(const google::protobuf::FileDescriptor* file, Context& ctx,
absl::string_view info_path) {
if (ctx.options().backend == Backend::CPP) {
EmitFileWarning(file, ctx);

Expand All @@ -80,6 +81,15 @@ void WriteHeader(const google::protobuf::FileDescriptor* file, Context& ctx) {
ctx.Emit(
"#include \"hpb/internal/os_macros_undef.inc\"\n");

if (!info_path.empty()) {
ctx.Emit({{"info_path", std::string(info_path)}},
R"cc(
#ifdef KYTHE_IS_RUNNING
#pragma kythe_metadata "$info_path$"
#endif // KYTHE_IS_RUNNING
)cc");
}

const std::vector<const google::protobuf::Descriptor*> this_file_messages =
SortedMessages(file);
const std::vector<const google::protobuf::FieldDescriptor*>
Expand Down Expand Up @@ -171,6 +181,15 @@ void WriteHeader(const google::protobuf::FileDescriptor* file, Context& ctx) {
ctx.Emit(
"#include \"hpb/internal/os_macros_undef.inc\"\n");

if (!info_path.empty()) {
ctx.Emit({{"info_path", std::string(info_path)}},
R"cc(
#ifdef KYTHE_IS_RUNNING
#pragma kythe_metadata "$info_path$"
#endif // KYTHE_IS_RUNNING
)cc");
}

const std::vector<const google::protobuf::Descriptor*> this_file_messages =
SortedMessages(file);
const std::vector<const google::protobuf::FieldDescriptor*> this_file_exts =
Expand Down Expand Up @@ -357,14 +376,20 @@ bool Generator::Generate(const google::protobuf::FileDescriptor* file,
annotations.get());
}

std::string meta_filename = "";
if (annotate_headers) {
meta_filename =
absl::StrCat(compiler::StripProto(file->name()), ".hpb.h.meta");
}

// Write model.hpb.h
Options options = {.backend = backend,
.strip_feature_includes = strip_nonfunctional_codegen,
.annotation_collector = annotation_collector.get()};
std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> header_output_stream(
context->Open(CppHeaderFilename(file)));
Context hdr_ctx(file, header_output_stream.get(), options);
WriteHeader(file, hdr_ctx);
WriteHeader(file, hdr_ctx, meta_filename);

// Write model.hpb.cc
std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> cc_output_stream(
Expand Down
12 changes: 12 additions & 0 deletions hpb_generator/tests/metadata_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ TEST_F(HpbMetadataTest, AnnotatesStringSemantics) {
});
}

TEST_F(HpbMetadataTest, GeneratesMetadataPragma) {
FileDescriptorProto file;
GeneratedCodeInfo info;
std::string hpb_h;
atu::AddFile("test.proto", kSmallTestFile);
EXPECT_TRUE(CaptureMetadata("test.proto", file, hpb_h, info));

EXPECT_THAT(hpb_h, ::testing::HasSubstr("#ifdef KYTHE_IS_RUNNING"));
EXPECT_THAT(hpb_h, ::testing::HasSubstr(
"#pragma kythe_metadata \"test.hpb.h.meta\""));
}

} // namespace
} // namespace hpb
} // namespace compiler
Expand Down
Loading