Files
carbon-lang/explorer/main.cpp
T
Jon Meow af694b97cb Prefix most macro names with CARBON_ (#1232)
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.
2022-05-06 15:30:25 -07:00

96 lines
3.1 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/main.h"
#include <unistd.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <optional>
#include <string>
#include <vector>
#include "common/error.h"
#include "explorer/common/arena.h"
#include "explorer/common/nonnull.h"
#include "explorer/interpreter/exec_program.h"
#include "explorer/syntax/parse.h"
#include "explorer/syntax/prelude.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/raw_ostream.h"
namespace Carbon {
namespace cl = llvm::cl;
static auto Main(llvm::StringRef default_prelude_file, int argc, char* argv[])
-> ErrorOr<Success> {
llvm::setBugReportMsg(
"Please report issues to "
"https://github.com/carbon-language/carbon-lang/issues and include the "
"crash backtrace.\n");
llvm::InitLLVM init_llvm(argc, argv);
// Printing to stderr should flush stdout. This is most noticeable when stderr
// is piped to stdout.
llvm::errs().tie(&llvm::outs());
cl::opt<std::string> input_file_name(cl::Positional, cl::desc("<input file>"),
cl::Required);
cl::opt<bool> parser_debug("parser_debug",
cl::desc("Enable debug output from the parser"));
cl::opt<std::string> trace_file_name(
"trace_file",
cl::desc("Output file for tracing; set to `-` to output to stdout."));
// Find the path of the executable if possible and use that as a relative root
cl::opt<std::string> prelude_file_name("prelude", cl::desc("<prelude file>"),
cl::init(default_prelude_file.str()));
cl::ParseCommandLineOptions(argc, argv);
// Set up a stream for trace output.
std::unique_ptr<llvm::raw_ostream> scoped_trace_stream;
std::optional<Nonnull<llvm::raw_ostream*>> trace_stream;
if (!trace_file_name.empty()) {
if (trace_file_name == "-") {
trace_stream = &llvm::outs();
} else {
std::error_code err;
scoped_trace_stream =
std::make_unique<llvm::raw_fd_ostream>(trace_file_name, err);
if (err) {
return Error(err.message());
}
trace_stream = scoped_trace_stream.get();
}
}
Arena arena;
CARBON_ASSIGN_OR_RETURN(AST ast,
Parse(&arena, input_file_name, parser_debug));
AddPrelude(prelude_file_name, &arena, &ast.declarations);
// Typecheck and run the parsed program.
CARBON_ASSIGN_OR_RETURN(int return_code,
ExecProgram(&arena, ast, trace_stream));
// Print the return code to stdout even when we aren't tracing.
(trace_stream ? **trace_stream : llvm::outs())
<< "result: " << return_code << "\n";
return Success();
}
auto ExplorerMain(llvm::StringRef default_prelude_file, int argc, char** argv)
-> int {
if (auto result = Main(default_prelude_file, argc, argv); !result.ok()) {
llvm::errs() << result.error().message() << "\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
} // namespace Carbon