mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-29 15:34:58 +01:00
Carbon fuzzing 2/3: added logic for converting carbon.proto to a Carbon source string. (#1155)
* Carbon fuzzing 2/3: added logic for converting carbon.proto to a Carbon source string. * adjusted to proto changes in proto PR * addressed todos * removed unnecessary has_xx() calls * minor fix * sync from part 1 * Update executable_semantics/fuzzing/proto_to_carbon_test.cpp Co-authored-by: Geoff Romer <gromer@google.com> * review comments * Update common/fuzzing/proto_to_carbon.cpp Co-authored-by: Jon Meow <jperkins@google.com> * Update common/fuzzing/proto_to_carbon.cpp Co-authored-by: Jon Meow <jperkins@google.com> * code review * code review * Update common/fuzzing/proto_to_carbon.cpp Co-authored-by: Jon Meow <jperkins@google.com> * code review * merged + added support for type_params in class * use <> includes for protobuf * review comments * code review comments Co-authored-by: Geoff Romer <gromer@google.com> Co-authored-by: Jon Meow <jperkins@google.com>
This commit is contained in:
committed by
GitHub
co-authored by
Geoff Romer
Jon Meow
parent
a89be4ffea
commit
f8ded337fb
@@ -35,3 +35,25 @@ cc_test(
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "proto_to_carbon_test",
|
||||
srcs = ["proto_to_carbon_test.cpp"],
|
||||
args = [
|
||||
"$(locations //executable_semantics:standard_libraries)",
|
||||
"$(locations //executable_semantics/testdata:carbon_files)",
|
||||
],
|
||||
data = [
|
||||
"//executable_semantics:standard_libraries",
|
||||
"//executable_semantics/testdata:carbon_files",
|
||||
],
|
||||
deps = [
|
||||
":ast_to_proto_lib",
|
||||
"//common/fuzzing:carbon_cc_proto",
|
||||
"//common/fuzzing:proto_to_carbon_lib",
|
||||
"//executable_semantics/syntax",
|
||||
"@com_google_googletest//:gtest",
|
||||
"@com_google_protobuf//:protobuf_headers",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -380,7 +380,14 @@ static auto StatementToProto(const Statement& statement) -> Fuzzing::Statement {
|
||||
ExpressionToProto(match.expression());
|
||||
for (const Match::Clause& clause : match.clauses()) {
|
||||
auto* clause_proto = match_proto->add_clauses();
|
||||
*clause_proto->mutable_pattern() = PatternToProto(clause.pattern());
|
||||
const bool is_default_clause =
|
||||
clause.pattern().kind() == PatternKind::BindingPattern &&
|
||||
cast<BindingPattern>(clause.pattern()).name() == AnonymousName;
|
||||
if (is_default_clause) {
|
||||
clause_proto->set_is_default(true);
|
||||
} else {
|
||||
*clause_proto->mutable_pattern() = PatternToProto(clause.pattern());
|
||||
}
|
||||
*clause_proto->mutable_statement() =
|
||||
StatementToProto(clause.statement());
|
||||
}
|
||||
@@ -466,6 +473,10 @@ static auto DeclarationToProto(const Declaration& declaration)
|
||||
const auto& class_decl = cast<ClassDeclaration>(declaration);
|
||||
auto* class_proto = declaration_proto.mutable_class_declaration();
|
||||
class_proto->set_name(class_decl.name());
|
||||
if (class_decl.type_params().has_value()) {
|
||||
*class_proto->mutable_type_params() =
|
||||
TuplePatternToProto(**class_decl.type_params());
|
||||
}
|
||||
for (Nonnull<const Declaration*> member : class_decl.members()) {
|
||||
*class_proto->add_members() = DeclarationToProto(*member);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "executable_semantics/fuzzing/ast_to_proto.h"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <google/protobuf/descriptor.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <filesystem>
|
||||
@@ -13,7 +14,6 @@
|
||||
#include <variant>
|
||||
|
||||
#include "executable_semantics/syntax/parse.h"
|
||||
#include "google/protobuf/descriptor.h"
|
||||
#include "llvm/Support/Error.h"
|
||||
|
||||
namespace Carbon::Testing {
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
// Exceptions. See /LICENSE for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
#include "common/fuzzing/proto_to_carbon.h"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "executable_semantics/fuzzing/ast_to_proto.h"
|
||||
#include "executable_semantics/syntax/parse.h"
|
||||
|
||||
namespace Carbon::Testing {
|
||||
namespace {
|
||||
|
||||
static std::vector<llvm::StringRef>* carbon_files = nullptr;
|
||||
|
||||
// Returns a string representation of `ast`.
|
||||
auto AstToString(const AST& ast) -> std::string {
|
||||
std::string s;
|
||||
llvm::raw_string_ostream out(s);
|
||||
out << "package " << ast.package.package << (ast.is_api ? "api" : "impl")
|
||||
<< ";\n";
|
||||
for (auto* declaration : ast.declarations) {
|
||||
out << *declaration << "\n";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
TEST(ProtoToCarbonTest, Roundtrip) {
|
||||
int parsed_ok_count = 0;
|
||||
for (const llvm::StringRef f : *carbon_files) {
|
||||
Carbon::Arena arena;
|
||||
const ErrorOr<AST> ast = Carbon::Parse(&arena, f, /*trace=*/false);
|
||||
if (ast.ok()) {
|
||||
++parsed_ok_count;
|
||||
const std::string source_from_proto = ProtoToCarbon(AstToProto(*ast));
|
||||
SCOPED_TRACE(testing::Message()
|
||||
<< "Carbon file: " << f << ", source from proto:\n"
|
||||
<< source_from_proto);
|
||||
const ErrorOr<AST> ast_from_proto = Carbon::ParseFromString(
|
||||
&arena, f, source_from_proto, /*trace=*/false);
|
||||
|
||||
if (ast_from_proto.ok()) {
|
||||
EXPECT_EQ(AstToString(*ast), AstToString(*ast_from_proto));
|
||||
} else {
|
||||
ADD_FAILURE() << "Parse error " << ast_from_proto.error().message();
|
||||
}
|
||||
}
|
||||
}
|
||||
// Makes sure files were actually processed.
|
||||
EXPECT_GT(parsed_ok_count, 0);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace Carbon::Testing
|
||||
|
||||
auto main(int argc, char** argv) -> int {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
Carbon::Testing::carbon_files =
|
||||
new std::vector<llvm::StringRef>(&argv[1], &argv[argc]);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Reference in New Issue
Block a user