Why does Eclipse CDT say: 'syntax error', but compilation no problem - c

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?

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.

I've fixed problem in eclipse CDT with Preferences->C/C++->Language Mappings : Add
Content Type : C-header
Language : C++

Sometimes, although the code compiles with no error, eclipse CDT's real-time code analyzer shows some errors in C/C++ files (eg. 'Function xxx could not be resolved). This is because eclipse CDT uses its own preprocessor/parser for analyzing the code and building the indexes instead of the MinGW's one (or any other GNU compiler). In order to fix this globally for all eclipse projects in the workspace, follow these steps:
(In order to fix this only for a specific project, follow steps 1, 2 and 4 in menu 'Project->Preferences')
1-In menu 'Window->Preferences->C/C++->Language Mappings', add the correct mappings as shown in below: (eg. for content types: C++ Source/Header File, use GNU C++ language and so on)
2-In menu 'Window->Preferences->C/C++->Indexer', set full indexing by checking all checkboxes (but not 'Skip' ones) as shown in below:
3-In each project's specific properties, menu 'Project->Properties->C/C++ general->Indexer', Uncheck 'Enable project specific settings' as shown in below:
4-Rebuild the indexing, menu 'Project->C/C++ Index->Rebuild'.

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;

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.

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

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.

I fixed similar problem after checking the tab Error Parsers in Makefile Project in New CDT Project Wizard, removing CDT Visual C Error Parser (I am using gcc)

