From a10a94dcb86e51d74d7c545d99d98110f88e3081 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Tue, 16 Aug 2022 16:24:37 -0400 Subject: Add some simple macro defines including __HLSL_VERSION (#2363) * #include an absolute path didn't work - because paths were taken to always be relative. * Add standard macros including __HLSL_VERSION * Added hlsl test. * Defined standard macro names to values, so not undefined. Allows more convenient #if style. Co-authored-by: Yong He --- source/slang/slang.cpp | 42 +++++++++++++++++++--- tests/preprocessor/simple-pre-defined.hlsl | 9 +++++ .../preprocessor/simple-pre-defined.hlsl.expected | 6 ++++ tests/preprocessor/simple-pre-defined.slang | 9 +++++ .../preprocessor/simple-pre-defined.slang.expected | 6 ++++ 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 tests/preprocessor/simple-pre-defined.hlsl create mode 100644 tests/preprocessor/simple-pre-defined.hlsl.expected create mode 100644 tests/preprocessor/simple-pre-defined.slang create mode 100644 tests/preprocessor/simple-pre-defined.slang.expected diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 4f135d144..710cc0b45 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -1964,6 +1964,9 @@ void FrontEndCompileRequest::parseTranslationUnit( break; } + // TODO(JS): + // Note! that a adding a define twice will cause an exception in debug builds + // that may be desirable or not... Dictionary combinedPreprocessorDefinitions; for(auto& def : getLinkage()->preprocessorDefinitions) combinedPreprocessorDefinitions.Add(def.Key, def.Value); @@ -1972,12 +1975,45 @@ void FrontEndCompileRequest::parseTranslationUnit( for(auto& def : translationUnit->preprocessorDefinitions) combinedPreprocessorDefinitions.Add(def.Key, def.Value); + // Define standard macros, if not already defined. This style assumes using `#if __SOME_VAR` style, as in + // + // ``` + // #if __SLANG_COMPILER__ + // ``` + // + // This choice is made because slang outputs a warning on using a variable in an #if if not defined + // + // Of course this means using #ifndef/#ifdef/defined() is probably not appropraite with thes variables. + { + // Used to identify level of HLSL language compatibility + combinedPreprocessorDefinitions.AddIfNotExists("__HLSL_VERSION", "2020"); + + // Indicates this is being compiled by the slang *compiler* + combinedPreprocessorDefinitions.AddIfNotExists("__SLANG_COMPILER__", "1"); + + // Set macro depending on source type + switch (translationUnit->sourceLanguage) + { + case SourceLanguage::HLSL: + // Used to indicate compiled as HLSL language + combinedPreprocessorDefinitions.AddIfNotExists("__HLSL__", "1"); + break; + case SourceLanguage::Slang: + // Used to indicate compiled as Slang language + combinedPreprocessorDefinitions.AddIfNotExists("__SLANG__", "1"); + break; + default: break; + } + + // If not set, define as 0. + combinedPreprocessorDefinitions.AddIfNotExists("__HLSL__", "0"); + combinedPreprocessorDefinitions.AddIfNotExists("__SLANG__", "0"); + } + auto module = translationUnit->getModule(); ASTBuilder* astBuilder = module->getASTBuilder(); - //ASTBuilder* astBuilder = linkage->getASTBuilder(); - ModuleDecl* translationUnitSyntax = astBuilder->create(); translationUnitSyntax->nameAndLoc.name = translationUnit->moduleName; @@ -2060,8 +2096,6 @@ void FrontEndCompileRequest::parseTranslationUnit( } } - - #if 0 // Test serialization { diff --git a/tests/preprocessor/simple-pre-defined.hlsl b/tests/preprocessor/simple-pre-defined.hlsl new file mode 100644 index 000000000..7789eb8eb --- /dev/null +++ b/tests/preprocessor/simple-pre-defined.hlsl @@ -0,0 +1,9 @@ +//DIAGNOSTIC_TEST:SIMPLE:-E + +__SLANG_COMPILER__ + +__HLSL_VERSION + +__SLANG__ + +__HLSL__ \ No newline at end of file diff --git a/tests/preprocessor/simple-pre-defined.hlsl.expected b/tests/preprocessor/simple-pre-defined.hlsl.expected new file mode 100644 index 000000000..98911f3c7 --- /dev/null +++ b/tests/preprocessor/simple-pre-defined.hlsl.expected @@ -0,0 +1,6 @@ +result code = 0 +standard error = { +} +standard output = { +1 2020 0 1 +} diff --git a/tests/preprocessor/simple-pre-defined.slang b/tests/preprocessor/simple-pre-defined.slang new file mode 100644 index 000000000..7789eb8eb --- /dev/null +++ b/tests/preprocessor/simple-pre-defined.slang @@ -0,0 +1,9 @@ +//DIAGNOSTIC_TEST:SIMPLE:-E + +__SLANG_COMPILER__ + +__HLSL_VERSION + +__SLANG__ + +__HLSL__ \ No newline at end of file diff --git a/tests/preprocessor/simple-pre-defined.slang.expected b/tests/preprocessor/simple-pre-defined.slang.expected new file mode 100644 index 000000000..4b5184e74 --- /dev/null +++ b/tests/preprocessor/simple-pre-defined.slang.expected @@ -0,0 +1,6 @@ +result code = 0 +standard error = { +} +standard output = { +1 2020 1 0 +} -- cgit v1.2.3