Skip to content

Commit fe6d5d5

Browse files
committed
NFC: Fix duplicate definition of macro warnings.
Centralize the `SWIFT_{BEGIN/END}_NULLABILITY_ANNOTATIONS` definitions in a new Nullability.h header to share.
1 parent dcf178f commit fe6d5d5

File tree

5 files changed

+56
-45
lines changed

5 files changed

+56
-45
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "swift/Basic/CBasicBridging.h"
1717
#include "swift/Basic/Compiler.h"
18+
#include "swift/Basic/Nullability.h"
1819

1920
// NOTE: DO NOT #include any stdlib headers here. e.g. <stdint.h>. Those are
2021
// part of "Darwin"/"Glibc" module, so when a Swift file imports this header,
@@ -488,7 +489,4 @@ _Bool Plugin_waitForNextMessage(PluginHandle handle, BridgedData *data);
488489

489490
SWIFT_END_NULLABILITY_ANNOTATIONS
490491

491-
#undef SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
492-
#undef SWIFT_END_NULLABILITY_ANNOTATIONS
493-
494492
#endif // SWIFT_C_AST_ASTBRIDGING_H

include/swift/Basic/BridgedSwiftObject.h

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#ifndef SWIFT_BASIC_BRIDGEDSWIFTOBJECT_H
1919
#define SWIFT_BASIC_BRIDGEDSWIFTOBJECT_H
2020

21+
#include "swift/Basic/Nullability.h"
22+
2123
#if defined(__OpenBSD__)
2224
#include <sys/stdint.h>
2325
#else
@@ -28,28 +30,6 @@
2830
#define __has_feature(feature) 0
2931
#endif
3032

31-
// TODO: These macro definitions are duplicated in Visibility.h. Move
32-
// them to a single file if we find a location that both Visibility.h and
33-
// BridgedSwiftObject.h can import.
34-
#if __has_feature(nullability)
35-
// Provide macros to temporarily suppress warning about the use of
36-
// _Nullable and _Nonnull.
37-
#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS \
38-
_Pragma("clang diagnostic push") \
39-
_Pragma("clang diagnostic ignored \"-Wnullability-extension\"")
40-
#define SWIFT_END_NULLABILITY_ANNOTATIONS \
41-
_Pragma("clang diagnostic pop")
42-
43-
#else
44-
// #define _Nullable and _Nonnull to nothing if we're not being built
45-
// with a compiler that supports them.
46-
#define _Nullable
47-
#define _Nonnull
48-
#define _Null_unspecified
49-
#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
50-
#define SWIFT_END_NULLABILITY_ANNOTATIONS
51-
#endif
52-
5333
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
5434

5535
typedef const void * _Nonnull SwiftMetatype;

include/swift/Basic/CBasicBridging.h

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,19 @@
1414
#define SWIFT_C_BASIC_BASICBRIDGING_H
1515

1616
#include "swift/Basic/Compiler.h"
17+
#include "swift/Basic/Nullability.h"
1718

1819
// NOTE: DO NOT #include any stdlib headers here. e.g. <stdint.h>. Those are
1920
// part of "Darwin"/"Glibc" module, so when a Swift file imports this header,
2021
// it causes importing the "Darwin"/"Glibc" overlay module. That violates
2122
// layering. i.e. Darwin overlay is created by Swift compiler.
2223

24+
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
25+
2326
#if __clang__
24-
// Provide macros to temporarily suppress warning about the use of
25-
// _Nullable and _Nonnull.
26-
#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS \
27-
_Pragma("clang diagnostic push") \
28-
_Pragma("clang diagnostic ignored \"-Wnullability-extension\"") \
29-
_Pragma("clang assume_nonnull begin")
30-
31-
#define SWIFT_END_NULLABILITY_ANNOTATIONS \
32-
_Pragma("clang diagnostic pop") _Pragma("clang assume_nonnull end")
33-
#else
34-
#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
35-
#define SWIFT_END_NULLABILITY_ANNOTATIONS
36-
#define _Nullable
37-
#define _Nonnull
27+
_Pragma("clang assume_nonnull begin")
3828
#endif
3929

40-
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
41-
4230
#ifdef __cplusplus
4331
extern "C" {
4432

@@ -119,9 +107,10 @@ void *JSON_array_pushNewValue(void *arrayPtr);
119107
}
120108
#endif
121109

122-
SWIFT_END_NULLABILITY_ANNOTATIONS
110+
#if __clang__
111+
_Pragma("clang assume_nonnull end")
112+
#endif
123113

124-
#undef SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
125-
#undef SWIFT_END_NULLABILITY_ANNOTATIONS
114+
SWIFT_END_NULLABILITY_ANNOTATIONS
126115

127116
#endif // SWIFT_C_BASIC_BASICBRIDGING_H

include/swift/Basic/Nullability.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===--- Nullability.h ----------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_BASIC_NULLABILITY_H
14+
#define SWIFT_BASIC_NULLABILITY_H
15+
16+
// TODO: These macro definitions are duplicated in Visibility.h. Move
17+
// them to a single file if we find a location that both Visibility.h and
18+
// BridgedSwiftObject.h can import.
19+
#if __has_feature(nullability)
20+
// Provide macros to temporarily suppress warning about the use of
21+
// _Nullable and _Nonnull.
22+
#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS \
23+
_Pragma("clang diagnostic push") \
24+
_Pragma("clang diagnostic ignored \"-Wnullability-extension\"")
25+
#define SWIFT_END_NULLABILITY_ANNOTATIONS _Pragma("clang diagnostic pop")
26+
27+
#else
28+
// #define _Nullable and _Nonnull to nothing if we're not being built
29+
// with a compiler that supports them.
30+
#define _Nullable
31+
#define _Nonnull
32+
#define _Null_unspecified
33+
#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
34+
#define SWIFT_END_NULLABILITY_ANNOTATIONS
35+
#endif
36+
37+
#endif

include/swift/Demangling/Errors.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#define SWIFT_DEMANGLING_ERRORS_H
2121

2222
#include "swift/Demangling/NamespaceMacros.h"
23-
#include "swift/shims/Visibility.h"
2423
#include <inttypes.h>
2524
#include <stdarg.h>
2625

@@ -44,6 +43,14 @@
4443
#endif
4544
#endif
4645

46+
#ifndef SWIFT_NORETURN
47+
#if __has_attribute(noreturn)
48+
#define SWIFT_NORETURN __attribute__((__noreturn__))
49+
#else
50+
#define SWIFT_NORETURN
51+
#endif
52+
#endif
53+
4754
namespace swift {
4855
namespace Demangle {
4956
SWIFT_BEGIN_INLINE_NAMESPACE

0 commit comments

Comments
 (0)