Author Topic: Help on code completion  (Read 8360 times)

Offline ouille

  • Multiple posting newcomer
  • *
  • Posts: 21
Help on code completion
« on: July 06, 2018, 06:05:11 pm »
Hello,

I use codeblocks to develop a linux kernel module. I manage to have the build process ok without error. Great !

But something goes wrong with code completion.

I have "add file recursively" added linux headers to my project. And code completion works as expected. I save the project.

If i close and re open codeblocks, codecompletion doesn't work any more. I need to "add file recursively" linux headers again to have correct behavior, but they are still in the project.

Am i doing something wrong ?

Thank for help.
Ouille
« Last Edit: July 06, 2018, 06:12:55 pm by ouille »

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Help on code completion
« Reply #1 on: July 06, 2018, 09:30:18 pm »
What version of codeblocks are you using?

Offline ouille

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Help on code completion
« Reply #2 on: July 07, 2018, 11:57:20 am »
Hello,

I use Codeblocks 17.12 : build jan 6 2018 10:12:19 , Windows 7 Pro

I've also tried "reparse this project" option without success.

It seem that parser doesn't run on headers on project loading ...

Have a nice day.
Ouille.
« Last Edit: July 08, 2018, 12:01:01 pm by ouille »

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Help on code completion
« Reply #3 on: July 09, 2018, 12:24:38 pm »
Can you try the latest nightly build? There were some changes made with path management and cc...

In all the cc for codeblocks is not the best anymore... There are better in work, but this will take time to develop...
this is one that works quite nice, but it is hard to install.. you may have a look at it https://github.com/yvesdm3000/ClangLib/tree/staging

Offline ouille

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Help on code completion
« Reply #4 on: November 07, 2018, 08:57:59 pm »
Hello,

I'm trying again.

I just manage to compile codeblocks from svn trunk. So i think i work on the last release.

Here is what i found :
Loading project which contain single .c file and all linux kernel headers files, here is the parsing log :
Quote
NativeParser::DoFullParsing(): AddProjectDefinedMacros failed!
NativeParser::DoFullParsing(): Adding cpp/c files to batch-parser
NativeParser::DoFullParsing(): Added 1 source file(s) for project 'cb_module' to batch-parser...
NativeParser::CreateParser(): Finish creating a new parser for project 'cb_module'
NativeParser::OnParserStart(): Starting batch parsing for project 'cb_module'...
NativeParser::OnParserEnd(): Project 'cb_module' parsing stage done!
Project 'cb_module' parsing stage done (9748 total parsed files, 272 tokens in 0 minute(s), 0.196 seconds).

Reparsing project :

Quote
NativeParser::DeleteParser(): Deleting parser for project 'cb_module'!
NativeParser::DoFullParsing(): AddProjectDefinedMacros failed!
NativeParser::DoFullParsing(): Adding cpp/c files to batch-parser
NativeParser::DoFullParsing(): Added 1 source file(s) for project 'cb_module' to batch-parser...
NativeParser::CreateParser(): Finish creating a new parser for project 'cb_module'
NativeParser::OnParserStart(): Starting batch parsing for project 'cb_module'...
NativeParser::OnParserEnd(): Project 'cb_module' parsing stage done!
Project 'cb_module' parsing stage done (9748 total parsed files, 272 tokens in 0 minute(s), 0.177 seconds).

Adding recursively linux kernel headers files

Quote
NativeParser::OnParserStart(): Starting add file parsing for project 'cb_module'...
... Lots of :
ReadVarNames() : Unexpected token '((packed))' for 'v4l2_event_vsync', file '/usr/src/linux-headers-4.4.0-130/include/uapi/linux/videodev2.h', line 2046.
ReadVarNames() : Unexpected token '((packed))' for 'v4l2_dbg_match', file '/usr/src/linux-headers-4.4.0-130/include/uapi/linux/videodev2.h', line 2143.
ReadVarNames() : Unexpected token '((packed))' for 'v4l2_dbg_register', file '/usr/src/linux-headers-4.4.0-130/include/uapi/linux/videodev2.h', line 2150.
ReadVarNames() : Unexpected token '((packed))' for 'v4l2_dbg_chip_info', file '/usr/src/linux-headers-4.4.0-130/include/uapi/linux/videodev2.h', line 2161.
...
Project 'cb_module' parsing stage done (9935 total parsed files, 494443 tokens in 0 minute(s), 34.161 seconds).

