Skip to content

Commit 20e1ac6

Browse files
committed
PrintAsClang: Ignore nullability attributes for C clients
In C mode we still print nullability attributes. Don't let clang warn on them and ignore the attribute if the C compiler doesn't know them.
1 parent a55b190 commit 20e1ac6

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lib/PrintAsClang/PrintAsClang.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,21 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
218218

219219
static_assert(SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4,
220220
"need to add SIMD typedefs here if max elements is increased");
221+
222+
if (ctx.LangOpts.hasFeature(Feature::CDecl)) {
223+
// For C compilers which don’t support nullability attributes, ignore them;
224+
// for ones which do, suppress warnings about them being an extension.
225+
out << "#if !__has_feature(nullability)\n"
226+
"# define _Nonnull\n"
227+
"# define _Nullable\n"
228+
"# define _Null_unspecified\n"
229+
"#elif !defined(__OBJC__)\n"
230+
"# pragma clang diagnostic ignored \"-Wnullability-extension\"\n"
231+
"#endif\n"
232+
"#if !__has_feature(nullability_nullable_result)\n"
233+
"# define _Nullable_result _Nullable\n"
234+
"#endif\n";
235+
}
221236
}
222237

223238
static int compareImportModulesByName(const ImportModuleTy *left,

0 commit comments

Comments
 (0)