Add utilities for printing Dawn enums and bitmasks

This is a header-only utility so it can be easily included and
used anyhere. Include it in DawnTest.h to automatically pick up
definitions to print test parameters.

Also, work around bot limitations for very-long test names.

Bug: none
Change-Id: I940263ab0a4cc415b06fa04749694f16ff08335c
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/51841
Auto-Submit: Austin Eng <enga@chromium.org>
Reviewed-by: Stephen White <senorblanco@chromium.org>
Commit-Queue: Austin Eng <enga@chromium.org>
diff --git a/generator/dawn_json_generator.py b/generator/dawn_json_generator.py
index 858be52..c6268ed 100644
--- a/generator/dawn_json_generator.py
+++ b/generator/dawn_json_generator.py
@@ -697,6 +697,11 @@
                 FileRender('webgpu_cpp.h', 'src/include/dawn/webgpu_cpp.h',
                            [base_params, api_params]))
 
+            renders.append(
+                FileRender('webgpu_cpp_print.h',
+                           'src/include/dawn/webgpu_cpp_print.h',
+                           [base_params, api_params]))
+
         if 'dawn_proc' in targets:
             renders.append(
                 FileRender('dawn_proc.c', 'src/dawn/dawn_proc.c',
diff --git a/generator/templates/webgpu_cpp_print.h b/generator/templates/webgpu_cpp_print.h
new file mode 100644
index 0000000..5434b61
--- /dev/null
+++ b/generator/templates/webgpu_cpp_print.h
@@ -0,0 +1,90 @@
+//* Copyright 2021 The Dawn Authors
+//*
+//* Licensed under the Apache License, Version 2.0 (the "License");
+//* you may not use this file except in compliance with the License.
+//* You may obtain a copy of the License at
+//*
+//*     http://www.apache.org/licenses/LICENSE-2.0
+//*
+//* Unless required by applicable law or agreed to in writing, software
+//* distributed under the License is distributed on an "AS IS" BASIS,
+//* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//* See the License for the specific language governing permissions and
+//* limitations under the License.
+
+#ifndef WEBGPU_CPP_PRINT_H_
+#define WEBGPU_CPP_PRINT_H_
+
+#include "dawn/webgpu_cpp.h"
+
+#include <iomanip>
+#include <ios>
+#include <ostream>
+#include <type_traits>
+
+namespace wgpu {
+
+  {% for type in by_category["enum"] %}
+      template <typename CharT, typename Traits>
+      std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, {{as_cppType(type.name)}} value) {
+          switch (value) {
+            {% for value in type.values %}
+              case {{as_cppType(type.name)}}::{{as_cppEnum(value.name)}}:
+                o << "{{as_cppType(type.name)}}::{{as_cppEnum(value.name)}}";
+                break;
+            {% endfor %}
+              default:
+                o << "{{as_cppType(type.name)}}::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<{{as_cppType(type.name)}}>::type>(value);
+          }
+          return o;
+      }
+  {% endfor %}
+
+  {% for type in by_category["bitmask"] %}
+      template <typename CharT, typename Traits>
+      std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, {{as_cppType(type.name)}} value) {
+        o << "{{as_cppType(type.name)}}::";
+        if (!static_cast<bool>(value)) {
+          {% for value in type.values if value.value == 0 %}
+            // 0 is often explicitly declared as None.
+            o << "{{as_cppEnum(value.name)}}";
+          {% else %}
+            o << std::showbase << std::hex << std::setfill('0') << std::setw(4) << 0;
+          {% endfor %}
+          return o;
+        }
+
+        bool moreThanOneBit = !HasZeroOrOneBits(value);
+        if (moreThanOneBit) {
+          o << "(";
+        }
+
+        bool first = true;
+        {% for value in type.values if value.value != 0 %}
+          if (value & {{as_cppType(type.name)}}::{{as_cppEnum(value.name)}}) {
+            if (!first) {
+              o << "|";
+            }
+            first = false;
+            o << "{{as_cppEnum(value.name)}}";
+            value &= ~{{as_cppType(type.name)}}::{{as_cppEnum(value.name)}};
+          }
+        {% endfor %}
+
+        if (static_cast<bool>(value)) {
+          if (!first) {
+            o << "|";
+          }
+          o << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<{{as_cppType(type.name)}}>::type>(value);
+        }
+
+        if (moreThanOneBit) {
+          o << ")";
+        }
+        return o;
+      }
+  {% endfor %}
+
+}  // namespace wgpu
+
+#endif // WEBGPU_CPP_PRINT_H_
diff --git a/src/common/Preprocessor.h b/src/common/Preprocessor.h
index b49fc3a..4eef736 100644
--- a/src/common/Preprocessor.h
+++ b/src/common/Preprocessor.h
@@ -61,11 +61,10 @@
 
 // Implementation for DAWN_PP_FOR_EACH.
 // Creates a call to DAWN_PP_FOR_EACH_X where X is 1, 2, ..., etc.
