ast_to_proto_test.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #include "explorer/fuzzing/ast_to_proto.h"
  5. #include <gmock/gmock.h>
  6. #include <google/protobuf/descriptor.h>
  7. #include <google/protobuf/util/message_differencer.h>
  8. #include <gtest/gtest.h>
  9. #include <filesystem>
  10. #include <numeric>
  11. #include <set>
  12. #include <variant>
  13. #include "explorer/syntax/parse.h"
  14. #include "testing/base/test_raw_ostream.h"
  15. #include "testing/fuzzing/proto_to_carbon.h"
  16. namespace Carbon::Testing {
  17. namespace {
  18. using ::google::protobuf::Descriptor;
  19. using ::google::protobuf::FieldDescriptor;
  20. using ::google::protobuf::Message;
  21. using ::google::protobuf::Reflection;
  22. static std::vector<llvm::StringRef>* carbon_files = nullptr;
  23. // Returns a string representation of `ast`.
  24. auto AstToString(const AST& ast) -> std::string {
  25. TestRawOstream out;
  26. out << "package " << ast.package.package << (ast.is_api ? "api" : "impl")
  27. << ";\n";
  28. for (auto* declaration : ast.declarations) {
  29. out << *declaration << "\n";
  30. }
  31. return out.TakeStr();
  32. }
  33. // Concatenates message and field names.
  34. auto FieldName(const Descriptor& descriptor, const FieldDescriptor& field)
  35. -> std::string {
  36. return descriptor.full_name() + "." + field.name();
  37. }
  38. // Traverses the proto to find all unique messages and fields.
  39. auto CollectAllFields(const Descriptor& descriptor,
  40. std::set<std::string>& all_messages,
  41. std::set<std::string>& all_fields) -> void {
  42. all_messages.insert(descriptor.full_name());
  43. for (int i = 0; i < descriptor.field_count(); ++i) {
  44. const FieldDescriptor* field = descriptor.field(i);
  45. all_fields.insert(FieldName(descriptor, *field));
  46. if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
  47. all_messages.find(field->message_type()->full_name()) ==
  48. all_messages.end()) {
  49. CollectAllFields(*field->message_type(), all_messages, all_fields);
  50. }
  51. }
  52. }
  53. // Traverses an instance of the proto to find all used fields.
  54. auto CollectUsedFields(const Message& message,
  55. std::set<std::string>& used_fields) -> void {
  56. const Descriptor* descriptor = message.GetDescriptor();
  57. const Reflection* reflection = message.GetReflection();
  58. for (int i = 0; i < descriptor->field_count(); ++i) {
  59. const FieldDescriptor* field = descriptor->field(i);
  60. if (!field->is_repeated()) {
  61. if (reflection->HasField(message, field)) {
  62. used_fields.insert(FieldName(*descriptor, *field));
  63. }
  64. } else {
  65. if (reflection->FieldSize(message, field) > 0) {
  66. used_fields.insert(FieldName(*descriptor, *field));
  67. }
  68. }
  69. if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  70. if (!field->is_repeated()) {
  71. if (reflection->HasField(message, field)) {
  72. CollectUsedFields(reflection->GetMessage(message, field),
  73. used_fields);
  74. }
  75. } else {
  76. for (int i = 0; i < reflection->FieldSize(message, field); ++i) {
  77. CollectUsedFields(reflection->GetRepeatedMessage(message, field, i),
  78. used_fields);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. // A 'smoke' test to check that each field present in `carbon.proto` is set at
  85. // least once after converting all Carbon test sources to proto representation.
  86. TEST(AstToProtoTest, SetsAllProtoFields) {
  87. Fuzzing::Carbon merged_proto;
  88. for (const llvm::StringRef f : *carbon_files) {
  89. Arena arena;
  90. const ErrorOr<AST> ast = Parse(*llvm::vfs::getRealFileSystem(), &arena, f,
  91. FileKind::Main, /*parser_debug=*/false);
  92. if (ast.ok()) {
  93. merged_proto.MergeFrom(AstToProto(*ast));
  94. }
  95. }
  96. std::set<std::string> all_messages;
  97. std::set<std::string> all_fields;
  98. CollectAllFields(*Fuzzing::Carbon::GetDescriptor(), all_messages, all_fields);
  99. std::set<std::string> used_fields;
  100. CollectUsedFields(merged_proto, used_fields);
  101. std::set<std::string> unused_fields;
  102. std::set_difference(all_fields.begin(), all_fields.end(), used_fields.begin(),
  103. used_fields.end(),
  104. std::inserter(unused_fields, unused_fields.begin()));
  105. EXPECT_EQ(unused_fields.size(), 0)
  106. << "Unused fields"
  107. << std::accumulate(unused_fields.begin(), unused_fields.end(),
  108. std::string(),
  109. [](const std::string& a, const std::string& b) {
  110. return a + '\n' + b;
  111. });
  112. }
  113. // Ensures that `carbon.proto` is able to represent ASTs correctly without
  114. // information loss by doing round-trip testing of files:
  115. //
  116. // 1) Converts each parseable Carbon file to a proto representation.
  117. // 2) Converts back to Carbon source.
  118. // 3) Parses the source into a second instance of an AST.
  119. // 4) Compares the second AST with the original.
  120. TEST(AstToProtoTest, Roundtrip) {
  121. int parsed_ok_count = 0;
  122. for (const llvm::StringRef f : *carbon_files) {
  123. Arena arena;
  124. const ErrorOr<AST> ast = Parse(*llvm::vfs::getRealFileSystem(), &arena, f,
  125. FileKind::Main, /*parser_debug=*/false);
  126. if (ast.ok()) {
  127. ++parsed_ok_count;
  128. const std::string source_from_proto =
  129. ProtoToCarbon(AstToProto(*ast), /*maybe_add_main=*/false);
  130. SCOPED_TRACE(testing::Message()
  131. << "Carbon file: " << f << ", source from proto:\n"
  132. << source_from_proto);
  133. const ErrorOr<AST> ast_from_proto = ParseFromString(
  134. &arena, f, FileKind::Main, source_from_proto, /*parser_debug=*/false);
  135. if (ast_from_proto.ok()) {
  136. EXPECT_EQ(AstToString(*ast), AstToString(*ast_from_proto));
  137. } else {
  138. ADD_FAILURE() << "Parse error " << ast_from_proto.error().message();
  139. }
  140. }
  141. }
  142. // Makes sure files were actually processed.
  143. EXPECT_GT(parsed_ok_count, 0);
  144. }
  145. auto CloneAST(Arena& arena, const AST& ast) -> AST {
  146. CloneContext context(&arena);
  147. return {
  148. .package = ast.package,
  149. .is_api = ast.is_api,
  150. .imports = ast.imports,
  151. .declarations = context.Clone(ast.declarations),
  152. .main_call = context.Clone(ast.main_call),
  153. .num_prelude_declarations = ast.num_prelude_declarations,
  154. };
  155. }
  156. // Verifies that an AST and its clone produce identical protos.
  157. TEST(AstToProtoTest, SameProtoAfterClone) {
  158. int parsed_ok_count = 0;
  159. for (const llvm::StringRef f : *carbon_files) {
  160. Arena arena;
  161. const ErrorOr<AST> ast = Parse(*llvm::vfs::getRealFileSystem(), &arena, f,
  162. FileKind::Main, /*parser_debug=*/false);
  163. if (ast.ok()) {
  164. ++parsed_ok_count;
  165. const AST clone = CloneAST(arena, *ast);
  166. const Fuzzing::Carbon orig_proto = AstToProto(*ast);
  167. const Fuzzing::Carbon clone_proto = AstToProto(clone);
  168. // TODO: Use EqualsProto once it's available.
  169. EXPECT_TRUE(google::protobuf::util::MessageDifferencer::Equals(
  170. orig_proto, clone_proto))
  171. << "clone produced a different AST. original:\n"
  172. << AstToString(*ast) << "clone:\n"
  173. << AstToString(clone);
  174. }
  175. }
  176. // Makes sure files were actually processed.
  177. EXPECT_GT(parsed_ok_count, 0);
  178. }
  179. } // namespace
  180. } // namespace Carbon::Testing
  181. auto main(int argc, char** argv) -> int {
  182. ::testing::InitGoogleTest(&argc, argv);
  183. // gtest should remove flags, leaving just input files.
  184. std::vector<llvm::StringRef> carbon_files(&argv[1], &argv[argc]);
  185. Carbon::Testing::carbon_files = &carbon_files;
  186. return RUN_ALL_TESTS();
  187. }