Clean up the Globals section so that we can generate extern declarations
for all globals out using this command after we carve it out into
globals.cc:

  grep ';' globals.cc |perl -pwe 's/[=(].*/;/' |perl -pwe 's/^[^\/# ]/extern $&/' > globals.h

The first perl command strips out initializers. The second prepends
'extern'. This simplistic approach requires each global definition to
lie all on one line.
This commit is contained in:
Kartik K. Agaram 2016-08-28 15:21:12 -07:00
parent fd6d8612ed
commit 1ba81b0f57
3 changed files with 32 additions and 20 deletions

View File

@ -98,6 +98,11 @@
#include "function_list" // by convention, files ending with '_list' are auto-generated
// Globals
//
// (Code in this section should strictly consist only of single-line variable
// definitions; the makefile will simplistically auto-generate extern
// declarations for them.)
//
// End Globals
int main(int argc, char* argv[]) {

View File

@ -10,16 +10,18 @@
:(before "End Types")
typedef void (*test_fn)(void);
:(before "End Globals")
:(before "Globals")
// move a global ahead into types that we can't generate an extern declaration for
const test_fn Tests[] = {
#include "test_list" // auto-generated; see makefile
};
:(before "End Globals")
bool Run_tests = false;
bool Passed = true; // set this to false inside any test to indicate failure
long Num_failures = 0;
:(before "End Includes")
#define CHECK(X) \
if (!(X)) { \
++Num_failures; \

View File

@ -155,11 +155,19 @@ string trace_stream::readable_contents(string label) {
trace_stream* Trace_stream = NULL;
int Trace_errors = 0; // used only when Trace_stream is NULL
:(before "End Includes")
#define CLEAR_TRACE delete Trace_stream, Trace_stream = new trace_stream;
// Top-level helper. IMPORTANT: can't nest
#define trace(...) !Trace_stream ? cerr /*print nothing*/ : Trace_stream->stream(__VA_ARGS__)
// Just for debugging; 'git log' should never show any calls to 'dbg'.
#define dbg trace(0, "a")
#define DUMP(label) if (Trace_stream) cerr << Trace_stream->readable_contents(label);
// Errors are a special layer.
#define raise (!Trace_stream ? (tb_shutdown(),++Trace_errors,cerr) /*do print*/ : Trace_stream->stream(Error_depth, "error"))
// Inside tests, fail any tests that displayed (unexpected) errors.
// Expected errors in tests should always be hidden and silently checked for.
:(before "End Test Teardown")
@ -168,9 +176,6 @@ if (Passed && !Hide_errors && trace_count("error") > 0) {
++Num_failures;
}
// Just for debugging.
#define dbg trace(0, "a")
:(before "End Types")
struct end {};
:(code)
@ -180,26 +185,26 @@ ostream& operator<<(ostream& os, unused end) {
}
:(before "End Globals")
#define CLEAR_TRACE delete Trace_stream, Trace_stream = new trace_stream;
#define DUMP(label) if (Trace_stream) cerr << Trace_stream->readable_contents(label);
bool Save_trace = false;
// Trace_stream is a resource, lease_tracer uses RAII to manage it.
:(before "End Types")
struct lease_tracer {
lease_tracer() { Trace_stream = new trace_stream; }
~lease_tracer() {
if (!Trace_stream) return; // in case tests close Trace_stream
if (Save_trace) {
ofstream fout("last_trace");
fout << Trace_stream->readable_contents("");
fout.close();
}
delete Trace_stream, Trace_stream = NULL;
}
lease_tracer();
~lease_tracer();
};
:(code)
lease_tracer::lease_tracer() { Trace_stream = new trace_stream; }
lease_tracer::~lease_tracer() {
if (!Trace_stream) return; // in case tests close Trace_stream
if (Save_trace) {
ofstream fout("last_trace");
fout << Trace_stream->readable_contents("");
fout.close();
}
delete Trace_stream, Trace_stream = NULL;
}
:(before "End Includes")
#define START_TRACING_UNTIL_END_OF_SCOPE lease_tracer leased_tracer;
:(before "End Test Setup")
START_TRACING_UNTIL_END_OF_SCOPE