arun11299 / cpp-subprocess

Subprocessing with modern C++
Other
456 stars 90 forks source link

Replace throw with noexcept for C17 compatibility #17

Closed bdangit closed 5 years ago

bdangit commented 5 years ago

C++11 started to deprecate the use of throw function identifiers. In C++17, this header no longer compiles due to the deprecation. Further, cleaned up other gcc compiler warnings around the use of throw.

Errors due to throw

# g++ -g -Wall -std=c++17 -lpthread -o test_cat test_cat.cc
../subprocess.hpp:220:38: error: ISO C++1z does not allow dynamic exception specifications
   std::pair<int, int> pipe_cloexec() throw (OSError)
                                      ^~~~~
../subprocess.hpp:968:24: error: ISO C++1z does not allow dynamic exception specifications
   void start_process() throw (CalledProcessError, OSError);
                        ^~~~~
../subprocess.hpp:974:14: error: ISO C++1z does not allow dynamic exception specifications
   int wait() throw(OSError);
              ^~~~~
../subprocess.hpp:976:14: error: ISO C++1z does not allow dynamic exception specifications
   int poll() throw(OSError);
              ^~~~~
../subprocess.hpp:1020:26: error: ISO C++1z does not allow dynamic exception specifications
   void execute_process() throw (CalledProcessError, OSError);
                          ^~~~~
../subprocess.hpp:1069:36: error: ISO C++1z does not allow dynamic exception specifications
 inline void Popen::start_process() throw (CalledProcessError, OSError)
                                    ^~~~~
../subprocess.hpp:1083:26: error: ISO C++1z does not allow dynamic exception specifications
 inline int Popen::wait() throw (OSError)
                          ^~~~~
../subprocess.hpp:1098:26: error: ISO C++1z does not allow dynamic exception specifications
 inline int Popen::poll() throw (OSError)
                          ^~~~~
../subprocess.hpp:1140:38: error: ISO C++1z does not allow dynamic exception specifications
 inline void Popen::execute_process() throw (CalledProcessError, OSError)

Other gcc errors discovered after fixing the throw identifiers

../subprocess.hpp: In function 'std::pair<int, int> subprocess::util::pipe_cloexec()':
../subprocess.hpp:225:42: warning: throw will always call terminate() [-Wterminate]
       throw OSError("pipe failure", errno);
                                          ^
../subprocess.hpp: In member function 'int subprocess::Popen::wait()':
../subprocess.hpp:1088:63: warning: throw will always call terminate() [-Wterminate]
     if (errno != ECHILD) throw OSError("waitpid failed", errno);
                                                               ^
../subprocess.hpp: In member function 'int subprocess::Popen::poll()':
../subprocess.hpp:1125:47: warning: throw will always call terminate() [-Wterminate]
     else throw OSError("waitpid failed", errno);
                                               ^
../subprocess.hpp: In member function 'void subprocess::Popen::execute_process()':
../subprocess.hpp:1164:39: warning: throw will always call terminate() [-Wterminate]
     throw OSError("fork failed", errno);
                                       ^
../subprocess.hpp:1206:13: warning: throw will always call terminate() [-Wterminate]
       throw exp;
             ^~~

Testing

_test_binaries=(
  test
  test_cat
  test_env
  test_err_redirection
  test_ret_code
  test_split
  test_subprocess
)

pushd "test" || exit 1
  for f in ${_test_binaries[@]}; do
    build_line "building test/$f"
    g++ -g -Wall -std=c++17 -lpthread -o "${f}" "${f}.cc"
  done
popd || exit 1

pushd "test" || exit 1
  for f in ${_test_binaries[@]}; do
    build_line "executing test/$f"
    ./"${f}"
  done
popd || exit 1

Environment

# g++ --version
g++.real (GCC) 7.3.0
Copyright (C) 2017 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.

# uname -a
Linux 512a1da44a6c 4.9.93-linuxkit-aufs #1 SMP Wed Jun 6 16:55:56 UTC 2018 x86_64 GNU/Linux

Signed-off-by: Ben Dang me@bdang.it

bdangit commented 5 years ago

Furthermore, tested compiling with -std=c++11 and -std=c++14. No errors and all tests work.

bdangit commented 5 years ago

Hi @arun11299, I updated the PR based on your comments. I have to admit that I didn't realize noexcept had the ability to resolve expressions that way. With that I brought back the throw within the function since that was the intention.

I compiled against c++11, c++14, and c++17. No warnings/errors and all tests pass.

   cpp-subprocess: building test/test
   cpp-subprocess: building test/test_cat
   cpp-subprocess: building test/test_env
   cpp-subprocess: building test/test_err_redirection
   cpp-subprocess: building test/test_ret_code
   cpp-subprocess: building test/test_split
   cpp-subprocess: building test/test_subprocess
/src /src
   cpp-subprocess: Running post-compile tests
/src/test /src /src
   cpp-subprocess: executing test/test
bufsize opt
exe opt
   cpp-subprocess: executing test/test_cat
Test::test_cat_pipe_redirection
END_TEST
Test::test_cat_file_redirection
END_TEST
44112
Test::test_buffer_growth_threaded_comm
44112
END_TEST
   cpp-subprocess: executing test/test_env
