[PATCH] D137214: [clang][modules] NFCI: Scaffolding for serialization of adjusted SourceManager offsets

2022-11-01 Thread Jan Svoboda via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG0bfc97e4f4eb: [clang][modules] NFCI: Scaffolding for 
serialization of adjusted SourceManager… (authored by jansvoboda11).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137214

Files:
  clang/include/clang/Serialization/ASTWriter.h
  clang/lib/Serialization/ASTWriter.cpp
  clang/lib/Serialization/ASTWriterDecl.cpp

Index: clang/lib/Serialization/ASTWriterDecl.cpp
===
--- clang/lib/Serialization/ASTWriterDecl.cpp
+++ clang/lib/Serialization/ASTWriterDecl.cpp
@@ -2457,11 +2457,12 @@
   SourceLocation Loc = D->getLocation();
   unsigned Index = ID - FirstDeclID;
   if (DeclOffsets.size() == Index)
-DeclOffsets.emplace_back(Loc, Offset, DeclTypesBlockStartOffset);
+DeclOffsets.emplace_back(getAdjustedLocation(Loc), Offset,
+ DeclTypesBlockStartOffset);
   else if (DeclOffsets.size() < Index) {
 // FIXME: Can/should this happen?
 DeclOffsets.resize(Index+1);
-DeclOffsets[Index].setLocation(Loc);
+DeclOffsets[Index].setLocation(getAdjustedLocation(Loc));
 DeclOffsets[Index].setBitOffset(Offset, DeclTypesBlockStartOffset);
   } else {
 llvm_unreachable("declarations should be emitted in ID order");
Index: clang/lib/Serialization/ASTWriter.cpp
===
--- clang/lib/Serialization/ASTWriter.cpp
+++ clang/lib/Serialization/ASTWriter.cpp
@@ -2063,7 +2063,7 @@
 Record.push_back(Code);
 
 // Starting offset of this entry within this module, so skip the dummy.
-Record.push_back(SLoc->getOffset() - 2);
+Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
 if (SLoc->isFile()) {
   const SrcMgr::FileInfo &File = SLoc->getFile();
   const SrcMgr::ContentCache *Content = &File.getContentCache();
@@ -2086,7 +2086,7 @@
 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
 Record.push_back(InputFileIDs[Content->OrigEntry]);
 
-Record.push_back(File.NumCreatedFIDs);
+Record.push_back(getAdjustedNumCreatedFIDs(FID));
 
 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
 if (FDI != FileDeclIDs.end()) {
@@ -2146,7 +2146,7 @@
   SourceLocation::UIntTy NextOffset = SourceMgr.getNextLocalOffset();
   if (I + 1 != N)
 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
-  Record.push_back(NextOffset - SLoc->getOffset() - 1);
+  Record.push_back(getAdjustedOffset(NextOffset - SLoc->getOffset()) - 1);
   Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
 }
   }
@@ -2170,7 +2170,7 @@
   {
 RecordData::value_type Record[] = {
 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
-SourceMgr.getNextLocalOffset() - 1 /* skip dummy */,
+getAdjustedOffset(SourceMgr.getNextLocalOffset()) - 1 /* skip dummy */,
 SLocEntryOffsetsBase - SourceManagerBlockOffset};
 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
   bytes(SLocEntryOffsets));
@@ -2569,7 +2569,7 @@
 uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
 assert((Offset >> 32) == 0 && "Preprocessed entity offset too large");
 PreprocessedEntityOffsets.push_back(
-PPEntityOffset((*E)->getSourceRange(), Offset));
+PPEntityOffset(getAdjustedRange((*E)->getSourceRange()), Offset));
 
 if (auto *MD = dyn_cast(*E)) {
   // Record this macro definition's ID.
@@ -3014,7 +3014,7 @@
 
 Record.push_back(FileIDAndFile.second.StateTransitions.size());
 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
-  Record.push_back(StatePoint.Offset);
+  Record.push_back(getAdjustedOffset(StatePoint.Offset));
   AddDiagState(StatePoint.State, false);
 }
   }
@@ -5225,12 +5225,42 @@
   Record.push_back(Raw);
 }
 
+FileID ASTWriter::getAdjustedFileID(FileID FID) const {
+  // TODO: Actually adjust this.
+  return FID;
+}
+
+unsigned ASTWriter::getAdjustedNumCreatedFIDs(FileID FID) const {
+  // TODO: Actually adjust this.
+  return PP->getSourceManager()
+  .getLocalSLocEntry(FID.ID)
+  .getFile()
+  .NumCreatedFIDs;
+}
+
+SourceLocation ASTWriter::getAdjustedLocation(SourceLocation Loc) const {
+  // TODO: Actually adjust this.
+  return Loc;
+}
+
+SourceRange ASTWriter::getAdjustedRange(SourceRange Range) const {
+  // TODO: Actually adjust this.
+  return Range;
+}
+
+SourceLocation::UIntTy
+ASTWriter::getAdjustedOffset(SourceLocation::UIntTy Offset) const {
+  // TODO: Actually adjust this.
+  return Offset;
+}
+
 void ASTWriter::AddFileID(FileID FID, RecordDataImpl &Record) {
-  Record.push_back(FID.getOpaqueValue());
+  Record.push_back(getAdjustedFileID(FID).getOpaqueValue());
 }
 
 

[PATCH] D137214: [clang][modules] NFCI: Scaffolding for serialization of adjusted SourceManager offsets

