Skip to content

Commit 6635f48

Browse files
committed
[Serialization] Remove ORIGINAL_PCH_DIR record
Use of `ORIGINAL_PCH_DIR` record has been superseeded by making PCH/PCM files with relocatable paths at write time. Removing this record is useful for producing an output-path-independent PCH file and enable sharing of the same PCH file even when it was intended for a different output path. Differential Revision: https://reviews.llvm.org/D131124
1 parent 9ec4ddd commit 6635f48

File tree

11 files changed

+20
-122
lines changed

11 files changed

+20
-122
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6018,9 +6018,6 @@ def fno_validate_pch : Flag<["-"], "fno-validate-pch">,
60186018
HelpText<"Disable validation of precompiled headers">,
60196019
MarshallingInfoFlag<PreprocessorOpts<"DisablePCHOrModuleValidation">, "DisableValidationForModuleKind::None">,
60206020
Normalizer<"makeFlagToValueNormalizer(DisableValidationForModuleKind::All)">;
6021-
def fpcm_output_path_independent : Flag<["-"], "fpcm-output-path-independent">,
6022-
HelpText<"Output a PCM/PCH file that does not write out information about its output path">,
6023-
MarshallingInfoFlag<FrontendOpts<"OutputPathIndependentPCM">>;
60246021
def fallow_pcm_with_errors : Flag<["-"], "fallow-pcm-with-compiler-errors">,
60256022
HelpText<"Accept a PCM file that was created with compiler errors">,
60266023
MarshallingInfoFlag<FrontendOpts<"AllowPCMWithCompilerErrors">>;

clang/include/clang/Frontend/FrontendOptions.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,6 @@ class FrontendOptions {
344344
/// When using -emit-module, treat the modulemap as a system module.
345345
unsigned IsSystemModule : 1;
346346

347-
/// Output a PCM/PCH file that does not write out information about its output
348-
/// path.
349-
///
350-
/// FIXME: This only controls whether \p ORIGINAL_PCH_DIR record is written
351-
/// out or not. Consider either removing that record entirely if it's no
352-
/// longer relevant or switching the default to not write it unless an option
353-
/// is set to true.
354-
unsigned OutputPathIndependentPCM : 1;
355-
356347
/// Output (and read) PCM files regardless of compiler errors.
357348
unsigned AllowPCMWithCompilerErrors : 1;
358349

@@ -525,8 +516,8 @@ class FrontendOptions {
525516
ASTDumpLookups(false), BuildingImplicitModule(false),
526517
BuildingImplicitModuleUsesLock(true), ModulesEmbedAllFiles(false),
527518
IncludeTimestamps(true), UseTemporary(true),
528-
OutputPathIndependentPCM(false), AllowPCMWithCompilerErrors(false),
529-
ModulesShareFileManager(true), TimeTraceGranularity(500) {}
519+
AllowPCMWithCompilerErrors(false), ModulesShareFileManager(true),
520+
TimeTraceGranularity(500) {}
530521

531522
/// getInputKindForExtension - Return the appropriate input kind for a file
532523
/// extension. For example, "c" would return Language::C.

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,6 @@ enum ControlRecordTypes {
343343
/// name.
344344
ORIGINAL_FILE,
345345

346-
/// The directory that the PCH was originally created in.
347-
ORIGINAL_PCH_DIR,
348-
349346
/// Record code for file ID of the file or buffer that was used to
350347
/// generate the AST file.
351348
ORIGINAL_FILE_ID,

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ class ASTWriter : public ASTDeserializationListener,
455455

456456
void WriteBlockInfoBlock();
457457
void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
458-
StringRef isysroot, StringRef OutputFile);
458+
StringRef isysroot);
459459

460460
/// Write out the signature and diagnostic options, and return the signature.
461461
ASTFileSignature writeUnhashedControlBlock(Preprocessor &PP,
@@ -533,7 +533,7 @@ class ASTWriter : public ASTDeserializationListener,
533533
void WriteDecl(ASTContext &Context, Decl *D);
534534

535535
ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot,
536-
StringRef OutputFile, Module *WritingModule);
536+
Module *WritingModule);
537537

