[clang] [llvm] [clang][Modules][Serialization] Avoid serializing duplicate source location entries for shared input files (PR #209795)

Ayokunle Amodu via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 22 14:48:34 PDT 2026


================
@@ -6789,9 +6839,39 @@ unsigned ASTWriter::getAdjustedNumCreatedFIDs(FileID FID) const {
   return AdjustedNumCreatedFIDs;
 }
 
+SourceLocation ASTWriter::getRedirectedLocation(SourceLocation Loc) const {
+  if (NonAffectingRedirectAdjustments.empty())
+    return SourceLocation();
+
+  SourceLocation::UIntTy Offset = Loc.getOffset();
+  if (PP->getSourceManager().isLoadedOffset(Offset))
+    return SourceLocation();
+
+  unsigned Idx = getNonAffectingRangeLowerBound(Offset);
+  if (Idx == NonAffectingRanges.size())
+    return SourceLocation();
+
+  // The search only rules out ranges ending before the offset, so check that
+  // the offset really is inside the one we landed on.
+  if (Offset < NonAffectingRanges[Idx].getBegin().getOffset())
+    return SourceLocation();
+
+  int64_t Adjustment = NonAffectingRedirectAdjustments[Idx];
+  if (!Adjustment)
+    return SourceLocation();
+  return SourceLocation::getFileLoc(static_cast<SourceLocation::UIntTy>(
+      static_cast<int64_t>(Offset) - Adjustment));
----------------
ayokunle321 wrote:

Adjustment is the distance between local file's start and the start of the copy that has already been loaded. So Offset-Adjustment is needed so that we can rebase the offset to the already loaded range. As it's now in the loaded range, the writer can emit the correct pair encoding, i.e., {already loaded module, offset}. 

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


More information about the llvm-commits mailing list