Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more default warnings #18

Merged
merged 5 commits into from
Oct 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ else()
message("-- Configuring release build")
endif()

# Enable extra warnings to adhere to https://github.com/mapbox/cpp/issues/37
set(DESIRED_WARNINGS "-Wall -Wextra -Wconversion -Wunreachable-code -Wuninitialized -pedantic-errors -Wold-style-cast -Wno-error=unused-variable -Wshadow -Wfloat-equal")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These represent 🍇 from mapbox/cpp#37 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, 🍇 and 🍊 (although I now realize I missed -Weffc++)


# Note: -D_GLIBCXX_USE_CXX11_ABI=0 is needed to support mason packages that are precompiled libs
# Currently we only depend on a header only library, but this will help avoid issues when more libs are added via mason
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OPTIMIZATION_FLAGS} -Wall -Wextra -pedantic -Wsign-compare -Wconversion -Wshadow")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OPTIMIZATION_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=0 ${DESIRED_WARNINGS}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@springmeyer what is D_GLIBCXX_USE_CXX11_ABI used for? a boolean setting for GCC to use a specific c++ version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. Did not realize I added this flag in this PR. It should have been there all along (as we make mention of it in the code comment above). More background on it is at https://github.com/mapbox/node-cpp-skel/blob/3f7d2c4bcf3a6162312accbf3554ac17ecbbb407/common.gypi#L6. And https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html. In short: until mason supports building against both "ABIS" we need to this flag to avoid edge case linking errors on different linux versions when using mason binary C++ packages. Currently in gzip-hpp we don't link to any mason packages that are C++, so this is technically not needed. But we put it in hpp-skel to avoid this pitfall for downstream users that might add c++ mason binary packages, and therefore my copy/paste here brought it in.


if (WERROR)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
Expand Down
9 changes: 5 additions & 4 deletions include/gzip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* gzip implements a standard namespace with a few available functions.
*/

#include "gzip/compress.hpp"
#include "gzip/decompress.hpp"
#include "gzip/utils.hpp"
#include "gzip/version.hpp"
#include <gzip/compress.hpp>
#include <gzip/config.hpp>
#include <gzip/decompress.hpp>
#include <gzip/utils.hpp>
#include <gzip/version.hpp>
8 changes: 5 additions & 3 deletions include/gzip/compress.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <iostream>
#include <gzip/config.hpp>

// zlib
#include <zlib.h>
// std
#include <limits>
#include <stdexcept>
#include <string>

namespace gzip {

Expand Down Expand Up @@ -31,7 +33,7 @@ std::string compress(const char* data,
{
throw std::runtime_error("deflate init failed");
}
deflate_s.next_in = (Bytef*)data;
deflate_s.next_in = reinterpret_cast<z_const Bytef*>(data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was(Bytef*)data flagged as an error?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah seeing #19 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


#ifdef DEBUG
// Verify if size input will fit into unsigned int, type used for zlib's avail_in
Expand All @@ -57,7 +59,7 @@ std::string compress(const char* data,
// There is no way we see that "increase" would not fit in an unsigned int,
// hence we use static cast here to avoid -Wshorten-64-to-32 error
deflate_s.avail_out = static_cast<unsigned int>(increase);
deflate_s.next_out = (Bytef*)(output.data() + length);
deflate_s.next_out = reinterpret_cast<Bytef*>((&output[0] + length));
// From http://www.zlib.net/zlib_how.html
// "deflate() has a return value that can indicate errors, yet we do not check it here.
// Why not? Well, it turns out that deflate() can do no wrong here."
Expand Down
5 changes: 5 additions & 0 deletions include/gzip/config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the vision of this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One single place to define ZLIB_CONST (without this it would need to be defined in every header before zlib.h is included. This consolidates enabling the const type inside zlib in one place.


#ifndef ZLIB_CONST
#define ZLIB_CONST
#endif
7 changes: 5 additions & 2 deletions include/gzip/decompress.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <gzip/config.hpp>

// zlib
#include <zlib.h>
// std
#include <limits>
#include <stdexcept>
#include <string>

namespace gzip {

Expand All @@ -25,7 +28,7 @@ std::string decompress(const char* data, std::size_t size)
{
throw std::runtime_error("inflate init failed");
}
inflate_s.next_in = (Bytef*)data;
inflate_s.next_in = reinterpret_cast<z_const Bytef*>(data);

#ifdef DEBUG
// Verify if size (long type) input will fit into unsigned int, type used for zlib's avail_in
Expand All @@ -47,7 +50,7 @@ std::string decompress(const char* data, std::size_t size)
{
output.resize(length + 2 * size);
inflate_s.avail_out = static_cast<unsigned int>(2 * size);
inflate_s.next_out = (Bytef*)(output.data() + length);
inflate_s.next_out = reinterpret_cast<Bytef*>(&output[0] + length);
int ret = inflate(&inflate_s, Z_FINISH);
if (ret != Z_STREAM_END && ret != Z_OK && ret != Z_BUF_ERROR)
{
Expand Down
3 changes: 3 additions & 0 deletions include/gzip/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <cstdlib>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read http://www.cplusplus.com/reference/cstdlib/, but still not sure why this header is needed here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, seeing #19 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed for uint8_t. Without it, this header would not compile on its own. So this is an IWYU fix, aka include what you use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More details on that at https://github.com/include-what-you-use/include-what-you-use. clang-tidy can also flag when headers are depending on types but not including the header for them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is include-what-you-use automatically included in clang-tidy? Or are you using it here as a general good practice?

Also, is tidy succeeding for you locally? I'm getting errors:

/Users/carol/dev/gzip-hpp/mason_packages/osx-x86_64/benchmark/1.2.0/include/benchmark/benchmark.h:832:3: error: Potential memory leak [clang-analyzer-cplusplus.NewDeleteLeaks,-warnings-as-errors]
  return internal::RegisterBenchmarkInternal(
  ^
/Users/carol/dev/gzip-hpp/bench/run.cpp:47:5: note: Taking false branch
    if (!stream.is_open())
    ^
Suppressed 10135 warnings (10134 in non-user code, 1 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
1 warning treated as error
21620 warnings generated.
/Users/carol/dev/gzip-hpp/test/test.cpp:28:5: error: consider replacing 'unsigned long' with 'uint64' [google-runtime-int,-warnings-as-errors]
    unsigned long l = 2000000001;
    ^
Suppressed 21619 warnings (21619 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
1 warning treated as error
Applying fixes ...


namespace gzip {

// These live in gzip.hpp because it doesnt need to use deps.
// Otherwise, they would need to live in impl files if these methods used
// zlib structures or functions like inflate/deflate)
Expand Down