https://github.com/Bigcheese created 
https://github.com/llvm/llvm-project/pull/243634

Add a repeatable `-fimplicit-modules-validation-key=<key>` option. When using 
implicitly built modules this key is stored in PCMs and used for PCM 
invalidation.

This gives the build system a conservative way to invalidate PCMs based on 
things it knows about the environment that may be missed by normal 
(particularly non-strict) Clang module validation due to being too expensive to 
check.

This is motivated by updating system SDKs in place sometimes causing issues 
that require a full clean including of Clang's module cache to resolve, but is 
more generic than just storing the SDK version in the PCM.

* Keys aren't part of the module context hash, so changing them rebuilds 
modules in place instead of growing the module cache.
* Mismatched keys are ignored when the module can't be rebuilt, such as when 
it's imported by a PCH or read by -module-file-info. Unlike a changed input 
file, different keys don't prove that the module is out of date.
* They only impact implicit PCMs, as the build system is already responsible 
for invalidating other PCMs/PCHs.

rdar://182228463
Assisted-by: Claude Code: opus-5.5

>From f76042a35a5edde87b76283f825ada4c16234306 Mon Sep 17 00:00:00 2001
From: Michael Spencer <[email protected]>
Date: Fri, 6 Nov 2026 12:42:25 -0800
Subject: [PATCH] [clang][modules] Add -fimplicit-modules-validation-key and
 use it for SDK version changes

Add a repeatable `-fimplicit-modules-validation-key=<key>` option.
When using implicitly built modules this key is stored in PCMs and
used for PCM invalidation.

This gives the build system a conservative way to invalidate PCMs
based on things it knows about the environment that may be missed by
normal (particularly non-strict) Clang module validation due to being
too expensive to check.

This is motivated by updating system SDKs in place sometimes causing
issues that require a full clean including of Clang's module cache to
resolve, but is more generic than just storing the SDK version in the
PCM.

* Keys aren't part of the module context hash, so changing them
  rebuilds modules in place instead of growing the module cache.
* Mismatched keys are ignored when the module can't be rebuilt, such
  as when it's imported by a PCH or read by -module-file-info. Unlike
  a changed input file, different keys don't prove that the module is
  out of date.
* They only impact implicit PCMs, as the build system is already
  responsible for invalidating other PCMs/PCHs.

rdar://182228463
Assisted-by: Claude Code: opus-5.5
---
 clang/docs/Modules.md                         | 17 +++++
 clang/docs/ReleaseNotes.md                    |  6 ++
 .../Basic/DiagnosticSerializationKinds.td     |  5 ++
 clang/include/clang/Driver/ToolChain.h        |  7 ++
 clang/include/clang/Lex/HeaderSearchOptions.h |  4 +
 clang/include/clang/Options/Options.td        |  6 ++
 .../include/clang/Serialization/ASTBitCodes.h |  3 +
 .../include/clang/Serialization/ModuleFile.h  |  4 +
 clang/lib/Driver/ToolChain.cpp                |  4 +
 clang/lib/Driver/ToolChains/Clang.cpp         | 12 ++-
 clang/lib/Driver/ToolChains/Darwin.cpp        | 22 ++++++
 clang/lib/Driver/ToolChains/Darwin.h          |  4 +
 clang/lib/Frontend/CompilerInvocation.cpp     |  1 +
 clang/lib/Serialization/ASTReader.cpp         | 22 ++++++
 clang/lib/Serialization/ASTWriter.cpp         |  9 +++
 .../implicit-modules-validation-key.c         | 42 +++++++++++
 .../darwin-implicit-modules-validation-key.c  | 66 ++++++++++++++++
 .../Modules/implicit-modules-validation-key.c | 75 +++++++++++++++++++
 18 files changed, 307 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/ClangScanDeps/implicit-modules-validation-key.c
 create mode 100644 clang/test/Driver/darwin-implicit-modules-validation-key.c
 create mode 100644 clang/test/Modules/implicit-modules-validation-key.c

