kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/extractor/bazel_event_reader.cc (about)

     1  /*
     2   * Copyright 2020 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  #include "kythe/cxx/extractor/bazel_event_reader.h"
    17  
    18  #include <utility>
    19  
    20  #include "absl/status/status.h"
    21  #include "google/protobuf/io/coded_stream.h"
    22  #include "google/protobuf/io/zero_copy_stream.h"
    23  #include "google/protobuf/util/delimited_message_util.h"
    24  #include "third_party/bazel/src/main/java/com/google/devtools/build/lib/buildeventstream/proto/build_event_stream.pb.h"
    25  
    26  namespace kythe {
    27  namespace {
    28  using ::build_event_stream::BuildEvent;
    29  using ::google::protobuf::io::CodedInputStream;
    30  using ::google::protobuf::io::ZeroCopyInputStream;
    31  using ::google::protobuf::util::ParseDelimitedFromCodedStream;
    32  using ::google::protobuf::util::ParseDelimitedFromZeroCopyStream;
    33  
    34  struct ParseEvent {
    35    BuildEvent* event;
    36    bool* clean_eof;
    37  
    38    template <typename T>
    39    bool From(T&& variant) {
    40      return absl::visit(*this, std::forward<T>(variant));
    41    }
    42  
    43    bool operator()(ZeroCopyInputStream* stream) const {
    44      return ParseDelimitedFromZeroCopyStream(event, stream, clean_eof);
    45    }
    46  
    47    bool operator()(CodedInputStream* stream) const {
    48      return ParseDelimitedFromCodedStream(event, stream, clean_eof);
    49    }
    50  };
    51  
    52  }  // namespace
    53  
    54  void BazelEventReader::Next() {
    55    bool clean_eof = true;
    56    BuildEvent event;
    57    if (ParseEvent{&event, &clean_eof}.From(stream_)) {
    58      value_ = std::move(event);
    59    } else if (clean_eof) {
    60      value_ = absl::OkStatus();
    61    } else {
    62      value_ = absl::UnknownError("Error decoding BuildEvent from stream");
    63    }
    64  }
    65  
    66  }  // namespace kythe