diff mbox series

[bpf-next] selftests/bpf: fix a clang deprecated-declarations compilation error

Message ID 20220217194005.2765348-1-yhs@fb.com (mailing list archive)
State Accepted
Commit a33c0c792d0aa2140d79799ca41d68428d2206cc
Delegated to: BPF
Headers show
Series [bpf-next] selftests/bpf: fix a clang deprecated-declarations compilation error | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 10 maintainers not CCed: linux-kselftest@vger.kernel.org kpsingh@kernel.org john.fastabend@gmail.com kafai@fb.com shuah@kernel.org songliubraving@fb.com nathan@kernel.org llvm@lists.linux.dev netdev@vger.kernel.org ndesaulniers@google.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 9 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next fail VM_Test

Commit Message

Yonghong Song Feb. 17, 2022, 7:40 p.m. UTC
Build the kernel and selftest with clang compiler with LLVM=1,
  make -j LLVM=1
  make -C tools/testing/selftests/bpf -j LLVM=1

I hit the following selftests/bpf compilation error:
  In file included from test_cpp.cpp:3:
  /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:73:8:
    error: 'relaxed_core_relocs' is deprecated: libbpf v0.6+: field has no effect [-Werror,-Wdeprecated-declarations]
  struct bpf_object_open_opts {
         ^
  test_cpp.cpp:56:2: note: in implicit move constructor for 'bpf_object_open_opts' first required here
          LIBBPF_OPTS(bpf_object_open_opts, opts);
          ^
  /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf_common.h:77:3: note: expanded from macro 'LIBBPF_OPTS'
                  (struct TYPE) {                                             \
                  ^
  /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:90:2: note: 'relaxed_core_relocs' has been explicitly marked deprecated here
          LIBBPF_DEPRECATED_SINCE(0, 6, "field has no effect")
          ^
  /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf_common.h:24:4: note: expanded from macro 'LIBBPF_DEPRECATED_SINCE'
                  (LIBBPF_DEPRECATED("libbpf v" # major "." # minor "+: " msg))
                   ^
  /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf_common.h:19:47: note: expanded from macro 'LIBBPF_DEPRECATED'
  #define LIBBPF_DEPRECATED(msg) __attribute__((deprecated(msg)))

There are two ways to fix the issue, one is to use GCC diagnostic ignore pragma, and the
other is to open code bpf_object_open_opts instead of using LIBBPF_OPTS.
Since in general LIBBPF_OPTS is preferred, the patch fixed the issue by
adding proper GCC diagnostic ignore pragmas.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/testing/selftests/bpf/test_cpp.cpp | 3 +++
 1 file changed, 3 insertions(+)

Comments

Andrii Nakryiko Feb. 17, 2022, 9:59 p.m. UTC | #1
On Thu, Feb 17, 2022 at 11:40 AM Yonghong Song <yhs@fb.com> wrote:
>
> Build the kernel and selftest with clang compiler with LLVM=1,
>   make -j LLVM=1
>   make -C tools/testing/selftests/bpf -j LLVM=1
>
> I hit the following selftests/bpf compilation error:
>   In file included from test_cpp.cpp:3:
>   /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:73:8:
>     error: 'relaxed_core_relocs' is deprecated: libbpf v0.6+: field has no effect [-Werror,-Wdeprecated-declarations]
>   struct bpf_object_open_opts {
>          ^
>   test_cpp.cpp:56:2: note: in implicit move constructor for 'bpf_object_open_opts' first required here
>           LIBBPF_OPTS(bpf_object_open_opts, opts);
>           ^
>   /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf_common.h:77:3: note: expanded from macro 'LIBBPF_OPTS'
>                   (struct TYPE) {                                             \
>                   ^
>   /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:90:2: note: 'relaxed_core_relocs' has been explicitly marked deprecated here
>           LIBBPF_DEPRECATED_SINCE(0, 6, "field has no effect")
>           ^
>   /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf_common.h:24:4: note: expanded from macro 'LIBBPF_DEPRECATED_SINCE'
>                   (LIBBPF_DEPRECATED("libbpf v" # major "." # minor "+: " msg))
>                    ^
>   /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf_common.h:19:47: note: expanded from macro 'LIBBPF_DEPRECATED'
>   #define LIBBPF_DEPRECATED(msg) __attribute__((deprecated(msg)))
>
> There are two ways to fix the issue, one is to use GCC diagnostic ignore pragma, and the
> other is to open code bpf_object_open_opts instead of using LIBBPF_OPTS.
> Since in general LIBBPF_OPTS is preferred, the patch fixed the issue by
> adding proper GCC diagnostic ignore pragmas.
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---

Do you know why we see this only with Clang? Why GCC doesn't generate this?


>  tools/testing/selftests/bpf/test_cpp.cpp | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/test_cpp.cpp b/tools/testing/selftests/bpf/test_cpp.cpp
> index 773f165c4898..19ad172036da 100644
> --- a/tools/testing/selftests/bpf/test_cpp.cpp
> +++ b/tools/testing/selftests/bpf/test_cpp.cpp
> @@ -1,6 +1,9 @@
>  /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
>  #include <iostream>
> +#pragma GCC diagnostic push
> +#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
>  #include <bpf/libbpf.h>
> +#pragma GCC diagnostic pop
>  #include <bpf/bpf.h>
>  #include <bpf/btf.h>
>  #include "test_core_extern.skel.h"
> --
> 2.30.2
>
Yonghong Song Feb. 17, 2022, 11:55 p.m. UTC | #2
On 2/17/22 1:59 PM, Andrii Nakryiko wrote:
> On Thu, Feb 17, 2022 at 11:40 AM Yonghong Song <yhs@fb.com> wrote:
>>
>> Build the kernel and selftest with clang compiler with LLVM=1,
>>    make -j LLVM=1
>>    make -C tools/testing/selftests/bpf -j LLVM=1
>>
>> I hit the following selftests/bpf compilation error:
>>    In file included from test_cpp.cpp:3:
>>    /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:73:8:
>>      error: 'relaxed_core_relocs' is deprecated: libbpf v0.6+: field has no effect [-Werror,-Wdeprecated-declarations]
>>    struct bpf_object_open_opts {
>>           ^
>>    test_cpp.cpp:56:2: note: in implicit move constructor for 'bpf_object_open_opts' first required here
>>            LIBBPF_OPTS(bpf_object_open_opts, opts);
>>            ^
>>    /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf_common.h:77:3: note: expanded from macro 'LIBBPF_OPTS'
>>                    (struct TYPE) {                                             \
>>                    ^
>>    /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:90:2: note: 'relaxed_core_relocs' has been explicitly marked deprecated here
>>            LIBBPF_DEPRECATED_SINCE(0, 6, "field has no effect")
>>            ^
>>    /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf_common.h:24:4: note: expanded from macro 'LIBBPF_DEPRECATED_SINCE'
>>                    (LIBBPF_DEPRECATED("libbpf v" # major "." # minor "+: " msg))
>>                     ^
>>    /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf_common.h:19:47: note: expanded from macro 'LIBBPF_DEPRECATED'
>>    #define LIBBPF_DEPRECATED(msg) __attribute__((deprecated(msg)))
>>
>> There are two ways to fix the issue, one is to use GCC diagnostic ignore pragma, and the
>> other is to open code bpf_object_open_opts instead of using LIBBPF_OPTS.
>> Since in general LIBBPF_OPTS is preferred, the patch fixed the issue by
>> adding proper GCC diagnostic ignore pragmas.
>>
>> Signed-off-by: Yonghong Song <yhs@fb.com>
>> ---
> 
> Do you know why we see this only with Clang? Why GCC doesn't generate this?

Not a gcc internal expert. But I guess gcc didn't look into struct field 
deprecated attribute.

The following is a simpler reproducer. The structure initialization
in the code means the compiler needs to initialize each named field
with either default value (0) or provided value. If one of fields
is marked as deprecated, the compiler can emit warning message since
that field is accessed.

[$ ~/tmp3] cat t.cc
struct bpf_object_open_opts {
         long sz;
         const char *objname;
         int relax_maps;
         int relaxed_core_relocs __attribute__((deprecated("msg")));
         const char *pin_root_path;
};

void foo(void *);
int test(void) {
         struct bpf_object_open_opts tmp = (struct bpf_object_open_opts) 
{ .sz = 2, };
         foo(&tmp);
         return 0;
}

[$ ~/tmp3] clang++ -c -g t.cc -Wall -Werror
t.cc:1:8: error: 'relaxed_core_relocs' is deprecated: msg 
[-Werror,-Wdeprecated-declarations]
struct bpf_object_open_opts {
        ^
t.cc:11:43: note: in implicit move constructor for 
'bpf_object_open_opts' first required here
         struct bpf_object_open_opts tmp = (struct bpf_object_open_opts) 
{ .sz = 2, };
                                           ^
t.cc:5:41: note: 'relaxed_core_relocs' has been explicitly marked 
deprecated here
         int relaxed_core_relocs __attribute__((deprecated("msg")));
                                                ^
1 error generated.
[$ ~/tmp3] g++ -c -g t.cc -Wall -Werror
[$ ~/tmp3] g++ --version
g++ (GCC) 8.5.0 20210514 (Red Hat 8.5.0-3)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[$ ~/tmp3]

> 
> 
>>   tools/testing/selftests/bpf/test_cpp.cpp | 3 +++
>>   1 file changed, 3 insertions(+)
>>
>> diff --git a/tools/testing/selftests/bpf/test_cpp.cpp b/tools/testing/selftests/bpf/test_cpp.cpp
>> index 773f165c4898..19ad172036da 100644
>> --- a/tools/testing/selftests/bpf/test_cpp.cpp
>> +++ b/tools/testing/selftests/bpf/test_cpp.cpp
>> @@ -1,6 +1,9 @@
>>   /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
>>   #include <iostream>
>> +#pragma GCC diagnostic push
>> +#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
>>   #include <bpf/libbpf.h>
>> +#pragma GCC diagnostic pop
>>   #include <bpf/bpf.h>
>>   #include <bpf/btf.h>
>>   #include "test_core_extern.skel.h"
>> --
>> 2.30.2
>>
patchwork-bot+netdevbpf@kernel.org Feb. 20, 2022, 12:50 a.m. UTC | #3
Hello:

This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Thu, 17 Feb 2022 11:40:05 -0800 you wrote:
> Build the kernel and selftest with clang compiler with LLVM=1,
>   make -j LLVM=1
>   make -C tools/testing/selftests/bpf -j LLVM=1
> 
> I hit the following selftests/bpf compilation error:
>   In file included from test_cpp.cpp:3:
>   /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:73:8:
>     error: 'relaxed_core_relocs' is deprecated: libbpf v0.6+: field has no effect [-Werror,-Wdeprecated-declarations]
>   struct bpf_object_open_opts {
>          ^
>   test_cpp.cpp:56:2: note: in implicit move constructor for 'bpf_object_open_opts' first required here
>           LIBBPF_OPTS(bpf_object_open_opts, opts);
>           ^
>   /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf_common.h:77:3: note: expanded from macro 'LIBBPF_OPTS'
>                   (struct TYPE) {                                             \
>                   ^
>   /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf.h:90:2: note: 'relaxed_core_relocs' has been explicitly marked deprecated here
>           LIBBPF_DEPRECATED_SINCE(0, 6, "field has no effect")
>           ^
>   /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf_common.h:24:4: note: expanded from macro 'LIBBPF_DEPRECATED_SINCE'
>                   (LIBBPF_DEPRECATED("libbpf v" # major "." # minor "+: " msg))
>                    ^
>   /.../tools/testing/selftests/bpf/tools/include/bpf/libbpf_common.h:19:47: note: expanded from macro 'LIBBPF_DEPRECATED'
>   #define LIBBPF_DEPRECATED(msg) __attribute__((deprecated(msg)))
> 
> [...]

Here is the summary with links:
  - [bpf-next] selftests/bpf: fix a clang deprecated-declarations compilation error
    https://git.kernel.org/bpf/bpf-next/c/a33c0c792d0a

You are awesome, thank you!
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/test_cpp.cpp b/tools/testing/selftests/bpf/test_cpp.cpp
index 773f165c4898..19ad172036da 100644
--- a/tools/testing/selftests/bpf/test_cpp.cpp
+++ b/tools/testing/selftests/bpf/test_cpp.cpp
@@ -1,6 +1,9 @@ 
 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
 #include <iostream>
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 #include <bpf/libbpf.h>
+#pragma GCC diagnostic pop
 #include <bpf/bpf.h>
 #include <bpf/btf.h>
 #include "test_core_extern.skel.h"