summaryrefslogtreecommitdiffstats
path: root/tools/slang-cpp-extractor/unit-test.cpp
blob: c239a2a2cfc97ae9a2fb7ec10af248d5d3590133 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "unit-test.h"


#include "../../source/compiler-core/slang-name-convention-util.h"

#include "../../source/core/slang-io.h"

#include "../../source/compiler-core/slang-source-loc.h"
#include "../../source/compiler-core/slang-lexer.h"

#include "identifier-lookup.h"
#include "node-tree.h"
#include "parser.h"
#include "options.h"

namespace CppExtract {
using namespace Slang;


struct TestState
{
    TestState():
        m_slicePool(StringSlicePool::Style::Default)
    {
        m_identifierLookup.initDefault(UnownedStringSlice::fromLiteral("SLANG_"));

        m_sourceManager.initialize(nullptr, nullptr);

        m_sink.init(&m_sourceManager, Lexer::sourceLocationLexer);

        m_namePool.setRootNamePool(&m_rootNamePool);
    }

    RootNamePool m_rootNamePool;
    Options m_options;
    SourceManager m_sourceManager;
    DiagnosticSink m_sink;
    NamePool m_namePool;
    StringSlicePool m_slicePool;
    IdentifierLookup m_identifierLookup;
};

static const char someSource[] =
"#define SLANG_REFLECTED\n"
"\n"
"SLANG_REFLECTED\n"  
"class ISomeInterface\n"
"{\n"
"    public:\n"
"    virtual int SLANG_MCALL someMethod(int a, int b) const = 0;\n"
"    virtual float SLANG_MCALL anotherMethod(float a) = 0;\n"
"};\n"
"\n"
"enum SomeEnum\n"
"{\n"
"    Value,\n"
"    Another = 10,\n"
"};\n"
"typedef SomeEnum AliasEnum;\n"
"void someFunc(int a, float b) { }\n"
"namespace Blah {\n"
"int add(int a, int b) { return a + b; }\n"
"}\n";


/* static */SlangResult UnitTestUtil::run()
{
    {
        TestState state;

        NodeTree tree(&state.m_slicePool, &state.m_namePool, &state.m_identifierLookup);
        
        UnownedStringSlice contents = UnownedStringSlice::fromLiteral(someSource);
        PathInfo pathInfo = PathInfo::makeFromString("source.h");

        SourceManager* sourceManager = &state.m_sourceManager;

        SourceFile* sourceFile = sourceManager->createSourceFileWithString(pathInfo, contents);
        SourceOrigin* sourceOrigin = tree.addSourceOrigin(sourceFile, state.m_options);

        Parser parser(&tree, &state.m_sink);
        // We don't require markers to reflect
        parser.setRequireMarker(false);

        {
            const Node::Kind enableKinds[] = { Node::Kind::Enum, Node::Kind::EnumClass, Node::Kind::EnumCase, Node::Kind::TypeDef };
            parser.setKindsEnabled(enableKinds, SLANG_COUNT_OF(enableKinds));
        }

        SLANG_RETURN_ON_FAIL(parser.parse(sourceOrigin, &state.m_options));


        {
            StringBuilder buf;
            tree.getRootNode()->dump(0, buf);

            SLANG_UNUSED(buf);
        }
    }

    return SLANG_OK;
}

} // namespace CppExtract