8000 [netlist] Do not abort when '/' or '(' is the last input character by 12yuuuu · Pull Request #5571 · google/xls · GitHub
[go: up one dir, main page]

Skip to content
Open
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
15 changes: 10 additions & 5 deletions xls/netlist/netlist_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "xls/netlist/netlist_parser.h"

#include <cctype>
#include <optional>
#include <string>

#include "absl/log/check.h"
Expand Down Expand Up @@ -85,8 +86,10 @@ char Scanner::PeekCharOrDie() const {
return text_[index_];
}

char Scanner::PeekChar2OrDie() const {
CHECK_GT(text_.size(), index_ + 1);
std::optional<char> Scanner::PeekChar2() const {
if (index_ + 1 >= text_.size()) {
return std::nullopt;
}
return text_[index_ + 1];
}

Expand Down Expand Up @@ -133,13 +136,15 @@ void Scanner::DropIgnoredChars() {
while (!AtEofInternal()) {
switch (PeekCharOrDie()) {
case '/': {
if (PeekChar2OrDie() == '/') {
// The '/' may be the last character of the input.
std::optional<char> next = PeekChar2();
if (next == '/') {
DropCharOrDie();
DropCharOrDie();
drop_to_eol_or_eof();
continue;
}
if (PeekChar2OrDie() == '*') {
if (next == '*') {
DropCharOrDie();
DropCharOrDie();
drop_to_block_comment_end_or_eof();
Expand All @@ -148,7 +153,7 @@ void Scanner::DropIgnoredChars() {
return;
}
case '(': {
if (PeekChar2OrDie() == '*') {
if (PeekChar2() == '*') {
DropCharOrDie();
DropCharOrDie();
drop_to_attr_end_or_eof();
Expand Down
3 changes: 2 additions & 1 deletion xls/netlist/netlist_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ class Scanner {
void DropIgnoredChars();

char PeekCharOrDie() const;
char PeekChar2OrDie() const;
// Returns the character after the current one, or nullopt if there is none.
std::optional<char> PeekChar2() const;
char PopCharOrDie();
void DropCharOrDie() { (void)PopCharOrDie(); }
Pos GetPos() const { return Pos{lineno_, colno_}; }
Expand Down
67 changes: 67 additions & 0 deletions xls/netlist/netlist_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -448,6 +449,72 @@ endmodule)";
TestAssignHelper(m);
}

// The scanner looks one character past a '/' or '(' to detect comments and
// attributes. These tests cover inputs where that character does not exist.
// See https://github.com/google/xls/issues/5563.
TEST(NetlistScannerTest, SlashAtEndOfInputIsError) {
Scanner lone("/");
EXPECT_FALSE(lone.AtEof());
EXPECT_THAT(lone.Pop(), StatusIs(absl::StatusCode::kUnimplemented,
HasSubstr("Unsupported character: '/'")));

Scanner trailing("a /");
XLS_ASSERT_OK_AND_ASSIGN(Token name, trailing.Pop());
EXPECT_EQ(name.value, "a");
EXPECT_FALSE(trailing.AtEof());
EXPECT_THAT(trailing.Pop(),
StatusIs(absl::StatusCode::kUnimplemented,
HasSubstr("Unsupported character: '/'")));
}

TEST(NetlistScannerTest, OpenParenAtEndOfInputIsToken) {
Scanner lone("(");
EXPECT_FALSE(lone.AtEof());
XLS_ASSERT_OK_AND_ASSIGN(Token lone_paren, lone.Pop());
EXPECT_EQ(lone_paren.kind, TokenKind::kOpenParen);
EXPECT_TRUE(lone.AtEof());

Scanner trailing("a (");
XLS_ASSERT_OK_AND_ASSIGN(Token name, trailing.Pop());
EXPECT_EQ(name.value, "a");
XLS_ASSERT_OK_AND_ASSIGN(Token trailing_paren, trailing.Pop());
EXPECT_EQ(trailing_paren.kind, TokenKind::kOpenParen);
EXPECT_TRUE(trailing.AtEof());
}

TEST(NetlistScannerTest, CommentOrAttributeAtEndOfInput) {
// None of these end with a newline, and some are not closed.
for (std::string_view text :
{"a //", "a // c", "a /*", "a /* c */", "a (*", "a (* c *)"}) {
Scanner scanner(text);
XLS_ASSERT_OK_AND_ASSIGN(Token token, scanner.Pop());
EXPECT_EQ(token.value, "a") << text;
EXPECT_TRUE(scanner.AtEof()) << text;
}
}

TEST(NetlistParserTest, SlashAtEndOfInputIsError) {
XLS_ASSERT_OK_AND_ASSIGN(CellLibrary cell_library, MakeFakeCellLibrary());
for (std::string_view netlist : {"/", "module main(); endmodule /"}) {
Scanner scanner(netlist);
EXPECT_THAT(Parser::ParseNetlist(&cell_library, &scanner),
StatusIs(absl::StatusCode::kUnimplemented,
HasSubstr("Unsupported character: '/'")))
<< netlist;
}
}

TEST(NetlistParserTest, OpenParenAtEndOfInputIsError) {
XLS_ASSERT_OK_AND_ASSIGN(CellLibrary cell_library, MakeFakeCellLibrary());
for (std::string_view netlist : {"(", "module main(); endmodule ("}) {
Scanner scanner(netlist);
EXPECT_THAT(Parser::ParseNetlist(&cell_library, &scanner),
StatusIs(absl::StatusCode::kInvalidArgument,
HasSubstr("Want keyword 'module'")))
<< netlist;
}
}

} // namespace
} // namespace rtl
} // namespace netlist
Expand Down
0