Skip to content

[clang][modules] Detect invalidation of SDKSettings.json #10661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/lib/Index/IndexingAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,8 @@ class ModuleFileIndexDependencyCollector : public IndexDependencyProvider {
// undesirable dependency on an intermediate build byproduct.
if (FE->getName().ends_with("module.modulemap"))
return;
if (FE->getName().ends_with("SDKSettings.json"))
return;

visitor(*FE, isSystem);
});
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/Serialization/ASTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5950,6 +5950,18 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema *SemaPtr, StringRef isysroot,
if (Chain)
Chain->finalizeForWriting();

// FIXME: This is here because the include-tree file list refers to this file
// and we need to validate it's up-to-date when reading the AST file. Make
// this more generic and include the remaining files.
StringRef Sysroot = PP->getHeaderSearchInfo().getHeaderSearchOpts().Sysroot;
if (!Sysroot.empty()) {
SmallString<128> SDKSettingsJSON = Sysroot;
llvm::sys::path::append(SDKSettingsJSON, "SDKSettings.json");
if (auto FE = PP->getFileManager().getOptionalFileRef(SDKSettingsJSON))
PP->getSourceManager().createFileID(*FE, SourceLocation(),
SrcMgr::C_System);
}

// This needs to be done very early, since everything that writes
// SourceLocations or FileIDs depends on it.
computeNonAffectingInputFiles();
Expand Down
37 changes: 37 additions & 0 deletions clang/test/ClangScanDeps/modules-include-tree-sdk-settings.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This test checks that the module cache gets invalidated when the
// SDKSettings.json file changes. This prevents "file changed during build"
// errors when the TU does get rescanned and recompiled.

// REQUIRES: ondisk_cas

// RUN: rm -rf %t
// RUN: split-file %s %t

//--- sdk/SDKSettings.json
{
"Version": "11.0",
"MaximumDeploymentTarget": "11.0.99"
}

//--- module.modulemap
module M { header "M.h" }
//--- M.h
//--- tu.c
#include "M.h"

// RUN: clang-scan-deps -format experimental-include-tree-full -cas-path %t/cas -o %t/deps_clean.json \
// RUN: -- %clang -target x86_64-apple-macos11 -isysroot %t/sdk \
// RUN: -c %t/tu.c -o %t/tu.o -fmodules -fmodules-cache-path=%t/cache

// RUN: sleep 1
// RUN: echo " " >> %t/sdk/SDKSettings.json
// RUN: echo " " >> %t/tu.c

// RUN: clang-scan-deps -format experimental-include-tree-full -cas-path %t/cas -o %t/deps_incremental.json \
// RUN: -- %clang -target x86_64-apple-macos11 -isysroot %t/sdk \
// RUN: -c %t/tu.c -o %t/tu.o -fmodules -fmodules-cache-path=%t/cache

// RUN: %deps-to-rsp %t/deps_incremental.json --module-name M > %t/M.rsp
// RUN: %deps-to-rsp %t/deps_incremental.json --tu-index 0 > %t/tu.rsp
// RUN: %clang @%t/M.rsp
// RUN: %clang @%t/tu.rsp
3 changes: 2 additions & 1 deletion clang/tools/c-index-test/core_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,8 @@ static int scanDeps(ArrayRef<const char *> Args, std::string WorkingDirectory,
llvm::outs() << " " << ModuleName << "\n";
llvm::outs() << " file-deps:\n";
for (const auto &FileName : ArrayRef(FileDeps.Strings, FileDeps.Count))
llvm::outs() << " " << FileName << "\n";
if (!StringRef(FileName).ends_with("SDKSettings.json"))
llvm::outs() << " " << FileName << "\n";
llvm::outs() << " build-args:";
for (const auto &Arg :
ArrayRef(BuildArguments.Strings, BuildArguments.Count))
Expand Down
8 changes: 6 additions & 2 deletions clang/tools/clang-scan-deps/ClangScanDeps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,8 @@ template <typename Container>
static auto toJSONStrings(llvm::json::OStream &JOS, Container &&Strings) {
return [&JOS, Strings = std::forward<Container>(Strings)] {
for (StringRef Str : Strings)
JOS.value(Str);
if (!Str.ends_with("SDKSettings.json"))
JOS.value(Str);
};
}

Expand Down Expand Up @@ -764,7 +765,10 @@ class FullDeps {
toJSONStrings(JOS, MD.getBuildArguments()));
JOS.attribute("context-hash", StringRef(MD.ID.ContextHash));
JOS.attributeArray("file-deps", [&] {
MD.forEachFileDep([&](StringRef FileDep) { JOS.value(FileDep); });
MD.forEachFileDep([&](StringRef FileDep) {
if (!FileDep.ends_with("SDKSettings.json"))
JOS.value(FileDep);
});
});
JOS.attributeArray("link-libraries",
toJSONSorted(JOS, MD.LinkLibraries));
Expand Down