Skip to content

Commit 7b3749d

Browse files
authored
Merge pull request #7722 from bitjammer/trivia-tests
[Syntax] Add Trivia C++ unit tests
2 parents 5c01d54 + 733988c commit 7b3749d

File tree

4 files changed

+310
-19
lines changed

4 files changed

+310
-19
lines changed

include/swift/Syntax/Trivia.h

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ enum class TriviaKind {
120120

121121
/// A backtick '`' character, used to escape identifiers.
122122
Backtick,
123-
124-
/// A semicolon ';' character, used for delimiting statements.
125-
Semicolon
126123
};
127124

128125
/// A contiguous stretch of a single kind of trivia. The constiuent part of
@@ -187,11 +184,6 @@ struct TriviaPiece {
187184
return TriviaPiece {TriviaKind::Backtick, 1, OwnedString{}};
188185
}
189186

190-
/// Return a piece of trivia for some number of semicolons in a row.
191-
static TriviaPiece semicolon(unsigned Count) {
192-
return TriviaPiece {TriviaKind::Semicolon, Count, OwnedString{}};
193-
}
194-
195187
void accumulateAbsolutePosition(AbsolutePosition &Pos) const;
196188

197189
/// Print a debug representation of this trivia piece to the provided output
@@ -200,6 +192,16 @@ struct TriviaPiece {
200192

201193
/// Print this piece of trivia to the provided output stream.
202194
void print(llvm::raw_ostream &OS) const;
195+
196+
bool operator==(const TriviaPiece &Other) const {
197+
return Kind == Other.Kind &&
198+
Count == Other.Count &&
199+
Text.str().compare(Other.Text.str()) == 0;
200+
}
201+
202+
bool operator!=(const TriviaPiece &Other) const {
203+
return !(*this == Other);
204+
}
203205
};
204206

205207
using TriviaList = std::deque<TriviaPiece>;
@@ -233,20 +235,23 @@ struct Trivia {
233235
///
234236
/// Precondition: !empty()
235237
const TriviaPiece &front() const {
238+
assert(!empty());
236239
return Pieces.front();
237240
}
238241

239242
/// Return a reference to the last piece.
240243
///
241244
/// Precondition: !empty()
242245
const TriviaPiece &back() const {
246+
assert(!empty());
243247
return Pieces.back();
244248
}
245249

246250
/// Remove the last piece from the Trivia collection.
247251
///
248252
/// Precondition: !empty()
249253
void pop_back() {
254+
assert(!empty());
250255
Pieces.pop_back();
251256
}
252257

@@ -283,41 +288,74 @@ struct Trivia {
283288
return find(Kind) != end();
284289
}
285290

291+
bool operator==(const Trivia &Other) const {
292+
if (Pieces.size() != Other.size()) {
293+
return false;
294+
}
295+
296+
for (size_t i = 0; i < Pieces.size(); ++i) {
297+
if (Pieces[i] != Other.Pieces[i]) {
298+
return false;
299+
}
300+
}
301+
302+
return true;
303+
}
304+
305+
bool operator!=(const Trivia &Other) const {
306+
return !(*this == Other);
307+
}
308+
286309
/// Return a collection of trivia of some number of space characters in a row.
287310
static Trivia spaces(unsigned Count) {
311+
if (Count == 0) {
312+
return {};
313+
}
288314
return {{ TriviaPiece {TriviaKind::Space, Count, OwnedString{}} }};
289315
}
290316

291317
/// Return a collection of trivia of some number of tab characters in a row.
292318
static Trivia tabs(unsigned Count) {
319+
if (Count == 0) {
320+
return {};
321+
}
293322
return {{ TriviaPiece {TriviaKind::Tab, Count, OwnedString{}} }};
294323
}
295324

296325
/// Return a collection of trivia of some number of newline characters
297326
// in a row.
298327
static Trivia newlines(unsigned Count) {
328+
if (Count == 0) {
329+
return {};
330+
}
299331
return {{ TriviaPiece {TriviaKind::Newline, Count, OwnedString{}} }};
300332
}
301333

302334
/// Return a collection of trivia with a single line of ('//')
303335
// developer comment.
304336
static Trivia lineComment(const OwnedString Text) {
337+
assert(Text.str().startswith("//"));
305338
return {{ TriviaPiece {TriviaKind::LineComment, 1, Text} }};
306339
}
307340

308341
/// Return a collection of trivia with a block comment ('/* ... */')
309342
static Trivia blockComment(const OwnedString Text) {
343+
assert(Text.str().startswith("/*"));
344+
assert(Text.str().endswith("*/"));
310345
return {{ TriviaPiece {TriviaKind::BlockComment, 1, Text} }};
311346
}
312347

313348
/// Return a collection of trivia with a single line of ('///') doc comment.
314349
static Trivia docLineComment(const OwnedString Text) {
350+
assert(Text.str().startswith("///"));
315351
return {{ TriviaPiece {TriviaKind::DocLineComment, 1, Text} }};
316352
}
317353

318354
/// Return a collection of trivia with a documentation block
319355
// comment ('/** ... */')
320356
static Trivia docBlockComment(const OwnedString Text) {
357+
assert(Text.str().startswith("/**"));
358+
assert(Text.str().endswith("*/"));
321359
return {{ TriviaPiece {TriviaKind::DocBlockComment, 1, Text} }};
322360
}
323361

@@ -326,11 +364,6 @@ struct Trivia {
326364
static Trivia backtick() {
327365
return {{ TriviaPiece {TriviaKind::Backtick, 1, OwnedString{}} }};
328366
}
329-
330-
/// Return a piece of trivia for some number of semicolons in a row.
331-
static Trivia semicolon() {
332-
return {{ TriviaPiece {TriviaKind::Semicolon, 1, OwnedString{}} }};
333-
}
334367
};
335368
}
336369
}

lib/Syntax/Trivia.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ void TriviaPiece::dump(llvm::raw_ostream &OS, unsigned Indent) const {
8080
case TriviaKind::Backtick:
8181
OS << "backtick " << Count;
8282
break;
83-
case TriviaKind::Semicolon:
84-
OS << "semicolon " << Count;
85-
break;
8683
}
8784
OS << ')';
8885
}
@@ -100,7 +97,6 @@ void TriviaPiece::accumulateAbsolutePosition(AbsolutePosition &Pos) const {
10097
break;
10198
case TriviaKind::Space:
10299
case TriviaKind::Backtick:
103-
case TriviaKind::Semicolon:
104100
case TriviaKind::Tab:
105101
case TriviaKind::VerticalTab:
106102
case TriviaKind::Formfeed:
@@ -135,8 +131,6 @@ void TriviaPiece::print(llvm::raw_ostream &OS) const {
135131
case TriviaKind::Backtick:
136132
printRepeated(OS, '`', Count);
137133
break;
138-
case TriviaKind::Semicolon:
139-
printRepeated(OS, ';', Count);
140134
}
141135
}
142136

unittests/Syntax/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_swift_unittest(SwiftSyntaxTests
55
RawSyntaxTests.cpp
66
StmtSyntaxTests.cpp
77
ThreadSafeCachingTests.cpp
8+
TriviaTests.cpp
89
TypeSyntaxTests.cpp)
910

1011
target_link_libraries(SwiftSyntaxTests

0 commit comments

Comments
 (0)