#include "libspirv.hpp"
#include "ir_loader.h"
+#include "make_unique.h"
namespace spvtools {
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<ir::IrLoader*>(builder)->SetModuleHeader(
- magic, version, generator, id_bound, reserved);
+ reinterpret_cast<ir::IrLoader*>(builder)
+ ->SetModuleHeader(magic, version, generator, id_bound, reserved);
return SPV_SUCCESS;
};
const std::vector<uint32_t>& binary) {
spv_diagnostic diagnostic = nullptr;
- std::unique_ptr<ir::Module> module(new ir::Module);
+ auto module = MakeUnique<ir::Module>();
ir::IrLoader loader(module.get());
spv_result_t status =
--- /dev/null
+// 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 <memory>
+#include <utility>
+
+namespace spvtools {
+
+template <typename T, typename... Args>
+std::unique_ptr<T> MakeUnique(Args&&... args) {
+ return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
+}
+
+} // namespace spvtools
+
+#endif // LIBSPIRV_OPT_MAKE_UNIQUE_H_
#ifndef LIBSPIRV_TEST_OPT_PASS_FIXTURE_H_
#define LIBSPIRV_TEST_OPT_PASS_FIXTURE_H_
-#include <memory>
#include <string>
#include <tuple>
#include <vector>
#include "opt/libspirv.hpp"
#include "opt/pass_manager.h"
#include "opt/passes.h"
+#include "opt/make_unique.h"
namespace spvtools {
std::tuple<std::string, bool> OptimizeAndDisassemble(
opt::Pass* pass, const std::string& original, bool skip_nop = false) {
std::unique_ptr<ir::Module> 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);
}
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);
}
template <typename PassT>
std::tuple<std::string, bool> SinglePassRunAndDisassemble(
const std::string& assembly, bool skip_nop = false) {
- auto pass = std::unique_ptr<PassT>(new PassT);
+ auto pass = MakeUnique<PassT>();
return OptimizeAndDisassemble(pass.get(), assembly, skip_nop);
}
#include "gmock/gmock.h"
#include "opt/iterator.h"
+#include "opt/make_unique.h"
namespace {
// Insert at the beginning
expected.insert(expected.begin(), -100);
ir::UptrVectorIterator<int> begin(&data, data.begin());
- auto insert_point = begin.InsertBefore(std::unique_ptr<int>(new int(-100)));
+ auto insert_point = begin.InsertBefore(MakeUnique<int>(-100));
for (int i = 0; i < count + 1; ++i) {
actual.push_back(*(insert_point++));
}
expected.push_back(-36);
expected.push_back(-77);
ir::UptrVectorIterator<int> end(&data, data.end());
- end = end.InsertBefore(std::unique_ptr<int>(new int(-77)));
- end = end.InsertBefore(std::unique_ptr<int>(new int(-36)));
- end = end.InsertBefore(std::unique_ptr<int>(new int(-42)));
+ end = end.InsertBefore(MakeUnique<int>(-77));
+ end = end.InsertBefore(MakeUnique<int>(-36));
+ end = end.InsertBefore(MakeUnique<int>(-42));
actual.clear();
begin = ir::UptrVectorIterator<int>(&data, data.begin());
ir::UptrVectorIterator<int> it(&data, data.begin());
for (int i = 0; i < insert_pos; ++i) ++it;
- it = it.InsertBefore(std::unique_ptr<int>(new int(-100)));
- it = it.InsertBefore(std::unique_ptr<int>(new int(-42)));
+ it = it.InsertBefore(MakeUnique<int>(-100));
+ it = it.InsertBefore(MakeUnique<int>(-42));
auto begin = ir::UptrVectorIterator<int>(&data, data.begin());
for (int i = 0; i < count + 2; ++i) {
actual.push_back(*(begin++));
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
#include "pass_fixture.h"
+#include "opt/make_unique.h"
namespace {
EXPECT_EQ(1u, manager.NumPasses());
EXPECT_STREQ("strip-debug", manager.GetPass(0)->name());
- manager.AddPass(std::unique_ptr<opt::NullPass>(new opt::NullPass));
+ manager.AddPass(MakeUnique<opt::NullPass>());
EXPECT_EQ(2u, manager.NumPasses());
EXPECT_STREQ("strip-debug", manager.GetPass(0)->name());
EXPECT_STREQ("null", manager.GetPass(1)->name());
class AppendOpNopPass : public opt::Pass {
const char* name() const override { return "AppendOpNop"; }
bool Process(ir::Module* module) override {
- std::unique_ptr<ir::Instruction> inst(new ir::Instruction());
+ auto inst = MakeUnique<ir::Instruction>();
module->AddDebugInst(std::move(inst));
return true;
}
class DuplicateInstPass : public opt::Pass {
const char* name() const override { return "DuplicateInst"; }
bool Process(ir::Module* module) override {
- std::unique_ptr<ir::Instruction> inst(
- new ir::Instruction(*(--module->debug_end())));
+ auto inst = MakeUnique<ir::Instruction>(*(--module->debug_end()));
module->AddDebugInst(std::move(inst));
return true;
}
#include <gtest/gtest.h>
-#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 {
}
TEST(Types, VectorElementCount) {
- auto s32 = std::unique_ptr<Integer>(new Integer(32, true));
+ auto s32 = MakeUnique<Integer>(32, true);
for (uint32_t c : {2, 3, 4}) {
- auto s32v = std::unique_ptr<Vector>(new Vector(s32.get(), c));
+ auto s32v = MakeUnique<Vector>(s32.get(), c);
EXPECT_EQ(c, s32v->element_count());
}
}
TEST(Types, MatrixElementCount) {
- auto s32 = std::unique_ptr<Integer>(new Integer(32, true));
- auto s32v4 = std::unique_ptr<Vector>(new Vector(s32.get(), 4));
+ auto s32 = MakeUnique<Integer>(32, true);
+ auto s32v4 = MakeUnique<Vector>(s32.get(), 4);
for (uint32_t c : {1, 2, 3, 4, 10, 100}) {
- auto s32m = std::unique_ptr<Matrix>(new Matrix(s32v4.get(), c));
+ auto s32m = MakeUnique<Matrix>(s32v4.get(), c);
EXPECT_EQ(c, s32m->element_count());
}
}