[flang-commits] [PATCH] D109048: [flang] COMMAND_ARGUMENT_COUNT runtime implementation

Diana Picus via Phabricator via flang-commits flang-commits at lists.llvm.org
Wed Sep 1 01:24:24 PDT 2021


rovka created this revision.
rovka added reviewers: klausler, jeanPerier.
rovka added a project: Flang.
Herald added subscribers: jdoerfert, mgorny.
Herald added a reviewer: sscalpone.
rovka requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Grab whatever ProgramStart has stored in 
executionEnvironment.argc and subtract 1 (based on the
assumption that ProgramStart is called with a C-style argc
that counts the command name as an argument).

Spoiler alert: The tests will evolve into fixtures when we
implement GET_COMMAND_ARGUMENT etc.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D109048

Files:
  flang/runtime/CMakeLists.txt
  flang/runtime/command.cpp
  flang/unittests/Runtime/CMakeLists.txt
  flang/unittests/Runtime/CommandTest.cpp


Index: flang/unittests/Runtime/CommandTest.cpp
===================================================================
--- /dev/null
+++ flang/unittests/Runtime/CommandTest.cpp
@@ -0,0 +1,32 @@
+//===-- flang/unittests/RuntimeGTest/CommandTest.cpp ----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "../../runtime/command.h"
+#include "gtest/gtest.h"
+#include "../../runtime/descriptor.h"
+#include "../../runtime/main.h"
+
+using namespace Fortran::runtime;
+
+TEST(ArgumentCount, ZeroArguments) {
+  const char *argv[] = {"aProgram"};
+  RTNAME(ProgramStart)(1, argv, {});
+  EXPECT_EQ(0, RTNAME(ArgumentCount)());
+}
+
+TEST(ArgumentCount, OneArgument) {
+  const char *argv[] = {"aProgram", "anArgument"};
+  RTNAME(ProgramStart)(2, argv, {});
+  EXPECT_EQ(1, RTNAME(ArgumentCount)());
+}
+
+TEST(ArgumentCount, SeveralArguments) {
+  const char *argv[] = {"aProgram", "arg1", "arg2", "arg3", "arg4"};
+  RTNAME(ProgramStart)(5, argv, {});
+  EXPECT_EQ(4, RTNAME(ArgumentCount)());
+}
Index: flang/unittests/Runtime/CMakeLists.txt
===================================================================
--- flang/unittests/Runtime/CMakeLists.txt
+++ flang/unittests/Runtime/CMakeLists.txt
@@ -1,6 +1,7 @@
 add_flang_unittest(FlangRuntimeTests
   BufferTest.cpp
   CharacterTest.cpp
+  CommandTest.cpp
   CrashHandlerFixture.cpp
   ExternalIOTest.cpp
   Format.cpp
Index: flang/runtime/command.cpp
===================================================================
--- /dev/null
+++ flang/runtime/command.cpp
@@ -0,0 +1,21 @@
+//===-- runtime/command.cpp -------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "command.h"
+#include "environment.h"
+
+namespace Fortran::runtime {
+CppTypeFor<TypeCategory::Integer, 4> RTNAME(ArgumentCount)() {
+  int argc = executionEnvironment.argc;
+  if (argc > 1) {
+    // C counts the command name as one of the arguments, but Fortran doesn't.
+    return argc - 1;
+  }
+  return 0;
+}
+} // namespace Fortran::runtime
Index: flang/runtime/CMakeLists.txt
===================================================================
--- flang/runtime/CMakeLists.txt
+++ flang/runtime/CMakeLists.txt
@@ -35,6 +35,7 @@
   allocatable.cpp
   assign.cpp
   buffer.cpp
+  command.cpp
   complex-reduction.c
   copy.cpp
   character.cpp


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109048.369879.patch
Type: text/x-patch
Size: 2846 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20210901/caba8be1/attachment.bin>


More information about the flang-commits mailing list