zequanwu updated this revision to Diff 277538.
zequanwu added a comment.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

Merge overlapped `SkippedRegion`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D83592/new/

https://reviews.llvm.org/D83592

Files:
  clang/include/clang/Lex/PPCallbacks.h
  clang/lib/CodeGen/CoverageMappingGen.cpp
  clang/lib/CodeGen/CoverageMappingGen.h
  clang/lib/Parse/Parser.cpp
  llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp

Index: llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp
===================================================================
--- llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp
+++ llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp
@@ -148,6 +148,41 @@
   encodeULEB128(encodeCounter(Expressions, C), OS);
 }
 
+static MutableArrayRef<CounterMappingRegion>
+mergeSkippedRegions(MutableArrayRef<CounterMappingRegion> MappingRegions) {
+  SmallVector<CounterMappingRegion, 32> MergedRegions;
+  for (size_t I = 0; I < MappingRegions.size() - 1; I++) {
+    CounterMappingRegion First = MappingRegions[I];
+    CounterMappingRegion Second = MappingRegions[I+1];
+    if (First.FileID != Second.FileID ||
+        First.Kind != CounterMappingRegion::SkippedRegion ||
+        First.Kind != Second.Kind) {
+      MergedRegions.push_back(First);
+      continue;
+    }
+
+    if (First.LineEnd > Second.LineEnd ||
+        (First.LineEnd == Second.LineEnd &&
+         First.ColumnEnd >= Second.ColumnEnd)) {
+      // When second region is inside first region, discard second region.
+      MergedRegions.push_back(First);
+      I++;
+    } else if (Second.LineStart >= First.LineStart &&
+               Second.LineStart <= First.LineEnd) {
+      // When the start location of second region is inside first region and the
+      // end location is outside first region.
+      MergedRegions.push_back(CounterMappingRegion::makeSkipped(
+          First.FileID, First.LineStart, First.ColumnStart, Second.LineEnd,
+          Second.ColumnEnd));
+    } else {
+      // When there is no overlapping.
+      MergedRegions.push_back(First);
+    }
+  }
+
+  return MergedRegions;
+}
+
 void CoverageMappingWriter::write(raw_ostream &OS) {
   // Check that we don't have any bogus regions.
   assert(all_of(MappingRegions,
@@ -167,6 +202,9 @@
     return LHS.Kind < RHS.Kind;
   });
 
+  // Merge overlapped regions.
+  MappingRegions = mergeSkippedRegions(MappingRegions);
+
   // Write out the fileid -> filename mapping.
   encodeULEB128(VirtualFileMapping.size(), OS);
   for (const auto &FileID : VirtualFileMapping)
Index: clang/lib/Parse/Parser.cpp
===================================================================
--- clang/lib/Parse/Parser.cpp
+++ clang/lib/Parse/Parser.cpp
@@ -34,6 +34,7 @@
   explicit ActionCommentHandler(Sema &S) : S(S) { }
 
   bool HandleComment(Preprocessor &PP, SourceRange Comment) override {
+    PP.getPPCallbacks()->CommentSkipped(Comment);
     S.ActOnComment(Comment);
     return false;
   }
Index: clang/lib/CodeGen/CoverageMappingGen.h
===================================================================
--- clang/lib/CodeGen/CoverageMappingGen.h
+++ clang/lib/CodeGen/CoverageMappingGen.h
@@ -38,6 +38,8 @@
   ArrayRef<SourceRange> getSkippedRanges() const { return SkippedRanges; }
 
   void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
+
+  void CommentSkipped(SourceRange Range) override;
 };
 
 namespace CodeGen {
Index: clang/lib/CodeGen/CoverageMappingGen.cpp
===================================================================
--- clang/lib/CodeGen/CoverageMappingGen.cpp
+++ clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -39,6 +39,10 @@
   SkippedRanges.push_back(Range);
 }
 
+void CoverageSourceInfo::CommentSkipped(SourceRange Range) {
+  SkippedRanges.push_back(Range);
+}
+
 namespace {
 
 /// A region of source code that can be mapped to a counter.
Index: clang/include/clang/Lex/PPCallbacks.h
===================================================================
--- clang/include/clang/Lex/PPCallbacks.h
+++ clang/include/clang/Lex/PPCallbacks.h
@@ -319,6 +319,10 @@
   virtual void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) {
   }
 
+  /// Hood called when the source range is comment, which should be skipped.
+  /// \param Range The SourceRange that is comment.
+  virtual void CommentSkipped(SourceRange Range) {}
+
   enum ConditionValueKind {
     CVK_NotEvaluated, CVK_False, CVK_True
   };
@@ -565,6 +569,11 @@
     Second->SourceRangeSkipped(Range, EndifLoc);
   }
 
+  void CommentSkipped(SourceRange Range) override {
+    First->CommentSkipped(Range);
+    Second->CommentSkipped(Range);
+  }
+
   /// Hook called whenever an \#if is seen.
   void If(SourceLocation Loc, SourceRange ConditionRange,
           ConditionValueKind ConditionValue) override {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to