mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
I'm doing this to avoid macro name conflicts, following https://google.github.io/styleguide/cppguide.html#Preprocessor_Macros: "If you do export a macro from a header, it must have a globally unique name. To achieve this, it must be named with a prefix consisting of your project's namespace name (but upper case)." Commands run: ``` sed -i 's/\(DCHECK\|CHECK\|FATAL\|MAKE_UNIQUE_NAME\|MAKE_UNIQUE_NAME_IMPL\|RAW_EXITING_STREAM\|RETURN_IF_ERROR\|RETURN_IF_ERROR_IMPL\|ASSIGN_OR_RETURN\|ASSIGN_OR_RETURN_IMPL\|DIAGNOSTIC_KIND\|RETURN_IF_STACK_LIMITED\)(/CARBON_\1(/g' $(git ls-files *.cpp *.h *.lpp *.ypp *.def ':!third_party') sed -i 's/#undef DIAGNOSTIC_KIND/#undef CARBON_DIAGNOSTIC_KIND/' toolchain/diagnostics/diagnostic_registry.def ``` Note this isn't *quite* everything, but it's intended to be a large pass at everything: ``` ╚╡git grep '#define ' *.cpp *.h *.lpp *.ypp *.def ':!third_party' | grep -v '#define CARBON' | grep -v _H_ explorer/syntax/lexer.lpp: #define YY_USER_ACTION \ explorer/syntax/lexer.lpp: #define SIMPLE_TOKEN(name) \ explorer/syntax/lexer.lpp: #define ARG_TOKEN(name, arg) \ explorer/syntax/parse_and_lex_context.h:#define YY_DECL \ migrate_cpp/cpp_refactoring/var_decl.cpp:#define ABSTRACT_TYPE(Class, Base) migrate_cpp/cpp_refactoring/var_decl.cpp:#define TYPE(Class, Base) \ ``` We may in particular want to do a pass to clean up #ifdef guards and make them be CARBON_ rooted.
80 lines
2.8 KiB
C++
80 lines
2.8 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 "common/check.h"
|
|
#include "common/fuzzing/proto_to_carbon.h"
|
|
#include "explorer/interpreter/exec_program.h"
|
|
#include "explorer/syntax/parse.h"
|
|
#include "explorer/syntax/prelude.h"
|
|
#include "llvm/Support/FileSystem.h"
|
|
#include "llvm/Support/Path.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
#include "tools/cpp/runfiles/runfiles.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 Internal::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 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;
|
|
}
|
|
const ErrorOr<std::string> prelude_path =
|
|
Internal::GetRunfilesFile("carbon/explorer/data/prelude.carbon");
|
|
CARBON_CHECK(prelude_path.ok()) << prelude_path.error().message();
|
|
AddPrelude(*prelude_path, &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
|