@@ -160,8 +160,8 @@ static TypeCode getTypeCodeForTypeClass(Type::TypeClass id) {
160
160
161
161
namespace {
162
162
163
- std::set<const FileEntry *> GetAllModuleMaps (const HeaderSearch &HS,
164
- Module *RootModule) {
163
+ std::set<const FileEntry *> GetAffectingModuleMaps (const HeaderSearch &HS,
164
+ Module *RootModule) {
165
165
std::set<const FileEntry *> ModuleMaps{};
166
166
std::set<const Module *> ProcessedModules;
167
167
SmallVector<const Module *> ModulesToProcess{RootModule};
@@ -1495,15 +1495,8 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1495
1495
AddFileID (SM.getMainFileID (), Record);
1496
1496
Stream.EmitRecord (ORIGINAL_FILE_ID, Record);
1497
1497
1498
- std::set<const FileEntry *> AffectingModuleMaps;
1499
- if (WritingModule) {
1500
- AffectingModuleMaps =
1501
- GetAllModuleMaps (PP.getHeaderSearchInfo (), WritingModule);
1502
- }
1503
-
1504
1498
WriteInputFiles (Context.SourceMgr ,
1505
- PP.getHeaderSearchInfo ().getHeaderSearchOpts (),
1506
- AffectingModuleMaps);
1499
+ PP.getHeaderSearchInfo ().getHeaderSearchOpts ());
1507
1500
Stream.ExitBlock ();
1508
1501
}
1509
1502
@@ -1521,9 +1514,8 @@ struct InputFileEntry {
1521
1514
1522
1515
} // namespace
1523
1516
1524
- void ASTWriter::WriteInputFiles (
1525
- SourceManager &SourceMgr, HeaderSearchOptions &HSOpts,
1526
- std::set<const FileEntry *> &AffectingModuleMaps) {
1517
+ void ASTWriter::WriteInputFiles (SourceManager &SourceMgr,
1518
+ HeaderSearchOptions &HSOpts) {
1527
1519
using namespace llvm ;
1528
1520
1529
1521
Stream.EnterSubblock (INPUT_FILES_BLOCK_ID, 4 );
@@ -1563,15 +1555,9 @@ void ASTWriter::WriteInputFiles(
1563
1555
if (!Cache->OrigEntry )
1564
1556
continue ;
1565
1557
1566
- if (isModuleMap (File.getFileCharacteristic ()) &&
1567
- !isSystem (File.getFileCharacteristic ()) &&
1568
- !AffectingModuleMaps.empty () &&
1569
- AffectingModuleMaps.find (Cache->OrigEntry ) ==
1570
- AffectingModuleMaps.end ()) {
1571
- SkippedModuleMaps.insert (Cache->OrigEntry );
1572
- // Do not emit modulemaps that do not affect current module.
1558
+ // Do not emit input files that do not affect current module.
1559
+ if (!IsSLocAffecting[I])
1573
1560
continue ;
1574
- }
1575
1561
1576
1562
InputFileEntry Entry;
1577
1563
Entry.File = Cache->OrigEntry ;
@@ -2082,12 +2068,9 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
2082
2068
if (SLoc->isFile ()) {
2083
2069
const SrcMgr::FileInfo &File = SLoc->getFile ();
2084
2070
const SrcMgr::ContentCache *Content = &File.getContentCache ();
2085
- if (Content->OrigEntry && !SkippedModuleMaps.empty () &&
2086
- SkippedModuleMaps.find (Content->OrigEntry ) !=
2087
- SkippedModuleMaps.end ()) {
2088
- // Do not emit files that were not listed as inputs.
2071
+ // Do not emit files that were not listed as inputs.
2072
+ if (!IsSLocAffecting[I])
2089
2073
continue ;
2090
- }
2091
2074
SLocEntryOffsets.push_back (Offset);
2092
2075
// Starting offset of this entry within this module, so skip the dummy.
2093
2076
Record.push_back (getAdjustedOffset (SLoc->getOffset ()) - 2 );
@@ -4531,6 +4514,69 @@ static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4531
4514
}
4532
4515
}
4533
4516
4517
+ void ASTWriter::collectNonAffectingInputFiles () {
4518
+ SourceManager &SrcMgr = PP->getSourceManager ();
4519
+ unsigned N = SrcMgr.local_sloc_entry_size ();
4520
+
4521
+ IsSLocAffecting.resize (N, true );
4522
+
4523
+ if (!WritingModule)
4524
+ return ;
4525
+
4526
+ auto AffectingModuleMaps =
4527
+ GetAffectingModuleMaps (PP->getHeaderSearchInfo (), WritingModule);
4528
+
4529
+ unsigned FileIDAdjustment = 0 ;
4530
+ unsigned OffsetAdjustment = 0 ;
4531
+
4532
+ NonAffectingFileIDAdjustments.reserve (N);
4533
+ NonAffectingOffsetAdjustments.reserve (N);
4534
+
4535
+ NonAffectingFileIDAdjustments.push_back (FileIDAdjustment);
4536
+ NonAffectingOffsetAdjustments.push_back (OffsetAdjustment);
4537
+
4538
+ for (unsigned I = 1 ; I != N; ++I) {
4539
+ const SrcMgr::SLocEntry *SLoc = &SrcMgr.getLocalSLocEntry (I);
4540
+ FileID FID = FileID::get (I);
4541
+ assert (&SrcMgr.getSLocEntry (FID) == SLoc);
4542
+
4543
+ if (!SLoc->isFile ())
4544
+ continue ;
4545
+ const SrcMgr::FileInfo &File = SLoc->getFile ();
4546
+ const SrcMgr::ContentCache *Cache = &File.getContentCache ();
4547
+ if (!Cache->OrigEntry )
4548
+ continue ;
4549
+
4550
+ if (!isModuleMap (File.getFileCharacteristic ()) ||
4551
+ isSystem (File.getFileCharacteristic ()) || AffectingModuleMaps.empty () ||
4552
+ AffectingModuleMaps.find (Cache->OrigEntry ) != AffectingModuleMaps.end ())
4553
+ continue ;
4554
+
4555
+ IsSLocAffecting[I] = false ;
4556
+
4557
+ FileIDAdjustment += 1 ;
4558
+ // Even empty files take up one element in the offset table.
4559
+ OffsetAdjustment += SrcMgr.getFileIDSize (FID) + 1 ;
4560
+
4561
+ // If the previous file was non-affecting as well, just extend its entry
4562
+ // with our information.
4563
+ if (!NonAffectingFileIDs.empty () &&
4564
+ NonAffectingFileIDs.back ().ID == FID.ID - 1 ) {
4565
+ NonAffectingFileIDs.back () = FID;
4566
+ NonAffectingRanges.back ().setEnd (SrcMgr.getLocForEndOfFile (FID));
4567
+ NonAffectingFileIDAdjustments.back () = FileIDAdjustment;
4568
+ NonAffectingOffsetAdjustments.back () = OffsetAdjustment;
4569
+ continue ;
4570
+ }
4571
+
4572
+ NonAffectingFileIDs.push_back (FID);
4573
+ NonAffectingRanges.emplace_back (SrcMgr.getLocForStartOfFile (FID),
4574
+ SrcMgr.getLocForEndOfFile (FID));
4575
+ NonAffectingFileIDAdjustments.push_back (FileIDAdjustment);
4576
+ NonAffectingOffsetAdjustments.push_back (OffsetAdjustment);
4577
+ }
4578
+ }
4579
+
4534
4580
ASTFileSignature ASTWriter::WriteASTCore (Sema &SemaRef, StringRef isysroot,
4535
4581
Module *WritingModule) {
4536
4582
using namespace llvm ;
@@ -4544,6 +4590,8 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4544
4590
ASTContext &Context = SemaRef.Context ;
4545
4591
Preprocessor &PP = SemaRef.PP ;
4546
4592
4593
+ collectNonAffectingInputFiles ();
4594
+
4547
4595
// Set up predefined declaration IDs.
4548
4596
auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4549
4597
if (D) {
@@ -5233,32 +5281,65 @@ void ASTWriter::AddAlignPackInfo(const Sema::AlignPackInfo &Info,
5233
5281
}
5234
5282
5235
5283
FileID ASTWriter::getAdjustedFileID (FileID FID) const {
5236
- // TODO: Actually adjust this.
5237
- return FID;
5284
+ if (FID.isInvalid () || PP->getSourceManager ().isLoadedFileID (FID) ||
5285
+ NonAffectingFileIDs.empty ())
5286
+ return FID;
5287
+ auto It = llvm::lower_bound (NonAffectingFileIDs, FID);
5288
+ unsigned Idx = std::distance (NonAffectingFileIDs.begin (), It);
5289
+ unsigned Offset = NonAffectingFileIDAdjustments[Idx];
5290
+ return FileID::get (FID.getOpaqueValue () - Offset);
5238
5291
}
5239
5292
5240
5293
unsigned ASTWriter::getAdjustedNumCreatedFIDs (FileID FID) const {
5241
- // TODO: Actually adjust this.
5242
- return PP->getSourceManager ()
5243
- .getLocalSLocEntry (FID.ID )
5244
- .getFile ()
5245
- .NumCreatedFIDs ;
5294
+ unsigned NumCreatedFIDs = PP->getSourceManager ()
5295
+ .getLocalSLocEntry (FID.ID )
5296
+ .getFile ()
5297
+ .NumCreatedFIDs ;
5298
+
5299
+ unsigned AdjustedNumCreatedFIDs = 0 ;
5300
+ for (unsigned I = FID.ID , N = I + NumCreatedFIDs; I != N; ++I)
5301
+ if (IsSLocAffecting[I])
5302
+ ++AdjustedNumCreatedFIDs;
5303
+ return AdjustedNumCreatedFIDs;
5246
5304
}
5247
5305
5248
5306
SourceLocation ASTWriter::getAdjustedLocation (SourceLocation Loc) const {
5249
- // TODO: Actually adjust this.
5250
- return Loc;
5307
+ if (Loc.isInvalid ())
5308
+ return Loc;
5309
+ return Loc.getLocWithOffset (-getAdjustment (Loc.getOffset ()));
5251
5310
}
5252
5311
5253
5312
SourceRange ASTWriter::getAdjustedRange (SourceRange Range) const {
5254
- // TODO: Actually adjust this.
5255
- return Range;
5313
+ return SourceRange ( getAdjustedLocation (Range. getBegin ()),
5314
+ getAdjustedLocation ( Range. getEnd ())) ;
5256
5315
}
5257
5316
5258
5317
SourceLocation::UIntTy
5259
5318
ASTWriter::getAdjustedOffset (SourceLocation::UIntTy Offset) const {
5260
- // TODO: Actually adjust this.
5261
- return Offset;
5319
+ return Offset - getAdjustment (Offset);
5320
+ }
5321
+
5322
+ SourceLocation::UIntTy
5323
+ ASTWriter::getAdjustment (SourceLocation::UIntTy Offset) const {
5324
+ if (NonAffectingRanges.empty ())
5325
+ return 0 ;
5326
+
5327
+ if (PP->getSourceManager ().isLoadedOffset (Offset))
5328
+ return 0 ;
5329
+
5330
+ if (Offset > NonAffectingRanges.back ().getEnd ().getOffset ())
5331
+ return NonAffectingOffsetAdjustments.back ();
5332
+
5333
+ if (Offset < NonAffectingRanges.front ().getBegin ().getOffset ())
5334
+ return 0 ;
5335
+
5336
+ auto Contains = [](const SourceRange &Range, SourceLocation::UIntTy Offset) {
5337
+ return Range.getEnd ().getOffset () < Offset;
5338
+ };
5339
+
5340
+ auto It = llvm::lower_bound (NonAffectingRanges, Offset, Contains);
5341
+ unsigned Idx = std::distance (NonAffectingRanges.begin (), It);
5342
+ return NonAffectingOffsetAdjustments[Idx];
5262
5343
}
5263
5344
5264
5345
void ASTWriter::AddFileID (FileID FID, RecordDataImpl &Record) {
@@ -5521,6 +5602,7 @@ void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
5521
5602
if (FID.isInvalid ())
5522
5603
return ;
5523
5604
assert (SM.getSLocEntry (FID).isFile ());
5605
+ assert (IsSLocAffecting[FID.ID ]);
5524
5606
5525
5607
std::unique_ptr<DeclIDInFileInfo> &Info = FileDeclIDs[FID];
5526
5608
if (!Info)
0 commit comments