Skip to content

Commit 9046d21

Browse files
committed
[Modules] No transitive source location change
1 parent c6a65e4 commit 9046d21

File tree

16 files changed

+238
-247
lines changed

16 files changed

+238
-247
lines changed

clang/include/clang/Basic/SourceLocation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class SourceLocation {
9090
friend class ASTWriter;
9191
friend class SourceManager;
9292
friend struct llvm::FoldingSetTrait<SourceLocation, void>;
93+
friend class SourceLocationEncoding;
9394

9495
public:
9596
using UIntTy = uint32_t;

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "clang/Basic/IdentifierTable.h"
2323
#include "clang/Basic/OperatorKinds.h"
2424
#include "clang/Basic/SourceLocation.h"
25+
#include "clang/Serialization/SourceLocationEncoding.h"
2526
#include "llvm/ADT/DenseMapInfo.h"
2627
#include "llvm/Bitstream/BitCodes.h"
2728
#include <cassert>
@@ -175,44 +176,47 @@ const unsigned int NUM_PREDEF_SUBMODULE_IDS = 1;
175176

176177
/// Source range/offset of a preprocessed entity.
177178
struct PPEntityOffset {
179+
using RawLocEncoding = SourceLocationEncoding::RawLocEncoding;
180+
178181
/// Raw source location of beginning of range.
179-
SourceLocation::UIntTy Begin;
182+
RawLocEncoding Begin;
180183

181184
/// Raw source location of end of range.
182-
SourceLocation::UIntTy End;
185+
RawLocEncoding End;
183186

184187
/// Offset in the AST file relative to ModuleFile::MacroOffsetsBase.
185188
uint32_t BitOffset;
186189

187-
PPEntityOffset(SourceRange R, uint32_t BitOffset)
188-
: Begin(R.getBegin().getRawEncoding()), End(R.getEnd().getRawEncoding()),
190+
PPEntityOffset(RawLocEncoding Begin, RawLocEncoding End, uint32_t BitOffset)
191+
: Begin(Begin), End(End),
189192
BitOffset(BitOffset) {}
190193

191-
SourceLocation getBegin() const {
192-
return SourceLocation::getFromRawEncoding(Begin);
194+
RawLocEncoding getBegin() const {
195+
return Begin;
193196
}
194-
195-
SourceLocation getEnd() const {
196-
return SourceLocation::getFromRawEncoding(End);
197+
RawLocEncoding getEnd() const {
198+
return End;
197199
}
198200
};
199201

200202
/// Source range of a skipped preprocessor region
201203
struct PPSkippedRange {
204+
using RawLocEncoding = SourceLocationEncoding::RawLocEncoding;
205+
202206
/// Raw source location of beginning of range.
203-
SourceLocation::UIntTy Begin;
207+
RawLocEncoding Begin;
204208
/// Raw source location of end of range.
205-
SourceLocation::UIntTy End;
209+
RawLocEncoding End;
206210

207-
PPSkippedRange(SourceRange R)
208-
: Begin(R.getBegin().getRawEncoding()), End(R.getEnd().getRawEncoding()) {
211+
PPSkippedRange(RawLocEncoding Begin, RawLocEncoding End)
212+
: Begin(Begin), End(End) {
209213
}
210214

211-
SourceLocation getBegin() const {
212-
return SourceLocation::getFromRawEncoding(Begin);
215+
RawLocEncoding getBegin() const {
216+
return Begin;
213217
}
214-
SourceLocation getEnd() const {
215-
return SourceLocation::getFromRawEncoding(End);
218+
RawLocEncoding getEnd() const {
219+
return End;
216220
}
217221
};
218222

@@ -239,25 +243,28 @@ struct UnderalignedInt64 {
239243

240244
/// Source location and bit offset of a declaration.
241245
struct DeclOffset {
246+
using RawLocEncoding = SourceLocationEncoding::RawLocEncoding;
247+
242248
/// Raw source location.
243-
SourceLocation::UIntTy Loc = 0;
249+
RawLocEncoding RawLoc = 0;
244250

245251
/// Offset relative to the start of the DECLTYPES_BLOCK block. Keep
246252
/// structure alignment 32-bit and avoid padding gap because undefined
247253
/// value in the padding affects AST hash.
248254
UnderalignedInt64 BitOffset;
249255

250256
DeclOffset() = default;
251-
DeclOffset(SourceLocation Loc, uint64_t BitOffset,
252-
uint64_t DeclTypesBlockStartOffset) {
253-
setLocation(Loc);
257+
DeclOffset(RawLocEncoding RawLoc, uint64_t BitOffset,
258+
uint64_t DeclTypesBlockStartOffset) : RawLoc(RawLoc) {
254259
setBitOffset(BitOffset, DeclTypesBlockStartOffset);
255260
}
256261

257-
void setLocation(SourceLocation L) { Loc = L.getRawEncoding(); }
262+
void setRawLoc(RawLocEncoding Loc) {
263+
RawLoc = Loc;
264+
}
258265

259-
SourceLocation getLocation() const {
260-
return SourceLocation::getFromRawEncoding(Loc);
266+
RawLocEncoding getRawLoc() const {
267+
return RawLoc;
261268
}
262269

263270
void setBitOffset(uint64_t Offset, const uint64_t DeclTypesBlockStartOffset) {

clang/include/clang/Serialization/ASTReader.h

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ class ASTReader
696696
/// Mapping from global submodule IDs to the module file in which the
697697
/// submodule resides along with the offset that should be added to the
698698
/// global submodule ID to produce a local ID.
699-
GlobalSubmoduleMapType GlobalSubmoduleMap;
699+
mutable GlobalSubmoduleMapType GlobalSubmoduleMap;
700700

701701
/// A set of hidden declarations.
702702
using HiddenNames = SmallVector<Decl *, 2>;
@@ -942,6 +942,10 @@ class ASTReader
942942
/// Sema tracks these to emit deferred diags.
943943
llvm::SmallSetVector<serialization::DeclID, 4> DeclsToCheckForDeferredDiags;
944944

945+
/// The module files imported by different module files. Indirectly imported module
946+
/// files are included too. The information comes from ReadModuleOffsetMap(ModuleFile&).
947+
mutable llvm::DenseMap<ModuleFile *, llvm::SmallVector<ModuleFile *>> ImportedModuleFiles;
948+
945949
private:
946950
struct ImportedSubmodule {
947951
serialization::SubmoduleID ID;
@@ -1761,6 +1765,7 @@ class ASTReader
17611765

17621766
/// Retrieve the module manager.
17631767
ModuleManager &getModuleManager() { return ModuleMgr; }
1768+
const ModuleManager &getModuleManager() const { return ModuleMgr; }
17641769

17651770
/// Retrieve the preprocessor.
17661771
Preprocessor &getPreprocessor() const { return PP; }
@@ -2171,7 +2176,7 @@ class ASTReader
21712176
/// Retrieve the global submodule ID given a module and its local ID
21722177
/// number.
21732178
serialization::SubmoduleID
2174-
getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID);
2179+
getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) const;
21752180

21762181
/// Retrieve the submodule that corresponds to a global submodule ID.
21772182
///
@@ -2184,7 +2189,7 @@ class ASTReader
21842189

21852190
/// Retrieve the module file with a given local ID within the specified
21862191
/// ModuleFile.
2187-
ModuleFile *getLocalModuleFile(ModuleFile &M, unsigned ID);
2192+
ModuleFile *getLocalModuleFile(ModuleFile &M, unsigned ID) const;
21882193

21892194
/// Get an ID for the given module file.
21902195
unsigned getModuleFileID(ModuleFile *M);
@@ -2220,40 +2225,47 @@ class ASTReader
22202225
return Sema::AlignPackInfo::getFromRawEncoding(Raw);
22212226
}
22222227

2228+
using RawLocEncoding = SourceLocationEncoding::RawLocEncoding;
2229+
22232230
/// Read a source location from raw form and return it in its
22242231
/// originating module file's source location space.
2225-
SourceLocation ReadUntranslatedSourceLocation(SourceLocation::UIntTy Raw,
2226-
LocSeq *Seq = nullptr) const {
2232+
std::pair<SourceLocation, unsigned>
2233+
ReadUntranslatedSourceLocation(RawLocEncoding Raw,
2234+
LocSeq *Seq = nullptr) const {
22272235
return SourceLocationEncoding::decode(Raw, Seq);
22282236
}
22292237

22302238
/// Read a source location from raw form.
2231-
SourceLocation ReadSourceLocation(ModuleFile &ModuleFile,
2232-
SourceLocation::UIntTy Raw,
2239+
SourceLocation ReadRawSourceLocation(ModuleFile &MF,
2240+
RawLocEncoding Raw,
22332241
LocSeq *Seq = nullptr) const {
2234-
SourceLocation Loc = ReadUntranslatedSourceLocation(Raw, Seq);
2235-
return TranslateSourceLocation(ModuleFile, Loc);
2242+
if (!MF.ModuleOffsetMap.empty())
2243+
ReadModuleOffsetMap(MF);
2244+
2245+
auto [Loc, ModuleFileIndex] = ReadUntranslatedSourceLocation(Raw, Seq);
2246+
ModuleFile *ModuleFileHomingLoc = ModuleFileIndex ? ImportedModuleFiles[&MF][ModuleFileIndex - 1] : &MF;
2247+
return TranslateSourceLocation(*ModuleFileHomingLoc, Loc);
22362248
}
22372249

22382250
/// Translate a source location from another module file's source
22392251
/// location space into ours.
22402252
SourceLocation TranslateSourceLocation(ModuleFile &ModuleFile,
22412253
SourceLocation Loc) const {
2242-
if (!ModuleFile.ModuleOffsetMap.empty())
2243-
ReadModuleOffsetMap(ModuleFile);
2244-
assert(ModuleFile.SLocRemap.find(Loc.getOffset()) !=
2245-
ModuleFile.SLocRemap.end() &&
2246-
"Cannot find offset to remap.");
2247-
SourceLocation::IntTy Remap =
2248-
ModuleFile.SLocRemap.find(Loc.getOffset())->second;
2249-
return Loc.getLocWithOffset(Remap);
2254+
if (Loc.isInvalid())
2255+
return Loc;
2256+
2257+
// It implies that the Loc is already translated.
2258+
if (SourceMgr.isLoadedSourceLocation(Loc))
2259+
return Loc;
2260+
2261+
return Loc.getLocWithOffset(ModuleFile.SLocEntryBaseOffset - 2);
22502262
}
22512263

22522264
/// Read a source location.
22532265
SourceLocation ReadSourceLocation(ModuleFile &ModuleFile,
22542266
const RecordDataImpl &Record, unsigned &Idx,
22552267
LocSeq *Seq = nullptr) {
2256-
return ReadSourceLocation(ModuleFile, Record[Idx++], Seq);
2268+
return ReadRawSourceLocation(ModuleFile, Record[Idx++], Seq);
22572269
}
22582270

22592271
/// Read a FileID.

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,9 @@ class ASTWriter : public ASTDeserializationListener,
648648
void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record,
649649
LocSeq *Seq = nullptr);
650650

651+
SourceLocationEncoding::RawLocEncoding
652+
getRawSourceLocationEncoding(SourceLocation Loc, LocSeq *Seq = nullptr);
653+
651654
/// Emit a source range.
652655
void AddSourceRange(SourceRange Range, RecordDataImpl &Record,
653656
LocSeq *Seq = nullptr);

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,6 @@ class ModuleFile {
295295
/// AST file.
296296
const uint32_t *SLocEntryOffsets = nullptr;
297297

298-
/// Remapping table for source locations in this module.
299-
ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy, 2>
300-
SLocRemap;
301-
302298
// === Identifiers ===
303299

304300
/// The number of identifiers in this AST file.

clang/include/clang/Serialization/SourceLocationEncoding.h

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,19 @@ class SourceLocationEncoding {
5252
friend SourceLocationSequence;
5353

5454
public:
55-
static uint64_t encode(SourceLocation Loc,
55+
using RawLocEncoding = uint64_t;
56+
57+
static RawLocEncoding encode(SourceLocation Loc,
58+
UIntTy BaseOffset,
59+
unsigned BaseModuleFileIndex,
5660
SourceLocationSequence * = nullptr);
57-
static SourceLocation decode(uint64_t, SourceLocationSequence * = nullptr);
61+
static std::pair<SourceLocation, unsigned> decode(RawLocEncoding, SourceLocationSequence * = nullptr);
5862
};
5963

64+
/// TODO: Remove SourceLocationSequence since it is not used now.
65+
/// Since we will put the index for ModuleFile in the high bits in the encodings
66+
/// for source locations, it is meaningless to reduce the size of source locations.
67+
///
6068
/// Serialized encoding of a sequence of SourceLocations.
6169
///
6270
/// Optimized to produce small values when locations with the sequence are
@@ -149,14 +157,30 @@ class SourceLocationSequence::State {
149157
operator SourceLocationSequence *() { return &Seq; }
150158
};
151159

152-
inline uint64_t SourceLocationEncoding::encode(SourceLocation Loc,
160+
inline SourceLocationEncoding::RawLocEncoding
161+
SourceLocationEncoding::encode(SourceLocation Loc,
162+
UIntTy BaseOffset,
163+
unsigned BaseModuleFileIndex,
153164
SourceLocationSequence *Seq) {
154-
return Seq ? Seq->encode(Loc) : encodeRaw(Loc.getRawEncoding());
165+
if (Loc.isInvalid())
166+
return 0;
167+
168+
assert(Loc.getOffset() > BaseOffset);
169+
Loc = Loc.getLocWithOffset(-BaseOffset);
170+
RawLocEncoding Encoded = encodeRaw(Loc.getRawEncoding());
171+
assert(Encoded < ((RawLocEncoding)1 << 32));
172+
173+
assert(BaseModuleFileIndex < ((RawLocEncoding)1 << 32));
174+
Encoded |= (RawLocEncoding)BaseModuleFileIndex << 32;
175+
return Encoded;
155176
}
156-
inline SourceLocation
157-
SourceLocationEncoding::decode(uint64_t Encoded, SourceLocationSequence *Seq) {
158-
return Seq ? Seq->decode(Encoded)
159-
: SourceLocation::getFromRawEncoding(decodeRaw(Encoded));
177+
inline std::pair<SourceLocation, unsigned>
178+
SourceLocationEncoding::decode(RawLocEncoding Encoded, SourceLocationSequence *Seq) {
179+
unsigned ModuleFileIndex = Encoded >> 32;
180+
Encoded &= ((RawLocEncoding)1 << 33) - 1;
181+
SourceLocation Loc = SourceLocation::getFromRawEncoding(decodeRaw(Encoded));
182+
183+
return {Loc, ModuleFileIndex};
160184
}
161185

162186
} // namespace clang

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,8 +2373,6 @@ bool ASTUnit::serialize(raw_ostream &OS) {
23732373
return serializeUnit(Writer, Buffer, getSema(), OS);
23742374
}
23752375

2376-
using SLocRemap = ContinuousRangeMap<unsigned, int, 2>;
2377-
23782376
void ASTUnit::TranslateStoredDiagnostics(
23792377
FileManager &FileMgr,
23802378
SourceManager &SrcMgr,

0 commit comments

Comments
 (0)