[flang] Extend common block size to cover equivalence storage

The size of common block should be extended to cover any storage
sequence that are storage associated with the common block via
equivalences (8.10.2.2 point 1 (2)).

In symbol size and offset computation, the size of the common block
was not always extended to cover storage association. It was only done
if the "base symbol of an equivalence group"(*) appeared in a common block
statement. Correct this to cover all cases where a symbol appearing in a
common block statement is storage associated.

(*) the base symbol of an equivalence group is the symbol whose storage
starts first in a storage association (if several symbols starts first,
the base symbol is the last one visited by the algorithm going through
the equivalence sets).

Differential Revision: https://reviews.llvm.org/D109156
diff --git a/flang/lib/Semantics/compute-offsets.cpp b/flang/lib/Semantics/compute-offsets.cpp
index a78d3f8..5ebe3e4 100644
--- a/flang/lib/Semantics/compute-offsets.cpp
+++ b/flang/lib/Semantics/compute-offsets.cpp
@@ -151,16 +151,12 @@
   for (auto &object : details.objects()) {
     Symbol &symbol{*object};
     DoSymbol(symbol);
+    auto eqIter{equivalenceBlock_.end()};
     auto iter{dependents_.find(symbol)};
     if (iter == dependents_.end()) {
-      // Get full extent of any EQUIVALENCE block into size of COMMON
-      auto eqIter{equivalenceBlock_.find(symbol)};
+      auto eqIter = equivalenceBlock_.find(symbol);
       if (eqIter != equivalenceBlock_.end()) {
-        SizeAndAlignment &blockInfo{eqIter->second};
-        DoEquivalenceBlockBase(symbol, blockInfo);
-        minSize = std::max(
-            minSize, std::max(offset_, symbol.offset() + blockInfo.size));
-        minAlignment = std::max(minAlignment, blockInfo.alignment);
+        DoEquivalenceBlockBase(symbol, eqIter->second);
       }
     } else {
       SymbolAndOffset &dep{iter->second};
@@ -183,10 +179,19 @@
             "'%s' cannot backward-extend COMMON block /%s/ via EQUIVALENCE with '%s'"_err_en_US,
             symbol.name(), commonBlock.name(), base.name());
       } else {
+        eqIter = equivalenceBlock_.find(base);
         base.get<ObjectEntityDetails>().set_commonBlock(commonBlock);
         base.set_offset(symbol.offset() - dep.offset);
       }
     }
+    // Get full extent of any EQUIVALENCE block into size of COMMON ( see
+    // 8.10.2.2 point 1 (2))
+    if (eqIter != equivalenceBlock_.end()) {
+      SizeAndAlignment &blockInfo{eqIter->second};
+      minSize = std::max(
+          minSize, std::max(offset_, eqIter->first->offset() + blockInfo.size));
+      minAlignment = std::max(minAlignment, blockInfo.alignment);
+    }
   }
   commonBlock.set_size(std::max(minSize, offset_));
   details.set_alignment(std::max(minAlignment, alignment_));