-#define DAWN_PP_FOR_EACH_(N, func, x, ...) \
-    DAWN_PP_CONCATENATE(DAWN_PP_FOR_EACH_, N)(func, x, __VA_ARGS__)
+#define DAWN_PP_FOR_EACH_(N, func, ...) DAWN_PP_CONCATENATE(DAWN_PP_FOR_EACH_, N)(func, __VA_ARGS__)
 
 // DAWN_PP_FOR_EACH: Apply |func| to each argument in |x| and __VA_ARGS__
-#define DAWN_PP_FOR_EACH(func, x, ...) \
-    DAWN_PP_FOR_EACH_(DAWN_PP_FOR_EACH_NARG(x, __VA_ARGS__), func, x, __VA_ARGS__)
+#define DAWN_PP_FOR_EACH(func, ...) \
+    DAWN_PP_FOR_EACH_(DAWN_PP_FOR_EACH_NARG(__VA_ARGS__), func, __VA_ARGS__)
 
 #endif  // COMMON_PREPROCESSOR_H_
diff --git a/src/dawn/BUILD.gn b/src/dawn/BUILD.gn
index ad48712..543c51e 100644
--- a/src/dawn/BUILD.gn
+++ b/src/dawn/BUILD.gn
@@ -51,7 +51,10 @@
 
 dawn_json_generator("dawncpp_headers_gen") {
   target = "dawncpp_headers"
-  outputs = [ "src/include/dawn/webgpu_cpp.h" ]
+  outputs = [
+    "src/include/dawn/webgpu_cpp.h",
+    "src/include/dawn/webgpu_cpp_print.h",
+  ]
 }
 
 source_set("dawncpp_headers") {
diff --git a/src/tests/DawnTest.cpp b/src/tests/DawnTest.cpp
index 31fc13d..a7e0aa4 100644
--- a/src/tests/DawnTest.cpp
+++ b/src/tests/DawnTest.cpp
@@ -167,35 +167,56 @@
 }
 
 std::ostream& operator<<(std::ostream& os, const AdapterTestParam& param) {
-    // Sanitize the adapter name for GoogleTest
-    std::string sanitizedName =
-        std::regex_replace(param.adapterProperties.adapterName, std::regex("[^a-zA-Z0-9]+"), "_");
-
-    // Strip trailing underscores, if any.
-    if (sanitizedName.back() == '_') {
-        sanitizedName.back() = '\0';
-    }
-
-    os << ParamName(param.adapterProperties.backendType) << "_" << sanitizedName.c_str();
+    os << ParamName(param.adapterProperties.backendType) << " "
+       << param.adapterProperties.adapterName;
 
     // In a Windows Remote Desktop session there are two adapters named "Microsoft Basic Render
     // Driver" with different adapter types. We must differentiate them to avoid any tests using the
     // same name.
     if (param.adapterProperties.deviceID == 0x008C) {
         std::string adapterType = AdapterTypeName(param.adapterProperties.adapterType);
-        std::replace(adapterType.begin(), adapterType.end(), ' ', '_');
-        os << "_" << adapterType;
+        os << " " << adapterType;
     }
 
     for (const char* forceEnabledWorkaround : param.forceEnabledWorkarounds) {
-        os << "__e_" << forceEnabledWorkaround;
+        os << "; e:" << forceEnabledWorkaround;
     }
     for (const char* forceDisabledWorkaround : param.forceDisabledWorkarounds) {
-        os << "__d_" << forceDisabledWorkaround;
+        os << "; d:" << forceDisabledWorkaround;
     }
     return os;
 }
 
