@@ -161,8 +161,8 @@ static TypeCode getTypeCodeForTypeClass(Type::TypeClass id) {
161
161
162
162
namespace {
163
163
164
- std::set<const FileEntry *> GetAllModuleMaps (const HeaderSearch &HS,
165
- Module *RootModule) {
164
+ std::set<const FileEntry *> GetAffectingModuleMaps (const HeaderSearch &HS,
165
+ Module *RootModule) {
166
166
std::set<const FileEntry *> ModuleMaps{};
167
167
std::set<const Module *> ProcessedModules;
168
168
SmallVector<const Module *> ModulesToProcess{RootModule};
@@ -1477,15 +1477,8 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1477
1477
AddFileID (SM.getMainFileID (), Record);
1478
1478
Stream.EmitRecord (ORIGINAL_FILE_ID, Record);
1479
1479
1480
- std::set<const FileEntry *> AffectingClangModuleMaps;
1481
- if (WritingModule) {
1482
- AffectingClangModuleMaps =
1483
- GetAllModuleMaps (PP.getHeaderSearchInfo (), WritingModule);
1484
- }
1485
-
1486
1480
WriteInputFiles (Context.SourceMgr ,
1487
- PP.getHeaderSearchInfo ().getHeaderSearchOpts (),
1488
- AffectingClangModuleMaps);
1481
+ PP.getHeaderSearchInfo ().getHeaderSearchOpts ());
1489
1482
Stream.ExitBlock ();
1490
1483
}
1491
1484
@@ -1503,9 +1496,8 @@ struct InputFileEntry {
1503
1496
1504
1497
} // namespace
1505
1498
1506
- void ASTWriter::WriteInputFiles (
1507
- SourceManager &SourceMgr, HeaderSearchOptions &HSOpts,
1508
- std::set<const FileEntry *> &AffectingClangModuleMaps) {
1499
+ void ASTWriter::WriteInputFiles (SourceManager &SourceMgr,
1500
+ HeaderSearchOptions &HSOpts) {
1509
1501
using namespace llvm ;
1510
1502
1511
1503
Stream.EnterSubblock (INPUT_FILES_BLOCK_ID, 4 );
@@ -1545,15 +1537,9 @@ void ASTWriter::WriteInputFiles(
1545
1537
if (!Cache->OrigEntry )
1546
1538
continue ;
1547
1539
1548
- if (isModuleMap (File.getFileCharacteristic ()) &&
1549
- !isSystem (File.getFileCharacteristic ()) &&
1550
- !AffectingClangModuleMaps.empty () &&
1551
- AffectingClangModuleMaps.find (Cache->OrigEntry ) ==
1552
- AffectingClangModuleMaps.end ()) {
1553
- SkippedModuleMaps.insert (Cache->OrigEntry );
1554
- // Do not emit modulemaps that do not affect current module.
1540
+ // Do not emit input files that do not affect current module.
1541
+ if (!IsSLocAffecting[I])
1555
1542
continue ;
1556
- }
1557
1543
1558
1544
InputFileEntry Entry;
1559
1545
Entry.File = Cache->OrigEntry ;
@@ -2064,12 +2050,9 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
2064
2050
if (SLoc->isFile ()) {
2065
2051
const SrcMgr::FileInfo &File = SLoc->getFile ();
2066
2052
const SrcMgr::ContentCache *Content = &File.getContentCache ();
2067
- if (Content->OrigEntry && !SkippedModuleMaps.empty () &&
2068
- SkippedModuleMaps.find (Content->OrigEntry ) !=
2069
- SkippedModuleMaps.end ()) {
2070
- // Do not emit files that were not listed as inputs.
2053
+ // Do not emit files that were not listed as inputs.
2054
+ if (!IsSLocAffecting[I])
2071
2055
continue ;
2072
- }
2073
2056
SLocEntryOffsets.push_back (Offset);
2074
2057
// Starting offset of this entry within this module, so skip the dummy.
2075
2058
Record.push_back (getAdjustedOffset (SLoc->getOffset ()) - 2 );
@@ -4527,6 +4510,69 @@ static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4527
4510
}
4528
4511
}
4529
4512
4513
+ void ASTWriter::collectNonAffectingInputFiles () {
4514
+ SourceManager &SrcMgr = PP->getSourceManager ();
4515
+ unsigned N = SrcMgr.local_sloc_entry_size ();
4516
+
4517
+ IsSLocAffecting.resize (N, true );
4518
+
4519
+ if (!WritingModule)
4520
+ return ;
4521
+
4522
+ auto AffectingModuleMaps =
4523
+ GetAffectingModuleMaps (PP->getHeaderSearchInfo (), WritingModule);
4524
+
4525
+ unsigned FileIDAdjustment = 0 ;
4526
+ unsigned OffsetAdjustment = 0 ;
4527
+
4528
+ NonAffectingFileIDAdjustments.reserve (N);
4529
+ NonAffectingOffsetAdjustments.reserve (N);
4530
+
4531
+ NonAffectingFileIDAdjustments.push_back (FileIDAdjustment);
4532
+ NonAffectingOffsetAdjustments.push_back (OffsetAdjustment);
4533
+
4534
+ for (unsigned I = 1 ; I != N; ++I) {
4535
+ const SrcMgr::SLocEntry *SLoc = &SrcMgr.getLocalSLocEntry (I);
4536
+ FileID FID = FileID::get (I);
4537
+ assert (&SrcMgr.getSLocEntry (FID) == SLoc);
4538
+
4539
+ if (!SLoc->isFile ())
4540
+ continue ;
4541
+ const SrcMgr::FileInfo &File = SLoc->getFile ();
4542
+ const SrcMgr::ContentCache *Cache = &File.getContentCache ();
4543
+ if (!Cache->OrigEntry )
4544
+ continue ;
4545
+
4546
+ if (!isModuleMap (File.getFileCharacteristic ()) ||
4547
+ isSystem (File.getFileCharacteristic ()) || AffectingModuleMaps.empty () ||
4548
+ AffectingModuleMaps.find (Cache->OrigEntry ) != AffectingModuleMaps.end ())
4549
+ continue ;
4550
+
4551
+ IsSLocAffecting[I] = false ;
4552
+
4553
+ FileIDAdjustment += 1 ;
4554
+ // Even empty files take up one element in the offset table.
4555
+ OffsetAdjustment += SrcMgr.getFileIDSize (FID) + 1 ;
4556
+
4557
+ // If the previous file was non-affecting as well, just extend its entry
4558
+ // with our information.
4559
+ if (!NonAffectingFileIDs.empty () &&
4560
+ NonAffectingFileIDs.back ().ID == FID.ID - 1 ) {
4561
+ NonAffectingFileIDs.back () = FID;
4562
+ NonAffectingRanges.back ().setEnd (SrcMgr.getLocForEndOfFile (FID));
4563
+ NonAffectingFileIDAdjustments.back () = FileIDAdjustment;
4564
+ NonAffectingOffsetAdjustments.back () = OffsetAdjustment;
4565
+ continue ;
4566
+ }
4567
+
4568
+ NonAffectingFileIDs.push_back (FID);
4569
+ NonAffectingRanges.emplace_back (SrcMgr.getLocForStartOfFile (FID),
4570
+ SrcMgr.getLocForEndOfFile (FID));
4571
+ NonAffectingFileIDAdjustments.push_back (FileIDAdjustment);
4572
+ NonAffectingOffsetAdjustments.push_back (OffsetAdjustment);
4573
+ }
4574
+ }
4575
+
4530
4576
ASTFileSignature ASTWriter::WriteASTCore (Sema &SemaRef, StringRef isysroot,
4531
4577
Module *WritingModule) {
4532
4578
using namespace llvm ;
@@ -4540,6 +4586,8 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4540
4586
ASTContext &Context = SemaRef.Context ;
4541
4587
Preprocessor &PP = SemaRef.PP ;
4542
4588
4589
+ collectNonAffectingInputFiles ();
4590
+
4543
4591
// Set up predefined declaration IDs.
4544
4592
auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4545
4593
if (D) {
@@ -5229,32 +5277,65 @@ void ASTWriter::AddAlignPackInfo(const Sema::AlignPackInfo &Info,
5229
5277
}
5230
5278
5231
5279
FileID ASTWriter::getAdjustedFileID (FileID FID) const {
5232
- // TODO: Actually adjust this.
5233
- return FID;
5280
+ if (FID.isInvalid () || PP->getSourceManager ().isLoadedFileID (FID) ||
5281
+ NonAffectingFileIDs.empty ())
5282
+ return FID;
5283
+ auto It = llvm::lower_bound (NonAffectingFileIDs, FID);
5284
+ unsigned Idx = std::distance (NonAffectingFileIDs.begin (), It);
5285
+ unsigned Offset = NonAffectingFileIDAdjustments[Idx];
5286
+ return FileID::get (FID.getOpaqueValue () - Offset);
5234
5287
}
5235
5288
5236
5289
unsigned ASTWriter::getAdjustedNumCreatedFIDs (FileID FID) const {
5237
- // TODO: Actually adjust this.
5238
- return PP->getSourceManager ()
5239
- .getLocalSLocEntry (FID.ID )
5240
- .getFile ()
5241
- .NumCreatedFIDs ;
5290
+ unsigned NumCreatedFIDs = PP->getSourceManager ()
5291
+ .getLocalSLocEntry (FID.ID )
5292
+ .getFile ()
5293
+ .NumCreatedFIDs ;
5294
+
5295
+ unsigned AdjustedNumCreatedFIDs = 0 ;
5296
+ for (unsigned I = FID.ID , N = I + NumCreatedFIDs; I != N; ++I)
5297
+ if (IsSLocAffecting[I])
5298
+ ++AdjustedNumCreatedFIDs;
5299
+ return AdjustedNumCreatedFIDs;
5242
5300
}
5243
5301
5244
5302
SourceLocation ASTWriter::getAdjustedLocation (SourceLocation Loc) const {
5245
- // TODO: Actually adjust this.
5246
- return Loc;
5303
+ if (Loc.isInvalid ())
5304
+ return Loc;
5305
+ return Loc.getLocWithOffset (-getAdjustment (Loc.getOffset ()));
5247
5306
}
5248
5307
5249
5308
SourceRange ASTWriter::getAdjustedRange (SourceRange Range) const {
5250
- // TODO: Actually adjust this.
5251
- return Range;
5309
+ return SourceRange ( getAdjustedLocation (Range. getBegin ()),
5310
+ getAdjustedLocation ( Range. getEnd ())) ;
5252
5311
}
5253
5312
5254
5313
SourceLocation::UIntTy
5255
5314
ASTWriter::getAdjustedOffset (SourceLocation::UIntTy Offset) const {
5256
- // TODO: Actually adjust this.
5257
- return Offset;
5315
+ return Offset - getAdjustment (Offset);
5316
+ }
5317
+
5318
+ SourceLocation::UIntTy
5319
+ ASTWriter::getAdjustment (SourceLocation::UIntTy Offset) const {
5320
+ if (NonAffectingRanges.empty ())
5321
+ return 0 ;
5322
+
5323
+ if (PP->getSourceManager ().isLoadedOffset (Offset))
5324
+ return 0 ;
5325
+
5326
+ if (Offset > NonAffectingRanges.back ().getEnd ().getOffset ())
5327
+ return NonAffectingOffsetAdjustments.back ();
5328
+
5329
+ if (Offset < NonAffectingRanges.front ().getBegin ().getOffset ())
5330
+ return 0 ;
5331
+
5332
+ auto Contains = [](const SourceRange &Range, SourceLocation::UIntTy Offset) {
5333
+ return Range.getEnd ().getOffset () < Offset;
5334
+ };
5335
+
5336
+ auto It = llvm::lower_bound (NonAffectingRanges, Offset, Contains);
5337
+ unsigned Idx = std::distance (NonAffectingRanges.begin (), It);
5338
+ return NonAffectingOffsetAdjustments[Idx];
5258
5339
}
5259
5340
5260
5341
void ASTWriter::AddFileID (FileID FID, RecordDataImpl &Record) {
@@ -5517,6 +5598,7 @@ void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
5517
5598
if (FID.isInvalid ())
5518
5599
return ;
5519
5600
assert (SM.getSLocEntry (FID).isFile ());
5601
+ assert (IsSLocAffecting[FID.ID ]);
5520
5602
5521
5603
std::unique_ptr<DeclIDInFileInfo> &Info = FileDeclIDs[FID];
5522
5604
if (!Info)
0 commit comments