Index: src/CodeGen/LineStream.cc
===================================================================
--- src/CodeGen/LineStream.cc	(revision f57668a590aa4cede590bc26725f85c38abb833a)
+++ src/CodeGen/LineStream.cc	(revision f57668a590aa4cede590bc26725f85c38abb833a)
@@ -0,0 +1,101 @@
+//
+// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
+//
+// The contents of this file are covered under the licence agreement in the
+// file "LICENCE" distributed with Cforall.
+//
+// LineStream.cc -- Modified stream that inserts line directives into output.
+//
+// Author           : Andrew Beach
+// Created On       : Thr May 4 13:15:00 2017
+// Last Modified By : Andrew Beach
+// Last Modified On : Fri May 5 14:29:00 2017
+// Update Count     : 0
+//
+
+#include "LineStream.h"
+
+namespace CodeGen {
+
+	LineStream::LineStream(std::ostream & baseStream, bool insertLines) :
+		baseStream(baseStream), insertLines(insertLines)
+	{}
+
+	void LineStream::printLineDirective(CodeLocation const & location) {
+		baseStream << "\n# " << location.linenumber;
+		if (std::string("") != location.filename) {
+			baseStream << " \"" << location.filename  << '"';
+		}
+		baseStream << '\n';
+	}
+
+	bool LineStream::actualDiffersFromExpected() const {
+		return actualLocation.isSet() &&
+			// actualLocation & expectedLocation must match at the line.
+			(actualLocation.linenumber != expectedLocation.linenumber ||
+			 actualLocation.filename != expectedLocation.filename);
+	}
+
+	void LineStream::emptyBuffer(bool addNewline) {
+		if (actualDiffersFromExpected()) {
+			printLineDirective(actualLocation);
+			expectedLocation = actualLocation;
+		}
+		actualLocation.unset();
+
+		if (addNewLine) {
+			expectedLocation.linenumber += 1;
+			buffer.put('\n');
+		}
+
+		baseStream << buffer.str() << std::flush;
+		buffer.str("");
+	}
+
+	void LineStream::setLoc(CodeLocation const & location) {
+		if (insertLines) {
+			if (expectedLocation.isUnset()) {
+				expectedLocation = actualLocation = location;
+			} else if (actualLocation.isUnset()) {
+				actualLocation = location;
+			} else if (actualLocation.filename != location.filename) {
+				emptyBuffer(true);
+				actualLocation = location;
+			} else if (location.linenumber <= actualLocation.linenumber){
+				actualLocation.linenumber = location.linenumber;
+			}
+		}
+	}
+
+	LineStream & LineStream::operator<<(char const * str) {
+		buffer << str;
+		return *this;
+	}
+
+	LineStream & LineStream::operator<<(std::string const & str) {
+		buffer << str;
+		return *this;
+	}
+
+	LineStream & LineStream::operator<<(StreamFlag flag) {
+		static StringFlag const endlCopy = std::endl;
+		if (!insertLines) {
+			baseStream << flag;
+		} else if (endlCopy == flag) {
+			emptyBuffer(true);
+		} else {
+			buffer << flag;
+		}
+		return *this;
+	}
+
+	LineStream & LineStream::flush() {
+		if (insertLines) {
+			emptyBuffer(false);
+		} else {
+			baseStream.flush();
+		}
+		return *this;
+	}
+
+} // CodeGen
Index: src/CodeGen/LineStream.h
===================================================================
--- src/CodeGen/LineStream.h	(revision f57668a590aa4cede590bc26725f85c38abb833a)
+++ src/CodeGen/LineStream.h	(revision f57668a590aa4cede590bc26725f85c38abb833a)
@@ -0,0 +1,59 @@
+//
+// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
+//
+// The contents of this file are covered under the licence agreement in the
+// file "LICENCE" distributed with Cforall.
+//
+// LineStream.h -- Modified stream that inserts line directives into output.
+//
+// Author           : Andrew Beach
+// Created On       : Wed May 4 09:15:00 2017
+// Last Modified By : Andrew Beach
+// Last Modified On : Fri May 5 14:29:00 2017
+// Update Count     : 1
+//
+
+#ifndef LINE_STREAM_H
+#define LINE_STREAM_H
+
+#include <ostream>
+#include <sstream>
+#include <string>
+
+#include "Common/utility.h"
+
+namespace CodeGen {
+
+	class LineStream : public std::ostream {
+		std::ostream & baseStream;
+		std::ostringstream buffer;
+
+		bool const insertLines;
+
+		CodeLocation actualLocation;
+		CodeLocation expectedLocation;
+
+		void printLineDirective(CodeLocation const & location);
+		bool actualDiffersFromExpected() const;
+		void emptyBuffer(bool addNewline);
+
+	public:
+		typedef std::ostream &(*StreamFlag)(std::ostream &);
+
+		LineStream(std::ostream & baseStream, bool insertLines) :
+			baseStream(baseStream), insertLines(insertLines)
+		{}
+
+		/// Update the currentLocation in source code.
+		void setLoc(CodeLocation const & location);
+
+		/// Formated output is buffered until flushed.
+		std::ostream & operator<<(char const *str);
+		std::ostream & operator<<(std::string str);
+		std::ostream & operator<<(StreamFlag flag);
+
+	}; // LineStream
+
+} // CodeGen
+
+#endif // LINE_STREAM_H
Index: src/Common/utility.h
===================================================================
--- src/Common/utility.h	(revision dba6db9a6025d74a9abed179c7aefcec1d0d09ab)
+++ src/Common/utility.h	(revision f57668a590aa4cede590bc26725f85c38abb833a)
@@ -9,7 +9,7 @@
 // Author           : Richard C. Bilson
 // Created On       : Mon May 18 07:44:20 2015
-// Last Modified By : Peter A. Buhr
-// Last Modified On : Wed Dec 14 21:25:25 2016
-// Update Count     : 31
+// Last Modified By : Andrew Beach
+// Last Modified On : Fri May 5 11:03:00 2017
+// Update Count     : 32
 //
 
@@ -322,17 +322,34 @@
 	std::string filename;
 
-	CodeLocation() 
+    /// Create a new unset CodeLocation.
+	CodeLocation()
 		: linenumber( -1 )
 		, filename("")
 	{}
 
+    /// Create a new CodeLocation with the given values.
 	CodeLocation( const char* filename, int lineno )
 		: linenumber( lineno )
 		, filename(filename ? filename : "")
 	{}
+
+    bool isSet () const {
+        return -1 != linenumber;
+    }
+
+    bool isUnset () const {
+        return !isSet();
+    }
+
+	void unset () {
+		linenumber = -1;
+		filename = "";
+	}
+
+	// Use field access for set.
 };
 
 inline std::string to_string( const CodeLocation& location ) {
-	return location.linenumber >= 0 ? location.filename + ":" + std::to_string(location.linenumber) + " " : "";
+	return location.isSet() ? location.filename + ":" + std::to_string(location.linenumber) + " " : "";
 }
 #endif // _UTILITY_H
