views:

1794

answers:

6

I am working in existing C code which has a couple of lines with statements similar to this one:

struct collect_conn *tc = (struct collect_conn *) 
     ((char *)c - offsetof(struct collect_conn, runicast_conn));

The struct collect_conn goes along the following lines:

struct collect_conn {
  struct runicast_conn runicast_conn;
  struct announcement announcement;
  const struct collect_callbacks *cb;
  struct ctimer t;
  uint16_t rtmetric;
  uint8_t forwarding;
  uint8_t seqno;
};

I am using Eclipse CDT, and it marks the line with an orange squiggly line as 'syntax error'. I think it is marked as such by the CDT indexer. However, compilation (manually in a terminal) is no problem.

This is a bit inconvenient however, since the elements on the line don't get indexed (so the call hierarchy tree isn't always correct, or the highlighting of elements, etc.)

Why does Ecipse not like the line as it is?

+2  A: 

It might be confused, check if you have a definition of offsetof in-scope, for instance. Otherwise you might try simplifying the expression, breaking it up using e.g. a #define with the offset of, or something.

I'm thinking the compiler might provide a built-in version of offsetof, while Eclipses's compiler/code-parser might not. If so, you would need to make sure you have the definition, for Eclipse to be able to properly parse your code.

unwind
Pressing F3 (goto declaration) on offset, takes me to the stddef.h file, so it seems to reference the correct offsetof macro
Rabarberski
And do you also #include that file, before the problematic line?
unwind
A: 

I've seen Eclipse do this some times, and I use it for Java. Usually closing and opening the file again fixes it for me (resets whatever is wrong). It usually seems to be an error that WAS there but has been fixed and the "error cache" isn't updated correctly.

MBCook
Yes, I know what you mean. I have to do this sometimes as well. But in this case, it doesn't get solved by doing this.
Rabarberski
+4  A: 

It seems the CDT parser doesn't like the portion offsetof(struct ...). If you declare collect_conn using a typedef the error goes away. At least for me, the following code works:

typedef struct  {
   struct runicast_conn runicast_conn;
   struct announcement announcement;
   const struct collect_callbacks *cb;
   struct ctimer t;
   uint16_t rtmetric;
   uint8_t forwarding;
   uint8_t seqno;
} collect_conn;
...
struct collect_conn *tc = (struct collect_conn *)
     ((char *)c - offsetof(collect_conn, runicast_conn));

If you can't change the original declaration do something like this:

typedef struct collect_conn collect_conn_t;
jassuncao
Yes, indeed, that works. But this is not an option for me :-(
Rabarberski
+1  A: 

Hello.

Iv got the same problem. There is 2 definition of offsetof (one for C and one for C++). IMO the problem come from that

For example if i type

#ifndef __cplusplus
#endif

Eclipse will grey it. It mean __cplusplus is defined, but my project is a C

Unfortunatly i dont find a fix.

Check that you created a c project and not a c++ project. I don't have the problem you mention.
Oliver
+2  A: 

Eclipse CDT contains its own preprocessor/parser for analyzing your code and building an index. However when you invoke a build CDT calls out to your system compiler, like gcc for example. There may be minor differences between the syntax accepted by the CDT parser and the syntax accepted by your compiler, when this happens the CDT parser can get confused.

On my system the offsetof macro expands into an expression that uses the __offsetof__ keyword. This keyword isn't recognized by CDT so that's why there's a syntax error. To deal with this problem the CDT parser has a macro built in to deal with __offsetof__ which looks like this:

#define __offsetof__(x) (x)

This doesn't appear to be correct, at least on my system the result is the removal of the __offsetof__ keyword from the source which still leads to a syntax error.

I was able to get rid of the syntax error by going to the Paths and Symbols property page and adding a macro for __offsetof__ which maps to 'foo'. This tricks the parser into thinking its just a call to a function it hasn't seen before, but not a syntax error.

Alternatively you can turn off syntax error reporting in the editor by going to Window > Preferences > General > Editors > Text Editors > Annotations and unchecking all the checkboxes for C/C++ Indexer Markers.

Mike Kucera
A: 

try switching the indexer to "Full c/C++ indexer (complete parse)" in Preferences->c/C++ -> indexer

peter