From 747eb149d06d9077e7b44aa1edd966e637386f33 Mon Sep 17 00:00:00 2001 From: Chih-Hung Hsieh Date: Tue, 25 Sep 2018 11:16:22 -0700 Subject: [PATCH] Add noexcept to move constructors and assignment operators. Bug: 116614593 Test: build with WITH_TIDY=1 Change-Id: I5a7461386946ca623ab509609092aa0ac8418b80 --- adb/types.h | 10 ++++------ base/include/android-base/scopeguard.h | 2 +- base/include/android-base/unique_fd.h | 4 ++-- fs_mgr/libdm/dm_test.cpp | 5 +++-- init/keychords_test.cpp | 8 ++++---- liblog/event_tag_map.cpp | 2 +- libmemunreachable/ScopedPipe.h | 4 ++-- libmemunreachable/Tarjan.h | 2 +- libutils/FileMap.cpp | 16 +++++++++++----- libutils/include/utils/FileMap.h | 4 ++-- libutils/include/utils/StrongPointer.h | 13 ++++++------- libziparchive/include/ziparchive/zip_writer.h | 4 ++-- libziparchive/zip_archive.cc | 2 +- libziparchive/zip_writer.cc | 4 ++-- logd/LogStatistics.h | 2 +- trusty/keymaster/3.0/TrustyKeymaster3Device.cpp | 3 ++- 16 files changed, 45 insertions(+), 40 deletions(-) diff --git a/adb/types.h b/adb/types.h index a3e5d4842..1f7008e00 100644 --- a/adb/types.h +++ b/adb/types.h @@ -42,14 +42,14 @@ struct Block { } Block(const Block& copy) = delete; - Block(Block&& move) { + Block(Block&& move) noexcept { std::swap(data_, move.data_); std::swap(capacity_, move.capacity_); std::swap(size_, move.size_); } Block& operator=(const Block& copy) = delete; - Block& operator=(Block&& move) { + Block& operator=(Block&& move) noexcept { clear(); std::swap(data_, move.data_); @@ -147,12 +147,10 @@ struct IOVector { } IOVector(const IOVector& copy) = delete; - IOVector(IOVector&& move) : IOVector() { - *this = std::move(move); - } + IOVector(IOVector&& move) noexcept : IOVector() { *this = std::move(move); } IOVector& operator=(const IOVector& copy) = delete; - IOVector& operator=(IOVector&& move) { + IOVector& operator=(IOVector&& move) noexcept { chain_ = std::move(move.chain_); chain_length_ = move.chain_length_; begin_offset_ = move.begin_offset_; diff --git a/base/include/android-base/scopeguard.h b/base/include/android-base/scopeguard.h index e6a9d109d..5a224d634 100644 --- a/base/include/android-base/scopeguard.h +++ b/base/include/android-base/scopeguard.h @@ -28,7 +28,7 @@ class ScopeGuard { public: ScopeGuard(F&& f) : f_(std::forward(f)), active_(true) {} - ScopeGuard(ScopeGuard&& that) : f_(std::move(that.f_)), active_(that.active_) { + ScopeGuard(ScopeGuard&& that) noexcept : f_(std::move(that.f_)), active_(that.active_) { that.active_ = false; } diff --git a/base/include/android-base/unique_fd.h b/base/include/android-base/unique_fd.h index 71025adc2..cd2dc0416 100644 --- a/base/include/android-base/unique_fd.h +++ b/base/include/android-base/unique_fd.h @@ -90,8 +90,8 @@ class unique_fd_impl final { explicit unique_fd_impl(int fd) { reset(fd); } ~unique_fd_impl() { reset(); } - unique_fd_impl(unique_fd_impl&& other) { reset(other.release()); } - unique_fd_impl& operator=(unique_fd_impl&& s) { + unique_fd_impl(unique_fd_impl&& other) noexcept { reset(other.release()); } + unique_fd_impl& operator=(unique_fd_impl&& s) noexcept { int fd = s.fd_; s.fd_ = -1; reset(fd, &s); diff --git a/fs_mgr/libdm/dm_test.cpp b/fs_mgr/libdm/dm_test.cpp index cc6191753..70823c619 100644 --- a/fs_mgr/libdm/dm_test.cpp +++ b/fs_mgr/libdm/dm_test.cpp @@ -59,7 +59,8 @@ class TempDevice { : dm_(DeviceMapper::Instance()), name_(name), valid_(false) { valid_ = dm_.CreateDevice(name, table); } - TempDevice(TempDevice&& other) : dm_(other.dm_), name_(other.name_), valid_(other.valid_) { + TempDevice(TempDevice&& other) noexcept + : dm_(other.dm_), name_(other.name_), valid_(other.valid_) { other.valid_ = false; } ~TempDevice() { @@ -103,7 +104,7 @@ class TempDevice { TempDevice(const TempDevice&) = delete; TempDevice& operator=(const TempDevice&) = delete; - TempDevice& operator=(TempDevice&& other) { + TempDevice& operator=(TempDevice&& other) noexcept { name_ = other.name_; valid_ = other.valid_; other.valid_ = false; diff --git a/init/keychords_test.cpp b/init/keychords_test.cpp index c8c47a8d5..a3baeb116 100644 --- a/init/keychords_test.cpp +++ b/init/keychords_test.cpp @@ -47,9 +47,9 @@ class EventHandler { public: EventHandler(); EventHandler(const EventHandler&) = delete; - EventHandler(EventHandler&&); + EventHandler(EventHandler&&) noexcept; EventHandler& operator=(const EventHandler&) = delete; - EventHandler& operator=(EventHandler&&); + EventHandler& operator=(EventHandler&&) noexcept; ~EventHandler() noexcept; bool init(); @@ -64,11 +64,11 @@ class EventHandler { EventHandler::EventHandler() : fd_(-1) {} -EventHandler::EventHandler(EventHandler&& rval) : fd_(rval.fd_) { +EventHandler::EventHandler(EventHandler&& rval) noexcept : fd_(rval.fd_) { rval.fd_ = -1; } -EventHandler& EventHandler::operator=(EventHandler&& rval) { +EventHandler& EventHandler::operator=(EventHandler&& rval) noexcept { fd_ = rval.fd_; rval.fd_ = -1; return *this; diff --git a/liblog/event_tag_map.cpp b/liblog/event_tag_map.cpp index 2e2bf8734..574a3869b 100644 --- a/liblog/event_tag_map.cpp +++ b/liblog/event_tag_map.cpp @@ -72,7 +72,7 @@ class MapString { explicit MapString(const std::string& str) : alloc(new std::string(str)), str(alloc->data(), alloc->length()) { } - MapString(MapString&& rval) + MapString(MapString&& rval) noexcept : alloc(rval.alloc), str(rval.data(), rval.length()) { rval.alloc = NULL; } diff --git a/libmemunreachable/ScopedPipe.h b/libmemunreachable/ScopedPipe.h index adabfd8b7..b9dead50d 100644 --- a/libmemunreachable/ScopedPipe.h +++ b/libmemunreachable/ScopedPipe.h @@ -33,12 +33,12 @@ class ScopedPipe { } ~ScopedPipe() { Close(); } - ScopedPipe(ScopedPipe&& other) { + ScopedPipe(ScopedPipe&& other) noexcept { SetReceiver(other.ReleaseReceiver()); SetSender(other.ReleaseSender()); } - ScopedPipe& operator=(ScopedPipe&& other) { + ScopedPipe& operator=(ScopedPipe&& other) noexcept { SetReceiver(other.ReleaseReceiver()); SetSender(other.ReleaseSender()); return *this; diff --git a/libmemunreachable/Tarjan.h b/libmemunreachable/Tarjan.h index 355679f72..f3ab65252 100644 --- a/libmemunreachable/Tarjan.h +++ b/libmemunreachable/Tarjan.h @@ -38,7 +38,7 @@ class Node { Node(T* ptr, Allocator allocator) : references_in(allocator), references_out(allocator), ptr(ptr){}; - Node(Node&& rhs) = default; + Node(Node&& rhs) noexcept = default; void Edge(Node* ref) { references_out.emplace(ref); ref->references_in.emplace(this); diff --git a/libutils/FileMap.cpp b/libutils/FileMap.cpp index 583c6b9e4..5feb2aa76 100644 --- a/libutils/FileMap.cpp +++ b/libutils/FileMap.cpp @@ -62,11 +62,17 @@ FileMap::FileMap(void) } // Move Constructor. -FileMap::FileMap(FileMap&& other) - : mFileName(other.mFileName), mBasePtr(other.mBasePtr), mBaseLength(other.mBaseLength), - mDataOffset(other.mDataOffset), mDataPtr(other.mDataPtr), mDataLength(other.mDataLength) +FileMap::FileMap(FileMap&& other) noexcept + : mFileName(other.mFileName), + mBasePtr(other.mBasePtr), + mBaseLength(other.mBaseLength), + mDataOffset(other.mDataOffset), + mDataPtr(other.mDataPtr), + mDataLength(other.mDataLength) #if defined(__MINGW32__) - , mFileHandle(other.mFileHandle), mFileMapping(other.mFileMapping) + , + mFileHandle(other.mFileHandle), + mFileMapping(other.mFileMapping) #endif { other.mFileName = nullptr; @@ -79,7 +85,7 @@ FileMap::FileMap(FileMap&& other) } // Move assign operator. -FileMap& FileMap::operator=(FileMap&& other) { +FileMap& FileMap::operator=(FileMap&& other) noexcept { mFileName = other.mFileName; mBasePtr = other.mBasePtr; mBaseLength = other.mBaseLength; diff --git a/libutils/include/utils/FileMap.h b/libutils/include/utils/FileMap.h index 8d402a3c2..f9f8f3c60 100644 --- a/libutils/include/utils/FileMap.h +++ b/libutils/include/utils/FileMap.h @@ -52,8 +52,8 @@ class FileMap { public: FileMap(void); - FileMap(FileMap&& f); - FileMap& operator=(FileMap&& f); + FileMap(FileMap&& f) noexcept; + FileMap& operator=(FileMap&& f) noexcept; /* * Create a new mapping on an open file. diff --git a/libutils/include/utils/StrongPointer.h b/libutils/include/utils/StrongPointer.h index 3abce1705..15711296d 100644 --- a/libutils/include/utils/StrongPointer.h +++ b/libutils/include/utils/StrongPointer.h @@ -56,7 +56,7 @@ public: sp(T* other); // NOLINT(implicit) sp(const sp& other); - sp(sp&& other); + sp(sp&& other) noexcept; template sp(U* other); // NOLINT(implicit) template sp(const sp& other); // NOLINT(implicit) template sp(sp&& other); // NOLINT(implicit) @@ -67,7 +67,7 @@ public: sp& operator = (T* other); sp& operator = (const sp& other); - sp& operator = (sp&& other); + sp& operator=(sp&& other) noexcept; template sp& operator = (const sp& other); template sp& operator = (sp&& other); @@ -125,9 +125,8 @@ sp::sp(const sp& other) m_ptr->incStrong(this); } -template -sp::sp(sp&& other) - : m_ptr(other.m_ptr) { +template +sp::sp(sp&& other) noexcept : m_ptr(other.m_ptr) { other.m_ptr = nullptr; } @@ -169,8 +168,8 @@ sp& sp::operator =(const sp& other) { return *this; } -template -sp& sp::operator =(sp&& other) { +template +sp& sp::operator=(sp&& other) noexcept { T* oldPtr(*const_cast(&m_ptr)); if (oldPtr) oldPtr->decStrong(this); if (oldPtr != *const_cast(&m_ptr)) sp_report_race(); diff --git a/libziparchive/include/ziparchive/zip_writer.h b/libziparchive/include/ziparchive/zip_writer.h index c350a2750..0e0caf11d 100644 --- a/libziparchive/include/ziparchive/zip_writer.h +++ b/libziparchive/include/ziparchive/zip_writer.h @@ -91,10 +91,10 @@ class ZipWriter { explicit ZipWriter(FILE* f); // Move constructor. - ZipWriter(ZipWriter&& zipWriter); + ZipWriter(ZipWriter&& zipWriter) noexcept; // Move assignment. - ZipWriter& operator=(ZipWriter&& zipWriter); + ZipWriter& operator=(ZipWriter&& zipWriter) noexcept; /** * Starts a new zip entry with the given path and flags. diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc index f8d1356aa..6a3db6bee 100644 --- a/libziparchive/zip_archive.cc +++ b/libziparchive/zip_archive.cc @@ -903,7 +903,7 @@ class FileWriter : public zip_archive::Writer { return FileWriter(fd, declared_length); } - FileWriter(FileWriter&& other) + FileWriter(FileWriter&& other) noexcept : fd_(other.fd_), declared_length_(other.declared_length_), total_bytes_written_(other.total_bytes_written_) { diff --git a/libziparchive/zip_writer.cc b/libziparchive/zip_writer.cc index 6ad3366e4..ed1d1350c 100644 --- a/libziparchive/zip_writer.cc +++ b/libziparchive/zip_writer.cc @@ -97,7 +97,7 @@ ZipWriter::ZipWriter(FILE* f) } } -ZipWriter::ZipWriter(ZipWriter&& writer) +ZipWriter::ZipWriter(ZipWriter&& writer) noexcept : file_(writer.file_), seekable_(writer.seekable_), current_offset_(writer.current_offset_), @@ -109,7 +109,7 @@ ZipWriter::ZipWriter(ZipWriter&& writer) writer.state_ = State::kError; } -ZipWriter& ZipWriter::operator=(ZipWriter&& writer) { +ZipWriter& ZipWriter::operator=(ZipWriter&& writer) noexcept { file_ = writer.file_; seekable_ = writer.seekable_; current_offset_ = writer.current_offset_; diff --git a/logd/LogStatistics.h b/logd/LogStatistics.h index d6b8ab385..469f6dcf4 100644 --- a/logd/LogStatistics.h +++ b/logd/LogStatistics.h @@ -531,7 +531,7 @@ struct TagNameKey { name = std::string_view(alloc->c_str(), alloc->size()); } - explicit TagNameKey(TagNameKey&& rval) + explicit TagNameKey(TagNameKey&& rval) noexcept : alloc(rval.alloc), name(rval.name.data(), rval.name.length()) { rval.alloc = nullptr; } diff --git a/trusty/keymaster/3.0/TrustyKeymaster3Device.cpp b/trusty/keymaster/3.0/TrustyKeymaster3Device.cpp index 0849ee959..98cbcc3f8 100644 --- a/trusty/keymaster/3.0/TrustyKeymaster3Device.cpp +++ b/trusty/keymaster/3.0/TrustyKeymaster3Device.cpp @@ -112,7 +112,8 @@ class KmParamSet : public keymaster_key_param_set_t { } } } - KmParamSet(KmParamSet&& other) : keymaster_key_param_set_t{other.params, other.length} { + KmParamSet(KmParamSet&& other) noexcept + : keymaster_key_param_set_t{other.params, other.length} { other.length = 0; other.params = nullptr; }