From f51d82338e6a129300562b2c73daee02d3918b77 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Thu, 18 Aug 2016 23:16:21 -0400 Subject: [PATCH] Add a home brewed make_unique to ease creating unique_ptrs. --- source/opt/libspirv.cpp | 7 ++++--- source/opt/make_unique.h | 42 ++++++++++++++++++++++++++++++++++++++++++ test/opt/pass_fixture.h | 11 +++++------ test/opt/test_iterator.cpp | 13 +++++++------ test/opt/test_pass_manager.cpp | 8 ++++---- test/opt/test_types.cpp | 14 ++++++++------ 6 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 source/opt/make_unique.h diff --git a/source/opt/libspirv.cpp b/source/opt/libspirv.cpp index c076762..a8eced0 100644 --- a/source/opt/libspirv.cpp +++ b/source/opt/libspirv.cpp @@ -27,6 +27,7 @@ #include "libspirv.hpp" #include "ir_loader.h" +#include "make_unique.h" namespace spvtools { @@ -36,8 +37,8 @@ namespace { spv_result_t SetSpvHeader(void* builder, spv_endianness_t, uint32_t magic, uint32_t version, uint32_t generator, uint32_t id_bound, uint32_t reserved) { - reinterpret_cast(builder)->SetModuleHeader( - magic, version, generator, id_bound, reserved); + reinterpret_cast(builder) + ->SetModuleHeader(magic, version, generator, id_bound, reserved); return SPV_SUCCESS; }; @@ -88,7 +89,7 @@ std::unique_ptr SpvTools::BuildModule( const std::vector& binary) { spv_diagnostic diagnostic = nullptr; - std::unique_ptr module(new ir::Module); + auto module = MakeUnique(); ir::IrLoader loader(module.get()); spv_result_t status = diff --git a/source/opt/make_unique.h b/source/opt/make_unique.h new file mode 100644 index 0000000..3701c5d --- /dev/null +++ b/source/opt/make_unique.h @@ -0,0 +1,42 @@ +// Copyright (c) 2016 Google Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and/or associated documentation files (the +// "Materials"), to deal in the Materials without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Materials, and to +// permit persons to whom the Materials are furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Materials. +// +// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS +// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS +// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT +// https://www.khronos.org/registry/ +// +// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. + +#ifndef LIBSPIRV_OPT_MAKE_UNIQUE_H_ +#define LIBSPIRV_OPT_MAKE_UNIQUE_H_ + +#include +#include + +namespace spvtools { + +template +std::unique_ptr MakeUnique(Args&&... args) { + return std::unique_ptr(new T(std::forward(args)...)); +} + +} // namespace spvtools + +#endif // LIBSPIRV_OPT_MAKE_UNIQUE_H_ diff --git a/test/opt/pass_fixture.h b/test/opt/pass_fixture.h index 9d403d0..848de7d 100644 --- a/test/opt/pass_fixture.h +++ b/test/opt/pass_fixture.h @@ -27,7 +27,6 @@ #ifndef LIBSPIRV_TEST_OPT_PASS_FIXTURE_H_ #define LIBSPIRV_TEST_OPT_PASS_FIXTURE_H_ -#include #include #include #include @@ -37,6 +36,7 @@ #include "opt/libspirv.hpp" #include "opt/pass_manager.h" #include "opt/passes.h" +#include "opt/make_unique.h" namespace spvtools { @@ -59,8 +59,8 @@ class PassTest : public TestT { std::tuple OptimizeAndDisassemble( opt::Pass* pass, const std::string& original, bool skip_nop = false) { std::unique_ptr module = tools_.BuildModule(original); - EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n" - << original << std::endl; + EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n" << original + << std::endl; if (!module) { return std::make_tuple(std::string(), false); } @@ -71,8 +71,7 @@ class PassTest : public TestT { module->ToBinary(&binary, skip_nop); std::string optimized; EXPECT_EQ(SPV_SUCCESS, tools_.Disassemble(binary, &optimized)) - << "Disassembling failed for shader:\n" - << original << std::endl; + << "Disassembling failed for shader:\n" << original << std::endl; return std::make_tuple(optimized, modified); } @@ -82,7 +81,7 @@ class PassTest : public TestT { template std::tuple SinglePassRunAndDisassemble( const std::string& assembly, bool skip_nop = false) { - auto pass = std::unique_ptr(new PassT); + auto pass = MakeUnique(); return OptimizeAndDisassemble(pass.get(), assembly, skip_nop); } diff --git a/test/opt/test_iterator.cpp b/test/opt/test_iterator.cpp index c67886f..cf79348 100644 --- a/test/opt/test_iterator.cpp +++ b/test/opt/test_iterator.cpp @@ -30,6 +30,7 @@ #include "gmock/gmock.h" #include "opt/iterator.h" +#include "opt/make_unique.h" namespace { @@ -148,7 +149,7 @@ TEST(Iterator, InsertBeginEnd) { // Insert at the beginning expected.insert(expected.begin(), -100); ir::UptrVectorIterator begin(&data, data.begin()); - auto insert_point = begin.InsertBefore(std::unique_ptr(new int(-100))); + auto insert_point = begin.InsertBefore(MakeUnique(-100)); for (int i = 0; i < count + 1; ++i) { actual.push_back(*(insert_point++)); } @@ -159,9 +160,9 @@ TEST(Iterator, InsertBeginEnd) { expected.push_back(-36); expected.push_back(-77); ir::UptrVectorIterator end(&data, data.end()); - end = end.InsertBefore(std::unique_ptr(new int(-77))); - end = end.InsertBefore(std::unique_ptr(new int(-36))); - end = end.InsertBefore(std::unique_ptr(new int(-42))); + end = end.InsertBefore(MakeUnique(-77)); + end = end.InsertBefore(MakeUnique(-36)); + end = end.InsertBefore(MakeUnique(-42)); actual.clear(); begin = ir::UptrVectorIterator(&data, data.begin()); @@ -189,8 +190,8 @@ TEST(Iterator, InsertMiddle) { ir::UptrVectorIterator it(&data, data.begin()); for (int i = 0; i < insert_pos; ++i) ++it; - it = it.InsertBefore(std::unique_ptr(new int(-100))); - it = it.InsertBefore(std::unique_ptr(new int(-42))); + it = it.InsertBefore(MakeUnique(-100)); + it = it.InsertBefore(MakeUnique(-42)); auto begin = ir::UptrVectorIterator(&data, data.begin()); for (int i = 0; i < count + 2; ++i) { actual.push_back(*(begin++)); diff --git a/test/opt/test_pass_manager.cpp b/test/opt/test_pass_manager.cpp index 5c48f8f..0e8c45a 100644 --- a/test/opt/test_pass_manager.cpp +++ b/test/opt/test_pass_manager.cpp @@ -25,6 +25,7 @@ // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. #include "pass_fixture.h" +#include "opt/make_unique.h" namespace { @@ -38,7 +39,7 @@ TEST(PassManager, Interface) { EXPECT_EQ(1u, manager.NumPasses()); EXPECT_STREQ("strip-debug", manager.GetPass(0)->name()); - manager.AddPass(std::unique_ptr(new opt::NullPass)); + manager.AddPass(MakeUnique()); EXPECT_EQ(2u, manager.NumPasses()); EXPECT_STREQ("strip-debug", manager.GetPass(0)->name()); EXPECT_STREQ("null", manager.GetPass(1)->name()); @@ -54,7 +55,7 @@ TEST(PassManager, Interface) { class AppendOpNopPass : public opt::Pass { const char* name() const override { return "AppendOpNop"; } bool Process(ir::Module* module) override { - std::unique_ptr inst(new ir::Instruction()); + auto inst = MakeUnique(); module->AddDebugInst(std::move(inst)); return true; } @@ -64,8 +65,7 @@ class AppendOpNopPass : public opt::Pass { class DuplicateInstPass : public opt::Pass { const char* name() const override { return "DuplicateInst"; } bool Process(ir::Module* module) override { - std::unique_ptr inst( - new ir::Instruction(*(--module->debug_end()))); + auto inst = MakeUnique(*(--module->debug_end())); module->AddDebugInst(std::move(inst)); return true; } diff --git a/test/opt/test_types.cpp b/test/opt/test_types.cpp index b9b336e..a2e75e8 100644 --- a/test/opt/test_types.cpp +++ b/test/opt/test_types.cpp @@ -29,11 +29,13 @@ #include -#include "source/opt/types.h" +#include "opt/make_unique.h" +#include "opt/types.h" namespace { using namespace spvtools::opt::analysis; +using spvtools::MakeUnique; // Fixture class providing some element types. class SameTypeTest : public ::testing::Test { @@ -241,18 +243,18 @@ TEST(Types, FloatWidth) { } TEST(Types, VectorElementCount) { - auto s32 = std::unique_ptr(new Integer(32, true)); + auto s32 = MakeUnique(32, true); for (uint32_t c : {2, 3, 4}) { - auto s32v = std::unique_ptr(new Vector(s32.get(), c)); + auto s32v = MakeUnique(s32.get(), c); EXPECT_EQ(c, s32v->element_count()); } } TEST(Types, MatrixElementCount) { - auto s32 = std::unique_ptr(new Integer(32, true)); - auto s32v4 = std::unique_ptr(new Vector(s32.get(), 4)); + auto s32 = MakeUnique(32, true); + auto s32v4 = MakeUnique(s32.get(), 4); for (uint32_t c : {1, 2, 3, 4, 10, 100}) { - auto s32m = std::unique_ptr(new Matrix(s32v4.get(), c)); + auto s32m = MakeUnique(s32v4.get(), c); EXPECT_EQ(c, s32m->element_count()); } } -- 2.7.4