1

I'm trying to compile some code (using GCC 4.8.2) and am getting an error: expression cannot be used as a function.

Here is the relevant code.

debug.h

// A macro for code which is not expected to be reached under valid assumptions
#if !defined(NDEBUG)
#define UNREACHABLE() do { \
    ERR("\t! Unreachable reached: %s(%d)\n", __FUNCTION__, __LINE__); \
    assert(false); \
    } while(0)
#else 
    #define UNREACHABLE() ERR("\t! Unreachable reached: %s(%d)\n", __FUNCTION__, __LINE__)
#endif

someFile.cpp (only the default line is really relevant)

HLSLBlockEncoder::HLSLBlockEncoderStrategy HLSLBlockEncoder::GetStrategyFor(ShShaderOutput outputType)
{
    switch (outputType)
    {
      case SH_HLSL9_OUTPUT: return ENCODE_LOOSE;
      case SH_HLSL11_OUTPUT: return ENCODE_PACKED;
      default: UNREACHABLE(); return ENCODE_PACKED;
    }
}

Error:

/.../debug.h:123:90: error: expression cannot be used as a function
     #define UNREACHABLE() ERR("\t! Unreachable reached: %s(%d)\n", __FUNCTION__, __LINE__)
                                                                                          ^
/.../someFile.cpp:217:16: note: in expansion of macro 'UNREACHABLE'
       default: UNREACHABLE(); return ENCODE_PACKED;
                ^

I am trying to understand why the error is happening. Looking at this question I thought maybe the issue was that the function (HLSL...) was being used as a variable due to __FUNCTION__ in the macro. But according to the GCC documentation: "GCC provides three magic variables which hold the name of the current function, as a string", so I don't believe that is the problem. Any other ideas?

Community
  • 1
  • 1
user2424607
  • 295
  • 3
  • 19
  • 1
    Where is `ERR()` defined? Is `NDEBUG` defined? Also, try running `gcc` with the `-E` command-line switch. That will dump the preprocessor results to the output file, which can help when debugging macros (as long as the error you're trying to debug isn't occurring in the preprocessor). – Jason R Oct 22 '15 at 19:33
  • It's defined in debug.h as `#define ERR(message, ...) (void(0))` – user2424607 Oct 22 '15 at 19:34
  • Disregard my earlier comment; I see now that the error occurred during expansion of the macro. – Jason R Oct 22 '15 at 19:36
  • 2
    Cannot reproduce. Please post a [MCVE](http://stackoverflow.com/help/mcve). Perhaps `ERR` is not really what you think it is. – M.M Oct 22 '15 at 19:43
  • You have to provide `ERR` – Support Ukraine Oct 22 '15 at 20:10
  • As previously stated, `ERR` is defined as `#define ERR(message, ...) (void(0))` and `NDEBUG` is defined (so it is using the definition of `UNREACHABLE` without the assert statement). I am currently trying to produce a MCVE. Will post update when I have it ready. – user2424607 Oct 22 '15 at 20:22

1 Answers1

1

Updating this with the solution I found.

Thanks to those above who told me to investigate ERR more. It turns out there was a duplicate definition of ERR in another header file that seems to have been causing my error. Changing the definition of ERR in debug.h to avoid this collision fixed my problems. :)

user2424607
  • 295
  • 3
  • 19