public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/109534] New: -fdirectives-only does not work with assembler-with-cpp
@ 2023-04-17  6:58 boris at kolpackov dot net
  2023-04-17  7:04 ` [Bug c/109534] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: boris at kolpackov dot net @ 2023-04-17  6:58 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109534
           Summary: -fdirectives-only does not work with
                    assembler-with-cpp
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: boris at kolpackov dot net
  Target Milestone: ---

I've noticed that -fdirectives-only has no effect when preprocessing
assembler-with-cpp files. Instead, one always get a fully preprocessed source:


$ cat <<EOF >test.S
#define MAIN_RESULT 2
.text
.global main
main:
  movq $MAIN_RESULT, %rax
  ret
EOF

$ gcc -E -fdirectives-only -o test.Si test.S
$ cat test.Si
# 0 "test.S"
# 0 "<built-in>"
# 0 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 0 "<command-line>" 2
# 1 "test.S"

.text
.global main
main:
  movq $2, %rax
  ret


Running the same command with -v reveals that -fdirectives-only is overriden
with -fno-directives-only:


$ gcc -v -E -fdirectives-only -o test.Si test.S
...
/usr/lib/gcc/x86_64-linux-gnu/12/cc1 -E -lang-asm -quiet -v -imultiarch
x86_64-linux-gnu test.S -o test.Si -mtune=generic -march=x86-64
-fdirectives-only -fasynchronous-unwind-tables -fno-directives-only -dumpbase
test.S -dumpbase-ext .S
...


And I've tracked this down to the following old gcc/ChangeLog-2007 entry:


2007-08-30  Ollie Wild  <aaw@google.com>

        * c-opts.c (c_common_handle_option): Support -fno-directives-only.
        * gcc.c (default_compilers): Add -fno-directives-only to
        @assembler-with-cpp.


Unfortunately there is no rationale (I've also checked the commit message and
there is nothing there either).

I then tried to manually remove -fno-directives-only and see what happens:


$ /usr/lib/gcc/x86_64-linux-gnu/12/cc1 -E -lang-asm -quiet -v -imultiarch
x86_64-linux-gnu test.S -o test.Si -mtune=generic -march=x86-64
-fdirectives-only -fasynchronous-unwind-tables -dumpbase test.S -dumpbase-ext
.S


The resulting test.Si file looked sensible so I tried to compile it further:


$ gcc -v -x assembler-with-cpp -fpreprocessed -fdirectives-only main.Sii


But -fno-directives-only is passed in this case as well so I had to tweak and
run the underlying commands manually:

$ /usr/lib/gcc/x86_64-linux-gnu/12/cc1 -E -lang-asm -quiet -v -imultiarch
x86_64-linux-gnu test.Si -mtune=generic -march=x86-64 -fpreprocessed
-fdirectives-only -fasynchronous-unwind-tables -o test.s


$ as -v --64 -o test.o test.s
test.Si:1: Error: junk at end of line, first unrecognized character is `0'

The beginning of test.s looks like this:

$ head -4 test.s
# 1 "test.Si"
 0 "test.S"
# 0 "<built-in>"

# 0 "<built-in>"

The second line definitely looks wrong (missing #? maybe that's what the above
commit was hacking around?). But if I fix that, for example, by adding the
leading #, the rest looks correct and everything compiles, links, and runs as
expected.

So it seems like making -fdirectives-only work for assembler-with-cpp is a
matter of tracking down and fixing that broken line directive generation.

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

* [Bug c/109534] -fdirectives-only does not work with assembler-with-cpp
  2023-04-17  6:58 [Bug c/109534] New: -fdirectives-only does not work with assembler-with-cpp boris at kolpackov dot net
@ 2023-04-17  7:04 ` pinskia at gcc dot gnu.org
  2023-04-17  7:09 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-17  7:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>And I've tracked this down to the following old gcc/ChangeLog-2007 entry:

https://inbox.sourceware.org/gcc-patches/65dd6fd50708231259v2dce17f4m1ff55e22f221a777@mail.gmail.com/

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

* [Bug c/109534] -fdirectives-only does not work with assembler-with-cpp
  2023-04-17  6:58 [Bug c/109534] New: -fdirectives-only does not work with assembler-with-cpp boris at kolpackov dot net
  2023-04-17  7:04 ` [Bug c/109534] " pinskia at gcc dot gnu.org
