From 0c375296cc771af7279427e942a207e3cfe7695f Mon Sep 17 00:00:00 2001 From: Diana Picus Date: Wed, 25 Aug 2021 07:51:48 +0000 Subject: [PATCH] [flang] COMMAND_ARGUMENT_COUNT runtime implementation 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. Differential Revision: https://reviews.llvm.org/D109048 --- flang/runtime/CMakeLists.txt | 1 + flang/runtime/command.cpp | 21 +++++++++++++++++++++ flang/unittests/Runtime/CMakeLists.txt | 1 + flang/unittests/Runtime/CommandTest.cpp | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 flang/runtime/command.cpp create mode 100644 flang/unittests/Runtime/CommandTest.cpp diff --git a/flang/runtime/CMakeLists.txt b/flang/runtime/CMakeLists.txt index bc5c2d1..b3e96fa 100644 --- a/flang/runtime/CMakeLists.txt +++ b/flang/runtime/CMakeLists.txt @@ -35,6 +35,7 @@ add_flang_library(FortranRuntime allocatable.cpp assign.cpp buffer.cpp + command.cpp complex-reduction.c copy.cpp character.cpp diff --git a/flang/runtime/command.cpp b/flang/runtime/command.cpp new file mode 100644 index 0000000..603da15 --- /dev/null +++ b/flang/runtime/command.cpp @@ -0,0 +1,21 @@ +//===-- runtime/command.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 "command.h" +#include "environment.h" + +namespace Fortran::runtime { +CppTypeFor 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 diff --git a/flang/unittests/Runtime/CMakeLists.txt b/flang/unittests/Runtime/CMakeLists.txt index 6ae2f64..33f1bd0 100644 --- a/flang/unittests/Runtime/CMakeLists.txt +++ b/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 diff --git a/flang/unittests/Runtime/CommandTest.cpp b/flang/unittests/Runtime/CommandTest.cpp new file mode 100644 index 0000000..9c67329 --- /dev/null +++ b/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)()); +} -- 2.7.4