Skip to content

with clang, [[nodiscard]] is emitted also for -std=c++11 #9181

@botovq

Description

@botovq

What version of protobuf and what language are you using?
Version: v3.19.1
Language: C++

What operating system (Linux, Windows, ...) and version?

OpenBSD -current

What runtime / compiler are you using (e.g., python version or gcc version)

clang 11.1.0

What did you do?

Build protobuf 3.19.1, then try to build protozero 1.7.0.

What did you expect to see

protozero 1.7.0 builds

What did you see instead?

The build failed because protozero builds with -pedantic -std=c++11 -Werror. On the one hand, this is enough to have PROTOBUF_NODISCARD defined as [[nodiscard]]. On the other hand, this then trips over -Werror due to -Wc++17-extensions:

[79/90] /tmp/pobj/protozero-1.7.0/bin/c++  -I/tmp/pobj/protozero-1.7.0/protozero-1.7.0/include -I/tmp/pobj/protozero-1.7.0/protozero-1.7.0/test/include -isystem /tmp/pobj/protozero-1.7.0/protozero-1.7.0/test/catch -isystem /usr/local/include -isystem test -O2 -pipe  -std=c++17 -DNDEBUG   -std=c++11 -Wall -Wextra -pedantic -Wsign-compare -Wunused-parameter -Wno-float-equal -Wno-covered-switch-default -Werror -pthread -MD -MT test/CMakeFiles/writer_tests.dir/t/bytes/bytes_testcase.pb.cc.o -MF test/CMakeFiles/writer_tests.dir/t/bytes/bytes_testcase.pb.cc.o.d -o test/CMakeFiles/writer_tests.dir/t/bytes/bytes_testcase.pb.cc.o -c test/t/bytes/bytes_testcase.pb.cc
FAILED: test/CMakeFiles/writer_tests.dir/t/bytes/bytes_testcase.pb.cc.o /tmp/pobj/protozero-1.7.0/bin/c++  -I/tmp/pobj/protozero-1.7.0/protozero-1.7.0/include -I/tmp/pobj/protozero-1.7.0/protozero-1.7.0/test/include -isystem /tmp/pobj/protozero-1.7.0/protozero-1.7.0/test/catch -isystem /usr/local/include -isystem test -O2 -pipe  -std=c++17 -DNDEBUG   -std=c++11 -Wall -Wextra -pedantic -Wsign-compare -Wunused-parameter -Wno-float-equal -Wno-covered-switch-default -Werror -pthread -MD -MT test/CMakeFiles/writer_tests.dir/t/bytes/bytes_testcase.pb.cc.o -MF test/CMakeFiles/writer_tests.dir/t/bytes/bytes_testcase.pb.cc.o.d -o test/CMakeFiles/writer_tests.dir/t/bytes/bytes_testcase.pb.cc.o -c test/t/bytes/bytes_testcase.pb.cc
In file included from test/t/bytes/bytes_testcase.pb.cc:4:
test/t/bytes/bytes_testcase.pb.h:190:3: error: use of the 'nodiscard' attribute is a C++17 extension [-Werror,-Wc++17-extensions]
  PROTOBUF_NODISCARD std::string* release_s();
  ^
/usr/local/include/google/protobuf/port_def.inc:463:30: note: expanded from macro 'PROTOBUF_NODISCARD'
#define PROTOBUF_NODISCARD [[nodiscard]]
                             ^
1 error generated.
ninja: build stopped: subcommand failed.

Anything else we should know about your project / environment

It seems #if __has_cpp_attribute(nodiscard) is insufficient to determine whether [[nodiscard]] is actually allowed. A similar issue was also reported here: nlohmann/json#1535

This could be avoided if PROTOBUF_NODISCARD were only defined to [[nodiscard]] with

#if __has_cpp_attribute(nodiscard) && PROTOBUF_CPLUSPLUS_MIN(201703L)
#define PROTOBUF_NODISCARD [[nodiscard]]
#elif
...

or similar.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions