Skip to content

Commit 7107e98

Browse files
author
Harlan Haskins
committed
Handle #sourceLocation
1 parent 692ce0d commit 7107e98

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

lib/AST/InlinableText.cpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,19 @@ static void appendRange(
216216
while (!token.is(tok::eof)) {
217217
lexer.lex(token);
218218

219+
// Skip over #sourceLocation's in the file.
220+
if (token.is(tok::pound_sourceLocation)) {
221+
lexer.lex(token);
222+
223+
// Skip from the left paren to the right paren.
224+
assert(token.is(tok::l_paren));
225+
while (!token.is(tok::r_paren)) {
226+
lexer.lex(token);
227+
}
228+
229+
nonCommentStart = Lexer::getLocForEndOfToken(sourceMgr, token.getLoc());
230+
}
231+
219232
if (token.is(tok::comment)) {
220233
// Grab the start of the full comment token (with leading trivia as well)
221234
SourceLoc commentLoc = token.getLoc();
@@ -224,31 +237,37 @@ static void appendRange(
224237
SourceLoc endLoc = Lexer::getLocForEndOfToken(sourceMgr, token.getLoc());
225238

226239
// The comment token's range includes leading/trailing whitespace, so trim
227-
// whitespace and only strip the portions of the comment that are not whitespace.
240+
// whitespace and only strip the portions of the comment that are not
241+
// whitespace.
228242
CharSourceRange range = CharSourceRange(sourceMgr, commentLoc, endLoc);
229243
StringRef fullTokenText = sourceMgr.extractText(range);
230-
unsigned leadingWhitespace = fullTokenText.size() - fullTokenText.ltrim().size();
244+
unsigned leadingWhitespace = fullTokenText.size() -
245+
fullTokenText.ltrim().size();
231246
if (leadingWhitespace > 0) {
232247
commentLoc = commentLoc.getAdvancedLoc(leadingWhitespace);
233248
}
234249

235-
unsigned trailingWhitespace = fullTokenText.size() - fullTokenText.rtrim().size();
250+
unsigned trailingWhitespace = fullTokenText.size() -
251+
fullTokenText.rtrim().size();
236252
if (trailingWhitespace > 0) {
237253
endLoc = endLoc.getAdvancedLoc(-trailingWhitespace);
238254
}
239255

240-
// First, extract the text up to the start of the comment, including the whitespace.
256+
// First, extract the text up to the start of the comment, including the
257+
// whitespace.
241258
auto charRange = CharSourceRange(sourceMgr, nonCommentStart, commentLoc);
242259
StringRef text = sourceMgr.extractText(charRange);
243260
scratch.append(text.begin(), text.end());
244261

245-
// Next, search through the comment text to see if it's a block comment with a newline. If so
246-
// we need to re-insert a newline to avoid fusing multi-line tokens together.
262+
// Next, search through the comment text to see if it's a block comment
263+
// with a newline. If so we need to re-insert a newline to avoid fusing
264+
// multi-line tokens together.
247265
auto commentTextRange = CharSourceRange(sourceMgr, commentLoc, endLoc);
248266
StringRef commentText = sourceMgr.extractText(commentTextRange);
249267
bool hasNewline = commentText.find_first_of("\n\r") != StringRef::npos;
250268

251-
// Use a newline as a filler character if the comment itself had a newline in it.
269+
// Use a newline as a filler character if the comment itself had a newline
270+
// in it.
252271
char filler = hasNewline ? '\n' : ' ';
253272

254273
// Append a single whitespace filler character, to avoid fusing tokens.

test/ModuleInterface/if-configs.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public func hasIfCompilerCheck(_ x: () -> Bool = {
141141
// CHECK-NEXT: let y = 2
142142
// CHECK: let a = 3
143143
// CHECK: let b = 2
144+
// CHECK-NOT: #sourceLocation
144145
// CHECK-NOT: #if
145146
// CHECK-NOT: comment!
146147
// CHECK: return true
@@ -171,6 +172,8 @@ public func hasComments() -> Bool {
171172
let a = 3
172173
/* test */let b = 2
173174

175+
#sourceLocation(file: "if-configs.swift", line: 200)
176+
174177
#if !NOT_PROVIDED
175178
// comment!
176179
return/* comment! */true/* comment! */

0 commit comments

Comments
 (0)