Skip to content

Commit 15dcf90

Browse files
committed
[ASTWriter] Provide capability to output a PCM/PCH file that does not write out information about its output path
This is useful to enable sharing of the same PCH file even when it's intended for a different output path. The only information this option disables writing is for `ORIGINAL_PCH_DIR` record which is treated as optional and (when present) used as fallback for resolving input file paths relative to it. Differential Revision: https://reviews.llvm.org/D130710
1 parent a8d28ad commit 15dcf90

File tree

7 files changed

+39
-11
lines changed

7 files changed

+39
-11
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5941,6 +5941,9 @@ 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">>;
59445947
def fallow_pcm_with_errors : Flag<["-"], "fallow-pcm-with-compiler-errors">,
59455948
HelpText<"Accept a PCM file that was created with compiler errors">,
59465949
MarshallingInfoFlag<FrontendOpts<"AllowPCMWithCompilerErrors">>;

clang/include/clang/Frontend/FrontendOptions.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,15 @@ 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+
356365
/// Output (and read) PCM files regardless of compiler errors.
357366
unsigned AllowPCMWithCompilerErrors : 1;
358367

@@ -522,7 +531,8 @@ class FrontendOptions {
522531
ASTDumpLookups(false), BuildingImplicitModule(false),
523532
BuildingImplicitModuleUsesLock(true), ModulesEmbedAllFiles(false),
524533
IncludeTimestamps(true), UseTemporary(true), CacheCompileJob(false),
525-
AllowPCMWithCompilerErrors(false), TimeTraceGranularity(500) {}
534+
OutputPathIndependentPCM(false), AllowPCMWithCompilerErrors(false),
535+
TimeTraceGranularity(500) {}
526536

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

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,8 @@ class ASTWriter : public ASTDeserializationListener,
571571
ASTFileSignature WriteAST(Sema &SemaRef, StringRef OutputFile,
572572
Module *WritingModule, StringRef isysroot,
573573
bool hasErrors = false,
574-
bool ShouldCacheASTInMemory = false);
574+
bool ShouldCacheASTInMemory = false,
575+
bool OutputPathIndependent = false);
575576

576577
/// Emit a token.
577578
void AddToken(const Token &Tok, RecordDataImpl &Record);
@@ -760,6 +761,7 @@ class PCHGenerator : public SemaConsumer {
760761
ASTWriter Writer;
761762
bool AllowASTWithErrors;
762763
bool ShouldCacheASTInMemory;
764+
bool OutputPathIndependent;
763765

764766
protected:
765767
ASTWriter &getWriter() { return Writer; }
@@ -772,7 +774,8 @@ class PCHGenerator : public SemaConsumer {
772774
std::shared_ptr<PCHBuffer> Buffer,
773775
ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
774776
bool AllowASTWithErrors = false, bool IncludeTimestamps = true,
775-
bool ShouldCacheASTInMemory = false);
777+
bool ShouldCacheASTInMemory = false,
778+
bool OutputPathIndependent = false);
776779
~PCHGenerator() override;
777780

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

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ 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));
143+
FrontendOpts.IncludeTimestamps, +CI.getLangOpts().CacheGeneratedPCH,
144+
FrontendOpts.OutputPathIndependentPCM));
144145
Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
145146
CI, std::string(InFile), OutputFile, std::move(OS), Buffer));
146147

@@ -203,7 +204,9 @@ GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
203204
/*IncludeTimestamps=*/
204205
+CI.getFrontendOpts().BuildingImplicitModule,
205206
/*ShouldCacheASTInMemory=*/
206-
+CI.getFrontendOpts().BuildingImplicitModule));
207+
+CI.getFrontendOpts().BuildingImplicitModule,
208+
/*OutputPathIndependent=*/
209+
+CI.getFrontendOpts().OutputPathIndependentPCM));
207210
Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
208211
CI, std::string(InFile), OutputFile, std::move(OS), Buffer));
209212
return std::make_unique<MultiplexConsumer>(std::move(Consumers));

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4473,7 +4473,8 @@ time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
44734473
ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef, StringRef OutputFile,
44744474
Module *WritingModule, StringRef isysroot,
44754475
bool hasErrors,
4476-
bool ShouldCacheASTInMemory) {
4476+
bool ShouldCacheASTInMemory,
4477+
bool OutputPathIndependent) {
44774478
WritingAST = true;
44784479

44794480
ASTHasCompilerErrors = hasErrors;
@@ -4489,8 +4490,9 @@ ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef, StringRef OutputFile,
44894490
Context = &SemaRef.Context;
44904491
PP = &SemaRef.PP;
44914492
this->WritingModule = WritingModule;
4492-
ASTFileSignature Signature =
4493-
WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
4493+
ASTFileSignature Signature = WriteASTCore(
4494+
SemaRef, isysroot, OutputPathIndependent ? StringRef() : OutputFile,
4495+
WritingModule);
44944496
Context = nullptr;
44954497
PP = nullptr;
44964498
this->WritingModule = nullptr;

clang/lib/Serialization/GeneratePCH.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ 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)
28+
bool ShouldCacheASTInMemory, bool OutputPathIndependent)
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) {
34+
ShouldCacheASTInMemory(ShouldCacheASTInMemory),
35+
OutputPathIndependent(OutputPathIndependent) {
3536
this->Buffer->IsComplete = false;
3637
}
3738

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

7576
Buffer->IsComplete = true;
7677
}
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 -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
5+
6+
// RUN: diff %t/a/t1.pch %t/b/t2.pch

0 commit comments

Comments
 (0)