Does Eclipse CDT's parser/indexer have a "self-identifying" preprocessor #define macro?
Asked Answered
A

1

5

I want to have some code only be included when Eclipse is parsing my source tree. At the moment, what I am doing^H^H^H^H^H trying to do is add a custom preprocessor macro - using Eclipse's Help | Preferences | C/C++ | Build | Setting Discovery - and insert a -DECLIPSE_BUILTIN into the various command-lines there.

So I was wondering whether there is already a standard, a default, macro which the preprocessor (and the indexer?) define to 'identify' themselves like this to the code - to save me the trouble.

Atheistic answered 29/12, 2016 at 20:46 Comment(1)
In case anyone needs to define your own custom macro that only the Eclipse indexer sees, you can define something like #define MY_MACRO value, which would look like -DMACRO=value if passed as a command-line argument to gcc or g++, by following my detailed instructions here: How can I convince Eclipse CDT that a macro is defined for source code editing and code completion?. This is really useful to "guide" the Eclipse indexer to index things properly without having to change your source code to use __CDT_PARSER__.Koffman
W
8

Yes, such a macro exists. It's __CDT_PARSER__.

Source

EDIT to clarify when this is used in the context of showing error and warning indicators:

CDT obtains error and warning indicators from two sources:

  1. It can parse the output of your build, recognize errors and warnings in that output, and surface those in the editor.

    These error and warning indicators have the annotation types Errors and Warnings (you can see the different annotation types, including the icons for each type, in in Preferences | General | Editors | Text Editors | Annotations).

    The build whose output is parsed for this purpose is the same build that produces your object files (and these indicators are only updated when you invoke a build, such as via Project | Build Project). As such, __CDT_PARSER__ is not used for this build.

  2. It can analyze your code using its own parser, and point out errors and warnings that it spots. This is independent of your compiler, and happens as you type.

    This produces error and warning indicators with the annotation types Codan Errors and Codan Warnings ("codan", short for code analysis, being the component that produces these). There is also C/C++ Indexer Markers for syntax errors, which are produced during the parsing itself.

    This, naturally, does use __CDT_PARSER__.

    Note that CDT's code analysis does not catch all of the errors (or warnings) that your compiler does. There is also the possibility of it producing false positive errors or warnings, which can often happen due to an indexer configuration that doesn't match the build configuration, but sometimes also happens due to bugs or deficiencies in CDT itself.

EDIT To further clarify, the indexer uses the same parser as codan (with __CDT_PARSER__ being defined and all) to build the index. (In fact, the indexer and the parser are not really separable, since the parser relies on the index to resolve names defined in included header files during parsing.)

Wyman answered 29/12, 2016 at 22:57 Comment(4)
I mean, some compiler invocations may be used for obtain error and warning indicators for various lines, while others - to populate the index. Those are not necessarily the same (I would think).Atheistic
@einpoklum: Ok, I see. Please see my edit, which hopefully clarifies things.Wyman
It clarifies some things, but not my question. I mean, what does the Indexer use to build its index? Does it make its own analysis or does it piggyback onto the code analyis you list as (2.) ?Atheistic
@einpoklum: The indexer uses the same parser as codan, with __CDT_PARSER__ being defined and all. (Edited again to clarify this.)Wyman

© 2022 - 2024 — McMap. All rights reserved.