From 29b4752d0ea4c793a55d4e08dcb9d6914405ddd1 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Thu, 26 Oct 2023 00:28:36 +0200 Subject: [PATCH] src: add some noexcept --- misc/analyze/clang-tidy/run-clang-tidy.py | 64 +++++++++++++++++------ src/util/xspan_impl_common.h | 28 +++++----- src/util/xspan_impl_ptr.h | 8 +-- src/util/xspan_impl_ptr_or_null.h | 4 ++ 4 files changed, 70 insertions(+), 34 deletions(-) diff --git a/misc/analyze/clang-tidy/run-clang-tidy.py b/misc/analyze/clang-tidy/run-clang-tidy.py index cebcbf74..79056a62 100755 --- a/misc/analyze/clang-tidy/run-clang-tidy.py +++ b/misc/analyze/clang-tidy/run-clang-tidy.py @@ -308,10 +308,21 @@ def main(): if yaml: parser.add_argument( "-export-fixes", - metavar="filename", + metavar="file_or_directory", dest="export_fixes", - help="Create a yaml file to store suggested fixes in, " - "which can be applied with clang-apply-replacements.", + help="A directory or a yaml file to store suggested fixes in, " + "which can be applied with clang-apply-replacements. If the " + "parameter is a directory, the fixes of each compilation unit are " + "stored in individual yaml files in the directory.", + ) + else: + parser.add_argument( + "-export-fixes", + metavar="directory", + dest="export_fixes", + help="A directory to store suggested fixes in, which can be applied " + "with clang-apply-replacements. The fixes of each compilation unit are " + "stored in individual yaml files in the directory.", ) parser.add_argument( "-j", @@ -384,14 +395,35 @@ def main(): clang_tidy_binary = find_binary(args.clang_tidy_binary, "clang-tidy", build_path) - tmpdir = None if args.fix: clang_apply_replacements_binary = find_binary( args.clang_apply_replacements_binary, "clang-apply-replacements", build_path ) - if args.fix or (yaml and args.export_fixes): - tmpdir = tempfile.mkdtemp() + combine_fixes = False + export_fixes_dir = None + delete_fixes_dir = False + if args.export_fixes is not None: + # if a directory is given, create it if it does not exist + if args.export_fixes.endswith(os.path.sep) and not os.path.isdir( + args.export_fixes + ): + os.makedirs(args.export_fixes) + + if not os.path.isdir(args.export_fixes): + if not yaml: + raise RuntimeError( + "Cannot combine fixes in one yaml file. Either install PyYAML or specify an output directory." + ) + + combine_fixes = True + + if os.path.isdir(args.export_fixes): + export_fixes_dir = args.export_fixes + + if export_fixes_dir is None and (args.fix or combine_fixes): + export_fixes_dir = tempfile.mkdtemp() + delete_fixes_dir = True # Load the database and extract all files. database = json.load(open(os.path.join(build_path, db_path))) @@ -410,8 +442,8 @@ def main(): matched_files.append(name) files = sorted(matched_files) if not files: - if tmpdir: - shutil.rmtree(tmpdir) + if delete_fixes_dir: + shutil.rmtree(export_fixes_dir) sys.exit(0) try: @@ -462,7 +494,7 @@ def main(): args=( args, clang_tidy_binary, - tmpdir, + export_fixes_dir, build_path, task_queue, lock, @@ -486,14 +518,14 @@ def main(): # This is a sad hack. Unfortunately subprocess goes # bonkers with ctrl-c and we start forking merrily. print("\nCtrl-C detected, goodbye.") - if tmpdir: - shutil.rmtree(tmpdir) + if delete_fixes_dir: + shutil.rmtree(export_fixes_dir) os.kill(0, 9) - if yaml and args.export_fixes: + if combine_fixes: print("Writing fixes to " + args.export_fixes + " ...") try: - merge_replacement_files(tmpdir, args.export_fixes) + merge_replacement_files(export_fixes_dir, args.export_fixes) except: print("Error exporting fixes.\n", file=sys.stderr) traceback.print_exc() @@ -502,14 +534,14 @@ def main(): if args.fix: print("Applying fixes ...") try: - apply_fixes(args, clang_apply_replacements_binary, tmpdir) + apply_fixes(args, clang_apply_replacements_binary, export_fixes_dir) except: print("Error applying fixes.\n", file=sys.stderr) traceback.print_exc() return_code = 1 - if tmpdir: - shutil.rmtree(tmpdir) + if delete_fixes_dir: + shutil.rmtree(export_fixes_dir) sys.exit(return_code) diff --git a/src/util/xspan_impl_common.h b/src/util/xspan_impl_common.h index aff1f194..b29bb7e1 100644 --- a/src/util/xspan_impl_common.h +++ b/src/util/xspan_impl_common.h @@ -69,7 +69,7 @@ private: xspan_check_range(ptr, base, size_in_bytes); } #else -forceinline_constexpr void assertInvariants() const noexcept {} +forceinline void assertInvariants() const noexcept {} #endif static inline pointer makeNotNull(pointer p) { @@ -288,49 +288,49 @@ public: reinterpret_cast(base)); } - bool operator==(pointer other) const { return ptr == other; } + bool operator==(pointer other) const noexcept { return ptr == other; } template XSPAN_REQUIRES_CONVERTIBLE_R(bool) - operator==(U *other) const { + operator==(U *other) const noexcept { return ptr == other; } - bool operator!=(pointer other) const { return ptr != other; } + bool operator!=(pointer other) const noexcept { return ptr != other; } template XSPAN_REQUIRES_CONVERTIBLE_R(bool) - operator!=(U *other) const { + operator!=(U *other) const noexcept { return ptr != other; } template XSPAN_REQUIRES_CONVERTIBLE_R(bool) - operator==(const PtrOrSpan &other) const { + operator==(const PtrOrSpan &other) const noexcept { return ptr == other.ptr; } template XSPAN_REQUIRES_CONVERTIBLE_R(bool) - operator==(const PtrOrSpanOrNull &other) const { + operator==(const PtrOrSpanOrNull &other) const noexcept { return ptr == other.ptr; } template XSPAN_REQUIRES_CONVERTIBLE_R(bool) - operator==(const Span &other) const { + operator==(const Span &other) const noexcept { return ptr == other.ptr; } template XSPAN_REQUIRES_CONVERTIBLE_R(bool) - operator!=(const PtrOrSpan &other) const { - return !(*this == other); + operator!=(const PtrOrSpan &other) const noexcept { + return ptr != other.ptr; } template XSPAN_REQUIRES_CONVERTIBLE_R(bool) - operator!=(const PtrOrSpanOrNull &other) const { - return !(*this == other); + operator!=(const PtrOrSpanOrNull &other) const noexcept { + return ptr != other.ptr; } template XSPAN_REQUIRES_CONVERTIBLE_R(bool) - operator!=(const Span &other) const { - return !(*this == other); + operator!=(const Span &other) const noexcept { + return ptr != other.ptr; } // check for notNull here diff --git a/src/util/xspan_impl_ptr.h b/src/util/xspan_impl_ptr.h index b3ce3ada..0a6bd89c 100644 --- a/src/util/xspan_impl_ptr.h +++ b/src/util/xspan_impl_ptr.h @@ -55,7 +55,7 @@ private: // inverse logic for ensuring valid pointers from existing objects inline pointer ensurePtr() const { return ptr; } // debug - forceinline_constexpr void assertInvariants() const noexcept {} + forceinline void assertInvariants() const noexcept {} public: #if XSPAN_CONFIG_ENABLE_IMPLICIT_CONVERSION || 1 @@ -132,15 +132,15 @@ public: // comparison - bool operator==(pointer other) const { return ptr == other; } + bool operator==(pointer other) const noexcept { return ptr == other; } template XSPAN_REQUIRES_CONVERTIBLE_R(bool) - operator==(U *other) const { + operator==(U *other) const noexcept { return ptr == other; } template XSPAN_REQUIRES_CONVERTIBLE_R(bool) - operator==(const Ptr &other) const { + operator==(const Ptr &other) const noexcept { return ptr == other.ptr; } diff --git a/src/util/xspan_impl_ptr_or_null.h b/src/util/xspan_impl_ptr_or_null.h index 7e8a82a8..558bcce5 100644 --- a/src/util/xspan_impl_ptr_or_null.h +++ b/src/util/xspan_impl_ptr_or_null.h @@ -87,6 +87,10 @@ public: // nullptr forceinline CSelf(std::nullptr_t) noexcept : ptr(nullptr), base(nullptr), size_in_bytes(0) {} + forceinline Self &operator=(std::nullptr_t) noexcept { + ptr = nullptr; + return *this; + } #undef CSelf };