Skip to content

Commit d125425

Browse files
omochirintaro
authored andcommitted
[Syntax] test diagnostics in Lexer with libSyntax (#14954)
1 parent 95e1558 commit d125425

File tree

7 files changed

+26
-10
lines changed

7 files changed

+26
-10
lines changed

include/swift/Parse/Parser.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,8 @@ tokenizeWithTrivia(const LangOptions &LangOpts,
14441444
const SourceManager &SM,
14451445
unsigned BufferID,
14461446
unsigned Offset = 0,
1447-
unsigned EndOffset = 0);
1447+
unsigned EndOffset = 0,
1448+
DiagnosticEngine *Diags = nullptr);
14481449
} // end namespace swift
14491450

14501451
#endif

include/swift/Subsystems.h

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ namespace swift {
128128
std::vector<Token> tokenize(const LangOptions &LangOpts,
129129
const SourceManager &SM, unsigned BufferID,
130130
unsigned Offset = 0, unsigned EndOffset = 0,
131+
DiagnosticEngine *Diags = nullptr,
131132
bool KeepComments = true,
132133
bool TokenizeInterpolatedString = true,
133134
ArrayRef<Token> SplitTokens = ArrayRef<Token>());

lib/Parse/Parser.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ namespace swift {
4545
template <typename DF>
4646
void tokenize(const LangOptions &LangOpts, const SourceManager &SM,
4747
unsigned BufferID, unsigned Offset, unsigned EndOffset,
48+
DiagnosticEngine * Diags,
4849
CommentRetentionMode RetainComments,
4950
TriviaRetentionMode TriviaRetention,
5051
bool TokenizeInterpolatedString, ArrayRef<Token> SplitTokens,
@@ -56,7 +57,7 @@ void tokenize(const LangOptions &LangOpts, const SourceManager &SM,
5657
if (Offset == 0 && EndOffset == 0)
5758
EndOffset = SM.getRangeForBuffer(BufferID).getByteLength();
5859

59-
Lexer L(LangOpts, SM, BufferID, /*Diags=*/nullptr, /*InSILMode=*/false,
60+
Lexer L(LangOpts, SM, BufferID, Diags, /*InSILMode=*/false,
6061
RetainComments, TriviaRetention, Offset, EndOffset);
6162

6263
auto TokComp = [&](const Token &A, const Token &B) {
@@ -260,6 +261,7 @@ static void getStringPartTokens(const Token &Tok, const LangOptions &LangOpts,
260261

261262
std::vector<Token> NewTokens = swift::tokenize(LangOpts, SM, BufID,
262263
Offset, EndOffset,
264+
/*Diags=*/nullptr,
263265
/*KeepComments=*/true);
264266
Toks.insert(Toks.end(), NewTokens.begin(), NewTokens.end());
265267

@@ -278,12 +280,14 @@ static void getStringPartTokens(const Token &Tok, const LangOptions &LangOpts,
278280
std::vector<Token> swift::tokenize(const LangOptions &LangOpts,
279281
const SourceManager &SM, unsigned BufferID,
280282
unsigned Offset, unsigned EndOffset,
283+
DiagnosticEngine *Diags,
281284
bool KeepComments,
282285
bool TokenizeInterpolatedString,
283286
ArrayRef<Token> SplitTokens) {
284287
std::vector<Token> Tokens;
285288

286289
tokenize(LangOpts, SM, BufferID, Offset, EndOffset,
290+
Diags,
287291
KeepComments ? CommentRetentionMode::ReturnAsTokens
288292
: CommentRetentionMode::AttachToNextToken,
289293
TriviaRetentionMode::WithoutTrivia, TokenizeInterpolatedString,
@@ -299,13 +303,15 @@ std::vector<Token> swift::tokenize(const LangOptions &LangOpts,
299303
std::vector<std::pair<RC<syntax::RawSyntax>, syntax::AbsolutePosition>>
300304
swift::tokenizeWithTrivia(const LangOptions &LangOpts, const SourceManager &SM,
301305
unsigned BufferID, unsigned Offset,
302-
unsigned EndOffset) {
306+
unsigned EndOffset,
307+
DiagnosticEngine *Diags) {
303308
std::vector<std::pair<RC<syntax::RawSyntax>, syntax::AbsolutePosition>>
304309
Tokens;
305310
syntax::AbsolutePosition RunningPos;
306311

307312
tokenize(
308313
LangOpts, SM, BufferID, Offset, EndOffset,
314+
Diags,
309315
CommentRetentionMode::AttachToNextToken, TriviaRetentionMode::WithTrivia,
310316
/*TokenizeInterpolatedString=*/false,
311317
/*SplitTokens=*/ArrayRef<Token>(),

test/Syntax/lexer_invalid_nul.swift

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: cat %s | tr '\132' '\0' > %t
2+
// RUN: %swift-syntax-test -input-source-filename %t -dump-full-tokens 2>&1 >/dev/null | %FileCheck %t
3+
4+
// CHECK: 5:18: warning: nul character embedded in middle of file
5+
let a = 3 // nul(Z)

tools/swift-syntax-test/swift-syntax-test.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,13 @@ namespace {
112112
int getTokensFromFile(unsigned BufferID,
113113
LangOptions &LangOpts,
114114
SourceManager &SourceMgr,
115-
DiagnosticEngine &Diags,
115+
swift::DiagnosticEngine &Diags,
116116
std::vector<std::pair<RC<syntax::RawSyntax>,
117117
syntax::AbsolutePosition>> &Tokens) {
118-
Tokens = tokenizeWithTrivia(LangOpts, SourceMgr, BufferID);
119-
return Diags.hadAnyError() ? EXIT_FAILURE : EXIT_SUCCESS;
118+
Tokens = tokenizeWithTrivia(LangOpts, SourceMgr, BufferID,
119+
/*Offset=*/0, /*EndOffset=*/0,
120+
&Diags);
121+
return EXIT_SUCCESS;
120122
}
121123

122124

@@ -191,7 +193,7 @@ int doFullLexRoundTrip(const StringRef InputFilename) {
191193
TokAndPos.first->print(llvm::outs(), {});
192194
}
193195

194-
return Diags.hadAnyError() ? EXIT_FAILURE : EXIT_SUCCESS;
196+
return EXIT_SUCCESS;
195197
}
196198

197199
int doDumpRawTokenSyntax(const StringRef InputFilename) {
@@ -215,7 +217,7 @@ int doDumpRawTokenSyntax(const StringRef InputFilename) {
215217
llvm::outs() << "\n";
216218
}
217219

218-
return Diags.hadAnyError() ? EXIT_FAILURE : EXIT_SUCCESS;
220+
return EXIT_SUCCESS;
219221
}
220222

221223
int doFullParseRoundTrip(const char *MainExecutablePath,

unittests/Parse/LexerTests.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class LexerTest : public ::testing::Test {
3535
if (KeepEOF)
3636
Toks = tokenizeAndKeepEOF(BufID);
3737
else
38-
Toks = tokenize(LangOpts, SourceMgr, BufID, 0, 0, KeepComments);
38+
Toks = tokenize(LangOpts, SourceMgr, BufID, 0, 0, /*Diags=*/nullptr, KeepComments);
3939
EXPECT_EQ(ExpectedTokens.size(), Toks.size());
4040
for (unsigned i = 0, e = ExpectedTokens.size(); i != e; ++i) {
4141
EXPECT_EQ(ExpectedTokens[i], Toks[i].getKind()) << "i = " << i;
@@ -685,7 +685,7 @@ TEST_F(LexerTest, TokenizePlaceholder) {
685685
TEST_F(LexerTest, NoPlaceholder) {
686686
auto checkTok = [&](StringRef Source) {
687687
unsigned BufID = SourceMgr.addMemBufferCopy(Source);
688-
std::vector<Token> Toks = tokenize(LangOpts, SourceMgr, BufID, 0, 0, false);
688+
std::vector<Token> Toks = tokenize(LangOpts, SourceMgr, BufID, 0, 0, /*Diags=*/nullptr, false);
689689
ASSERT_FALSE(Toks.empty());
690690
EXPECT_NE(tok::identifier, Toks[0].getKind());
691691
};

unittests/Parse/TokenizerTests.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class TokenizerTest : public ::testing::Test {
9898
BufID,
9999
/* Offset = */ 0,
100100
/* EndOffset = */ 0,
101+
/* Diags = */nullptr,
101102
/* KeepComments = */ true,
102103
/* TokenizeInterpolatedString = */ true,
103104
SplitTokens);

0 commit comments

Comments
 (0)