yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

CopilotMerge NamePool and RootNamePool into a single type (#7797)3485710e9

master
4.0 KiB124 linesraw
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{
19    TestState()
20        : m_slicePool(StringSlicePool::Style::Default)
21    {
22        m_identifierLookup.initDefault(UnownedStringSlice::fromLiteral("SLANG_"));
23
24        m_sourceManager.initialize(nullptr, nullptr);
25
26        m_sink.init(&m_sourceManager, Lexer::sourceLocationLexer);
27
28        // We don't require marker
29        m_options.m_requireMark = false;
30    }
31
32    Options m_options;
33    SourceManager m_sourceManager;
34    DiagnosticSink m_sink;
35    NamePool m_namePool;
36    StringSlicePool m_slicePool;
37    IdentifierLookup 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    {
77        TestState state;
78
79        NodeTree tree(&state.m_slicePool, &state.m_namePool, &state.m_identifierLookup);
80
81        UnownedStringSlice contents = UnownedStringSlice::fromLiteral(someSource);
82        PathInfo pathInfo = PathInfo::makeFromString("source.h");
83
84        SourceManager* sourceManager = &state.m_sourceManager;
85
86        SourceFile* sourceFile = sourceManager->createSourceFileWithString(pathInfo, contents);
87        SourceOrigin* sourceOrigin = tree.addSourceOrigin(sourceFile, state.m_options);
88
89        Parser parser(&tree, &state.m_sink);
90
91
92        {
93            const Node::Kind enableKinds[] = {
94                Node::Kind::Enum,
95                Node::Kind::EnumClass,
96                Node::Kind::EnumCase,
97                Node::Kind::TypeDef};
98            parser.setKindsEnabled(enableKinds, SLANG_COUNT_OF(enableKinds));
99        }
100
101        SlangResult res = parser.parse(sourceOrigin, &state.m_options);
102
103        if (state.m_sink.outputBuffer.getLength())
104        {
105            printf("%s\n", state.m_sink.outputBuffer.getBuffer());
106        }
107
108        if (SLANG_FAILED(res))
109        {
110            return res;
111        }
112
113        {
114            StringBuilder buf;
115            tree.getRootNode()->dump(0, buf);
116
117            SLANG_UNUSED(buf);
118        }
119    }
120
121    return SLANG_OK;
122}
123
124} // namespace CppParse