[clang] [clang] Adjust TextDiagnostic style ranges for interesting source region (PR #164941)

via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 31 03:59:22 PDT 2025


================
@@ -521,8 +518,41 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
   if (BackColumnsRemoved > Columns(BackEllipse.size()))
     SourceLine.replace(SourceEnd.V, std::string::npos, BackEllipse);
 
+  // Since we've modified the SourceLine, we also need to adjust the line's
+  // highlighting information. In particular, if we've removed
+  // from the front of the line, we need to move the style ranges to the
+  // left and remove unneeded ranges.
+  Columns FrontDiff = FrontColumnsRemoved > FrontEllipse.size()
+                          ? FrontColumnsRemoved - FrontEllipse.size()
+                          : 0;
+  Columns CodeStart = FrontColumnsRemoved > FrontEllipse.size()
+                          ? FrontEllipse.size()
+                          : FrontColumnsRemoved;
+  Bytes CodeEnd = Map.columnToByte(CodeStart + ColumnsKept);
+
+  for (auto &R : Styles) {
+    // Style ranges too far left. Just move them where they don't bother.
+    if (R.End < static_cast<unsigned>(FrontDiff.V)) {
+      R.Start = R.End = std::numeric_limits<int>::max();
+      continue;
+    }
+    // Move them left. (Note that this can wrap R.Start, but that doesn't
+    // matter).
+    R.Start -= FrontDiff.V;
+    R.End -= FrontDiff.V;
+
+    // If the range overlaps the end of the code, don't leak into the back
+    // ellipse.
+    if (R.Start < static_cast<unsigned>(CodeEnd.V) &&
+        R.End > static_cast<unsigned>(CodeEnd.V))
+      R.End = CodeEnd.V;
+    // Don't leak into the ellipse at the end.
+    if (R.Start >= static_cast<unsigned>(CodeEnd.V))
+      R.Start = R.End = std::numeric_limits<int>::max();
----------------
Sirraide wrote:

```suggestion
    // Move them left. (Note that this can wrap R.Start, but that doesn't
    // matter).
    R.Start -= FrontDiff.V;
    R.End -= FrontDiff.V;
    
    // Style ranges too far left. Just move them where they don't bother.
    if (R.End < static_cast<unsigned>(FrontDiff.V) ||
        R.Start >= static_cast<unsigned>(CodeEnd.V)) {
      R.Start = R.End = std::numeric_limits<int>::max();
      continue;
    }

    // If the range overlaps the end of the code, don't leak into the back
    // ellipse.
    R.Start = std::min(R.Start, FrontDiff.V);
    R.End = std::max(R.End, CodeEnd.V);
```
Two changes here:

1. I’d merge the first and last `if` statement where we discard a range that is entirely outside the code we’re displaying; that also necessitates moving the two `-=` to the start of the loop.
2. Currently, this code only clamps the range against the back ellipsis, but not the front ellipsis. I think we should either do both or neither, so I’ve done both above (and I think it’s easiest to use `std::min`/`std::max` for that).

https://github.com/llvm/llvm-project/pull/164941


More information about the cfe-commits mailing list