[clang] 78668c8 - [clang][modules][pch] Allow loading PCH with different modules cache path

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 14 02:05:03 PDT 2021


Author: Jan Svoboda
Date: 2021-06-14T11:04:56+02:00
New Revision: 78668c822af9504f77a554f5924e1097365d9c33

URL: https://github.com/llvm/llvm-project/commit/78668c822af9504f77a554f5924e1097365d9c33
DIFF: https://github.com/llvm/llvm-project/commit/78668c822af9504f77a554f5924e1097365d9c33.diff

LOG: [clang][modules][pch] Allow loading PCH with different modules cache path

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

Added: 
    clang/test/Modules/Inputs/pch-typedef.h
    clang/test/Modules/module-pch-different-cache-path.c

Modified: 
    clang/include/clang/Driver/Options.td
    clang/include/clang/Lex/PreprocessorOptions.h
    clang/lib/Serialization/ASTReader.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 3968aa3431ea..329caae5f9fe 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5499,6 +5499,10 @@ def fallow_pch_with_errors : Flag<["-"], "fallow-pch-with-compiler-errors">,
   HelpText<"Accept a PCH file that was created with compiler errors">,
   MarshallingInfoFlag<PreprocessorOpts<"AllowPCHWithCompilerErrors">>,
   ImpliedByAnyOf<[fallow_pcm_with_errors.KeyPath]>;
+def fallow_pch_with_
diff erent_modules_cache_path :
+  Flag<["-"], "fallow-pch-with-
diff erent-modules-cache-path">,
+  HelpText<"Accept a PCH file that was created with a 
diff erent 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">>;

diff  --git a/clang/include/clang/Lex/PreprocessorOptions.h b/clang/include/clang/Lex/PreprocessorOptions.h
index 7f024989bf9b..99085b98fc7a 100644
--- a/clang/include/clang/Lex/PreprocessorOptions.h
+++ b/clang/include/clang/Lex/PreprocessorOptions.h
@@ -106,6 +106,10 @@ class PreprocessorOptions {
   /// When true, a PCH with compiler errors will not be rejected.
   bool AllowPCHWithCompilerErrors = false;
 
+  /// When true, a PCH with modules cache path 
diff erent to the current
+  /// compilation will not be rejected.
+  bool AllowPCHWithDifferentModulesCachePath = false;
+
   /// Dump declarations that are deserialized from PCH, for testing.
   bool DumpDeserializedPCHDecls = false;
 

diff  --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp
index 55e0d084ea4b..80ee56a9b7df 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -783,9 +783,11 @@ static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
                                      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;
@@ -802,7 +804,7 @@ bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
   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) {
@@ -5142,8 +5144,8 @@ namespace {
                                  StringRef SpecificModuleCachePath,
                                  bool Complain) override {
       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
-                                      ExistingModuleCachePath,
-                                      nullptr, ExistingLangOpts);
+                                      ExistingModuleCachePath, nullptr,
+                                      ExistingLangOpts, ExistingPPOpts);
     }
 
     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,

diff  --git a/clang/test/Modules/Inputs/pch-typedef.h b/clang/test/Modules/Inputs/pch-typedef.h
new file mode 100644
index 000000000000..f2b30f34a5b3
--- /dev/null
+++ b/clang/test/Modules/Inputs/pch-typedef.h
@@ -0,0 +1 @@
+typedef int pch_int;

diff  --git a/clang/test/Modules/module-pch-
diff erent-cache-path.c b/clang/test/Modules/module-pch-
diff erent-cache-path.c
new file mode 100644
index 000000000000..8778adc886f7
--- /dev/null
+++ b/clang/test/Modules/module-pch-
diff erent-cache-path.c
@@ -0,0 +1,18 @@
+// 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-
diff erent-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 '{{.*}}'


        


More information about the cfe-commits mailing list