Internal Change

PiperOrigin-RevId: 530601400
pull/12733/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 11388e85fa
commit c78b0f10b7
  1. 5
      src/google/protobuf/compiler/annotation_test_util.cc
  2. 6
      src/google/protobuf/compiler/annotation_test_util.h
  3. 7
      src/google/protobuf/io/printer.cc
  4. 23
      src/google/protobuf/io/printer.h

@ -137,7 +137,8 @@ const GeneratedCodeInfo::Annotation* FindAnnotationOnPath(
bool AtLeastOneAnnotationMatchesSubstring(
const std::string& file_content,
const std::vector<const GeneratedCodeInfo::Annotation*>& annotations,
const std::string& expected_text) {
const std::string& expected_text,
absl::optional<GeneratedCodeInfo::Annotation::Semantic> semantic) {
for (std::vector<const GeneratedCodeInfo::Annotation*>::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;

@ -97,11 +97,13 @@ const GeneratedCodeInfo::Annotation* FindAnnotationOnPath(
const std::vector<int>& 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<const GeneratedCodeInfo::Annotation*>& annotations,
const std::string& expected_text);
const std::string& expected_text,
absl::optional<GeneratedCodeInfo::Annotation::Semantic> expected_semantic =
absl::nullopt);
// Returns true if the provided annotation covers a given substring in
// file_content.

@ -364,7 +364,8 @@ absl::optional<std::pair<size_t, size_t>> Printer::GetSubstitutionRange(
void Printer::Annotate(absl::string_view begin_varname,
absl::string_view end_varname,
absl::string_view file_path,
const std::vector<int>& path) {
const std::vector<int>& path,
absl::optional<AnnotationCollector::Semantic> 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) {

@ -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 <typename SomeDescriptor>
void Annotate(absl::string_view varname, const SomeDescriptor* descriptor) {
Annotate(varname, varname, descriptor);
void Annotate(
absl::string_view varname, const SomeDescriptor* descriptor,
absl::optional<AnnotationCollector::Semantic> 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 <typename Desc>
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<AnnotationCollector::Semantic> 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<int>& path);
absl::string_view file_path, const std::vector<int>& path,
absl::optional<AnnotationCollector::Semantic> 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 <typename Desc>
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<AnnotationCollector::Semantic> semantic) {
if (options_.annotation_collector == nullptr) {
return;
}
std::vector<int> path;
descriptor->GetLocationPath(&path);
Annotate(begin_varname, end_varname, descriptor->file()->name(), path);
Annotate(begin_varname, end_varname, descriptor->file()->name(), path,
semantic);
}
template <typename Map>

Loading…
Cancel
Save