yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
3485710e9
master
1#include "unit-test.h" 2 3#include "compiler-core/slang-lexer.h" 4#include "compiler-core/slang-name-convention-util.h" 5#include "compiler-core/slang-source-loc.h" 6#include "core/slang-io.h" 7#include "identifier-lookup.h" 8#include "node-tree.h" 9#include "options.h" 10#include "parser.h" 11 12namespace CppParse 13{ 14using namespace Slang ; 15 16 17struct TestState 18{ 19TestState () 20 :m_slicePool (StringSlicePool ::Style ::Default ) 21 { 22m_identifierLookup .initDefault (UnownedStringSlice ::fromLiteral ("SLANG_" )); 23 24m_sourceManager .initialize (nullptr ,nullptr ); 25 26m_sink .init (& m_sourceManager ,Lexer ::sourceLocationLexer ); 27 28// We don't require marker 29m_options .m_requireMark = false; 30 } 31 32Options m_options ; 33SourceManager m_sourceManager ; 34DiagnosticSink m_sink ; 35NamePool m_namePool ; 36StringSlicePool m_slicePool ; 37IdentifierLookup m_identifierLookup ; 38}; 39 40static const char someSource []= "class ISomeInterface\n" 41"{\n" 42" public:\n" 43" virtual int SLANG_MCALL someMethod(int a, int b) const = 0;\n" 44" virtual float SLANG_MCALL anotherMethod(float a) = 0;\n" 45"};\n" 46"\n" 47"struct SomeStruct\n" 48"{\n" 49" SomeStruct() = default;\n" 50" SomeStruct(float v = 0.0f):b(v) {}\n" 51" ~SomeStruct() {}\n" 52" int a = 10; \n" 53" float b; \n" 54" int another[10];\n" 55" const char* yetAnother = nullptr;\n" 56"};\n" 57"\n" 58"enum SomeEnum\n" 59"{\n" 60" Value,\n" 61" Another = 10,\n" 62"};\n" 63"\n" 64"typedef int (*SomeFunc)(int a);\n" 65"\n" 66"typedef SomeEnum AliasEnum;\n" 67"void someFunc(int a, float b) { }\n" 68"namespace Blah {\n" 69"int add(int a, int b) { return a + b; }\n" 70"unsigned add(unsigned a, unsigned b) { return a + b; }\n" 71"}\n" ; 72 73 74/* static */ SlangResult UnitTestUtil ::run () 75{ 76 { 77TestState state ; 78 79NodeTree tree (& state .m_slicePool ,& state .m_namePool ,& state .m_identifierLookup ); 80 81UnownedStringSlice contents = UnownedStringSlice ::fromLiteral (someSource ); 82PathInfo pathInfo = PathInfo ::makeFromString ("source.h" ); 83 84SourceManager * sourceManager = & state .m_sourceManager ; 85 86SourceFile * sourceFile = sourceManager -> createSourceFileWithString (pathInfo ,contents ); 87SourceOrigin * sourceOrigin = tree .addSourceOrigin (sourceFile ,state .m_options ); 88 89Parser parser (& tree ,& state .m_sink ); 90 91 92 { 93const Node ::Kind enableKinds []= { 94Node ::Kind ::Enum , 95Node ::Kind ::EnumClass , 96Node ::Kind ::EnumCase , 97Node ::Kind ::TypeDef }; 98parser .setKindsEnabled (enableKinds ,SLANG_COUNT_OF (enableKinds )); 99 } 100 101SlangResult res = parser .parse (sourceOrigin ,& state .m_options ); 102 103if (state .m_sink .outputBuffer .getLength ()) 104 { 105printf ("%s\n" ,state .m_sink .outputBuffer .getBuffer ()); 106 } 107 108if (SLANG_FAILED (res )) 109 { 110return res ; 111 } 112 113 { 114StringBuilder buf ; 115tree .getRootNode ()-> dump (0 ,buf ); 116 117SLANG_UNUSED (buf ); 118 } 119 } 120 121return SLANG_OK ; 122} 123 124}// namespace CppParse