diff --git a/clang/docs/Modules.md b/clang/docs/Modules.md
index 234124cc221833..71a25706910183 100644
--- a/clang/docs/Modules.md
+++ b/clang/docs/Modules.md
@@ -245,6 +245,23 @@ Modules maintain references to each of the headers that 
were part of the module
   prebuilt modules paths (specified via `-fprebuilt-module-path`), we will
   look for a matching implicit module in the prebuilt modules paths.
 
+`-fimplicit-modules-validation-key=<key>`
+
+: Record `<key>` in each implicitly built module, and treat the module as out
+  of date if it is later loaded with a different list of keys. The keys are
+  compared in order and are otherwise opaque.
+
+  Keys are conservative indicators that something more permanent about the
+  environment has changed in a way that:
+  - is expensive or fragile for Clang to check,
+  - is potentially cheap for the build system to conservatively check, and
+  - will not change based on the build configuration.
+
+  These do not impact the context hash as the intent is to replace the old
+  modules in the cache to reduce cache size growth on invalidation. An example
+  of this is the SDK version of Apple SDKs on Darwin-based systems, which the
+  driver passes as a validation key.
+
 #### -cc1 Options
 
 `-fmodules-strict-context-hash`
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 0601ffcec82fdb..b698a9acba196d 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -404,6 +404,12 @@ documentation](CIR/index.md) for more information 
regarding ClangIR's status and
   to be moved without rewriting paths. Paths outside the working directory
   remain absolute. This option cannot be combined with `--relocatable-pch`.
 
+- New option `-fimplicit-modules-validation-key=<key>` makes an implicitly
+  built module out of date when it is validated with different keys than it was
+  built with. When targeting Darwin, the driver adds a key for the SDK's
+  `SystemVersion.plist`, so implicitly built modules are rebuilt when the SDK
+  is updated in place.
+
 - New option `-fmodules-validate-directory-dependencies` makes an implicitly
   built module out of date when a header is added to a directory it enumerated,
   such as an umbrella directory or the directory of an umbrella header, after 
it
diff --git a/clang/include/clang/Basic/DiagnosticSerializationKinds.td 
b/clang/include/clang/Basic/DiagnosticSerializationKinds.td
index 8f29a5645f7106..dfa86f04212260 100644
--- a/clang/include/clang/Basic/DiagnosticSerializationKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSerializationKinds.td
@@ -127,6 +127,11 @@ def err_module_path_invalidated
     : Error<"module '%0' cannot be reused because the build system reported "
             "that '%1' changed">,
       ShowInSystemHeader, DefaultFatal;
+def remark_module_validation_keys_changed
+    : Remark<"module '%0' is out of date because it was built with validation "
+             "keys %1 but the current keys are %2">,
+      ShowInSystemHeader,
+      InGroup<ModuleValidation>;
 def remark_module_check_relocation
     : Remark<"checking if module '%0' from '%1' has relocated">,
       ShowInSystemHeader,
diff --git a/clang/include/clang/Driver/ToolChain.h 
b/clang/include/clang/Driver/ToolChain.h
index b01dbb18c483cf..c3dc7086cf52e0 100644
--- a/clang/include/clang/Driver/ToolChain.h
+++ b/clang/include/clang/Driver/ToolChain.h
@@ -757,6 +757,13 @@ class ToolChain {
                         llvm::opt::ArgStringList &CC1Args, BoundArch BA,
                         Action::OffloadKind DeviceOffloadKind) const;
 
