It's useful to be able to load explicitly-built PCH files into an implicit build (e.g. during dependency scanning). That's currently impossible, since the explicitly-built PCH has an empty modules cache path, while the current compilation has (and needs to have) a valid path, triggering an error in the `PCHValidator`.
This patch adds a preprocessor option and command-line flag that can be used to omit this check.
Reviewed By: dexonsmith
Differential Revision: https://reviews.llvm.org/D103802
HelpText<"Accept a PCH file that was created with compiler errors">,
MarshallingInfoFlag<PreprocessorOpts<"AllowPCHWithCompilerErrors">>,
ImpliedByAnyOf<[fallow_pcm_with_errors.KeyPath]>;
+def fallow_pch_with_different_modules_cache_path :
+ Flag<["-"], "fallow-pch-with-different-modules-cache-path">,
+ HelpText<"Accept a PCH file that was created with a different modules cache path">,
+ MarshallingInfoFlag<PreprocessorOpts<"AllowPCHWithDifferentModulesCachePath">>;
def dump_deserialized_pch_decls : Flag<["-"], "dump-deserialized-decls">,
HelpText<"Dump declarations that are deserialized from PCH, for testing">,
MarshallingInfoFlag<PreprocessorOpts<"DumpDeserializedPCHDecls">>;
/// When true, a PCH with compiler errors will not be rejected.
bool AllowPCHWithCompilerErrors = false;
+ /// When true, a PCH with modules cache path different to the current
+ /// compilation will not be rejected.
+ bool AllowPCHWithDifferentModulesCachePath = false;
+
/// Dump declarations that are deserialized from PCH, for testing.
bool DumpDeserializedPCHDecls = false;
StringRef SpecificModuleCachePath,
StringRef ExistingModuleCachePath,
DiagnosticsEngine *Diags,
- const LangOptions &LangOpts) {
+ const LangOptions &LangOpts,
+ const PreprocessorOptions &PPOpts) {
if (LangOpts.Modules) {
- if (SpecificModuleCachePath != ExistingModuleCachePath) {
+ if (SpecificModuleCachePath != ExistingModuleCachePath &&
+ !PPOpts.AllowPCHWithDifferentModulesCachePath) {
if (Diags)
Diags->Report(diag::err_pch_modulecache_mismatch)
<< SpecificModuleCachePath << ExistingModuleCachePath;
return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
PP.getHeaderSearchInfo().getModuleCachePath(),
Complain ? &Reader.Diags : nullptr,
- PP.getLangOpts());
+ PP.getLangOpts(), PP.getPreprocessorOpts());
}
void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
StringRef SpecificModuleCachePath,
bool Complain) override {
return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
- ExistingModuleCachePath,
- nullptr, ExistingLangOpts);
+ ExistingModuleCachePath, nullptr,
+ ExistingLangOpts, ExistingPPOpts);
}
bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
--- /dev/null
+typedef int pch_int;
--- /dev/null
+// RUN: rm -rf %t
+// RUN: mkdir %t
+//
+// RUN: %clang_cc1 -x objective-c-header -emit-pch %S/Inputs/pch-typedef.h -o %t/pch-typedef.h.gch \
+// RUN: -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/one
+//
+// RUN: not %clang_cc1 -x objective-c -fsyntax-only %s -include-pch %t/pch-typedef.h.gch \
+// RUN: -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/two 2>&1 \
+// RUN: | FileCheck %s -check-prefixes=CHECK-ERROR
+//
+// RUN: %clang_cc1 -x objective-c -fsyntax-only %s -include-pch %t/pch-typedef.h.gch \
+// RUN: -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/two 2>&1 -fallow-pch-with-different-modules-cache-path \
+// RUN: | FileCheck %s -check-prefixes=CHECK-SUCCESS -allow-empty
+
+pch_int x = 0;
+
+// CHECK-ERROR: PCH was compiled with module cache path '{{.*}}', but the path is currently '{{.*}}'
+// CHECK-SUCCESS-NOT: PCH was compiled with module cache path '{{.*}}', but the path is currently '{{.*}}'