Bug 245070 - Initialization "unsigned var(0);" yields Syntax error annotation in C++ file
Summary: Initialization "unsigned var(0);" yields Syntax error annotation in C++ file
Status: RESOLVED FIXED
Alias: None
Product: CDT
Classification: Tools
Component: cdt-parser (show other bugs)
Version: 5.0   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 5.0.1   Edit
Assignee: Markus Schorn CLA
QA Contact:
URL:
Whiteboard:
Keywords: contributed
Depends on:
Blocks:
 
Reported: 2008-08-24 21:57 EDT by Andrew Gvozdev CLA
Modified: 2008-09-03 12:37 EDT (History)
1 user (show)

See Also:


Attachments
proposed patch (2.35 KB, patch)
2008-09-01 00:31 EDT, Andrew Gvozdev CLA
no flags Details | Diff
more fine grained patch (2.34 KB, patch)
2008-09-02 23:21 EDT, Andrew Gvozdev CLA
mschorn.eclipse: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Gvozdev CLA 2008-08-24 21:57:22 EDT
See subj, parser won't do that. However, this is valid C++ syntax and C++ compiler has no problems with it:

insigned var1(0);
signed var2(0); // I figured this may also be a problem


A good thing that these get parsed just fine:

insigned var3=0;
int var4(0);
unsigned int var5(0);
Comment 1 Andrew Gvozdev CLA 2008-09-01 00:31:15 EDT
Created attachment 111371 [details]
proposed patch

Function canHaveConstructorInitializer() does not take into account that "int" can be omitted when using certain specifiers. Allowing IASTSimpleDeclSpecifier.t_unspecified to have initializer seems to solve the problem. According IASTSimpleDeclSpecifier documentation t_unspecified is intended to be  used exactly in such cases when "int" is omitted.

Markus, is there another good reason for t_unspecified to make canHaveConstructorInitializer() return false? All test cases run by me (or rather those which made it through java heap) passed.
Comment 2 Andrew Gvozdev CLA 2008-09-01 21:45:46 EDT
Markus, you worked on this part fairly recently like bug 84242, is there any chance you could take a look at this one?
Comment 3 Markus Schorn CLA 2008-09-02 08:59:57 EDT
(In reply to comment #1)
> Markus, is there another good reason for t_unspecified to make
> canHaveConstructorInitializer() return false? All test cases run by me (or
> rather those which made it through java heap) passed.

The situation is more difficult when you use an identifier as an argument:
   int varOrFunc(id);  // variable declaration of function prototype?

In the following situation, we know that we have a declaration for a ctor:
   varOrFunc(id);      // this declares a constructor, not a variable.

That explains why canHaveConstructorInitializer() checks for t_unspecified, however as you pointed out the check is not correct.
Comment 4 Andrew Gvozdev CLA 2008-09-02 09:17:39 EDT
I see. In this case we could use additional check if modifiers are present with 4 functions: isSigned(), isUnsigned(), isShort(), isLong(). Let me redo the patch if you agree.
Comment 5 Markus Schorn CLA 2008-09-02 10:16:44 EDT
(In reply to comment #4)
> I see. In this case we could use additional check if modifiers are present with
> 4 functions: isSigned(), isUnsigned(), isShort(), isLong(). Let me redo the
> patch if you agree.

Yes, that's the way to go, you also need to check for IGPPASTSimpleDeclSpecifier.isLongLong(). I wait for you patch.
Comment 6 Andrew Gvozdev CLA 2008-09-02 23:21:47 EDT
Created attachment 111540 [details]
more fine grained patch

Here is the patch. It is not necessary to check isLongLong() because isLong() will evaluate to true discovering at least one specifier "long".
Comment 7 Markus Schorn CLA 2008-09-03 05:52:10 EDT
(In reply to comment #6)
> Here is the patch. It is not necessary to check isLongLong() because isLong()
> will evaluate to true discovering at least one specifier "long".

Not checking isLongLong() works only as long as you do not use the gnu-extensions (in this cast the long long is silently represented as long, which is questionable). I have modified the testcase and added the check for isLongLong().

Thanks for detecting the problem and looking into it!
Fixed in 5.0.1 > 20080903.
Comment 8 Andrew Gvozdev CLA 2008-09-03 12:37:07 EDT
Works as expected from head. Thanks for applying patch.

I've been putting some effort trying to break it. Neither my attempts nor most prominent specimens from our code base showed any wrong. The best I could do was to crash eclipse with out of memory error.