mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 12:10:10 +01:00
This is a workaround for #1208, allowing to remove manual tag to allow building and testing fuzzer code during precommits and in bazel test ... Tested: bazel test -k ... bazel build -c opt executable_semantics/fuzzing:executable_semantics_fuzzer bazel-bin/executable_semantics/fuzzing/executable_semantics_fuzzer
58 lines
2.0 KiB
C++
58 lines
2.0 KiB
C++
// 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 "executable_semantics/fuzzing/fuzzer_util.h"
|
|
|
|
#include "common/check.h"
|
|
#include "common/fuzzing/proto_to_carbon.h"
|
|
#include "executable_semantics/interpreter/exec_program.h"
|
|
#include "executable_semantics/syntax/parse.h"
|
|
#include "executable_semantics/syntax/prelude.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
namespace Carbon {
|
|
|
|
// Appended to fuzzer-generated Carbon source when the source is missing
|
|
// `Main()` definition, to prevent early error return in semantic analysis.
|
|
static constexpr char EmptyMain[] = R"(
|
|
fn Main() -> i32 {
|
|
return 0;
|
|
}
|
|
)";
|
|
|
|
auto ProtoToCarbonWithMain(const Fuzzing::CompilationUnit& compilation_unit)
|
|
-> std::string {
|
|
const bool has_main = std::any_of(
|
|
compilation_unit.declarations().begin(),
|
|
compilation_unit.declarations().end(),
|
|
[](const Fuzzing::Declaration& decl) {
|
|
return decl.kind_case() == Fuzzing::Declaration::kFunction &&
|
|
decl.function().name() == "Main";
|
|
});
|
|
return Carbon::ProtoToCarbon(compilation_unit) + (has_main ? "" : EmptyMain);
|
|
}
|
|
|
|
void ParseAndExecute(const Fuzzing::CompilationUnit& compilation_unit) {
|
|
const std::string source = ProtoToCarbonWithMain(compilation_unit);
|
|
|
|
Arena arena;
|
|
ErrorOr<AST> ast = ParseFromString(&arena, "Fuzzer.carbon", source,
|
|
/*parser_debug=*/false);
|
|
if (!ast.ok()) {
|
|
llvm::errs() << "Parsing failed: " << ast.error().message() << "\n";
|
|
return;
|
|
}
|
|
AddPrelude("executable_semantics/data/prelude.carbon", &arena,
|
|
&ast->declarations);
|
|
const ErrorOr<int> result =
|
|
ExecProgram(&arena, *ast, /*trace_stream=*/std::nullopt);
|
|
if (!result.ok()) {
|
|
llvm::errs() << "Execution failed: " << result.error().message() << "\n";
|
|
return;
|
|
}
|
|
llvm::outs() << "Executed OK: " << *result << "\n";
|
|
}
|
|
|
|
} // namespace Carbon
|