Skip to content

Commit 6711faf

Browse files
authored
Merge pull request #23213 from nkcsgexi/doc-sanitize
SourceKit/InterfaceGen: sanitize Clang-specific doc-comment style when printing headers as Swift interface
2 parents 558a318 + cc395e7 commit 6711faf

File tree

5 files changed

+90
-43
lines changed

5 files changed

+90
-43
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,24 @@ class PrintAST : public ASTVisitor<PrintAST> {
487487
Printer << Ctx.SourceMgr.extractText(Range);
488488
}
489489

490+
static std::string sanitizeClangDocCommentStyle(StringRef Line) {
491+
static StringRef ClangStart = "/*!";
492+
static StringRef SwiftStart = "/**";
493+
auto Pos = Line.find(ClangStart);
494+
if (Pos == StringRef::npos)
495+
return Line.str();
496+
StringRef Segment[2];
497+
// The text before "/*!"
498+
Segment[0] = Line.substr(0, Pos);
499+
// The text after "/*!"
500+
Segment[1] = Line.substr(Pos).substr(ClangStart.size());
501+
// Only sanitize when "/*!" appears at the start of this line.
502+
if (Segment[0].trim().empty()) {
503+
return (llvm::Twine(Segment[0]) + SwiftStart + Segment[1]).str();
504+
}
505+
return Line.str();
506+
}
507+
490508
void printClangDocumentationComment(const clang::Decl *D) {
491509
const auto &ClangContext = D->getASTContext();
492510
const clang::RawComment *RC = ClangContext.getRawCommentForAnyRedecl(D);
@@ -507,10 +525,14 @@ class PrintAST : public ASTVisitor<PrintAST> {
507525
StringRef RawText =
508526
RC->getRawText(ClangContext.getSourceManager()).rtrim("\n\r");
509527
trimLeadingWhitespaceFromLines(RawText, WhitespaceToTrim, Lines);
510-
528+
bool FirstLine = true;
511529
for (auto Line : Lines) {
512-
Printer << ASTPrinter::sanitizeUtf8(Line);
530+
if (FirstLine)
531+
Printer << sanitizeClangDocCommentStyle(ASTPrinter::sanitizeUtf8(Line));
532+
else
533+
Printer << ASTPrinter::sanitizeUtf8(Line);
513534
Printer.printNewline();
535+
FirstLine = false;
514536
}
515537
}
516538

test/IDE/print_clang_decls.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
// TAG_DECLS_AND_TYPEDEFS: {{^}} init(x: Int32, y: Double){{$}}
3333
// TAG_DECLS_AND_TYPEDEFS: {{^}}}{{$}}
3434

35-
// TAG_DECLS_AND_TYPEDEFS: /*!
35+
// TAG_DECLS_AND_TYPEDEFS: /**
3636
// TAG_DECLS_AND_TYPEDEFS-NEXT: @keyword Foo2
3737
// TAG_DECLS_AND_TYPEDEFS-NEXT: */
3838
// TAG_DECLS_AND_TYPEDEFS-NEXT: {{^}}struct FooStruct2 {{{$}}

test/SourceKit/InterfaceGen/Inputs/header2.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11

22
typedef struct {
3+
/*! Some clang-style comments */
34
long width;
5+
/*!
6+
* Some clang-style comments
7+
*/
48
long height;
9+
510
} NUPixelSize;
611

712
#if __swift__ >= 50000

test/SourceKit/InterfaceGen/gen_clang_module.swift.response

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public func fooFuncNoreturn2() -> Never
136136
*/
137137
public func fooFuncWithComment1()
138138

139-
/*!
139+
/**
140140
Aaa. fooFuncWithComment2. Bbb.
141141
*/
142142
public func fooFuncWithComment2()
@@ -1695,7 +1695,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
16951695
key.length: 19
16961696
},
16971697
{
1698-
key.kind: source.lang.swift.syntaxtype.comment,
1698+
key.kind: source.lang.swift.syntaxtype.doccomment,
16991699
key.offset: 2680,
17001700
key.length: 42
17011701
},
@@ -5457,6 +5457,8 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
54575457
key.length: 26,
54585458
key.nameoffset: 2735,
54595459
key.namelength: 21,
5460+
key.docoffset: 2680,
5461+
key.doclength: 42,
54605462
key.attributes: [
54615463
{
54625464
key.offset: 2723,

test/SourceKit/InterfaceGen/gen_header.swift.header2.response

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11

22
public struct NUPixelSize {
33

4+
/** Some clang-style comments */
45
public var width: Int
56

7+
/**
8+
* Some clang-style comments
9+
*/
610
public var height: Int
711

812
public init()
@@ -27,108 +31,118 @@ public struct NUPixelSize {
2731
key.length: 11
2832
},
2933
{
30-
key.kind: source.lang.swift.syntaxtype.attribute.builtin,
34+
key.kind: source.lang.swift.syntaxtype.doccomment,
3135
key.offset: 34,
36+
key.length: 32
37+
},
38+
{
39+
key.kind: source.lang.swift.syntaxtype.attribute.builtin,
40+
key.offset: 71,
3241
key.length: 6
3342
},
3443
{
3544
key.kind: source.lang.swift.syntaxtype.keyword,
36-
key.offset: 41,
45+
key.offset: 78,
3746
key.length: 3
3847
},
3948
{
4049
key.kind: source.lang.swift.syntaxtype.identifier,
41-
key.offset: 45,
50+
key.offset: 82,
4251
key.length: 5
4352
},
4453
{
4554
key.kind: source.lang.swift.syntaxtype.typeidentifier,
46-
key.offset: 52,
55+
key.offset: 89,
4756
key.length: 3
4857
},
58+
{
59+
key.kind: source.lang.swift.syntaxtype.doccomment,
60+
key.offset: 98,
61+
key.length: 44
62+
},
4963
{
5064
key.kind: source.lang.swift.syntaxtype.attribute.builtin,
51-
key.offset: 61,
65+
key.offset: 147,
5266
key.length: 6
5367
},
5468
{
5569
key.kind: source.lang.swift.syntaxtype.keyword,
56-
key.offset: 68,
70+
key.offset: 154,
5771
key.length: 3
5872
},
5973
{
6074
key.kind: source.lang.swift.syntaxtype.identifier,
61-
key.offset: 72,
75+
key.offset: 158,
6276
key.length: 6
6377
},
6478
{
6579
key.kind: source.lang.swift.syntaxtype.typeidentifier,
66-
key.offset: 80,
80+
key.offset: 166,
6781
key.length: 3
6882
},
6983
{
7084
key.kind: source.lang.swift.syntaxtype.attribute.builtin,
71-
key.offset: 89,
85+
key.offset: 175,
7286
key.length: 6
7387
},
7488
{
7589
key.kind: source.lang.swift.syntaxtype.keyword,
76-
key.offset: 96,
90+
key.offset: 182,
7791
key.length: 4
7892
},
7993
{
8094
key.kind: source.lang.swift.syntaxtype.attribute.builtin,
81-
key.offset: 108,
95+
key.offset: 194,
8296
key.length: 6
8397
},
8498
{
8599
key.kind: source.lang.swift.syntaxtype.keyword,
86-
key.offset: 115,
100+
key.offset: 201,
87101
key.length: 4
88102
},
89103
{
90104
key.kind: source.lang.swift.syntaxtype.identifier,
91-
key.offset: 120,
105+
key.offset: 206,
92106
key.length: 5
93107
},
94108
{
95109
key.kind: source.lang.swift.syntaxtype.typeidentifier,
96-
key.offset: 127,
110+
key.offset: 213,
97111
key.length: 3
98112
},
99113
{
100114
key.kind: source.lang.swift.syntaxtype.identifier,
101-
key.offset: 132,
115+
key.offset: 218,
102116
key.length: 6
103117
},
104118
{
105119
key.kind: source.lang.swift.syntaxtype.typeidentifier,
106-
key.offset: 140,
120+
key.offset: 226,
107121
key.length: 3
108122
}
109123
]
110124
[
111125
{
112126
key.kind: source.lang.swift.ref.struct,
113-
key.offset: 52,
127+
key.offset: 89,
114128
key.length: 3,
115129
key.is_system: 1
116130
},
117131
{
118132
key.kind: source.lang.swift.ref.struct,
119-
key.offset: 80,
133+
key.offset: 166,
120134
key.length: 3,
121135
key.is_system: 1
122136
},
123137
{
124138
key.kind: source.lang.swift.ref.struct,
125-
key.offset: 127,
139+
key.offset: 213,
126140
key.length: 3,
127141
key.is_system: 1
128142
},
129143
{
130144
key.kind: source.lang.swift.ref.struct,
131-
key.offset: 140,
145+
key.offset: 226,
132146
key.length: 3,
133147
key.is_system: 1
134148
}
@@ -139,11 +153,11 @@ public struct NUPixelSize {
139153
key.accessibility: source.lang.swift.accessibility.public,
140154
key.name: "NUPixelSize",
141155
key.offset: 8,
142-
key.length: 138,
156+
key.length: 224,
143157
key.nameoffset: 15,
144158
key.namelength: 11,
145159
key.bodyoffset: 28,
146-
key.bodylength: 117,
160+
key.bodylength: 203,
147161
key.attributes: [
148162
{
149163
key.offset: 1,
@@ -157,14 +171,16 @@ public struct NUPixelSize {
157171
key.accessibility: source.lang.swift.accessibility.public,
158172
key.setter_accessibility: source.lang.swift.accessibility.public,
159173
key.name: "width",
160-
key.offset: 41,
174+
key.offset: 78,
161175
key.length: 14,
162176
key.typename: "Int",
163-
key.nameoffset: 45,
177+
key.nameoffset: 82,
164178
key.namelength: 5,
179+
key.docoffset: 34,
180+
key.doclength: 32,
165181
key.attributes: [
166182
{
167-
key.offset: 34,
183+
key.offset: 71,
168184
key.length: 6,
169185
key.attribute: source.decl.attribute.public
170186
}
@@ -175,14 +191,16 @@ public struct NUPixelSize {
175191
key.accessibility: source.lang.swift.accessibility.public,
176192
key.setter_accessibility: source.lang.swift.accessibility.public,
177193
key.name: "height",
178-
key.offset: 68,
194+
key.offset: 154,
179195
key.length: 15,
180196
key.typename: "Int",
181-
key.nameoffset: 72,
197+
key.nameoffset: 158,
182198
key.namelength: 6,
199+
key.docoffset: 98,
200+
key.doclength: 44,
183201
key.attributes: [
184202
{
185-
key.offset: 61,
203+
key.offset: 147,
186204
key.length: 6,
187205
key.attribute: source.decl.attribute.public
188206
}
@@ -192,13 +210,13 @@ public struct NUPixelSize {
192210
key.kind: source.lang.swift.decl.function.method.instance,
193211
key.accessibility: source.lang.swift.accessibility.public,
194212
key.name: "init()",
195-
key.offset: 96,
213+
key.offset: 182,
196214
key.length: 6,
197-
key.nameoffset: 96,
215+
key.nameoffset: 182,
198216
key.namelength: 6,
199217
key.attributes: [
200218
{
201-
key.offset: 89,
219+
key.offset: 175,
202220
key.length: 6,
203221
key.attribute: source.decl.attribute.public
204222
}
@@ -208,13 +226,13 @@ public struct NUPixelSize {
208226
key.kind: source.lang.swift.decl.function.method.instance,
209227
key.accessibility: source.lang.swift.accessibility.public,
210228
key.name: "init(width:height:)",
211-
key.offset: 115,
229+
key.offset: 201,
212230
key.length: 29,
213-
key.nameoffset: 115,
231+
key.nameoffset: 201,
214232
key.namelength: 29,
215233
key.attributes: [
216234
{
217-
key.offset: 108,
235+
key.offset: 194,
218236
key.length: 6,
219237
key.attribute: source.decl.attribute.public
220238
}
@@ -223,19 +241,19 @@ public struct NUPixelSize {
223241
{
224242
key.kind: source.lang.swift.decl.var.parameter,
225243
key.name: "width",
226-
key.offset: 120,
244+
key.offset: 206,
227245
key.length: 10,
228246
key.typename: "Int",
229-
key.nameoffset: 120,
247+
key.nameoffset: 206,
230248
key.namelength: 5
231249
},
232250
{
233251
key.kind: source.lang.swift.decl.var.parameter,
234252
key.name: "height",
235-
key.offset: 132,
253+
key.offset: 218,
236254
key.length: 11,
237255
key.typename: "Int",
238-
key.nameoffset: 132,
256+
key.nameoffset: 218,
239257
key.namelength: 6
240258
}
241259
]

0 commit comments

Comments
 (0)