Author Topic: Code Completion fails at extern declared functions  (Read 17859 times)

Offline christobal

  • Multiple posting newcomer
  • *
  • Posts: 41
Code Completion fails at extern declared functions
« on: December 01, 2009, 11:29:00 am »
Hi,

The Code Completion fails if a function (and probably variables as well, but I haven't checked) is declared with the extern keyword.

The reason is IMHO the check for 'extern "C"' in src/plugins/codecompletion/parser/parserthread.cpp at line 479 (Rev. 5874) where the Token is read, but don't given back if it's not ParserConsts::kw_C.

I have patched the code, so that the Token will be "unget" in case the extern "C" - check fails.

Code
        }
        else if (token==ParserConsts::kw_extern)
        {
            // check for "C"
            m_Str = m_Tokenizer.GetToken();
            if (m_Str==ParserConsts::kw_C)
            {
                m_Tokenizer.GetToken(); // "eat" {
                DoParse(); // time for recursion ;)
            }
            else
            {
                // do nothing, just skip keyword "extern", otherwise uncomment:
                //SkipToOneOfChars(ParserConsts::semicolon); // skip externs
               m_Tokenizer.UngetToken();
            }
            m_Str.Clear();
        }

can anyone confirm this?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9693
Re: Code Completion fails at extern declared functions
« Reply #1 on: December 01, 2009, 01:00:48 pm »
The Code Completion fails
What do you mean with "fails"? Is it not visible? Because this works fine here...?!
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline christobal

  • Multiple posting newcomer
  • *
  • Posts: 41
Re: Code Completion fails at extern declared functions
« Reply #2 on: December 01, 2009, 01:54:04 pm »
Sorry, I think I was not very clear about this  :oops:. The Code Completion doesn't work if you have only the declaration. I use a library which has all functions defined as extern in it's header file.

As long as the implementation is part of the project, the codecompletion works (but not the "Find declaration of: ..." from the Right Mouse Button menue).

You can repoduce it if you open an empty project and add these lines:
Code
int foobar_test(void);
extern int foobar_blah(void);


int main (void)
{

}

If you now type foobar within main, you will only get codecompletion for foobar_test. foobar_blah has not been recognized (as you can see as well in the Symbol Browser).

BTW: I use Code::Blocks SVN Rev. 5895

Offline blueshake

  • Regular
  • ***
  • Posts: 458
Re: Code Completion fails at extern declared functions
« Reply #3 on: December 01, 2009, 02:09:34 pm »
confirm it.but since it is declared outside.it will be parsed correctly if the file exists where it is declared.


Edit:

thanks for your patch.
« Last Edit: December 01, 2009, 02:17:42 pm by blueshake »
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Offline christobal

  • Multiple posting newcomer
  • *
  • Posts: 41
Re: Code Completion fails at extern declared functions
« Reply #4 on: December 01, 2009, 02:22:15 pm »
Quote
confirm it.but since it is declared outside.it will be parsed correctly if the file exists where it is declared.

I guess you mean that it will be parsed correctly if the file exists where it is implemented? That is right, but if a library is used, the implementation is not part of the project. Therefore we need to parse the declaration correctly.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9693
Re: Code Completion fails at extern declared functions
« Reply #5 on: December 01, 2009, 02:47:04 pm »
That is right, but if a library is used, the implementation is not part of the project.
It's pretty weird to declare a declaration for a library function as "extern" in the interface, isn't it?!
However, yes - it's reproducible.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline christobal

  • Multiple posting newcomer
  • *
  • Posts: 41
Re: Code Completion fails at extern declared functions
« Reply #6 on: December 01, 2009, 03:59:56 pm »
Quote
It's pretty weird to declare a declaration for a library function as "extern" in the interface, isn't it?!

I have to admit that it has no effect on function declarations, but it is necessary for declarations of variables. Could you confirm that the patch above works correctly? I don't have a clue about the codecompletion plugin and so it's just a wild guess... :)

Offline ivan1979

  • Single posting newcomer
  • *
  • Posts: 5
Re: Code Completion fails at extern declared functions
« Reply #7 on: March 12, 2010, 03:49:19 pm »
Hi,

i'm very interesting  in a libcodecompletion.so that implements extern resolution also, can anyone help me?
where can i found it?
Actually i include in mu C::B C parser a gcc arm toolchain and very many functions are declarated as extern so codecompletion cannot resolve those  :(
Thanks

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6034
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Code Completion fails at extern declared functions
« Reply #8 on: March 12, 2010, 04:06:02 pm »
Hi,

i'm very interesting  in a libcodecompletion.so that implements extern resolution also, can anyone help me?
where can i found it?
Actually i include in mu C::B C parser a gcc arm toolchain and very many functions are declarated as extern so codecompletion cannot resolve those  :(
Thanks
Sorry If I can't get your idea.
You want to get the function declaration from a "shared library"?
@christobal
I'm confused about your discussion, can you give a simple test case? or where is the patch?

By the way, this thread should be moved to the CodeCompletion redesign subforum.
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 ivan1979

  • Single posting newcomer
  • *
  • Posts: 5
Re: Code Completion fails at extern declared functions
« Reply #9 on: March 12, 2010, 04:49:01 pm »
Sorry,
i think plugin is implemented by a library (libcodecompletion.so) o no???

Offline christobal

  • Multiple posting newcomer
  • *
  • Posts: 41
Re: Code Completion fails at extern declared functions
« Reply #10 on: March 15, 2010, 08:38:12 am »
@christobal
I'm confused about your discussion, can you give a simple test case? or where is the patch?

The Patch is the code snippet from my fist post and the test case is the code snippet from my second post.

The problem is, that every token after the "extern" keyword get "swallowed" by the tokenizer without evaluation, and so the extern declared symbol won't appear in the codecompletion unless the implementation of this symbol can be found elsewhere in the project. In most cases, you will have the implementation in the same project and so the codecompletion will work. But if you use a library in your project and just have the headers, you won't see any symbols that have been declared with the extern keyword.

I hope I made this clear   :)

By the way, this is just a bugfix and has IMHO nothing to do with the CodeCompletion redesign.
« Last Edit: March 15, 2010, 08:43:40 am by christobal »

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6034
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Code Completion fails at extern declared functions
« Reply #11 on: March 15, 2010, 08:54:58 am »
@christobal
Ok, I see. If you have both a declaration and a function declaration in your project, how can CC locate the true location of a function declaration?

For example, you have A.h, and it contains:
Code
extern void f();

And in B.h, you have :
Code
void f();

Then, both A.h and B.h was in your project, so, the parser must have two "f" declarations if your not skip extern statement.

Technically, it is really easy to handle "extern" statement, but we need some comments from CB's developers. :D
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 ivan1979

  • Single posting newcomer
  • *
  • Posts: 5
Re: Code Completion fails at extern declared functions
« Reply #12 on: March 15, 2010, 10:34:36 am »
Hi,
that's my problem!
I include many arm-linux toolchain headers (pthread.h, sys/time.h, ...) but i can't see functions tooltips help because they are all declarated as extern functions...

Offline ivan1979

  • Single posting newcomer
  • *
  • Posts: 5
Re: Code Completion fails at extern declared functions
« Reply #13 on: March 15, 2010, 04:39:40 pm »
Hi,

i have just recompiled codeblocks and modify the parserthread.cpp on according christobal patch, so i replaced the libcodecomplete.so with the new owe...it works!!!

Thanks
Ivan