From patchwork Mon Jun 15 08:26:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Matthias_M=C3=A4nnich?= X-Patchwork-Id: 39606 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 422463851C20; Mon, 15 Jun 2020 08:26:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 422463851C20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1592209590; bh=dw30bl5idSs2y/xKS1+droH330H2hOn9mTGwryIfCbA=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Help: List-Subscribe:From:Reply-To:Cc:From; b=JNVqqlTSnhB0XOFFIUr7vWL2iIFvlxLQ2I94fzQ56lssUihLvQog/t8VaPgE3kW5r yXyfb7/76rTn87t6uv3tuF87gVc9QGLKwzyDkPt9WuFLFTskYnkdXllC7oiprOGLqe ffHR+ZqXobJYCjhjwyb3CgtoimJMsZwfoiq6gHgc= X-Original-To: libabigail@sourceware.org Delivered-To: libabigail@sourceware.org Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by sourceware.org (Postfix) with ESMTPS id 734013851C20 for ; Mon, 15 Jun 2020 08:26:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 734013851C20 Received: by mail-yb1-xb4a.google.com with SMTP id k186so19572768ybc.19 for ; Mon, 15 Jun 2020 01:26:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=dw30bl5idSs2y/xKS1+droH330H2hOn9mTGwryIfCbA=; b=oOylVxJXl0v4VUkhL3NCF1j7KwoQvhtEjQ2jDk8jboXhCaPILPNtuR0FawSKUR3LS3 lTBHTFzmVQtupJbz+OjlDDPbOR1t2Z5pSzWSOLl1HGe67L4kAZV8MpK+kArM/yqZYCIf K8o80iWgtdu8M3RcvMcVDv0j9fO+BZIm2IXkK3rGUOZMEPyBf8hIMtUvAMWY0Vs7P3N0 e2BSkFyk1r2+uv1V6EPoQdy0Ps0dMvHLTs19Jsq76aJQsIDxydRH/Xujy1+EweVLDQhH 4VIYesVUJ6teNEGzsbWgOR2qZrDNLdS+8CtAf5l3n/tn+CfjeU7qoX3BcL0Jdz5VdyWF VEBw== X-Gm-Message-State: AOAM533cDQ0E7DxAGK7M+H7/199Lo03liTcM27so3swT2Gq2xkHPiBDs YUiLq0qZZaXPeQIvuQkxLDinfA8iPk2o6dio85Di2v94Xn7BJ2vlwGvFLTIe7h8tt2PN/yL0Hkr AECV/G7/hDjDDVdljFgsB6dMXuL/Q3g6MbreNvsSAluwRhNvI1u8PYxXx6ym9poL9Rn+Gjcg= X-Google-Smtp-Source: ABdhPJzA0FR+Ro2PrTT6JveBB8bhAybGgwWz6JD6IHovQTGnzwcLe8zSBVM3MmhsSbNdeYG4shRoUZIaVIk8vQ== X-Received: by 2002:a25:da8f:: with SMTP id n137mr39022635ybf.298.1592209586809; Mon, 15 Jun 2020 01:26:26 -0700 (PDT) Date: Mon, 15 Jun 2020 10:26:14 +0200 Message-Id: <20200615082614.257387-1-maennich@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.27.0.290.gba653c62da-goog Subject: [PATCH] cleanup: std::weak_ptr use: replace manual lock by std::weak_ptr::lock To: libabigail@sourceware.org X-Spam-Status: No, score=-23.6 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libabigail@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Mailing list of the Libabigail project List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-Patchwork-Original-From: Matthias Maennich via Libabigail From: =?utf-8?q?Matthias_M=C3=A4nnich?= Reply-To: Matthias Maennich Cc: maennich@google.com, gprocida@google.com, kernel-team@android.com Errors-To: libabigail-bounces@sourceware.org Sender: "Libabigail" The pattern expired() ? shared_ptr() : shared_ptr(*this) on std::weak_ptr can be simplified by using std::weak_ptr::lock. Since weak_ptr::lock does this atomically, this patch also addresses potential data races between the call to expired() and the construction based on the assumption that the shared_ptr is still around. Hence, apply this simplification/fix to the code base. * src/abg-comparison-priv.h (diff::priv::get_context): improve weak_ptr usage. (corpus_diff:diff_stats::priv::ctxt): Likewise. * src/abg-comparison.cc (corpus_diff::priv::get_context): Likewise. * src/abg-ir.cc (elf_symbol::get_next_alias): Likewise. (elf_symbol::get_next_common_instance): Likewise. (type_base::get_canonical_type): Likewise. (qualified_type_def::get_underlying_type): Likewise. (pointer_type_def::get_pointed_to_type): Likewise. (reference_type_def::get_pointed_to_type): Likewise. (array_type_def::subrange_type::get_underlying_type): Likewise. (array_type_def::get_element_type): Likewise. (typedef_decl::get_underlying_type): Likewise. (var_decl::get_type): Likewise. (function_type::get_return_type): Likewise. (function_decl::get_type): Likewise. (function_decl::parameter::get_type): Likewise. (class_or_union::get_naming_typedef): Likewise. (class_or_union::get_definition_of_declaration): Likewise. (class_decl::base_spec::get_base_class): Likewise. (template_parameter::get_enclosing_template_decl): Likewise. (non_type_tparameter::get_type): Likewise. (type_composition::get_composed_type): Likewise. Signed-off-by: Matthias Maennich Reviewed-by: Giuliano Procida --- src/abg-comparison-priv.h | 8 +-- src/abg-comparison.cc | 6 +- src/abg-ir.cc | 114 +++++++------------------------------- 3 files changed, 22 insertions(+), 106 deletions(-) diff --git a/src/abg-comparison-priv.h b/src/abg-comparison-priv.h index 420e7014421c..6f081b961f1a 100644 --- a/src/abg-comparison-priv.h +++ b/src/abg-comparison-priv.h @@ -305,11 +305,7 @@ public: /// @returnt a smart pointer to the diff context. diff_context_sptr get_context() const - { - if (ctxt_.expired()) - return diff_context_sptr(); - return diff_context_sptr(ctxt_); - } + { return ctxt_.lock(); } /// Check if a given categorization of a diff node should make it be /// filtered out. @@ -1372,7 +1368,7 @@ struct corpus_diff::diff_stats::priv diff_context_sptr ctxt() - {return ctxt_.expired() ? diff_context_sptr() : diff_context_sptr(ctxt_);} + { return ctxt_.lock(); } }; // end class corpus_diff::diff_stats::priv void diff --git a/src/abg-comparison.cc b/src/abg-comparison.cc index 5797c9dd7f3f..cb6989a2316c 100644 --- a/src/abg-comparison.cc +++ b/src/abg-comparison.cc @@ -8901,11 +8901,7 @@ corpus_diff::diff_stats::net_num_leaf_var_changes() const /// @return a smart pointer to the context associate with the corpus. diff_context_sptr corpus_diff::priv::get_context() -{ - if (ctxt_.expired()) - return diff_context_sptr(); - return diff_context_sptr(ctxt_); -} +{ return ctxt_.lock(); } /// Tests if the lookup tables are empty. /// diff --git a/src/abg-ir.cc b/src/abg-ir.cc index 5cc39f59400a..69393028b93d 100644 --- a/src/abg-ir.cc +++ b/src/abg-ir.cc @@ -1725,11 +1725,7 @@ elf_symbol::is_main_symbol() const ///@return the alias, or NULL if there is no alias. elf_symbol_sptr elf_symbol::get_next_alias() const -{ - if (priv_->next_alias_.expired()) - return elf_symbol_sptr(); - return elf_symbol_sptr(priv_->next_alias_); -} +{ return priv_->next_alias_.lock(); } /// Check if the current elf_symbol has an alias. @@ -1828,11 +1824,7 @@ elf_symbol::has_other_common_instances() const /// @return the next common instance, or nil if there is not any. elf_symbol_sptr elf_symbol::get_next_common_instance() const -{ - if (priv_->next_common_instance_.expired()) - return elf_symbol_sptr(); - return elf_symbol_sptr(priv_->next_common_instance_); -} +{ return priv_->next_common_instance_.lock(); } /// Add a common instance to the current common elf symbol. /// @@ -12109,11 +12101,7 @@ type_base::type_base(const environment* e, size_t s, size_t a) /// type. type_base_sptr type_base::get_canonical_type() const -{ - if (priv_->canonical_type.expired()) - return type_base_sptr(); - return type_base_sptr(priv_->canonical_type); -} +{ return priv_->canonical_type.lock(); } /// Getter of the canonical type pointer. /// @@ -13400,11 +13388,7 @@ qualified_type_def::get_cv_quals_string_prefix() const /// Getter of the underlying type shared_ptr qualified_type_def::get_underlying_type() const -{ - if (priv_->underlying_type_.expired()) - return type_base_sptr(); - return type_base_sptr(priv_->underlying_type_); -} +{ return priv_->underlying_type_.lock(); } /// Non-member equality operator for @ref qualified_type_def /// @@ -13639,11 +13623,7 @@ pointer_type_def::operator==(const pointer_type_def& other) const /// @return the pointed-to type. const type_base_sptr pointer_type_def::get_pointed_to_type() const -{ - if (priv_->pointed_to_type_.expired()) - return type_base_sptr(); - return type_base_sptr(priv_->pointed_to_type_); -} +{ return priv_->pointed_to_type_.lock(); } /// Getter of a naked pointer to the pointed-to type. /// @@ -13938,11 +13918,7 @@ reference_type_def::operator==(const reference_type_def& o) const type_base_sptr reference_type_def::get_pointed_to_type() const -{ - if (pointed_to_type_.expired()) - return type_base_sptr(); - return type_base_sptr(pointed_to_type_); -} +{ return pointed_to_type_.lock(); } bool reference_type_def::is_lvalue() const @@ -14258,11 +14234,7 @@ array_type_def::subrange_type::subrange_type(const environment* env, /// @return the underlying type. type_base_sptr array_type_def::subrange_type::get_underlying_type() const -{ - if (priv_->underlying_type_.expired()) - return type_base_sptr(); - return type_base_sptr(priv_->underlying_type_); -} +{ return priv_->underlying_type_.lock(); } /// Setter of the underlying type of the subrange, that is, the type /// that defines the range. @@ -14796,11 +14768,7 @@ array_type_def::operator==(const type_base& o) const /// @return the type of an array element. const type_base_sptr array_type_def::get_element_type() const -{ - if (priv_->element_type_.expired()) - return type_base_sptr(); - return type_base_sptr(priv_->element_type_); -} +{ return priv_->element_type_.lock(); } /// Setter of the type of array element. /// @@ -15666,11 +15634,7 @@ typedef_decl::get_pretty_representation(bool internal, /// @return the underlying_type. type_base_sptr typedef_decl::get_underlying_type() const -{ - if (priv_->underlying_type_.expired()) - return type_base_sptr(); - return type_base_sptr(priv_->underlying_type_); -} +{ return priv_->underlying_type_.lock(); } /// This implements the ir_traversable_base::traverse pure virtual /// function. @@ -15760,11 +15724,7 @@ var_decl::var_decl(const string& name, /// @return the type of the variable. const type_base_sptr var_decl::get_type() const -{ - if (priv_->type_.expired()) - return type_base_sptr(); - return type_base_sptr(priv_->type_); -} +{ return priv_->type_.lock(); } /// Getter of the type of the variable. /// @@ -16357,11 +16317,7 @@ function_type::function_type(const environment* env, /// @return the return type. type_base_sptr function_type::get_return_type() const -{ - if (priv_->return_type_.expired()) - return type_base_sptr(); - return type_base_sptr(priv_->return_type_); -} +{ return priv_->return_type_.lock(); } /// Setter of the return type of the current instance of @ref /// function_type. @@ -17203,11 +17159,7 @@ function_decl::get_first_non_implicit_parm() const /// @return the type of the current instance of @ref function_decl. const shared_ptr function_decl::get_type() const -{ - if (priv_->type_.expired()) - return function_type_sptr(); - return function_type_sptr(priv_->type_); -} +{ return priv_->type_.lock(); } /// Fast getter of the type of the current instance of @ref function_decl. /// @@ -17727,11 +17679,7 @@ function_decl::parameter::parameter(const type_base_sptr type, const type_base_sptr function_decl::parameter::get_type()const -{ - if (priv_->type_.expired()) - return type_base_sptr(); - return type_base_sptr(priv_->type_); -} +{ return priv_->type_.lock(); } /// @return a copy of the type name of the parameter. interned_string @@ -18567,11 +18515,7 @@ class_or_union::set_is_declaration_only(bool f) /// @return the naming typedef, if any. Otherwise, returns nil. typedef_decl_sptr class_or_union::get_naming_typedef() const -{ - if (priv_->naming_typedef_.expired()) - return typedef_decl_sptr(); - return typedef_decl_sptr(priv_->naming_typedef_); -} +{ return priv_->naming_typedef_.lock(); } /// Set the naming typedef of the current instance of @ref class_decl. /// @@ -18609,11 +18553,7 @@ class_or_union::set_definition_of_declaration(class_or_union_sptr d) /// @return the definition of this decl-only class. const class_or_union_sptr class_or_union::get_definition_of_declaration() const -{ - if (priv_->definition_of_declaration_.expired()) - return class_or_union_sptr(); - return class_or_union_sptr(priv_->definition_of_declaration_); -} +{ return priv_->definition_of_declaration_.lock(); } /// If this @ref class_or_union is declaration-only, get its /// definition, if any. @@ -19916,11 +19856,7 @@ class_decl::base_spec::base_spec(const class_decl_sptr& base, /// @return the base class. class_decl_sptr class_decl::base_spec::get_base_class() const -{ - if (priv_->base_class_.expired()) - return class_decl_sptr(); - return class_decl_sptr(priv_->base_class_); -} +{ return priv_->base_class_.lock(); } /// Getter of the "is-virtual" proprerty of the base class specifier. /// @@ -21896,11 +21832,7 @@ template_parameter::get_index() const const template_decl_sptr template_parameter::get_enclosing_template_decl() const -{ - if (priv_->template_decl_.expired()) - return template_decl_sptr(); - return template_decl_sptr(priv_->template_decl_); -} +{ return priv_->template_decl_.lock(); } bool template_parameter::get_hashing_has_started() const @@ -22068,11 +22000,7 @@ non_type_tparameter::non_type_tparameter(unsigned index, /// @return the type of the template parameter. const type_base_sptr non_type_tparameter::get_type() const -{ - if (priv_->type_.expired()) - return type_base_sptr(); - return type_base_sptr(priv_->type_); -} +{ return priv_->type_.lock(); } /// Get the hash value of the current instance. /// @@ -22241,11 +22169,7 @@ type_composition::type_composition(unsigned index, /// @return the composed type. const type_base_sptr type_composition::get_composed_type() const -{ - if (priv_->type_.expired()) - return type_base_sptr(); - return type_base_sptr(priv_->type_); -} +{ return priv_->type_.lock(); } /// Setter for the resulting composed type. ///