-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathInlinableText.cpp
164 lines (140 loc) · 6.29 KB
/
InlinableText.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//===---- InlinableText.cpp - Extract inlinable source text -----*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "InlinableText.h"
#include "swift/AST/ASTBridging.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTNode.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/Basic/Assertions.h"
#include "swift/Bridging/ASTGen.h"
#include "swift/Parse/Lexer.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/SmallString.h"
using namespace swift;
#if SWIFT_BUILD_SWIFT_SYNTAX
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
extern "C" BridgedStringRef
swift_ASTGen_extractInlinableText(BridgedASTContext ctx,
BridgedStringRef sourceText);
#pragma clang diagnostic pop
#else
/// Appends the textual contents of the provided source range, stripping
/// the contents of comments that appear in the source.
///
/// Given that comments are treated as whitespace, this also appends a
/// space or newline (depending if the comment was multi-line and itself
/// had newlines in the body) in place of the comment, to avoid fusing tokens
/// together.
static void appendRange(
SourceManager &sourceMgr, SourceLoc start, SourceLoc end,
SmallVectorImpl<char> &scratch) {
unsigned bufferID = sourceMgr.findBufferContainingLoc(start);
unsigned offset = sourceMgr.getLocOffsetInBuffer(start, bufferID);
unsigned endOffset = sourceMgr.getLocOffsetInBuffer(end, bufferID);
// Strip comments from the chunk before adding it by re-lexing the range.
LangOptions FakeLangOpts;
Lexer lexer(FakeLangOpts, sourceMgr, bufferID, nullptr, LexerMode::Swift,
HashbangMode::Disallowed, CommentRetentionMode::ReturnAsTokens,
offset, endOffset);
SourceLoc nonCommentStart = start;
Token token;
// Re-lex the range, and skip the full text of `tok::comment` tokens.
while (!token.is(tok::eof)) {
lexer.lex(token);
// Skip over #sourceLocation's in the file.
if (token.is(tok::pound_sourceLocation)) {
// Append the text leading up to the #sourceLocation
auto charRange = CharSourceRange(
sourceMgr, nonCommentStart, token.getLoc());
StringRef text = sourceMgr.extractText(charRange);
scratch.append(text.begin(), text.end());
// Skip to the right paren. We know the AST is already valid, so there's
// definitely a right paren.
while (!token.is(tok::r_paren)) {
lexer.lex(token);
}
nonCommentStart = Lexer::getLocForEndOfToken(sourceMgr, token.getLoc());
}
if (token.is(tok::comment)) {
// Grab the start of the full comment token (with leading trivia as well)
SourceLoc commentLoc = token.getLoc();
// Find the end of the token (with trailing trivia)
SourceLoc endLoc = Lexer::getLocForEndOfToken(sourceMgr, token.getLoc());
// The comment token's range includes leading/trailing whitespace, so trim
// whitespace and only strip the portions of the comment that are not
// whitespace.
CharSourceRange range = CharSourceRange(sourceMgr, commentLoc, endLoc);
StringRef fullTokenText = sourceMgr.extractText(range);
unsigned leadingWhitespace = fullTokenText.size() -
fullTokenText.ltrim().size();
if (leadingWhitespace > 0) {
commentLoc = commentLoc.getAdvancedLoc(leadingWhitespace);
}
unsigned trailingWhitespace = fullTokenText.size() -
fullTokenText.rtrim().size();
if (trailingWhitespace > 0) {
endLoc = endLoc.getAdvancedLoc(-trailingWhitespace);
}
// First, extract the text up to the start of the comment, including the
// whitespace.
auto charRange = CharSourceRange(sourceMgr, nonCommentStart, commentLoc);
StringRef text = sourceMgr.extractText(charRange);
scratch.append(text.begin(), text.end());
// Next, search through the comment text to see if it's a block comment
// with a newline. If so we need to re-insert a newline to avoid fusing
// multi-line tokens together.
auto commentTextRange = CharSourceRange(sourceMgr, commentLoc, endLoc);
StringRef commentText = sourceMgr.extractText(commentTextRange);
bool hasNewline = commentText.find_first_of("\n\r") != StringRef::npos;
// Use a newline as a filler character if the comment itself had a newline
// in it.
char filler = hasNewline ? '\n' : ' ';
// Append a single whitespace filler character, to avoid fusing tokens.
scratch.push_back(filler);
// Start the next region after the contents of the comment.
nonCommentStart = endLoc;
}
}
if (nonCommentStart.isValid() && nonCommentStart != end) {
auto charRange = CharSourceRange(sourceMgr, nonCommentStart, end);
StringRef text = sourceMgr.extractText(charRange);
scratch.append(text.begin(), text.end());
}
}
#endif // SWIFT_BUILD_SWIFT_SYNTAX
StringRef swift::extractInlinableText(ASTContext &ctx, ASTNode node,
SmallVectorImpl<char> &scratch) {
SourceManager &sourceMgr = ctx.SourceMgr;
#if SWIFT_BUILD_SWIFT_SYNTAX
CharSourceRange sourceTextRange =
Lexer::getCharSourceRangeFromSourceRange(
sourceMgr, node.getSourceRange());
StringRef sourceText = sourceMgr.extractText(sourceTextRange);
auto resultText = swift_ASTGen_extractInlinableText(ctx, sourceText);
scratch.clear();
scratch.insert(scratch.begin(),
resultText.unbridged().begin(),
resultText.unbridged().end());
swift_ASTGen_freeBridgedString(resultText);
return { scratch.data(), scratch.size() };
#else
// Get the full start and end of the provided node, as character locations.
SourceLoc start = node.getStartLoc();
SourceLoc end = Lexer::getLocForEndOfToken(sourceMgr, node.getEndLoc());
appendRange(sourceMgr, start, end, scratch);
return { scratch.data(), scratch.size() };
#endif
}