about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-08-28 15:21:12 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-08-28 15:21:12 -0700
commit1ba81b0f57f965f390e09f40c5f70eb9931c7498 (patch)
tree1d9a10935897804d4c96a6de10baa1a475fb7463
parentfd6d8612edf07ea4a4612fd5263ad4250e06c77f (diff)
downloadmu-1ba81b0f57f965f390e09f40c5f70eb9931c7498.tar.gz
3270
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.
-rw-r--r--000organization.cc5
-rw-r--r--002test.cc6
-rw-r--r--003trace.cc41
3 files changed, 32 insertions, 20 deletions
diff --git a/000organization.cc b/000organization.cc
index cd02d58d..dccbee8e 100644
--- a/000organization.cc
+++ b/000organization.cc
@@ -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[]) {
diff --git a/002test.cc b/002test.cc
index baa2efa4..b9cb0e15 100644
--- a/002test.cc
+++ b/002test.cc
@@ -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; \
diff --git a/003trace.cc b/003trace.cc
index bbe5fb80..d8b92ba1 100644
--- a/003trace.cc
+++ b/003trace.cc
@@ -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