Skip to content

Commit 19d929c

Browse files
authored
Convert to using DEBUG macros (#2497)
This means that debugging/tracing can now be enabled and controlled centrally without managing and passing state around the codebase.
1 parent f346478 commit 19d929c

20 files changed

Lines changed: 272 additions & 647 deletions

src/asm2wasm.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@
3838
#include "pass.h"
3939
#include "passes/passes.h"
4040
#include "shared-constants.h"
41+
#include "support/debug.h"
4142
#include "wasm-builder.h"
4243
#include "wasm-emscripten.h"
4344
#include "wasm-module-building.h"
4445
#include "wasm.h"
4546

47+
#define DEBUG_TYPE "asm2wasm"
48+
4649
namespace wasm {
4750

4851
using namespace cashew;
@@ -1780,9 +1783,7 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
17801783
Function* Asm2WasmBuilder::processFunction(Ref ast) {
17811784
auto name = ast[1]->getIString();
17821785

1783-
if (debug) {
1784-
std::cout << "asm2wasming func: " << ast[1]->getIString().str << '\n';
1785-
}
1786+
BYN_TRACE("asm2wasming func: " << ast[1]->getIString().str << '\n');
17861787

17871788
auto function = new Function;
17881789
function->name = name;

src/binaryen-c.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3796,7 +3796,7 @@ void BinaryenModulePrintAsmjs(BinaryenModuleRef module) {
37963796
Wasm2JSBuilder wasm2js(flags, globalPassOptions);
37973797
Ref asmjs = wasm2js.processWasm(wasm);
37983798
JSPrinter jser(true, true, asmjs);
3799-
Output out("", Flags::Text, Flags::Release); // stdout
3799+
Output out("", Flags::Text); // stdout
38003800
Wasm2JSGlue glue(*wasm, out, flags, "asmFunc");
38013801
glue.emitPre();
38023802
jser.printAst();
@@ -3917,8 +3917,8 @@ static BinaryenBufferSizes writeModule(BinaryenModuleRef module,
39173917
char* sourceMap,
39183918
size_t sourceMapSize) {
39193919
Module* wasm = (Module*)module;
3920-
BufferWithRandomAccess buffer(false);
3921-
WasmBinaryWriter writer(wasm, buffer, false);
3920+
BufferWithRandomAccess buffer;
3921+
WasmBinaryWriter writer(wasm, buffer);
39223922
writer.setNamesSection(globalPassOptions.debugInfo);
39233923
std::ostringstream os;
39243924
if (sourceMapUrl) {
@@ -3992,8 +3992,8 @@ BinaryenModuleAllocateAndWrite(BinaryenModuleRef module,
39923992
}
39933993

39943994
Module* wasm = (Module*)module;
3995-
BufferWithRandomAccess buffer(false);
3996-
WasmBinaryWriter writer(wasm, buffer, false);
3995+
BufferWithRandomAccess buffer;
3996+
WasmBinaryWriter writer(wasm, buffer);
39973997
writer.setNamesSection(globalPassOptions.debugInfo);
39983998
std::ostringstream os;
39993999
if (sourceMapUrl) {
@@ -4036,7 +4036,7 @@ BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize) {
40364036
buffer.resize(inputSize);
40374037
std::copy_n(input, inputSize, buffer.begin());
40384038
try {
4039-
WasmBinaryBuilder parser(*wasm, buffer, false);
4039+
WasmBinaryBuilder parser(*wasm, buffer);
40404040
parser.read();
40414041
} catch (ParseException& p) {
40424042
p.dump(std::cerr);

src/support/file.cpp

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
*/
1616

1717
#include "support/file.h"
18+
#include "support/debug.h"
1819

1920
#include <cstdint>
2021
#include <cstdlib>
2122
#include <iostream>
2223
#include <limits>
2324

24-
std::vector<char> wasm::read_stdin(Flags::DebugOption debug) {
25-
if (debug == Flags::Debug) {
26-
std::cerr << "Loading stdin..." << std::endl;
27-
}
25+
#define DEBUG_TYPE "file"
26+
27+
std::vector<char> wasm::read_stdin() {
28+
BYN_TRACE("Loading stdin...\n");
2829
std::vector<char> input;
2930
char c;
3031
while (std::cin.get(c) && !std::cin.eof()) {
@@ -34,12 +35,8 @@ std::vector<char> wasm::read_stdin(Flags::DebugOption debug) {
3435
}
3536

3637
template<typename T>
37-
T wasm::read_file(const std::string& filename,
38-
Flags::BinaryOption binary,
39-
Flags::DebugOption debug) {
40-
if (debug == Flags::Debug) {
41-
std::cerr << "Loading '" << filename << "'..." << std::endl;
42-
}
38+
T wasm::read_file(const std::string& filename, Flags::BinaryOption binary) {
39+
BYN_TRACE("Loading '" << filename << "'...\n");
4340
std::ifstream infile;
4441
std::ios_base::openmode flags = std::ifstream::in;
4542
if (binary == Flags::Binary) {
@@ -81,28 +78,22 @@ std::string wasm::read_possible_response_file(const std::string& input) {
8178
if (input.size() == 0 || input[0] != '@') {
8279
return input;
8380
}
84-
return wasm::read_file<std::string>(
85-
input.substr(1), Flags::Text, Flags::Release);
81+
return wasm::read_file<std::string>(input.substr(1), Flags::Text);
8682
}
8783

8884
// Explicit instantiations for the explicit specializations.
89-
template std::string
90-
wasm::read_file<>(const std::string&, Flags::BinaryOption, Flags::DebugOption);
91-
template std::vector<char>
92-
wasm::read_file<>(const std::string&, Flags::BinaryOption, Flags::DebugOption);
85+
template std::string wasm::read_file<>(const std::string&, Flags::BinaryOption);
86+
template std::vector<char> wasm::read_file<>(const std::string&,
87+
Flags::BinaryOption);
9388

94-
wasm::Output::Output(const std::string& filename,
95-
Flags::BinaryOption binary,
96-
Flags::DebugOption debug)
97-
: outfile(), out([this, filename, binary, debug]() {
89+
wasm::Output::Output(const std::string& filename, Flags::BinaryOption binary)
90+
: outfile(), out([this, filename, binary]() {
9891
if (filename == "-") {
9992
return std::cout.rdbuf();
10093
}
10194
std::streambuf* buffer;
10295
if (filename.size()) {
103-
if (debug == Flags::Debug) {
104-
std::cerr << "Opening '" << filename << "'" << std::endl;
105-
}
96+
BYN_TRACE("Opening '" << filename << "'\n");
10697
auto flags = std::ofstream::out | std::ofstream::trunc;
10798
if (binary == Flags::Binary) {
10899
flags |= std::ofstream::binary;

src/support/file.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,18 @@ namespace wasm {
3030

3131
namespace Flags {
3232
enum BinaryOption { Binary, Text };
33-
enum DebugOption { Debug, Release };
3433
} // namespace Flags
3534

36-
std::vector<char> read_stdin(Flags::DebugOption);
35+
std::vector<char> read_stdin();
3736

3837
template<typename T>
39-
T read_file(const std::string& filename,
40-
Flags::BinaryOption binary,
41-
Flags::DebugOption debug);
38+
T read_file(const std::string& filename, Flags::BinaryOption binary);
4239

4340
// Declare the valid explicit specializations.
44-
extern template std::string
45-
read_file<>(const std::string&, Flags::BinaryOption, Flags::DebugOption);
46-
extern template std::vector<char>
47-
read_file<>(const std::string&, Flags::BinaryOption, Flags::DebugOption);
41+
extern template std::string read_file<>(const std::string&,
42+
Flags::BinaryOption);
43+
extern template std::vector<char> read_file<>(const std::string&,
44+
Flags::BinaryOption);
4845

4946
// Given a string which may be a response file (i.e., a filename starting
5047
// with "@"), if it is a response file read it and return that, or if it
@@ -54,9 +51,7 @@ std::string read_possible_response_file(const std::string&);
5451
class Output {
5552
public:
5653
// An empty filename will open stdout instead.
57-
Output(const std::string& filename,
58-
Flags::BinaryOption binary,
59-
Flags::DebugOption debug);
54+
Output(const std::string& filename, Flags::BinaryOption binary);
6055
~Output() = default;
6156
template<typename T> std::ostream& operator<<(const T& v) { return out << v; }
6257

src/tools/asm2wasm.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,8 @@ int main(int argc, const char* argv[]) {
199199
// debug info is disabled if a map file is not specified with wasm binary
200200
pre.debugInfo =
201201
options.passOptions.debugInfo && (!emitBinary || sourceMapFilename.size());
202-
auto input(read_file<std::vector<char>>(options.extra["infile"],
203-
Flags::Text,
204-
options.debug ? Flags::Debug
205-
: Flags::Release));
202+
auto input(
203+
read_file<std::vector<char>>(options.extra["infile"], Flags::Text));
206204
char* start = pre.process(input.data());
207205

208206
if (options.debug) {
@@ -224,8 +222,7 @@ int main(int argc, const char* argv[]) {
224222
const auto& memInit = options.extra.find("mem init");
225223
if (memInit != options.extra.end()) {
226224
auto filename = memInit->second.c_str();
227-
auto data(read_file<std::vector<char>>(
228-
filename, Flags::Binary, options.debug ? Flags::Debug : Flags::Release));
225+
auto data(read_file<std::vector<char>>(filename, Flags::Binary));
229226
// create the memory segment
230227
Expression* init;
231228
const auto& memBase = options.extra.find("mem base");
@@ -293,7 +290,6 @@ int main(int argc, const char* argv[]) {
293290
std::cerr << "emitting..." << std::endl;
294291
}
295292
ModuleWriter writer;
296-
writer.setDebug(options.debug);
297293
writer.setDebugInfo(options.passOptions.debugInfo);
298294
writer.setSymbolMap(symbolMap);
299295
writer.setBinary(emitBinary);

src/tools/fuzzing.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ class TranslateToFuzzReader {
6565
public:
6666
TranslateToFuzzReader(Module& wasm, std::string& filename)
6767
: wasm(wasm), builder(wasm) {
68-
auto input(
69-
read_file<std::vector<char>>(filename, Flags::Binary, Flags::Release));
68+
auto input(read_file<std::vector<char>>(filename, Flags::Binary));
7069
readData(input);
7170
}
7271

src/tools/wasm-as.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ int main(int argc, const char* argv[]) {
9797
removeSpecificSuffix(options.extra["infile"], ".wast") + ".wasm";
9898
}
9999

100-
auto input(
101-
read_file<std::string>(options.extra["infile"],
102-
Flags::Text,
103-
options.debug ? Flags::Debug : Flags::Release));
100+
auto input(read_file<std::string>(options.extra["infile"], Flags::Text));
104101

105102
Module wasm;
106103

src/tools/wasm-ctor-eval.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,7 @@ int main(int argc, const char* argv[]) {
433433
});
434434
options.parse(argc, argv);
435435

436-
auto input(
437-
read_file<std::string>(options.extra["infile"],
438-
Flags::Text,
439-
options.debug ? Flags::Debug : Flags::Release));
436+
auto input(read_file<std::string>(options.extra["infile"], Flags::Text));
440437

441438
Module wasm;
442439

@@ -445,8 +442,6 @@ int main(int argc, const char* argv[]) {
445442
std::cerr << "reading...\n";
446443
}
447444
ModuleReader reader;
448-
reader.setDebug(options.debug);
449-
450445
try {
451446
reader.read(options.extra["infile"], wasm);
452447
} catch (ParseException& p) {
@@ -488,7 +483,6 @@ int main(int argc, const char* argv[]) {
488483
std::cerr << "writing..." << std::endl;
489484
}
490485
ModuleWriter writer;
491-
writer.setDebug(options.debug);
492486
writer.setBinary(emitBinary);
493487
writer.setDebugInfo(debugInfo);
494488
writer.write(wasm, options.extra["output"]);

src/tools/wasm-dis.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ int main(int argc, const char* argv[]) {
7575
if (options.debug) {
7676
std::cerr << "Printing..." << std::endl;
7777
}
78-
Output output(options.extra["output"],
79-
Flags::Text,
80-
options.debug ? Flags::Debug : Flags::Release);
78+
Output output(options.extra["output"], Flags::Text);
8179
WasmPrinter::printModule(&wasm, output.getStream());
8280
output << '\n';
8381

src/tools/wasm-emscripten-finalize.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ int main(int argc, const char* argv[]) {
277277
// Finally, separate out data segments if relevant (they may have been needed
278278
// for metadata).
279279
if (!dataSegmentFile.empty()) {
280-
Output memInitFile(dataSegmentFile, Flags::Binary, Flags::Release);
280+
Output memInitFile(dataSegmentFile, Flags::Binary);
281281
if (globalBase == INVALID_BASE) {
282282
Fatal() << "globalBase must be set";
283283
}
@@ -296,10 +296,8 @@ int main(int argc, const char* argv[]) {
296296
passRunner.run();
297297
}
298298

299-
auto outputBinaryFlag = emitBinary ? Flags::Binary : Flags::Text;
300-
Output output(outfile, outputBinaryFlag, Flags::Release);
299+
Output output(outfile, emitBinary ? Flags::Binary : Flags::Text);
301300
ModuleWriter writer;
302-
writer.setDebug(options.debug);
303301
writer.setDebugInfo(debugInfo);
304302
// writer.setSymbolMap(symbolMap);
305303
writer.setBinary(emitBinary);

0 commit comments

Comments
 (0)