Skip to content

Commit dd3e1d9

Browse files
authored
Merge pull request #78898 from beccadax/whitespace-the-final-frontier
Correct newline emission in generated headers
2 parents 066dc94 + 60f6afb commit dd3e1d9

File tree

7 files changed

+58
-64
lines changed

7 files changed

+58
-64
lines changed

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,6 @@ class ModuleWriter {
488488
(inserted || !it->second.second))
489489
ClangValueTypePrinter::forwardDeclType(os, CD, printer);
490490
it->second = {EmissionState::Defined, true};
491-
os << '\n';
492491
printer.print(CD);
493492
return true;
494493
}
@@ -507,7 +506,6 @@ class ModuleWriter {
507506
forwardDeclareType(TD);
508507
});
509508

510-
os << '\n';
511509
printer.print(FD);
512510
return true;
513511
}
@@ -550,7 +548,6 @@ class ModuleWriter {
550548
return false;
551549

552550
seenTypes[PD] = { EmissionState::Defined, true };
553-
os << '\n';
554551
printer.print(PD);
555552
return true;
556553
}
@@ -576,7 +573,6 @@ class ModuleWriter {
576573
if (!forwardDeclareMemberTypes(ED->getAllMembers(), ED))
577574
return false;
578575

579-
os << '\n';
580576
printer.print(ED);
581577
return true;
582578
}
@@ -840,6 +836,7 @@ class ModuleWriter {
840836
while (!declsToWrite.empty()) {
841837
const Decl *D = declsToWrite.back();
842838
bool success = true;
839+
auto posBefore = os.tell();
843840

844841
if (auto ED = dyn_cast<EnumDecl>(D)) {
845842
success = writeEnum(ED);
@@ -870,7 +867,10 @@ class ModuleWriter {
870867

871868
if (success) {
872869
assert(declsToWrite.back() == D);
873-
os << "\n";
870+
// If we actually wrote something to the file, add a newline after it.
871+
// (As opposed to, for instance, an extension we decided to skip.)
872+
if (posBefore != os.tell())
873+
os << "\n";
874874
declsToWrite.pop_back();
875875
}
876876
}

lib/PrintAsClang/PrintAsClang.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ bool swift::printAsClangHeader(raw_ostream &os, ModuleDecl *M,
589589
clangHeaderSearchInfo, exposedModuleHeaderNames);
590590
});
591591
writePostImportPrologue(os, *M);
592-
emitObjCConditional(os, [&] { os << objcModuleContents.str(); });
592+
emitObjCConditional(os, [&] { os << "\n" << objcModuleContents.str(); });
593593
writeObjCEpilogue(os);
594594
emitCxxConditional(os, [&] {
595595
// FIXME: Expose Swift with @expose by default.

test/Interop/CxxToSwiftToCxx/bridge-cxx-struct-back-to-cxx.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ public struct Strct {
269269
// CHECK-NEXT: } // namespace swift
270270
// CHECK-EMPTY:
271271
// CHECK-NEXT: namespace UseCxxTy SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("UseCxxTy") {
272-
// CHECK-EMPTY:
273272
// CHECK-NEXT: SWIFT_INLINE_THUNK ns::NonTrivialTemplate<ns::TrivialinNS> retNonTrivial2() noexcept SWIFT_SYMBOL({{.*}}) SWIFT_WARN_UNUSED_RESULT {
274273
// CHECK-NEXT: alignas(alignof(ns::NonTrivialTemplate<ns::TrivialinNS>)) char storage[sizeof(ns::NonTrivialTemplate<ns::TrivialinNS>)];
275274
// CHECK-NEXT: auto * _Nonnull storageObjectPtr = reinterpret_cast<ns::NonTrivialTemplate<ns::TrivialinNS> *>(storage);

test/Interop/SwiftToCxx/expose-attr/expose-swift-decls-to-cxx.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,10 @@ public final class ExposedClass {
9898
// CHECK-NEXT: _impl::$s6Expose8exposed1yyF();
9999
// CHECK-NEXT: }
100100
// CHECK-EMPTY:
101-
// CHECK-EMPTY:
102101
// CHECK-NEXT: SWIFT_INLINE_THUNK void exposed3() noexcept SWIFT_SYMBOL("{{.*}}") {
103102
// CHECK-NEXT: _impl::$s6Expose8exposed3yyF();
104103
// CHECK-NEXT: }
105104
// CHECK-EMPTY:
106-
// CHECK-EMPTY:
107105
// CHECK-NEXT: SWIFT_INLINE_THUNK void exposed4() noexcept SWIFT_SYMBOL("{{.*}}") {
108106
// CHECK-NEXT: _impl::$s6Expose15exposed4RenamedyyF();
109107
// CHECK-NEXT: }

test/PrintAsCxx/empty.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
// CHECK-NEXT: #if __has_feature(objc_modules)
9595

9696
// CHECK-LABEL: #if defined(__OBJC__)
97+
// CHECK-EMPTY:
9798
// CHECK-NEXT: #endif
9899
// CHECK-NEXT: #if __has_attribute(external_source_symbol)
99100
// CHECK-NEXT: # pragma clang attribute pop

test/PrintAsObjC/Inputs/comments-expected-output.h

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ SWIFT_CLASS("_TtC8comments4A000")
22
@interface A000
33
@end
44

5-
65
/// Aaa. A010. Bbb.
76
SWIFT_CLASS("_TtC8comments21A010_AttachToEntities")
87
@interface A010_AttachToEntities
@@ -17,37 +16,32 @@ SWIFT_CLASS_PROPERTY(@property (nonatomic, class, readonly) NSInteger v2;)
1716
+ (NSInteger)v2 SWIFT_WARN_UNUSED_RESULT;
1817
@end
1918

20-
2119
/// Aaa. A013.
2220
SWIFT_PROTOCOL("_TtP8comments21A013_AttachToEntities_")
2321
@protocol A013_AttachToEntities
2422
@end
2523

26-
2724
SWIFT_CLASS("_TtC8comments10ATXHeaders")
2825
@interface ATXHeaders
2926
/// <h1>LEVEL ONE</h1>
3027
/// <h2>LEVEL TWO</h2>
3128
- (void)f0;
3229
@end
3330

34-
3531
SWIFT_CLASS("_TtC8comments10Attributes")
3632
@interface Attributes
3733
/// Here is an attribute:
3834
/// Attribute text
3935
- (void)f0;
4036
@end
4137

42-
4338
SWIFT_CLASS("_TtC8comments13AutomaticLink")
4439
@interface AutomaticLink
4540
/// And now for a URL.
4641
/// <a href="http://developer.apple.com/swift/">http://developer.apple.com/swift/</a>
4742
- (void)f0;
4843
@end
4944

50-
5145
SWIFT_CLASS("_TtC8comments10BlockQuote")
5246
@interface BlockQuote
5347
/// Aaa.
@@ -62,7 +56,6 @@ SWIFT_CLASS("_TtC8comments10BlockQuote")
6256
- (void)f0;
6357
@end
6458

65-
6659
SWIFT_CLASS("_TtC8comments5Brief")
6760
@interface Brief
6861
/// Aaa.
@@ -81,14 +74,12 @@ SWIFT_CLASS("_TtC8comments5Brief")
8174
- (void)f3;
8275
@end
8376

84-
8577
SWIFT_CLASS("_TtC8comments15ClosingComments")
8678
@interface ClosingComments
8779
/// Some comment. */
8880
- (void)closingComment;
8981
@end
9082

91-
9283
SWIFT_CLASS("_TtC8comments16ClosureContainer")
9384
@interface ClosureContainer
9485
/// Partially applies a binary operator.
@@ -156,7 +147,6 @@ SWIFT_CLASS("_TtC8comments16ClosureContainer")
156147
- (void)closureParameterOutlineOutlineWithA:(NSInteger)a combine:(SWIFT_NOESCAPE NSInteger (^ _Nonnull)(NSInteger, NSInteger))combine;
157148
@end
158149

159-
160150
SWIFT_CLASS("_TtC8comments9CodeBlock")
161151
@interface CodeBlock
162152
/// This is how you use this code.
@@ -169,15 +159,13 @@ SWIFT_CLASS("_TtC8comments9CodeBlock")
169159
- (void)f0;
170160
@end
171161

172-
173162
SWIFT_CLASS("_TtC8comments8Emphasis")
174163
@interface Emphasis
175164
/// Aaa <em>bbb</em> ccc.
176165
/// Aaa <em>bbb</em> ccc.
177166
- (void)f0;
178167
@end
179168

180-
181169
SWIFT_CLASS("_TtC8comments13EmptyComments")
182170
@interface EmptyComments
183171
///
@@ -192,7 +180,6 @@ SWIFT_CLASS("_TtC8comments13EmptyComments")
192180
- (void)f4;
193181
@end
194182

195-
196183
SWIFT_CLASS("_TtC8comments9Footnotes")
197184
@interface Footnotes
198185
/// Has some footnotes.
@@ -202,7 +189,6 @@ SWIFT_CLASS("_TtC8comments9Footnotes")
202189
- (void)f0;
203190
@end
204191

205-
206192
SWIFT_CLASS("_TtC8comments19HasThrowingFunction")
207193
@interface HasThrowingFunction
208194
/// Might throw something.
@@ -214,7 +200,6 @@ SWIFT_CLASS("_TtC8comments19HasThrowingFunction")
214200
- (void)f1:(NSInteger)x;
215201
@end
216202

217-
218203
SWIFT_CLASS("_TtC8comments15HorizontalRules")
219204
@interface HorizontalRules
220205
/// Briefly.
@@ -223,14 +208,12 @@ SWIFT_CLASS("_TtC8comments15HorizontalRules")
223208
- (void)f0;
224209
@end
225210

226-
227211
SWIFT_CLASS("_TtC8comments16ImplicitNameLink")
228212
@interface ImplicitNameLink
229213
/// <a href="https://www.apple.com/">Apple</a>
230214
- (void)f0;
231215
@end
232216

233-
234217
SWIFT_CLASS("_TtC8comments20IndentedBlockComment")
235218
@interface IndentedBlockComment
236219
/// Brief.
@@ -257,21 +240,18 @@ SWIFT_CLASS("_TtC8comments20IndentedBlockComment")
257240
- (void)f2;
258241
@end
259242

260-
261243
SWIFT_CLASS("_TtC8comments10InlineCode")
262244
@interface InlineCode
263245
/// Aaa <code>bbb</code> ccc.
264246
- (void)f0;
265247
@end
266248

267-
268249
SWIFT_CLASS("_TtC8comments10InlineLink")
269250
@interface InlineLink
270251
/// Aaa <a href="/path/to/something">bbb</a> ccc.
271252
- (void)f0;
272253
@end
273254

274-
275255
SWIFT_CLASS("_TtC8comments14MultiLineBrief")
276256
@interface MultiLineBrief
277257
/// Brief first line.
@@ -280,7 +260,6 @@ SWIFT_CLASS("_TtC8comments14MultiLineBrief")
280260
- (void)f0;
281261
@end
282262

283-
284263
SWIFT_CLASS("_TtC8comments11OrderedList")
285264
@interface OrderedList
286265
/// <ol>
@@ -295,7 +274,6 @@ SWIFT_CLASS("_TtC8comments11OrderedList")
295274
- (void)f0;
296275
@end
297276

298-
299277
/// \param x A number
300278
///
301279
SWIFT_CLASS("_TtC8comments15ParamAndReturns")
@@ -340,7 +318,6 @@ SWIFT_CLASS("_TtC8comments15ParamAndReturns")
340318
- (void)f4;
341319
@end
342320

343-
344321
SWIFT_CLASS("_TtC8comments16ParameterOutline")
345322
@interface ParameterOutline
346323
/// \param x A number
@@ -352,7 +329,6 @@ SWIFT_CLASS("_TtC8comments16ParameterOutline")
352329
- (void)f0:(NSInteger)x y:(NSInteger)y z:(NSInteger)z;
353330
@end
354331

355-
356332
SWIFT_CLASS("_TtC8comments22ParameterOutlineMiddle")
357333
@interface ParameterOutlineMiddle
358334
/// <ul>
@@ -372,12 +348,10 @@ SWIFT_CLASS("_TtC8comments22ParameterOutlineMiddle")
372348
- (void)f0:(NSInteger)x y:(NSInteger)y z:(NSInteger)z;
373349
@end
374350

375-
376351
SWIFT_CLASS("_TtC8comments13ReferenceLink")
377352
@interface ReferenceLink
378353
@end
379354

380-
381355
SWIFT_CLASS("_TtC8comments7Returns")
382356
@interface Returns
383357
///
@@ -386,15 +360,13 @@ SWIFT_CLASS("_TtC8comments7Returns")
386360
- (NSInteger)f0 SWIFT_WARN_UNUSED_RESULT;
387361
@end
388362

389-
390363
SWIFT_CLASS("_TtC8comments18SeparateParameters")
391364
@interface SeparateParameters
392365
/// \param x A number
393366
///
394367
- (void)f0:(NSInteger)x y:(NSInteger)y;
395368
@end
396369

397-
398370
SWIFT_CLASS("_TtC8comments13SetextHeaders")
399371
@interface SetextHeaders
400372
/// <h1>LEVEL ONE</h1>
@@ -406,15 +378,13 @@ SWIFT_CLASS("_TtC8comments13SetextHeaders")
406378
- (void)f0;
407379
@end
408380

409-
410381
SWIFT_CLASS("_TtC8comments14StrongEmphasis")
411382
@interface StrongEmphasis
412383
/// Aaa <em>bbb</em> ccc.
413384
/// Aaa <em>bbb</em> ccc.
414385
- (void)f0;
415386
@end
416387

417-
418388
SWIFT_CLASS("_TtC8comments13UnorderedList")
419389
@interface UnorderedList
420390
/// <ul>

0 commit comments

Comments
 (0)