mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 12:50:12 +01:00
In explorer, we already support parsing a string_view, so use that. In toolchain, we need to build support, probably using vfs, so that's a todo. bazel test //explorer:file_test --runs_per_test=5 - branch: Stats over 250 runs: max = 18.3s, min = 5.2s, avg = 11.2s, dev = 2.9s - trunk: Stats over 250 runs: max = 22.3s, min = 5.9s, avg = 12.1s, dev = 2.8s Not a dramatic improvement, but maybe more effective long-term, and this'd been requested on #2876
48 lines
1.7 KiB
C++
48 lines
1.7 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 "explorer/fuzzing/fuzzer_util.h"
|
|
|
|
#include <google/protobuf/text_format.h>
|
|
|
|
#include "common/check.h"
|
|
#include "common/error.h"
|
|
#include "common/fuzzing/proto_to_carbon.h"
|
|
#include "explorer/parse_and_execute/parse_and_execute.h"
|
|
#include "llvm/Support/FileSystem.h"
|
|
#include "llvm/Support/Path.h"
|
|
#include "tools/cpp/runfiles/runfiles.h"
|
|
|
|
namespace Carbon::Testing {
|
|
|
|
auto GetRunfilesFile(const std::string& file) -> ErrorOr<std::string> {
|
|
using bazel::tools::cpp::runfiles::Runfiles;
|
|
std::string error;
|
|
// `Runfiles::Create()` fails if passed an empty `argv0`.
|
|
std::unique_ptr<Runfiles> runfiles(Runfiles::Create(
|
|
/*argv0=*/llvm::sys::fs::getMainExecutable(nullptr, nullptr), &error));
|
|
if (runfiles == nullptr) {
|
|
return Error(error);
|
|
}
|
|
std::string full_path = runfiles->Rlocation(file);
|
|
if (!llvm::sys::fs::exists(full_path)) {
|
|
return ErrorBuilder() << full_path << " doesn't exist";
|
|
}
|
|
return full_path;
|
|
}
|
|
|
|
auto ParseAndExecuteProto(const Fuzzing::Carbon& carbon) -> ErrorOr<int> {
|
|
const ErrorOr<std::string> prelude_path =
|
|
GetRunfilesFile("carbon/explorer/data/prelude.carbon");
|
|
// Can't do anything without a prelude, so it's a fatal error.
|
|
CARBON_CHECK(prelude_path.ok()) << prelude_path.error();
|
|
|
|
const std::string source = ProtoToCarbon(carbon, /*maybe_add_main=*/true);
|
|
TraceStream trace_stream;
|
|
return ParseAndExecute(*prelude_path, "fuzzer.carbon", source,
|
|
/*parser_debug=*/false, &trace_stream, &llvm::nulls());
|
|
}
|
|
|
|
} // namespace Carbon::Testing
|