summaryrefslogtreecommitdiffstats
path: root/tools/slang-cpp-extractor/unit-test.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2021-04-22 09:32:25 -0400
committerGitHub <noreply@github.com>2021-04-22 09:32:25 -0400
commitda0d295d6c8b6fb03245dea0583437c198890349 (patch)
treeed17baba750b15f6ace1427f04cf19690269161e /tools/slang-cpp-extractor/unit-test.cpp
parent34fba7b5e726136c6eee8a318ab9a75381399c00 (diff)
C++ extractor improvements (#1803)
* #include an absolute path didn't work - because paths were taken to always be relative. * Split of NodeTree. Split out FileUtil. Split out MacroWriter. * Rename slang-cpp-extractor-main.cpp -> cpp-extractor-main.cpp * First pass at extractor unit-tests * Initial parsing of enum. * Ability to disable/enable parsing of scope types. * Initial support for typedef. * Added operator== != to ArrayVIew. Added test for splitting to unit tests. * Improve comment in StringUtil. * Fix comment. * Fix typo.
Diffstat (limited to 'tools/slang-cpp-extractor/unit-test.cpp')
-rw-r--r--tools/slang-cpp-extractor/unit-test.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/tools/slang-cpp-extractor/unit-test.cpp b/tools/slang-cpp-extractor/unit-test.cpp
new file mode 100644
index 000000000..7648e4e50
--- /dev/null
+++ b/tools/slang-cpp-extractor/unit-test.cpp
@@ -0,0 +1,80 @@
+#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[] =
+"enum SomeEnum\n"
+"{\n"
+" Value,\n"
+" Another = 10,\n"
+"};\n"
+"typedef SomeEnum AliasEnum;\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::Type enableTypes[] = { Node::Type::Enum, Node::Type::EnumClass, Node::Type::EnumCase, Node::Type::TypeDef };
+ parser.setTypesEnabled(enableTypes, SLANG_COUNT_OF(enableTypes));
+ }
+
+ SLANG_RETURN_ON_FAIL(parser.parse(sourceOrigin, &state.m_options));
+ }
+
+ return SLANG_OK;
+}
+
+} // namespace CppExtract