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

Highlighting error for concatenated strings on multiple lines #1468

Closed
jhoussay opened this issue Apr 26, 2018 · 4 comments
Closed

Highlighting error for concatenated strings on multiple lines #1468

jhoussay opened this issue Apr 26, 2018 · 4 comments
Assignees
Labels
Milestone

Comments

@jhoussay
Copy link

Description

For concatenated strings declared on multiple lines, an highlighting error occurs.
This is quite a minor issue as it does not prevent defect detection in the rest of the source file, it only generates invalid highlighting in the source file and pollutes sonar-scanner outputs with warnings.

Sample code to reproduce the problem

#include <string>

// Works !
std::string ok = "foo" "-bar";

// Does not work...
std::string fail = "foo"
                   "-bar";

sonar-scanner warning

WARN: Highligthing error in file '.../Test.cpp' at line:7, column:19

Source visualization in SonarQube

image

As expected with the warning, highlighting fails on the second string declaration.
However, highlighting is also incorrect for the last two characters on the first string declaration.

@guwirth
Copy link
Collaborator

guwirth commented Apr 27, 2018

@jhoussay this is an known issue. Problem is the two step mechanism to parse the code. First step is the preprocessor doing the string concatenation (intermediate result) and in a second step the C++ parser is parsing and creating the syntax highlighting.

Good ideas/solutions are welcome.

ivangalkin added a commit to ivangalkin/sonar-cxx that referenced this issue May 22, 2018
Fix highlighting issue SonarOpenCommunity#1468

* `JoinStringsPreprocessor` replaces adjacent string Tokens
  with a single concatenated one
* Previously the original Tokens were lost and proper highligting
  for multiline adjacent string literals were impossible
* Solution:
  a) store original Tokens as Trivia
  b) if such trivia was found during highlighting: highlight
     preserved Tokens

* ALSO minor refactoring and simplification of `JoinStringsPreprocessor`
ivangalkin added a commit to ivangalkin/sonar-cxx that referenced this issue May 22, 2018
Fix highlighting issue SonarOpenCommunity#1468

* `JoinStringsPreprocessor` replaces adjacent string Tokens
  with a single concatenated one
* Previously the original Tokens were lost and proper highligting
  for multiline adjacent string literals were impossible
* Solution:
  a) store original Tokens as Trivia
  b) if such trivia was found during highlighting: highlight
     preserved Tokens

* ALSO minor refactoring and simplification of `JoinStringsPreprocessor`
@ivangalkin
Copy link
Contributor

Hi @guwirth, @jhoussay and all,

I suggest to store the original Tokens, merged during the preprocessing as Trivia. Please see PR #1480 for proposed solution.

Regards

@ivangalkin
Copy link
Contributor

Hi @guwirth,

although my change (#1480) is consistent with the previous highlighting logic (comments and preprocessed preprocessor statements are also stored in Trivia), I am wondering why is highlighting is done after preprocessing at all?

Usually AST is not required for highlighting at all, only lexer is used. So maybe the long-term (?) solution is to split our parser or/and put highlighting between the lexer and later parsing steps (e.g. preprocessor etc)?

@guwirth guwirth self-assigned this May 28, 2018
@guwirth guwirth added bug and removed question labels May 28, 2018
@guwirth guwirth added this to the 1.1 milestone May 28, 2018
@guwirth
Copy link
Collaborator

guwirth commented May 28, 2018

closed with #1480

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants