public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug preprocessor/110558] New: __has_include argument expansion results in unexpected filename
@ 2023-07-05 11:29 provisorisch at online dot de
  2023-07-05 13:21 ` [Bug preprocessor/110558] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: provisorisch at online dot de @ 2023-07-05 11:29 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-03-14 11:36 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-05 11:29 [Bug preprocessor/110558] New: __has_include argument expansion results in unexpected filename provisorisch at online dot de
2023-07-05 13:21 ` [Bug preprocessor/110558] " pinskia at gcc dot gnu.org
2023-07-05 14:46 ` provisorisch at online dot de
2023-12-11 10:44 ` fw at gcc dot gnu.org
2023-12-11 22:06 ` lhyatt at gcc dot gnu.org
2023-12-12 23:20 ` lhyatt at gcc dot gnu.org
2024-03-14 11:33 ` cvs-commit at gcc dot gnu.org
2024-03-14 11:36 ` lhyatt at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).