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

SymbolTable support #1401

Open
guwirth opened this issue Jan 21, 2018 · 4 comments
Open

SymbolTable support #1401

guwirth opened this issue Jan 21, 2018 · 4 comments

Comments

@guwirth
Copy link
Collaborator

guwirth commented Jan 21, 2018

As an user I like to get symbol highlighting support within a file. Double clicking a symbol all corresponding symbols should also be highlighted.


Sample could be: https://github.com/SonarSource/sonar-java/blob/d35941d6b78031c22a63b875226a4f0ea49cd967/java-frontend/src/main/java/org/sonar/java/ast/visitors/SonarSymbolTableVisitor.java

@ivangalkin
Copy link
Contributor

Hello @guwirth,

I started to look into this user story by comparing of sonar-java and sonar-cxx. The sonar-java's implementation of SonarSymbolTableVisitor is easy, but it is based on the proper implemented visitor pattern and a fine granular abstract syntax tree.

sonar-cxx relies on SSLR only, not only for checks but also e.g. for highlighting. SSLR produces a tree of ASTNodes and Tokens without any further class hierarchies. It seems to be convenient if one writes some simple rules, but it's not suitable for complex syntax processing (which causes #1468 and maybe even #1415 ).

E.g. symbols in java's SonarSymbolTableVisitor have a fully-qualified declaration scope e.g.

*<class>::<enum>::<enum_value>
*<class>::<method>::<formal_parameter> (in C++ there will be also a namespace)

Traversing of our ASTNodes back and forth in order to collect such scopes will be pretty complex task.

As far as I know sonar-cxx is designed to be used with external analyzers, rather than implement own rules (#1410). But I really don't known how we could implement a good highlighting or correct symbol table without AST.

There is an open source implementation of C/C++ AST made by EclipseCDT. There are examples how to use it in stand-alone projects. So maybe we should use them as a first step?

@Bertk
Copy link
Contributor

Bertk commented Apr 30, 2018

Hallo @guwirth, @ivangalkin,

I was also looking into this improvement and used the implementation from the python plugin as template.
Unfortunately I am busy with urgent project topics and postponed this contribution.
Please check the current work in progress content and as always early feedback is always appreciated.

By the way, introducing another AST model will be a breaking change if the current will be replaced because the current supports C++/CLI and CUDA kernel declaration.

I did not plan to share the sources in this state but the last comment forced me to do it now. The last 2 commits has the partial implementation of a SymbolTable.
Next steps - fix unit tests:

//  @Test
  public void global_variable_reference() {
    CxxSymbol a = lookup(rootTree, "global1");
    assertThat(a.writeUsages()).extracting(AstNode::getTokenLine).contains(15, 87);
  }
//  @Test
  public void class_variable() {
    AstNode classMotorController = rootTree.getFirstDescendant(CxxGrammarImpl.classSpecifier);
    assertThat(symbolTable.symbols(classMotorController)).hasSize(12);
    CxxSymbol classVariableA = lookup(classMotorController, "speed");
    assertThat(classVariableA.writeUsages()).extracting(AstNode::getTokenLine).containsOnly(19,27,50,66);
    assertThat(classVariableA.readUsages()).extracting(AstNode::getTokenLine).containsOnly(90,66,55);
  }
@ivangalkin
Copy link
Contributor

Hello @Bertk,

thank you for the information.

Regarding the EclipseCDT: I searched for the CUDA / C++CLI support and at least according to the screenshots they are properly highlighted. By the way, which kind of support does sonar-cxx provide?

I took a brief look at your commit Bertk@78643d3 . All I can say is that it is very complex and the main reason IMHO is a lack of proper parsing/AST: In order to implement a single (and pretty basic functionality) you had to re-invent a big part of lexical/semantic analysis ( CxxSymbolNameHelper::getSymbolName() or CxxScopeVisitor, FirstPhaseVisitor etc) and you had to do it in the imperative way (if node has type A: doA() elif node has type B: doB() ...). We will have to do it once again if the next Visitor will be introduced... or just create a correct AST once and for all.

@ivangalkin
Copy link
Contributor

Hello All,

I was very interested in this feature and was able to implement the functionality with Eclipse CDT. Please take a look at #1479. The pull request contains implementation details and the first evaluation. Please review.

Thank you

@guwirth guwirth changed the title US: SymbolTable support Dec 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment