Skip to content

Commit dd9e831

Browse files
committed
[libSyntax] Explicitly pass source file length to c parse actions
Previously, the passed in source file was implicitly terminated by the first null character. The source file might, however, contain a null character in the middle and we shouldn't stop parsing at it.
1 parent ffaf867 commit dd9e831

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

Diff for: include/swift-c/SyntaxParser/SwiftSyntaxParser.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,10 @@ swiftparse_parser_set_node_lookup(swiftparse_parser_t,
218218
/// via the return value of \c swiftparse_parse_string.
219219
///
220220
/// \param source a null-terminated UTF8 string buffer.
221+
/// \param len The length of the source string. This allows \p source to contain
222+
/// intermediate null characters.
221223
SWIFTPARSE_PUBLIC swiftparse_client_node_t
222-
swiftparse_parse_string(swiftparse_parser_t, const char *source);
224+
swiftparse_parse_string(swiftparse_parser_t, const char *source, size_t len);
223225

224226
/// Returns a constant string pointer for verification purposes.
225227
///

Diff for: tools/libSwiftSyntaxParser/libSwiftSyntaxParser.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class SynParser {
9595
setDiagnosticHandler(nullptr);
9696
}
9797

98-
swiftparse_client_node_t parse(const char *source);
98+
swiftparse_client_node_t parse(const char *source, size_t len);
9999
};
100100

101101
class CLibParseActions : public SyntaxParseActions {
@@ -274,10 +274,10 @@ struct SynParserDiagConsumer: public DiagnosticConsumer {
274274
}
275275
};
276276

277-
swiftparse_client_node_t SynParser::parse(const char *source) {
277+
swiftparse_client_node_t SynParser::parse(const char *source, size_t len) {
278278
SourceManager SM;
279-
unsigned bufID = SM.addNewSourceBuffer(
280-
llvm::MemoryBuffer::getMemBuffer(source, "syntax_parse_source"));
279+
unsigned bufID = SM.addNewSourceBuffer(llvm::MemoryBuffer::getMemBuffer(
280+
StringRef(source, len), "syntax_parse_source"));
281281
TypeCheckerOptions tyckOpts;
282282
LangOptions langOpts;
283283
langOpts.BuildSyntaxTree = true;
@@ -329,10 +329,11 @@ swiftparse_parser_set_node_lookup(swiftparse_parser_t c_parser,
329329
parser->setNodeLookup(lookup);
330330
}
331331

332-
swiftparse_client_node_t
333-
swiftparse_parse_string(swiftparse_parser_t c_parser, const char *source) {
332+
swiftparse_client_node_t swiftparse_parse_string(swiftparse_parser_t c_parser,
333+
const char *source,
334+
size_t len) {
334335
SynParser *parser = static_cast<SynParser*>(c_parser);
335-
return parser->parse(source);
336+
return parser->parse(source, len);
336337
}
337338

338339
const char* swiftparse_syntax_structure_versioning_identifier(void) {

Diff for: tools/swift-syntax-parser-test/swift-syntax-parser-test.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,18 @@ makeNode(const swiftparse_syntax_node_t *raw_node, StringRef source) {
136136
}
137137

138138
static swiftparse_client_node_t
139-
parse(const char *source, swiftparse_node_handler_t node_handler,
139+
parse(StringRef source, swiftparse_node_handler_t node_handler,
140140
swiftparse_diagnostic_handler_t diag_handler = nullptr) {
141141
swiftparse_parser_t parser = swiftparse_parser_create();
142142
swiftparse_parser_set_node_handler(parser, node_handler);
143143
swiftparse_parser_set_diagnostic_handler(parser, diag_handler);
144-
swiftparse_client_node_t top = swiftparse_parse_string(parser, source);
144+
swiftparse_client_node_t top =
145+
swiftparse_parse_string(parser, source.data(), source.size());
145146
swiftparse_parser_dispose(parser);
146147
return top;
147148
}
148149

149-
static int dumpTree(const char *source) {
150+
static int dumpTree(StringRef source) {
150151
swiftparse_node_handler_t nodeHandler =
151152
^swiftparse_client_node_t(const swiftparse_syntax_node_t *raw_node) {
152153
return makeNode(raw_node, source);
@@ -216,7 +217,7 @@ static void printDiagInfo(const swiftparser_diagnostic_t diag,
216217
}
217218
}
218219

219-
static int dumpDiagnostics(const char* source, llvm::SourceMgr &SM,
220+
static int dumpDiagnostics(StringRef source, llvm::SourceMgr &SM,
220221
unsigned BufferId) {
221222
swiftparse_node_handler_t nodeHandler =
222223
^swiftparse_client_node_t(const swiftparse_syntax_node_t *raw_node) {
@@ -255,7 +256,7 @@ static void printTimeRecord(unsigned numInvoks, const TimeRecord &total,
255256
OS << '\n';
256257
}
257258

258-
static int timeParsing(const char *source, unsigned numInvoks) {
259+
static int timeParsing(StringRef source, unsigned numInvoks) {
259260
swiftparse_node_handler_t nodeHandler =
260261
^swiftparse_client_node_t(const swiftparse_syntax_node_t *raw_node) {
261262
return nullptr;
@@ -288,10 +289,10 @@ int main(int argc, char *argv[]) {
288289
auto BufferId = SM.AddNewSourceBuffer(std::move(*fileBufOrErr), SMLoc());
289290
switch (options::Action) {
290291
case ActionType::DumpTree:
291-
return dumpTree(source.data());
292+
return dumpTree(source);
292293
case ActionType::Time:
293-
return timeParsing(source.data(), options::NumParses);
294+
return timeParsing(source, options::NumParses);
294295
case ActionType::Diagnostics:
295-
return dumpDiagnostics(source.data(), SM, BufferId);
296+
return dumpDiagnostics(source, SM, BufferId);
296297
}
297298
}

Diff for: unittests/SyntaxParser/SyntaxParserTests.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@
1919
using namespace swift;
2020

2121
static swiftparse_client_node_t
22-
parse(const char *source, swiftparse_node_handler_t node_handler,
22+
parse(StringRef source, swiftparse_node_handler_t node_handler,
2323
swiftparse_node_lookup_t node_lookup) {
2424
swiftparse_parser_t parser = swiftparse_parser_create();
2525
swiftparse_parser_set_node_handler(parser, node_handler);
2626
swiftparse_parser_set_node_lookup(parser, node_lookup);
27-
swiftparse_client_node_t top = swiftparse_parse_string(parser, source);
27+
swiftparse_client_node_t top = swiftparse_parse_string(parser, source.data(), source.size());
2828
swiftparse_parser_dispose(parser);
2929
return top;
3030
}
3131

3232
TEST(SwiftSyntaxParserTests, IncrementalParsing) {
33-
const char *source1 =
33+
StringRef source1 =
3434
"func t1() { }\n"
3535
"func t2() { }\n";
36-
const char *source2 =
36+
StringRef source2 =
3737
"func t1renamed() { }\n"
3838
"func t2() { }\n";
3939

0 commit comments

Comments
 (0)