Skip to content

Commit a8f47b3

Browse files
committed
ClangImporter: support SDK for APINotes
Honour the SDK for APINotes to augment the system libraries. This allows us to distribute APINotes with the Swift SDK and impact the system without having to map the APINotes into the filesystem.
1 parent fa54a4c commit a8f47b3

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -749,10 +749,24 @@ void importer::getNormalInvocationArguments(
749749
invocationArgStrs.push_back("-fapinotes-modules");
750750
invocationArgStrs.push_back("-fapinotes-swift-version=" +
751751
languageVersion.asAPINotesVersionString());
752-
invocationArgStrs.push_back("-iapinotes-modules");
753-
invocationArgStrs.push_back((llvm::Twine(searchPathOpts.RuntimeResourcePath) +
754-
llvm::sys::path::get_separator() +
755-
"apinotes").str());
752+
753+
// Prefer `-sdk` paths.
754+
if (!searchPathOpts.getSDKPath().empty()) {
755+
llvm::SmallString<261> path{searchPathOpts.getSDKPath()};
756+
llvm::sys::path::append(path, "usr", "lib", "swift", "apinotes");
757+
758+
invocationArgStrs.push_back("-iapinotes-modules");
759+
invocationArgStrs.push_back(path.str().str());
760+
}
761+
762+
// Fallback to "legacy" `-resource-dir` paths.
763+
{
764+
llvm::SmallString<261> path{searchPathOpts.RuntimeResourcePath};
765+
llvm::sys::path::append(path, "apinotes");
766+
767+
invocationArgStrs.push_back("-iapinotes-modules");
768+
invocationArgStrs.push_back(path.str().str());
769+
}
756770
}
757771

758772
static void

test/ClangImporter/sdk-apinotes.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck %s -verify
2+
3+
import cfuncs
4+
5+
let array: [UnsafePointer<CChar>?] = [nil]
6+
array.withUnsafeBufferPointer { nullability_note($0.baseAddress!) }

test/Inputs/clang-importer-sdk/usr/include/cfuncs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ void opaque_pointer_param(struct not_importable *);
6060

6161
int unsupported_parameter_type(int param1, _Complex int param2);
6262
_Complex int unsupported_return_type();
63+
64+
void nullability_note(const char * _Nonnull const * _Nonnull parameter);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
Name: cfuncs
3+
Functions:
4+
- Name: nullability_note
5+
Parameters:
6+
- Position: 0
7+
Type: "const char * _Nullable const * _Nonnull"

0 commit comments

Comments
 (0)