Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] Newbie question to parse standalone C/C++ header file and retrieve all function declarations programmaticaly

Hi Alexandre,

Welcome! This is a good place to ask such questions. The forum is the end user support channel, people dealing with using, contributing, extending CDT APIs should feel free to ask here on the cdt-dev mailing list.

Here is a simple bit of code to get you started - this works for C/C++ projects, but won't work for standalone C files:

    IFile file = ...
    ICElement elem = CoreModel.getDefault().create(file);
    if (elem instanceof ITranslationUnit) {
        ITranslationUnit tu = (ITranslationUnit) elem;
        ICElement[] children = tu.getChildren();

I don't know what public API there is to do the above without a CProject - if any. The file does not have to be in the project, for example you can have a CProject and an external file, see for e.g. CoreModel.createTranslationUnitFrom. You may also find CoreModelUtil[2] useful.

The API reference for CDT is here: - sadly the navigation is currently broken[3], so the search box does not work. I find the Javadoc view in Eclipse the most effective.

PS a google search with these flags will allow you to search the cdt-dev archives: inurl:/lists/cdt-dev



Jonah Graham
Kichwa Coders

On Fri, 25 Jun 2021 at 08:49, Alexandre Honorat via cdt-dev <cdt-dev@xxxxxxxxxxx> wrote:

I am new to using Eclipse CDT directly from its Java API. So sorry if my question is irrelevant.
Actually I've read in this post ( ) that this list is more indicated than the forum for my type of question.
Unfortunately I could not easily look at all the archived emails to see if my question is already answered :/ sorry if it is the case.

Anyway, here is my problem and thank you in advance for your help :
I want to parse standalone (I don't want to resolve the includes) C and C++ header files (I get an IFile object pointing to each of them) and retrieve all function declarations (as IFunctionDeclaration objects I guess). In short, I want to retrieve the content of the outline view, but programmatically and directly on an IFile of any project not being C/C++ project.

After some hours of googling, I found a few relevant posts but I failed to obtain a MWE from them.
Moreover the snippets I tried are using the AST whereas I believe that the C-Model or the C-Index seem more convenient to get the function declarations only.
- (does not really indicate how to retrieve the functions)

I have tried a small part of the last sourced link in my own project, especially by using
GPPLanguage.getDefault().getASTTranslationUnit(fileContent, info, emptyIncludes, index, options, log).getDeclarations()
However, all the functions declared in the header are fetched with the CPPASTSimpleDeclaration type instead of IFunctionDeclaration or something similar. So I don't know if it is an error in my code or if I am only missing a few more steps.
I can provide more comprehensive examples and source files if needed.

I guess this is dummy question, but unfortunately I also had troubles to read the doc: I did not find the javadoc of the CDT project online, did I miss a link?

Best regards,

Alexandre Honorat

cdt-dev mailing list
To unsubscribe from this list, visit

Back to the top