+  /// Add -fimplicit-modules-validation-key options for state of this target
+  /// that implicitly built modules should be invalidated on, such as the
+  /// version of the SDK. Only called when implicit modules are enabled.
+  virtual void
+  addImplicitModulesValidationKeys(const llvm::opt::ArgList &DriverArgs,
+                                   llvm::opt::ArgStringList &CC1Args) const;
+
   /// Add options that need to be passed to cc1as for this target.
   virtual void
   addClangCC1ASTargetOptions(const llvm::opt::ArgList &Args,
diff --git a/clang/include/clang/Lex/HeaderSearchOptions.h 
b/clang/include/clang/Lex/HeaderSearchOptions.h
index 49ff8b82f63e4e..bf240baefed061 100644
--- a/clang/include/clang/Lex/HeaderSearchOptions.h
+++ b/clang/include/clang/Lex/HeaderSearchOptions.h
@@ -110,6 +110,10 @@ class HeaderSearchOptions {
   /// The directory used for the module cache.
   std::string ModuleCachePath;
 
+  /// Opaque keys that an implicitly built module records and is validated
+  /// against, in order.
+  std::vector<std::string> ImplicitModulesValidationKeys;
+
   /// The directory used for a user build.
   std::string ModuleUserBuildPath;
 
diff --git a/clang/include/clang/Options/Options.td 
b/clang/include/clang/Options/Options.td
index 9156640ff67c79..bac784270b92a7 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -3765,6 +3765,12 @@ def fimplicit_modules_lock_timeout_EQ : Joined<["-"], 
"fimplicit-modules-lock-ti
   MetaVarName<"<seconds>">,
   HelpText<"Time to wait on an implicit module lock before timing out">,
   MarshallingInfoInt<FrontendOpts<"ImplicitModulesLockTimeoutSeconds">, "90">;
+def fimplicit_modules_validation_key_EQ : Joined<["-"], 
"fimplicit-modules-validation-key=">,
+  Flags<[NoXarchOption]>, Visibility<[ClangOption, CLOption, CC1Option]>,
+  MetaVarName<"<key>">,
+  HelpText<"Treat an implicitly built module as out of date when it is 
validated "
+           "with different keys than it was built with">,
+  
MarshallingInfoStringVector<HeaderSearchOpts<"ImplicitModulesValidationKeys">>;
 
 defm skip_odr_check_in_gmf : BoolOption<"f", "skip-odr-check-in-gmf",
   LangOpts<"SkipODRCheckInGMF">, DefaultFalse,
diff --git a/clang/include/clang/Serialization/ASTBitCodes.h 
b/clang/include/clang/Serialization/ASTBitCodes.h
index 04cc0c5af89d15..7d147d5230b333 100644
--- a/clang/include/clang/Serialization/ASTBitCodes.h
+++ b/clang/include/clang/Serialization/ASTBitCodes.h
@@ -422,6 +422,9 @@ enum UnhashedControlBlockRecordTypes {
 
   /// Record code for the indices of used VFSs.
   VFS_USAGE,
+
+  /// Record code for the keys an implicitly built module is validated against.
+  IMPLICIT_MODULES_VALIDATION_KEYS,
 };
 
 /// Record code for extension blocks.
diff --git a/clang/include/clang/Serialization/ModuleFile.h 
b/clang/include/clang/Serialization/ModuleFile.h
index c6353d31d4e541..a3268c5135af93 100644
--- a/clang/include/clang/Serialization/ModuleFile.h
+++ b/clang/include/clang/Serialization/ModuleFile.h
@@ -207,6 +207,10 @@ class ModuleFile {
   /// Directories this module file depends on the listing of.
   std::vector<std::string> DirectoryDependencies;
 
+  /// The keys this module was built with, see
+  /// \c HeaderSearchOptions::ImplicitModulesValidationKeys.
+  std::vector<std::string> ImplicitModulesValidationKeys;
+
   /// Whether this precompiled header is a relocatable PCH file.
   bool RelocatablePCH = false;
 
diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index 5bfbd9c5fa4dc5..c5a8b0c6934b0b 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -1575,6 +1575,10 @@ void ToolChain::addClangTargetOptions(
 void ToolChain::addClangCC1ASTargetOptions(const ArgList &Args,
                                            ArgStringList &CC1ASArgs) const {}
 
+void ToolChain::addImplicitModulesValidationKeys(const ArgList &DriverArgs,
+                                                 ArgStringList &CC1Args) const 
{
+}
+
 void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {}
 
 void ToolChain::addProfileRTLibs(const llvm::opt::ArgList &Args,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index b1d0422ae275f9..a59f12a0c9ef62 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -4140,7 +4140,8 @@ clang::driver::tools::getCXX20NamedModuleOutputPath(const 
ArgList &Args,
 }
 
 static bool RenderModulesOptions(Compilation &C, const Driver &D,
-                                 const ArgList &Args, const InputInfo &Input,
+                                 const ToolChain &TC, const ArgList &Args,
+                                 const InputInfo &Input,
                                  const InputInfo &Output, bool HaveStd20,
                                  ArgStringList &CmdArgs) {
   const bool IsCXX = types::isCXX(Input.getType());
@@ -4219,7 +4220,14 @@ static bool RenderModulesOptions(Compilation &C, const 
Driver &D,
     }
 
     Args.AddLastArg(CmdArgs, options::OPT_fimplicit_modules_lock_timeout_EQ);
+
+    // User keys come first, followed by the toolchain's.
+    Args.AddAllArgs(CmdArgs, options::OPT_fimplicit_modules_validation_key_EQ);
+    TC.addImplicitModulesValidationKeys(Args, CmdArgs);
   }
+  // The keys only affect implicitly built modules; don't warn about them when
+  // a build system passes them to every compile.
+  Args.ClaimAllArgs(options::OPT_fimplicit_modules_validation_key_EQ);
 
   if (HaveModules) {
     if (Args.hasFlag(options::OPT_fprebuilt_implicit_modules,
@@ -7926,7 +7934,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction 
&JA,
        Std->containsValue("c++2d") || Std->containsValue("gnu++2d") ||
        Std->containsValue("c++latest") || Std->containsValue("gnu++latest"));
   bool HaveModules =
-      RenderModulesOptions(C, D, Args, Input, Output, HaveCxx20, CmdArgs);
+      RenderModulesOptions(C, D, TC, Args, Input, Output, HaveCxx20, CmdArgs);
 
   // -fdelayed-template-parsing is default when targeting MSVC.
   // Many old Windows SDK versions require this to parse.
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp 
b/clang/lib/Driver/ToolChains/Darwin.cpp
index 69ff4188dbaf9c..d307c1d4fa2c08 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -23,6 +23,7 @@
 #include "llvm/Support/Path.h"
 #include "llvm/Support/Threading.h"
 #include "llvm/Support/VirtualFileSystem.h"
+#include "llvm/Support/xxhash.h"
 #include "llvm/TargetParser/TargetParser.h"
 #include "llvm/TargetParser/Triple.h"
 #include <cstdlib> // ::getenv
@@ -3705,6 +3706,27 @@ void Darwin::addClangCC1ASTargetOptions(
   }
 }
 
+void Darwin::addImplicitModulesValidationKeys(const ArgList &DriverArgs,
+                                              ArgStringList &CC1Args) const {
+  // Rebuild implicitly built modules when the SDK is updated in place. Its
+  // SystemVersion.plist changes with every build of the SDK, so hash it 
instead
+  // of parsing it, as Clang doesn't have an XML parser.
+  StringRef SDKRoot =
+      DriverArgs.getLastArgValue(options::OPT_isysroot, getDriver().SysRoot);
+  if (SDKRoot.empty())
+    return;
+  SmallString<256> Path(SDKRoot);
+  llvm::sys::path::append(Path, "System", "Library", "CoreServices",
+                          "SystemVersion.plist");
+  auto Buffer = getVFS().getBufferForFile(Path);
+  if (!Buffer)
+    return;
+  uint64_t Hash = llvm::xxh3_64bits((*Buffer)->getBuffer());
+  CC1Args.push_back(DriverArgs.MakeArgString(
+      "-fimplicit-modules-validation-key=darwin-sdk-system-version:" +
+      llvm::utohexstr(Hash, /*LowerCase=*/true)));
+}
+
 DerivedArgList *
 Darwin::TranslateArgs(const DerivedArgList &Args, BoundArch BA,
                       Action::OffloadKind DeviceOffloadKind) const {
diff --git a/clang/lib/Driver/ToolChains/Darwin.h 
b/clang/lib/Driver/ToolChains/Darwin.h
index 3ca5753f3012cf..430d66849e71c7 100644
--- a/clang/lib/Driver/ToolChains/Darwin.h
+++ b/clang/lib/Driver/ToolChains/Darwin.h
@@ -616,6 +616,10 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public AppleMachO {
       const llvm::opt::ArgList &Args,
       llvm::opt::ArgStringList &CC1ASArgs) const override;
 
+  void addImplicitModulesValidationKeys(
+      const llvm::opt::ArgList &DriverArgs,
+      llvm::opt::ArgStringList &CC1Args) const override;
+
   StringRef getOSLibraryNameSuffix(bool IgnoreSim = false) const override;
 
 public:
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index d0d99f6335dad5..b1c1337798cbfa 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -5590,6 +5590,7 @@ void 
CompilerInvocation::clearImplicitModuleBuildOptions() {
   getHeaderSearchOpts().ModulesValidateOncePerBuildSession = false;
   getHeaderSearchOpts().ModulesValidateDirectoryDependencies = false;
   getHeaderSearchOpts().BuildSessionTimestamp = 0;
+  getHeaderSearchOpts().ImplicitModulesValidationKeys.clear();
   // The specific values we canonicalize to for pruning don't affect behaviour,
   /// so use the default values so they may be dropped from the command-line.
   getHeaderSearchOpts().ModuleCachePruneInterval = 7 * 24 * 60 * 60;
diff --git a/clang/lib/Serialization/ASTReader.cpp 
b/clang/lib/Serialization/ASTReader.cpp
index db04c06f3cee73..749fa51cf6556d 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -3351,6 +3351,21 @@ ASTReader::ReadControlBlock(ModuleFile &F,
                   : InputFilesValidation::SkippedInBuildSession;
         }
 
+        // Unlike a changed input, different keys don't prove that the module 
is
+        // out of date, so they are ignored when it can't be rebuilt.
+        if (!WasValidated && !Complain && F.Kind == MK_ImplicitModule &&
+            F.ImplicitModulesValidationKeys !=
+                HSOpts.ImplicitModulesValidationKeys) {
+          auto Quote = [](ArrayRef<std::string> Keys) {
+            return Keys.empty() ? std::string("none")
+                                : "'" + llvm::join(Keys, "', '") + "'";
+          };
+          Diag(diag::remark_module_validation_keys_changed)
+              << F.ModuleName << Quote(F.ImplicitModulesValidationKeys)
+              << Quote(HSOpts.ImplicitModulesValidationKeys);
+          return OutOfDate;
+        }
+
         if (N != 0)
           Diag(diag::remark_module_validation)
               << N << F.ModuleName << F.FileName;
@@ -5681,6 +5696,13 @@ ASTReader::ASTReadResult 
ASTReader::readUnhashedControlBlockImpl(
       if (F)
         F->VFSUsage = ReadBitVector(Record, Blob);
       break;
+    case IMPLICIT_MODULES_VALIDATION_KEYS:
+      if (F) {
+        F->ImplicitModulesValidationKeys.clear();
+        for (unsigned Idx = 0; Idx < Record.size();)
+          F->ImplicitModulesValidationKeys.push_back(ReadString(Record, Idx));
+      }
+      break;
     }
   }
 }
diff --git a/clang/lib/Serialization/ASTWriter.cpp 
b/clang/lib/Serialization/ASTWriter.cpp
index 3ae7ac0e8c9fa0..a44c54d27387d7 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -1172,6 +1172,7 @@ void ASTWriter::WriteBlockInfoBlock() {
   RECORD(DIAG_PRAGMA_MAPPINGS);
   RECORD(HEADER_SEARCH_ENTRY_USAGE);
   RECORD(VFS_USAGE);
+  RECORD(IMPLICIT_MODULES_VALIDATION_KEYS);
 
 #undef RECORD
 #undef BLOCK
@@ -1446,6 +1447,14 @@ void ASTWriter::writeUnhashedControlBlock(Preprocessor 
&PP) {
     Stream.EmitRecordWithBlob(VFSUsageAbbrevCode, Record, bytes(VFSUsage));
   }
 
+  // Implicit modules validation keys.
+  if (BuildingImplicitModule && !HSOpts.ImplicitModulesValidationKeys.empty()) 
{
+    Record.clear();
+    for (StringRef Key : HSOpts.ImplicitModulesValidationKeys)
+      AddString(Key, Record);
+    Stream.EmitRecord(IMPLICIT_MODULES_VALIDATION_KEYS, Record);
+  }
+
   // Leave the options block.
   Stream.ExitBlock();
   UnhashedControlBlockRange.second = Stream.GetCurrentBitNo() >> 3;
diff --git a/clang/test/ClangScanDeps/implicit-modules-validation-key.c 
b/clang/test/ClangScanDeps/implicit-modules-validation-key.c
new file mode 100644
index 00000000000000..84aa08316ef9da
--- /dev/null
+++ b/clang/test/ClangScanDeps/implicit-modules-validation-key.c
@@ -0,0 +1,42 @@
+// Check that -fimplicit-modules-validation-key invalidates the scanner's
+// implicitly built modules, but isn't passed to explicit module or translation
+// unit commands, and doesn't change their context hashes.
+
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: sed -e "s|DIR|%/t|g" -e "s|KEY|a|g" %t/cdb.json.template > 
%t/cdb-a.json
+// RUN: sed -e "s|DIR|%/t|g" -e "s|KEY|b|g" %t/cdb.json.template > 
%t/cdb-b.json
+
+// RUN: clang-scan-deps -compilation-database %t/cdb-a.json -j 1 \
+// RUN:   -format experimental-full > %t/a.json
+// RUN: find %t/cache -name "Mod-*.pcm" -exec cp "{}" %t/mod-a.pcm ";"
+// RUN: clang-scan-deps -compilation-database %t/cdb-b.json -j 1 \
+// RUN:   -format experimental-full > %t/b.json
+// RUN: find %t/cache -name "Mod-*.pcm" -exec cp "{}" %t/mod-b.pcm ";"
+// RUN: FileCheck %s < %t/a.json
+// CHECK-NOT: -fimplicit-modules-validation-key
+// CHECK: "name": "Mod"
+// CHECK-NOT: -fimplicit-modules-validation-key
+
+// The scanner's own module was rebuilt with the new key.
+// RUN: not diff %t/mod-a.pcm %t/mod-b.pcm
+
+// RUN: grep '"context-hash"' %t/a.json > %t/a.hash
+// RUN: grep '"context-hash"' %t/b.json > %t/b.hash
+// RUN: diff %t/a.hash %t/b.hash
+
+//--- cdb.json.template
+[{
+  "directory": "DIR",
+  "command": "clang -fsyntax-only DIR/tu.c -fmodules 
-fmodules-cache-path=DIR/cache -I DIR/include 
-fimplicit-modules-validation-key=KEY",
+  "file": "DIR/tu.c"
+}]
+
+//--- include/module.modulemap
+module Mod { header "mod.h" }
+
+//--- include/mod.h
+int mod;
+
+//--- tu.c
+#include "mod.h"
diff --git a/clang/test/Driver/darwin-implicit-modules-validation-key.c 
b/clang/test/Driver/darwin-implicit-modules-validation-key.c
new file mode 100644
index 00000000000000..a9f95abe340bc7
--- /dev/null
+++ b/clang/test/Driver/darwin-implicit-modules-validation-key.c
@@ -0,0 +1,66 @@
+// Check that the Darwin driver adds a validation key for the SDK's
+// SystemVersion.plist after the user's keys, and only for implicit modules.
+
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+
+// RUN: %clang -### --target=arm64-apple-macos14 -isysroot %t/SDK -fmodules \
+// RUN:   -fimplicit-modules-validation-key=user -c %t/tu.c 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=IMPLICIT
+// RUN: env -u SDKROOT %clang -### --target=arm64-apple-macos14 
--sysroot=%t/SDK -fmodules \
+// RUN:   -fimplicit-modules-validation-key=user -c %t/tu.c 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=IMPLICIT
+// IMPLICIT: "-fimplicit-modules-validation-key=user" 
"-fimplicit-modules-validation-key=darwin-sdk-system-version:{{[0-9a-f]+}}"
+
+// The key changes with the contents of SystemVersion.plist.
+// RUN: %clang -### --target=arm64-apple-macos14 -isysroot %t/SDK -fmodules \
+// RUN:   -c %t/tu.c 2>&1 | grep -o 'darwin-sdk-system-version:[0-9a-f]*' > 
%t/key1
+// RUN: %clang -### --target=arm64-apple-macos14 -isysroot %t/SDK2 -fmodules \
+// RUN:   -c %t/tu.c 2>&1 | grep -o 'darwin-sdk-system-version:[0-9a-f]*' > 
%t/key2
+// RUN: not diff %t/key1 %t/key2
+
+// RUN: %clang -### --target=arm64-apple-macos14 -isysroot %t/SDK -fmodules \
+// RUN:   -fno-implicit-modules -fimplicit-modules-validation-key=user \
+// RUN:   -c %t/tu.c 2>&1 | FileCheck %s --check-prefix=NONE
+// NONE-NOT: warning: argument unused
+// NONE: "-cc1"
+// NONE-NOT: -fimplicit-modules-validation-key
+
+// No SystemVersion.plist, so only the user's key.
+// RUN: %clang -### --target=arm64-apple-macos14 -isysroot %t/NoPlist.sdk \
+// RUN:   -fmodules -fimplicit-modules-validation-key=user -c %t/tu.c 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=USER-ONLY
+// RUN: %clang -### --target=x86_64-linux-gnu -fmodules \
+// RUN:   -fimplicit-modules-validation-key=user -c %t/tu.c 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=USER-ONLY
+// USER-ONLY: "-cc1"
+// USER-ONLY-SAME: "-fimplicit-modules-validation-key=user"
+// USER-ONLY-NOT: darwin-sdk-system-version
+
+//--- SDK/System/Library/CoreServices/SystemVersion.plist
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
"http://www.apple.com/DTDs/PropertyList-1.0.dtd";>
+<plist version="1.0">
+<dict>
+       <key>ProductBuildVersion</key>
+       <string>23A344</string>
+       <key>ProductVersion</key>
+       <string>14.0</string>
+</dict>
+</plist>
+
+//--- SDK2/System/Library/CoreServices/SystemVersion.plist
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
"http://www.apple.com/DTDs/PropertyList-1.0.dtd";>
+<plist version="1.0">
+<dict>
+       <key>ProductBuildVersion</key>
+       <string>23B74</string>
+       <key>ProductVersion</key>
+       <string>14.1</string>
+</dict>
+</plist>
+
+//--- NoPlist.sdk/usr/include/stdio.h
+
+//--- tu.c
diff --git a/clang/test/Modules/implicit-modules-validation-key.c 
b/clang/test/Modules/implicit-modules-validation-key.c
new file mode 100644
index 00000000000000..563d05cc3982d8
--- /dev/null
+++ b/clang/test/Modules/implicit-modules-validation-key.c
@@ -0,0 +1,75 @@
+// Check that an implicitly built module is out of date when it's validated
+// with different -fimplicit-modules-validation-key values than it was built
+// with, and that the keys are only compared when the inputs are validated.
+
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: echo "-fsyntax-only -fmodules -fimplicit-module-maps 
-fmodules-cache-path=%/t/cache" > %t/ctx.rsp
+// RUN: echo "-I %/t/include -Rmodule-build -Rmodule-validation" >> %t/ctx.rsp
+
+// RUN: %clang_cc1 @%t/ctx.rsp %t/tu.c -fimplicit-modules-validation-key=a \
+// RUN:   2>&1 | FileCheck %s --check-prefix=BUILD
+// RUN: %clang_cc1 @%t/ctx.rsp %t/tu.c -fimplicit-modules-validation-key=a \
+// RUN:   2>&1 | FileCheck %s --check-prefix=REUSE --allow-empty
+// RUN: %clang_cc1 @%t/ctx.rsp %t/tu.c -fimplicit-modules-validation-key=b \
+// RUN:   2>&1 | FileCheck %s --check-prefixes=A-TO-B,BUILD
+
+// The keys are compared in order.
+// RUN: %clang_cc1 @%t/ctx.rsp %t/tu.c -fimplicit-modules-validation-key=b \
+// RUN:   -fimplicit-modules-validation-key=c \
+// RUN:   2>&1 | FileCheck %s --check-prefixes=B-TO-BC,BUILD
+// RUN: %clang_cc1 @%t/ctx.rsp %t/tu.c -fimplicit-modules-validation-key=c \
+// RUN:   -fimplicit-modules-validation-key=b \
+// RUN:   2>&1 | FileCheck %s --check-prefixes=BC-TO-CB,BUILD
+// RUN: %clang_cc1 @%t/ctx.rsp %t/tu.c \
+// RUN:   2>&1 | FileCheck %s --check-prefixes=CB-TO-NONE,BUILD
+
+// Once the module was validated in the current build session, the keys aren't
+// compared again until the next one.
+// RUN: %clang_cc1 @%t/ctx.rsp %t/tu.c -fimplicit-modules-validation-key=x \
+// RUN:   -fmodules-validate-once-per-build-session 
-fbuild-session-timestamp=1 \
+// RUN:   2>&1 | FileCheck %s --check-prefixes=NONE-TO-X,BUILD
+// RUN: %clang_cc1 @%t/ctx.rsp %t/tu.c -fimplicit-modules-validation-key=y \
+// RUN:   -fmodules-validate-once-per-build-session 
-fbuild-session-timestamp=1 \
+// RUN:   2>&1 | FileCheck %s --check-prefix=REUSE --allow-empty
+// RUN: %clang_cc1 @%t/ctx.rsp %t/tu.c -fimplicit-modules-validation-key=y \
+// RUN:   -fmodules-validate-once-per-build-session \
+// RUN:   -fbuild-session-timestamp=4000000000 \
+// RUN:   2>&1 | FileCheck %s --check-prefixes=X-TO-Y,BUILD
+
+// A precompiled header can't rebuild the modules it imports, so they are used
+// even though the keys differ.
+// RUN: %clang_cc1 -fmodules -fimplicit-module-maps 
-fmodules-cache-path=%/t/cache \
+// RUN:   -I %/t/include -emit-pch -x c-header %t/pch.h -o %t/pch.pch \
+// RUN:   -fimplicit-modules-validation-key=p
+// RUN: %clang_cc1 @%t/ctx.rsp %t/use-pch.c -include-pch %t/pch.pch \
+// RUN:   -fimplicit-modules-validation-key=q \
+// RUN:   2>&1 | FileCheck %s --check-prefix=REUSE-NOT-REBUILT --allow-empty
+
+// A-TO-B: remark: module 'Mod' is out of date because it was built with 
validation keys 'a' but the current keys are 'b'
+// B-TO-BC: remark: module 'Mod' is out of date because it was built with 
validation keys 'b' but the current keys are 'b', 'c'
+// BC-TO-CB: remark: module 'Mod' is out of date because it was built with 
validation keys 'b', 'c' but the current keys are 'c', 'b'
+// CB-TO-NONE: remark: module 'Mod' is out of date because it was built with 
validation keys 'c', 'b' but the current keys are none
+// NONE-TO-X: remark: module 'Mod' is out of date because it was built with 
validation keys none but the current keys are 'x'
+// X-TO-Y: remark: module 'Mod' is out of date because it was built with 
validation keys 'x' but the current keys are 'y'
+// BUILD: remark: building module 'Mod'
+// REUSE: validating {{[0-9]+}} input files in module 'Mod'
+// REUSE-NOT: out of date
+// REUSE-NOT: building module
+// REUSE-NOT-REBUILT-NOT: out of date
+// REUSE-NOT-REBUILT-NOT: building module
+
+//--- include/module.modulemap
+module Mod { header "mod.h" }
+
+//--- include/mod.h
+int mod;
+
+//--- tu.c
+#include "mod.h"
+
+//--- pch.h
+#include "mod.h"
+
+//--- use-pch.c
+int use(void) { return mod; }

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to