Reputation: 119
To build a Eclipse plugin upon Eclipse CDT(NOT developing CXX using CDT), by using the API provided by Eclipse CDT, for the following code snippet, in Eclipse CDT, we can detect the ifdef statement(See IASTPressorIfStatement), but how can we obtain the statements under this ifdef directive. Specifically, in this example, we want to get the code "#include ". Or is there any other tools could do this?
#ifdef HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
#endif
For example, in the code snippet, we want to get statements within the structure <#ifdef ...,#endif>. The return should be "#include".
For a normal case, let have the code snippet like:
#ifdef condition
statement A;
statement B;
.....
statement Z;
#endif
I want to get the statement A, statement B,... and statement Z.*Note in the preprocessor statement, like IASTPreprocessorIfdefStatement, we can extract the macro name but not the statements I mentioned.
Upvotes: 2
Views: 201
Reputation: 52837
In general, the region inside an #ifdef
block need not contain complete declarations (or indeed complete AST nodes of any kind). For example, consider:
int i =
#ifdef FOO
42
#else
43
#endif
;
This is valid code, but it does not make sense to ask for the declarations contained in the #ifdef
or #else
branches, because neither contains a complete declaration.
My best suggestion is to use an ASTVisitor
to traverse the AST, and compare the offset and length of nodes you're interested in to the offset and length of the preprocessor statements. (Offsets and lengths of nodes can be obtained via IASTNode.getFileLocation()
.) If the AST node is contained entirely within, say, an #ifdef
and an #else
, you know it's inside that branch. If the AST node overlaps the boundary of a preprocessor branch, you're in a situation like my example above, and it's up to you what you want to do with it.
Upvotes: 1