[flang] Add ExtendToCover() to CharBlock and Interval
authorTim Keith <tkeith@nvidia.com>
Thu, 29 Nov 2018 17:10:19 +0000 (09:10 -0800)
committerTim Keith <tkeith@nvidia.com>
Thu, 29 Nov 2018 17:10:19 +0000 (09:10 -0800)
Use them in Scope::AddSourceRange().

Original-commit: flang-compiler/f18@ec2f6db46a42998ee1b1bed955538387fadf5c09
Reviewed-on: https://github.com/flang-compiler/f18/pull/230

flang/lib/common/interval.h
flang/lib/parser/char-block.h
flang/lib/semantics/scope.cc

index 1a8550f..ba123da 100644 (file)
@@ -69,6 +69,15 @@ public:
     }
     return false;
   }
+  void ExtendToCover(const Interval &that) {
+    if (size_ == 0) {
+      *this = that;
+    } else if (that.size_ != 0) {
+      const auto end{std::max(NextAfter(), that.NextAfter())};
+      start_ = std::min(start_, that.start_);
+      size_ = end - start_;
+    }
+  }
 
   std::size_t MemberOffset(const A &x) const {
     CHECK(Contains(x));
index ae829de..e9439f4 100644 (file)
@@ -52,6 +52,10 @@ public:
     return interval_.Contains(that.interval_);
   }
 
+  void ExtendToCover(const CharBlock &that) {
+    interval_.ExtendToCover(that.interval_);
+  }
+
   bool IsBlank() const {
     for (char ch : *this) {
       if (ch != ' ' && ch != '\t') {
index ae275b4..981d9fb 100644 (file)
@@ -147,13 +147,7 @@ const Scope *Scope::FindScope(const parser::CharBlock &source) const {
 }
 
 void Scope::AddSourceRange(const parser::CharBlock &source) {
-  if (sourceRange_.empty()) {
-    sourceRange_ = source;
-  } else if (!source.empty()) {
-    sourceRange_ =
-        parser::CharBlock(std::min(sourceRange_.begin(), source.begin()),
-            std::max(sourceRange_.end(), source.end()));
-  }
+  sourceRange_.ExtendToCover(source);
 }
 
 std::ostream &operator<<(std::ostream &os, const Scope &scope) {