From 01580dd19ab47db86bf5fbdaeccb4fced1e7a77f Mon Sep 17 00:00:00 2001 From: Tim Keith Date: Thu, 1 Mar 2018 18:04:05 -0800 Subject: [PATCH] [flang] Replace raw pointers with std::unique_ptr. This simplifies the "delete and set to null" pattern to just a call to reset(), and "assign and set to null" to a call to std::move. The ideal would be a custom deleter that throws an error when an implicit delete happens but not when reset() is called, but I'm not sure that's possible. Original-commit: flang-compiler/f18@f618fc729a5b92b036437ec9828788823ce88f13 Reviewed-on: https://github.com/flang-compiler/f18/pull/23 --- flang/lib/semantics/make-types.cc | 111 +++++++++++++++--------------- 1 file changed, 54 insertions(+), 57 deletions(-) diff --git a/flang/lib/semantics/make-types.cc b/flang/lib/semantics/make-types.cc index 8d1bd9f7861b..a91622764bba 100644 --- a/flang/lib/semantics/make-types.cc +++ b/flang/lib/semantics/make-types.cc @@ -1,10 +1,12 @@ +#include "make-types.h" #include "attr.h" #include "type.h" #include "../parser/idioms.h" -#include "../parser/parse-tree.h" #include "../parser/parse-tree-visitor.h" +#include "../parser/parse-tree.h" #include +#include #include namespace Fortran { @@ -27,21 +29,19 @@ public: return true; } void Post(const parser::TypeDeclarationStmt &x) { - CHECK(declTypeSpec_ != nullptr); out_ << *declTypeSpec_ << "\n\n"; - delete declTypeSpec_; - declTypeSpec_ = nullptr; + declTypeSpec_.reset(); } bool Pre(const parser::DerivedTypeDef &x) { - builder_ = new DerivedTypeDefBuilder{}; + CHECK(!builder_); + builder_ = std::make_unique(); return true; } void Post(const parser::DerivedTypeDef &x) { DerivedTypeDef derivedType{*builder_}; out_ << derivedType << "\n\n"; - delete builder_; - builder_ = nullptr; + builder_.reset(); } bool Pre(const parser::TypeAttrSpec::Extends &x) { @@ -152,64 +152,54 @@ public: } bool Pre(const parser::ProcComponentDefStmt &x) { - CHECK(attrs_ == nullptr); - attrs_ = new Attrs(); + CHECK(!attrs_); + attrs_ = std::make_unique(); return true; } void Post(const parser::ProcComponentDefStmt &x) { if (declTypeSpec_) { std::cerr << "ProcComponentDefStmt: " << *declTypeSpec_ << "\n"; - delete declTypeSpec_; - declTypeSpec_ = nullptr; + declTypeSpec_.reset(); } - delete attrs_; - attrs_ = nullptr; + attrs_.reset(); } void Post(const parser::ProcDecl &x) { - CHECK(attrs_ != nullptr); const auto &name = std::get(x.t); //TODO: std::get>(x.t) builder_->procComponent(ProcComponentDef(ProcDecl(name), *attrs_)); } bool Pre(const parser::DataComponentDefStmt &x) { - CHECK(attrs_ == nullptr); - attrs_ = new Attrs(); + CHECK(!attrs_); + attrs_ = std::make_unique(); return true; } void Post(const parser::DataComponentDefStmt &x) { - delete declTypeSpec_; - declTypeSpec_ = nullptr; - delete attrs_; - attrs_ = nullptr; - delete attrArraySpec_; - attrArraySpec_ = nullptr; + declTypeSpec_.reset(); + attrs_.reset(); + attrArraySpec_.reset(); } void Post(const parser::ComponentAttrSpec &x) { - if (attrArraySpec_ == nullptr) { - attrArraySpec_ = arraySpec_; - arraySpec_ = nullptr; + if (!attrArraySpec_) { + attrArraySpec_ = std::move(arraySpec_); } } void Post(const parser::ComponentDecl &x) { - CHECK(declTypeSpec_ != nullptr); - CHECK(attrs_ != nullptr); const auto &name = std::get(x.t); // use the array spec in the decl if present const auto &arraySpec = arraySpec_ && !arraySpec_->empty() ? *arraySpec_ - : attrArraySpec_ != nullptr ? *attrArraySpec_ : ComponentArraySpec{}; + : attrArraySpec_ ? *attrArraySpec_ : ComponentArraySpec{}; builder_->dataComponent( DataComponentDef(*declTypeSpec_, name, *attrs_, arraySpec)); - delete arraySpec_; - arraySpec_ = nullptr; + arraySpec_.reset(); } bool Pre(const parser::ComponentArraySpec &x) { - CHECK(arraySpec_ == nullptr); - arraySpec_ = new std::list(); + CHECK(!arraySpec_); + arraySpec_ = std::make_unique>(); return true; } bool Pre(const parser::DeferredShapeSpecList &x) { @@ -230,11 +220,15 @@ public: } bool Pre(const parser::DeclarationTypeSpec::ClassStar &x) { - declTypeSpec_ = new DeclTypeSpec{semantics::DeclTypeSpec::MakeClassStar()}; + CHECK(!declTypeSpec_); + declTypeSpec_ = + std::make_unique(DeclTypeSpec::MakeClassStar()); return false; } bool Pre(const parser::DeclarationTypeSpec::TypeStar &x) { - declTypeSpec_ = new DeclTypeSpec{semantics::DeclTypeSpec::MakeTypeStar()}; + CHECK(!declTypeSpec_); + declTypeSpec_ = + std::make_unique(DeclTypeSpec::MakeTypeStar()); return false; } bool Pre(const parser::DeclarationTypeSpec::Type &x) { @@ -250,26 +244,30 @@ public: return true; } bool Pre(const parser::IntegerTypeSpec &x) { - declTypeSpec_ = new DeclTypeSpec{DeclTypeSpec::MakeIntrinsic( - IntegerTypeSpec::Make(GetKindParamValue(x.v)))}; + CHECK(!declTypeSpec_); + declTypeSpec_ = std::make_unique(DeclTypeSpec::MakeIntrinsic( + IntegerTypeSpec::Make(GetKindParamValue(x.v)))); return false; } bool Pre(const parser::IntrinsicTypeSpec::Logical &x) { - declTypeSpec_ = new DeclTypeSpec{DeclTypeSpec::MakeIntrinsic( - LogicalTypeSpec::Make(GetKindParamValue(x.kind)))}; + CHECK(!declTypeSpec_); + declTypeSpec_ = std::make_unique(DeclTypeSpec::MakeIntrinsic( + LogicalTypeSpec::Make(GetKindParamValue(x.kind)))); return false; } bool Pre(const parser::IntrinsicTypeSpec::Real &x) { - declTypeSpec_ = new DeclTypeSpec{DeclTypeSpec::MakeIntrinsic( - RealTypeSpec::Make(GetKindParamValue(x.kind)))}; + CHECK(!declTypeSpec_); + declTypeSpec_ = std::make_unique(DeclTypeSpec::MakeIntrinsic( + RealTypeSpec::Make(GetKindParamValue(x.kind)))); return false; } bool Pre(const parser::IntrinsicTypeSpec::Complex &x) { - declTypeSpec_ = new DeclTypeSpec{DeclTypeSpec::MakeIntrinsic( - ComplexTypeSpec::Make(GetKindParamValue(x.kind)))}; + CHECK(!declTypeSpec_); + declTypeSpec_ = std::make_unique(DeclTypeSpec::MakeIntrinsic( + ComplexTypeSpec::Make(GetKindParamValue(x.kind)))); return false; } @@ -287,35 +285,34 @@ public: } bool Pre(const parser::DerivedTypeStmt &x) { - CHECK(attrs_ == nullptr); - attrs_ = new Attrs{}; + CHECK(!attrs_); + attrs_ = std::make_unique(); return true; } void Post(const parser::DerivedTypeStmt &x) { builder_->name(std::get(x.t)); builder_->attrs(*attrs_); - delete attrs_; - attrs_ = nullptr; + attrs_.release(); } void Post(const parser::Program &) { // ensure that all temps were deallocated - CHECK(builder_ == nullptr); - CHECK(declTypeSpec_ == nullptr); - CHECK(attrs_ == nullptr); - CHECK(arraySpec_ == nullptr); - CHECK(attrArraySpec_ == nullptr); + CHECK(!builder_); + CHECK(!declTypeSpec_); + CHECK(!attrs_); + CHECK(!arraySpec_); + CHECK(!attrArraySpec_); } private: std::ostream &out_; - DerivedTypeDefBuilder *builder_ = nullptr; - DeclTypeSpec *declTypeSpec_ = nullptr; - Attrs *attrs_ = nullptr; - std::list *arraySpec_ = nullptr; + std::unique_ptr builder_; + std::unique_ptr declTypeSpec_; + std::unique_ptr attrs_; + std::unique_ptr> arraySpec_; // attrArraySpec_ is used to save the component-array-spec that is part of // the component-attr-spec - std::list *attrArraySpec_ = nullptr; + std::unique_ptr> attrArraySpec_; }; @@ -346,7 +343,7 @@ static const IntExpr *GetIntExpr(const parser::ScalarIntExpr &x) { } } std::cerr << "IntExpr:\n" << expr << "\n"; - return new semantics::IntExpr(); // TODO + return new IntExpr(); // TODO } static Bound GetBound(const parser::SpecificationExpr &x) {