From b9c8650e2f55f9f18b4ed2d38ee9bf9b65582f6f Mon Sep 17 00:00:00 2001 From: Andrii Verbytskyi <andrii.verbytskyi@mpp.mpg.de> Date: Tue, 7 Feb 2023 15:37:02 +0100 Subject: [PATCH] More clangtidy suggestions FedoraCI CentOSCI --- CMakeLists.txt | 20 ++++++++++---------- search/src/Selector.cc | 2 +- src/GenEvent.cc | 38 ++++++++++++++++++-------------------- src/GenRunInfo.cc | 2 +- src/ReaderPlugin.cc | 8 ++++---- src/WriterPlugin.cc | 8 ++++---- 6 files changed, 38 insertions(+), 40 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a5cdfad..953bdd18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ SET(HEPMC3_VERSION_MAJOR 3) SET(HEPMC3_VERSION_MINOR 2) SET(HEPMC3_VERSION_PATCH 6) -set(CMAKE_VERBOSE_MAKEFILE ON) +set(CMAKE_VERBOSE_MAKEFILE OFF) #This module respects HFS, e.g. defines lib or lib64 when it is needed. include("GNUInstallDirs") #---Setting HepMC3 Build type----------------------------------------------- @@ -39,16 +39,16 @@ option(HEPMC3_BUILD_STATIC_LIBS "Enables building of static libraties." ON) option(HEPMC3_ENABLE_CLANG_TIDY "Enables usage of clang-tidy." OFF) if (HEPMC3_ENABLE_CLANG_TIDY) set(ctor_1 "-cppcoreguidelines-pro-type-reinterpret-cast,-altera-id-dependent-backward-branch,-readability-function-cognitive-complexity,-fuchsia-overloaded-operator") - set(ctor_2 "-google-readability-casting,-cppcoreguidelines-pro-type-vararg,-hicpp-vararg,-altera-unroll-loops,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers") - set(ctor_3 "-fuchsia-default-arguments-calls,-cppcoreguidelines-pro-type-cstyle-cast,-llvmlibc*,-modernize-loop-convert,-llvm-else-after-return,-readability-else-after-return") - set(ctor_4 "-readability-identifier-length,-modernize-use-trailing-return-type,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory") + set(ctor_2 "-google-readability-casting,-cppcoreguidelines-pro-type-vararg,-hicpp-vararg,-altera-unroll-loops,-cppcoreguidelines-avoid-magic-numbers") + set(ctor_3 "-fuchsia-default-arguments-calls,-cppcoreguidelines-pro-type-cstyle-cast,-llvmlibc*,-modernize-loop-convert,-llvm-else-after-return") + set(ctor_4 "-readability-braces-around-statements,-readability-identifier-length,-modernize-use-trailing-return-type,-cppcoreguidelines-avoid-non-const-global-variables") set(ctor_5 "-bugprone-easily-swappable-parameters,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-readability-implicit-bool-conversion,-hicpp-braces-around-statements") - set(ctor_6 "-readability-braces-around-statements,-hicpp-use-emplace,-modernize-use-emplace,-google-default-arguments,-modernize-use-using,-readability-redundant-smartptr-get") - set(ctor_7 "-performance-unnecessary-value-param,-performance-for-range-copy,-google-runtime-int,-modernize-pass-by-value,-misc-no-recursion,-clang-analyzer-core.NonNullParamChecker") - set(ctor_8 "-cppcoreguidelines-pro-type-member-init,-hicpp-member-init,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-hicpp-use-auto,-modernize-use-auto") - set(ctor_9 "-cppcoreguidelines-prefer-member-initializer,-concurrency-mt-unsafe,-readability-convert-member-functions-to-static,-bugprone-implicit-widening-of-multiplication-result") - set(ctor_10 "-clang-analyzer-optin.cplusplus.VirtualCall,-cert-err33-c,-cert-err34-c,-cert-err58-cpp,-fuchsia-statically-constructed-objects") - set(ctor_11 "-readability-delete-null-pointer,-cppcoreguidelines-init-variables,-clang-analyzer-core.CallAndMessage,-altera-struct-pack-align") + set(ctor_6 "-google-default-arguments,-readability-redundant-smartptr-get,-cppcoreguidelines-owning-memory,-llvm-include-order,-readability-magic-numbers") + set(ctor_7 "-performance-unnecessary-value-param,-performance-for-range-copy,-google-runtime-int,-modernize-pass-by-value,-readability-else-after-return") + set(ctor_8 "-cppcoreguidelines-pro-type-member-init,-hicpp-member-init,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions") + set(ctor_9 "-cppcoreguidelines-prefer-member-initializer,-concurrency-mt-unsafe,-readability-convert-member-functions-to-static,-clang-analyzer-core.NonNullParamChecker") + set(ctor_10 "-clang-analyzer-optin.cplusplus.VirtualCall,-cert-err33-c,-cert-err34-c,-cert-err58-cpp,-fuchsia-statically-constructed-objects,-misc-no-recursion") + set(ctor_11 "-readability-delete-null-pointer,-clang-analyzer-core.CallAndMessage,-altera-struct-pack-align,-bugprone-implicit-widening-of-multiplication-result") set(CMAKE_CXX_CLANG_TIDY "clang-tidy;--checks=*,${ctor_1},${ctor_2},${ctor_3},${ctor_4},${ctor_5},${ctor_6},${ctor_7},${ctor_8},${ctor_9},${ctor_10},${ctor_11}") endif() diff --git a/search/src/Selector.cc b/search/src/Selector.cc index cdcafca0..7689b7eb 100644 --- a/search/src/Selector.cc +++ b/search/src/Selector.cc @@ -25,6 +25,6 @@ ConstSelectorPtr abs(const Selector &input) return input.abs(); } -AttributeFeature Selector::ATTRIBUTE(const std::string &name) {return AttributeFeature(name);} +AttributeFeature Selector::ATTRIBUTE(const std::string &name) {return {name};} } // namespace HepMC3 diff --git a/src/GenEvent.cc b/src/GenEvent.cc index 4e1bfa4c..2ddf4c31 100644 --- a/src/GenEvent.cc +++ b/src/GenEvent.cc @@ -73,11 +73,11 @@ GenEvent::GenEvent(const GenEvent&e) { } GenEvent::~GenEvent() { - for ( std::map< std::string, std::map<int, std::shared_ptr<Attribute> > >::iterator attm = m_attributes.begin(); attm != m_attributes.end(); ++attm) { - for ( std::map<int, std::shared_ptr<Attribute> >::iterator att = attm->second.begin(); att != attm->second.end(); ++att) { if (att->second) att->second->m_event = nullptr;} + for ( auto attm = m_attributes.begin(); attm != m_attributes.end(); ++attm) { + for ( auto att = attm->second.begin(); att != attm->second.end(); ++att) { if (att->second) att->second->m_event = nullptr;} } - for ( std::vector<GenVertexPtr>::iterator v = m_vertices.begin(); v != m_vertices.end(); ++v ) if (*v) if ((*v)->m_event == this) (*v)->m_event = nullptr; - for ( std::vector<GenParticlePtr>::iterator p = m_particles.begin(); p != m_particles.end(); ++p ) if (*p) if ((*p)->m_event == this) (*p)->m_event = nullptr; + for ( auto v = m_vertices.begin(); v != m_vertices.end(); ++v ) if (*v) if ((*v)->m_event == this) (*v)->m_event = nullptr; + for ( auto p = m_particles.begin(); p != m_particles.end(); ++p ) if (*p) if ((*p)->m_event == this) (*p)->m_event = nullptr; } GenEvent& GenEvent::operator=(const GenEvent& e) { @@ -137,12 +137,12 @@ void GenEvent::remove_particle(GenParticlePtr p) { HEPMC3_DEBUG(30, "GenEvent::remove_particle - erasing particle: " << p->id()) int idx = p->id(); - std::vector<GenParticlePtr>::iterator it = m_particles.erase(m_particles.begin() + idx-1); + auto it = m_particles.erase(m_particles.begin() + idx-1); // Remove attributes of this particle std::lock_guard<std::recursive_mutex> lock(m_lock_attributes); for (att_key_t& vt1: m_attributes) { - std::map<int, std::shared_ptr<Attribute> >::iterator vt2 = vt1.second.find(idx); + auto vt2 = vt1.second.find(idx); if (vt2 == vt1.second.end()) continue; vt1.second.erase(vt2); } @@ -155,9 +155,9 @@ void GenEvent::remove_particle(GenParticlePtr p) { for (att_key_t& vt1: m_attributes) { changed_attributes.clear(); - for (std::map<int, std::shared_ptr<Attribute> >::iterator vt2 = vt1.second.begin(); vt2 != vt1.second.end(); ++vt2) { + for (auto vt2 = vt1.second.begin(); vt2 != vt1.second.end(); ++vt2) { if ( (*vt2).first > p->id() ) { - changed_attributes.push_back(*vt2); + changed_attributes.emplace_back(*vt2); } } @@ -186,7 +186,7 @@ struct sort_by_id_asc { void GenEvent::remove_particles(std::vector<GenParticlePtr> v) { std::sort(v.begin(), v.end(), sort_by_id_asc()); - for (std::vector<GenParticlePtr>::iterator p = v.begin(); p != v.end(); ++p) { + for (auto p = v.begin(); p != v.end(); ++p) { remove_particle(*p); } } @@ -212,11 +212,11 @@ void GenEvent::remove_vertex(GenVertexPtr v) { HEPMC3_DEBUG(30, "GenEvent::remove_vertex - erasing vertex: " << v->id()) int idx = -v->id(); - std::vector<GenVertexPtr>::iterator it = m_vertices.erase(m_vertices.begin() + idx-1); + auto it = m_vertices.erase(m_vertices.begin() + idx-1); // Remove attributes of this vertex std::lock_guard<std::recursive_mutex> lock(m_lock_attributes); for (att_key_t& vt1: m_attributes) { - std::map<int, std::shared_ptr<Attribute> >::iterator vt2 = vt1.second.find(-idx); + auto vt2 = vt1.second.find(-idx); if (vt2 == vt1.second.end()) continue; vt1.second.erase(vt2); } @@ -230,9 +230,9 @@ void GenEvent::remove_vertex(GenVertexPtr v) { for ( att_key_t& vt1: m_attributes ) { changed_attributes.clear(); - for (std::map<int, std::shared_ptr<Attribute> >::iterator vt2 = vt1.second.begin(); vt2 != vt1.second.end(); ++vt2) { + for (auto vt2 = vt1.second.begin(); vt2 != vt1.second.end(); ++vt2) { if ( (*vt2).first < v->id() ) { - changed_attributes.push_back(*vt2); + changed_attributes.emplace_back(*vt2); } } @@ -362,7 +362,7 @@ void GenEvent::add_tree(const std::vector<GenParticlePtr> &parts) { std::vector< std::pair< int, std::shared_ptr<Attribute> > > changed_attributes; for ( auto vt2 : vt1.second ) { if ( vt2.first <= rootid ) { - changed_attributes.push_back(vt2); + changed_attributes.emplace_back(vt2); } } for ( auto val : changed_attributes ) { @@ -612,11 +612,10 @@ void GenEvent::clear() { void GenEvent::remove_attribute(const std::string &name, const int& id) { std::lock_guard<std::recursive_mutex> lock(m_lock_attributes); - std:: map< std::string, std::map<int, std::shared_ptr<Attribute> > >::iterator i1 = - m_attributes.find(name); + auto i1 = m_attributes.find(name); if ( i1 == m_attributes.end() ) return; - std::map<int, std::shared_ptr<Attribute> >::iterator i2 = i1->second.find(id); + auto i2 = i1->second.find(id); if ( i2 == i1->second.end() ) return; i1->second.erase(i2); @@ -786,8 +785,7 @@ void GenEvent::add_beam_particle(GenParticlePtr p1) { std::string GenEvent::attribute_as_string(const std::string &name, const int& id) const { std::lock_guard<std::recursive_mutex> lock(m_lock_attributes); - std::map< std::string, std::map<int, std::shared_ptr<Attribute> > >::iterator i1 = - m_attributes.find(name); + auto i1 = m_attributes.find(name); if ( i1 == m_attributes.end() ) { if ( id == 0 && run_info() ) { return run_info()->attribute_as_string(name); @@ -795,7 +793,7 @@ std::string GenEvent::attribute_as_string(const std::string &name, const int& id return {}; } - std::map<int, std::shared_ptr<Attribute> >::iterator i2 = i1->second.find(id); + auto i2 = i1->second.find(id); if (i2 == i1->second.end() ) return {}; if ( !i2->second ) return {}; diff --git a/src/GenRunInfo.cc b/src/GenRunInfo.cc index e0cf5f4f..22b42e45 100644 --- a/src/GenRunInfo.cc +++ b/src/GenRunInfo.cc @@ -39,7 +39,7 @@ void GenRunInfo::set_weight_names(const std::vector<std::string> & names) { std::string GenRunInfo::attribute_as_string(const std::string &name) const { std::lock_guard<std::recursive_mutex> lock(m_lock_attributes); - std::map< std::string, std::shared_ptr<Attribute> >::iterator i = m_attributes.find(name); + auto i = m_attributes.find(name); if ( i == m_attributes.end() ) return {}; if ( !i->second ) return {}; diff --git a/src/ReaderPlugin.cc b/src/ReaderPlugin.cc index 89eb82c7..8a70c6a1 100644 --- a/src/ReaderPlugin.cc +++ b/src/ReaderPlugin.cc @@ -38,8 +38,8 @@ ReaderPlugin::ReaderPlugin(std::istream & stream, const std::string &libname, co #if defined(__linux__) || defined(__darwin__) || defined(__APPLE__) || defined(BSD) || defined(__sun) dll_handle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL); if (!dll_handle) { printf("Error while loading library %s: %s\n", libname.c_str(), dlerror()); m_reader = nullptr; return; } - Reader* (*newReader)(std::istream & stream); - newReader = (Reader* (*)(std::istream & stream))dlsym(dll_handle, newreader.c_str()); + using f_funci = Reader *(*)(std::istream &); + auto newReader = (f_funci)dlsym(dll_handle, newreader.c_str()); if (!newReader) { printf("Error while loading function %s from library %s: %s\n", newreader.c_str(), libname.c_str(), dlerror()); m_reader = nullptr; return; } m_reader = (Reader*)(newReader(stream)); #endif @@ -58,8 +58,8 @@ ReaderPlugin::ReaderPlugin(const std::string& filename, const std::string &libna #if defined(__linux__) || defined(__darwin__) || defined(__APPLE__) || defined(BSD) || defined(__sun) dll_handle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL); if (!dll_handle) { printf("Error while loading library %s: %s\n", libname.c_str(), dlerror()); m_reader = nullptr; return; } - Reader* (*newReader)(const std::string&); - newReader = (Reader* (*)(const std::string&))dlsym(dll_handle, newreader.c_str()); + using f_funci = Reader *(*)(const std::string&); + auto newReader = (f_funci)dlsym(dll_handle, newreader.c_str()); if (!newReader) { printf("Error while loading function %s from library %s: %s\n", newreader.c_str(), libname.c_str(), dlerror()); m_reader = nullptr; return; } m_reader = (Reader*)(newReader(filename)); #endif diff --git a/src/WriterPlugin.cc b/src/WriterPlugin.cc index 386bca14..6b70069f 100644 --- a/src/WriterPlugin.cc +++ b/src/WriterPlugin.cc @@ -40,8 +40,8 @@ WriterPlugin::WriterPlugin(std::ostream & stream, const std::string &libname, co #if defined(__linux__) || defined(__darwin__) || defined(__APPLE__) || defined(BSD) || defined(__sun) dll_handle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL); if (!dll_handle) { printf("Error while loading library %s: %s\n", libname.c_str(), dlerror()); m_writer=nullptr; return; } - Writer* (*newWriter)(std::ostream & stream, std::shared_ptr<GenRunInfo>); - newWriter = (Writer* (*)(std::ostream & stream, std::shared_ptr<GenRunInfo>))dlsym(dll_handle, newwriter.c_str()); + using f_funci = Writer* (*)(std::ostream & stream, std::shared_ptr<GenRunInfo>); + auto newWriter = (f_funci)dlsym(dll_handle, newwriter.c_str()); if (!newWriter) { printf("Error while loading function %s from library %s: %s\n", newwriter.c_str(), libname.c_str(), dlerror()); m_writer = nullptr; return; } m_writer = (Writer*)(newWriter(stream, run)); #endif @@ -60,8 +60,8 @@ WriterPlugin::WriterPlugin(const std::string& filename, const std::string &libna #if defined(__linux__) || defined(__darwin__) || defined(__APPLE__) || defined(BSD) || defined(__sun) dll_handle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL); if (!dll_handle) { printf("Error while loading library %s: %s\n", libname.c_str(), dlerror()); m_writer = nullptr; return; } - Writer* (*newWriter)(const std::string&, std::shared_ptr<GenRunInfo>); - newWriter = (Writer* (*)(const std::string&, std::shared_ptr<GenRunInfo>))dlsym(dll_handle, newwriter.c_str()); + using f_funci = Writer* (*)(const std::string&, shared_ptr<GenRunInfo>); + auto newWriter = (f_funci)dlsym(dll_handle, newwriter.c_str()); if (!newWriter) { printf("Error while loading function %s from library %s: %s\n", newwriter.c_str(), libname.c_str(), dlerror()); m_writer = nullptr; return; } m_writer = (Writer*)(newWriter(filename, run)); #endif -- GitLab