[flang] Finish cleaning up debug-parser.
authorpeter klausler <pklausler@nvidia.com>
Fri, 20 Apr 2018 23:14:57 +0000 (16:14 -0700)
committerpeter klausler <pklausler@nvidia.com>
Mon, 23 Apr 2018 22:44:28 +0000 (15:44 -0700)
Original-commit: flang-compiler/f18@0ef551025ac7cf192e47fad1139ae3381a874b91
Reviewed-on: https://github.com/flang-compiler/f18/pull/66
Tree-same-pre-rewrite: false

flang/lib/parser/CMakeLists.txt
flang/lib/parser/debug-parser.cc
flang/lib/parser/debug-parser.h
flang/lib/parser/parsing.cc
flang/lib/parser/parsing.h
flang/lib/parser/user-state.h
flang/tools/f18/f18.cc

index 46cc2e8..f1ad22e 100644 (file)
@@ -2,7 +2,7 @@ add_library(FortranParser
   char-buffer.cc
   char-set.cc
   characters.cc
-# debug-parser.cc  # not to be used in production, it uses std::cout
+  debug-parser.cc
   idioms.cc
   instrumented-parser.cc
   message.cc
index 4976241..29cd4e6 100644 (file)
@@ -1,5 +1,6 @@
 #include "debug-parser.h"
-#include <iostream>
+#include "user-state.h"
+#include <ostream>
 #include <string>
 
 namespace Fortran {
@@ -7,13 +8,15 @@ namespace parser {
 
 std::optional<Success> DebugParser::Parse(ParseState &state) const {
   if (auto ustate = state.userState()) {
-    const CookedSource &cooked{ustate->cooked()};
-    if (auto context = state.context()) {
-      context->Emit(std::cout, cooked);
+    if (auto out = ustate->debugOutput()) {
+      const CookedSource &cooked{ustate->cooked()};
+      if (auto context = state.context()) {
+        context->Emit(*out, cooked);
+      }
+      Provenance p{cooked.GetProvenance(state.GetLocation()).start()};
+      cooked.allSources().Identify(*out, p, "", true);
+      *out << "   parser debug: " << std::string{str_, length_} << "\n\n";
     }
-    Provenance p{cooked.GetProvenance(state.GetLocation()).start()};
-    cooked.allSources().Identify(std::cout, p, "", true);
-    std::cout << "   parser debug: " << std::string{str_, length_} << "\n\n";
   }
   return {Success{}};
 }
index c2e9146..4fec230 100644 (file)
@@ -4,8 +4,6 @@
 // Implements the parser with syntax "(YOUR MESSAGE HERE)"_debug for use
 // in temporary modifications to the grammar intended for tracing the
 // flow of the parsers.  Not to be used in production.
-// When this feature is in use for temporary debugging, be sure to
-// compile and link debug-parser.cc.
 
 #include "basic-parsers.h"
 #include "parse-state.h"
index 7ba1c4e..02a9ed5 100644 (file)
@@ -78,9 +78,11 @@ void Parsing::DumpParsingLog(std::ostream &out) const {
   log_.Dump(out, cooked_);
 }
 
-void Parsing::Parse() {
+void Parsing::Parse(std::ostream *out) {
   UserState userState{cooked_};
-  userState.set_instrumentedParse(options_.instrumentedParse).set_log(&log_);
+  userState.set_debugOutput(out)
+      .set_instrumentedParse(options_.instrumentedParse)
+      .set_log(&log_);
   ParseState parseState{cooked_};
   parseState.set_inFixedForm(options_.isFixedForm)
       .set_encoding(options_.encoding)
index 1cfeb1a..3f6d3c9 100644 (file)
@@ -45,7 +45,7 @@ public:
   void DumpCookedChars(std::ostream &) const;
   void DumpProvenance(std::ostream &) const;
   void DumpParsingLog(std::ostream &) const;
-  void Parse();
+  void Parse(std::ostream *debugOutput = nullptr);
   void ClearLog();
 
   void Identify(std::ostream &o, const char *at, const std::string &prefix,
index 00a4e27..e2249cf 100644 (file)
@@ -11,6 +11,7 @@
 #include "parse-tree.h"
 #include <cinttypes>
 #include <optional>
+#include <ostream>
 #include <set>
 #include <unordered_set>
 
@@ -27,6 +28,12 @@ public:
 
   const CookedSource &cooked() const { return cooked_; }
 
+  std::ostream *debugOutput() const { return debugOutput_; }
+  UserState &set_debugOutput(std::ostream *out) {
+    debugOutput_ = out;
+    return *this;
+  }
+
   ParsingLog *log() const { return log_; }
   UserState &set_log(ParsingLog *log) {
     log_ = log;
@@ -71,6 +78,8 @@ public:
 private:
   const CookedSource &cooked_;
 
+  std::ostream *debugOutput_{nullptr};
+
   ParsingLog *log_{nullptr};
   bool instrumentedParse_{false};
 
index 0e3c58a..33cda50 100644 (file)
@@ -162,7 +162,7 @@ std::string CompileFortran(
     parsing.DumpCookedChars(std::cout);
     return {};
   }
-  parsing.Parse();
+  parsing.Parse(&std::cout);
   if (options.instrumentedParse) {
     parsing.DumpParsingLog(std::cout);
     return {};