Skip to content

Commit 919a794

Browse files
Merge branch 'warnings'
2 parents 641932f + c2ba231 commit 919a794

File tree

77 files changed

+372
-319
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+372
-319
lines changed

libuavcan/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,14 @@ if (DEBUG_BUILD)
119119
message(STATUS "Debug build (note: requires gtest)")
120120

121121
if (COMPILER_IS_GCC_COMPATIBLE)
122+
# No such thing as too many warnings
122123
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -pedantic -Wfloat-equal -Wconversion")
123124
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsign-conversion -Wcast-align -Wmissing-declarations -Wlogical-op")
124125
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wdouble-promotion -Wswitch-enum -Wtype-limits -Wno-error=array-bounds")
125-
set(cpp03_flags "-std=c++03 -Wno-variadic-macros -Wno-long-long")
126+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wzero-as-null-pointer-constant -Wnon-virtual-dtor")
127+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Woverloaded-virtual -Wsign-promo -Wold-style-cast")
128+
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Weffc++ -Wno-error=effc++") # Produces heaps of useless warnings
129+
set(cpp03_flags "-std=c++03 -Wno-variadic-macros -Wno-long-long -Wno-zero-as-null-pointer-constant")
126130
set(optim_flags "-O3 -DNDEBUG -g0")
127131
else ()
128132
message(STATUS "Compiler ID: ${CMAKE_CXX_COMPILER_ID}")

libuavcan/dsdl_compiler/libuavcan_dsdl_compiler/data_type_template.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ template <>
365365
inline const typename ${scope_prefix}<0>::TagToType< ${scope_prefix}<0>::Tag::${a.name} >::StorageType*
366366
${scope_prefix}<0>::as< ${scope_prefix}<0>::Tag::${a.name} >() const
367367
{
368-
return is(${scope_prefix}<0>::Tag::${a.name}) ? &${a.name} : NULL;
368+
return is(${scope_prefix}<0>::Tag::${a.name}) ? &${a.name} : UAVCAN_NULLPTR;
369369
}
370370

371371
template <>

libuavcan/include/uavcan/build_config.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@
3838
# endif
3939
#endif
4040

