[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.
This commit is contained in:
Alex Hoppen
2021-03-04 18:19:39 +01:00
parent ffaf8679c0
commit dd9e831ba7
4 changed files with 24 additions and 20 deletions

View File

@@ -136,17 +136,18 @@ makeNode(const swiftparse_syntax_node_t *raw_node, StringRef source) {
}
static swiftparse_client_node_t
parse(const char *source, swiftparse_node_handler_t node_handler,
parse(StringRef source, swiftparse_node_handler_t node_handler,
swiftparse_diagnostic_handler_t diag_handler = nullptr) {
swiftparse_parser_t parser = swiftparse_parser_create();
swiftparse_parser_set_node_handler(parser, node_handler);
swiftparse_parser_set_diagnostic_handler(parser, diag_handler);
swiftparse_client_node_t top = swiftparse_parse_string(parser, source);
swiftparse_client_node_t top =
swiftparse_parse_string(parser, source.data(), source.size());
swiftparse_parser_dispose(parser);
return top;
}
static int dumpTree(const char *source) {
static int dumpTree(StringRef source) {
swiftparse_node_handler_t nodeHandler =
^swiftparse_client_node_t(const swiftparse_syntax_node_t *raw_node) {
return makeNode(raw_node, source);
@@ -216,7 +217,7 @@ static void printDiagInfo(const swiftparser_diagnostic_t diag,
}
}
static int dumpDiagnostics(const char* source, llvm::SourceMgr &SM,
static int dumpDiagnostics(StringRef source, llvm::SourceMgr &SM,
unsigned BufferId) {
swiftparse_node_handler_t nodeHandler =
^swiftparse_client_node_t(const swiftparse_syntax_node_t *raw_node) {
@@ -255,7 +256,7 @@ static void printTimeRecord(unsigned numInvoks, const TimeRecord &total,
OS << '\n';
}
static int timeParsing(const char *source, unsigned numInvoks) {
static int timeParsing(StringRef source, unsigned numInvoks) {
swiftparse_node_handler_t nodeHandler =
^swiftparse_client_node_t(const swiftparse_syntax_node_t *raw_node) {
return nullptr;
@@ -288,10 +289,10 @@ int main(int argc, char *argv[]) {
auto BufferId = SM.AddNewSourceBuffer(std::move(*fileBufOrErr), SMLoc());
switch (options::Action) {
case ActionType::DumpTree:
return dumpTree(source.data());
return dumpTree(source);
case ActionType::Time:
return timeParsing(source.data(), options::NumParses);
return timeParsing(source, options::NumParses);
case ActionType::Diagnostics:
return dumpDiagnostics(source.data(), SM, BufferId);
return dumpDiagnostics(source, SM, BufferId);
}
}