summaryrefslogtreecommitdiffstats
path: root/tools/slang-cpp-parser/unit-test.cpp
diff options
context:
space:
mode:
authorLauro Oyen <15063951+laurooyen@users.noreply.github.com>2024-12-02 20:46:43 +0100
committerGitHub <noreply@github.com>2024-12-02 11:46:43 -0800
commiteaa8dcfcc9deabb906cc09bf31fc17ab6f343ff4 (patch)
tree8e0f4658de3efb5e7696e8588c55471f9d65ba18 /tools/slang-cpp-parser/unit-test.cpp
parent7aaf7009e2c6055a714ba4a93ab3dd73d2d2cdb7 (diff)
Move c++ parsing code from slang-cpp-extractor to static library (#5675)
* Move c++ parsing code from slang-cpp-extractor to static library * Format code * Remove relative includes --------- Co-authored-by: slangbot <ellieh+slangbot@nvidia.com> Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'tools/slang-cpp-parser/unit-test.cpp')
-rw-r--r--tools/slang-cpp-parser/unit-test.cpp127
1 files changed, 127 insertions, 0 deletions
diff --git a/tools/slang-cpp-parser/unit-test.cpp b/tools/slang-cpp-parser/unit-test.cpp
new file mode 100644
index 000000000..70851fd7c
--- /dev/null
+++ b/tools/slang-cpp-parser/unit-test.cpp
@@ -0,0 +1,127 @@
+#include "unit-test.h"
+
+#include "compiler-core/slang-lexer.h"
+#include "compiler-core/slang-name-convention-util.h"
+#include "compiler-core/slang-source-loc.h"
+#include "core/slang-io.h"
+#include "identifier-lookup.h"
+#include "node-tree.h"
+#include "options.h"
+#include "parser.h"
+
+namespace CppParse
+{
+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);
+
+ // We don't require marker
+ m_options.m_requireMark = false;
+ }
+
+ 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[] = "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"
+ "struct SomeStruct\n"
+ "{\n"
+ " SomeStruct() = default;\n"
+ " SomeStruct(float v = 0.0f):b(v) {}\n"
+ " ~SomeStruct() {}\n"
+ " int a = 10; \n"
+ " float b; \n"
+ " int another[10];\n"
+ " const char* yetAnother = nullptr;\n"
+ "};\n"
+ "\n"
+ "enum SomeEnum\n"
+ "{\n"
+ " Value,\n"
+ " Another = 10,\n"
+ "};\n"
+ "\n"
+ "typedef int (*SomeFunc)(int a);\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"
+ "unsigned add(unsigned a, unsigned 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);
+
+
+ {
+ const Node::Kind enableKinds[] = {
+ Node::Kind::Enum,
+ Node::Kind::EnumClass,
+ Node::Kind::EnumCase,
+ Node::Kind::TypeDef};
+ parser.setKindsEnabled(enableKinds, SLANG_COUNT_OF(enableKinds));
+ }
+
+ SlangResult res = parser.parse(sourceOrigin, &state.m_options);
+
+ if (state.m_sink.outputBuffer.getLength())
+ {
+ printf("%s\n", state.m_sink.outputBuffer.getBuffer());
+ }
+
+ if (SLANG_FAILED(res))
+ {
+ return res;
+ }
+
+ {
+ StringBuilder buf;
+ tree.getRootNode()->dump(0, buf);
+
+ SLANG_UNUSED(buf);
+ }
+ }
+
+ return SLANG_OK;
+}
+
+} // namespace CppParse