2022-11-01 Thread Duncan P. N. Exon Smith via Phabricator via cfe-commits
dexonsmith accepted this revision.
dexonsmith added a comment.
This revision is now accepted and ready to land.

LGTM.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137214

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D137214: [clang][modules] NFCI: Scaffolding for serialization of adjusted SourceManager offsets

2022-11-01 Thread Jan Svoboda via Phabricator via cfe-commits
jansvoboda11 created this revision.
jansvoboda11 added reviewers: Bigcheese, dexonsmith, vsapsai.
Herald added a subscriber: ributzka.
Herald added a project: All.
jansvoboda11 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This patch is a NFC prep for D136624 , where 
we start adjusting offsets into `SourceManager`.

Depends on D137213 .


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137214

Files:
  clang/include/clang/Serialization/ASTWriter.h
  clang/lib/Serialization/ASTWriter.cpp
  clang/lib/Serialization/ASTWriterDecl.cpp

Index: clang/lib/Serialization/ASTWriterDecl.cpp
===
--- clang/lib/Serialization/ASTWriterDecl.cpp
+++ clang/lib/Serialization/ASTWriterDecl.cpp
@@ -2457,11 +2457,12 @@
   SourceLocation Loc = D->getLocation();
   unsigned Index = ID - FirstDeclID;
   if (DeclOffsets.size() == Index)
-DeclOffsets.emplace_back(Loc, Offset, DeclTypesBlockStartOffset);
+DeclOffsets.emplace_back(getAdjustedLocation(Loc), Offset,
+ DeclTypesBlockStartOffset);
   else if (DeclOffsets.size() < Index) {
 // FIXME: Can/should this happen?
 DeclOffsets.resize(Index+1);
-DeclOffsets[Index].setLocation(Loc);
+DeclOffsets[Index].setLocation(getAdjustedLocation(Loc));
 DeclOffsets[Index].setBitOffset(Offset, DeclTypesBlockStartOffset);
   } else {
 llvm_unreachable("declarations should be emitted in ID order");
Index: clang/lib/Serialization/ASTWriter.cpp
===
--- clang/lib/Serialization/ASTWriter.cpp
+++ clang/lib/Serialization/ASTWriter.cpp
@@ -2063,7 +2063,7 @@
 Record.push_back(Code);
 
 // Starting offset of this entry within this module, so skip the dummy.
-Record.push_back(SLoc->getOffset() - 2);
+Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
 if (SLoc->isFile()) {
   const SrcMgr::FileInfo &File = SLoc->getFile();
   const SrcMgr::ContentCache *Content = &File.getContentCache();
@@ -2086,7 +2086,7 @@
 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
 Record.push_back(InputFileIDs[Content->OrigEntry]);
 
-Record.push_back(File.NumCreatedFIDs);
+Record.push_back(getAdjustedNumCreatedFIDs(FID));
 
 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
 if (FDI != FileDeclIDs.end()) {
@@ -2146,7 +2146,7 @@
   SourceLocation::UIntTy NextOffset = SourceMgr.getNextLocalOffset();
   if (I + 1 != N)
 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
-  Record.push_back(NextOffset - SLoc->getOffset() - 1);
+  Record.push_back(getAdjustedOffset(NextOffset - SLoc->getOffset()) - 1);
   Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
 }
   }
@@ -2170,7 +2170,7 @@
   {
 RecordData::value_type Record[] = {
 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
-SourceMgr.getNextLocalOffset() - 1 /* skip dummy */,
+getAdjustedOffset(SourceMgr.getNextLocalOffset()) - 1 /* skip dummy */,
 SLocEntryOffsetsBase - SourceManagerBlockOffset};
 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
   bytes(SLocEntryOffsets));
@@ -2569,7 +2569,7 @@
 uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
 assert((Offset >> 32) == 0 && "Preprocessed entity offset too large");
 PreprocessedEntityOffsets.push_back(
-PPEntityOffset((*E)->getSourceRange(), Offset));
+PPEntityOffset(getAdjustedRange((*E)->getSourceRange()), Offset));
 
 if (auto *MD = dyn_cast(*E)) {
   // Record this macro definition's ID.
@@ -3014,7 +3014,7 @@
 
 Record.push_back(FileIDAndFile.second.StateTransitions.size());
 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
-  Record.push_back(StatePoint.Offset);
+  Record.push_back(getAdjustedOffset(StatePoint.Offset));
   AddDiagState(StatePoint.State, false);
 }
   }
@@ -5225,12 +5225,42 @@
   Record.push_back(Raw);
 }
 
+FileID ASTWriter::getAdjustedFileID(FileID FID) const {
+  // TODO: Actually adjust this.
+  return FID;
+}
+
+unsigned ASTWriter::getAdjustedNumCreatedFIDs(FileID FID) const {
+  // TODO: Actually adjust this.
+  return PP->getSourceManager()
+  .getLocalSLocEntry(FID.ID)
+  .getFile()
+  .NumCreatedFIDs;
+}
+
+SourceLocation ASTWriter::getAdjustedLocation(SourceLocation Loc) const {
+  // TODO: Actually adjust this.
+  return Loc;
+}
+
+SourceRange ASTWriter::getAdjustedRange(SourceRange Range) const {
+  // TODO: Actually adjust this.
+  return Range;
+}
+
+SourceLocation::UIntTy
+ASTWriter::getAdjustedOffset(SourceLocation::UIntTy Offset) const {
+  // TODO: Actually adjust this.
+  return Offset;
+}
+
 void ASTWriter::AddFileID(FileID FID, Record