Index: src/CodeGen/CodeGenerator.cc
===================================================================
--- src/CodeGen/CodeGenerator.cc	(revision d48e529cc868a651ee3760fbd3cf7b1570a205b4)
+++ src/CodeGen/CodeGenerator.cc	(revision 47b5b6376ade4e44d786f34c4ff34e1a3d2973c0)
@@ -79,11 +79,12 @@
 	}
 
-	/* Using updateLocation at the beginning of a node and nextLine
+	/* Using updateLocation at the beginning of a node and endl
 	 * within a node should become the method of formating.
 	 */
 	void CodeGenerator::updateLocation( CodeLocation const & to ) {
-		if ( !lineMarks ) {
-			return;
-		} else if ( currentLocation.followedBy( to, 0 ) ) {
+		// skip if linemarks shouldn't appear or if codelocation is unset
+		if ( !lineMarks || to.isUnset() ) return;
+
+		if ( currentLocation.followedBy( to, 0 ) ) {
 			return;
 		} else if ( currentLocation.followedBy( to, 1 ) ) {
@@ -105,11 +106,16 @@
 	}
 
-	void CodeGenerator::nextLine() {
-		if ( !lineMarks ) {
-			output << "\n" << indent << std::flush;
-		}
-	}
-
-	CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( CodeGenerator::tabsize ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
+	// replace endl
+	ostream & CodeGenerator::LineEnder::operator()( ostream & os ) const {
+		// if ( !cg.lineMarks ) {
+		// 	os << "\n" << cg.indent << std::flush;
+		// }
+		os << "\n" << std::flush;
+		cg.currentLocation.first_line++;
+		// os << "/* did endl; current loc is: " << cg.currentLocation.first_line << "*/";
+		return os;
+	}
+
+	CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( CodeGenerator::tabsize ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ), endl( *this ) {}
 
 	string CodeGenerator::mangleName( DeclarationWithType * decl ) {
@@ -140,8 +146,9 @@
 
 	// *** BaseSyntaxNode
-	void CodeGenerator::previsit( BaseSyntaxNode * ) {
+	void CodeGenerator::previsit( BaseSyntaxNode * node ) {
 		// turn off automatic recursion for all nodes, to allow each visitor to
 		// precisely control the order in which its children are visited.
 		visit_children = false;
+		updateLocation( node );
 	}
 
@@ -165,5 +172,4 @@
 		asmName( functionDecl );
 
-		// acceptAll( functionDecl->get_oldDecls(), *visitor );
 		if ( functionDecl->get_statements() ) {
 			functionDecl->get_statements()->accept( *visitor );
@@ -216,5 +222,4 @@
 			++indent;
 			for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
-				updateLocation( *i );
 				output << indent;
 				(*i)->accept( *visitor );
@@ -240,5 +245,4 @@
 	void CodeGenerator::postvisit( EnumDecl * enumDecl ) {
 		extension( enumDecl );
-		updateLocation( enumDecl );
 		output << "enum ";
 		genAttributes( enumDecl->get_attributes() );
@@ -255,5 +259,4 @@
 				ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
 				assert( obj );
-				updateLocation( obj );
 				output << indent << mangleName( obj );
 				if ( obj->get_init() ) {
@@ -278,5 +281,4 @@
 	void CodeGenerator::postvisit( TypedefDecl * typeDecl ) {
 		assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
-		updateLocation( typeDecl );
 		output << "typedef ";
 		output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl;
@@ -337,12 +339,12 @@
 	}
 
-	void CodeGenerator::postvisit( __attribute__((unused)) ConstructorInit * init ){
+	void CodeGenerator::postvisit( ConstructorInit * init ){
 		assertf( ! genC, "ConstructorInit nodes should not reach code generation." );
 		// pseudo-output for constructor/destructor pairs
-		output << "<ctorinit>{" << std::endl << ++indent << "ctor: ";
+		output << "<ctorinit>{" << endl << ++indent << "ctor: ";
 		maybeAccept( init->get_ctor(), *visitor );
-		output << ", " << std::endl << indent << "dtor: ";
+		output << ", " << endl << indent << "dtor: ";
 		maybeAccept( init->get_dtor(), *visitor );
-		output << std::endl << --indent << "}";
+		output << endl << --indent << "}";
 	}
 
@@ -763,11 +765,9 @@
 	void CodeGenerator::postvisit( StmtExpr * stmtExpr ) {
 		std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
-		updateLocation( stmtExpr );
-		output << "({" << std::endl;
+		output << "({" << endl;
 		++indent;
 		unsigned int numStmts = stmts.size();
 		unsigned int i = 0;
 		for ( Statement * stmt : stmts ) {
-			updateLocation( stmt );
 			output << indent << printLabels( stmt->get_labels() );
 			if ( i+1 == numStmts ) {
@@ -860,5 +860,4 @@
 
 	void CodeGenerator::postvisit( IfStmt * ifStmt ) {
-		updateLocation( ifStmt );
 		output << "if ( ";
 		ifStmt->get_condition()->accept( *visitor );
@@ -874,10 +873,9 @@
 
 	void CodeGenerator::postvisit( SwitchStmt * switchStmt ) {
-		updateLocation( switchStmt );
 		output << "switch ( " ;
 		switchStmt->get_condition()->accept( *visitor );
 		output << " ) ";
 
-		output << "{" << std::endl;
+		output << "{" << endl;
 		++indent;
 		acceptAll( switchStmt->get_statements(), *visitor );
@@ -887,5 +885,4 @@
 
 	void CodeGenerator::postvisit( CaseStmt * caseStmt ) {
-		updateLocation( caseStmt );
 		if ( caseStmt->isDefault()) {
 			output << "default";
@@ -894,5 +891,5 @@
 			caseStmt->get_condition()->accept( *visitor );
 		} // if
-		output << ":\n";
+		output << ":" << endl;
 
 		std::list<Statement *> sts = caseStmt->get_statements();
Index: src/CodeGen/CodeGenerator.h
===================================================================
--- src/CodeGen/CodeGenerator.h	(revision d48e529cc868a651ee3760fbd3cf7b1570a205b4)
+++ src/CodeGen/CodeGenerator.h	(revision 47b5b6376ade4e44d786f34c4ff34e1a3d2973c0)
@@ -31,6 +31,4 @@
 
 		CodeGenerator( std::ostream &os, bool pretty = false, bool genC = false, bool lineMarks = false );
-		CodeGenerator( std::ostream &os, std::string, int indent = 0, bool infun = false );
-		CodeGenerator( std::ostream &os, char *, int indent = 0, bool infun = false );
 
 		//*** Turn off visit_children for all nodes
@@ -125,16 +123,22 @@
 
 		void updateLocation( BaseSyntaxNode const * to );
+		struct LineEnder {
+			CodeGenerator & cg;
+			LineEnder( CodeGenerator & cg ) : cg( cg ) {}
+			std::ostream & operator()(std::ostream &) const;
+		};
 	  private:
 		Indenter indent;
-		bool insideFunction;
-		std::ostream &output;
+		std::ostream & output;
 		LabelPrinter printLabels;
 		bool pretty = false;  // pretty print
 		bool genC = false;    // true if output has to be C code
 		bool lineMarks = false;
+	public:
+		LineEnder endl;
+	private:
 
 		CodeLocation currentLocation;
 		void updateLocation( CodeLocation const & to );
-		void nextLine();
 
 		void handleStorageClass( DeclarationWithType *decl );
@@ -163,4 +167,8 @@
 	/// returns C-compatible name of declaration
 	std::string genName( DeclarationWithType * decl );
+
+	inline std::ostream & operator<<( std::ostream & os, const CodeGenerator::LineEnder & endl ) {
+		return endl( os );
+	}
 } // namespace CodeGen
 
Index: src/CodeGen/Generate.cc
===================================================================
--- src/CodeGen/Generate.cc	(revision d48e529cc868a651ee3760fbd3cf7b1570a205b4)
+++ src/CodeGen/Generate.cc	(revision 47b5b6376ade4e44d786f34c4ff34e1a3d2973c0)
@@ -57,5 +57,5 @@
 					os << ";";
 				} // if
-				os << std::endl;
+				os << cgv.pass.endl;
 			} // if
 		} // for