+DawnTestBase::PrintToStringParamName::PrintToStringParamName(const char* test) : mTest(test) {
+}
+
+std::string DawnTestBase::PrintToStringParamName::SanitizeParamName(std::string paramName,
+                                                                    size_t index) const {
+    // Sanitize the adapter name for GoogleTest
+    std::string sanitizedName = std::regex_replace(paramName, std::regex("[^a-zA-Z0-9]+"), "_");
+
+    // Strip trailing underscores, if any.
+    while (sanitizedName.back() == '_') {
+        sanitizedName.resize(sanitizedName.length() - 1);
+    }
+
+    // We don't know the the test name at this point, but the format usually looks like
+    // this.
+    std::string prefix = mTest + ".TheTestNameUsuallyGoesHere/";
+    std::string testFormat = prefix + sanitizedName;
+    if (testFormat.length() > 220) {
+        // The bots don't support test names longer than 256. Shorten the name and append a unique
+        // index if we're close. The failure log will still print the full param name.
+        std::string suffix = std::string("__") + std::to_string(index);
+        size_t targetLength = sanitizedName.length();
+        targetLength -= testFormat.length() - 220;
+        targetLength -= suffix.length();
+        sanitizedName.resize(targetLength);
+        sanitizedName = sanitizedName + suffix;
+    }
+    return sanitizedName;
+}
+
 // Implementation of DawnTestEnvironment
 
 void InitDawnEnd2EndTestEnvironment(int argc, char** argv) {
diff --git a/src/tests/DawnTest.h b/src/tests/DawnTest.h
index 6656a62..8bf1629 100644
--- a/src/tests/DawnTest.h
+++ b/src/tests/DawnTest.h
@@ -19,6 +19,7 @@
 #include "common/Preprocessor.h"
 #include "dawn/dawn_proc_table.h"
 #include "dawn/webgpu_cpp.h"
+#include "dawn/webgpu_cpp_print.h"
 #include "dawn_native/DawnNative.h"
 #include "tests/ParamGenerator.h"
 #include "tests/ToggleParser.h"
@@ -301,6 +302,18 @@
 
     virtual std::unique_ptr<dawn_platform::Platform> CreateTestPlatform();
 
+    struct PrintToStringParamName {
+        PrintToStringParamName(const char* test);
+        std::string SanitizeParamName(std::string paramName, size_t index) const;
+
+        template <class ParamType>
+        std::string operator()(const ::testing::TestParamInfo<ParamType>& info) const {
+            return SanitizeParamName(::testing::PrintToStringParamName()(info), info.index);
+        }
+
+        std::string mTest;
+    };
+
   protected:
     wgpu::Device device;
     wgpu::Queue queue;
@@ -510,7 +523,7 @@
         , testName,                                                                     \
         testing::ValuesIn(::detail::GetAvailableAdapterTestParamsForBackends(           \
             testName##params, sizeof(testName##params) / sizeof(testName##params[0]))), \
-        testing::PrintToStringParamName());                                             \
+        DawnTestBase::PrintToStringParamName(#testName));                               \
     GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(testName)
 
 // Instantiate the test once for each backend provided in the first param list.
@@ -526,13 +539,13 @@
 #define DAWN_INSTANTIATE_TEST_P(testName, ...)                                                 \
     INSTANTIATE_TEST_SUITE_P(                                                                  \
         , testName, ::testing::ValuesIn(MakeParamGenerator<testName::ParamType>(__VA_ARGS__)), \
-        testing::PrintToStringParamName());                                                    \
+        DawnTestBase::PrintToStringParamName(#testName));                                      \
     GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(testName)
 
 // Implementation for DAWN_TEST_PARAM_STRUCT to declare/print struct fields.
 #define DAWN_TEST_PARAM_STRUCT_DECL_STRUCT_FIELD(Type) Type DAWN_PP_CONCATENATE(m, Type);
 #define DAWN_TEST_PARAM_STRUCT_PRINT_STRUCT_FIELD(Type) \
-    o << "__" << #Type << "_" << param.DAWN_PP_CONCATENATE(m, Type);
+    o << "; " << #Type << "=" << param.DAWN_PP_CONCATENATE(m, Type);
 
 // Usage: DAWN_TEST_PARAM_STRUCT(Foo, TypeA, TypeB, ...)
 // Generate a test param struct called Foo which extends AdapterTestParam and generated
@@ -564,7 +577,7 @@
     };                                                                                             \
     std::ostream& operator<<(std::ostream& o, const StructName& param) {                           \
         o << static_cast<const AdapterTestParam&>(param);                                          \
-        o << "_" << static_cast<const DAWN_PP_CONCATENATE(_Dawn_, StructName)&>(param);            \
+        o << "; " << static_cast<const DAWN_PP_CONCATENATE(_Dawn_, StructName)&>(param);           \
         return o;                                                                                  \
     }