proto_to_carbon_main.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. // To convert a crashing input in text proto to Carbon source:
  5. // `proto_to_carbon <file.textproto>`
  6. #include <google/protobuf/text_format.h>
  7. #include <fstream>
  8. #include <iostream>
  9. #include <sstream>
  10. #include "common/bazel_working_dir.h"
  11. #include "common/error.h"
  12. #include "testing/fuzzing/proto_to_carbon.h"
  13. namespace Carbon {
  14. static auto Main(int argc, char** argv) -> ErrorOr<Success> {
  15. Carbon::SetWorkingDirForBazel();
  16. if (argc != 2) {
  17. return Error("Syntax: proto_to_carbon <file.textproto>");
  18. }
  19. if (!std::filesystem::is_regular_file(argv[1])) {
  20. return Error("Argument must be a file.");
  21. }
  22. // Read the input file.
  23. std::ifstream proto_file(argv[1]);
  24. std::stringstream buffer;
  25. buffer << proto_file.rdbuf();
  26. proto_file.close();
  27. CARBON_ASSIGN_OR_RETURN(Fuzzing::Carbon proto,
  28. Carbon::ParseCarbonTextProto(buffer.str()));
  29. std::cout << Carbon::ProtoToCarbon(proto, /*maybe_add_main=*/true);
  30. return Success();
  31. }
  32. } // namespace Carbon
  33. auto main(int argc, char** argv) -> int {
  34. auto err = Carbon::Main(argc, argv);
  35. if (!err.ok()) {
  36. std::cerr << err.error().message() << "\n";
  37. return EXIT_FAILURE;
  38. }
  39. return EXIT_SUCCESS;
  40. }