From dce7336bbe57f744bcf4d018d0ad422051df8817 Mon Sep 17 00:00:00 2001 From: Jacob Perron Date: Fri, 30 Aug 2019 00:53:13 -0700 Subject: [PATCH] Add compiler flags '-Wall -Wextra -Wpedantic' Fix warnings as a result. Signed-off-by: Jacob Perron --- CMakeLists.txt | 2 ++ include/create/types.h | 2 +- include/create/util.h | 4 ++-- src/create.cpp | 21 ++++++++++----------- src/packet.cpp | 6 +++--- src/serial.cpp | 6 +++--- src/serial_stream.cpp | 14 ++++---------- 7 files changed, 25 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a3ad38..753993d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,8 @@ cmake_minimum_required(VERSION 2.8.12) project(libcreate) +add_compile_options(-Wall -Wextra -Wpedantic) + set(PACKAGE_VERSION 1.6.1) option(LIBCREATE_BUILD_TESTS "Enable the build of tests." ON) diff --git a/include/create/types.h b/include/create/types.h index f9d99b0..4cf374f 100644 --- a/include/create/types.h +++ b/include/create/types.h @@ -181,7 +181,7 @@ namespace create { OC_SENSORS= 142, OC_QUERY_LIST=149, OC_STREAM = 148, - OC_TOGGLE_STREAM = 150, + OC_TOGGLE_STREAM = 150 }; enum BAUDCODE { diff --git a/include/create/util.h b/include/create/util.h index bbeaacd..6a28f06 100644 --- a/include/create/util.h +++ b/include/create/util.h @@ -55,13 +55,13 @@ namespace create { while (a < -PI) a += TWO_PI; while (a > PI) a -= TWO_PI; return a; - }; + } typedef unsigned long long timestamp_t; /** Get a timestamp for the current time in micro-seconds. */ - static timestamp_t getTimestamp() { + inline timestamp_t getTimestamp() { struct timeval now; gettimeofday(&now, NULL); return now.tv_usec + (timestamp_t) now.tv_sec * 1000000; diff --git a/src/create.cpp b/src/create.cpp index f908661..c023c8a 100644 --- a/src/create.cpp +++ b/src/create.cpp @@ -64,15 +64,15 @@ namespace create { } Create::Matrix Create::addMatrices(const Matrix &A, const Matrix &B) const { - int rows = A.size1(); - int cols = A.size2(); + size_t rows = A.size1(); + size_t cols = A.size2(); assert(rows == B.size1()); assert(cols == B.size2()); Matrix C(rows, cols); - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { + for (size_t i = 0u; i < rows; i++) { + for (size_t j = 0u; j < cols; j++) { const float a = A(i, j); const float b = B(i, j); if (util::willFloatOverflow(a, b)) { @@ -345,8 +345,8 @@ namespace create { bool Create::setDate(const DayOfWeek& day, const uint8_t& hour, const uint8_t& min) const { if (day < 0 || day > 6 || - hour < 0 || hour > 23 || - min < 0 || min > 59) + hour > 23 || + min > 59) return false; uint8_t cmd[4] = { OC_DATE, day, hour, min }; @@ -362,7 +362,7 @@ namespace create { int16_t radius_mm = roundf(radius * 1000); // Bound radius if not a special case - if (radius_mm != 32768 && radius_mm != 32767 && + if (radius_mm != -32768 && radius_mm != 32767 && radius_mm != -1 && radius_mm != 1) { BOUND(radius_mm, -util::MAX_RADIUS * 1000, util::MAX_RADIUS * 1000); } @@ -561,7 +561,7 @@ namespace create { const float* durations) const { int i, j; uint8_t duration; - uint8_t cmd[2 * songLength + 3]; + std::vector cmd(2 * songLength + 3); cmd[0] = OC_SONG; cmd[1] = songNumber; cmd[2] = songLength; @@ -575,11 +575,11 @@ namespace create { j++; } - return serial->send(cmd, 2 * songLength + 3); + return serial->send(cmd.data(), cmd.size()); } bool Create::playSong(const uint8_t& songNumber) const { - if (songNumber < 0 || songNumber > 4) + if (songNumber > 4) return false; uint8_t cmd[2] = { OC_PLAY, songNumber }; return serial->send(cmd, 2); @@ -754,7 +754,6 @@ namespace create { ChargingState Create::getChargingState() const { if (data->isValidPacketID(ID_CHARGE_STATE)) { uint8_t chargeState = GET_DATA(ID_CHARGE_STATE); - assert(chargeState >= 0); assert(chargeState <= 5); return (ChargingState) chargeState; } diff --git a/src/packet.cpp b/src/packet.cpp index 9b65305..d408f75 100644 --- a/src/packet.cpp +++ b/src/packet.cpp @@ -3,10 +3,10 @@ namespace create { Packet::Packet(const uint8_t& numBytes, const std::string& comment) : - nbytes(numBytes), - info(comment), data(0), - tmpData(0) { } + tmpData(0), + nbytes(numBytes), + info(comment) { } Packet::~Packet() { } diff --git a/src/serial.cpp b/src/serial.cpp index 8cf27eb..ad9f7fc 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -6,11 +6,11 @@ namespace create { Serial::Serial(boost::shared_ptr d) : - data(d), - port(io), signals(io, SIGINT, SIGTERM), - isReading(false), + port(io), dataReady(false), + isReading(false), + data(d), corruptPackets(0), totalPackets(0) { } diff --git a/src/serial_stream.cpp b/src/serial_stream.cpp index b06f263..6cd8ea3 100644 --- a/src/serial_stream.cpp +++ b/src/serial_stream.cpp @@ -10,19 +10,13 @@ namespace create { bool SerialStream::startSensorStream() { // Request from Create that we want a stream containing all packets - uint8_t numPackets = data->getNumPackets(); + const uint8_t numPackets = data->getNumPackets(); std::vector packetIDs = data->getPacketIDs(); - uint8_t streamReq[2 + numPackets]; - streamReq[0] = OC_STREAM; - streamReq[1] = numPackets; - int i = 2; - for (std::vector::iterator it = packetIDs.begin(); it != packetIDs.end(); ++it) { - streamReq[i] = *it; - i++; - } + packetIDs.insert(packetIDs.begin(), numPackets); + packetIDs.insert(packetIDs.begin(), OC_STREAM); // Start streaming data - send(streamReq, 2 + numPackets); + send(packetIDs.data(), packetIDs.size()); expectedNumBytes = data->getTotalDataBytes() + numPackets;