diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2017-07-21 11:52:17 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-07-21 11:52:17 -0700 |
| commit | 3fa85ede1a6d532b0e86c8b71d3f37d30c353aad (patch) | |
| tree | dff607d1927c037f44a6a6fbbf2847f3944f5061 | |
| parent | 85f60e07507f347ef4cb613b6d87521ff10e6c81 (diff) | |
| parent | 9718ab32e91ba632b03ad8d0f5e89a597b225a4c (diff) | |
Merge pull request #132 from tfoleyNV/line-directive-control
Add an API option to control emission of `#line` directives
| -rw-r--r-- | slang.h | 19 | ||||
| -rw-r--r-- | source/slang/compiler.cpp | 2 | ||||
| -rw-r--r-- | source/slang/compiler.h | 11 | ||||
| -rw-r--r-- | source/slang/emit.cpp | 61 | ||||
| -rw-r--r-- | source/slang/slang.cpp | 9 |
5 files changed, 87 insertions, 15 deletions
@@ -96,6 +96,18 @@ extern "C" SLANG_COMPILE_FLAG_NO_CHECKING = 1 << 0, /**< Disable semantic checking as much as possible. */ }; + /*! + @brief Options to control emission of `#line` directives + */ + typedef unsigned int SlangLineDirectiveMode; + enum + { + SLANG_LINE_DIRECTIVE_MODE_DEFAULT = 0, /**< Default behavior: pick behavior base on target. */ + SLANG_LINE_DIRECTIVE_MODE_NONE, /**< Don't emit line directives at all. */ + SLANG_LINE_DIRECTIVE_MODE_STANDARD, /**< Emit standard C-style `#line` directives. */ + SLANG_LINE_DIRECTIVE_MODE_GLSL, /**< Emit GLSL-style directives with file *number* instead of name */ + }; + typedef int SlangSourceLanguage; enum { @@ -177,6 +189,13 @@ extern "C" int enable); /*! + @brief Set whether (and how) `#line` directives hsould be output. + */ + SLANG_API void spSetLineDirectiveMode( + SlangCompileRequest* request, + SlangLineDirectiveMode mode); + + /*! @brief Sets the target for code generation. @param ctx The compilation context. @param target The code generation target. Possible values are: diff --git a/source/slang/compiler.cpp b/source/slang/compiler.cpp index e16b9dec7..81f107bd8 100644 --- a/source/slang/compiler.cpp +++ b/source/slang/compiler.cpp @@ -690,7 +690,7 @@ namespace Slang int id = counter++; String path; - path.append("slang-"); + path.append("slang-dump-"); path.append(id); path.append(ext); diff --git a/source/slang/compiler.h b/source/slang/compiler.h index a7d104b5f..09797664f 100644 --- a/source/slang/compiler.h +++ b/source/slang/compiler.h @@ -48,6 +48,14 @@ namespace Slang ReflectionJSON = SLANG_REFLECTION_JSON, }; + enum class LineDirectiveMode : SlangLineDirectiveMode + { + Default = SLANG_LINE_DIRECTIVE_MODE_DEFAULT, + None = SLANG_LINE_DIRECTIVE_MODE_NONE, + Standard = SLANG_LINE_DIRECTIVE_MODE_STANDARD, + GLSL = SLANG_LINE_DIRECTIVE_MODE_GLSL, + }; + enum class ResultFormat { None, @@ -210,6 +218,9 @@ namespace Slang // Should we dump intermediate results along the way, for debugging? bool shouldDumpIntermediates = false; + // How should `#line` directives be emitted (if at all)? + LineDirectiveMode lineDirectiveMode = LineDirectiveMode::Default; + // Output stuff DiagnosticSink mSink; String mDiagnosticOutput; diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index ee90510a9..61e473c7d 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -511,16 +511,33 @@ struct EmitVisitor bool shouldUseGLSLStyleLineDirective = false; - // TODO: Eventually we should give he user a proper API - // and command-line mechanism to control what kind of line - // directives we output (and to turn the feature off - // completely), but for now we always emit C-style line - // directives *unless* the final target language is raw - // GLSL code (all other targets will eventually pass - // through glslang, which supports C-style line directives). - if (context->shared->finalTarget == CodeGenTarget::GLSL) + auto mode = context->shared->entryPoint->compileRequest->lineDirectiveMode; + switch (mode) { + case LineDirectiveMode::None: + SLANG_UNEXPECTED("should not trying to emit '#line' directive"); + return; + + case LineDirectiveMode::Default: + default: + // To try to make the default behavior reasonable, we will + // always use C-style line directives (to give the user + // good source locations on error messages from downstream + // compilers) *unless* they requested raw GLSL as the + // output (in which case we want to maximize compatibility + // with downstream tools). + if (context->shared->finalTarget == CodeGenTarget::GLSL) + { + shouldUseGLSLStyleLineDirective = true; + } + break; + + case LineDirectiveMode::Standard: + break; + + case LineDirectiveMode::GLSL: shouldUseGLSLStyleLineDirective = true; + break; } if(shouldUseGLSLStyleLineDirective) @@ -597,6 +614,12 @@ struct EmitVisitor void emitLineDirectiveIfNeeded( CodePosition const& sourceLocation) { + // Don't do any of this work if the user has requested that we + // not emit line directives. + auto mode = context->shared->entryPoint->compileRequest->lineDirectiveMode; + if (mode == LineDirectiveMode::None) + return; + // Ignore invalid source locations if(sourceLocation.Line <= 0) return; @@ -675,13 +698,16 @@ struct EmitVisitor void emitTokenWithLocation(Token const& token) { - if( token.Position.FileName.Length() != 0 ) - { - advanceToSourceLocation(token.Position); - } - else + auto mode = context->shared->entryPoint->compileRequest->lineDirectiveMode; + if (mode == LineDirectiveMode::None) + return; + + + if ((mode == LineDirectiveMode::None) + || token.Position.FileName.Length() == 0) { - // If we don't have the original position info, we need to play + // If we don't have the original position info, or we are in the + // mode where the user didn't want line directives, we need to play // it safe and emit whitespace to line things up nicely if(token.flags & TokenFlag::AtStartOfLine) @@ -691,6 +717,13 @@ struct EmitVisitor else // if(token.flags & TokenFlag::AfterWhitespace) Emit(" "); } + else + { + // If location information is available, and we are emitting + // such information, then just advance our tracking location + // to the right place. + advanceToSourceLocation(token.Position); + } // Emit the raw textual content of the token emit(token.Content); diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index caa50ad0e..65f12fe59 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -675,6 +675,15 @@ SLANG_API void spSetDumpIntermediates( REQ(request)->shouldDumpIntermediates = enable != 0; } +SLANG_API void spSetLineDirectiveMode( + SlangCompileRequest* request, + SlangLineDirectiveMode mode) +{ + // TODO: validation + + REQ(request)->lineDirectiveMode = Slang::LineDirectiveMode(mode); +} + SLANG_API void spSetCodeGenTarget( SlangCompileRequest* request, |
