Skip to content

Commit 949e3b0

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 15dcf90 commit 949e3b0

File tree

10 files changed

+19
-115
lines changed

10 files changed

+19
-115
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5941,9 +5941,6 @@ def fno_validate_pch : Flag<["-"], "fno-validate-pch">,
59415941
HelpText<"Disable validation of precompiled headers">,
59425942
MarshallingInfoFlag<PreprocessorOpts<"DisablePCHOrModuleValidation">, "DisableValidationForModuleKind::None">,
59435943
Normalizer<"makeFlagToValueNormalizer(DisableValidationForModuleKind::All)">;
5944-
def fpcm_output_path_independent : Flag<["-"], "fpcm-output-path-independent">,
5945-
HelpText<"Output a PCM/PCH file that does not write out information about its output path">,
5946-
MarshallingInfoFlag<FrontendOpts<"OutputPathIndependentPCM">>;
59475944
def fallow_pcm_with_errors : Flag<["-"], "fallow-pcm-with-compiler-errors">,
59485945
HelpText<"Accept a PCM file that was created with compiler errors">,
59495946
MarshallingInfoFlag<FrontendOpts<"AllowPCMWithCompilerErrors">>;

clang/include/clang/Frontend/FrontendOptions.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,6 @@ class FrontendOptions {
353353
/// is specified.
354354
unsigned CacheCompileJob : 1;
355355

356-
/// Output a PCM/PCH file that does not write out information about its output
357-
/// path.
358-
///
359-
/// FIXME: This only controls whether \p ORIGINAL_PCH_DIR record is written
360-
/// out or not. Consider either removing that record entirely if it's no
361-
/// longer relevant or switching the default to not write it unless an option
362-
/// is set to true.
363-
unsigned OutputPathIndependentPCM : 1;
364-
365356
/// Output (and read) PCM files regardless of compiler errors.
366357
unsigned AllowPCMWithCompilerErrors : 1;
367358

@@ -531,8 +522,7 @@ class FrontendOptions {
531522
ASTDumpLookups(false), BuildingImplicitModule(false),
532523
BuildingImplicitModuleUsesLock(true), ModulesEmbedAllFiles(false),
533524
IncludeTimestamps(true), UseTemporary(true), CacheCompileJob(false),
534-
OutputPathIndependentPCM(false), AllowPCMWithCompilerErrors(false),
535-
TimeTraceGranularity(500) {}
525+
AllowPCMWithCompilerErrors(false), TimeTraceGranularity(500) {}
536526

537527
/// getInputKindForExtension - Return the appropriate input kind for a file
538528
/// 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
@@ -452,7 +452,7 @@ class ASTWriter : public ASTDeserializationListener,
452452

453453
void WriteBlockInfoBlock();
454454
void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
455-
StringRef isysroot, StringRef OutputFile);
455+
StringRef isysroot);
456456

457457
/// Write out the signature and diagnostic options, and return the signature.
458458
ASTFileSignature writeUnhashedControlBlock(Preprocessor &PP,
@@ -531,7 +531,7 @@ class ASTWriter : public ASTDeserializationListener,
531531
void WriteDecl(ASTContext &Context, Decl *D);
532532

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

536536
public:
537537
/// Create a new precompiled header writer that outputs to
@@ -571,8 +571,7 @@ class ASTWriter : public ASTDeserializationListener,
571571
ASTFileSignature WriteAST(Sema &SemaRef, StringRef OutputFile,
572572
Module *WritingModule, StringRef isysroot,
573573
bool hasErrors = false,
574-
bool ShouldCacheASTInMemory = false,
575-
bool OutputPathIndependent = false);
574+
bool ShouldCacheASTInMemory = false);
576575

577576
/// Emit a token.
578577
void AddToken(const Token &Tok, RecordDataImpl &Record);
@@ -761,7 +760,6 @@ class PCHGenerator : public SemaConsumer {
761760
ASTWriter Writer;
762761
bool AllowASTWithErrors;
763762
bool ShouldCacheASTInMemory;
764-
bool OutputPathIndependent;
765763

766764
protected:
767765
ASTWriter &getWriter() { return Writer; }
@@ -774,8 +772,7 @@ class PCHGenerator : public SemaConsumer {
774772
std::shared_ptr<PCHBuffer> Buffer,
775773
ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
776774
bool AllowASTWithErrors = false, bool IncludeTimestamps = true,
777-
bool ShouldCacheASTInMemory = false,
778-
bool OutputPathIndependent = false);
775+
bool ShouldCacheASTInMemory = false);
779776
~PCHGenerator() override;
780777

