ast_to_proto_test.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "common/fuzzing/proto_to_carbon.h"
  14. #include "explorer/syntax/parse.h"
  15. namespace Carbon::Testing {
  16. namespace {
  17. using ::google::protobuf::Descriptor;
  18. using ::google::protobuf::FieldDescriptor;
  19. using ::google::protobuf::Message;
  20. using ::google::protobuf::Reflection;
  21. static std::vector<llvm::StringRef>* carbon_files = nullptr;
  22. // Returns a string representation of `ast`.
  23. auto AstToString(const AST& ast) -> std::string {
  24. std::string s;
  25. llvm::raw_string_ostream out(s);
  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 s;
  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(&arena, f, /*parser_debug=*/false);
  91. if (ast.ok()) {
  92. merged_proto.MergeFrom(AstToProto(*ast));
  93. }
  94. }
  95. std::set<std::string> all_messages;
  96. std::set<std::string> all_fields;
  97. CollectAllFields(*Fuzzing::Carbon::GetDescriptor(), all_messages, all_fields);
  98. std::set<std::string> used_fields;
  99. CollectUsedFields(merged_proto, used_fields);
  100. std::set<std::string> unused_fields;
  101. std::set_difference(all_fields.begin(), all_fields.end(), used_fields.begin(),
  102. used_fields.end(),
  103. std::inserter(unused_fields, unused_fields.begin()));
  104. EXPECT_EQ(unused_fields.size(), 0)
  105. << "Unused fields"
  106. << std::accumulate(unused_fields.begin(), unused_fields.end(),
  107. std::string(),
  108. [](const std::string& a, const std::string& b) {
  109. return a + '\n' + b;
  110. });
  111. }
  112. // Ensures that `carbon.proto` is able to represent ASTs correctly without
  113. // information loss by doing round-trip testing of files:
  114. //
  115. // 1) Converts each parseable Carbon file to a proto representation.
  116. // 2) Converts back to Carbon source.
  117. // 3) Parses the source into a second instance of an AST.
  118. // 4) Compares the second AST with the original.
  119. TEST(AstToProtoTest, Roundtrip) {
  120. int parsed_ok_count = 0;
  121. for (const llvm::StringRef f : *carbon_files) {
  122. Arena arena;
  123. const ErrorOr<AST> ast = Parse(&arena, f, /*parser_debug=*/false);
  124. if (ast.ok()) {
  125. ++parsed_ok_count;
  126. const std::string source_from_proto =
  127. ProtoToCarbon(AstToProto(*ast), /*maybe_add_main=*/false);
  128. SCOPED_TRACE(testing::Message()
  129. << "Carbon file: " << f << ", source from proto:\n"
  130. << source_from_proto);
  131. const ErrorOr<AST> ast_from_proto =
  132. ParseFromString(&arena, f, source_from_proto, /*parser_debug=*/false);
  133. if (ast_from_proto.ok()) {
  134. EXPECT_EQ(AstToString(*ast), AstToString(*ast_from_proto));
  135. } else {
  136. ADD_FAILURE() << "Parse error " << ast_from_proto.error().message();
  137. }
  138. }
  139. }
  140. // Makes sure files were actually processed.
  141. EXPECT_GT(parsed_ok_count, 0);
  142. }
  143. auto CloneAST(Arena& arena, const AST& ast) -> AST {
  144. CloneContext context(&arena);
  145. return {
  146. .package = ast.package,
  147. .is_api = ast.is_api,
  148. .imports = ast.imports,
  149. .declarations = context.Clone(ast.declarations),
  150. .main_call = context.Clone(ast.main_call),
  151. .num_prelude_declarations = ast.num_prelude_declarations,
  152. };
  153. }
  154. // Verifies that an AST and its clone produce identical protos.
  155. TEST(AstToProtoTest, SameProtoAfterClone) {
  156. int parsed_ok_count = 0;
  157. for (const llvm::StringRef f : *carbon_files) {
  158. Arena arena;
  159. const ErrorOr<AST> ast = Parse(&arena, f, /*parser_debug=*/false);
  160. if (ast.ok()) {
  161. ++parsed_ok_count;
  162. const AST clone = CloneAST(arena, *ast);
  163. const Fuzzing::Carbon orig_proto = AstToProto(*ast);
  164. const Fuzzing::Carbon clone_proto = AstToProto(clone);
  165. // TODO: Use EqualsProto once it's available.
  166. EXPECT_TRUE(google::protobuf::util::MessageDifferencer::Equals(
  167. orig_proto, clone_proto))
  168. << "clone produced a different AST. original:\n"
  169. << AstToString(*ast) << "clone:\n"
  170. << AstToString(clone);
  171. }
  172. }
  173. // Makes sure files were actually processed.
  174. EXPECT_GT(parsed_ok_count, 0);
  175. }
  176. } // namespace
  177. } // namespace Carbon::Testing
  178. auto main(int argc, char** argv) -> int {
  179. ::testing::InitGoogleTest(&argc, argv);
  180. // gtest should remove flags, leaving just input files.
  181. std::vector<llvm::StringRef> carbon_files(&argv[1], &argv[argc]);
  182. Carbon::Testing::carbon_files = &carbon_files;
  183. return RUN_ALL_TESTS();
  184. }