41+
/**
42+
* The library uses UAVCAN_NULLPTR instead of UAVCAN_NULLPTR and nullptr in order to allow the use of
43+
* -Wzero-as-null-pointer-constant.
44+
*/
45+
#ifndef UAVCAN_NULLPTR
46+
# if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
47+
# define UAVCAN_NULLPTR nullptr
48+
# else
49+
# define UAVCAN_NULLPTR NULL
50+
# endif
51+
#endif
52+
4153
/**
4254
* By default, libuavcan enables all features if it detects that it is being built for a general-purpose
4355
* target like Linux. Value of this macro influences other configuration options located below in this file.

libuavcan/include/uavcan/driver/can.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct UAVCAN_EXPORT CanFrame
4646
id(can_id),
4747
dlc((data_len > MaxDataLen) ? MaxDataLen : data_len)
4848
{
49-
UAVCAN_ASSERT(can_data != NULL);
49+
UAVCAN_ASSERT(can_data != UAVCAN_NULLPTR);
5050
UAVCAN_ASSERT(data_len == dlc);
5151
(void)copy(can_data, can_data + dlc, this->data);
5252
}
@@ -232,7 +232,7 @@ class UAVCAN_EXPORT ICanDriver
232232
* The pending TX argument contains an array of pointers to CAN frames that the library wants to transmit
233233
* next, per interface. This is intended to allow the driver to properly prioritize transmissions; many
234234
* drivers will not need to use it. If a write flag for the given interface is set to one in the select mask
235-
* structure, then the corresponding pointer is guaranteed to be valid (not NULL).
235+
* structure, then the corresponding pointer is guaranteed to be valid (not UAVCAN_NULLPTR).
236236
*
237237
* @param [in,out] inout_masks Masks indicating which interfaces are needed/available for IO.
238238
* @param [in] pending_tx Array of frames, per interface, that are likely to be transmitted next.

libuavcan/include/uavcan/dynamic_memory.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,15 @@ PoolAllocator<PoolSize, BlockSize, RaiiSynchronizer>::PoolAllocator() :
153153
// coverity[dead_error_line : FALSE]
154154
free_list_[i].next = free_list_ + i + 1;
155155
}
156-
free_list_[NumBlocks - 1].next = NULL;
156+
free_list_[NumBlocks - 1].next = UAVCAN_NULLPTR;
157157
}
158158

159159
template <std::size_t PoolSize, uint8_t BlockSize, typename RaiiSynchronizer>
160160
void* PoolAllocator<PoolSize, BlockSize, RaiiSynchronizer>::allocate(std::size_t size)
161161
{
162-
if (free_list_ == NULL || size > BlockSize)
162+
if (free_list_ == UAVCAN_NULLPTR || size > BlockSize)
163163
{
164-
return NULL;
164+
return UAVCAN_NULLPTR;
165165
}
166166

167167
RaiiSynchronizer lock;
@@ -184,7 +184,7 @@ void* PoolAllocator<PoolSize, BlockSize, RaiiSynchronizer>::allocate(std::size_t
184184
template <std::size_t PoolSize, uint8_t BlockSize, typename RaiiSynchronizer>
185185
void PoolAllocator<PoolSize, BlockSize, RaiiSynchronizer>::deallocate(const void* ptr)
186186
{
187-
if (ptr == NULL)
187+
if (ptr == UAVCAN_NULLPTR)
188188
{
189189
return;
190190
}

libuavcan/include/uavcan/helpers/heap_based_pool_allocator.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class UAVCAN_EXPORT HeapBasedPoolAllocator : public IPoolAllocator,
7575
static_cast<uint32_t>(NumericTraits<uint16_t>::max())))),
7676
num_reserved_blocks_(0),
7777
num_allocated_blocks_(0),
78-
reserve_(NULL)
78+
reserve_(UAVCAN_NULLPTR)
7979
{ }
8080

8181
/**
@@ -101,7 +101,7 @@ class UAVCAN_EXPORT HeapBasedPoolAllocator : public IPoolAllocator,
101101
{
102102
if (size > BlockSize)
103103
{
104-
return NULL;
104+
return UAVCAN_NULLPTR;
105105
}
106106

107107
{
@@ -110,7 +110,7 @@ class UAVCAN_EXPORT HeapBasedPoolAllocator : public IPoolAllocator,
110110

111111
Node* const p = reserve_;
112112

113-
if (UAVCAN_LIKELY(p != NULL))
113+
if (UAVCAN_LIKELY(p != UAVCAN_NULLPTR))
114114
{
115115
reserve_ = reserve_->next;
116116
num_allocated_blocks_++;
@@ -119,13 +119,13 @@ class UAVCAN_EXPORT HeapBasedPoolAllocator : public IPoolAllocator,
119119

120120
if (num_reserved_blocks_ >= capacity_hard_limit_) // Hard limit reached, no further allocations
121121
{
122-
return NULL;
122+
return UAVCAN_NULLPTR;
123123
}
124124
}
125125

126126
// Unlikely branch
127127
void* const m = std::malloc(sizeof(Node));
128-
if (m != NULL)
128+
if (m != UAVCAN_NULLPTR)
129129
{
130130
RaiiSynchronizer lock;
131131
(void)lock;
@@ -142,7 +142,7 @@ class UAVCAN_EXPORT HeapBasedPoolAllocator : public IPoolAllocator,
142142
*/
143143
virtual void deallocate(const void* ptr)
144144
{
145-
if (ptr != NULL)
145+
if (ptr != UAVCAN_NULLPTR)
146146
{
147147
RaiiSynchronizer lock;
148148
(void)lock;
@@ -171,15 +171,15 @@ class UAVCAN_EXPORT HeapBasedPoolAllocator : public IPoolAllocator,
171171
*/
172172
void shrink()
173173
{
174-
Node* p = NULL;
174+
Node* p = UAVCAN_NULLPTR;
175175
for (;;)
176176
{
177177
{
178178
RaiiSynchronizer lock;
179179
(void)lock;
180180
// Removing from reserve and updating the counter.
181181
p = reserve_;
182-
if (p != NULL)
182+
if (p != UAVCAN_NULLPTR)
183183
{
184184
reserve_ = reserve_->next;
185185
num_reserved_blocks_--;

libuavcan/include/uavcan/marshal/array.hpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,8 @@ class UAVCAN_EXPORT Array : public ArrayImpl<T, ArrayMode, MaxSize_>
761761
* Members must be comparable via operator ==.
762762
*/
763763
template <typename R>
764-
typename EnableIf<sizeof(((const R*)(0U))->size()) && sizeof((*((const R*)(0U)))[0]), bool>::Type
764+
typename EnableIf<sizeof((reinterpret_cast<const R*>(0))->size()) &&
765+
sizeof((*(reinterpret_cast<const R*>(0)))[0]), bool>::Type
765766
operator==(const R& rhs) const
766767
{
767768
if (size() != rhs.size())
@@ -786,7 +787,8 @@ class UAVCAN_EXPORT Array : public ArrayImpl<T, ArrayMode, MaxSize_>
786787
* Any container with size() and [] is acceptable.
787788
*/
788789
template <typename R>
789-
typename EnableIf<sizeof(((const R*)(0U))->size()) && sizeof((*((const R*)(0U)))[0]), bool>::Type
790+
typename EnableIf<sizeof((reinterpret_cast<const R*>(0))->size()) &&
791+
sizeof((*(reinterpret_cast<const R*>(0)))[0]), bool>::Type
790792
isClose(const R& rhs) const
791793
{
792794
if (size() != rhs.size())
@@ -809,7 +811,7 @@ class UAVCAN_EXPORT Array : public ArrayImpl<T, ArrayMode, MaxSize_>
809811
*/
810812
bool operator==(const char* ch) const
811813
{
812-
if (ch == NULL)
814+
if (ch == UAVCAN_NULLPTR)
813815
{
814816
return false;
815817
}
@@ -830,7 +832,7 @@ class UAVCAN_EXPORT Array : public ArrayImpl<T, ArrayMode, MaxSize_>
830832
StaticAssert<Base::IsStringLike>::check();
831833
StaticAssert<IsDynamic>::check();
832834
Base::clear();
833-
if (ch == NULL)
835+
if (ch == UAVCAN_NULLPTR)
834836
{
835837
handleFatalError("Array::operator=(const char*)");
836838
}
@@ -849,7 +851,7 @@ class UAVCAN_EXPORT Array : public ArrayImpl<T, ArrayMode, MaxSize_>
849851
{
850852
StaticAssert<Base::IsStringLike>::check();
851853
StaticAssert<IsDynamic>::check();
852-
if (ch == NULL)
854+
if (ch == UAVCAN_NULLPTR)
853855
{
854856
handleFatalError("Array::operator+=(const char*)");
855857
}
@@ -1001,7 +1003,8 @@ class UAVCAN_EXPORT Array : public ArrayImpl<T, ArrayMode, MaxSize_>
10011003
* Note that matrix packing code uses @ref areClose() for comparison.
10021004
*/
10031005
template <typename R>
1004-
typename EnableIf<sizeof(((const R*)(0U))->begin()) && sizeof(((const R*)(0U))->size())>::Type
1006+
typename EnableIf<sizeof((reinterpret_cast<const R*>(0))->begin()) &&
1007+
sizeof((reinterpret_cast<const R*>(0))->size())>::Type
10051008
packSquareMatrix(const R& src_row_major)
10061009
{
10071010
if (src_row_major.size() == MaxSize)
@@ -1057,7 +1060,8 @@ class UAVCAN_EXPORT Array : public ArrayImpl<T, ArrayMode, MaxSize_>
10571060
* Please refer to the specification to learn more about matrix packing.
10581061
*/
10591062
template <typename R>
1060-
typename EnableIf<sizeof(((const R*)(0U))->begin()) && sizeof(((const R*)(0U))->size())>::Type
1063+
typename EnableIf<sizeof((reinterpret_cast<const R*>(0))->begin()) &&
1064+
sizeof((reinterpret_cast<const R*>(0))->size())>::Type
10611065
unpackSquareMatrix(R& dst_row_major) const
10621066
{
10631067
if (dst_row_major.size() == MaxSize)

libuavcan/include/uavcan/node/generic_publisher.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class UAVCAN_EXPORT GenericPublisher : public GenericPublisherBase
8686
{
8787
struct ZeroTransferBuffer : public StaticTransferBufferImpl
8888
{
89-
ZeroTransferBuffer() : StaticTransferBufferImpl(NULL, 0) { }
89+
ZeroTransferBuffer() : StaticTransferBufferImpl(UAVCAN_NULLPTR, 0) { }
9090
};
9191

9292
typedef typename Select<DataStruct::MaxBitLen == 0,
@@ -138,7 +138,7 @@ class UAVCAN_EXPORT GenericPublisher : public GenericPublisherBase
138138
int publish(const DataStruct& message, TransferType transfer_type, NodeID dst_node_id,
139139
MonotonicTime blocking_deadline = MonotonicTime())
140140
{
141-
return genericPublish(message, transfer_type, dst_node_id, NULL, blocking_deadline);
141+
return genericPublish(message, transfer_type, dst_node_id, UAVCAN_NULLPTR, blocking_deadline);
142142
}
143143

144144
int publish(const DataStruct& message, TransferType transfer_type, NodeID dst_node_id, TransferID tid,

libuavcan/include/uavcan/node/generic_subscriber.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,21 @@ namespace uavcan
2929
* void first(const ReceivedDataStructure<Foo>& msg);
3030
* void second(const Foo& msg);
3131
* In the latter case, an implicit cast will happen before the callback is invoked.
32+
*
33+
* This class is not copyable because it holds a reference to a stack-allocated transfer descriptor object.
34+
* You can slice cast it to the underlying data type though, which would be copyable:
35+
* DataType dt = rds; // where rds is of type ReceivedDataStructure<DataType>
36+
* // dt is now copyable
3237
*/
3338
template <typename DataType_>
34-
class UAVCAN_EXPORT ReceivedDataStructure : public DataType_
39+
class UAVCAN_EXPORT ReceivedDataStructure : public DataType_, Noncopyable
3540
{
3641
const IncomingTransfer* const _transfer_; ///< Such weird name is necessary to avoid clashing with DataType fields
3742

3843
template <typename Ret, Ret(IncomingTransfer::*Fun) () const>
3944
Ret safeget() const
4045
{
41-
if (_transfer_ == NULL)
46+
if (_transfer_ == UAVCAN_NULLPTR)
4247
{
4348
return Ret();
4449
}
@@ -47,13 +52,13 @@ class UAVCAN_EXPORT ReceivedDataStructure : public DataType_
4752

4853
protected:
4954
ReceivedDataStructure()
50-
: _transfer_(NULL)
55+
: _transfer_(UAVCAN_NULLPTR)
5156
{ }
5257

5358
ReceivedDataStructure(const IncomingTransfer* arg_transfer)
5459
: _transfer_(arg_transfer)
5560
{
56-
UAVCAN_ASSERT(arg_transfer != NULL);
61+
UAVCAN_ASSERT(arg_transfer != UAVCAN_NULLPTR);
5762
}
5863

5964
public:
@@ -230,7 +235,7 @@ int GenericSubscriber<DataSpec, DataStruct, TransferListenerType>::checkInit()
230235
GlobalDataTypeRegistry::instance().freeze();
231236
const DataTypeDescriptor* const descr =
232237
GlobalDataTypeRegistry::instance().find(DataTypeKind(DataSpec::DataTypeKind), DataSpec::getDataTypeFullName());
233-
if (descr == NULL)
238+
if (descr == UAVCAN_NULLPTR)
234239
{
235240
UAVCAN_TRACE("GenericSubscriber", "Type [%s] is not registered", DataSpec::getDataTypeFullName());
236241
return -ErrUnknownDataType;

libuavcan/include/uavcan/node/global_data_type_registry.hpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ class UAVCAN_EXPORT GlobalDataTypeRegistry : Noncopyable
4949
{
5050
const DataTypeID id;
5151
explicit EntryInsertionComparator(const Entry* dtd)
52-
: id((dtd == NULL) ? DataTypeID() : dtd->descriptor.getID())
52+
: id((dtd == UAVCAN_NULLPTR) ? DataTypeID() : dtd->descriptor.getID())
5353
{
54-
UAVCAN_ASSERT(dtd != NULL);
54+
UAVCAN_ASSERT(dtd != UAVCAN_NULLPTR);
5555
}
5656
bool operator()(const Entry* entry) const
5757
{
58-
UAVCAN_ASSERT(entry != NULL);
58+
UAVCAN_ASSERT(entry != UAVCAN_NULLPTR);
5959
return entry->descriptor.getID() > id;
6060
}
6161
};
@@ -211,15 +211,21 @@ GlobalDataTypeRegistry::RegistrationResult GlobalDataTypeRegistry::registerDataT
211211
{
212212
return RegistrationResultFrozen;
213213
}
214+
214215
static Entry entry;
216+
215217
{
216218
const RegistrationResult remove_res = remove(&entry);
217219
if (remove_res != RegistrationResultOk)
218220
{
219221
return remove_res;
220222
}
221223
}
222-
entry = Entry(DataTypeKind(Type::DataTypeKind), id, Type::getDataTypeSignature(), Type::getDataTypeFullName());
224+
225+
// We can't just overwrite the entry itself because it's noncopyable
226+
entry.descriptor = DataTypeDescriptor(DataTypeKind(Type::DataTypeKind), id,
227+
Type::getDataTypeSignature(), Type::getDataTypeFullName());
228+
223229
{
224230
const RegistrationResult remove_res = remove(&entry);
225231
if (remove_res != RegistrationResultOk)

0 commit comments

Comments
 (0)