Parenthesize SPV_BIT macro to avoid surprises.
authorDavid Neto <dneto@google.com>
Fri, 14 Aug 2015 17:57:02 +0000 (13:57 -0400)
committerDavid Neto <dneto@google.com>
Mon, 17 Aug 2015 21:01:42 +0000 (17:01 -0400)
CMakeLists.txt
include/libspirv/libspirv.h
test/LibspirvMacros.cpp [new file with mode: 0644]

index 62e85e1..16f34db 100644 (file)
@@ -158,6 +158,7 @@ if (TARGET gtest)
     ${CMAKE_CURRENT_SOURCE_DIR}/test/FixWord.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test/BinaryHeaderGet.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test/BinaryToText.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/test/LibspirvMacros.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test/NamedId.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test/OpcodeTableGet.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test/OpcodeIsVariable.cpp
index fcb24b4..6b3d1d4 100644 (file)
@@ -90,7 +90,7 @@ extern "C" {
 
 #define spvIsInBitfield(value, bitfield) (value == (value & bitfield))
 
-#define SPV_BIT(shift) 1 << shift
+#define SPV_BIT(shift) (1 << (shift))
 
 #define SPV_FORCE_16_BIT_ENUM(name) _##name = 0x7fff
 #define SPV_FORCE_32_BIT_ENUM(name) _##name = 0x7fffffff
diff --git a/test/LibspirvMacros.cpp b/test/LibspirvMacros.cpp
new file mode 100644 (file)
index 0000000..ebbded6
--- /dev/null
@@ -0,0 +1,35 @@
+// Copyright (c) 2015 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"
+
+TEST(Macros, BitShiftInnerParens) {
+  ASSERT_EQ(65536, SPV_BIT(2 << 3));
+}
+
+TEST(Macros, BitShiftOuterParens) {
+  ASSERT_EQ(15, SPV_BIT(4)-1);
+}