[libc] [llvm] [flang] [compiler-rt] [clang] [clang-tools-extra] Apply kind code check on exitstat and cmdstat (PR #78286)

Yi Wu via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 18 01:28:14 PST 2024


https://github.com/yi-wu-arm updated https://github.com/llvm/llvm-project/pull/78286

>From d56eca56c8e4c64e649febc43e2c48b6e5146680 Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Tue, 16 Jan 2024 14:08:00 +0000
Subject: [PATCH 1/6] change exitstat and cmsstat from AnyInt to DefaultInt

---
 flang/lib/Evaluate/intrinsics.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index da6d5970089884..0b9bdac88a78dc 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -1314,9 +1314,9 @@ static const IntrinsicInterface intrinsicSubroutine[]{
     {"execute_command_line",
         {{"command", DefaultChar, Rank::scalar},
             {"wait", AnyLogical, Rank::scalar, Optionality::optional},
-            {"exitstat", AnyInt, Rank::scalar, Optionality::optional,
+            {"exitstat", DefaultInt, Rank::scalar, Optionality::optional,
                 common::Intent::InOut},
-            {"cmdstat", AnyInt, Rank::scalar, Optionality::optional,
+            {"cmdstat", DefaultInt, Rank::scalar, Optionality::optional,
                 common::Intent::Out},
             {"cmdmsg", DefaultChar, Rank::scalar, Optionality::optional,
                 common::Intent::InOut}},

>From 2741652cae00ca1a94ae7a3310af1f25308e8105 Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Tue, 16 Jan 2024 16:54:42 +0000
Subject: [PATCH 2/6] add KindCode::greaterEqualToKind

Now execute_command_line will accept exitstat kind>=4, cmdstat kind >=2
---
 flang/lib/Evaluate/intrinsics.cpp | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index 0b9bdac88a78dc..947e31967bdf45 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -77,7 +77,7 @@ static constexpr CategorySet AnyType{IntrinsicType | DerivedType};
 
 ENUM_CLASS(KindCode, none, defaultIntegerKind,
     defaultRealKind, // is also the default COMPLEX kind
-    doublePrecision, defaultCharKind, defaultLogicalKind,
+    doublePrecision, defaultCharKind, defaultLogicalKind, greaterAndEqualToKind,
     any, // matches any kind value; each instance is independent
     // match any kind, but all "same" kinds must be equal. For characters, also
     // implies that lengths must be equal.
@@ -104,7 +104,8 @@ ENUM_CLASS(KindCode, none, defaultIntegerKind,
 struct TypePattern {
   CategorySet categorySet;
   KindCode kindCode{KindCode::none};
-  int exactKindValue{0}; // for KindCode::exactKind
+  int kindValue{
+      0}; // for KindCode::exactKind and KindCode::greaterAndEqualToKind
   llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
 };
 
@@ -1314,10 +1315,12 @@ static const IntrinsicInterface intrinsicSubroutine[]{
     {"execute_command_line",
         {{"command", DefaultChar, Rank::scalar},
             {"wait", AnyLogical, Rank::scalar, Optionality::optional},
-            {"exitstat", DefaultInt, Rank::scalar, Optionality::optional,
-                common::Intent::InOut},
-            {"cmdstat", DefaultInt, Rank::scalar, Optionality::optional,
-                common::Intent::Out},
+            {"exitstat",
+                TypePattern{IntType, KindCode::greaterAndEqualToKind, 4},
+                Rank::scalar, Optionality::optional, common::Intent::InOut},
+            {"cmdstat",
+                TypePattern{IntType, KindCode::greaterAndEqualToKind, 2},
+                Rank::scalar, Optionality::optional, common::Intent::Out},
             {"cmdmsg", DefaultChar, Rank::scalar, Optionality::optional,
                 common::Intent::InOut}},
         {}, Rank::elemental, IntrinsicClass::impureSubroutine},
@@ -1834,7 +1837,10 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
       argOk = true;
       break;
     case KindCode::exactKind:
-      argOk = type->kind() == d.typePattern.exactKindValue;
+      argOk = type->kind() == d.typePattern.kindValue;
+      break;
+    case KindCode::greaterAndEqualToKind:
+      argOk = type->kind() >= d.typePattern.kindValue;
       break;
     case KindCode::sameAtom:
       if (!sameArg) {
@@ -2177,8 +2183,9 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
       resultType = DynamicType{
           GetBuiltinDerivedType(builtinsScope, "__builtin_team_type")};
       break;
+    case KindCode::greaterAndEqualToKind:
     case KindCode::exactKind:
-      resultType = DynamicType{*category, result.exactKindValue};
+      resultType = DynamicType{*category, result.kindValue};
       break;
     case KindCode::typeless:
     case KindCode::any:

>From cea484080cba83ad32abb5622048b1864e4a49dc Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Tue, 16 Jan 2024 17:25:12 +0000
Subject: [PATCH 3/6] doc fixes

---
 flang/docs/Intrinsics.md | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/flang/docs/Intrinsics.md b/flang/docs/Intrinsics.md
index 5ade2574032977..9811903ab8130a 100644
--- a/flang/docs/Intrinsics.md
+++ b/flang/docs/Intrinsics.md
@@ -852,13 +852,14 @@ used in constant expressions have currently no folding support at all.
 - **Syntax:** `CALL EXECUTE_COMMAND_LINE(COMMAND [, WAIT, EXITSTAT, CMDSTAT, CMDMSG ])`
 - **Arguments:**
 
-  | Argument  | Description                                                  |
-  |-----------|--------------------------------------------------------------|
-  | `COMMAND` | Shall be a default CHARACTER scalar.                         |
-  | `WAIT`    | (Optional) Shall be a default LOGICAL scalar.                |
-  | `EXITSTAT`| (Optional) Shall be an INTEGER of the default kind.          |
-  | `CMDSTAT` | (Optional) Shall be an INTEGER of the default kind.          |
-  | `CMDMSG`  | (Optional) Shall be a CHARACTER scalar of the default kind.  |
+| Argument   | Description                                                           |
+|------------|-----------------------------------------------------------------------|
+| `COMMAND`  | Shall be a default CHARACTER scalar.                                  |
+| `WAIT`     | (Optional) Shall be a default LOGICAL scalar.                         |
+| `EXITSTAT` | (Optional) Shall be an INTEGER with kind greater than or equal to 2.  |
+| `CMDSTAT`  | (Optional) Shall be an INTEGER with kindgreater than or equal to 2.   |
+| `CMDMSG`   | (Optional) Shall be a CHARACTER scalar of the default kind.           |
+
 
 #### Implementation Specifics
 

>From 0b049991dd04edac358c7e865537aed8ed7c307a Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Wed, 17 Jan 2024 10:41:04 +0000
Subject: [PATCH 4/6] add test and rename variable

---
 flang/lib/Evaluate/intrinsics.cpp             | 13 ++++-----
 flang/test/Semantics/execute_command_line.f90 | 28 +++++++++++++++++++
 2 files changed, 34 insertions(+), 7 deletions(-)
 create mode 100644 flang/test/Semantics/execute_command_line.f90

diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index 947e31967bdf45..b2c0fcaf8e1110 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -77,7 +77,7 @@ static constexpr CategorySet AnyType{IntrinsicType | DerivedType};
 
 ENUM_CLASS(KindCode, none, defaultIntegerKind,
     defaultRealKind, // is also the default COMPLEX kind
-    doublePrecision, defaultCharKind, defaultLogicalKind, greaterAndEqualToKind,
+    doublePrecision, defaultCharKind, defaultLogicalKind, greaterOrEqualToKind,
     any, // matches any kind value; each instance is independent
     // match any kind, but all "same" kinds must be equal. For characters, also
     // implies that lengths must be equal.
@@ -105,7 +105,7 @@ struct TypePattern {
   CategorySet categorySet;
   KindCode kindCode{KindCode::none};
   int kindValue{
-      0}; // for KindCode::exactKind and KindCode::greaterAndEqualToKind
+      0}; // for KindCode::exactKind and KindCode::greaterOrEqualToKind
   llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
 };
 
@@ -1316,10 +1316,9 @@ static const IntrinsicInterface intrinsicSubroutine[]{
         {{"command", DefaultChar, Rank::scalar},
             {"wait", AnyLogical, Rank::scalar, Optionality::optional},
             {"exitstat",
-                TypePattern{IntType, KindCode::greaterAndEqualToKind, 4},
+                TypePattern{IntType, KindCode::greaterOrEqualToKind, 4},
                 Rank::scalar, Optionality::optional, common::Intent::InOut},
-            {"cmdstat",
-                TypePattern{IntType, KindCode::greaterAndEqualToKind, 2},
+            {"cmdstat", TypePattern{IntType, KindCode::greaterOrEqualToKind, 2},
                 Rank::scalar, Optionality::optional, common::Intent::Out},
             {"cmdmsg", DefaultChar, Rank::scalar, Optionality::optional,
                 common::Intent::InOut}},
@@ -1839,7 +1838,7 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
     case KindCode::exactKind:
       argOk = type->kind() == d.typePattern.kindValue;
       break;
-    case KindCode::greaterAndEqualToKind:
+    case KindCode::greaterOrEqualToKind:
       argOk = type->kind() >= d.typePattern.kindValue;
       break;
     case KindCode::sameAtom:
@@ -2183,7 +2182,7 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
       resultType = DynamicType{
           GetBuiltinDerivedType(builtinsScope, "__builtin_team_type")};
       break;
-    case KindCode::greaterAndEqualToKind:
+    case KindCode::greaterOrEqualToKind:
     case KindCode::exactKind:
       resultType = DynamicType{*category, result.kindValue};
       break;
diff --git a/flang/test/Semantics/execute_command_line.f90 b/flang/test/Semantics/execute_command_line.f90
new file mode 100644
index 00000000000000..f43b8ab60e9ce3
--- /dev/null
+++ b/flang/test/Semantics/execute_command_line.f90
@@ -0,0 +1,28 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic
+! Tests for the EXECUTE_COMMAND_LINE intrinsics
+subroutine bad_kind(command, exitVal, cmdVal)
+CHARACTER(30) :: command
+INTEGER(KIND=2) :: exitVal
+INTEGER(KIND=1) :: cmdVal
+!ERROR: Actual argument for 'exitstat=' has bad type or kind 'INTEGER(2)'
+call execute_command_line(command, exitstat=exitVal)
+
+!ERROR: Actual argument for 'cmdstat=' has bad type or kind 'INTEGER(1)'
+call execute_command_line(command, cmdstat=cmdVal)
+end subroutine bad_kind
+
+subroutine good_kind_equal(command, exitVal, cmdVal)
+CHARACTER(30) :: command
+INTEGER(KIND=4) :: exitVal
+INTEGER(KIND=2) :: cmdVal
+call execute_command_line(command, exitstat=exitVal)
+call execute_command_line(command, cmdstat=cmdVal)
+end subroutine good_kind_equal
+
+subroutine good_kind_greater(command, exitVal, cmdVal)
+CHARACTER(30) :: command
+INTEGER(KIND=8) :: exitVal
+INTEGER(KIND=4) :: cmdVal
+call execute_command_line(command, exitstat=exitVal)
+call execute_command_line(command, cmdstat=cmdVal)
+end subroutine good_kind_greater
\ No newline at end of file

>From 450bc78b8db6f624dc617407b854407a3ff42cb4 Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Wed, 17 Jan 2024 10:45:36 +0000
Subject: [PATCH 5/6] doc and comment fixes

---
 flang/docs/Intrinsics.md          | 2 +-
 flang/lib/Evaluate/intrinsics.cpp | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/flang/docs/Intrinsics.md b/flang/docs/Intrinsics.md
index 9811903ab8130a..2e5ffea1c12ebf 100644
--- a/flang/docs/Intrinsics.md
+++ b/flang/docs/Intrinsics.md
@@ -857,7 +857,7 @@ used in constant expressions have currently no folding support at all.
 | `COMMAND`  | Shall be a default CHARACTER scalar.                                  |
 | `WAIT`     | (Optional) Shall be a default LOGICAL scalar.                         |
 | `EXITSTAT` | (Optional) Shall be an INTEGER with kind greater than or equal to 2.  |
-| `CMDSTAT`  | (Optional) Shall be an INTEGER with kindgreater than or equal to 2.   |
+| `CMDSTAT`  | (Optional) Shall be an INTEGER with kind greater than or equal to 2.  |
 | `CMDMSG`   | (Optional) Shall be a CHARACTER scalar of the default kind.           |
 
 
diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index b2c0fcaf8e1110..1701a475942ff5 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -77,7 +77,9 @@ static constexpr CategorySet AnyType{IntrinsicType | DerivedType};
 
 ENUM_CLASS(KindCode, none, defaultIntegerKind,
     defaultRealKind, // is also the default COMPLEX kind
-    doublePrecision, defaultCharKind, defaultLogicalKind, greaterOrEqualToKind,
+    doublePrecision, defaultCharKind, defaultLogicalKind,
+    greaterOrEqualToKind, // match kind value greater than or equal to a single
+                          // explicit kind value
     any, // matches any kind value; each instance is independent
     // match any kind, but all "same" kinds must be equal. For characters, also
     // implies that lengths must be equal.
@@ -104,8 +106,7 @@ ENUM_CLASS(KindCode, none, defaultIntegerKind,
 struct TypePattern {
   CategorySet categorySet;
   KindCode kindCode{KindCode::none};
-  int kindValue{
-      0}; // for KindCode::exactKind and KindCode::greaterOrEqualToKind
+  int kindValue{0}; // for KindCode::exactKind and greaterOrEqualToKind
   llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
 };
 

>From 98b3962d6fa62a0285c2eb7b410738abe96ab93f Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Wed, 17 Jan 2024 10:59:40 +0000
Subject: [PATCH 6/6] add a newline at end of the file

---
 flang/test/Semantics/execute_command_line.f90 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/test/Semantics/execute_command_line.f90 b/flang/test/Semantics/execute_command_line.f90
index f43b8ab60e9ce3..64c53bffc64628 100644
--- a/flang/test/Semantics/execute_command_line.f90
+++ b/flang/test/Semantics/execute_command_line.f90
@@ -25,4 +25,4 @@ subroutine good_kind_greater(command, exitVal, cmdVal)
 INTEGER(KIND=4) :: cmdVal
 call execute_command_line(command, exitstat=exitVal)
 call execute_command_line(command, cmdstat=cmdVal)
-end subroutine good_kind_greater
\ No newline at end of file
+end subroutine good_kind_greater



More information about the cfe-commits mailing list