Skip to content

[libSyntax] Explicitly pass source file length to c parse actions #36283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/swift-c/SyntaxParser/SwiftSyntaxParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,10 @@ swiftparse_parser_set_node_lookup(swiftparse_parser_t,
/// via the return value of \c swiftparse_parse_string.
///
/// \param source a null-terminated UTF8 string buffer.
/// \param len The length of the source string. This allows \p source to contain
/// intermediate null characters.
SWIFTPARSE_PUBLIC swiftparse_client_node_t
swiftparse_parse_string(swiftparse_parser_t, const char *source);
swiftparse_parse_string(swiftparse_parser_t, const char *source, size_t len);

/// Returns a constant string pointer for verification purposes.
///
Expand Down
15 changes: 8 additions & 7 deletions tools/libSwiftSyntaxParser/libSwiftSyntaxParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class SynParser {
setDiagnosticHandler(nullptr);
}

swiftparse_client_node_t parse(const char *source);
swiftparse_client_node_t parse(const char *source, size_t len);
};

class CLibParseActions : public SyntaxParseActions {
Expand Down Expand Up @@ -274,10 +274,10 @@ struct SynParserDiagConsumer: public DiagnosticConsumer {
}
};

swiftparse_client_node_t SynParser::parse(const char *source) {
swiftparse_client_node_t SynParser::parse(const char *source, size_t len) {
SourceManager SM;
unsigned bufID = SM.addNewSourceBuffer(
llvm::MemoryBuffer::getMemBuffer(source, "syntax_parse_source"));
unsigned bufID = SM.addNewSourceBuffer(llvm::MemoryBuffer::getMemBuffer(
StringRef(source, len), "syntax_parse_source"));
TypeCheckerOptions tyckOpts;
LangOptions langOpts;
langOpts.BuildSyntaxTree = true;
Expand Down Expand Up @@ -329,10 +329,11 @@ swiftparse_parser_set_node_lookup(swiftparse_parser_t c_parser,
parser->setNodeLookup(lookup);
}

swiftparse_client_node_t
swiftparse_parse_string(swiftparse_parser_t c_parser, const char *source) {
swiftparse_client_node_t swiftparse_parse_string(swiftparse_parser_t c_parser,
const char *source,
size_t len) {
SynParser *parser = static_cast<SynParser*>(c_parser);
return parser->parse(source);
return parser->parse(source, len);
}

const char* swiftparse_syntax_structure_versioning_identifier(void) {
Expand Down
17 changes: 9 additions & 8 deletions tools/swift-syntax-parser-test/swift-syntax-parser-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
8 changes: 4 additions & 4 deletions unittests/SyntaxParser/SyntaxParserTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@
using namespace swift;

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_node_lookup_t node_lookup) {
swiftparse_parser_t parser = swiftparse_parser_create();
swiftparse_parser_set_node_handler(parser, node_handler);
swiftparse_parser_set_node_lookup(parser, node_lookup);
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;
}

TEST(SwiftSyntaxParserTests, IncrementalParsing) {
const char *source1 =
StringRef source1 =
"func t1() { }\n"
"func t2() { }\n";
const char *source2 =
StringRef source2 =
"func t1renamed() { }\n"
"func t2() { }\n";

Expand Down