538538
public:
539539
/// Create a new precompiled header writer that outputs to
@@ -573,8 +573,7 @@ class ASTWriter : public ASTDeserializationListener,
573573
ASTFileSignature WriteAST(Sema &SemaRef, StringRef OutputFile,
574574
Module *WritingModule, StringRef isysroot,
575575
bool hasErrors = false,
576-
bool ShouldCacheASTInMemory = false,
577-
bool OutputPathIndependent = false);
576+
bool ShouldCacheASTInMemory = false);
578577

579578
/// Emit a token.
580579
void AddToken(const Token &Tok, RecordDataImpl &Record);
@@ -765,7 +764,6 @@ class PCHGenerator : public SemaConsumer {
765764
ASTWriter Writer;
766765
bool AllowASTWithErrors;
767766
bool ShouldCacheASTInMemory;
768-
bool OutputPathIndependent;
769767

770768
protected:
771769
ASTWriter &getWriter() { return Writer; }
@@ -778,8 +776,7 @@ class PCHGenerator : public SemaConsumer {
778776
std::shared_ptr<PCHBuffer> Buffer,
779777
ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
780778
bool AllowASTWithErrors = false, bool IncludeTimestamps = true,
781-
bool ShouldCacheASTInMemory = false,
782-
bool OutputPathIndependent = false);
779+
bool ShouldCacheASTInMemory = false);
783780
~PCHGenerator() override;
784781

785782
void InitializeSema(Sema &S) override { SemaPtr = &S; }

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,6 @@ class ModuleFile {
148148
/// build this AST file.
149149
FileID OriginalSourceFileID;
150150

151-
/// The directory that the PCH was originally created in. Used to
152-
/// allow resolving headers even after headers+PCH was moved to a new path.
153-
std::string OriginalDir;
154-
155151
std::string ModuleMapPath;
156152

157153
/// Whether this precompiled header is a relocatable PCH file.

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
140140
CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer,
141141
FrontendOpts.ModuleFileExtensions,
142142
CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
143-
FrontendOpts.IncludeTimestamps, +CI.getLangOpts().CacheGeneratedPCH,
144-
FrontendOpts.OutputPathIndependentPCM));
143+
FrontendOpts.IncludeTimestamps, +CI.getLangOpts().CacheGeneratedPCH));
145144
Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
146145
CI, std::string(InFile), OutputFile, std::move(OS), Buffer));
147146

@@ -204,9 +203,7 @@ GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
204203
/*IncludeTimestamps=*/
205204
+CI.getFrontendOpts().BuildingImplicitModule,
206205
/*ShouldCacheASTInMemory=*/
207-
+CI.getFrontendOpts().BuildingImplicitModule,
208-
/*OutputPathIndependent=*/
209-
+CI.getFrontendOpts().OutputPathIndependentPCM));
206+
+CI.getFrontendOpts().BuildingImplicitModule));
210207
Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
211208
CI, std::string(InFile), OutputFile, std::move(OS), Buffer));
212209
return std::make_unique<MultiplexConsumer>(std::move(Consumers));

clang/lib/Serialization/ASTReader.cpp

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,41 +1395,6 @@ llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
13951395
}
13961396
}
13971397