I ended up solving the problem like this. First I opened the project properties, then the C/C++ general->Paths and Symbols category. Under the Symbols tab I added this entry:
Symbol: offsetof(TYPE,MEMBER)
Value: ((ssize_t) &((TYPE *)0)->MEMBER)
These symbols are used by the indexer but not passed to the compiler (at least in Makefile projects, I haven't tried it in the other kind of C project), so it doesn't override GCC's built-in offsetof

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.

Related

Eclipse CDT: Glib headers not parsed correctly

I am developing a C application, and using Eclipse CDT IDE, which I find great. The project uses Glib,Gtk,and GStreamer , so whenever I use some of their features in a file, I need to include:
#include <glib.h>
#include <gtk/gtk.h>
#include <gst/gst.h>
The code compiles without any error, since the PATH variable to search those headers is set correctly in a CMakeLists.txt.
However, while working on the project, I found annoying errors highlighting in my code, regarding type definitions like gchar or GValue or GTKApplication; the error outlined is "symbol **** could not be resolved". These definitions are inside a header file that my Eclipse IDE cannot find (included by glib.h), if not at compile time (indeed the program compiles correctly). Instead, the type GError , defined in gst.h , is not highlighted as an error by the pre-compiler.
I would like then that my Eclipse IDE could search on nested headers (#include inside an #inlcude inside...) to find those type definition, in order so to not have those annoying errors highlighting. How can I do so? I would not like to have to include directly all files where the type definitions are done.
EDIT: As Jonah Graham outlined, the problem is not beacuse Eclispe does a "single-step research" on the headers, since it inspects includes inside other includes like any other IDE. It is a CMake bug with c and Eclipse
Thanks in advance.
The problem you are facing is a CMake bug*. CMake adds __cplusplus into the defined symbols unconditionally, which means that glib headers are not parsed properly when in C mode. You can see this clearly by opening gmacros.h around the definition for G_BEGIN_DECLS:
Because CMake told CDT __cplusplus is defined, it thinks G_BEGIN_DECLS is also defined, which makes code like this from gtypes.h parse incorrectly:
G_BEGIN_DECLS
/* Provide type definitions for commonly used types.
* These are useful because a "gint8" can be adjusted
* to be 1 byte (8 bits) on all platforms. Similarly and
* more importantly, "gint32" can be adjusted to be
* 4 bytes (32 bits) on all platforms.
*/
typedef char gchar;
...
Of course with no gchar defined, everything else is going to go badly.
Luckily there is a quick workaround until the problem is resolved in CMake, remove __cplusplus from the info in CDT.
Open Project Properties
C/C++ Include Paths and Symbols
Remove __cplusplus from the list and press OK
(sometimes necessary) Right-click on project -> Index -> Rebuild
* There may be some other workarounds if you know CMake better. The bug says also it will be fixed for the next release of CMake.

Where are things like zval defined?

I'm trying to develop a PHP extension using Eclipse and CDT, but the extension code is reported as full of syntax errors and warnings. I managed to get rid of part of them by telling CDT to look for the PHP include directory and the main directory from the PHP source code, which resolved all the includes, but there are still a lot of problems.
Specifically, where do I find the definition for zval, SUCCESS, FAILURE, STANDARD_MODULE_PROPERTIES, zend_module_entry, zend_function_entry and zend_parse_parameters? How do I get CDT to not mark <module name>, <module name>_functions and return_value as an unresolvable symbols?
Is there a better way to develop a PHP extension using an IDE?
For where to find:
typedef struct _zval_struct zval; : ./Zend/zend.h
struct _zval_struct { : ./Zend/zend.h
SUCCESS : ./Zend/zend.h
FAILURE : ./Zend/zend.h
STANDARD_MODULE_PROPERTIES : ./Zend/zend_modules.h
typedef struct _zend_module_entry zend_module_entry; : ./Zend/zend_modules.h
struct _zend_module_entry { : : ./Zend/zend_modules.h
typedef struct _zend_function_entry {
…
} zend_function_entry; : ./Zend/zend_API.h
zend_parse_parameters( : ./Zend/zend_API.c
If you want to look at PHP source code like that i suggest you use i.e. Vim in combination with cscope. Then all you have to do is have the marker on i.e. zval, enter two keys and it jumps to the definition.
From a quick google it looks like Eclipse also has some cscope support or the like. Search workspace? It would require you to load the PHP source in to workspace tho I guess.
When it comes to CDT I have no idea. Never used it.
Edit:
Had a look at it out of curiosity and need to get to know CDT.
Created a standard C project.
Right click project
Paths and Symbols
Source location; added PHP directory.
Right click project
Index
Rebuild (Big source so takes a while)
Now I can say i.e. zend_parse_param CtrlSpace and get auto completion. Or zend_parse_parameters( CtrlSpace.
Or select Search -> C/C++... and select i.e. "Definitions", text "zval" -> search and get definitions.
Or type zva CtrlSpace and get zval etc.
You can also right click PHP source in Project Explorer and select i.e. Exclude from build etc.

Where is UNS_32 defined?

I'm using Doxygen on a client's source code and Doxygen can't find a symbol UNS_32.
The client's code compiles without errors using the GNU ARM compiler.
I have searched the client's code base and can't find the definition of UNS_32.
I searched the GNU ARM source code tree and can't find it either.
So, where is the symbol UNS_32 defined?
*Note: I'm not looking for the meaning. I want the definition so I can put it in my Doxygen configuration file. *
Example usage:
void lpc_heap_init (void *base_addr, UNS_32 heap_size);
It is defined in lpc_types.h
http://code.google.com/p/32bitmicro/source/browse/trunk/src/nxp/lpc17xx/LPC1700CMSIS/Drivers/include/lpc_types.h?spec=svn226&r=226
/** SMA type for 32 bit unsigned value */
typedef uint32_t UNS_32;
You need also to search and headers in paths specified to the compiler with -I options. You can similarly specify these in Doxygen.
My preferred approach is to use Visual Studio, create a "Makefile Project", add the sources and copy all the project's command line defined macros and include paths to the project settings, then let the Intellisense simplify code navigation - it is a simple case of right-clicking the symbol, and selecting "Goto Definition", or using the "Code Definition" window which shows the defining source for any symbol you place the cursor on. You could even set up the project to actiually build the code too.

FreeRTOS, Eclipse IDE, and Syntax Errors

I have a slight annoyance when dealing with FreeRTOS code in Eclipse and I'm not sure if it's just me or if other people have this issue too but I see a lot of syntax errors highlighted in my code but it compiles/executes fine. The syntax errors seem to be caused by FreeRTOS specific code like:
signed portCHAR *x;
or
vSemaphoreCreateBinary (semaphore);
or
signed portBASE_TYPE gpsTaskStart (void)
{
return xTaskCreate (vGPSTask, (const signed portCHAR * const) "GPS", configMINIMAL_STACK_SIZE, NULL, (tskIDLE_PRIORITY + 1), &taskHandles [TASKHANDLE_GPS]);
}
I was wondering if there was a way to configure Eclipse to parse this syntax properly.
I think you just have to update your Eclipse project configuration for the incremental checker to be able to find FreeRTOS symbol definitions.
Right click on your project > Properties > C/C++ General > Path and Symbols > Includes.
Add the FreeRTOS folder there.
Go to project settings > directories and include all directories containing .h files (including portable/GCC/%yourplatform%). Once done, make a build. This should work, at least works for me.

How can I get Eclipse to index code inside #ifdef .... #endif

I'm using eclipse to work on some c code and it is not indexing code inside conditional compilation blocks like this:
#ifdef USE_FEATURE_A
int feature_a(...) {
some = code(here);
}
#endif
How can I get eclipse to index the feature_a function?
You could tell eclipse that USE_FEATURE_A is defined. Open your project properties and go to the "C/C++ General->Paths and Symbols" page, under the "Symbols" tab click the "Add" button and put USE_FEATURE_A in the name feild and click OK.
Note: this will cause it not to index any #else sides to your preprocessor stuff... so unless they are all like the one in question you can't AFAIK, but if they are they you're good. (Eclipse contains a C preprocessor that it uses to analyize your code all the stuff above does is essentially the same as adding -DUSE_FEATURE_A to your command line so Eclipse's preprocessor will behave differently from the one in your compiler)
This is an easier and in my opinion more elegant solution to the one selected as the solution:
If someone has the same problem (as I had), this can (now?) easily be solved by going to Window->Preference->C/C++/Indexer and enable "Index all header variants".
Then click Project->C/C++ Indexer->rebuild and clean and build your project. This should resolve all error originating from preprocessor commands.
For what it's worth, getting eclipse to parse conditionally compiled code is much harder to do than would appear at first glance. I found a paper on by IBM from 2007 where they said they will prioritize for the "next release".
Handling Conditional Compilation in CDT's Core
I had this same problem, but the code conditionally eliminated by preprocessing was perfectly valid c code and I wanted it formatted... This was my solution:
1) Global find/replace of #if to #JUNKif
2) Ctrl-Shift-F to reformat the source
3) Another global find/replace of #JUNKif to #if
One way to index code under flag in Eclipse(Kepler) c/c++Editor.
You can enable the compilation flags in Eclipse editor so that code under them can be indexed.
Properties > Preprocessor Include Paths > CDT User settings Entries
Click on ADD and add the Preprocessor Macro and you can specify its value.
Best way I guess is to use the Indexer option : Project Properties>C/C++ General>Indexer.
You can choose Enable project specific settings
I prefer choosing "Use active build configuration" so that all files which are actually built in the project are indexed.
Anyhow you can also choose to index all files in the project even if they are not included in the build ...

Resources