Skip to content

Commit

Permalink
make highlighting more robust against parsing errors
Browse files Browse the repository at this point in the history
- add a raw string literal to reproduce the error
- raw string literal detection is erroneous
- fix raw string literal issue (close #944)
  • Loading branch information
guwirth committed Sep 11, 2016
1 parent 9b6be47 commit c07c708
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,30 +89,35 @@ private boolean readString(CodeReader code) {

private boolean readRawString(CodeReader code) {
// "delimiter( raw_character* )delimiter"
char charAt;
index++;
while (code.charAt(index) != '(') { // delimiter
if (code.charAt(index) == EOF) {
while ((charAt = code.charAt(index)) != '(') { // delimiter in front of (
if (charAt == EOF) {
return false;
}
sb.append(code.charAt(index));
sb.append(charAt);
index++;
}
String delimiter = sb.toString();
do {
sb.setLength(0);
while (code.charAt(index) != ')') { // raw_character*
if (code.charAt(index) == EOF) {
while ((charAt = code.charAt(index)) != ')') { // raw_character*
if (charAt == EOF) {
return false;
}
index++;
}
index++;
while (code.charAt(index) != '"') { // delimiter
if (code.charAt(index) == EOF) {
while ((charAt = code.charAt(index)) != '"') { // delimiter after )
if (charAt == EOF) {
return false;
}
sb.append(code.charAt(index));
sb.append(charAt);
index++;

if( sb.length() > delimiter.length() ) {
break;
}
}
} while (!sb.toString().equals(delimiter));
sb.setLength(0);
Expand Down
4 changes: 4 additions & 0 deletions cxx-squid/src/test/java/org/sonar/cxx/lexer/CxxLexerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@ public void rawstring_literals() {
assertThat("raw string: complex example",
lexer.lex("R\"X*X(A C++11 raw string literal can be specified like this: R\"(This is my raw string)\" )X*X\""),
hasToken("R\"X*X(A C++11 raw string literal can be specified like this: R\"(This is my raw string)\" )X*X\"", CxxTokenType.STRING));

assertThat("raw string: regex sample",
lexer.lex("R\"([.^$|()\\[\\]{}*+?\\\\])\""),
hasToken("R\"([.^$|()\\[\\]{}*+?\\\\])\"", CxxTokenType.STRING));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.squidbridge.SquidAstVisitor;
import org.sonar.cxx.api.CxxTokenType;
import org.sonar.cxx.api.CxxKeyword;

public class CxxHighlighter extends SquidAstVisitor<Grammar> implements AstAndTokenVisitor {

private static final Logger LOG = Loggers.get(CxxHighlighter.class);

private NewHighlighting newHighlighting;
private final SensorContext context;

Expand Down Expand Up @@ -88,7 +92,7 @@ public CommentLocation(Token token) {
private class PreprocessorDirectiveLocation extends TokenLocation {

private final Pattern r = Pattern.compile("^[ \t]*#[ \t]*\\w+");

PreprocessorDirectiveLocation(Token token) {
super(token);
Matcher m = r.matcher(token.getValue());
Expand All @@ -99,7 +103,7 @@ private class PreprocessorDirectiveLocation extends TokenLocation {
}
}
}

public CxxHighlighter(SensorContext context) {
this.context = context;
}
Expand Down Expand Up @@ -142,7 +146,12 @@ public void visitToken(Token token) {
}

private void highlight(TokenLocation location, TypeOfText typeOfText) {
newHighlighting.highlight(location.startLine(), location.startLineOffset(), location.endLine(), location.endLineOffset(), typeOfText);
try {
newHighlighting.highlight(location.startLine(), location.startLineOffset(), location.endLine(), location.endLineOffset(), typeOfText);
} catch (Exception e) {
// ignore hightlight errors: parsing errors could lead to wrong loacation data
LOG.debug("Highligthing error in file '{}' at line:{}, column:{}", getContext().getFile().getAbsoluteFile(), location.startLine(), location.startLineOffset());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void stringLiteral() throws Exception {

checkOnRange(49, 19, 7, TypeOfText.STRING); // "hello"
checkOnRange(50, 19, 18, TypeOfText.STRING); // "hello\tworld\r\n"
checkOnRange(73, 32, 24, TypeOfText.STRING); // R"([.^$|()\[\]{}*+?\\])"
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ void func()
}
}

void test1()
{
const std::regex RegexEscape(R"([.^$|()\[\]{}*+?\\])"); // raw string literal
}

/* EOF */

0 comments on commit c07c708

Please sign in to comment.