Skip to content

Commit

Permalink
spvDiagnosticDestroy is safe to call on nullptr
Browse files Browse the repository at this point in the history
  • Loading branch information
dneto0 committed Jan 11, 2016
1 parent 3b69d05 commit 59f4100
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ if (NOT ${SPIRV_SKIP_EXECUTABLES})
${CMAKE_CURRENT_SOURCE_DIR}/test/BinaryToText.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/BinaryToText.Literal.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/Comment.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/DiagnosticDestroy.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/DiagnosticPrint.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/DiagnosticStream.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/ExtInstGLSLstd450.cpp
Expand Down
3 changes: 2 additions & 1 deletion include/libspirv/libspirv.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ spv_result_t spvValidate(const spv_const_context context,
spv_diagnostic spvDiagnosticCreate(const spv_position position,
const char* message);

/// Destroys a diagnostic object.
// Destroys a diagnostic object. This is a no-op if diagnostic is a null
// pointer.
void spvDiagnosticDestroy(spv_diagnostic diagnostic);

// Prints the diagnostic to stderr.
Expand Down
59 changes: 59 additions & 0 deletions test/DiagnosticDestroy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2015-2016 The Khronos Group 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.

#include "UnitSPIRV.h"

namespace {

TEST(DiagnosticDestroy, DestroyNull) { spvDiagnosticDestroy(nullptr); }

// Returns a newly created diagnostic value.
spv_diagnostic MakeValidDiagnostic() {
spv_position_t position = {};
spv_diagnostic diagnostic = spvDiagnosticCreate(&position, "");
return diagnostic;
}

TEST(DiagnosticDestroy, DestroyValidDiagnostic) {
spv_diagnostic diagnostic = MakeValidDiagnostic();
EXPECT_NE(nullptr, diagnostic);
spvDiagnosticDestroy(diagnostic);
// We aren't allowed to use the diagnostic pointer anymore.
// So we can't test its behaviour.
}

TEST(DiagnosticDestroy, DestroyValidDiagnosticAfterReassignment) {
spv_diagnostic diagnostic = MakeValidDiagnostic();
spv_diagnostic second_diagnostic = MakeValidDiagnostic();
EXPECT_NE(nullptr, diagnostic);
EXPECT_NE(nullptr, second_diagnostic);
EXPECT_TRUE(diagnostic != second_diagnostic);
spvDiagnosticDestroy(diagnostic);
diagnostic = second_diagnostic;
spvDiagnosticDestroy(diagnostic);
}

} // anonymous namespace

0 comments on commit 59f4100

Please sign in to comment.