diff --git a/Test/baseResults/preprocessor.line.frag.err b/Test/baseResults/preprocessor.line.frag.err new file mode 100644 index 00000000..5f177e6c --- /dev/null +++ b/Test/baseResults/preprocessor.line.frag.err @@ -0,0 +1,2 @@ +Warning, version 310 is not yet complete; most version-specific features are present, but some are missing. + diff --git a/Test/baseResults/preprocessor.line.frag.out b/Test/baseResults/preprocessor.line.frag.out new file mode 100644 index 00000000..2145b6f0 --- /dev/null +++ b/Test/baseResults/preprocessor.line.frag.out @@ -0,0 +1,6 @@ +#version 310 es + +#line 1 2 +#pragma something +void main(){ } + diff --git a/Test/baseResults/preprocessor.line.vert.out b/Test/baseResults/preprocessor.line.vert.out index 2893521b..5d9709b3 100644 --- a/Test/baseResults/preprocessor.line.vert.out +++ b/Test/baseResults/preprocessor.line.vert.out @@ -7,7 +7,6 @@ #line 2 #line 0 - #line 4 #line 8 diff --git a/Test/preprocessor.line.frag b/Test/preprocessor.line.frag new file mode 100644 index 00000000..0b7ac766 --- /dev/null +++ b/Test/preprocessor.line.frag @@ -0,0 +1,4 @@ +#version 310 es +#line 1 2 +#pragma something +void main() {} diff --git a/Test/test-preprocessor-list b/Test/test-preprocessor-list index b35d03f4..d4fef577 100644 --- a/Test/test-preprocessor-list +++ b/Test/test-preprocessor-list @@ -3,5 +3,6 @@ preprocessor.errors.vert preprocessor.extensions.vert preprocessor.function_macro.vert preprocessor.line.vert +preprocessor.line.frag preprocessor.pragma.vert preprocessor.simple.vert diff --git a/glslang/MachineIndependent/ParseHelper.cpp b/glslang/MachineIndependent/ParseHelper.cpp index 34d3fbfc..8527fd56 100644 --- a/glslang/MachineIndependent/ParseHelper.cpp +++ b/glslang/MachineIndependent/ParseHelper.cpp @@ -2654,6 +2654,19 @@ void TParseContext::updateImplicitArraySize(TSourceLoc loc, TIntermNode *node, i symbol->getWritableType().setImplicitArraySize(index + 1); } +// Returns true if the first argument to the #line directive is the line number for the next line. +// +// Desktop, pre-version 3.30: "After processing this directive +// (including its new-line), the implementation will behave as if it is compiling at line number line+1 and +// source string number source-string-number." +// +// Desktop, version 3.30 and later, and ES: "After processing this directive +// (including its new-line), the implementation will behave as if it is compiling at line number line and +// source string number source-string-number. +bool TParseContext::lineDirectiveShouldSetNextLine() const { + return profile == EEsProfile || version >= 330; +} + // // Enforce non-initializer type/qualifier rules. // diff --git a/glslang/MachineIndependent/ParseHelper.h b/glslang/MachineIndependent/ParseHelper.h index 3d933d9f..ee8ff0ab 100644 --- a/glslang/MachineIndependent/ParseHelper.h +++ b/glslang/MachineIndependent/ParseHelper.h @@ -212,6 +212,8 @@ public: void setCurrentString(int string) { currentScanner->setString(string); } void setScanner(TInputScanner* scanner) { currentScanner = scanner; } + bool lineDirectiveShouldSetNextLine() const; + void notifyVersion(int line, int version, const char* type_string); void notifyErrorDirective(int line, const char* error_message); void notifyLineDirective(int line, bool has_source, int source); diff --git a/glslang/MachineIndependent/ShaderLang.cpp b/glslang/MachineIndependent/ShaderLang.cpp index a1516a01..10709c59 100644 --- a/glslang/MachineIndependent/ShaderLang.cpp +++ b/glslang/MachineIndependent/ShaderLang.cpp @@ -639,18 +639,25 @@ struct DoPreprocessing { adjustLine(line); outputStream << "#extension " << extension << " : " << behavior; }); - parseContext.setLineCallback([&lastLine, &outputStream]( - int line, bool hasSource, int sourceNum) { + parseContext.setLineCallback([&lastLine, &outputStream, &parseContext]( + int newLineNo, bool hasSource, int sourceNum) { // SourceNum is the number of the source-string that is being parsed. if (lastLine != -1) { outputStream << std::endl; } - outputStream << "#line " << line; + outputStream << "#line " << newLineNo; if (hasSource) { outputStream << " " << sourceNum; } + if (parseContext.lineDirectiveShouldSetNextLine()) { + // newLineNo is the new line number for the line following the #line + // directive. So the new line number for the current line is + newLineNo -= 1; + } outputStream << std::endl; - lastLine = std::max(line - 1, 1); + // Line number starts from 1. And we are at the next line of the #line + // directive now. So lastLine (from 0) should be (newLineNo - 1) + 1. + lastLine = newLineNo; }); diff --git a/glslang/MachineIndependent/preprocessor/Pp.cpp b/glslang/MachineIndependent/preprocessor/Pp.cpp index 6b8cdc0a..84b40735 100644 --- a/glslang/MachineIndependent/preprocessor/Pp.cpp +++ b/glslang/MachineIndependent/preprocessor/Pp.cpp @@ -641,14 +641,7 @@ int TPpContext::CPPline(TPpToken* ppToken) if (token == '\n') ++lineRes; - // Desktop, pre-version 3.30: "After processing this directive - // (including its new-line), the implementation will behave as if it is compiling at line number line+1 and - // source string number source-string-number." - // - // Desktop, version 3.30 and later, and ES: "After processing this directive - // (including its new-line), the implementation will behave as if it is compiling at line number line and - // source string number source-string-number. - if (parseContext.profile == EEsProfile || parseContext.version >= 330) + if (parseContext.lineDirectiveShouldSetNextLine()) --lineRes; parseContext.setCurrentLine(lineRes);