At this point everything is ok !

Reparsing project :

Quote
Reparsing when typing for editor /home/jl/Modules/cb_module/mod1.c
NativeParser::OnParserStart(): Starting re-parsing for project 'cb_module'...
Project 'cb_module' parsing stage done (9935 total parsed files, 494444 tokens in 0 minute(s), 0.005 seconds).
NativeParser::DeleteParser(): Deleting parser for project 'cb_module'!
NativeParser::DoFullParsing(): AddProjectDefinedMacros failed!
NativeParser::DoFullParsing(): Adding cpp/c files to batch-parser
NativeParser::DoFullParsing(): Added 1 source file(s) for project 'cb_module' to batch-parser...
NativeParser::CreateParser(): Finish creating a new parser for project 'cb_module'
NativeParser::OnParserStart(): Starting batch parsing for project 'cb_module'...
NativeParser::OnParserEnd(): Project 'cb_module' parsing stage done!
Project 'cb_module' parsing stage done (9748 total parsed files, 272 tokens in 0 minute(s), 0.150 seconds).

As i can debug codecompletion i'll continue to try to find what is going wrong.

Any idea welcome.

I have now to fully understand code completion code ... ouch

Have a nice day.
Ouille

Offline ouille

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Help on code completion
« Reply #5 on: November 08, 2018, 03:30:58 pm »
Hello,

It seem that parser by default only parse .c/.cpp files.

Modifying line 1093 of nativearser.cpp with :
Code
            if ((ft == ParserCommon::ftSource) || (ft==ParserCommon::ftHeader)) // parse source files and Headers Files

Will parse project as I expect !

Is there a raison why .h are not parsed on project load or project reparse  (but parsed when added to the project ?).

Is it the right way of handling this case ?

Have a nice day.
Ouille.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Help on code completion
« Reply #6 on: November 08, 2018, 07:45:59 pm »
The reason (probably) is that C/C++ are based on translation units and header files are not translation unit.

Are those headers you need parsed included in some .c file?

I doubt this is the correct solution. A header file has meaning only when it is included in a .c/cpp file.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Help on code completion
« Reply #7 on: November 09, 2018, 04:18:59 am »
Code
NativeParser::DoFullParsing(): AddProjectDefinedMacros failed!
From your log, I see this is an error.


Quote
Is there a raison why .h are not parsed on project load or project reparse  (but parsed when added to the project ?).

If you have two a.c and b.c, and a.h and b.h
Here is a.c
Code
#include "a.h"
#include "b.h"
...

Here is b.c
Code
#include "a.h"
#include "b.h"
...

Now the logic is, our parser just parse the a.c and b.c, but note that the a.h and b.h is parsed when parsing the a.c. After that, when parsing b.c, those headers are just skipped.

I hope you can understand this logic.

EDIT:
Reparse the project should rebuild the whole thing, so if you have updated the header file, reparse the project should work, so what's the exact problem you have?
« Last Edit: November 09, 2018, 04:20:44 am by ollydbg »
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ouille

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Help on code completion
« Reply #8 on: November 09, 2018, 09:03:36 pm »
Hello,

Thank's for your reply.

First : The project is in attachment. It's a small linux module WIP.
It build ok.
But using CC on inode for example didn't give any hints, copy_to_user didn't complete.
Forcing parsing .h files -> CC work well on inode and copy_to_user for example.
Perhaps project structure is not correct ?

My comments :

Quote
The reason (probably) is that C/C++ are based on translation units and header files are not translation unit.
Ok, but are they parsed when files are added to project.

Quote
Are those headers you need parsed included in some .c file?
Not directly, but project builds ok. So they are found by some way, it's mystery for me ;o)

Quote
I doubt this is the correct solution. A header file has meaning only when it is included in a .c/cpp file.
Probaly not, but i try to undestrand ;o)

Code
NativeParser::DoFullParsing(): AddProjectDefinedMacros failed!
I don't know where this error come from.
Do you think it can be related ?

For the rest i'm ok is your explanations. But why can't i use CC on a.h if it parsed at least once ?

Looking in Editor / CC options I fond thise check box (see attachment).

Exhibit a "Enable Headers Code Completion".

But this options doesn't seemed to be used in codeblocks inner code.

Comments and remarks welcome.
Have a nice day.






Offline ouille

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Help on code completion
« Reply #9 on: November 09, 2018, 09:09:27 pm »
Hello,

I've try to handle "Enable Headers Code Completion" inside codeblock CC.

Here is a patch (see attachment).

If option checked, headers are parsed / and reparsed correctly (can be slow on linux headers).

If unchecked : previous behavior.

Feedback welcome.

Have a nice day.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Help on code completion
« Reply #10 on: November 10, 2018, 04:08:55 pm »
Hello, ouille, thanks for the contribution!
Here are some of my comments about the patch:
1, It looks like you forgot to add the file changes of "plugins\codecompletion\resources\settings.xrc" in the patch?
2, About how the add the files to the batch parser(the thread pool), which will parse the files in FIFO(first in first out) mode as they added.
Let's say, some headers files are not included in any translation unit files. Such as a file named "c.h" in my previous post. (Re: Help on code completion)

How, what I think the way is: first add the translation unit files(the .c or .cpp files) first, and then the header files.
So, we have such sequence:
Code
a.c
b.c
a.h
b.h
c.h
When parsing a.c, the a.h and b.h will be parsed.
When parsing b.c, no need to parse its included file a.h and b.h, since they are already in symbol tree.
Next, a.h and b.h are just skipped since they are already in the symbol tree.
Last, when we look at c.h, we can't find this header files in the symbol tree, so we parse it.

3, Please note there is an option named "Enable headers code-completion", which means when user hit the "<" after he typed the "#include", the header file names will be prompted for auto suggestion. This is not the same thing was we discussed in this thread.  ;)


« Last Edit: November 10, 2018, 04:24:46 pm by ollydbg »
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ouille

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Help on code completion
« Reply #11 on: November 10, 2018, 09:54:14 pm »
Hello,

Thank you for your feedback.

Quote
3, Please note there is an option named "Enable headers code-completion", which means when user hit the "<" after he typed the "#include", the header file names will be prompted for auto suggestion. This is not the same thing was we discussed in this thread.  ;)
OK I was totally wrong ! I use this option to implement header parsing ... sorry, i have to revert now !

Quote
1, It looks like you forgot to add the file changes of "plugins\codecompletion\resources\settings.xrc" in the patch?
It is not modified, reason above.

Quote
2, About how the add the files to the batch parser(the thread pool), which will parse the files in FIFO(first in first out) mode as they added.
OK, it's the right way. I'll try find where and then I'll propose another patch.

Thank you for reviewing.

Have a nice day.
Ouille.


Offline ouille

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Help on code completion
« Reply #12 on: November 11, 2018, 07:24:57 pm »
Hello,

So, here's a new version of the patch.

An option has been added to "settings -> editor -> code completion -> C/C+++ Parser"
This time, "settings.xrc" is well handled in the patch.

If "Parse headers" is selected, the project header files are added at the end of the list of files to be parsed.

Otherwise, we find the old behavior.

Is the option useful? Is it really annoying to parse the header files that have been added to the project?

Returns are welcome.

Have a good day.

Translated with www.DeepL.com/Translator
Ouille.


Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Help on code completion
« Reply #13 on: November 11, 2018, 09:36:19 pm »
Is the option useful? Is it really annoying to parse the header files that have been added to the project?
I still think that you're solving the wrong problem.
Your problem is that a header is not parsed because it is included behind some complex macro logic which our parser doesn't understand or something similar.

If you have to spend time and find out why the file is not included and not add just a workaround which in most cases would waste time and memory and probably would provide wrong information in headers which depend on some marcos being defined.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ouille

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Help on code completion
« Reply #14 on: November 12, 2018, 08:41:37 am »
Hello,

Quote
Your problem is that a header is not parsed because it is included behind some complex macro logic which our parser doesn't understand or something similar.

The remark is interesting. I will test to see if the parser is actually lost in the headers tree.

The idea was however, to add the includes of the Linux kernel to have the completion of the functions without having to add #include in first intention. A kind of help in writing the code.
It is then easier to use a function and add the header afterwards. It would seem logical to me to have this functionality as long as we can voluntarily add independent headers to the project.

Perhaps this is not a good way of working. In this case I would keep the patch for this type of development. It's not embarrassing, as it's really easy to build codeblocks from source.

However, there is still a slight inconsistency, since these headers files are parsed when they are added to the project.

Have a nice day.
Ouille.