[PATCH] Fix compilation of .c files as C++ when using Clang

Pedro Alves pedro@palves.net
Thu Sep 17 22:18:26 GMT 2020


On 9/16/20 4:49 PM, Gary Benson via Gdb-patches wrote:
> Hi all,
> 
> In commit 221db974e653659edb280787af1b3efdd1615083, this patch:
> 
>     2020-06-24  Pedro Alves  <palves@redhat.com>
> 
>     * lib/gdb.exp (gdb_compile): Pass "-x c++" explicitly when
>     compiling C++ programs.
> 
> attempted to fix problems with testcases that compile .c files
> with the C++ compiler.  They pass the "c++" option to gdb_compile,
> resulting in the following error when using Clang:
> 
>     gdb compile failed, clang-10: warning: treating 'c' input as 'c++'
>     when in C++ mode, this behavior is deprecated [-Wdeprecated]
> 
> This fix did not work for gdb.base/infcall-nested-structs-c++.exp,
> however: the "-x c++" appeared in the compiler's commandline after
> the .c file, so the option was not enabled for that file.  

Huh.  You're missing explaining why this testcase is different.

So I digged, and noticed that the original testcases my patch
was addressing where using build_executable/prepare_for_testing.
They all end up in build_executable_from_specs, which compiles and links
in separate steps, using gdb_compile -- the compile step passes $type=object
to gdb_compile.  While the link step passes $type=executable.

Now, gdb.base/infcall-nested-structs-c++.exp uses gdb_compile directly
instead, and it passes $type=executable to compile and link all in one step.

So the difference is that when $type=object, the sources appear at the
end, and when $type=executable, the sources are at the beginning.

Odd.

Digging a little deeper, I found it in dejagnu's sources,
in default_target_compile:

    # This is obscure: we put SOURCES at the end when building an
    # object, because otherwise, in some situations, libtool will
    # become confused about the name of the actual source file.
    if {$type == "object"} {
	set opts "$add_flags $sources"
    } else {
	set opts "$sources $add_flags"
    }

There you go...

> This
> commit moves the "-x c++" earlier in the compiler's commandline.
> Unfortunately this then broke the testcase that required the original
> fix, gdb.compile/compile-cplus.exp: the "-x c++" was being parsed
> for the linker pass, causing the compiler to attempt to parse the
> .o files as C++.  This commit makes passing "-x c++" conditional on
> the source being a .c file.
> 
> Checked on Fedora 32 x86_64, GCC and clang.  Is this ok to commit?

OK.  But I'd appreciate it if you included some of the info above
in the commit log.

Thanks,
Pedro Alves


More information about the Gdb-patches mailing list