Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 332278 - Parser reports error for BOOST_FOREACH
Summary: Parser reports error for BOOST_FOREACH
Status: RESOLVED FIXED
Alias: None
Product: CDT
Classification: Tools
Component: cdt-indexer (show other bugs)
Version: 8.0   Edit
Hardware: All All
: P3 normal with 4 votes (vote)
Target Milestone: 8.1.0   Edit
Assignee: Markus Schorn CLA
QA Contact: Markus Schorn CLA
URL:
Whiteboard:
Keywords:
Depends on: 197989 367753
Blocks:
  Show dependency tree
 
Reported: 2010-12-10 03:31 EST by Nathan Ridge CLA
Modified: 2012-02-23 11:33 EST (History)
11 users (show)

See Also:


Attachments
preprocessed source file (755.99 KB, text/plain)
2010-12-10 03:31 EST, Nathan Ridge CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nathan Ridge CLA 2010-12-10 03:31:38 EST
Created attachment 184923 [details]
preprocessed source file

The parser reports an error for the following code:

#include <boost/foreach.hpp>
int main()
{
    int array[3] = {1, 2, 3};
    
    BOOST_FOREACH(int& i, array)
        i++;
    
    return 0;
}

The error is: 

Multiple markers at this line
	- Invalid arguments Candidates are: boost::foreach_detail_::type2type<#0,mpl_::bool_<1>> * encode_type(const #0 &, mpl_::bool_<1> *) boost::foreach_detail_::type2type<#0,mpl_::bool_<0>> * encode_type(#0 &, 
	 mpl_::bool_<0> *)
	- Invalid arguments Candidates are: ? * or_(#0 *, #1 *, #2 *) ? * or_(#0 *, #1 *)
	- Invalid arguments Candidates are: ? * and_(#0 *, #1 *, #2 *) ? * and_(#0 *, #1 *)
	- Invalid arguments Candidates are: boost::mpl::not_<#0> * not_(#0 *)
	- Invalid arguments Candidates are: boost::foreach_detail_::auto_any<#0> contain(const #0 &, mpl_::bool_<1> *) boost::foreach_detail_::auto_any<#0 *> contain(#0 &, mpl_::bool_<0> *)
	- Invalid arguments Candidates are: boost::foreach_detail_::foreach_reference<#0,#1>::type deref(const boost::foreach_detail_::auto_any_base &, boost::foreach_detail_::type2type<#0,#1> *)
	- Invalid arguments Candidates are: void next(const boost::foreach_detail_::auto_any_base &, boost::foreach_detail_::type2type<#0,#1> *)
	- Invalid arguments Candidates are: bool done(const boost::foreach_detail_::auto_any_base &, const boost::foreach_detail_::auto_any_base &, boost::foreach_detail_::type2type<#0,#1> *) bool done(const 
	 boost::foreach_detail_::auto_any_base &, const boost::foreach_detail_::auto_any_base &, boost::foreach_detail_::type2type<#0 *,#1> *)
	- Invalid arguments Candidates are: boost::foreach_detail_::auto_any<int> end(const boost::foreach_detail_::auto_any_base &, boost::foreach_detail_::type2type<#0 *,#1> *, mpl_::bool_<1> *) 
	 boost::foreach_detail_::auto_any<boost::foreach_detail_::foreach_iterator<#0,#1>::type> end(const boost::foreach_detail_::auto_any_base &, boost::foreach_detail_::type2type<#0,#1> *, mpl_::bool_<0> *) 
	 boost::foreach_detail_::auto_any<boost::foreach_detail_::foreach_iterator<#0,#1>::type> end(const boost::foreach_detail_::auto_any_base &, boost::foreach_detail_::type2type<#0,#1> *, mpl_::bool_<1> *)
	- Invalid arguments Candidates are: boost::foreach_detail_::auto_any<#0 *> begin(const boost::foreach_detail_::auto_any_base &, boost::foreach_detail_::type2type<#0 *,#1> *, mpl_::bool_<1> *) 
	 boost::foreach_detail_::auto_any<boost::foreach_detail_::foreach_iterator<#0,#1>::type> begin(const boost::foreach_detail_::auto_any_base &, boost::foreach_detail_::type2type<#0,#1> *, mpl_::bool_<0> *) 
	 boost::foreach_detail_::auto_any<boost::foreach_detail_::foreach_iterator<#0,#1>::type> begin(const boost::foreach_detail_::auto_any_base &, boost::foreach_detail_::type2type<#0,#1> *, mpl_::bool_<1> *)

This error occurs every time BOOST_FOREACH is used, I just gave one example.

I have attached the preprocessed source file.
Comment 1 Robert CLA 2011-06-02 17:40:02 EDT
This is still an issue with indigo-rc2, is there any way to ignore errors?
Comment 2 Matthias Hilbig CLA 2011-06-24 08:29:43 EDT
And I get it still with Indigo. It's pretty annoying to always see error flags, but I don't want to completely disable Codan.
Comment 3 Cornelius Missing name CLA 2011-06-25 00:25:48 EDT
As long as you're on a project which can be compiled with std=c++0x the workaround is using the for-range-syntax, which is supported by recent clang and gcc versions, and now cdt :)
Comment 4 Nathan Ridge CLA 2011-06-25 00:32:13 EDT
Actually, your project doesn't even need to use C++0x to take advantage of CDT 8.0's support for range-based for loops. You can write a "foreach" macro that looks like this:

#ifdef ECLIPSE_PARSER
    #define foreach(a, b) for(a : b)
#else
    #define foreach(a, b) BOOST_FOREACH(a, b)
#endif

and then define the symbol ECLIPSE_PARSER in eclipse project settings, but not in your makefile. This way, the eclipse parser will see range-based for loops and give no errors, and the compiler will see BOOST_FOREACH, which it can compiler without C++0x support.
Comment 5 Robert CLA 2011-07-10 20:12:07 EDT
(In reply to comment #4)
> Actually, your project doesn't even need to use C++0x to take advantage of CDT
> 8.0's support for range-based for loops. You can write a "foreach" macro that
> looks like this:
> 
> #ifdef ECLIPSE_PARSER
>     #define foreach(a, b) for(a : b)
> #else
>     #define foreach(a, b) BOOST_FOREACH(a, b)
> #endif
> 
> and then define the symbol ECLIPSE_PARSER in eclipse project settings, but not
> in your makefile. This way, the eclipse parser will see range-based for loops
> and give no errors, and the compiler will see BOOST_FOREACH, which it can
> compiler without C++0x support.

Where in project settings do you set ECLIPSE_PARSER?
Comment 6 Nathan Ridge CLA 2011-07-10 20:16:15 EDT
(In reply to comment #5)
> (In reply to comment #4)
> > Actually, your project doesn't even need to use C++0x to take advantage of CDT
> > 8.0's support for range-based for loops. You can write a "foreach" macro that
> > looks like this:
> > 
> > #ifdef ECLIPSE_PARSER
> >     #define foreach(a, b) for(a : b)
> > #else
> >     #define foreach(a, b) BOOST_FOREACH(a, b)
> > #endif
> > 
> > and then define the symbol ECLIPSE_PARSER in eclipse project settings, but not
> > in your makefile. This way, the eclipse parser will see range-based for loops
> > and give no errors, and the compiler will see BOOST_FOREACH, which it can
> > compiler without C++0x support.
> Where in project settings do you set ECLIPSE_PARSER?

Project Properties -> C/C++ General -> Paths and Symbols -> Symbols tab. Be sure to add it under the "GNU C++" Language, not "GNU C" which is selected by default.

This is assuming you're using a makefile project. I'm not sure how to do this with managed build projects as I never use them.
Comment 7 Markus Schorn CLA 2011-07-11 04:44:12 EDT
(In reply to comment #6)
You can also use the built-in macro __CDT_PARSER__, it is always set by our preprocessor.
Comment 8 Nathan Ridge CLA 2011-07-11 16:29:08 EDT
(In reply to comment #7)
> (In reply to comment #6)
> You can also use the built-in macro __CDT_PARSER__, it is always set by our
> preprocessor.

Ah, good to know! Then it can be done with both makefile and managed build projects.
Comment 9 Robert CLA 2011-07-11 16:51:55 EDT
(In reply to comment #7)
> (In reply to comment #6)
> You can also use the built-in macro __CDT_PARSER__, it is always set by our
> preprocessor.

Thanks this is just what I wanted.
Comment 10 fa_kong CLA 2011-08-09 08:56:31 EDT
Just want to know when this can be fixed since it was not an issue in Eclipse 3.5 or 3.6. And it is annoying if I don't want to change my code as the "foreach" macro-define.
Comment 11 Nathan Ridge CLA 2011-08-09 14:14:13 EDT
It has always been an issue, you just haven't seen the red underline in 3.5 or 3.6 because that feature (showing red underlines for problems) was only introduced in 3.7.

I strongly suspect that, like many issues involving boost, the root cause is https://bugs.eclipse.org/bugs/show_bug.cgi?id=197989. (Try preprocessing a file that uses BOOST_FOREACH and replacing the code in the editor with the preprocessed version - surprise surprise, the error goes away).
Comment 12 bb3138 bb3138 CLA 2011-11-29 15:41:04 EST
Perhaps, this might be helpful. Actually, my workaround is even simpler and does not require adding/changing header files:

1. Goto Project Properties → Paths and Symbols → Symbols tab
2. Add name: BOOST_FOREACH(a,b)
   with value: for(a:b)

I did this for both languages GNU C and GNU C++.

After re-parsing all sources using the BOOST_FOREACH macro are recognized correctly.

(Eclipse version: Indigo Service Release 1)
Comment 13 Markus Schorn CLA 2012-01-03 08:39:13 EST
Tested with Boost 1.46.1.