[flang-commits] [flang] 3261aef - [flang] Extend runtime API for PAUSE to allow a stop code

peter klausler via flang-commits flang-commits at lists.llvm.org
Thu Oct 1 17:22:48 PDT 2020


Author: peter klausler
Date: 2020-10-01T17:20:11-07:00
New Revision: 3261aefc72b3769e8b3eccbb67e1145e195ffa8d

URL: https://github.com/llvm/llvm-project/commit/3261aefc72b3769e8b3eccbb67e1145e195ffa8d
DIFF: https://github.com/llvm/llvm-project/commit/3261aefc72b3769e8b3eccbb67e1145e195ffa8d.diff

LOG: [flang] Extend runtime API for PAUSE to allow a stop code

Support integer and default character stop codes on PAUSE
statements.  Add length argument to STOP statement with a
character stop code.

Differential revision: https://reviews.llvm.org/D88692

Added: 
    

Modified: 
    flang/runtime/stop.cpp
    flang/runtime/stop.h

Removed: 
    


################################################################################
diff  --git a/flang/runtime/stop.cpp b/flang/runtime/stop.cpp
index 3b8c1385293a..d417f44d175f 100644
--- a/flang/runtime/stop.cpp
+++ b/flang/runtime/stop.cpp
@@ -64,26 +64,53 @@ static void CloseAllExternalUnits(const char *why) {
 }
 
 [[noreturn]] void RTNAME(StopStatementText)(
-    const char *code, bool isErrorStop, bool quiet) {
+    const char *code, std::size_t length, bool isErrorStop, bool quiet) {
   CloseAllExternalUnits("STOP statement");
   if (!quiet) {
-    std::fprintf(
-        stderr, "Fortran %s: %s\n", isErrorStop ? "ERROR STOP" : "STOP", code);
+    std::fprintf(stderr, "Fortran %s: %.*s\n",
+        isErrorStop ? "ERROR STOP" : "STOP", static_cast<int>(length), code);
     DescribeIEEESignaledExceptions();
   }
   std::exit(EXIT_FAILURE);
 }
 
-void RTNAME(PauseStatement)() {
+static bool StartPause() {
   if (Fortran::runtime::io::IsATerminal(0)) {
     Fortran::runtime::io::IoErrorHandler handler{"PAUSE statement"};
     Fortran::runtime::io::ExternalFileUnit::FlushAll(handler);
+    return true;
+  }
+  return false;
+}
+
+static void EndPause() {
+  std::fflush(nullptr);
+  if (std::fgetc(stdin) == EOF) {
+    CloseAllExternalUnits("PAUSE statement");
+    std::exit(EXIT_SUCCESS);
+  }
+}
+
+void RTNAME(PauseStatement)() {
+  if (StartPause()) {
     std::fputs("Fortran PAUSE: hit RETURN to continue:", stderr);
-    std::fflush(nullptr);
-    if (std::fgetc(stdin) == EOF) {
-      CloseAllExternalUnits("PAUSE statement");
-      std::exit(EXIT_SUCCESS);
-    }
+    EndPause();
+  }
+}
+
+void RTNAME(PauseStatementInt)(int code) {
+  if (StartPause()) {
+    std::fprintf(stderr, "Fortran PAUSE %d: hit RETURN to continue:", code);
+    EndPause();
+  }
+}
+
+void RTNAME(PauseStatementText)(const char *code, std::size_t length) {
+  if (StartPause()) {
+    std::fprintf(stderr,
+        "Fortran PAUSE %.*s: hit RETURN to continue:", static_cast<int>(length),
+        code);
+    EndPause();
   }
 }
 

diff  --git a/flang/runtime/stop.h b/flang/runtime/stop.h
index 3d5f22e5761a..638fa179edd3 100644
--- a/flang/runtime/stop.h
+++ b/flang/runtime/stop.h
@@ -18,9 +18,11 @@ FORTRAN_EXTERN_C_BEGIN
 // Program-initiated image stop
 NORETURN void RTNAME(StopStatement)(int code DEFAULT_VALUE(EXIT_SUCCESS),
     bool isErrorStop DEFAULT_VALUE(false), bool quiet DEFAULT_VALUE(false));
-NORETURN void RTNAME(StopStatementText)(const char *,
+NORETURN void RTNAME(StopStatementText)(const char *, size_t,
     bool isErrorStop DEFAULT_VALUE(false), bool quiet DEFAULT_VALUE(false));
 void RTNAME(PauseStatement)(NO_ARGUMENTS);
+void RTNAME(PauseStatementInt)(int);
+void RTNAME(PauseStatementText)(const char *, size_t);
 NORETURN void RTNAME(FailImageStatement)(NO_ARGUMENTS);
 NORETURN void RTNAME(ProgramEndStatement)(NO_ARGUMENTS);
 


        


More information about the flang-commits mailing list