Skip to content

Commit b245061

Browse files
authored
Merge pull request #12551 from nkcsgexi/libsyntax-test
2 parents 5c8fefc + e0dfa61 commit b245061

File tree

9 files changed

+77
-7
lines changed

9 files changed

+77
-7
lines changed

include/swift/Syntax/RawSyntax.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ enum class SourcePresence {
184184
Missing,
185185
};
186186

187+
/// The print option to specify when printing a raw syntax node.
188+
struct SyntaxPrintOptions {
189+
bool PrintSyntaxKind = false;
190+
};
191+
187192
/// RawSyntax - the strictly immutable, shared backing nodes for all syntax.
188193
///
189194
/// This is implementation detail - do not expose it in public API.
@@ -298,7 +303,7 @@ struct RawSyntax : public llvm::ThreadSafeRefCountedBase<RawSyntax> {
298303
}
299304

300305
/// Print this piece of syntax recursively.
301-
void print(llvm::raw_ostream &OS) const;
306+
void print(llvm::raw_ostream &OS, SyntaxPrintOptions Opts) const;
302307

303308
/// Dump this piece of syntax recursively for debugging or testing.
304309
void dump() const;

include/swift/Syntax/Syntax.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class Syntax {
156156
bool isPresent() const;
157157

158158
/// Print the syntax node with full fidelity to the given output stream.
159-
void print(llvm::raw_ostream &OS) const;
159+
void print(llvm::raw_ostream &OS, SyntaxPrintOptions Opts = SyntaxPrintOptions()) const;
160160

161161
/// Print a debug representation of the syntax node to the given output stream
162162
/// and indentation level.

include/swift/Syntax/SyntaxKind.h.gyb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ enum class SyntaxKind {
4747

4848
void dumpSyntaxKind(llvm::raw_ostream &os, const SyntaxKind kind);
4949

50+
/// Whether this kind is a syntax collection.
51+
bool isCollectionKind(SyntaxKind Kind);
52+
5053
} // end namespace syntax
5154

5255
namespace json {

lib/Syntax/RawSyntax.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,40 @@
2020
using llvm::dyn_cast;
2121
using namespace swift::syntax;
2222

23-
void RawSyntax::print(llvm::raw_ostream &OS) const {
23+
namespace {
24+
static bool isTrivialSyntaxKind(SyntaxKind Kind) {
25+
if (isCollectionKind(Kind))
26+
return true;
27+
switch(Kind) {
28+
case SyntaxKind::SourceFile:
29+
case SyntaxKind::TopLevelCodeDecl:
30+
case SyntaxKind::ExpressionStmt:
31+
return true;
32+
default:
33+
return false;
34+
}
35+
}
36+
} // end of anonymous namespace
37+
void RawSyntax::print(llvm::raw_ostream &OS, SyntaxPrintOptions Opts) const {
38+
const bool PrintKind = Opts.PrintSyntaxKind && !isToken() &&
39+
!isTrivialSyntaxKind(Kind);
40+
if (PrintKind) {
41+
OS << "<";
42+
dumpSyntaxKind(OS, Kind);
43+
OS << ">";
44+
}
45+
2446
if (const auto Tok = dyn_cast<RawTokenSyntax>(this)) {
2547
Tok->print(OS);
2648
}
2749

2850
for (const auto &LE : Layout) {
29-
LE->print(OS);
51+
LE->print(OS, Opts);
52+
}
53+
if (PrintKind) {
54+
OS << "</";
55+
dumpSyntaxKind(OS, Kind);
56+
OS << ">";
3057
}
3158
}
3259

lib/Syntax/Syntax.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ SyntaxKind Syntax::getKind() const {
2424
return getRaw()->Kind;
2525
}
2626

27-
void Syntax::print(llvm::raw_ostream &OS) const {
28-
getRaw()->print(OS);
27+
void Syntax::print(llvm::raw_ostream &OS, SyntaxPrintOptions Opts) const {
28+
getRaw()->print(OS, Opts);
2929
}
3030

3131
void Syntax::dump() const {

lib/Syntax/SyntaxKind.cpp.gyb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,18 @@ void dumpSyntaxKind(llvm::raw_ostream &os, const SyntaxKind kind) {
3838
}
3939
}
4040

41+
bool isCollectionKind(SyntaxKind Kind) {
42+
switch(Kind) {
43+
% for node in SYNTAX_NODES:
44+
% if node.is_syntax_collection():
45+
case SyntaxKind::${node.syntax_kind}:
46+
% end
47+
% end
48+
return true;
49+
default:
50+
return false;
51+
}
52+
}
53+
4154
} // end namespace syntax
4255
} // end namespace swift
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<StringLiteralExpr>// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t
2+
// RUN: diff %t %s
3+
// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -print-node-kind > %t.withkinds
4+
// RUN: diff %t.withkinds %S/Outputs/round_trip_parse_gen.swift.withkinds
5+
6+
"String Literal"</StringLiteralExpr><StringLiteralExpr>
7+
8+
""</StringLiteralExpr><IntegerLiteralExpr>
9+
10+
/*comments*/+3 // comments</IntegerLiteralExpr>

test/Syntax/round_trip_parse_gen.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t
22
// RUN: diff %t %s
3+
// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -print-node-kind > %t.withkinds
4+
// RUN: diff %t.withkinds %S/Outputs/round_trip_parse_gen.swift.withkinds
35

46
"String Literal"
57

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "llvm/Support/raw_ostream.h"
3737

3838
using namespace swift;
39+
using namespace swift::syntax;
3940
using llvm::StringRef;
4041

4142
enum class ActionType {
@@ -48,6 +49,7 @@ enum class ActionType {
4849
};
4950

5051
namespace options {
52+
static llvm::cl::OptionCategory Category("swift-syntax-test Options");
5153
static llvm::cl::opt<ActionType>
5254
Action(llvm::cl::desc("Action (required):"),
5355
llvm::cl::init(ActionType::None),
@@ -76,6 +78,12 @@ Action(llvm::cl::desc("Action (required):"),
7678
static llvm::cl::opt<std::string>
7779
InputSourceFilename("input-source-filename",
7880
llvm::cl::desc("Path to the input .swift file"));
81+
82+
static llvm::cl::opt<bool>
83+
PrintNodeKind("print-node-kind",
84+
llvm::cl::desc("To print syntax node kind"),
85+
llvm::cl::cat(Category),
86+
llvm::cl::init(false));
7987
} // end namespace options
8088

8189
namespace {
@@ -273,7 +281,9 @@ int dumpParserGen(const char *MainExecutablePath,
273281
const StringRef InputFileName) {
274282
CompilerInstance Instance;
275283
SourceFile *SF = getSourceFile(Instance, InputFileName, MainExecutablePath);
276-
SF->getSyntaxRoot().print(llvm::outs());
284+
SyntaxPrintOptions Opts;
285+
Opts.PrintSyntaxKind = options::PrintNodeKind;
286+
SF->getSyntaxRoot().print(llvm::outs(), Opts);
277287
return 0;
278288
}
279289

0 commit comments

Comments
 (0)