Search code examples
cpowerpcmisra

What is the __builtin_expect() prototype?


To get rid of a static code analysis warning (QA-C), I need to provide a function prototype for __builtin_expect().

I am using the WindRiver Diab compiler for PowerPC. In the compiler manual I have found the following information:

__builtin_expect(long exp, long c): ... exp is also the return value.

So, my prototype is as follows:

long __builtin_expect(long exp, long c);

However, it does not compile, I am getting the following error:

error (dcc:1701): invalid types on prototype to intrinsic __builtin_expect - when the intrinsic is enabled, optional user prototype must match

It seems like my prototype is not correct. What is the correct prototype for __builtin_expect?

The error message states that the user prototype is optional. So it should be possible to define it, right?


Solution

  • You need to somehow define __builtin_expect to make your static analyzer happy, because it doesn't know what that function is. But you need to use #ifdef to disable that definition when you are compiling your program normally, because your compiler will not like it if you try to define compiler builtins yourself. The builtins come with the compiler so they are not supposed to be defined in your program.

    Something like this would work:

    #ifdef _HEY_I_AM_RUNNING_STATIC_ANALYZER
    #define __builtin_expect(e,c) (e)
    #endif
    

    I don't know the details of how your static analyzer works, so I don't know what the right macro is to test in the #ifdef. You can read the documentation of your static analyzer to find out if it defines any preprocessor symbols by default, or if you can tell it what preprocessor symbols to define when you run it.