public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "provisorisch at online dot de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug preprocessor/110558] New: __has_include argument expansion results in unexpected filename
Date: Wed, 05 Jul 2023 11:29:59 +0000	[thread overview]
Message-ID: <bug-110558-4@http.gcc.gnu.org/bugzilla/> (raw)

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	other threads:[~2023-07-05 11:30 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-05 11:29 provisorisch at online dot de [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-110558-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).