Skip to content

[stable][clang][cas] Record $SDK/SDKSettings.json as a CAS input to accomodate availability checks during compilation #5218

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

Merged
merged 1 commit into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,11 +474,9 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {
InitializeFileRemapping(PP->getDiagnostics(), PP->getSourceManager(),
PP->getFileManager(), PPOpts);

if (getFrontendOpts().CASIncludeTreeID.empty()) {
// Predefine macros and configure the preprocessor.
InitializePreprocessor(*PP, PPOpts, getPCHContainerReader(),
getFrontendOpts());
}
// Predefine macros and configure the preprocessor.
InitializePreprocessor(*PP, PPOpts, getPCHContainerReader(),
getFrontendOpts());

// Initialize the header search object. In CUDA compilations, we use the aux
// triple (the host triple) to initialize our header search, since we need to
Expand Down
13 changes: 12 additions & 1 deletion clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,11 @@ void IncludeTreePPConsumer::handleHasIncludeCheck(Preprocessor &PP,
void IncludeTreePPConsumer::finalize(CompilerInstance &CI) {
FileManager &FM = CI.getFileManager();

auto addFile = [&](StringRef FilePath) -> bool {
auto addFile = [&](StringRef FilePath, bool IgnoreFileError = false) -> bool {
llvm::ErrorOr<const FileEntry *> FE = FM.getFile(FilePath);
if (!FE) {
if (IgnoreFileError)
return true;
ErrorToReport = llvm::errorCodeToError(FE.getError());
return false;
}
Expand All @@ -321,6 +323,15 @@ void IncludeTreePPConsumer::finalize(CompilerInstance &CI) {
return;
}

StringRef Sysroot = CI.getHeaderSearchOpts().Sysroot;
if (!Sysroot.empty()) {
// Include 'SDKSettings.json', if it exists, to accomodate availability
// checks during the compilation.
llvm::SmallString<256> FilePath = Sysroot;
llvm::sys::path::append(FilePath, "SDKSettings.json");
addFile(FilePath, /*IgnoreFileError*/ true);
}

PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
if (PPOpts.ImplicitPCHInclude.empty())
return; // no need for additional work.
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Tooling/DependencyScanning/ScanAndUpdateArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ static void updateCompilerInvocation(CompilerInvocation &Invocation,
if (ProduceIncludeTree) {
Invocation.getFrontendOpts().CASIncludeTreeID = RootID;
Invocation.getFrontendOpts().Inputs.clear();
// Preserve sysroot path to accommodate lookup for 'SDKSettings.json' during
// availability checking.
std::string OriginalSysroot = Invocation.getHeaderSearchOpts().Sysroot;
Invocation.getHeaderSearchOpts() = HeaderSearchOptions();
Invocation.getHeaderSearchOpts().Sysroot = OriginalSysroot;
auto &PPOpts = Invocation.getPreprocessorOpts();
// We don't need this because we save the contents of the PCH file in the
// include tree root.
Expand Down
23 changes: 23 additions & 0 deletions clang/test/CAS/Inputs/MacOSX11.0.sdk/SDKSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"DefaultVariant": "macos", "DisplayName": "macOS 11",
"Version": "11.0",
"MaximumDeploymentTarget": "11.0.99",
"PropertyConditionFallbackNames": [], "VersionMap": {
"iOSMac_macOS": {
"13.2": "10.15.1",
"13.4": "10.15.4",
"13.3.1": "10.15.3",
"13.3": "10.15.2",
"13.1": "10.15",
"14.0": "11.0"
},
"macOS_iOSMac": {
"10.15.2": "13.3",
"11.0": "14.0",
"10.15": "13.1",
"10.15.3": "13.3.1",
"10.15.1": "13.2",
"10.15.4": "13.4"
}
}
}
17 changes: 17 additions & 0 deletions clang/test/CAS/availability-check.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: rm -rf %t && mkdir %t

// RUN: %clang_cc1 -triple x86_64-apple-ios14-macabi -isysroot %S/Inputs/MacOSX11.0.sdk -fsyntax-only -verify %s

// RUN: %clang -cc1depscan -o %t/cmd.rsp -fdepscan=inline -fdepscan-include-tree -cc1-args \
// RUN: -cc1 -fcas-path %t/cas -triple x86_64-apple-ios14-macabi -isysroot %S/Inputs/MacOSX11.0.sdk %s

// FIXME: `-verify` should work with a CAS invocation.
// RUN: not %clang @%t/cmd.rsp -fsyntax-only 2> %t/out.txt
// RUN: FileCheck -input-file %t/out.txt %s
// CHECK: error: 'fUnavail' is unavailable

void fUnavail(void) __attribute__((availability(macOS, obsoleted = 10.15))); // expected-note {{marked unavailable here}}

void test() {
fUnavail(); // expected-error {{unavailable}}
}
2 changes: 1 addition & 1 deletion clang/test/CAS/depscan-include-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

// CHECK: "-fcas-path" "[[PREFIX]]/cas"
// CHECK: "-fcas-include-tree"
// CHECK: "-isysroot"
// SHOULD-NOT: "-fcas-fs"
// SHOULD-NOT: "-fcas-fs-working-directory"
// SHOULD-NOT: "-isysroot"
// SHOULD-NOT: "-I"
// SHOULD-NOT: "[[PREFIX]]/t.c"
// SHOULD-NOT: "-D"
Expand Down
4 changes: 2 additions & 2 deletions clang/test/ClangScanDeps/include-tree-with-pch.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: split-file %s %t
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json

// RUN: %clang -x c-header %t/prefix.h -o %t/prefix.pch -fdepscan=inline -fdepscan-include-tree -Xclang -fcas-path -Xclang %t/cas
// RUN: %clang -x c-header %t/prefix.h -target x86_64-apple-macos12 -o %t/prefix.pch -fdepscan=inline -fdepscan-include-tree -Xclang -fcas-path -Xclang %t/cas
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-include-tree -cas-path %t/cas > %t/result.txt
// RUN: FileCheck %s -input-file %t/result.txt -DPREFIX=%/t

Expand Down Expand Up @@ -43,7 +43,7 @@ int n3 = 0;
//--- cdb.json.template
[{
"directory" : "DIR",
"command" : "clang -fsyntax-only DIR/t.c -Xclang -include-pch -Xclang DIR/prefix.pch",
"command" : "clang -fsyntax-only DIR/t.c -target x86_64-apple-macos12 -isysroot DIR -Xclang -include-pch -Xclang DIR/prefix.pch",
"file" : "DIR/t.c"
}]

Expand Down