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

Question: Reproduce parsing errors on local machine #2286

Closed
amai2012 opened this issue Dec 8, 2021 · 6 comments · Fixed by #2291
Closed

Question: Reproduce parsing errors on local machine #2286

amai2012 opened this issue Dec 8, 2021 · 6 comments · Fixed by #2291
Assignees
Labels
Milestone

Comments

@amai2012
Copy link

amai2012 commented Dec 8, 2021

I have a lot of parsing errors in my codebase.

https://github.com/SonarOpenCommunity/sonar-cxx/wiki/Detect-and-fix-parsing-errors has some suggestions but I lack a short hello-world project for testing.
Is it possible to reproduce the parse errors on my local machine without a full-blown analysis?

@guwirth
Copy link
Collaborator

guwirth commented Dec 8, 2021

Hi @amai2012,

you found already https://github.com/SonarOpenCommunity/sonar-cxx/wiki/Detect-and-fix-parsing-errors.

The best way is normally to define some macros to fix parsing errors. You should always keep in mind, that the cxx parser is only doing a syntax analysis (you don't have to provide all includes, macros, types for a semantic analysis):
https://github.com/SonarOpenCommunity/sonar-cxx/wiki/Dealing-with-compiler-specific-code-pieces

Add the macros of your project to a header file and include it with https://github.com/SonarOpenCommunity/sonar-cxx/wiki/sonar.cxx.forceIncludes. Good samples for headers are here: https://github.com/SonarOpenCommunity/sonar-cxx/tree/master/cxx-sensors/src/main/resources/macros.

To test things locally on your PC you can use the SSLR toolkit. Copy paste your code into the toolkit window
https://github.com/SonarOpenCommunity/sonar-cxx/releases/download/cxx-2.0.6/cxx-sslr-toolkit-2.0.6.2925.jar

  • Hint: The tool works only with plain ASCII. With Windows you can copy/paste the text over Notepad to get it done.

grafik

Hope this helps!

Regards,

@amai2012
Copy link
Author

So far I have found three kind of issues:

  • missing compiler-specific macros -> I'm feeding them now via forced include
  • errors with complex nested macros from my own codebase -> to be analyzed

And this one:
The following code snippet without macro

struct Portfolio { int get() const; };

int check(const Portfolio& parObj, const int ind)
{
    if (parObj.get() < 0 || parObj.get() >= ind)
    {
        return 0;
    }
    return 1;
}

triggers

com.sonar.sslr.api.RecognitionException: Parse error at line 3 column 10:

    1: struct Portfolio { int get() const; };
    2: 
  -->  int check(const Portfolio& parObj, const int ind)
    4: {
    5:     if (parObj.get() < 0 || parObj.get() >= ind)
    6:     {

	at org.sonar.sslr.internal.vm.Machine.parse(Machine.java:73)
	at com.sonar.sslr.impl.Parser.parse(Parser.java:84)
	at com.sonar.sslr.impl.Parser.parse(Parser.java:78)
	at org.sonar.sslr.internal.toolkit.SourceCodeModel.setSourceCode(SourceCodeModel.java:62)
	at org.sonar.sslr.internal.toolkit.ToolkitPresenter.onSourceCodeParseButtonClick(ToolkitPresenter.java:122)
	at org.sonar.sslr.internal.toolkit.ToolkitViewImpl$5.actionPerformed(ToolkitViewImpl.java:201)
	at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
	at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
	at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
	at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
	at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
	at java.desktop/java.awt.Component.processMouseEvent(Component.java:6632)
	at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
	at java.desktop/java.awt.Component.processEvent(Component.java:6397)
	at java.desktop/java.awt.Container.processEvent(Container.java:2263)
	at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5008)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
	at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
	at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
	at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
	at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

@guwirth
Copy link
Collaborator

guwirth commented Dec 22, 2021

Hello @amai2012,

thank you for your investigation.

Could you please further narrow down the last finding. Try each test individual with the SSLR toolkit. Maybe it’s the const int?

// Test 1
struct Portfolio { int get() const; };
int check(const Portfolio& parObj, const int ind) { return 1; }

// Test 2
int check(const Portfolio& parObj, const int ind) { return 1; }

// Test 3
int check(const int ind) { return 1; }

// Test 4
int check(int ind) { return 1; }

Regards,

@amai2012
Copy link
Author

amai2012 commented Dec 27, 2021

Hello @amai2012,

thank you for your investigation.

Could you please further narrow down the last finding. Try each test individual with the SSLR toolkit. Maybe it’s the const int?

...

Tests 1-4 are passing here, the original example fails.
I've only been able to reduce to:

struct Portfolio { int get() const; };

void check( Portfolio& parObj, int ind)
{
    if (parObj.get() < 0 || parObj.get() >= ind)
    {
        return;
    }
}

I observe some odd behaviour with that sslr-tool here:
I am loading the example from a file (ANSI as of Notepad++) and the < char seems to be absent (see screenshot). However explicitly adding it in the Source code window seems to work, it shows up on the right hand side and the error persists...
Missing_char

@guwirth
Copy link
Collaborator

guwirth commented Jan 4, 2022

Hi @amai2012,

I was able to narrow this down (see below). Problem are the brackets () in combination with <...>=. Fast woraround is to set brackets ((...) || (...)).

That has maybe something to do with

public class RightAngleBracketsChannel extends Channel<Lexer> {

if ((next != '<') && (next != '=')) { // not <<, <=, <<=, <=>,

Samples:

void check() { // failure
    a() < 0 || c >= 1;
}
void check() { // ok
    a() < 0 || c > 1;
}
void check() { // ok
    a < 0 || c >= 1;
}
//-------------------------------------------------
void check() { // failure
    if ( a.b() < 0 || c >= 1) {}
}
void check() { // failure
    if ( a() < 0 || c >= 1) {}
}
void check() { // failure
    if ( a.b(1) < 0 || c >= 1 ) {}
}
void check() { // ok
    if ( a() < 0 || c > 1) {}
}
void check() { // ok
    if ( (a.b() < 0) || (c >= 1) ) {}
}
void check() { // ok
    if ( a.b < 0 || c >= 1) {}
}
void check() { // ok
    if ( a < 0 || c >= 1) {}
}

@guwirth guwirth added bug and removed question labels Jan 4, 2022
guwirth added a commit to guwirth/sonar-cxx that referenced this issue Jan 5, 2022
- close SonarOpenCommunity#2286

```C++
void check() {
    a() < 0 || c >= 1;
}
```
guwirth added a commit to guwirth/sonar-cxx that referenced this issue Jan 10, 2022
- close SonarOpenCommunity#2286

```C++
void check() {
    a() < 0 || c >= 1;
}
```
guwirth added a commit to guwirth/sonar-cxx that referenced this issue Jan 10, 2022
- close SonarOpenCommunity#2286

```C++
void check() {
    a() < 0 || c >= 1;
}
```
guwirth added a commit to guwirth/sonar-cxx that referenced this issue Jan 11, 2022
- close SonarOpenCommunity#2286

```C++
void check() {
    a() < 0 || c >= 1;
    a() < 0 || c > 1;
    a() < 0 || c >> 1;

    if(a() < 0 || c >= 1) {}
    if(a() < 0 || c > 1) {}
    if(a() < 0 || c >> 1) {}

    Y<X<(6<1)>>;
    Y<X<(6>=1)>>;
    Y<X<(6<=1)>>;
    Y<X<(6>>1)>>;
    Y<X<(6<<1)>>;
    Y<X<(6<=>1)>>;
}
```
@guwirth guwirth self-assigned this Jan 12, 2022
@guwirth guwirth added this to the 2.0.7 milestone Jan 12, 2022
@amai2012
Copy link
Author

amai2012 commented Mar 8, 2022

Thanks for fixing this.
I had no time to verify before, but now picking up 2.0.7 and trying to resolve more issues

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