Skip to content

[ClangImporter] Handle diagnostics about cast types in macros #13972

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 2 commits into from
Jan 19, 2018
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

| Contents |
| :--------------------- |
| [Swift 5.0](#swift-50) |
| [Swift 4.1](#swift-41) |
| [Swift 4.0](#swift-40) |
| [Swift 3.1](#swift-31) |
Expand All @@ -19,6 +20,16 @@ CHANGELOG

</details>

Swift 5.0
---------

* C macros containing casts are no longer imported to Swift if the type in the
cast is unavailable or deprecated, or produces some other diagnostic when
referenced. (These macros were already only imported under very limited
circumstances with very simple values, so this is unlikely to affect
real-world code.)


Swift 4.1
---------

Expand Down
9 changes: 8 additions & 1 deletion lib/ClangImporter/ImportMacro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "clang/AST/Expr.h"
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/DelayedDiagnostic.h"
#include "clang/Sema/Sema.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
#include "swift/AST/ASTContext.h"
Expand Down Expand Up @@ -361,10 +362,16 @@ static ValueDecl *importMacro(ClangImporter::Implementation &impl,
}
auto identifierName = identifierInfo->getName();
auto &identifier = impl.getClangASTContext().Idents.get(identifierName);

clang::sema::DelayedDiagnosticPool diagPool{
impl.getClangSema().DelayedDiagnostics.getCurrentPool()};
auto diagState = impl.getClangSema().DelayedDiagnostics.push(diagPool);
auto parsedType = impl.getClangSema().getTypeName(identifier,
clang::SourceLocation(),
/*scope*/nullptr);
if (parsedType) {
impl.getClangSema().DelayedDiagnostics.popWithoutEmitting(diagState);

if (parsedType && diagPool.empty()) {
castType = parsedType.get();
} else {
return nullptr;
Expand Down
6 changes: 6 additions & 0 deletions test/ClangImporter/macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,9 @@ func testRecursion() {
_ = RECURSION_IN_EXPR2 // expected-error {{use of unresolved identifier 'RECURSION_IN_EXPR2'}}
_ = RECURSION_IN_EXPR3 // expected-error {{use of unresolved identifier 'RECURSION_IN_EXPR3'}}
}

func testNulls() {
let _: Int = UNAVAILABLE_ONE // expected-error {{use of unresolved identifier 'UNAVAILABLE_ONE'}}
let _: Int = DEPRECATED_ONE // expected-error {{use of unresolved identifier 'DEPRECATED_ONE'}}
let _: Int = OKAY_TYPED_ONE // expected-error {{cannot convert value of type 'okay_t' (aka 'UInt32') to specified type 'Int'}}
}
9 changes: 9 additions & 0 deletions test/Inputs/clang-importer-sdk/usr/include/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,12 @@

#define RECURSION_WITH_EXPR3 RECURSION_WITH_EXPR3_HELPER + 1
#define RECURSION_WITH_EXPR3_HELPER RECURSION_WITH_EXPR3 + 1


// Casts with problematic types
#define UNAVAILABLE_ONE ((unavailable_t)1)
typedef unsigned unavailable_t __attribute__((unavailable));
#define DEPRECATED_ONE ((deprecated_t)1)
typedef unsigned deprecated_t __attribute__((deprecated));
#define OKAY_TYPED_ONE ((okay_t)1)
typedef unsigned okay_t;