1398-
/// If a header file is not found at the path that we expect it to be
1399-
/// and the PCH file was moved from its original location, try to resolve the
1400-
/// file by assuming that header+PCH were moved together and the header is in
1401-
/// the same place relative to the PCH.
1402-
static std::string
1403-
resolveFileRelativeToOriginalDir(const std::string &Filename,
1404-
const std::string &OriginalDir,
1405-
const std::string &CurrDir) {
1406-
assert(OriginalDir != CurrDir &&
1407-
"No point trying to resolve the file if the PCH dir didn't change");
1408-
1409-
using namespace llvm::sys;
1410-
1411-
SmallString<128> filePath(Filename);
1412-
fs::make_absolute(filePath);
1413-
assert(path::is_absolute(OriginalDir));
1414-
SmallString<128> currPCHPath(CurrDir);
1415-
1416-
path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1417-
fileDirE = path::end(path::parent_path(filePath));
1418-
path::const_iterator origDirI = path::begin(OriginalDir),
1419-
origDirE = path::end(OriginalDir);
1420-
// Skip the common path components from filePath and OriginalDir.
1421-
while (fileDirI != fileDirE && origDirI != origDirE &&
1422-
*fileDirI == *origDirI) {
1423-
++fileDirI;
1424-
++origDirI;
1425-
}
1426-
for (; origDirI != origDirE; ++origDirI)
1427-
path::append(currPCHPath, "..");
1428-
path::append(currPCHPath, fileDirI, fileDirE);
1429-
path::append(currPCHPath, path::filename(Filename));
1430-
return std::string(currPCHPath.str());
1431-
}
1432-
14331398
bool ASTReader::ReadSLocEntry(int ID) {
14341399
if (ID == 0)
14351400
return false;
@@ -2332,16 +2297,6 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
23322297
OptionalFileEntryRefDegradesToFileEntryPtr File =
23332298
expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false));
23342299

2335-
// If we didn't find the file, resolve it relative to the
2336-
// original directory from which this AST file was created.
2337-
if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2338-
F.OriginalDir != F.BaseDirectory) {
2339-
std::string Resolved = resolveFileRelativeToOriginalDir(
2340-
std::string(Filename), F.OriginalDir, F.BaseDirectory);
2341-
if (!Resolved.empty())
2342-
File = expectedToOptional(FileMgr.getFileRef(Resolved));
2343-
}
2344-
23452300
// For an overridden file, create a virtual file with the stored
23462301
// size/timestamp.
23472302
if ((Overridden || Transient) && !File)
@@ -2896,11 +2851,6 @@ ASTReader::ReadControlBlock(ModuleFile &F,
28962851
F.OriginalSourceFileID = FileID::get(Record[0]);
28972852
break;
28982853

2899-
case ORIGINAL_PCH_DIR:
2900-
F.OriginalDir = std::string(Blob);
2901-
ResolveImportedPath(F, F.OriginalDir);
2902-
break;
2903-
29042854
case MODULE_NAME:
29052855
F.ModuleName = std::string(Blob);
29062856
Diag(diag::remark_module_import)
@@ -8704,8 +8654,9 @@ ASTReader::getSourceDescriptor(unsigned ID) {
87048654
ModuleFile &MF = ModuleMgr.getPrimaryModule();
87058655
StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
87068656
StringRef FileName = llvm::sys::path::filename(MF.FileName);
8707-
return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8708-
MF.Signature);
8657+
return ASTSourceDescriptor(ModuleName,
8658+
llvm::sys::path::parent_path(MF.FileName),
8659+
FileName, MF.Signature);
87098660
}
87108661
return None;
87118662
}

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,6 @@ void ASTWriter::WriteBlockInfoBlock() {
786786
RECORD(MODULE_MAP_FILE);
787787
RECORD(IMPORTS);
788788
RECORD(ORIGINAL_FILE);
789-
RECORD(ORIGINAL_PCH_DIR);
790789
RECORD(ORIGINAL_FILE_ID);
791790
RECORD(INPUT_FILE_OFFSETS);
792791

@@ -1187,7 +1186,7 @@ ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
11871186

11881187
/// Write the control block.
11891188
void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1190-
StringRef isysroot, StringRef OutputFile) {
1189+
StringRef isysroot) {
11911190
using namespace llvm;
11921191

11931192
Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
@@ -1470,21 +1469,6 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
14701469
Record.push_back(SM.getMainFileID().getOpaqueValue());
14711470
Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
14721471

1473-
// Original PCH directory
1474-
if (!OutputFile.empty() && OutputFile != "-") {
1475-
auto Abbrev = std::make_shared<BitCodeAbbrev>();
1476-
Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1477-
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1478-
unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1479-
1480-
SmallString<128> OutputPath(OutputFile);
1481-
PreparePathForOutput(OutputPath);
1482-
StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1483-
1484-
RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
1485-
Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1486-
}
1487-
14881472
std::set<const FileEntry *> AffectingModuleMaps;
14891473
if (WritingModule) {
14901474
AffectingModuleMaps =
@@ -4483,8 +4467,7 @@ time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
44834467
ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef, StringRef OutputFile,
44844468
Module *WritingModule, StringRef isysroot,
44854469
bool hasErrors,
4486-
bool ShouldCacheASTInMemory,
4487-
bool OutputPathIndependent) {
4470+
bool ShouldCacheASTInMemory) {
44884471
WritingAST = true;
44894472

44904473
ASTHasCompilerErrors = hasErrors;
@@ -4500,9 +4483,7 @@ ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef, StringRef OutputFile,
45004483
Context = &SemaRef.Context;
45014484
PP = &SemaRef.PP;
45024485
this->WritingModule = WritingModule;
4503-
ASTFileSignature Signature = WriteASTCore(
4504-
SemaRef, isysroot, OutputPathIndependent ? StringRef() : OutputFile,
4505-
WritingModule);
4486+
ASTFileSignature Signature = WriteASTCore(SemaRef, isysroot, WritingModule);
45064487
Context = nullptr;
45074488
PP = nullptr;
45084489
this->WritingModule = nullptr;
@@ -4528,7 +4509,6 @@ static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
45284509
}
45294510

45304511
ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4531-
StringRef OutputFile,
45324512
Module *WritingModule) {
45334513
using namespace llvm;
45344514

@@ -4682,7 +4662,7 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
46824662
}
46834663

