diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index 5456b2a8fb922..76cc2ba0d0eb4 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -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 diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp index 6f2f25b51a85f..a832c0e63416a 100644 --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp @@ -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 FE = FM.getFile(FilePath); if (!FE) { + if (IgnoreFileError) + return true; ErrorToReport = llvm::errorCodeToError(FE.getError()); return false; } @@ -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. diff --git a/clang/lib/Tooling/DependencyScanning/ScanAndUpdateArgs.cpp b/clang/lib/Tooling/DependencyScanning/ScanAndUpdateArgs.cpp index 50542c7c4f667..e596f99a0a46b 100644 --- a/clang/lib/Tooling/DependencyScanning/ScanAndUpdateArgs.cpp +++ b/clang/lib/Tooling/DependencyScanning/ScanAndUpdateArgs.cpp @@ -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. diff --git a/clang/test/CAS/Inputs/MacOSX11.0.sdk/SDKSettings.json b/clang/test/CAS/Inputs/MacOSX11.0.sdk/SDKSettings.json new file mode 100644 index 0000000000000..b40e35e882e60 --- /dev/null +++ b/clang/test/CAS/Inputs/MacOSX11.0.sdk/SDKSettings.json @@ -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" + } + } +} diff --git a/clang/test/CAS/availability-check.c b/clang/test/CAS/availability-check.c new file mode 100644 index 0000000000000..ac55bf485ddd3 --- /dev/null +++ b/clang/test/CAS/availability-check.c @@ -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}} +} diff --git a/clang/test/CAS/depscan-include-tree.c b/clang/test/CAS/depscan-include-tree.c index b163d07c69602..71d8ab87a4dcd 100644 --- a/clang/test/CAS/depscan-include-tree.c +++ b/clang/test/CAS/depscan-include-tree.c @@ -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" diff --git a/clang/test/ClangScanDeps/include-tree-with-pch.c b/clang/test/ClangScanDeps/include-tree-with-pch.c index 3fec4e4116553..416bbfd60668e 100644 --- a/clang/test/ClangScanDeps/include-tree-with-pch.c +++ b/clang/test/ClangScanDeps/include-tree-with-pch.c @@ -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 @@ -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" }]