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,

A declaration like CPPASTSimpleDeclaration contain a declaration specifier and declarators. For example this is a declaration with an “int” specifier containing two declarators (a function and a variable):

So you can call CPPASTSimpleDeclaration.getDeclarators and on each declarator do an instanceof check and cast to IASTFunctionDeclarator or a more derived type as needed (IASTStandardFunctionDeclarator, ICPPASTFunctionDeclarator, etc). Then on a declarator you can call getName (an IASTName) if you need the name. There are also helpers in ASTStringUtil if you want the whole signature String from a declarator.

Depending on what information you want to retrieve, this might be enough. Otherwise you can go to other representation of the code, like the "semantic model". To do that, you typically need an IASTName (like mentioned above) and call resolveBinding on it. Then IBinding can be instanceof/cast to a whole different tree of types, including IFunction. IBinding is also what you can use to query the index: IIndex.findReferences, etc.

There are probably several ways to go about this but going through the AST and semantic model are the more powerful/detailed ways.

For the possibility of going through the CModel, constructing possibly an ExternalTranslationUnit and retrieving IFunctionDeclaration. I don’t know on top of my head but I’m not sure you could do that without creating a “fake” CDT project. But you are close to getting it working with AST only so it might be best to stick with this even if a bit lower level than you would like.


PS: The DOM AST view in my screenshot is in the plugin org.eclipse.cdt.ui.tests. Available either when using CDT from source or by installing the “C/C++ Unit Testing Support” from the CDT p2 repository.

On Jun 25, 2021, at 12:10 PM, Jonah Graham <jonah@xxxxxxxxxxxxxxxx> wrote:

Hi Alexandere, 

Your case is what I meant when I said you needed a CDT project. I don't know what API there is for non-CDT projects to parse. For non cdt projects that method is supposed to return null. 

The CEditor can open and display an outline because it makes a workingcopy. But I don't know what api is available to access that externally.


On Fri., Jun. 25, 2021, 12:01 Alexandre Honorat, <alexandre.honorat@xxxxxxxx> wrote:
Hello Jonah,

thanks for your answer and all the links to the doc!
I tried your method but unfortunately CoreModel.getDefault().create(file); returns me a null reference.
Yet, my input IFile is correct since I open it in the same method with FileContent.create(file); for example.

So I have looked into the code directly, and I guess an error occurs but I cannot see which one since Core errors
are captured without messages in core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ (line 325, master branch).

Do you have any quick idea why I could get a null reference ? I have to precise that I am using CDT 9.11 and that my file is already part of an Eclipse project of another type (specific to the Eclipse feature we are developping, so not a CProject).

I guess I have to set some parameters of the default CoreModel before using it, I'll try to get that from the doc.

Thank you,

Alexandre Honorat

Le vendredi 25 juin 2021 à 15:41:35 UTC+2, Jonah Graham <jonah@xxxxxxxxxxxxxxxx> a écrit :

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
cdt-dev mailing list
To unsubscribe from this list, visit

Back to the top