diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2021-05-19 17:53:24 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-19 14:53:24 -0700 |
| commit | c4c90f5a6da45229405533372215ba40de91df37 (patch) | |
| tree | b9fdf847656199c5f9b34f081d37ff7f466b7a6d /source/compiler-core/slang-command-line-args.cpp | |
| parent | 61e9154cb797cffe19cfbf3205b4a5a614e8b552 (diff) | |
SourceLoc use in command line processing (#1848)
* #include an absolute path didn't work - because paths were taken to always be relative.
* Added SourceLoc handling for command line parsing.
* Fix typo in debug.
* Fix issue around the DiagnosticSink used in options parsing not having a writer available - by having DiagnosticSink parenting.
* Small rename for clarity.
Co-authored-by: T. Foley <tfoleyNV@users.noreply.github.com>
Diffstat (limited to 'source/compiler-core/slang-command-line-args.cpp')
| -rw-r--r-- | source/compiler-core/slang-command-line-args.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/source/compiler-core/slang-command-line-args.cpp b/source/compiler-core/slang-command-line-args.cpp new file mode 100644 index 000000000..da262e9bb --- /dev/null +++ b/source/compiler-core/slang-command-line-args.cpp @@ -0,0 +1,95 @@ +#include "slang-command-line-args.h" + +#include "../core/slang-process-util.h" +#include "../core/slang-string-escape-util.h" + +#include "slang-core-diagnostics.h" + +namespace Slang { + +void CommandLineArgs::setArgs(const char*const* args, size_t argCount) +{ + m_args.clear(); + + const SourceLoc startLoc = m_sourceManager->getNextRangeStart(); + + StringBuilder buf; + + auto escapeHandler = ProcessUtil::getEscapeHandler(); + + for (size_t i = 0; i < argCount; ++i) + { + const Index offset = buf.getLength(); + + const char* srcArg = args[i]; + + Arg dstArg; + dstArg.loc = startLoc + offset; + dstArg.value = srcArg; + + m_args.add(dstArg); + + // Write the string escaped if necessary + StringEscapeUtil::appendMaybeQuoted(escapeHandler, dstArg.value.getUnownedSlice(), buf); + + // Put a space between the args + buf << " "; + } + + SourceFile* sourceFile = m_sourceManager->createSourceFileWithString(PathInfo::makeUnknown(), buf.ProduceString()); + m_sourceView = m_sourceManager->createSourceView(sourceFile, nullptr, SourceLoc::fromRaw(0)); + + SLANG_ASSERT(m_sourceView->getRange().begin == startLoc); +} + +/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + + CommandLineReader + +!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ + +String CommandLineReader::getPreviousValue() const +{ + SLANG_ASSERT(m_index > 0); + if (m_index > 0) + { + const auto& prevArg = (*m_args)[m_index - 1]; + return prevArg.value; + } + else + { + return String(); + } +} + +SlangResult CommandLineReader::expectArg(String& outArg) +{ + if (hasArg()) + { + outArg = m_args->m_args[m_index++].value; + return SLANG_OK; + } + else + { + m_sink->diagnose(peekLoc(), MiscDiagnostics::expectedArgumentForOption, getPreviousValue()); + return SLANG_FAIL; + } +} + +SlangResult CommandLineReader::expectArg(CommandLineArg& outArg) +{ + if (hasArg()) + { + outArg = peekArg(); + advance(); + return SLANG_OK; + } + else + { + m_sink->diagnose(peekLoc(), MiscDiagnostics::expectedArgumentForOption, getPreviousValue()); + return SLANG_FAIL; + } +} + + +} // namespace Slang |
