https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110558

            Bug ID: 110558
           Summary: __has_include argument expansion results in unexpected
                    filename
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: preprocessor
          Assignee: unassigned at gcc dot gnu.org
          Reporter: provisorisch at online dot de
  Target Milestone: ---

Hello!
I am building a path to an include file using a function like macro and then
checking if the file exists using __has_include.
However, if the argument to that macro is padded with spaces it will fail to
find the file.

Here an example with following 2 files in the same directory:


my.header.h:
--------
#pragma message("included my.header.h")
--------


test.c:
--------
#ifndef USE_IMPL
#   define USE_IMPL my
#endif

#define STRINGIFY_(X) #X
#define STRINGIFY(X) STRINGIFY_(X)
#define MAKE_INCLUDE_PATH_(IMPL, NAME) STRINGIFY(IMPL.NAME)
#define MAKE_INCLUDE_PATH(NAME) MAKE_INCLUDE_PATH_(USE_IMPL, NAME)

// checking __has_include with spaces
#if __has_include(MAKE_INCLUDE_PATH( header.h )) // this will return false!
#   pragma message("\""MAKE_INCLUDE_PATH( header.h )"\" exists")
#else
#   pragma message("\""MAKE_INCLUDE_PATH( header.h )"\" does NOT exist!") //
except it does
#endif
#include MAKE_INCLUDE_PATH( header.h ) // works nonetheless

// checking __has_include without spaces
#if __has_include(MAKE_INCLUDE_PATH(header.h)) // works
#   pragma message("\""MAKE_INCLUDE_PATH(header.h)"\" exists")
#else
#   pragma message("\""MAKE_INCLUDE_PATH(header.h)"\" does NOT exist!")
#endif
#include MAKE_INCLUDE_PATH(header.h)


int main() { return 0; }
--------


Then to compile:

# gcc -Wall -Wextra test.c                                                     
                                                                               
                                                                               
          test.c:14:12: note: ‘#pragma message: "my.header.h" does NOT exist!’
   14 | #   pragma message("\""MAKE_INCLUDE_PATH( header.h )"\" does NOT
exist!") // except it does
      |            ^~~~~~~
In file included from test.c:16:
my.header.h:1:9: note: ‘#pragma message: included my.header.h’
    1 | #pragma message("included my.header.h")
      |         ^~~~~~~
test.c:20:12: note: ‘#pragma message: "my.header.h" exists’
   20 | #   pragma message("\""MAKE_INCLUDE_PATH(header.h)"\" exists")
      |            ^~~~~~~
In file included from test.c:24:
my.header.h:1:9: note: ‘#pragma message: included my.header.h’
    1 | #pragma message("included my.header.h")
      |         ^~~~~~~


The first __has_include fails to find "my.header.h" but the subsequent #include
works.
The second __has_include works as expected.

Using strace I noticed that an extra space character ended up in the filename:
openat(AT_FDCWD, "my. header.h", O_RDONLY|O_NOCTTY) = -1 ENOENT (No such file
or directory)

I would have expected the first __has_include to behave like the second one.

Reply via email to