Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax error while trying to parse regex in raw string #1748

Closed
Korny0301 opened this issue Aug 14, 2019 · 4 comments · Fixed by #1749
Closed

Syntax error while trying to parse regex in raw string #1748

Korny0301 opened this issue Aug 14, 2019 · 4 comments · Fixed by #1749
Assignees
Labels
Milestone

Comments

@Korny0301
Copy link

Description

Sonar-scanner can't parse a specific regular expression that is defined in a raw string. When using capturing groups with round brackets () inside the regular expression, the closing bracket of a capturing group can not be placed just in front of the very last closing bracket of the raw string.

Steps to reproduce the problem

Create c++ file with regular expression:

#include <regex>
constexpr auto rgxStr = R"(([A-Z\d]))";

Run sonar-scanner with this file as inclusion (e.g. sonar-scanner -Dsonar.inclusion=test.cpp).

Expected behavior

Line with defined regular expression in raw string should be parsed without syntax error.

Actual behavior

Expression can't be parsed. Sonar-scanner resolves in error:
C++ skip parser error
C++ Parser can't read code. Declaration is skipped.

Known workarounds

Avoid closing bracket immediately before the very last closing bracket that is used to finish the raw string. You could e.g. use a dollar sign before the very last closing bracket:

constexpr auto rgxStr = R"(([A-Z\d])$)";

LOG file

10:27:42.924 DEBUG: finished preprocessing '/opt/wsp/project/test.cpp'
10:27:42.935 DEBUG: [/opt/wsp/project/test.cpp:2]: syntax error: const auto rgxStr = R "(([A-Z\d]\d{4}))"

Related information

cxx plugin version: 1.2.0
SonarQube version: 7.2.1 (14109)

@guwirth guwirth added the bug label Aug 14, 2019
@guwirth
Copy link
Collaborator

guwirth commented Aug 16, 2019

Hi @Korny0301,

thanks for your feedback

Raw string literal. Used to avoid escaping of any character. Anything between the delimiters becomes part of the string:

R "delimiter( raw_characters )delimiter"

  • delimiter: A character sequence made of any source character but parentheses, backslash and spaces (can be empty, and at most 16 characters long)

Your sample: R"(([A-Z\d]))"

  • empty / delimiter
  • detection of )" seems to be wrong in this case (maybe we have to count opening / closing brackets)
  • other possible workaround should be to set delimiter e.g. R"regex(([A-Z\d]))regex"

Code reading raw strings is here: https://github.com/SonarOpenCommunity/sonar-cxx/blob/master/cxx-squid/src/main/java/org/sonar/cxx/channels/StringLiteralsChannel.java#L90

Regards,

@Korny0301
Copy link
Author

Hi @guwirth,

thanks for your response.

I already tried to use an additional delimiter but without success. The raw string R"regex(([A-Z\d]))regex" results in a syntax error as well.

Best regards

@guwirth guwirth self-assigned this Aug 17, 2019
@guwirth guwirth added this to the 1.3.1 milestone Aug 17, 2019
@guwirth
Copy link
Collaborator

guwirth commented Aug 18, 2019

Hi @Korny0301, fixed! You can try with https://ci.appveyor.com/project/SonarOpenCommunity/sonar-cxx/branch/master/artifacts. Regards

@Korny0301
Copy link
Author

I tried the new version and it works now. Thanks for fixing!!

@guwirth guwirth mentioned this issue Sep 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants