From c4c90f5a6da45229405533372215ba40de91df37 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 19 May 2021 17:53:24 -0400 Subject: 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 --- source/compiler-core/slang-command-line-args.cpp | 95 ++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 source/compiler-core/slang-command-line-args.cpp (limited to 'source/compiler-core/slang-command-line-args.cpp') 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 -- cgit v1.2.3