protocolbuffers/protobuf

proto defined enums conflicts with globally defined variables using protoc cpp

Open

#8 953 ouverte le 3 sept. 2021

Voir sur GitHub
 (1 commentaire) (0 réactions) (0 assignés)C++ (16 128 forks)batch import
c++help wantedprotoc

Métriques du dépôt

Stars
 (71 223 stars)
Métriques de merge PR
 (Merge moyen 2j 11h) (185 PRs mergées en 30 j)

Description

What version of protobuf and what language are you using? Version: master/v3.11.1 proto3 syntax. Language: C++

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

What runtime / compiler are you using (e.g., python version or gcc version) vs2019 (v142), SDK 10.0.18362 What did you do? Steps to reproduce the behavior:

Using globally defined names in enums in proto files conflicts with each other resulting in build errors. Here is an example: Using the following enum type:

enum AlarmSeverityProto {
  SEVERITY_INFO = 0;
  SEVERITY_WARNING = 1;
  SEVERITY_ERROR = 2;
}

conflicts with a globally defined variable (SEVERITY_ERROR) defined in winerror.h which is included in one of the following header files:

#include <grpc/grpc.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/channel.h>
#include <grpcpp/client_context.h>

Investigating the proto ph.h header file generated:

...
PROTOBUF_NAMESPACE_CLOSE
namespace NOV {
namespace Valhall {
namespace AlarmHandling {

enum AlarmSeverityProto : int {
  SEVERITY_INFO = 0,
  SEVERITY_WARNING = 1,
  SEVERITY_ERROR = 2,
  AlarmSeverityProto_INT_MIN_SENTINEL_DO_NOT_USE_ = std::numeric_limits<::PROTOBUF_NAMESPACE_ID::int32>::min(),
  AlarmSeverityProto_INT_MAX_SENTINEL_DO_NOT_USE_ = std::numeric_limits<::PROTOBUF_NAMESPACE_ID::int32>::max()
};
bool AlarmSeverityProto_IsValid(int value);
constexpr AlarmSeverityProto AlarmSeverityProto_MIN = SEVERITY_INFO;
constexpr AlarmSeverityProto AlarmSeverityProto_MAX = SEVERITY_ERROR;
...
}}}

I can see that in the line constexpr AlarmSeverityProto AlarmSeverityProto_MAX = SEVERITY_ERROR;, SEVERITY_ERROR gets resolved to the global defined variable in winerror.h. I think this is weird behaviour as I would think the compiler would instead use the one defined in the namespace 🤷

Currently I have found 2 workarounds for this bug:

  1. Do a #undef SEVERITY_ERROR before including the grpc/proto core header files mentioned above.
  2. Insert the namespace of the enum in line constexpr AlarmSeverityProto AlarmSeverityProto_MAX = SEVERITY_ERROR; as the following constexpr AlarmSeverityProto AlarmSeverityProto_MAX = AlarmSeverityProto::SEVERITY_ERROR;

What did you expect to see

Expected it to use its own namespace defined enum instead of the global one.

What did you see instead?

It uses the global defined one.

Anything else we should know about your project / environment Here is the error message I receive: protolib_build_error.txt

Guide contributeur