Author Topic: references for static inline functions  (Read 10409 times)

aleung

  • Community Member
  • Posts: 9
  • Hero Points: 0
references for static inline functions
« on: September 29, 2009, 10:41:07 PM »
I had a question about references (Ctrl+/) working with my inline functions.  Basically, if I declare an inline in a header as "static inline" (as opposed to simply "inline") I do not get references outside the current file:

--- inlines.h ---

static inline void *inlineFunc (someStruct *arg)
{
     // function details
}

--- file1.c ---

// call to inline function
buffer = inlineFunc (arg);

--- file2.c ---

// call to inline function
buffer = inlineFunc (arg);

If I do a references on inlineFunc from file1.c, I only get the file1.c and inlines.h references (i.e. file2.c does not show up).  The same thing happens when I do a references on inlineFunc from file2.c.  If I do a references from inlines.h, I get what is expected: file1.c, file2.c and inlines.h references.  If I make this non-static, it behaves as expected.  It seems that SlickEdit is treating this inline function as if it were a static function in a .c file that can only be accessed in that file.  However, it is an inline in a header that can be used in multiple source files.  Thus, it seems like references in one .c file should show all the references, including other .c files. Is there a workaround in SlickEdit (besides simply issuing the references from the header file) for this so I don't have to change my source code?

Thanks in advance!

ScottW, VP of Dev

  • Senior Community Member
  • Posts: 1471
  • Hero Points: 64
Re: references for static inline functions
« Reply #1 on: September 30, 2009, 06:04:27 PM »
According to Stroustrup (The C++ Programming Language, 3rd edition, Section 9.2 Linkage), the keyword "static" should not be used except inside of classes and functions. In the context you have used it, static means to use internal linking. No matter how many files you include inlines.h into, that function in each file is a different function. So, SlickEdit is correctly only showing you references to that same function, not functions with the same name.

Why are you declaring this function static? The code is correct without the static keyword.

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: references for static inline functions
« Reply #2 on: September 30, 2009, 08:23:24 PM »
@Scott: This allows macro-like usage of functions where appropriate. See GCC docs here for some details.
It's usually used for ultra-fast/optimized assembler functions e.g. in the Linux kernel etc.
There was already a somehow related problem report in the forum a while ago and I was hoping (didn't test it yet) that this stuff is not longer an issue...
HS2

ScottW, VP of Dev

  • Senior Community Member
  • Posts: 1471
  • Hero Points: 64
Re: references for static inline functions
« Reply #3 on: September 30, 2009, 09:07:54 PM »
I'll certainly defer to those with more experience here, but the speed benefit seems to come from the use of inline rather than static. It seems like the addition of static only affects the creation of separate assembly for the inlined function in cases where it cannot be inlined--which is what I would have expected anyway. Or maybe the addition of static prevents the generation of such separate assembly in the case where all instances could be integrated? I wish the documentation page you referenced was a bit more clear on that distinction.

The page does say that such instances are not equivalent, meaning that pointers to the functions would be different. So, it still seems like the behavior in SlickEdit is correct. These are different functions with the same name, so references shouldn't locate them.

I'll pass this along to Dennis, who is far more knowledgeable about such things.

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: references for static inline functions
« Reply #4 on: September 30, 2009, 10:07:52 PM »
That's a good idea !
However, even if it turns out that the occurences of these fct.s are not 'pure' references it's one of these stupid real world problems where great tools win ;)
I'll keep my finger crossed...
HS2

aleung

  • Community Member
  • Posts: 9
  • Hero Points: 0
Re: references for static inline functions
« Reply #5 on: September 30, 2009, 10:35:51 PM »
Taking the "static" out is not an option for me.  When I compile with GCC I get a multiple definition error:

Code: [Select]
$make
gcc -c file1.c
gcc -c file2.c
gcc -o inlines file1.o file2.o
file2.o: In function `inlineFunc':
file2.c(.text+0x0): multiple definition of `inlineFunc'
file1.ofile1.c(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [all] Error 1

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6866
  • Hero Points: 528
Re: references for static inline functions
« Reply #6 on: October 01, 2009, 05:05:29 PM »
This needs to work. The only work around I know requires lame preprocessing for the static keyword (preprocessing when compiled and a global define for SlickEdit too). Can't promise when this might be fixed yet but we hear your pain and are investigating.

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: references for static inline functions
« Reply #7 on: October 01, 2009, 05:56:17 PM »
Sounds good ! (I've seen that I still use '#define static static ' in usercpp.h...)
Thanks Clark, HS2

aleung

  • Community Member
  • Posts: 9
  • Hero Points: 0
Re: references for static inline functions
« Reply #8 on: October 01, 2009, 07:11:20 PM »
Clark/hs2 - thanks for your replies. I'll use preprocessing for static until the fix is released.

BTW, is there any reason to use "#define static static" instead of just "#define static"?  They both seem to work.

And finally, is the only side-effect of using preprocessing for static that if there are multiple “real”, non-inline, static functions in separate .c files, doing a references on one of them will yield all the functions, across the files?

Thanks again.
« Last Edit: October 01, 2009, 07:38:46 PM by aleung »

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: references for static inline functions
« Reply #9 on: October 01, 2009, 07:38:40 PM »
As far as I remember there was/is no good reason - but it seemed 'more correct' for me ;) - HS2

aleung

  • Community Member
  • Posts: 9
  • Hero Points: 0
Re: references for static inline functions
« Reply #10 on: October 01, 2009, 07:55:34 PM »
Thanks, HS2.  I snuck in another question in my last post, probably as you were replying.  Do you know of any side effects besides what I mentioned?

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: references for static inline functions
« Reply #11 on: October 01, 2009, 10:10:32 PM »
There is one additional, related side effect I know. The 'Symbol Properties' of these functions (activated e.g. via context menu of Defs TB or Symbols TB) don't show 'static' any more.
HS2

aleung

  • Community Member
  • Posts: 9
  • Hero Points: 0
Re: references for static inline functions
« Reply #12 on: October 13, 2010, 11:22:28 PM »
I looks like SE v15.0.1.3 has this exact same issue with static __inline functions. Only this time, the "#define static" C/C++ preprocessor define doesn't help. Anyone get around this?

Thanks in advance.