From 172538fdb418f7a2faab1f5a410f3b2cb8e18ba5 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Fri, 21 May 2021 18:41:54 -0400 Subject: Downstream option handling (#1850) * #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. * WIP extracting command line args for downstream tools. * Unit tests/bug fixes around extracting args. * Use DownstreamArgs in the EndToEndCompileRequest * Passing downstream compiler options downstream. * Fix issue with endToEndReq being nullptr. * Fix issue with diagnostics number change. * Small improvements to how the source line is displayed if it's too long. Default to 120, as suggested in previous review. Co-authored-by: T. Foley --- tools/slang-test/unit-test-command-line-args.cpp | 126 +++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 tools/slang-test/unit-test-command-line-args.cpp (limited to 'tools') diff --git a/tools/slang-test/unit-test-command-line-args.cpp b/tools/slang-test/unit-test-command-line-args.cpp new file mode 100644 index 000000000..fd50ceeef --- /dev/null +++ b/tools/slang-test/unit-test-command-line-args.cpp @@ -0,0 +1,126 @@ +// unit-test-command-line-args.cpp + +#include "../../source/compiler-core/slang-command-line-args.h" + +#include "test-context.h" + +using namespace Slang; + +static void commandLineArgsUnitTest() +{ + RefPtr context = new CommandLineContext; + + + // Simple scoped version + { + CommandLineArgs args(context); + DownstreamArgs downstreamArgs(context); + + DiagnosticSink sink(context->getSourceManager(), nullptr); + + const char* inArgs[] = + { + "-Xa...", + "-blah", + "10", + "-X.", + }; + + args.setArgs(inArgs, SLANG_COUNT_OF(inArgs)); + + SLANG_CHECK(SLANG_SUCCEEDED(downstreamArgs.stripDownstreamArgs(args, DownstreamArgs::Flag::AllowNewNames, &sink))); + + const char* aArgs[] = + { + "-blah", + "10" + }; + + SLANG_CHECK(downstreamArgs.getArgsByName("a").hasArgs(aArgs, SLANG_COUNT_OF(aArgs))); + SLANG_CHECK(args.getArgCount() == 0 && sink.getErrorCount() == 0); + } + + // Leaving off terminating -X. is ok + { + CommandLineArgs args(context); + DownstreamArgs downstreamArgs(context); + + DiagnosticSink sink(context->getSourceManager(), nullptr); + + const char* inArgs[] = + { + "-Xa...", + "-blah", + "10", + }; + + args.setArgs(inArgs, SLANG_COUNT_OF(inArgs)); + + SLANG_CHECK(SLANG_SUCCEEDED(downstreamArgs.stripDownstreamArgs(args, DownstreamArgs::Flag::AllowNewNames, &sink))); + + const char* aArgs[] = + { + "-blah", + "10" + }; + + SLANG_CHECK(downstreamArgs.getArgsByName("a").hasArgs(aArgs, SLANG_COUNT_OF(aArgs))); + SLANG_CHECK(args.getArgCount() == 0 && sink.getErrorCount() == 0); + } + + // Having a nesting + + { + CommandLineArgs args(context); + DownstreamArgs downstreamArgs(context); + + DiagnosticSink sink(context->getSourceManager(), nullptr); + + const char* inArgs[] = + { + "-something", + "andAnother", + "-Xa...", + "-blah", + "-Xb...", + "-hey", + "-X.", + "10", + "-X.", + "-Xc", + "somethingForC", + }; + + args.setArgs(inArgs, SLANG_COUNT_OF(inArgs)); + + SLANG_CHECK(SLANG_SUCCEEDED(downstreamArgs.stripDownstreamArgs(args, DownstreamArgs::Flag::AllowNewNames, &sink))); + + const char* aArgs[] = + { + "-blah", + "-Xb...", + "-hey", + "-X.", + "10" + }; + + const char* cArgs[] = + { + "somethingForC", + }; + + const char* mainArgs[] = + { + "-something", + "andAnother", + }; + + SLANG_CHECK(downstreamArgs.getArgsByName("a").hasArgs(aArgs, SLANG_COUNT_OF(aArgs))); + SLANG_CHECK(downstreamArgs.getArgsByName("c").hasArgs(cArgs, SLANG_COUNT_OF(cArgs))); + + SLANG_CHECK(args.hasArgs(mainArgs, SLANG_COUNT_OF(mainArgs)) && sink.getErrorCount() == 0); + } + +} + +SLANG_UNIT_TEST("CommandLineArgs", commandLineArgsUnitTest); -- cgit v1.2.3