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 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.

Jonah

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/CModelManager.java (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: https://help.eclipse.org/2021-06/topic/org.eclipse.cdt.doc.isv/reference/api/overview-summary.html?cp=15_1_0 - 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 site:eclipse.org


HTH,
Jonah

~~~
Jonah Graham
Kichwa Coders
www.kichwacoders.com


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

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 ( https://www.eclipse.org/forums/index.php/t/1087955/ ) 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.
- https://wiki.eclipse.org/CDT/User/FAQ#I_am_developing_a_plug-in_that_requires_a_parsing_a_c_file_and_obtaining_the_information_such_as_method_signatures.2C_parameter_types_etc._How_can_I_do_this_using_the_CDT_parser.3F (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
cdt-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/cdt-dev

Back to the top