summaryrefslogtreecommitdiffstats
path: root/tools/slang-cpp-extractor/options.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2021-04-19 15:39:42 -0400
committerGitHub <noreply@github.com>2021-04-19 15:39:42 -0400
commit778428fecc0548af565e92745cf1344bcf19367f (patch)
treeadaf9be98bb8a4c36e6f7e42f24dbf653973ed7e /tools/slang-cpp-extractor/options.cpp
parent22b562d1a47443f266b114b4b207bcdd4eb3c54f (diff)
Splitting up C++ extractor (#1800)
* #include an absolute path didn't work - because paths were taken to always be relative. * Refactor out ClassLikeNode * WIP around ScopeNode. * Use push and popScope. * Small improvements around C++ extractor. * Adding dynamic casting support. * Made Field another Node type. * Disable command line dumping by default. * Removed comment. * Fix shadowed variable bug found on linux. * Split out node. * Renamed C++ extractor diagnostics to just diagnostics.cpp/.h * Remove C++ extractor Options into separate options.cpp/options.h files. * Split out parser and identifier lookup from C++ extractor. * Put in CppExtract namespace. Simplify some of the class names. * Some simple renaming. * Split out NodeTree from Parser.
Diffstat (limited to 'tools/slang-cpp-extractor/options.cpp')
-rw-r--r--tools/slang-cpp-extractor/options.cpp134
1 files changed, 134 insertions, 0 deletions
diff --git a/tools/slang-cpp-extractor/options.cpp b/tools/slang-cpp-extractor/options.cpp
new file mode 100644
index 000000000..c7b6a9df5
--- /dev/null
+++ b/tools/slang-cpp-extractor/options.cpp
@@ -0,0 +1,134 @@
+#include "options.h"
+
+#include "diagnostics.h"
+
+namespace CppExtract {
+
+SlangResult OptionsParser::_parseArgFlag(const char* option, bool& outFlag)
+{
+ SLANG_ASSERT(UnownedStringSlice(m_args[m_index]) == option);
+ SLANG_ASSERT(m_index < m_argCount);
+
+ m_index++;
+ outFlag = true;
+ return SLANG_OK;
+}
+
+SlangResult OptionsParser::_parseArgWithValue(const char* option, String& ioValue)
+{
+ SLANG_ASSERT(UnownedStringSlice(m_args[m_index]) == option);
+ if (m_index + 1 < m_argCount)
+ {
+ // Next parameter is the output path, there can only be one
+ if (ioValue.getLength())
+ {
+ // There already is output
+ m_sink->diagnose(SourceLoc(), CPPDiagnostics::optionAlreadyDefined, option, ioValue);
+ return SLANG_FAIL;
+ }
+ }
+ else
+ {
+ m_sink->diagnose(SourceLoc(), CPPDiagnostics::requireValueAfterOption, option);
+ return SLANG_FAIL;
+ }
+
+ ioValue = m_args[m_index + 1];
+ m_index += 2;
+ return SLANG_OK;
+}
+
+SlangResult OptionsParser::_parseArgReplaceValue(const char* option, String& ioValue)
+{
+ SLANG_ASSERT(UnownedStringSlice(m_args[m_index]) == option);
+ if (m_index + 1 >= m_argCount)
+ {
+ m_sink->diagnose(SourceLoc(), CPPDiagnostics::requireValueAfterOption, option);
+ return SLANG_FAIL;
+ }
+
+ ioValue = m_args[m_index + 1];
+ m_index += 2;
+ return SLANG_OK;
+}
+
+SlangResult OptionsParser::parse(int argc, const char*const* argv, DiagnosticSink* sink, Options& outOptions)
+{
+ outOptions.reset();
+
+ m_index = 0;
+ m_argCount = argc;
+ m_args = argv;
+ m_sink = sink;
+
+ outOptions.reset();
+
+ while (m_index < m_argCount)
+ {
+ const UnownedStringSlice arg = UnownedStringSlice(argv[m_index]);
+
+ if (arg.getLength() > 0 && arg[0] == '-')
+ {
+ if (arg == "-d")
+ {
+ SLANG_RETURN_ON_FAIL(_parseArgWithValue("-d", outOptions.m_inputDirectory));
+ continue;
+ }
+ else if (arg == "-o")
+ {
+ SLANG_RETURN_ON_FAIL(_parseArgWithValue("-o", outOptions.m_outputPath));
+ continue;
+ }
+ else if (arg == "-dump")
+ {
+ outOptions.m_dump = true;
+ m_index++;
+ continue;
+ }
+ else if (arg == "-mark-prefix")
+ {
+ SLANG_RETURN_ON_FAIL(_parseArgReplaceValue("-mark-prefix", outOptions.m_markPrefix));
+ continue;
+ }
+ else if (arg == "-mark-suffix")
+ {
+ SLANG_RETURN_ON_FAIL(_parseArgReplaceValue("-mark-suffix", outOptions.m_markSuffix));
+ continue;
+ }
+ else if (arg == "-defs")
+ {
+ SLANG_RETURN_ON_FAIL(_parseArgFlag("-defs", outOptions.m_defs));
+ continue;
+ }
+ else if (arg == "-output-fields")
+ {
+ SLANG_RETURN_ON_FAIL(_parseArgFlag("-output-fields", outOptions.m_outputFields));
+ continue;
+ }
+ else if (arg == "-strip-prefix")
+ {
+ SLANG_RETURN_ON_FAIL(_parseArgWithValue("-strip-prefix", outOptions.m_stripFilePrefix));
+ continue;
+ }
+
+ m_sink->diagnose(SourceLoc(), CPPDiagnostics::unknownOption, arg);
+ return SLANG_FAIL;
+ }
+ else
+ {
+ // If it starts with - then it an unknown option
+ outOptions.m_inputPaths.add(arg);
+ m_index++;
+ }
+ }
+
+ if (outOptions.m_inputPaths.getCount() < 0)
+ {
+ m_sink->diagnose(SourceLoc(), CPPDiagnostics::noInputPathsSpecified);
+ return SLANG_FAIL;
+ }
+
+ return SLANG_OK;
+}
+
+} // namespace CppExtract