ENV1 = VALUE-1
ENV2 = VALUE-2
ENV3 = VALUE-3
   cpp-subprocess: executing test/test_err_redirection
-1
   cpp-subprocess: executing test/test_ret_code
Test::test_poll_ret_code
  template <typename Buffer>
  template <typename T>
  template <typename T>
  template <typename Func>
  template <typename T>
template <typename... T> struct param_pack{};
template <typename F
template <typename F>
template <typename F
template <typename F
  template <typename... Args>
  template <typename... Args>
  template <typename F
template <typename F
  template<typename F
  template<typename F
  template<typename... Args>
template<typename... Args>
template<typename... Args>
template <typename... Args>
template <typename... Args>
template<typename... Args>

retcode: 0
   cpp-subprocess: executing test/test_split
a
b
c
5
   cpp-subprocess: executing test/test_subprocess
total 5624
-rw-r--r-- 1 root root     322 Oct  8 04:44 cat_fredirect.txt
-rwxr-xr-x 1 root root      91 Oct  5 17:36 env_script.sh
-rwxr-xr-x 1 root root  164048 Oct  8 04:44 test
-rw-r--r-- 1 root root     944 Oct  5 17:36 test.cc
-rwxr-xr-x 1 root root 1293920 Oct  8 04:44 test_cat
-rwxr-xr-x 1 root root    1450 Oct  5 17:36 test_cat.cc
-rwxr-xr-x 1 root root  681008 Oct  8 04:44 test_env
-rw-r--r-- 1 root root     347 Oct  5 17:36 test_env.cc
-rwxr-xr-x 1 root root  633704 Oct  8 04:44 test_err_redirection
-rw-r--r-- 1 root root     266 Oct  5 17:36 test_err_redirection.cc
-rwxr-xr-x 1 root root 1283072 Oct  8 04:44 test_ret_code
-rw-r--r-- 1 root root     776 Oct  5 17:36 test_ret_code.cc
drwxr-xr-x 3 root root      96 Oct  5 17:36 test_ret_code.dSYM
-rwxr-xr-x 1 root root  239712 Oct  8 04:44 test_split
-rw-r--r-- 1 root root     844 Oct  5 17:36 test_split.cc
-rwxr-xr-x 1 root root 1382912 Oct  8 04:44 test_subprocess
-rwxr-xr-x 1 root root    1421 Oct  5 17:36 test_subprocess.cc
-rwxr-xr-x 1 root root    1667 Oct  5 17:36 write_err.sh
-rw-r--r-- 1 root root   23040 Oct  8 04:44 write_err.txt
0
four
five

  template <typename Buffer>
  template <typename T>
  template <typename T>
  template <typename Func>
  template <typename T>
template <typename... T> struct param_pack{};
template <typename F
template <typename F>
template <typename F
template <typename F
  template <typename... Args>
  template <typename... Args>
  template <typename F
template <typename F
  template<typename F
  template<typename F
  template<typename... Args>
template<typename... Args>
template<typename... Args>
template <typename... Args>
template <typename... Args>
template<typename... Args>

  template <typename... Args>
  template <typename... Args>
  template <typename F, typename... Args>
template <typename F, typename... Args>
  template<typename F, typename... Args>
  template<typename F, typename... Args>
  template<typename... Args>
template<typename... Args>
template<typename... Args>
template <typename... Args>
template <typename... Args>
template<typename... Args>

total 5624
-rw-r--r-- 1 root root     322 Oct  8 04:44 cat_fredirect.txt
-rwxr-xr-x 1 root root      91 Oct  5 17:36 env_script.sh
-rwxr-xr-x 1 root root  164048 Oct  8 04:44 test
-rw-r--r-- 1 root root     944 Oct  5 17:36 test.cc
-rwxr-xr-x 1 root root 1293920 Oct  8 04:44 test_cat
-rwxr-xr-x 1 root root    1450 Oct  5 17:36 test_cat.cc
-rwxr-xr-x 1 root root  681008 Oct  8 04:44 test_env
-rw-r--r-- 1 root root     347 Oct  5 17:36 test_env.cc
-rwxr-xr-x 1 root root  633704 Oct  8 04:44 test_err_redirection
-rw-r--r-- 1 root root     266 Oct  5 17:36 test_err_redirection.cc
-rwxr-xr-x 1 root root 1283072 Oct  8 04:44 test_ret_code
-rw-r--r-- 1 root root     776 Oct  5 17:36 test_ret_code.cc
drwxr-xr-x 3 root root      96 Oct  5 17:36 test_ret_code.dSYM
-rwxr-xr-x 1 root root  239712 Oct  8 04:44 test_split
-rw-r--r-- 1 root root     844 Oct  5 17:36 test_split.cc
-rwxr-xr-x 1 root root 1382912 Oct  8 04:44 test_subprocess
-rwxr-xr-x 1 root root    1421 Oct  5 17:36 test_subprocess.cc
-rwxr-xr-x 1 root root    1667 Oct  5 17:36 write_err.sh
-rw-r--r-- 1 root root   23040 Oct  8 04:44 write_err.txt

Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Sleep ended: ret code = 0
/src /src
bdangit commented 5 years ago

Awesome. One small ask, could you help to tag another release, please? :)