@ 2023-04-17  7:09 ` pinskia at gcc dot gnu.org
  2023-04-17  8:09 ` [Bug preprocessor/109534] " rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-17  7:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So from what I understand this part is what you want:
```
diff --git a/gcc/gcc.cc b/gcc/gcc.cc
index 16bb07f2cdc..2825ef3f037 100644
--- a/gcc/gcc.cc
+++ b/gcc/gcc.cc
@@ -1471,12 +1471,12 @@ static const struct compiler default_compilers[] =
   {".S", "@assembler-with-cpp", 0, 0, 0},
   {"@assembler-with-cpp",
 #ifdef AS_NEEDS_DASH_FOR_PIPED_INPUT
-   "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
+   "%(trad_capable_cpp) -lang-asm %(cpp_options) %{!E:-fno-directives-only} \
       %{E|M|MM:%(cpp_debug_options)}\
       %{!M:%{!MM:%{!E:%{!S:-o %|.s |\n\
        as %(asm_debug) %(asm_options) %|.s %A }}}}"
 #else
-   "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
+   "%(trad_capable_cpp) -lang-asm %(cpp_options) %{!E:-fno-directives-only} \
       %{E|M|MM:%(cpp_debug_options)}\
       %{!M:%{!MM:%{!E:%{!S:-o %|.s |\n\
        as %(asm_debug) %(asm_options) %m.s %A }}}}"

```
Because -fno-directives-only is needed without -E.
Secondary is the issue of what is produced.

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

* [Bug preprocessor/109534] -fdirectives-only does not work with assembler-with-cpp
  2023-04-17  6:58 [Bug c/109534] New: -fdirectives-only does not work with assembler-with-cpp boris at kolpackov dot net
  2023-04-17  7:04 ` [Bug c/109534] " pinskia at gcc dot gnu.org
  2023-04-17  7:09 ` pinskia at gcc dot gnu.org
@ 2023-04-17  8:09 ` rguenth at gcc dot gnu.org
  2023-04-17  8:57 ` boris at kolpackov dot net
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-04-17  8:09 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-04-17
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.

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

* [Bug preprocessor/109534] -fdirectives-only does not work with assembler-with-cpp
  2023-04-17  6:58 [Bug c/109534] New: -fdirectives-only does not work with assembler-with-cpp boris at kolpackov dot net
                   ` (2 preceding siblings ...)
  2023-04-17  8:09 ` [Bug preprocessor/109534] " rguenth at gcc dot gnu.org
@ 2023-04-17  8:57 ` boris at kolpackov dot net
  2023-04-17 14:37 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: boris at kolpackov dot net @ 2023-04-17  8:57 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Boris Kolpackov <boris at kolpackov dot net> ---
Thanks for the link to the patch submission though I find the
"-fdirectives-only option is incompatible with assembly" statement puzzling.

> So from what I understand this part is what you want:
>
> -   "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
> +   "%(trad_capable_cpp) -lang-asm %(cpp_options) %{!E:-fno-directives-only} \
>
> Because -fno-directives-only is needed without -E.

Hm, wouldn't that still break the second invocation (-fpreprocessed
-fdirectives-only)?

I think the correct fix is to remove -fno-directives-only altogether (provided
the second issue is fixed, of course) leaving the user-specified
-fdirectives-only to take effect regardless of the mode.

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

* [Bug preprocessor/109534] -fdirectives-only does not work with assembler-with-cpp
  2023-04-17  6:58 [Bug c/109534] New: -fdirectives-only does not work with assembler-with-cpp boris at kolpackov dot net
                   ` (3 preceding siblings ...)
  2023-04-17  8:57 ` boris at kolpackov dot net
@ 2023-04-17 14:37 ` pinskia at gcc dot gnu.org
  2023-04-18  5:31 ` boris at kolpackov dot net
  2023-04-18  5:48 ` boris at kolpackov dot net
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-17 14:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Boris Kolpackov from comment #4)
> Thanks for the link to the patch submission though I find the
> "-fdirectives-only option is incompatible with assembly" statement puzzling.
> 
> > So from what I understand this part is what you want:
> >
> > -   "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
> > +   "%(trad_capable_cpp) -lang-asm %(cpp_options) %{!E:-fno-directives-only} \
> >
> > Because -fno-directives-only is needed without -E.
> 
> Hm, wouldn't that still break the second invocation (-fpreprocessed
> -fdirectives-only)?
> 
> I think the correct fix is to remove -fno-directives-only altogether
> (provided the second issue is fixed, of course) leaving the user-specified
> -fdirectives-only to take effect regardless of the mode.

