Skip to content

Commit

Permalink
Add clang-tidy config and tidy-up generated code (#3254)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier authored Dec 11, 2024
1 parent af4ac49 commit fedd40a
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 11 deletions.
20 changes: 20 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Checks:
'-*,
clang-analyzer-*,
-clang-diagnostic-shadow-uncaptured-local,
cert-*,
modernize-*,
-modernize-avoid-c-arrays,
-modernize-use-trailing-return-type,
performance-*,
-performance-avoid-endl
'
# WarningsAsErrors: '*'
HeaderFilterRegex: ''
UseColor: true
FormatStyle: 'file'
ExtraArgs: ['-std=c++17']
CheckOptions:
modernize-use-nullptr.NullMacros: 'NULL'
# std::exception_ptr is a cheap to copy, pointer-like type; we pass it by value all the time.
performance-unnecessary-value-param.AllowedTypes: 'exception_ptr$;'
2 changes: 1 addition & 1 deletion cpp/include/Ice/FactoryTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace IceInternal
class ICE_API CompactIdInit
{
public:
CompactIdInit(std::string_view, int) noexcept;
CompactIdInit(const char* typeId, int compactId) noexcept;
~CompactIdInit();

private:
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/Ice/FactoryTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ IceInternal::FactoryTableInit::~FactoryTableInit()
}
}

IceInternal::CompactIdInit::CompactIdInit(string_view typeId, int compactId) noexcept : _compactId(compactId)
IceInternal::CompactIdInit::CompactIdInit(const char* typeId, int compactId) noexcept : _compactId(compactId)
{
assert(_compactId >= 0);
factoryTable->addTypeId(_compactId, typeId);
Expand Down
34 changes: 34 additions & 0 deletions cpp/src/slice2cpp/CPlusPlusUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@ using namespace IceInternal;

namespace
{
bool isTriviallyCopyable(const StructPtr& st)
{
assert(st);
for (const auto& m : st->dataMembers())
{
if (BuiltinPtr builtin = dynamic_pointer_cast<Builtin>(m->type()))
{
if (builtin->isVariableLength())
{
return false;
}
// Numeric and bool are fixed-length and trivially copyable.
}
else if (StructPtr nestedSt = dynamic_pointer_cast<Struct>(m->type()))
{
if (!isTriviallyCopyable(nestedSt))
{
return false;
}
}
else if (!dynamic_pointer_cast<Enum>(m->type()))
{
return false;
}
}
return true;
}

string stringTypeToString(const TypePtr&, const MetadataList& metadata, TypeContext typeCtx)
{
string strType = findMetadata(metadata, typeCtx);
Expand Down Expand Up @@ -423,6 +451,12 @@ Slice::isMovable(const TypePtr& type)
}
}
}

if (StructPtr st = dynamic_pointer_cast<Struct>(type))
{
return !isTriviallyCopyable(st);
}

return !dynamic_pointer_cast<Enum>(type);
}

Expand Down
22 changes: 16 additions & 6 deletions cpp/test/Ice/custom/CustomBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Test
template<typename T> class CustomBuffer
{
public:
CustomBuffer() : _buf(0), _count(0) {}
CustomBuffer() : _buf(nullptr), _count(0) {}

CustomBuffer(const CustomBuffer& o) : _buf(0), _count(o._count)
{
Expand All @@ -31,14 +31,14 @@ namespace Test
}
}

~CustomBuffer()
CustomBuffer(CustomBuffer&& o) : _buf(o._buf), _count(o._count)
{
if (_buf != 0)
{
delete[] _buf;
}
o._buf = nullptr;
o._count = 0;
}

~CustomBuffer() { delete[] _buf; }

CustomBuffer& operator=(const CustomBuffer& o)
{
_count = o._count;
Expand All @@ -53,6 +53,16 @@ namespace Test
return *this;
}

CustomBuffer& operator=(CustomBuffer&& o)
{
delete[] _buf;
_buf = o._buf;
_count = o._count;
o._buf = nullptr;
o._count = 0;
return *this;
}

size_t count() const { return _count; }

T* get() const { return _buf; }
Expand Down
25 changes: 23 additions & 2 deletions cpp/test/Ice/custom/MyByteSeq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ MyByteSeq::MyByteSeq(const MyByteSeq& seq)
}
else
{
_data = 0;
_data = nullptr;
}
}

MyByteSeq::MyByteSeq(MyByteSeq&& seq) noexcept : _size(seq._size), _data(seq._data)
{
seq._size = 0;
seq._data = nullptr;
}

MyByteSeq::~MyByteSeq() { delete[] _data; }

size_t
Expand Down Expand Up @@ -62,7 +68,7 @@ MyByteSeq::end() const
return _data + _size;
}

void
MyByteSeq&
MyByteSeq::operator=(const MyByteSeq& rhs)
{
delete[] _data;
Expand All @@ -74,6 +80,21 @@ MyByteSeq::operator=(const MyByteSeq& rhs)
_data = new std::byte[_size];
memcpy(_data, rhs._data, _size);
}
return *this;
}

MyByteSeq&
MyByteSeq::operator=(MyByteSeq&& rhs)
{
delete[] _data;
_data = nullptr;

_size = rhs._size;
_data = rhs._data;

rhs._size = 0;
rhs._data = nullptr;
return *this;
}

bool
Expand Down
6 changes: 5 additions & 1 deletion cpp/test/Ice/custom/MyByteSeq.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ class MyByteSeq
MyByteSeq();
MyByteSeq(size_t);
MyByteSeq(const MyByteSeq&);
MyByteSeq(MyByteSeq&&) noexcept;
~MyByteSeq();

size_t size() const;
void swap(MyByteSeq&);
const_iterator begin() const;
const_iterator end() const;
void operator=(const MyByteSeq&);

MyByteSeq& operator=(const MyByteSeq&);
MyByteSeq& operator=(MyByteSeq&&);

bool operator==(const MyByteSeq&) const;

private:
Expand Down

0 comments on commit fedd40a

Please sign in to comment.