Преглед изворни кода

Code improvements

- Fix clang analyzer issues
- Fix compilation flags for windows
Alexey Edelev пре 5 година
родитељ
комит
9fa469a155

+ 1 - 1
CMakeLists.txt

@@ -49,7 +49,7 @@ elseif(WIN32)
     if (${CMAKE_CXX_COMPILER_ID} MATCHES "MSVC")
         set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "/WX /W4")
     else()
-        message(WARNING "Only MSVC compilers family is supported for build")
+        message(WARNING "Only MSVC compilers family are supported for build")
     endif()
 endif()
 

+ 1 - 1
examples/addressbookserver/main.cpp

@@ -46,7 +46,7 @@ public:
         m_lastCallStatus.set_status(CallStatus::Inactive);
     }
 
-    ~AddressBookService() {}
+    ~AddressBookService() = default;
 
     void updateContacts() {
         for(unsigned int i = 0; i < (m_clients.size() - 1); i++) {

+ 2 - 2
src/generator/classgeneratorbase.cpp

@@ -38,7 +38,7 @@ using namespace ::google::protobuf;
 using namespace ::google::protobuf::io;
 using namespace ::google::protobuf::compiler;
 
-ClassGeneratorBase::ClassGeneratorBase(std::string fullClassName, std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> out) : mOutput(std::move(out))
+ClassGeneratorBase::ClassGeneratorBase(const std::string &fullClassName, std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> out) : mOutput(std::move(out))
   , mPrinter(mOutput.get(), '$')
 {
     mPrinter.Print(Templates::DisclaimerTemplate);
@@ -208,7 +208,7 @@ std::string ClassGeneratorBase::getNamespacesList(const T *message, std::vector<
         }
     }
 
-    if (container.size() > 0
+    if (!container.empty()
             && localNamespace != result) {
         result = result.append("::");
     } else {

+ 1 - 1
src/generator/classgeneratorbase.h

@@ -46,7 +46,7 @@ using PropertyMap = std::map<std::string, std::string>;
 class ClassGeneratorBase
 {
 public:
-    ClassGeneratorBase(std::string className, std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out);
+    ClassGeneratorBase(const std::string &className, std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out);
     virtual ~ClassGeneratorBase() = default;
     virtual void run() = 0;
 protected:

+ 1 - 1
src/generator/classsourcegeneratorbase.cpp

@@ -35,7 +35,7 @@ using namespace ::google::protobuf;
 using namespace ::google::protobuf::io;
 using namespace ::google::protobuf::compiler;
 
-ClassSourceGeneratorBase::ClassSourceGeneratorBase(std::string fullClassName,
+ClassSourceGeneratorBase::ClassSourceGeneratorBase(const std::string &fullClassName,
                                                    std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> out) :
     ClassGeneratorBase(fullClassName, std::move(out))
 {

+ 2 - 2
src/generator/classsourcegeneratorbase.h

@@ -36,8 +36,8 @@ namespace generator {
 class ClassSourceGeneratorBase : public ClassGeneratorBase
 {
 public:
-    ClassSourceGeneratorBase(std::string className, std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out);
-    virtual ~ClassSourceGeneratorBase() = default;
+    ClassSourceGeneratorBase(const std::string &className, std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out);
+    ~ClassSourceGeneratorBase() = default;
     virtual void run() = 0;
 
 protected:

+ 3 - 3
src/generator/generator.h

@@ -40,16 +40,16 @@ namespace generator {
 
 class QtGenerator : public ::google::protobuf::compiler::CodeGenerator
 {
-    virtual bool Generate(const ::google::protobuf::FileDescriptor *file,
+    bool Generate(const ::google::protobuf::FileDescriptor *file,
                           const std::string &parameter,
                           ::google::protobuf::compiler::GeneratorContext *generatorContext,
                           std::string *error) const override;
 
-    virtual bool GenerateAll(const std::vector<const ::google::protobuf::FileDescriptor *> &files,
+    bool GenerateAll(const std::vector<const ::google::protobuf::FileDescriptor *> &files,
                              const std::string &parameter,
                              ::google::protobuf::compiler::GeneratorContext *generatorContext,
                              std::string *error) const override;
-      virtual bool HasGenerateAll() const override { return true; }
+    bool HasGenerateAll() const override { return true; }
 };
 
 } //namespace generator

+ 1 - 1
src/generator/globalenumssourcegenerator.cpp

@@ -55,7 +55,7 @@ void GlobalEnumsSourceGenerator::run() {
 void GlobalEnumsSourceGenerator::printRegisterBody(const std::list<const FileDescriptor *> &list,
                                                    const std::vector<std::string> &namespaces)
 {
-    assert(list.size() > 0);
+    assert(!list.empty());
 
     std::string fullNamespace;
     for (auto name : namespaces) {

+ 1 - 0
src/generator/servergenerator.cpp

@@ -42,6 +42,7 @@ using namespace ::google::protobuf::compiler;
 
 ServerGenerator::ServerGenerator(const ServiceDescriptor *service, std::unique_ptr<io::ZeroCopyOutputStream> out) :
     ServiceGeneratorBase(service, std::move(out))
+  , mService(nullptr)
 {
     mClassName += "Server";
 }

+ 1 - 1
src/generator/utils.h

@@ -50,7 +50,7 @@ static void split(const std::string &str, std::vector<std::string> &container, c
     }
 }
 
-static void replace(std::string & data, std::string from, std::string to) {
+static void replace(std::string &data, const std::string &from, const std::string &to) {
     size_t pos = data.find(from);
     while (pos != std::string::npos) {
         data.replace(pos, from.size(), to);

+ 5 - 3
src/grpc/abstractclient.cpp

@@ -28,8 +28,10 @@
 #include <QTimer>
 
 namespace qtprotobuf {
-struct AbstractClientPrivate final {
-    AbstractClientPrivate(const QString &service) : service(service) {}
+class AbstractClientPrivate final {
+public:
+    AbstractClientPrivate(const QString &service) : service(service)
+    , lastError(AbstractChannel::StatusCodes::Ok) {}
 
     std::shared_ptr<AbstractChannel> channel;
     const QString service;
@@ -51,7 +53,7 @@ AbstractClient::~AbstractClient()
     delete d;
 }
 
-void AbstractClient::attachChannel(std::shared_ptr<AbstractChannel> channel)
+void AbstractClient::attachChannel(const std::shared_ptr<AbstractChannel> &channel)
 {
     d->channel = channel;
 }

+ 3 - 1
src/grpc/abstractclient.h

@@ -47,7 +47,7 @@ class AbstractClientPrivate;
 class QTGRPCSHARED_EXPORT AbstractClient : public QObject
 {
 public:
-    void attachChannel(std::shared_ptr<AbstractChannel> channel);
+    void attachChannel(const std::shared_ptr<AbstractChannel> &channel);
 
     AbstractChannel::StatusCodes lastError() const;
     QString lastErrorString() const;
@@ -119,6 +119,8 @@ private:
     void subscribe_p(const QString &method, const QByteArray& arg, const std::function<void(const QByteArray&)> &handler);
 
     Q_DISABLE_COPY(AbstractClient)
+    AbstractClient(AbstractClient &&) = delete;
+    AbstractClient &operator =(AbstractClient &&) = delete;
 
     AbstractClientPrivate *d;
 };

+ 2 - 0
src/grpc/asyncreply.h

@@ -66,6 +66,8 @@ protected:
 private:
     AsyncReply();
     Q_DISABLE_COPY(AsyncReply)
+    AsyncReply(AsyncReply &&) = delete;
+    AsyncReply &operator =(AsyncReply &&) = delete;
 
     friend class AbstractClient;
 

+ 1 - 1
src/protobuf/QtProtobufGen.cmake.in

@@ -45,7 +45,7 @@ function(generate_qtprotobuf)
                 ${PROTO_FILES}
             WORKING_DIRECTORY ${OUT_DIR}
             DEPENDS ${PROTO_FILES} ${QTPROTOBUF_EXECUTABLE}
-            COMMENT "Generating QtProtobuf sources ${QTPROTOBUF_GENERATED_SOURCES} ${QTPROTOBUF_GENERATED_HEADERS}"
+            COMMENT "Generating QtProtobuf ${generate_qtprotobuf_TARGET} sources..."
     )
     
     add_custom_target(${GEN_TARGET} DEPENDS ${QTPROTOBUF_GENERATED_SOURCES} ${QTPROTOBUF_GENERATED_HEADERS} ${PROTO_FILES})

+ 6 - 4
src/protobuf/qprotobufobject_p.h

@@ -60,6 +60,8 @@ class QTPROTOBUFSHARED_EXPORT ProtobufObjectPrivate
     ProtobufObjectPrivate() = delete;
     ~ProtobufObjectPrivate() = delete;
     Q_DISABLE_COPY(ProtobufObjectPrivate)
+    ProtobufObjectPrivate(ProtobufObjectPrivate &&) = delete;
+    ProtobufObjectPrivate &operator =(ProtobufObjectPrivate &&) = delete;
 public:
     using Serializer = std::function<QByteArray(const QVariant &, int &)>;
     using Deserializer = std::function<void(SelfcheckIterator &, QVariant &)>;
@@ -75,7 +77,7 @@ public:
 
     template <typename T,
               typename std::enable_if_t<!std::is_base_of<QObject, T>::value, int> = 0>
-    static void wrapSerializer(std::function<QByteArray(const T &, int &)> s, std::function<QVariant(SelfcheckIterator &)> d, WireTypes type)
+    static void wrapSerializer(const std::function<QByteArray(const T &, int &)> &s, const std::function<QVariant(SelfcheckIterator &)> &d, WireTypes type)
     {
         serializers[qMetaTypeId<T>()] = {
             [s](const QVariant &value, int &fieldIndex) {
@@ -90,7 +92,7 @@ public:
 
     template <typename T,
               typename std::enable_if_t<!std::is_base_of<QObject, T>::value, int> = 0>
-    static void wrapSerializer(std::function<QByteArray(const T &, int &)> s, std::function<void(SelfcheckIterator &it, QVariant & value)> d, WireTypes type)
+    static void wrapSerializer(const std::function<QByteArray(const T &, int &)> &s, const std::function<void(SelfcheckIterator &it, QVariant & value)> &d, WireTypes type)
     {
         serializers[qMetaTypeId<T>()] = {
             [s](const QVariant &value, int &fieldIndex) {
@@ -103,7 +105,7 @@ public:
 
     template <typename T,
               typename std::enable_if_t<std::is_base_of<QObject, T>::value, int> = 0>
-    static void wrapSerializer(std::function<QByteArray(const T &, int &)> s, std::function<QVariant(SelfcheckIterator &)> d, WireTypes type)
+    static void wrapSerializer(const std::function<QByteArray(const T &, int &)> &s, const std::function<QVariant(SelfcheckIterator &)> &d, WireTypes type)
     {
         serializers[qMetaTypeId<T*>()] = {
             [s](const QVariant &value, int &fieldIndex) {
@@ -118,7 +120,7 @@ public:
 
     template <typename T,
               typename std::enable_if_t<std::is_base_of<QObject, T>::value, int> = 0>
-    static void wrapSerializer(std::function<QByteArray(const T &, int &)> s, std::function<void(SelfcheckIterator &it, QVariant &value)> d, WireTypes type)
+    static void wrapSerializer(const std::function<QByteArray(const T &, int &)> &s, const std::function<void(SelfcheckIterator &it, QVariant &value)> &d, WireTypes type)
     {
         serializers[qMetaTypeId<T*>()] = {
             [s](const QVariant &value, int &fieldIndex) {

+ 14 - 10
src/protobuf/selfcheckiterator.h

@@ -35,20 +35,24 @@ namespace qtprotobuf {
 class QTPROTOBUFSHARED_EXPORT SelfcheckIterator
 {
 public:
-    SelfcheckIterator(const QByteArray& container) : m_sizeLeft(container.size())
+    SelfcheckIterator(const QByteArray &container) : m_sizeLeft(container.size())
       , m_containerSize(container.size())
       , m_it(container.begin()){}
 
-    SelfcheckIterator(const SelfcheckIterator& other) : m_sizeLeft(other.m_sizeLeft)
-      ,m_containerSize(other.m_containerSize)
-      , m_it(other.m_it){}
+    SelfcheckIterator(const SelfcheckIterator &other) : m_sizeLeft(other.m_sizeLeft)
+      , m_containerSize(other.m_containerSize)
+      , m_it(other.m_it) {
+        if (m_sizeLeft >= m_containerSize) {
+            throw std::out_of_range("Container is less than required fields number. Deserialization failed");
+        }
+    }
 
     explicit operator QByteArray::const_iterator&() { return m_it; }
     explicit operator QByteArray::const_iterator() const { return m_it; }
 
     char operator *() { return *m_it; }
 
-    SelfcheckIterator& operator ++() {
+    SelfcheckIterator &operator ++() {
         --m_sizeLeft;
         if (m_sizeLeft < 0) {
             throw std::out_of_range("Container is less than required fields number. Deserialization failed");
@@ -57,7 +61,7 @@ public:
         return *this;
     }
 
-    SelfcheckIterator& operator --() {
+    SelfcheckIterator &operator --() {
         ++m_sizeLeft;
         if (m_sizeLeft >= m_containerSize) {
             throw std::out_of_range("Container is less than required fields number. Deserialization failed");
@@ -66,7 +70,7 @@ public:
         return *this;
     }
 
-    SelfcheckIterator& operator +=(int count) {
+    SelfcheckIterator &operator +=(int count) {
         m_sizeLeft -= count;
         if (m_sizeLeft < 0) {
             throw std::out_of_range("Container is less than required fields number. Deserialization failed");
@@ -75,7 +79,7 @@ public:
         return *this;
     }
 
-    SelfcheckIterator& operator -=(int count) {
+    SelfcheckIterator &operator -=(int count) {
         m_sizeLeft += count;
         if (m_sizeLeft >= m_containerSize) {
             throw std::out_of_range("Container is less than required fields number. Deserialization failed");
@@ -84,7 +88,7 @@ public:
         return *this;
     }
 
-    SelfcheckIterator& operator =(SelfcheckIterator& other) {
+    SelfcheckIterator &operator =(const SelfcheckIterator &other) {
         if (this == &other) {
             return *this;
         }
@@ -119,7 +123,7 @@ private:
     QByteArray::const_iterator m_it;
 };
 
-inline SelfcheckIterator operator +(const SelfcheckIterator& it, int lenght) {
+inline SelfcheckIterator operator +(const SelfcheckIterator &it, int lenght) {
     SelfcheckIterator itNew = it;
     return itNew += lenght;
 }

+ 1 - 3
tests/test_protobuf/deserializationtest.cpp

@@ -90,9 +90,7 @@ using namespace qtprotobufnamespace::tests;
 using namespace qtprotobuf::tests;
 using namespace qtprotobuf;
 
-DeserializationTest::DeserializationTest()
-{
-}
+DeserializationTest::DeserializationTest() = default;
 
 TEST_F(DeserializationTest, FixedInt32MessageDeserializeTest)
 {