0

I use gtest and gmock for my unit tests, I mock unistd read/write functions for input, sending and receiving testing(C function Gmock):

class MockSysFuncs {
 public:
  MOCK_METHOD3(read, ssize_t(int, char*, size_t));
  MOCK_METHOD3(write, ssize_t(int, const char*, size_t));
  sem_t readSem;
  static std::shared_ptr<MockSysFuncs> _mockSysFuncObj;
};

ssize_t read(int __fd, void* __buf, size_t __nbytes);
ssize_t write(int __fd, const void* __buf, size_t __n);

Functions implementation (in the mock cpp file):

ssize_t read(int __fd, void *__buf, size_t __nbytes) {
  sem_wait(&MockSysFuncs::_mockSysFuncObj->readSem);
  return MockSysFuncs::_mockSysFuncObj->read(__fd, static_cast<char *>(__buf), __nbytes);
}

ssize_t write(int __fd, const void *__buf, size_t __n) {
  return MockSysFuncs::_mockSysFuncObj->write(__fd, static_cast<const char *>(__buf), __n);
}

On compilation, I receive this warnings:

warning: redundant redeclaration of 'ssize_t read(int, void*, size_t)' in same scope [-Wredundant-decls]
  168 | ssize_t read(int __fd, void* __buf, size_t __nbytes);
  /usr/include/unistd.h:360:16: note: previous declaration of 'ssize_t read(int, void*, size_t)'
  360 | extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur;

warning: redundant redeclaration of 'ssize_t write(int, const void*, size_t)' in same scope [-Wredundant-decls]
  169 | ssize_t write(int __fd, const void* __buf, size_t __n);
  /usr/include/unistd.h:366:16: note: previous declaration of 'ssize_t write(int, const void*, size_t)'
  366 | extern ssize_t write (int __fd, const void *__buf, size_t __n) __wur;

Is there a way to solve this warnings without removing the -Wredundant-decls compilation flag?

lior.i
  • 573
  • 1
  • 7
  • 20
  • Remove the duplicate declarations? – Jarod42 Dec 10 '20 at 11:58
  • @Jarod42, The duplicate declaration is in ```unistd.h``` I cannot touch this file and I need to include it for other purposes except read/write. – lior.i Dec 10 '20 at 13:19
  • Remove the ones you add after class `class MockSysFuncs`. – Jarod42 Dec 10 '20 at 13:21
  • This is the mock for the read/write functions, if I remove this declaration I remove the mock. This is how I mock C functions with gmock which is cpp tool. – lior.i Dec 10 '20 at 13:26
  • As I understand, you redefine C-functions with your own version (which is UB but supported by some compileras gcc with weak symbol), but warning is about your **declarations** in header. – Jarod42 Dec 10 '20 at 13:41
  • Thanks @Jarod42, you are right, the double declaration is unneeded. The tests can reach the Mock implementation without that. – lior.i Dec 13 '20 at 08:12

2 Answers2

1

Your warning is about redundant declarations, so just remove them:

#include <unistd.h> // declare read/write

class MockSysFuncs
{
public:
  MOCK_METHOD3(read, ssize_t(int, char*, size_t));
  MOCK_METHOD3(write, ssize_t(int, const char*, size_t));
  sem_t readSem;
  static std::shared_ptr<MockSysFuncs> _mockSysFuncObj;
};
Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

The solution that I found for now is to use #pragma:

class MockSysFuncs {
 public:
  MOCK_METHOD3(read, ssize_t(int, char*, size_t));
  MOCK_METHOD3(write, ssize_t(int, const char*, size_t));
  sem_t readSem;
  static std::shared_ptr<MockSysFuncs> _mockSysFuncObj;
};

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wredundant-decls"
ssize_t read(int __fd, void* __buf, size_t __nbytes);
ssize_t write(int __fd, const void* __buf, size_t __n);
#pragma GCC diagnostic pop

In general, I find #pragma as bad practice and try to avoid using it in my code but since this is mock file for unit tests and not operational code I guess it fine.

lior.i
  • 573
  • 1
  • 7
  • 20