From 411b24361e31503171b2940ebd44dc436550a716 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 19 Jun 2017 13:20:53 -0700 Subject: Don't emit redundant `#line` directives If the line number for the next token is within a small range, then go ahead and output newlines to get caught up, rather than emit a `#line` directive. This saves a small amount of clutter, and in the particular case where the number of lines is 1, it stops our current behavior of putting a directive on each line. --- source/slang/emit.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'source') diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index ef5df830f..21ae498b6 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -1424,6 +1424,9 @@ static void emitLineDirective( emitRawText(context, charBuffer); break; + // The incoming file path might use `/` and/or `\\` as + // a directory separator. We want to canonicalize this. + // // TODO: should probably canonicalize paths to not use backslash somewhere else // in the compilation pipeline... case '\\': @@ -1463,7 +1466,26 @@ static void advanceToSourceLocation( || sourceLocation.Line != context->loc.Line || sourceLocation.Col < context->loc.Col) { - emitLineDirectiveAndUpdateSourceLocation(context, sourceLocation); + // Special case: if we are in the same file, and within a small number + // of lines of the target location, then go ahead and output newlines + // to get us caught up. + enum { kSmallLineCount = 3 }; + auto lineDiff = sourceLocation.Line - context->loc.Line; + if(sourceLocation.FileName == context->loc.FileName + && sourceLocation.Line > context->loc.Line + && lineDiff <= kSmallLineCount) + { + for(int ii = 0; ii < lineDiff; ++ii ) + { + Emit(context, "\n"); + } + assert(sourceLocation.Line == context->loc.Line); + } + else + { + // Go ahead and output a `#line` directive to get us caught up + emitLineDirectiveAndUpdateSourceLocation(context, sourceLocation); + } } // Now indent up to the appropriate column, so that error messages -- cgit v1.2.3