• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/compiler/mlir/tensorflow/utils/parse_text_proto.h"
17 
18 #include "absl/strings/match.h"
19 #include "absl/strings/string_view.h"
20 #include "tensorflow/core/lib/core/errors.h"
21 #include "tensorflow/core/lib/core/status.h"
22 #include "tensorflow/core/platform/casts.h"
23 #include "tensorflow/core/platform/protobuf.h"
24 
25 namespace tensorflow {
26 
27 namespace {
28 // Error collector that simply ignores errors reported.
29 class NoOpErrorCollector : public protobuf::io::ErrorCollector {
30  public:
AddError(int line,int column,const std::string & message)31   void AddError(int line, int column, const std::string& message) override {}
32 };
33 }  // namespace
34 
ConsumePrefix(absl::string_view str,absl::string_view prefix,absl::string_view * output)35 Status ConsumePrefix(absl::string_view str, absl::string_view prefix,
36                      absl::string_view* output) {
37   if (absl::StartsWith(str, prefix)) {
38     *output = str.substr(prefix.size());
39     return Status::OK();
40   }
41   return errors::NotFound("No prefix \"", prefix, "\" in \"", str, "\"");
42 }
43 
ParseTextProto(absl::string_view text_proto,absl::string_view prefix_to_strip,protobuf::Message * parsed_proto)44 Status ParseTextProto(absl::string_view text_proto,
45                       absl::string_view prefix_to_strip,
46                       protobuf::Message* parsed_proto) {
47   protobuf::TextFormat::Parser parser;
48   // Don't produce errors when attempting to parse text format as it would fail
49   // when the input is actually a binary file.
50   NoOpErrorCollector collector;
51   parser.RecordErrorsTo(&collector);
52   // Attempt to parse as text.
53   absl::string_view text_proto_without_prefix = text_proto;
54   if (!prefix_to_strip.empty()) {
55     TF_RETURN_IF_ERROR(
56         ConsumePrefix(text_proto, prefix_to_strip, &text_proto_without_prefix));
57   }
58   protobuf::io::ArrayInputStream input_stream(text_proto_without_prefix.data(),
59                                               text_proto_without_prefix.size());
60   if (parser.Parse(&input_stream, parsed_proto)) {
61     return Status::OK();
62   }
63   parsed_proto->Clear();
64   return errors::InvalidArgument("Could not parse text proto: ", text_proto);
65 }
66 
67 }  // namespace tensorflow
68