Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 314593

Summary: Improve range of syntax error for statements with missing semicolon
Product: [Tools] CDT Reporter: Elena Laskavaia <elaskavaia.cdt>
Component: cdt-parserAssignee: Markus Schorn <mschorn.eclipse>
Status: RESOLVED FIXED QA Contact: Mike Kucera <mikekucera>
Severity: normal    
Priority: P3 CC: cdt.genie, yevshif
Version: 7.0   
Target Milestone: 8.0   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Attachments:
Description Flags
java hover
none
c hover
none
testcase + fix mschorn.eclipse: iplog-

Description Elena Laskavaia CLA 2010-05-26 21:37:16 EDT
Created attachment 170127 [details]
java hover

Hover over function haves gives nice overview of parameters for function,
when user typing the code it is good to have it. However cdt parser
shows parse error (because of no semicolon for example) and range of the error
starts at the begging of the function I am typing and ends god knows where.
Because of that I loose my useful hover.

For example
printf()
return;

would mark all this code as big error


I am attaching 2 screenshots: one from java and one for c. Same error - different user experience. Java parser only highlights ")" which what user needs.
Another solution to show both hovers. (Or I can turn off squiggly line but I like it)
Comment 1 Elena Laskavaia CLA 2010-05-26 21:37:36 EDT
Created attachment 170128 [details]
c hover
Comment 2 Markus Schorn CLA 2010-05-27 05:30:34 EDT
For declarations we have a way to report the syntax error for a missing semicolon. A similar approach could be taken for statements. While this 
would for instance not help for missing closing parenthesis, it would address the specific example.

(In reply to comment #0)
> Hover over function haves gives nice overview of parameters for function,
> when user typing the code it is good to have it. However cdt parser
> shows parse error (because of no semicolon for example) and range of the error
> starts at the begging of the function I am typing and ends god knows where.
> Because of that I loose my useful hover.
The error ends at the next semicolon or closing brace.

> I am attaching 2 screenshots: one from java and one for c. Same error -
> different user experience. Java parser only highlights ")" which what user
> needs.
Nevertheless, the java-parser is bad at parsing c/c++.
   
> Another solution to show both hovers. (Or I can turn off squiggly line but I
> like it)
I don't think the hover framework allows for two hovers.
Comment 3 CDT Genie CLA 2010-05-27 15:35:43 EDT
> > I am attaching 2 screenshots: one from java and one for c. Same error -
> > different user experience. Java parser only highlights ")" which what user
> > needs.
> Nevertheless, the java-parser is bad at parsing c/c++.

This is a joke right?

I know how hard hard to parse c/c++ (I wrote a few parsers).
But it is not impossible. Gcc for example knows that user is missing semicolon 
and where. i.e
error: expected ';' before 'return'
Comment 4 Markus Schorn CLA 2010-05-28 04:20:42 EDT
(In reply to comment #3)
> > > I am attaching 2 screenshots: one from java and one for c. Same error -
> > > different user experience. Java parser only highlights ")" which what user
> > > needs.
> > Nevertheless, the java-parser is bad at parsing c/c++.
> 
> This is a joke right?
You can also take it seriously: The java parser is really bad at parsing c/c++.
 
> I know how hard hard to parse c/c++ (I wrote a few parsers).
> But it is not impossible. Gcc for example knows that user is missing 
> semicolon 
> and where. i.e
> error: expected ';' before 'return'

Sure it is possible (as I wrote in comment 2 we could do something similar as when parsing lists of declarations), nevertheless we have to consider the effects of such an implementation.
The fact that this works with gcc does not really help us. The problem with doing something smart is that you can find some input that explores the worst side of your solution. Gcc for instance deals with crazy situations by stopping with a message similar to 'Too many errors, bailing out...'. We don't really have that option.

A specific concern is that when we create an error, where we think there is a missing semicolon and keep on parsing as if it has been there, in the worst case we'd do that after every token until we hit the next semicolon. With that we'd potentially create lots of syntax errors, where there should be just one.
Comment 5 Markus Schorn CLA 2010-06-23 05:22:35 EDT
Created attachment 172489 [details]
testcase + fix

The patch fixes the range of the syntax error. With that you do get an AST node for the statement without the semicolon. 

Note, that the name resolution for the name of the function call will fail when the parameters are not specified, you'll get a IProblemBinding.
Comment 6 Markus Schorn CLA 2010-06-23 05:23:37 EDT
Fixed in 8.0 > 20100623.
Comment 7 CDT Genie CLA 2010-06-23 06:23:02 EDT
*** cdt cvs genie on behalf of mschorn ***
Bug 314593: Handling of missing semicolon after statement.

[*] GNUCSourceParser.java 1.152 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java?root=Tools_Project&r1=1.151&r2=1.152

[*] AbstractGNUSourceCodeParser.java 1.153 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java?root=Tools_Project&r1=1.152&r2=1.153

[*] GNUCPPSourceParser.java 1.234 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java?root=Tools_Project&r1=1.233&r2=1.234

[*] CPPPopulateASTViewAction.java 1.30 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt-core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/CPPPopulateASTViewAction.java?root=Tools_Project&r1=1.29&r2=1.30

[*] FaultToleranceTests.java 1.6 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt-core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/FaultToleranceTests.java?root=Tools_Project&r1=1.5&r2=1.6
[*] AST2SelectionParseTest.java 1.17 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt-core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2SelectionParseTest.java?root=Tools_Project&r1=1.16&r2=1.17
Comment 8 Elena Laskavaia CLA 2010-06-23 09:21:38 EDT
Parameters are not specified you mean printf() or print?
In C we should not get problem binding for function, only in C++.
It is legal in C to call function which less arguments than defined.
Comment 9 Markus Schorn CLA 2010-06-23 11:24:10 EDT
(In reply to comment #8)
> Parameters are not specified you mean printf() or print?
> In C we should not get problem binding for function, only in C++.
> It is legal in C to call function which less arguments than defined.

You are right, there will be no problem binding for printf() in c.