Bug 177154 - [patch] single-line comments incorrectly retained in macros
Summary: [patch] single-line comments incorrectly retained in macros
Status: RESOLVED FIXED
Alias: None
Product: CDT
Classification: Tools
Component: cdt-core (show other bugs)
Version: 4.0   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: 4.0 RC0   Edit
Assignee: Anton Leherbauer CLA
QA Contact:
URL:
Whiteboard:
Keywords: contributed
Depends on:
Blocks:
 
Reported: 2007-03-13 11:05 EDT by Ed Swartz CLA
Modified: 2008-06-20 11:27 EDT (History)
3 users (show)

See Also:


Attachments
patch and test case (8.05 KB, patch)
2007-03-13 11:07 EDT, Ed Swartz CLA
bjorn.freeman-benson: iplog+
Details | Diff
screenshot (46.19 KB, image/pjpeg)
2007-03-28 09:54 EDT, Markus Schorn CLA
no flags Details
another unit test (2.06 KB, patch)
2007-04-18 08:52 EDT, Ed Swartz CLA
bjorn.freeman-benson: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ed Swartz CLA 2007-03-13 11:05:52 EDT
Code that #defines macros with trailing single-line comments will be pasted, with comments, into expansion contexts.  D'oh!

#define LIT 1  // my value
int func(int x) {
   
}

int main() {
  return func(LIT);   // fails to parse
}
Comment 1 Ed Swartz CLA 2007-03-13 11:07:03 EDT
Created attachment 60671 [details]
patch and test case
Comment 2 Chris Recoskie CLA 2007-03-13 11:21:46 EDT
Took a look... looks good to me...

Any objections from anyone to committing this?
Comment 3 Markus Schorn CLA 2007-03-27 06:22:29 EDT
I have a look.
Comment 4 Anton Leherbauer CLA 2007-03-28 05:13:57 EDT
I don't get an error with the example in comment 0. The reason is probably that the scanner does not pop contexts when skipping a line comment.
Comment 5 Markus Schorn CLA 2007-03-28 07:40:34 EDT
(In reply to comment #4)
> I don't get an error with the example in comment 0. The reason is probably that
> the scanner does not pop contexts when skipping a line comment.

Ok, while technically the macro expansion should neither contain single-line nor
multi-line comments it has no effect on parsing the code. --> changing severity.

A fix should remove both kinds of comments.

Comment 6 Ed Swartz CLA 2007-03-28 09:22:03 EDT
I disagree that the severity is minor.  At least in 4.0M5, this bug caused the attached unit tests to fail, meaning the parser was affected.  The end result of this is that an editor containing uses of such macros shows an Outline that does not include the given function or any others past it.  

The scanner is storing the definition of the macro "LIT"  as "1  // my value".  Then substituting this text causes the func(LIT) call to be parsed as:

func(1  // my value);

which looks like:

func(1

Meaning the trailing ");" tokens are ignored, and the function call looks like a syntax error.  (Or maybe I misunderstand and the literal '/' and '/' and 'my' and 'value' tokens are being scanned -- in either case, it's a problem, and does affect parsing the code.)

My patch removes the comments from the macro definition beforehand.  Multi-line trailing (and embedded) comments were already being removed, but not single-line ones.


Comment 7 Anton Leherbauer CLA 2007-03-28 09:34:09 EDT
(In reply to comment #6)
> I disagree that the severity is minor.  At least in 4.0M5, this bug caused the
> attached unit tests to fail, meaning the parser was affected.  The end result
> of this is that an editor containing uses of such macros shows an Outline that
> does not include the given function or any others past it.

The unit test does not test whether the parser reports a syntax error in this case. The line comment in the macro expansion is not right, but it does not create a syntax error when expanded, therefore it is not a big issue. Did you actually try your example?
There may be another problem why the outline is missing something.
Comment 8 Markus Schorn CLA 2007-03-28 09:54:40 EDT
Created attachment 62226 [details]
screenshot
Comment 9 Markus Schorn CLA 2007-03-28 09:55:43 EDT
I have tested with M5, works fine.
Comment 10 Ed Swartz CLA 2007-03-28 10:06:26 EDT
Ok, sorry for the confusion.  

(1) We are using a patched 4.0M5.  Maybe the underlying issue has been fixed elsewhere since then.  I did see the scanner from the HEAD neglected to remove single-line comments from macros as well, so I thought the same problem would arise there.  But it appears from Anton's comment that comments may be expected to be handled inside macro expansions.  

(2) The vastly simplified case shown in the summary indeed does NOT show a problem when the trailing comment is left in place.  Sorry.  My reasoning was incorrect in presuming what a simpler example would look like.

The second case in the attached unit test, however, which is based on the actual code our customers reported the bug against, does show the problem for me.  Can anyone verify this?

Taking out my patch, the failure occurs when parsing the "actual reduced test case" in this test:

org.eclipse.cdt.internal.core.parser.ParserException: FAILURE
	at org.eclipse.cdt.core.parser.tests.ast2.AST2BaseTest.parse(AST2BaseTest.java:140)
	at org.eclipse.cdt.core.parser.tests.ast2.AST2Tests.testMacroCommentsBug_177154(AST2Tests.java:3558)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at junit.framework.TestCase.runTest(TestCase.java:154)
	at junit.framework.TestCase.runBare(TestCase.java:127)
	at junit.framework.TestResult$1.protect(TestResult.java:106)
	at junit.framework.TestResult.runProtected(TestResult.java:124)
	at junit.framework.TestResult.run(TestResult.java:109)
	at junit.framework.TestCase.run(TestCase.java:118)
	at org.eclipse.cdt.core.testplugin.util.BaseTestCase.run(BaseTestCase.java:85)
	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Comment 11 Anton Leherbauer CLA 2007-03-28 10:28:41 EDT
Now, I see the problem. 
If a macro with a line comment is itself used in an expansion, then the problem arises.
Thanks for the clarification!
Comment 12 Anton Leherbauer CLA 2007-03-28 10:34:23 EDT
Patch applied.
Comment 13 Ed Swartz CLA 2007-04-18 08:52:43 EDT
Created attachment 64178 [details]
another unit test
Comment 14 Ed Swartz CLA 2007-04-18 08:53:56 EDT
Our customers found another instance of this bug.  If the single line comment is placed on the macro definition, the parser enters an infinite loop.  If the comment is absent, the parser succeeds.  The attached test demonstrates this against today's HEAD.
Comment 15 Anton Leherbauer CLA 2007-04-19 05:35:12 EDT
Interestingly the endless loop is somehow caused by the trailing '\t' (which is not removed from the macro definition). If the tab is changed to spaces, there is no problem.
I still don't know why the tab causes an endless loop in the parser, but I fixed the comment removal such that tabs are replaced by spaces (outside string and character literals) anyway.
Comment 16 Mike Kucera CLA 2007-04-19 10:23:54 EDT
(In reply to comment #15)
> Interestingly the endless loop is somehow caused by the trailing '\t' (which is
> not removed from the macro definition). If the tab is changed to spaces, there
> is no problem.

During an investigation into bug 179383 I discovered that any runtime exception thrown by doFetchToken() in BaseScanner has the potential to cause an infinite loop in the parser. Maybe you are seeing a symptom of that.
Comment 17 Mike Kucera CLA 2007-04-19 10:26:14 EDT
I also want to mention that the LALR based C99 parser that I'm working on (bug 173110) has a lexer that removes all comments before passing the tokens to the preprocessor. This means that things like comments and spaces in macros are ignored by the preprocessor, so the kinds of problems described in this bug will not occur.