diff --git a/src/google/protobuf/compiler/annotation_test_util.cc b/src/google/protobuf/compiler/annotation_test_util.cc index cfc0f6c90a..99db5a2bec 100644 --- a/src/google/protobuf/compiler/annotation_test_util.cc +++ b/src/google/protobuf/compiler/annotation_test_util.cc @@ -137,7 +137,8 @@ const GeneratedCodeInfo::Annotation* FindAnnotationOnPath( bool AtLeastOneAnnotationMatchesSubstring( const std::string& file_content, const std::vector& annotations, - const std::string& expected_text) { + const std::string& expected_text, + absl::optional semantic) { for (std::vector::const_iterator i = annotations.begin(), e = annotations.end(); @@ -149,7 +150,7 @@ bool AtLeastOneAnnotationMatchesSubstring( return false; } if (file_content.substr(begin, end - begin) == expected_text) { - return true; + return !semantic || annotation->semantic() == *semantic; } } return false; diff --git a/src/google/protobuf/compiler/annotation_test_util.h b/src/google/protobuf/compiler/annotation_test_util.h index 58d3541bbc..6ac10357c1 100644 --- a/src/google/protobuf/compiler/annotation_test_util.h +++ b/src/google/protobuf/compiler/annotation_test_util.h @@ -97,11 +97,13 @@ const GeneratedCodeInfo::Annotation* FindAnnotationOnPath( const std::vector& path); // Returns true if at least one of the provided annotations covers a given -// substring in file_content. +// substring with the given semantic in file_content. bool AtLeastOneAnnotationMatchesSubstring( const std::string& file_content, const std::vector& annotations, - const std::string& expected_text); + const std::string& expected_text, + absl::optional expected_semantic = + absl::nullopt); // Returns true if the provided annotation covers a given substring in // file_content. diff --git a/src/google/protobuf/io/printer.cc b/src/google/protobuf/io/printer.cc index f2a0d8ca84..9030eb4dcf 100644 --- a/src/google/protobuf/io/printer.cc +++ b/src/google/protobuf/io/printer.cc @@ -364,7 +364,8 @@ absl::optional> Printer::GetSubstitutionRange( void Printer::Annotate(absl::string_view begin_varname, absl::string_view end_varname, absl::string_view file_path, - const std::vector& path) { + const std::vector& path, + absl::optional semantic) { if (options_.annotation_collector == nullptr) { return; } @@ -381,8 +382,8 @@ void Printer::Annotate(absl::string_view begin_varname, << " to " << end_varname; return; } - options_.annotation_collector->AddAnnotation(begin->first, end->second, - std::string(file_path), path); + options_.annotation_collector->AddAnnotation( + begin->first, end->second, std::string(file_path), path, semantic); } void Printer::WriteRaw(const char* data, size_t size) { diff --git a/src/google/protobuf/io/printer.h b/src/google/protobuf/io/printer.h index b217a706a2..ed400104f3 100644 --- a/src/google/protobuf/io/printer.h +++ b/src/google/protobuf/io/printer.h @@ -628,8 +628,10 @@ class PROTOBUF_EXPORT Printer { // Link a substitution variable emitted by the last call to Print to the // object described by descriptor. template - void Annotate(absl::string_view varname, const SomeDescriptor* descriptor) { - Annotate(varname, varname, descriptor); + void Annotate( + absl::string_view varname, const SomeDescriptor* descriptor, + absl::optional semantic = absl::nullopt) { + Annotate(varname, varname, descriptor, semantic); } // Link the output range defined by the substitution variables as emitted by @@ -637,8 +639,10 @@ class PROTOBUF_EXPORT Printer { // begins at begin_varname's value and ends after the last character of the // value substituted for end_varname. template - void Annotate(absl::string_view begin_varname, absl::string_view end_varname, - const Desc* descriptor); + void Annotate( + absl::string_view begin_varname, absl::string_view end_varname, + const Desc* descriptor, + absl::optional semantic = absl::nullopt); // Link a substitution variable emitted by the last call to Print to the file // with path file_name. @@ -656,7 +660,7 @@ class PROTOBUF_EXPORT Printer { return; } - Annotate(begin_varname, end_varname, file_name, {}); + Annotate(begin_varname, end_varname, file_name, {}, absl::nullopt); } // Indent text by `options.spaces_per_indent`; undone by Outdent(). @@ -717,7 +721,8 @@ class PROTOBUF_EXPORT Printer { // // `begin_varname` and `end_varname may` refer to the same variable. void Annotate(absl::string_view begin_varname, absl::string_view end_varname, - absl::string_view file_path, const std::vector& path); + absl::string_view file_path, const std::vector& path, + absl::optional semantic); // The core printing implementation. There are three public entry points, // which enable different slices of functionality that are controlled by the @@ -1072,14 +1077,16 @@ void Printer::Print(absl::string_view text, const Args&... args) { template void Printer::Annotate(absl::string_view begin_varname, - absl::string_view end_varname, const Desc* descriptor) { + absl::string_view end_varname, const Desc* descriptor, + absl::optional semantic) { if (options_.annotation_collector == nullptr) { return; } std::vector path; descriptor->GetLocationPath(&path); - Annotate(begin_varname, end_varname, descriptor->file()->name(), path); + Annotate(begin_varname, end_varname, descriptor->file()->name(), path, + semantic); } template