Skip to content

Commit

Permalink
Merge pull request #2198 from guwirth/error-recovery-msg
Browse files Browse the repository at this point in the history
C++ Parser can't read code. Declaration is skipped.
  • Loading branch information
guwirth authored Jun 18, 2021
2 parents ac8f4d0 + b42c161 commit 1ca187d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.tag.Tag;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.NoSqale;
import org.sonar.cxx.squidbridge.checks.SquidCheck;
import org.sonar.cxx.tag.Tag;

@Rule(
key = "ParsingErrorRecovery",
Expand All @@ -45,8 +45,16 @@ public void init() {

@Override
public void visitNode(AstNode node) {
getContext().createLineViolation(this, "C++ Parser can't read code. Declaration is skipped.",
node.getToken().getLine());
var msg = "C++ Parser can't read code. Declaration is skipped";
var lastToken = node.getLastToken();
if (lastToken != null) {
msg += String.format(" (last token='%s', line=%d, column=%d)",
lastToken.getValue(),
lastToken.getLine(),
lastToken.getColumn());
}
msg += ".";
getContext().createLineViolation(this, msg, node.getToken().getLine());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ public void test_syntax_error_recovery() throws UnsupportedEncodingException, IO
new ParsingErrorRecoveryCheck());

CheckMessagesVerifier.verify(file.getCheckMessages())
.next().atLine(2).withMessage("C++ Parser can't read code. Declaration is skipped.")
.next().atLine(6)
.withMessage("C++ Parser can't read code. Declaration is skipped (last token='}', line=9, column=0).")
.next().atLine(16)
.withMessage("C++ Parser can't read code. Declaration is skipped (last token='{', line=17, column=0).")
.next().atLine(19)
.withMessage("C++ Parser can't read code. Declaration is skipped (last token='++', line=20, column=6).")
.next().atLine(21)
.withMessage("C++ Parser can't read code. Declaration is skipped (last token='}', line=21, column=0).")
.noMore();
}

Expand Down
21 changes: 18 additions & 3 deletions cxx-checks/src/test/resources/checks/parsingError3.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
void f1(){int i1;}
void f2(){int i2 }
void f3(){int i3;}
void f1()
{
int i1;
}

void f2()
{
int i2
}

void f3()
{
int i3;
}

void f4()
{
int i4;
if( i4
i4++;
}

0 comments on commit 1ca187d

Please sign in to comment.