Skip to content

Commit

Permalink
[clang-format] Stop crashing when formatting Verilog (llvm#112043)
Browse files Browse the repository at this point in the history
The part of the code for parsing Verilog module instantiations
dereferenced a pointer without checking for null pointer. The pointer
may be null if the input is not complete and a line starts with a comma.
  • Loading branch information
sstwcw authored and DanielCChen committed Oct 16, 2024
1 parent c4a9197 commit 9ffcc24
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,8 @@ class AnnotatingParser {
// Case D.
if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
if (PrevParen->is(tok::r_paren) && PrevParen->MatchingParen &&
if (PrevParen && PrevParen->is(tok::r_paren) &&
PrevParen->MatchingParen &&
PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
return true;
}
Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Format/FormatTestVerilog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,7 @@ TEST_F(FormatTestVerilog, Instantiation) {
" .qbar(out1),\n"
" .clear(in1),\n"
" .preset(in2));");
verifyNoCrash(", ff1();");
// With breaking between instance ports disabled.
auto Style = getDefaultStyle();
Style.VerilogBreakBetweenInstancePorts = false;
Expand Down

0 comments on commit 9ffcc24

Please sign in to comment.