The documentation says specifically-fdirectives-only is ignored if
-fpreprocessed is supplied. So it will never break the second invocation.

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

* [Bug preprocessor/109534] -fdirectives-only does not work with assembler-with-cpp
  2023-04-17  6:58 [Bug c/109534] New: -fdirectives-only does not work with assembler-with-cpp boris at kolpackov dot net
                   ` (4 preceding siblings ...)
  2023-04-17 14:37 ` pinskia at gcc dot gnu.org
@ 2023-04-18  5:31 ` boris at kolpackov dot net
  2023-04-18  5:48 ` boris at kolpackov dot net
  6 siblings, 0 replies; 8+ messages in thread
From: boris at kolpackov dot net @ 2023-04-18  5:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Boris Kolpackov <boris at kolpackov dot net> ---
> The documentation says specifically-fdirectives-only is ignored if -fpreprocessed is supplied.

Hm, that's not how it works, IME. Specifically, just "-fpreprocessed" means the
source code is fully preprocessed (and no further preprocessing is required)
while "-fpreprocessed -fdirectives-only" means the source code is preprocessed
to directives only (and further preprocessing is required). Here is a
demonstration:

$ cat <EOF >test.c
#define FOO 2
int f () {return FOO;}
EOF

$ gcc -E -fdirectives-only -o test.i test.c

$ tail -3 test.i
# 1 "test.c"
#define FOO 2
int f () {return FOO;}

$ gcc -fpreprocessed -fdirectives-only -c -o test.o test.i

$ gcc -fpreprocessed -c -o test.o test.i
test.c: In function ‘f’:
test.c:2:18: error: ‘FOO’ undeclared (first use in this function)
    2 | int f () {return FOO;}

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

* [Bug preprocessor/109534] -fdirectives-only does not work with assembler-with-cpp
  2023-04-17  6:58 [Bug c/109534] New: -fdirectives-only does not work with assembler-with-cpp boris at kolpackov dot net
                   ` (5 preceding siblings ...)
  2023-04-18  5:31 ` boris at kolpackov dot net
@ 2023-04-18  5:48 ` boris at kolpackov dot net
  6 siblings, 0 replies; 8+ messages in thread
From: boris at kolpackov dot net @ 2023-04-18  5:48 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Boris Kolpackov <boris at kolpackov dot net> ---
BTW, my understanding of the rationale for the original patch (the one that
forces -fno-directives-only) is to paper over some underlying issue with
-fdirectives-only when used on .S files, potentially the same as the one I've
encountered with a broken line directive. And the intend of the patch was to
make builds that use -fdirectives-only on .S files to work transparently by
forcing full preprocessing instead. But that "transparently" part doesn't seem
to hold (using the same test.S):


$ gcc -E -fdirectives-only -o test.Si test.S
$ 
$ gcc -x assembler-with-cpp -fpreprocessed -fdirectives-only -c -o test.o
test.Si
test.Si:1: Error: junk at end of line, first unrecognized character is `0'


The error is due to -fno-directives-only cancelling out -fdirectives-only and
just -fpreprocessed means the file is fully preprocessed.

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

end of thread, other threads:[~2023-04-18  5:48 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-17  6:58 [Bug c/109534] New: -fdirectives-only does not work with assembler-with-cpp boris at kolpackov dot net
2023-04-17  7:04 ` [Bug c/109534] " pinskia at gcc dot gnu.org
2023-04-17  7:09 ` pinskia at gcc dot gnu.org
2023-04-17  8:09 ` [Bug preprocessor/109534] " rguenth at gcc dot gnu.org
2023-04-17  8:57 ` boris at kolpackov dot net
2023-04-17 14:37 ` pinskia at gcc dot gnu.org
2023-04-18  5:31 ` boris at kolpackov dot net
2023-04-18  5:48 ` boris at kolpackov dot net

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).