46844664
// Write the control block
4685-
WriteControlBlock(PP, Context, isysroot, OutputFile);
4665+
WriteControlBlock(PP, Context, isysroot);
46864666

46874667
// Write the remaining AST contents.
46884668
Stream.FlushToWord();

clang/lib/Serialization/GeneratePCH.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ PCHGenerator::PCHGenerator(
2525
StringRef OutputFile, StringRef isysroot, std::shared_ptr<PCHBuffer> Buffer,
2626
ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
2727
bool AllowASTWithErrors, bool IncludeTimestamps,
28-
bool ShouldCacheASTInMemory, bool OutputPathIndependent)
28+
bool ShouldCacheASTInMemory)
2929
: PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()),
3030
SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
3131
Writer(Stream, this->Buffer->Data, ModuleCache, Extensions,
3232
IncludeTimestamps),
3333
AllowASTWithErrors(AllowASTWithErrors),
34-
ShouldCacheASTInMemory(ShouldCacheASTInMemory),
35-
OutputPathIndependent(OutputPathIndependent) {
34+
ShouldCacheASTInMemory(ShouldCacheASTInMemory) {
3635
this->Buffer->IsComplete = false;
3736
}
3837

@@ -71,7 +70,7 @@ void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
7170
// For serialization we are lenient if the errors were
7271
// only warn-as-error kind.
7372
PP.getDiagnostics().hasUncompilableErrorOccurred(),
74-
ShouldCacheASTInMemory, OutputPathIndependent);
73+
ShouldCacheASTInMemory);
7574

7675
Buffer->IsComplete = true;
7776
}

clang/test/Modules/relative-original-dir.m

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: rm -rf %t && mkdir -p %t/a %t/b
22

3-
// RUN: %clang_cc1 -triple x86_64-apple-macos11 -emit-pch %s -o %t/a/t1.pch -fpcm-output-path-independent
4-
// RUN: %clang_cc1 -triple x86_64-apple-macos11 -emit-pch %s -o %t/b/t2.pch -fpcm-output-path-independent
3+
// RUN: %clang_cc1 -triple x86_64-apple-macos11 -emit-pch %s -o %t/a/t1.pch
4+
// RUN: %clang_cc1 -triple x86_64-apple-macos11 -emit-pch %s -o %t/b/t2.pch
55

66
// RUN: diff %t/a/t1.pch %t/b/t2.pch

0 commit comments

Comments
 (0)