Pass the flag instead of using a global. (#893)

Arguably missed in #769 

Note, this is reminding me we have more class members to rename for `_`, but I felt it's best to use the new naming instead of adding more to clean up.
This commit is contained in:
Jon Meow
2021-10-19 09:16:57 -07:00
committed by GitHub
parent a3eac75a5b
commit a9eed3dbf1
17 changed files with 43 additions and 79 deletions
-1
View File
@@ -8,7 +8,6 @@ cc_binary(
name = "executable_semantics",
srcs = ["main.cpp"],
deps = [
"//executable_semantics/common:tracing_flag",
"//executable_semantics/interpreter:exec_program",
"//executable_semantics/syntax",
"@llvm-project//llvm:Support",
-7
View File
@@ -38,10 +38,3 @@ cc_library(
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "tracing_flag",
srcs = ["tracing_flag.cpp"],
hdrs = ["tracing_flag.h"],
visibility = ["//executable_semantics:__subpackages__"],
)
@@ -1,11 +0,0 @@
// 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/common/tracing_flag.h"
namespace Carbon {
bool tracing_output = false;
} // namespace Carbon
@@ -1,15 +0,0 @@
// 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
#ifndef EXECUTABLE_SEMANTICS_COMMON_TRACING_FLAG_H_
#define EXECUTABLE_SEMANTICS_COMMON_TRACING_FLAG_H_
namespace Carbon {
// Program option to enable/disable tracing.
extern bool tracing_output;
} // namespace Carbon
#endif // EXECUTABLE_SEMANTICS_COMMON_TRACING_FLAG_H_
-2
View File
@@ -98,7 +98,6 @@ cc_library(
"//executable_semantics/ast:declaration",
"//executable_semantics/ast:expression",
"//executable_semantics/common:arena",
"//executable_semantics/common:tracing_flag",
"@llvm-project//llvm:Support",
],
)
@@ -121,7 +120,6 @@ cc_library(
"//executable_semantics/ast:expression",
"//executable_semantics/ast:statement",
"//executable_semantics/common:arena",
"//executable_semantics/common:tracing_flag",
"@llvm-project//llvm:Support",
],
)
@@ -7,7 +7,6 @@
#include "common/check.h"
#include "common/ostream.h"
#include "executable_semantics/common/arena.h"
#include "executable_semantics/common/tracing_flag.h"
#include "executable_semantics/interpreter/interpreter.h"
#include "executable_semantics/interpreter/type_checker.h"
@@ -34,23 +33,23 @@ static void AddIntrinsics(Nonnull<Arena*> arena,
declarations->insert(declarations->begin(), print);
}
void ExecProgram(Nonnull<Arena*> arena, AST ast) {
void ExecProgram(Nonnull<Arena*> arena, AST ast, bool trace) {
AddIntrinsics(arena, &ast.declarations);
if (tracing_output) {
if (trace) {
llvm::outs() << "********** source program **********\n";
for (const auto decl : ast.declarations) {
llvm::outs() << *decl;
}
llvm::outs() << "********** type checking **********\n";
}
TypeChecker type_checker(arena);
TypeChecker type_checker(arena, trace);
TypeChecker::TypeCheckContext p = type_checker.TopLevel(&ast.declarations);
TypeEnv top = p.types;
Env ct_top = p.values;
for (const auto decl : ast.declarations) {
type_checker.TypeCheck(decl, top, ct_top);
}
if (tracing_output) {
if (trace) {
llvm::outs() << "\n";
llvm::outs() << "********** type checking complete **********\n";
for (const auto decl : ast.declarations) {
@@ -63,7 +62,8 @@ void ExecProgram(Nonnull<Arena*> arena, AST ast) {
Nonnull<Expression*> call_main = arena->New<CallExpression>(
source_loc, arena->New<IdentifierExpression>(source_loc, "main"),
arena->New<TupleLiteral>(source_loc));
int result = Interpreter(arena).InterpProgram(ast.declarations, call_main);
int result =
Interpreter(arena, trace).InterpProgram(ast.declarations, call_main);
llvm::outs() << "result: " << result << "\n";
}
@@ -14,7 +14,7 @@
namespace Carbon {
// Runs the top-level declaration list.
void ExecProgram(Nonnull<Arena*> arena, AST ast);
void ExecProgram(Nonnull<Arena*> arena, AST ast, bool trace);
} // namespace Carbon
@@ -16,7 +16,6 @@
#include "executable_semantics/ast/expression.h"
#include "executable_semantics/common/arena.h"
#include "executable_semantics/common/error.h"
#include "executable_semantics/common/tracing_flag.h"
#include "executable_semantics/interpreter/action.h"
#include "executable_semantics/interpreter/frame.h"
#include "executable_semantics/interpreter/stack.h"
@@ -377,7 +376,7 @@ void Interpreter::PatternAssignment(Nonnull<const Value*> pat,
auto Interpreter::StepLvalue() -> Transition {
Nonnull<Action*> act = stack.Top()->todo.Top();
const Expression& exp = cast<LValAction>(*act).expression();
if (tracing_output) {
if (trace_) {
llvm::outs() << "--- step lvalue " << exp << " (" << exp.source_loc()
<< ") --->\n";
}
@@ -460,7 +459,7 @@ auto Interpreter::StepLvalue() -> Transition {
auto Interpreter::StepExp() -> Transition {
Nonnull<Action*> act = stack.Top()->todo.Top();
const Expression& exp = cast<ExpressionAction>(*act).expression();
if (tracing_output) {
if (trace_) {
llvm::outs() << "--- step exp " << exp << " (" << exp.source_loc()
<< ") --->\n";
}
@@ -658,7 +657,7 @@ auto Interpreter::StepExp() -> Transition {
auto Interpreter::StepPattern() -> Transition {
Nonnull<Action*> act = stack.Top()->todo.Top();
const Pattern& pattern = cast<PatternAction>(*act).pattern();
if (tracing_output) {
if (trace_) {
llvm::outs() << "--- step pattern " << pattern << " ("
<< pattern.source_loc() << ") --->\n";
}
@@ -741,7 +740,7 @@ auto Interpreter::StepStmt() -> Transition {
Nonnull<Frame*> frame = stack.Top();
Nonnull<Action*> act = frame->todo.Top();
const Statement& stmt = cast<StatementAction>(*act).statement();
if (tracing_output) {
if (trace_) {
llvm::outs() << "--- step stmt ";
stmt.PrintDepth(1, llvm::outs());
llvm::outs() << " (" << stmt.source_loc() << ") --->\n";
@@ -1136,7 +1135,7 @@ auto Interpreter::InterpProgram(llvm::ArrayRef<Nonnull<Declaration*>> fs,
CHECK(stack.IsEmpty());
CHECK(program_value == std::nullopt);
if (tracing_output) {
if (trace_) {
llvm::outs() << "********** initializing globals **********\n";
}
InitGlobals(fs);
@@ -1145,14 +1144,14 @@ auto Interpreter::InterpProgram(llvm::ArrayRef<Nonnull<Declaration*>> fs,
auto scopes = Stack<Nonnull<Scope*>>(arena->New<Scope>(globals));
stack = Stack<Nonnull<Frame*>>(arena->New<Frame>("top", scopes, todo));
if (tracing_output) {
if (trace_) {
llvm::outs() << "********** calling main function **********\n";
PrintState(llvm::outs());
}
while (stack.Count() > 1 || !stack.Top()->todo.IsEmpty()) {
Step();
if (tracing_output) {
if (trace_) {
PrintState(llvm::outs());
}
}
@@ -25,8 +25,8 @@ using Env = Dictionary<std::string, Address>;
class Interpreter {
public:
explicit Interpreter(Nonnull<Arena*> arena)
: arena(arena), globals(arena), heap(arena) {}
explicit Interpreter(Nonnull<Arena*> arena, bool trace)
: arena(arena), globals(arena), heap(arena), trace_(trace) {}
// Interpret the whole program.
auto InterpProgram(llvm::ArrayRef<Nonnull<Declaration*>> fs,
@@ -160,6 +160,8 @@ class Interpreter {
Stack<Nonnull<Frame*>> stack;
Heap heap;
std::optional<Nonnull<const Value*>> program_value;
bool trace_;
};
} // namespace Carbon
@@ -14,7 +14,6 @@
#include "executable_semantics/ast/declaration.h"
#include "executable_semantics/common/arena.h"
#include "executable_semantics/common/error.h"
#include "executable_semantics/common/tracing_flag.h"
#include "executable_semantics/interpreter/interpreter.h"
#include "executable_semantics/interpreter/value.h"
#include "llvm/ADT/StringExtras.h"
@@ -418,7 +417,7 @@ auto TypeChecker::Substitute(TypeEnv dict, Nonnull<const Value*> type)
auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
Env values) -> TCResult {
if (tracing_output) {
if (trace_) {
llvm::outs() << "checking expression " << *e << "\ntypes: ";
PrintTypeEnv(types, llvm::outs());
llvm::outs() << "\nvalues: ";
@@ -708,7 +707,7 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
auto TypeChecker::TypeCheckPattern(
Nonnull<Pattern*> p, TypeEnv types, Env values,
std::optional<Nonnull<const Value*>> expected) -> TCResult {
if (tracing_output) {
if (trace_) {
llvm::outs() << "checking pattern " << *p;
if (expected) {
llvm::outs() << ", expecting " << **expected;
@@ -20,8 +20,8 @@ using TypeEnv = Dictionary<std::string, Nonnull<const Value*>>;
class TypeChecker {
public:
explicit TypeChecker(Nonnull<Arena*> arena)
: arena(arena), interpreter(arena) {}
explicit TypeChecker(Nonnull<Arena*> arena, bool trace)
: arena(arena), interpreter(arena, trace), trace_(trace) {}
struct TypeCheckContext {
explicit TypeCheckContext(Nonnull<Arena*> arena)
@@ -139,6 +139,8 @@ class TypeChecker {
Nonnull<Arena*> arena;
Interpreter interpreter;
bool trace_;
};
} // namespace Carbon
+3 -6
View File
@@ -6,7 +6,6 @@
#include <cstring>
#include <iostream>
#include "executable_semantics/common/tracing_flag.h"
#include "executable_semantics/interpreter/exec_program.h"
#include "executable_semantics/syntax/parse.h"
#include "llvm/Support/CommandLine.h"
@@ -30,13 +29,10 @@ int main(int argc, char* argv[]) {
llvm::cl::Required);
llvm::cl::ParseCommandLineOptions(argc, argv);
if (trace_option) {
Carbon::tracing_output = true;
}
Carbon::Arena arena;
std::variant<Carbon::AST, Carbon::SyntaxErrorCode> ast_or_error =
Carbon::Parse(&arena, input_file_name);
Carbon::Parse(&arena, input_file_name, trace_option);
if (auto* error = std::get_if<Carbon::SyntaxErrorCode>(&ast_or_error)) {
// Diagnostic already reported to std::cerr; this is just a return code.
@@ -44,5 +40,6 @@ int main(int argc, char* argv[]) {
}
// Typecheck and run the parsed program.
Carbon::ExecProgram(&arena, std::get<Carbon::AST>(ast_or_error));
Carbon::ExecProgram(&arena, std::get<Carbon::AST>(ast_or_error),
trace_option);
}
-1
View File
@@ -43,7 +43,6 @@ cc_library(
"//executable_semantics/ast:paren_contents",
"//executable_semantics/common:arena",
"//executable_semantics/common:error",
"//executable_semantics/common:tracing_flag",
],
)
+4 -5
View File
@@ -9,7 +9,6 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "common/check.h"
#include "common/string_helpers.h"
#include "executable_semantics/common/tracing_flag.h"
#include "executable_semantics/syntax/parse_and_lex_context.h"
#include "executable_semantics/syntax/parser.h"
#include "llvm/ADT/StringExtras.h"
@@ -246,8 +245,8 @@ string_literal \"([^\\\"\n\v\f\r]|\\.)*\"
CHECK(str.consume_front("\"") && str.consume_back("\""));
std::optional<std::string> unescaped = Carbon::UnescapeStringLiteral(str);
if (unescaped == std::nullopt) {
if (Carbon::tracing_output) {
// Print a newline because tracing prints an incomplete line
if (context.trace()) {
// Print a newline because trace prints an incomplete line
// "Reading a token: ".
llvm::errs() << "\n";
}
@@ -279,8 +278,8 @@ string_literal \"([^\\\"\n\v\f\r]|\\.)*\"
}
. {
if (Carbon::tracing_output) {
// Print a newline because tracing prints an incomplete line
if (context.trace()) {
// Print a newline because trace prints an incomplete line
// "Reading a token: ".
llvm::errs() << "\n";
}
+4 -5
View File
@@ -6,15 +6,14 @@
#include "common/check.h"
#include "executable_semantics/common/error.h"
#include "executable_semantics/common/tracing_flag.h"
#include "executable_semantics/syntax/lexer.h"
#include "executable_semantics/syntax/parse_and_lex_context.h"
#include "executable_semantics/syntax/parser.h"
namespace Carbon {
auto Parse(Nonnull<Arena*> arena, const std::string& input_file_name)
-> std::variant<AST, SyntaxErrorCode> {
auto Parse(Nonnull<Arena*> arena, const std::string& input_file_name,
bool trace) -> std::variant<AST, SyntaxErrorCode> {
FILE* input_file = fopen(input_file_name.c_str(), "r");
if (input_file == nullptr) {
FATAL_PROGRAM_ERROR_NO_LINE() << "Error opening '" << input_file_name
@@ -28,11 +27,11 @@ auto Parse(Nonnull<Arena*> arena, const std::string& input_file_name)
// Prepare other parser arguments.
std::optional<AST> ast = std::nullopt;
ParseAndLexContext context(arena->New<std::string>(input_file_name));
ParseAndLexContext context(arena->New<std::string>(input_file_name), trace);
// Do the parse.
auto parser = Parser(arena, scanner, context, &ast);
if (tracing_output) {
if (trace) {
parser.set_debug_level(1);
}
auto syntax_error_code = parser();
+2 -2
View File
@@ -18,8 +18,8 @@ using SyntaxErrorCode = int;
// Returns the AST representing the contents of the named file, or an error code
// if parsing fails. Allocations go into the provided arena.
auto Parse(Nonnull<Arena*> arena, const std::string& input_file_name)
-> std::variant<Carbon::AST, SyntaxErrorCode>;
auto Parse(Nonnull<Arena*> arena, const std::string& input_file_name,
bool trace) -> std::variant<Carbon::AST, SyntaxErrorCode>;
} // namespace Carbon
@@ -17,8 +17,8 @@ namespace Carbon {
class ParseAndLexContext {
public:
// Creates an instance analyzing the given input file.
ParseAndLexContext(Nonnull<const std::string*> input_file_name)
: input_file_name(input_file_name) {}
ParseAndLexContext(Nonnull<const std::string*> input_file_name, bool trace)
: input_file_name(input_file_name), trace_(trace) {}
// Writes a syntax error diagnostic containing message to standard error.
auto PrintDiagnostic(const std::string& message) -> void;
@@ -28,6 +28,8 @@ class ParseAndLexContext {
static_cast<int>(current_token_position.begin.line));
}
auto trace() -> bool { return trace_; }
// The source range of the token being (or just) lex'd.
location current_token_position;
@@ -35,6 +37,8 @@ class ParseAndLexContext {
// A path to the file processed, relative to the current working directory
// when *this is called.
Nonnull<const std::string*> input_file_name;
bool trace_;
};
} // namespace Carbon