781778
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 & 51 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)
@@ -2892,10 +2847,6 @@ ASTReader::ReadControlBlock(ModuleFile &F,
28922847
F.OriginalSourceFileID = FileID::get(Record[0]);
28932848
break;
28942849

2895-
case ORIGINAL_PCH_DIR:
2896-
F.OriginalDir = std::string(Blob);
2897-
break;
2898-
28992850
case MODULE_NAME:
29002851
F.ModuleName = std::string(Blob);
29012852
Diag(diag::remark_module_import)
@@ -8701,8 +8652,9 @@ ASTReader::getSourceDescriptor(unsigned ID) {
87018652
ModuleFile &MF = ModuleMgr.getPrimaryModule();
87028653
StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
87038654
StringRef FileName = llvm::sys::path::filename(MF.FileName);
8704-
return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8705-
MF.Signature);
8655+
return ASTSourceDescriptor(ModuleName,
8656+
llvm::sys::path::parent_path(MF.FileName),
8657+
FileName, MF.Signature);
87068658
}
87078659
return None;
87088660
}

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,6 @@ void ASTWriter::WriteBlockInfoBlock() {
801801
RECORD(MODULE_MAP_FILE);
802802
RECORD(IMPORTS);
803803
RECORD(ORIGINAL_FILE);
804-
RECORD(ORIGINAL_PCH_DIR);
805804
RECORD(ORIGINAL_FILE_ID);
806805
RECORD(INPUT_FILE_OFFSETS);
807806

@@ -1232,7 +1231,7 @@ ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
12321231

12331232
/// Write the control block.
12341233
void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1235-
StringRef isysroot, StringRef OutputFile) {
1234+
StringRef isysroot) {
12361235
using namespace llvm;
12371236

12381237
Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
@@ -1482,22 +1481,6 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
14821481
Record.push_back(SM.getMainFileID().getOpaqueValue());
14831482
Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
14841483

1485-
// Original PCH directory
1486-
if (!OutputFile.empty() && OutputFile != "-") {
1487-
auto Abbrev = std::make_shared<BitCodeAbbrev>();
1488-
Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1489-
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1490-
unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1491-
1492-
SmallString<128> OutputPath(OutputFile);
1493-
1494-
SM.getFileManager().makeAbsolutePath(OutputPath);
1495-
StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1496-
1497-
RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
1498-
Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1499-
}
1500-
15011484
std::set<const FileEntry *> AffectingModuleMaps;
15021485
if (WritingModule) {
15031486
AffectingModuleMaps =
@@ -4473,8 +4456,7 @@ time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
44734456
ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef, StringRef OutputFile,
44744457
Module *WritingModule, StringRef isysroot,
44754458
bool hasErrors,
4476-
bool ShouldCacheASTInMemory,
4477-
bool OutputPathIndependent) {
4459+
bool ShouldCacheASTInMemory) {
44784460
WritingAST = true;
44794461

44804462
ASTHasCompilerErrors = hasErrors;
@@ -4490,9 +4472,7 @@ ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef, StringRef OutputFile,
44904472
Context = &SemaRef.Context;
44914473
PP = &SemaRef.PP;
44924474
this->WritingModule = WritingModule;
4493-
ASTFileSignature Signature = WriteASTCore(
4494-
SemaRef, isysroot, OutputPathIndependent ? StringRef() : OutputFile,
4495-
WritingModule);
4475+
ASTFileSignature Signature = WriteASTCore(SemaRef, isysroot, WritingModule);
44964476
Context = nullptr;
44974477
PP = nullptr;
44984478
this->WritingModule = nullptr;
@@ -4518,7 +4498,6 @@ static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
45184498
}
45194499

45204500
ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4521-
StringRef OutputFile,
45224501
Module *WritingModule) {
45234502
using namespace llvm;
45244503

@@ -4672,7 +4651,7 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
46724651
}
46734652

46744653
// Write the control block
4675-
WriteControlBlock(PP, Context, isysroot, OutputFile);
4654+
WriteControlBlock(PP, Context, isysroot);
46764655

46774656
// Write the remaining AST contents.
46784657
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
}
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)