From 26d77bd8c1f0dde8d3a41d0fb77ea5f7f5ff33b3 Mon Sep 17 00:00:00 2001 From: Nuri Jung Date: Fri, 18 Oct 2024 10:12:14 +0900 Subject: [PATCH 1/5] chore(dev): update clang tools config --- .clangd | 2 +- test/.clang-tidy | 1 + test/.clangd | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.clangd b/.clangd index 536e06ac..6d5e0821 100644 --- a/.clangd +++ b/.clangd @@ -46,7 +46,7 @@ InlayHints: DeducedTypes: Yes --- If: - PathMatch: "third_party/.*" + PathMatch: "third-party/.*" Diagnostics: Suppress: "*" UnusedIncludes: None diff --git a/test/.clang-tidy b/test/.clang-tidy index 00cf7f50..541346c9 100644 --- a/test/.clang-tidy +++ b/test/.clang-tidy @@ -7,3 +7,4 @@ InheritParentConfig: true ExtraArgsBefore: - -Wno-gnu-zero-variadic-macro-arguments - -Wno-unused-member-function + - -Wno-c++20-extensions diff --git a/test/.clangd b/test/.clangd index ddae5981..6bbae244 100644 --- a/test/.clangd +++ b/test/.clangd @@ -5,3 +5,4 @@ CompileFlags: Add: - -Wno-gnu-zero-variadic-macro-arguments + - -Wno-c++20-extensions From 3cdf8149cad552ec1ce2050bd4902f25e4722991 Mon Sep 17 00:00:00 2001 From: Nuri Jung Date: Fri, 18 Oct 2024 10:29:33 +0900 Subject: [PATCH 2/5] refactor: remove redundant specifiers --- include/nuri/core/element.h | 14 +++++--------- include/nuri/core/molecule.h | 2 +- include/nuri/utils.h | 24 ++++++++++++------------ 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/include/nuri/core/element.h b/include/nuri/core/element.h index 7ee6376f..5842e725 100644 --- a/include/nuri/core/element.h +++ b/include/nuri/core/element.h @@ -31,13 +31,11 @@ struct Isotope { double abundance; }; -constexpr inline bool operator==(const Isotope &lhs, - const Isotope &rhs) noexcept { +constexpr bool operator==(const Isotope &lhs, const Isotope &rhs) noexcept { return &lhs == &rhs; } -constexpr inline bool operator!=(const Isotope &lhs, - const Isotope &rhs) noexcept { +constexpr bool operator!=(const Isotope &lhs, const Isotope &rhs) noexcept { return &lhs != &rhs; } @@ -360,13 +358,11 @@ class Element { std::vector isotopes_; }; -constexpr inline bool operator==(const Element &lhs, - const Element &rhs) noexcept { +constexpr bool operator==(const Element &lhs, const Element &rhs) noexcept { return lhs.atomic_number() == rhs.atomic_number(); } -constexpr inline bool operator!=(const Element &lhs, - const Element &rhs) noexcept { +constexpr bool operator!=(const Element &lhs, const Element &rhs) noexcept { return lhs.atomic_number() != rhs.atomic_number(); } @@ -488,7 +484,7 @@ class PeriodicTable final { // 118 elements + dummy // NOLINTNEXTLINE(readability-identifier-naming) - constexpr inline static int kElementCount_ = 118 + 1; + constexpr static int kElementCount_ = 118 + 1; private: PeriodicTable() noexcept; diff --git a/include/nuri/core/molecule.h b/include/nuri/core/molecule.h index 008061e2..7ae863c3 100644 --- a/include/nuri/core/molecule.h +++ b/include/nuri/core/molecule.h @@ -2327,7 +2327,7 @@ namespace internal { * @param atom An atom. * @return Number of all neighbors of the atom, including implicit hydrogens. */ -extern inline int all_neighbors(Molecule::Atom atom) { +inline int all_neighbors(Molecule::Atom atom) { return atom.degree() + atom.data().implicit_hydrogens(); } diff --git a/include/nuri/utils.h b/include/nuri/utils.h index e1103481..892d0090 100644 --- a/include/nuri/utils.h +++ b/include/nuri/utils.h @@ -566,43 +566,43 @@ constexpr auto make_zipped_iterator(Iters... iters) { } template , U = 0> -constexpr inline E operator|(E lhs, E rhs) { +constexpr E operator|(E lhs, E rhs) { return static_cast(static_cast(lhs) | static_cast(rhs)); } template , U = 0> -constexpr inline E &operator|=(E &self, E rhs) { +constexpr E &operator|=(E &self, E rhs) { return self = self | rhs; } template , U = 0> -constexpr inline E operator&(E lhs, E rhs) { +constexpr E operator&(E lhs, E rhs) { return static_cast(static_cast(lhs) & static_cast(rhs)); } template , U = 0> -constexpr inline E &operator&=(E &self, E rhs) { +constexpr E &operator&=(E &self, E rhs) { return self = self & rhs; } template , U = 0> -constexpr inline E operator^(E lhs, E rhs) { +constexpr E operator^(E lhs, E rhs) { return static_cast(static_cast(lhs) ^ static_cast(rhs)); } template , U = 0> -constexpr inline E &operator^=(E &self, E rhs) { +constexpr E &operator^=(E &self, E rhs) { return self = self ^ rhs; } template , U = 0> -constexpr inline E operator~(E val) { +constexpr E operator~(E val) { return static_cast(~static_cast(val)); } template , U = 0, std::enable_if_t, int> = 0> -constexpr inline E operator-(E val) { +constexpr E operator-(E val) { return static_cast(-static_cast(val)); } @@ -699,7 +699,7 @@ namespace internal { set_name(props, std::string(name)); } - constexpr inline int negate_if_false(bool cond) { + constexpr int negate_if_false(bool cond) { int ret = (static_cast(cond) << 1) - 1; ABSL_ASSUME(ret == 1 || ret == -1); return ret; @@ -854,8 +854,8 @@ inline std::string_view extension_no_dot(const std::filesystem::path &ext) { return ext_view; } -constexpr inline std::string_view slice(std::string_view str, std::size_t begin, - std::size_t end) { +constexpr std::string_view slice(std::string_view str, std::size_t begin, + std::size_t end) { return str.substr(begin, end - begin); } @@ -915,7 +915,7 @@ inline Matrix3Xd stack(const std::vector &vs) { return m; } -constexpr inline int value_if(bool cond, int val = 1) { +constexpr int value_if(bool cond, int val = 1) { return static_cast(cond) * val; } From c9fbf9e0878ef16fdc69b5daaffbff9c3e8308e9 Mon Sep 17 00:00:00 2001 From: Nuri Jung Date: Fri, 18 Oct 2024 10:47:19 +0900 Subject: [PATCH 3/5] feat(meta): add macro NURI_CLANG_ANALYZER_NOLINT* --- include/nuri/meta.h | 10 ++++++++++ include/nuri/utils.h | 11 ----------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/include/nuri/meta.h b/include/nuri/meta.h index 69c5f1a3..7043944d 100644 --- a/include/nuri/meta.h +++ b/include/nuri/meta.h @@ -11,6 +11,16 @@ #include /// @endcond +#if defined(__clang_analyzer__) && __clang_major__ >= 18 +#define NURI_CLANG_ANALYZER_NOLINT [[clang::suppress]] +#define NURI_CLANG_ANALYZER_NOLINT_BEGIN [[clang::suppress]] { +#define NURI_CLANG_ANALYZER_NOLINT_END } +#else +#define NURI_CLANG_ANALYZER_NOLINT +#define NURI_CLANG_ANALYZER_NOLINT_BEGIN +#define NURI_CLANG_ANALYZER_NOLINT_END +#endif + namespace nuri { namespace internal { // Use of std::underlying_type_t on non-enum types is UB until C++20. diff --git a/include/nuri/utils.h b/include/nuri/utils.h index 892d0090..b59d2b32 100644 --- a/include/nuri/utils.h +++ b/include/nuri/utils.h @@ -37,17 +37,6 @@ #include "nuri/eigen_config.h" #include "nuri/meta.h" -// Introduced in clang 18 -// #ifdef __clang_analyzer__ -// #define NURI_CLANG_ANALYZER_NOLINT [[clang::suppress]] -// #define NURI_CLANG_ANALYZER_NOLINT_BEGIN [[clang::suppress]] { -// #define NURI_CLANG_ANALYZER_NOLINT_END } -// #else -// #define NURI_CLANG_ANALYZER_NOLINT -// #define NURI_CLANG_ANALYZER_NOLINT_BEGIN -// #define NURI_CLANG_ANALYZER_NOLINT_END -// #endif - namespace nuri { template , int> = 0> constexpr T min(T a, T b) { From 9ea04aedc726cd2ffd20b10b6dc885816d3b592f Mon Sep 17 00:00:00 2001 From: Nuri Jung Date: Fri, 18 Oct 2024 10:47:47 +0900 Subject: [PATCH 4/5] refactor(fmt/sdf): use NURI_CLANG_ANALYZER_NOLINT attribute macro --- src/fmt/sdf.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/fmt/sdf.cpp b/src/fmt/sdf.cpp index 55b3ca9f..3e440521 100644 --- a/src/fmt/sdf.cpp +++ b/src/fmt/sdf.cpp @@ -41,6 +41,7 @@ #include "nuri/core/element.h" #include "nuri/core/molecule.h" #include "nuri/fmt/base.h" +#include "nuri/meta.h" #include "nuri/utils.h" namespace nuri { @@ -83,11 +84,7 @@ struct HeaderReadResult { private: // Clang analyzer complains about uninitialized members -#ifdef __clang_analyzer__ - HeaderReadResult(): version_(-1), natoms_(0), nbonds_(0) { } -#else - HeaderReadResult(): version_(-1) { } -#endif + NURI_CLANG_ANALYZER_NOLINT HeaderReadResult(): version_(-1) { } HeaderReadResult(int v, int a, int b): version_(v), natoms_(a), nbonds_(b) { } From db5fc7c2ec2f4623111a8c17f38a033fc9cb1916 Mon Sep 17 00:00:00 2001 From: Nuri Jung Date: Fri, 18 Oct 2024 10:48:19 +0900 Subject: [PATCH 5/5] refactor(fmt/sdf): cast inside factory --- src/fmt/sdf.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/fmt/sdf.cpp b/src/fmt/sdf.cpp index 3e440521..aa177726 100644 --- a/src/fmt/sdf.cpp +++ b/src/fmt/sdf.cpp @@ -68,10 +68,12 @@ namespace x3 = boost::spirit::x3; using Iterator = std::vector::const_iterator; struct HeaderReadResult { - static HeaderReadResult failure() { return HeaderReadResult(); } + static HeaderReadResult failure() { return {}; } - static HeaderReadResult success(int version, int natoms, int nbonds) { - return HeaderReadResult(version, natoms, nbonds); + template + static HeaderReadResult success(N1 version, N2 natoms, N3 nbonds) { + return { static_cast(version), static_cast(natoms), + static_cast(nbonds) }; } int version() const { return version_; } @@ -661,8 +663,7 @@ bool try_read_v3000_header(HeaderReadResult &metadata, Iterator &it, ABSL_DCHECK(counts.size() >= 2); - metadata = HeaderReadResult::success(3000, static_cast(counts[0]), - static_cast(counts[1])); + metadata = HeaderReadResult::success(3000, counts[0], counts[1]); return true; }