Skip to content

Commit accf2a2

Browse files
authored
Merge pull request #5091 from akyrtzi/pr/stable-picks-pch-out-independent
Cherry-picks from upstream llvm.org that make PCH files output path independent
2 parents 5babde6 + e1d22f5 commit accf2a2

File tree

7 files changed

+16
-86
lines changed

7 files changed

+16
-86
lines changed

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: 2 additions & 3 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, const std::string &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,6 @@ class ASTWriter : public ASTDeserializationListener,
531531
void WriteDecl(ASTContext &Context, Decl *D);
532532

533533
ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot,
534-
const std::string &OutputFile,
535534
Module *WritingModule);
536535

537536
public:
@@ -569,7 +568,7 @@ class ASTWriter : public ASTDeserializationListener,
569568
///
570569
/// \return the module signature, which eventually will be a hash of
571570
/// the module but currently is merely a random 32-bit number.
572-
ASTFileSignature WriteAST(Sema &SemaRef, const std::string &OutputFile,
571+
ASTFileSignature WriteAST(Sema &SemaRef, StringRef OutputFile,
573572
Module *WritingModule, StringRef isysroot,
574573
bool hasErrors = false,
575574
bool ShouldCacheASTInMemory = false);

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/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,8 +1231,7 @@ ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
12321231

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

12391237
Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
@@ -1483,22 +1481,6 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
14831481
Record.push_back(SM.getMainFileID().getOpaqueValue());
14841482
Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
14851483

1486-
// Original PCH directory
1487-
if (!OutputFile.empty() && OutputFile != "-") {
1488-
auto Abbrev = std::make_shared<BitCodeAbbrev>();
1489-
Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1490-
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1491-
unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1492-
1493-
SmallString<128> OutputPath(OutputFile);
1494-
1495-
SM.getFileManager().makeAbsolutePath(OutputPath);
1496-
StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1497-
1498-
RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
1499-
Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1500-
}
1501-
15021484
std::set<const FileEntry *> AffectingModuleMaps;
15031485
if (WritingModule) {
15041486
AffectingModuleMaps =
@@ -4471,8 +4453,7 @@ time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
44714453
return IncludeTimestamps ? E->getModificationTime() : 0;
44724454
}
44734455

4474-
ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
4475-
const std::string &OutputFile,
4456+
ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef, StringRef OutputFile,
44764457
Module *WritingModule, StringRef isysroot,
44774458
bool hasErrors,
44784459
bool ShouldCacheASTInMemory) {
@@ -4491,8 +4472,7 @@ ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
44914472
Context = &SemaRef.Context;
44924473
PP = &SemaRef.PP;
44934474
this->WritingModule = WritingModule;
4494-
ASTFileSignature Signature =
4495-
WriteASTCore(SemaRef, isysroot, OutputFile, 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-
const std::string &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();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11

2+
// RUN: rm -rf %t
23
// RUN: %clang_cc1 -std=c++20 -fmodules -fmodules-cache-path=%t -fimplicit-module-maps -I%S/Inputs -verify %s
34
export import dummy; // expected-error {{export declaration can only be used within a module interface unit after the module declaration}}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: rm -rf %t && mkdir -p %t/a %t/b
2+
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
5+
6+
// RUN: diff %t/a/t1.pch %t/b/t2.pch

0 commit comments

Comments
 (0)