Skip to content

Commit 1eb304d

Browse files
authored
Merge pull request #5218 from akyrtzi/stable/20220421
[stable][clang][cas] Record `$SDK/SDKSettings.json` as a CAS input to accomodate availability checks during compilation
2 parents f8c90a0 + 76d6952 commit 1eb304d

File tree

7 files changed

+62
-9
lines changed

7 files changed

+62
-9
lines changed

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,9 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {
474474
InitializeFileRemapping(PP->getDiagnostics(), PP->getSourceManager(),
475475
PP->getFileManager(), PPOpts);
476476

477-
if (getFrontendOpts().CASIncludeTreeID.empty()) {
478-
// Predefine macros and configure the preprocessor.
479-
InitializePreprocessor(*PP, PPOpts, getPCHContainerReader(),
480-
getFrontendOpts());
481-
}
477+
// Predefine macros and configure the preprocessor.
478+
InitializePreprocessor(*PP, PPOpts, getPCHContainerReader(),
479+
getFrontendOpts());
482480

483481
// Initialize the header search object. In CUDA compilations, we use the aux
484482
// triple (the host triple) to initialize our header search, since we need to

clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,11 @@ void IncludeTreePPConsumer::handleHasIncludeCheck(Preprocessor &PP,
302302
void IncludeTreePPConsumer::finalize(CompilerInstance &CI) {
303303
FileManager &FM = CI.getFileManager();
304304

305-
auto addFile = [&](StringRef FilePath) -> bool {
305+
auto addFile = [&](StringRef FilePath, bool IgnoreFileError = false) -> bool {
306306
llvm::ErrorOr<const FileEntry *> FE = FM.getFile(FilePath);
307307
if (!FE) {
308+
if (IgnoreFileError)
309+
return true;
308310
ErrorToReport = llvm::errorCodeToError(FE.getError());
309311
return false;
310312
}
@@ -321,6 +323,15 @@ void IncludeTreePPConsumer::finalize(CompilerInstance &CI) {
321323
return;
322324
}
323325

326+
StringRef Sysroot = CI.getHeaderSearchOpts().Sysroot;
327+
if (!Sysroot.empty()) {
328+
// Include 'SDKSettings.json', if it exists, to accomodate availability
329+
// checks during the compilation.
330+
llvm::SmallString<256> FilePath = Sysroot;
331+
llvm::sys::path::append(FilePath, "SDKSettings.json");
332+
addFile(FilePath, /*IgnoreFileError*/ true);
333+
}
334+
324335
PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
325336
if (PPOpts.ImplicitPCHInclude.empty())
326337
return; // no need for additional work.

clang/lib/Tooling/DependencyScanning/ScanAndUpdateArgs.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ static void updateCompilerInvocation(CompilerInvocation &Invocation,
8585
if (ProduceIncludeTree) {
8686
Invocation.getFrontendOpts().CASIncludeTreeID = RootID;
8787
Invocation.getFrontendOpts().Inputs.clear();
88+
// Preserve sysroot path to accommodate lookup for 'SDKSettings.json' during
89+
// availability checking.
90+
std::string OriginalSysroot = Invocation.getHeaderSearchOpts().Sysroot;
8891
Invocation.getHeaderSearchOpts() = HeaderSearchOptions();
92+
Invocation.getHeaderSearchOpts().Sysroot = OriginalSysroot;
8993
auto &PPOpts = Invocation.getPreprocessorOpts();
9094
// We don't need this because we save the contents of the PCH file in the
9195
// include tree root.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"DefaultVariant": "macos", "DisplayName": "macOS 11",
3+
"Version": "11.0",
4+
"MaximumDeploymentTarget": "11.0.99",
5+
"PropertyConditionFallbackNames": [], "VersionMap": {
6+
"iOSMac_macOS": {
7+
"13.2": "10.15.1",
8+
"13.4": "10.15.4",
9+
"13.3.1": "10.15.3",
10+
"13.3": "10.15.2",
11+
"13.1": "10.15",
12+
"14.0": "11.0"
13+
},
14+
"macOS_iOSMac": {
15+
"10.15.2": "13.3",
16+
"11.0": "14.0",
17+
"10.15": "13.1",
18+
"10.15.3": "13.3.1",
19+
"10.15.1": "13.2",
20+
"10.15.4": "13.4"
21+
}
22+
}
23+
}

clang/test/CAS/availability-check.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: rm -rf %t && mkdir %t
2+
3+
// RUN: %clang_cc1 -triple x86_64-apple-ios14-macabi -isysroot %S/Inputs/MacOSX11.0.sdk -fsyntax-only -verify %s
4+
5+
// RUN: %clang -cc1depscan -o %t/cmd.rsp -fdepscan=inline -fdepscan-include-tree -cc1-args \
6+
// RUN: -cc1 -fcas-path %t/cas -triple x86_64-apple-ios14-macabi -isysroot %S/Inputs/MacOSX11.0.sdk %s
7+
8+
// FIXME: `-verify` should work with a CAS invocation.
9+
// RUN: not %clang @%t/cmd.rsp -fsyntax-only 2> %t/out.txt
10+
// RUN: FileCheck -input-file %t/out.txt %s
11+
// CHECK: error: 'fUnavail' is unavailable
12+
13+
void fUnavail(void) __attribute__((availability(macOS, obsoleted = 10.15))); // expected-note {{marked unavailable here}}
14+
15+
void test() {
16+
fUnavail(); // expected-error {{unavailable}}
17+
}

clang/test/CAS/depscan-include-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
// CHECK: "-fcas-path" "[[PREFIX]]/cas"
1313
// CHECK: "-fcas-include-tree"
14+
// CHECK: "-isysroot"
1415
// SHOULD-NOT: "-fcas-fs"
1516
// SHOULD-NOT: "-fcas-fs-working-directory"
16-
// SHOULD-NOT: "-isysroot"
1717
// SHOULD-NOT: "-I"
1818
// SHOULD-NOT: "[[PREFIX]]/t.c"
1919
// SHOULD-NOT: "-D"

clang/test/ClangScanDeps/include-tree-with-pch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RUN: split-file %s %t
33
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
44

5-
// RUN: %clang -x c-header %t/prefix.h -o %t/prefix.pch -fdepscan=inline -fdepscan-include-tree -Xclang -fcas-path -Xclang %t/cas
5+
// 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
66
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-include-tree -cas-path %t/cas > %t/result.txt
77
// RUN: FileCheck %s -input-file %t/result.txt -DPREFIX=%/t
88

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

0 commit comments

Comments
 (0)