From b42c1616ff55e2c36bf3289091d8ab1e969a9fbc Mon Sep 17 00:00:00 2001 From: guwirth Date: Wed, 16 Jun 2021 18:53:37 +0200 Subject: [PATCH] C++ Parser can't read code. Declaration is skipped. - Add last known token with position. Problem is most likely before/after this token --- .../error/ParsingErrorRecoveryCheck.java | 14 ++++++++++--- .../error/ParsingErrorRecoveryCheckTest.java | 9 +++++++- .../test/resources/checks/parsingError3.cc | 21 ++++++++++++++++--- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/cxx-checks/src/main/java/org/sonar/cxx/checks/error/ParsingErrorRecoveryCheck.java b/cxx-checks/src/main/java/org/sonar/cxx/checks/error/ParsingErrorRecoveryCheck.java index 05e286dec9..6a81ae9650 100644 --- a/cxx-checks/src/main/java/org/sonar/cxx/checks/error/ParsingErrorRecoveryCheck.java +++ b/cxx-checks/src/main/java/org/sonar/cxx/checks/error/ParsingErrorRecoveryCheck.java @@ -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", @@ -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()); } } diff --git a/cxx-checks/src/test/java/org/sonar/cxx/checks/error/ParsingErrorRecoveryCheckTest.java b/cxx-checks/src/test/java/org/sonar/cxx/checks/error/ParsingErrorRecoveryCheckTest.java index c348d9bdd8..67f4e17223 100644 --- a/cxx-checks/src/test/java/org/sonar/cxx/checks/error/ParsingErrorRecoveryCheckTest.java +++ b/cxx-checks/src/test/java/org/sonar/cxx/checks/error/ParsingErrorRecoveryCheckTest.java @@ -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(); } diff --git a/cxx-checks/src/test/resources/checks/parsingError3.cc b/cxx-checks/src/test/resources/checks/parsingError3.cc index bf42de56e7..b6194508cc 100644 --- a/cxx-checks/src/test/resources/checks/parsingError3.cc +++ b/cxx-checks/src/test/resources/checks/parsingError3.cc @@ -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++; +}