diff options
| author | Yong He <yonghe@outlook.com> | 2024-12-11 13:34:54 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-11 13:34:54 -0800 |
| commit | 941f07040a505f1f673c96da959bde839c629aba (patch) | |
| tree | fe5cd3cd0a63919ad8971d32cd18e8161f9cbd99 /tools/slang-unit-test | |
| parent | e50aac13e2c161d672b137a62f6d66820d0f9ff1 (diff) | |
Fix attribute reflection. (#5823)
* Fix attribute reflection.
* Fix.
* Fix.
Diffstat (limited to 'tools/slang-unit-test')
3 files changed, 81 insertions, 2 deletions
diff --git a/tools/slang-unit-test/unit-test-attribute-reflection.cpp b/tools/slang-unit-test/unit-test-attribute-reflection.cpp new file mode 100644 index 000000000..e60eeb2d4 --- /dev/null +++ b/tools/slang-unit-test/unit-test-attribute-reflection.cpp @@ -0,0 +1,79 @@ +// unit-test-translation-unit-import.cpp + +#include "../../source/core/slang-io.h" +#include "../../source/core/slang-process.h" +#include "slang-com-ptr.h" +#include "slang.h" +#include "unit-test/slang-unit-test.h" + +#include <stdio.h> +#include <stdlib.h> + +using namespace Slang; + +// Test that the reflection API provides correct info about attributes. + +SLANG_UNIT_TEST(attributeReflection) +{ + const char* userSourceBody = R"( + public enum E + { + V0, + V1, + }; + + [__AttributeUsage(_AttributeTargets.Struct)] + public struct NormalTextureAttribute + { + public E Type; + }; + + [COM("042BE50B-CB01-4DBB-8367-3A9CDCBE2F49")] + interface IInterface { void f(); } + + [NormalTexture(E.V1)] + struct TS {}; + )"; + String userSource = userSourceBody; + ComPtr<slang::IGlobalSession> globalSession; + SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK); + slang::TargetDesc targetDesc = {}; + targetDesc.format = SLANG_HLSL; + targetDesc.profile = globalSession->findProfile("sm_5_0"); + slang::SessionDesc sessionDesc = {}; + sessionDesc.targetCount = 1; + sessionDesc.targets = &targetDesc; + ComPtr<slang::ISession> session; + SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK); + + ComPtr<slang::IBlob> diagnosticBlob; + auto module = session->loadModuleFromSourceString( + "m", + "m.slang", + userSourceBody, + diagnosticBlob.writeRef()); + SLANG_CHECK(module != nullptr); + + auto reflection = module->getLayout(); + + auto interfaceType = reflection->findTypeByName("IInterface"); + SLANG_CHECK(interfaceType != nullptr); + + auto comAttribute = interfaceType->findAttributeByName("COM"); + SLANG_CHECK(comAttribute != nullptr); + + size_t size = 0; + auto guid = comAttribute->getArgumentValueString(0, &size); + UnownedStringSlice stringSlice = UnownedStringSlice(guid, size); + SLANG_CHECK(stringSlice == "\"042BE50B-CB01-4DBB-8367-3A9CDCBE2F49\""); + + auto testType = reflection->findTypeByName("TS"); + SLANG_CHECK(testType != nullptr); + + auto normalTextureAttribute = testType->findAttributeByName("NormalTexture"); + SLANG_CHECK(normalTextureAttribute != nullptr); + + int value = 0; + normalTextureAttribute->getArgumentValueInt(0, &value); + SLANG_CHECK(value == 1); +} diff --git a/tools/slang-unit-test/unit-test-decl-tree-reflection.cpp b/tools/slang-unit-test/unit-test-decl-tree-reflection.cpp index 2ceb9981b..512be9be5 100644 --- a/tools/slang-unit-test/unit-test-decl-tree-reflection.cpp +++ b/tools/slang-unit-test/unit-test-decl-tree-reflection.cpp @@ -178,7 +178,7 @@ SLANG_UNIT_TEST(declTreeReflection) SLANG_CHECK(result == SLANG_OK); SLANG_CHECK(val == 1024); SLANG_CHECK( - funcReflection->findUserAttributeByName(globalSession.get(), "MyFuncProperty") == + funcReflection->findAttributeByName(globalSession.get(), "MyFuncProperty") == userAttribute); } diff --git a/tools/slang-unit-test/unit-test-function-reflection.cpp b/tools/slang-unit-test/unit-test-function-reflection.cpp index 52c2e795a..3ce6ab7a5 100644 --- a/tools/slang-unit-test/unit-test-function-reflection.cpp +++ b/tools/slang-unit-test/unit-test-function-reflection.cpp @@ -108,7 +108,7 @@ SLANG_UNIT_TEST(functionReflection) SLANG_CHECK(result == SLANG_OK); SLANG_CHECK(val == 1024); SLANG_CHECK( - funcReflection->findUserAttributeByName(globalSession.get(), "MyFuncProperty") == + funcReflection->findAttributeByName(globalSession.get(), "MyFuncProperty") == userAttribute); // Check overloaded method resolution |
