tools/gen: clang-format generated .cc, .h and .inl files

Change-Id: I5a79cc0b5da1967632d9df02e058a8e3e5073d2d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/97441
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
diff --git a/src/tint/ast/storage_class.h b/src/tint/ast/storage_class.h
index cb21115..4da3db0 100644
--- a/src/tint/ast/storage_class.h
+++ b/src/tint/ast/storage_class.h
@@ -37,8 +37,8 @@
     kUniform,
     kStorage,
     kHandle,  // Tint-internal enum entry - not parsed
-    kIn,  // Tint-internal enum entry - not parsed
-    kOut,  // Tint-internal enum entry - not parsed
+    kIn,      // Tint-internal enum entry - not parsed
+    kOut,     // Tint-internal enum entry - not parsed
 };
 
 /// @param out the std::ostream to write to
diff --git a/src/tint/ast/storage_class_bench.cc b/src/tint/ast/storage_class_bench.cc
index d1232df..d8e9ae9 100644
--- a/src/tint/ast/storage_class_bench.cc
+++ b/src/tint/ast/storage_class_bench.cc
@@ -31,41 +31,12 @@
 
 void StorageClassParser(::benchmark::State& state) {
     std::array kStrings{
-        "fccnctin",
-        "ucti3",
-        "functVon",
-        "function",
-        "1unction",
-        "unJtqqon",
-        "llun77tion",
-        "ppqqivtHH",
-        "prcv",
-        "bivaGe",
-        "private",
-        "priviive",
-        "8WWivate",
-        "pxxvate",
-        "wXkgrggup",
-        "worXVup",
-        "3orkgroup",
-        "workgroup",
-        "workgroEp",
-        "woTTPkroup",
-        "ddorkroxxp",
-        "u44iform",
-        "unSSfoVVm",
-        "RniR22m",
-        "uniform",
-        "uFfo9m",
-        "uniorm",
-        "VOORRHrm",
-        "straye",
-        "llntrrr77ge",
-        "stor4g00",
-        "storage",
-        "trooe",
-        "zzrage",
-        "siioppa1",
+        "fccnctin",   "ucti3",      "functVon",   "function", "1unction",  "unJtqqon",
+        "llun77tion", "ppqqivtHH",  "prcv",       "bivaGe",   "private",   "priviive",
+        "8WWivate",   "pxxvate",    "wXkgrggup",  "worXVup",  "3orkgroup", "workgroup",
+        "workgroEp",  "woTTPkroup", "ddorkroxxp", "u44iform", "unSSfoVVm", "RniR22m",
+        "uniform",    "uFfo9m",     "uniorm",     "VOORRHrm", "straye",    "llntrrr77ge",
+        "stor4g00",   "storage",    "trooe",      "zzrage",   "siioppa1",
     };
     for (auto _ : state) {
         for (auto& str : kStrings) {
diff --git a/src/tint/ast/storage_class_test.cc b/src/tint/ast/storage_class_test.cc
index 2085168..9688236 100644
--- a/src/tint/ast/storage_class_test.cc
+++ b/src/tint/ast/storage_class_test.cc
@@ -42,28 +42,19 @@
 }
 
 static constexpr Case kValidCases[] = {
-    {"function", StorageClass::kFunction},
-    {"private", StorageClass::kPrivate},
-    {"workgroup", StorageClass::kWorkgroup},
-    {"uniform", StorageClass::kUniform},
+    {"function", StorageClass::kFunction},   {"private", StorageClass::kPrivate},
+    {"workgroup", StorageClass::kWorkgroup}, {"uniform", StorageClass::kUniform},
     {"storage", StorageClass::kStorage},
 };
 
 static constexpr Case kInvalidCases[] = {
-    {"fccnctin", StorageClass::kInvalid},
-    {"ucti3", StorageClass::kInvalid},
-    {"functVon", StorageClass::kInvalid},
-    {"priv1te", StorageClass::kInvalid},
-    {"pqiJate", StorageClass::kInvalid},
-    {"privat7ll", StorageClass::kInvalid},
-    {"workroppqHH", StorageClass::kInvalid},
-    {"workru", StorageClass::kInvalid},
-    {"wbkgGoup", StorageClass::kInvalid},
-    {"unifiivm", StorageClass::kInvalid},
-    {"8WWiform", StorageClass::kInvalid},
-    {"uxxform", StorageClass::kInvalid},
-    {"sXraggg", StorageClass::kInvalid},
-    {"traXe", StorageClass::kInvalid},
+    {"fccnctin", StorageClass::kInvalid},    {"ucti3", StorageClass::kInvalid},
+    {"functVon", StorageClass::kInvalid},    {"priv1te", StorageClass::kInvalid},
+    {"pqiJate", StorageClass::kInvalid},     {"privat7ll", StorageClass::kInvalid},
+    {"workroppqHH", StorageClass::kInvalid}, {"workru", StorageClass::kInvalid},
+    {"wbkgGoup", StorageClass::kInvalid},    {"unifiivm", StorageClass::kInvalid},
+    {"8WWiform", StorageClass::kInvalid},    {"uxxform", StorageClass::kInvalid},
+    {"sXraggg", StorageClass::kInvalid},     {"traXe", StorageClass::kInvalid},
     {"stor3ge", StorageClass::kInvalid},
 };
 
diff --git a/src/tint/ast/texel_format_bench.cc b/src/tint/ast/texel_format_bench.cc
index 8dde6b2..a17906a 100644
--- a/src/tint/ast/texel_format_bench.cc
+++ b/src/tint/ast/texel_format_bench.cc
@@ -31,118 +31,29 @@
 
 void TexelFormatParser(::benchmark::State& state) {
     std::array kStrings{
-        "rgbaunccrm",
-        "rlbanr3",
-        "rVba8unorm",
-        "rgba8unorm",
-        "rgba1unorm",
-        "rgbJqqnorm",
-        "rgb7ll8unorm",
-        "rgqqappnoHHm",
-        "rv8scor",
-        "rgbbGsnrm",
-        "rgba8snorm",
-        "rgba8vniirm",
-        "rg8a8snoWWm",
-        "Mgbaxxnorm",
-        "rXa8uggnt",
-        "rgbXVut",
-        "3gba8uint",
-        "rgba8uint",
-        "rgba8uiEt",
-        "rgTTPauint",
-        "ddgbauixxt",
-        "44gba8sint",
-        "VVgbaSSsint",
-        "rba8si2Rt",
-        "rgba8sint",
-        "r9bFsint",
-        "rgba8int",
-        "rgVROOsHnt",
-        "ryba1uint",
-        "r77ba1nnullrrt",
-        "rgb4006uint",
-        "rgba16uint",
-        "rb1uioot",
-        "rga1uzznt",
-        "r11b1uppiit",
-        "XXgba16sint",
-        "IIgb9916nni55t",
-        "rYbaSSrrsiHHat",
-        "rgba16sint",
-        "rbkk6Hit",
-        "jgba1sgRR",
-        "rgbab6si",
-        "rgba16fljat",
-        "rgba6float",
-        "rbq6float",
-        "rgba16float",
-        "rgba1NNloat",
-        "rgbvv6flot",
-        "rgbaQQ6foat",
-        "r3ffir",
-        "r32uijt",
-        "rNNwuin8",
-        "r32uint",
-        "r32int",
-        "rrr2uint",
-        "G32uint",
-        "r32sinFF",
-        "32st",
-        "r3rrint",
-        "r32sint",
-        "2sint",
-        "D3siJJt",
-        "r38n",
-        "r211lk",
-        "r32floa",
-        "r3flJat",
-        "r32float",
-        "r32fcoat",
-        "r32floOt",
-        "r32floKK_vtt",
-        "rxx32ui8",
-        "Fg3qq__n",
-        "rg32iqqt",
-        "rg32uint",
-        "rg333uin6",
-        "rtto62u9QQt",
-        "rg366uin",
-        "rOx2si6zz",
-        "rg3yysint",
-        "rHHsint",
-        "rg32sint",
-        "qWW432snt",
-        "rg3OOsnt",
-        "g32siYt",
-        "g32flo",
-        "rg32foaF",
-        "rg32fwat",
-        "rg32float",
-        "G3fKoaff",
-        "KKgq2float",
-        "rg32mmlo3t",
-        "rgba32uit",
-        "rqba3uint",
-        "rgbabb2uin",
-        "rgba32uint",
-        "rba32iint",
-        "qgba32uiOt",
-        "rgba32uiTTvv",
-        "rgFFa32sint",
-        "rg00Q2sPnt",
-        "rgbaP2sint",
-        "rgba32sint",
-        "rgb77s2sint",
-        "rgba32sbbRRC",
-        "rgbXX32sint",
-        "rOOOba3CCqoat",
-        "rgbu32fsLt",
-        "rgba3Xfloat",
-        "rgba32float",
-        "rba32float",
-        "qqb3float",
-        "rgba32fl22at",
+        "rgbaunccrm",    "rlbanr3",        "rVba8unorm",     "rgba8unorm",   "rgba1unorm",
+        "rgbJqqnorm",    "rgb7ll8unorm",   "rgqqappnoHHm",   "rv8scor",      "rgbbGsnrm",
+        "rgba8snorm",    "rgba8vniirm",    "rg8a8snoWWm",    "Mgbaxxnorm",   "rXa8uggnt",
+        "rgbXVut",       "3gba8uint",      "rgba8uint",      "rgba8uiEt",    "rgTTPauint",
+        "ddgbauixxt",    "44gba8sint",     "VVgbaSSsint",    "rba8si2Rt",    "rgba8sint",
+        "r9bFsint",      "rgba8int",       "rgVROOsHnt",     "ryba1uint",    "r77ba1nnullrrt",
+        "rgb4006uint",   "rgba16uint",     "rb1uioot",       "rga1uzznt",    "r11b1uppiit",
+        "XXgba16sint",   "IIgb9916nni55t", "rYbaSSrrsiHHat", "rgba16sint",   "rbkk6Hit",
+        "jgba1sgRR",     "rgbab6si",       "rgba16fljat",    "rgba6float",   "rbq6float",
+        "rgba16float",   "rgba1NNloat",    "rgbvv6flot",     "rgbaQQ6foat",  "r3ffir",
+        "r32uijt",       "rNNwuin8",       "r32uint",        "r32int",       "rrr2uint",
+        "G32uint",       "r32sinFF",       "32st",           "r3rrint",      "r32sint",
+        "2sint",         "D3siJJt",        "r38n",           "r211lk",       "r32floa",
+        "r3flJat",       "r32float",       "r32fcoat",       "r32floOt",     "r32floKK_vtt",
+        "rxx32ui8",      "Fg3qq__n",       "rg32iqqt",       "rg32uint",     "rg333uin6",
+        "rtto62u9QQt",   "rg366uin",       "rOx2si6zz",      "rg3yysint",    "rHHsint",
+        "rg32sint",      "qWW432snt",      "rg3OOsnt",       "g32siYt",      "g32flo",
+        "rg32foaF",      "rg32fwat",       "rg32float",      "G3fKoaff",     "KKgq2float",
+        "rg32mmlo3t",    "rgba32uit",      "rqba3uint",      "rgbabb2uin",   "rgba32uint",
+        "rba32iint",     "qgba32uiOt",     "rgba32uiTTvv",   "rgFFa32sint",  "rg00Q2sPnt",
+        "rgbaP2sint",    "rgba32sint",     "rgb77s2sint",    "rgba32sbbRRC", "rgbXX32sint",
+        "rOOOba3CCqoat", "rgbu32fsLt",     "rgba3Xfloat",    "rgba32float",  "rba32float",
+        "qqb3float",     "rgba32fl22at",
     };
     for (auto _ : state) {
         for (auto& str : kStrings) {
diff --git a/src/tint/ast/texel_format_test.cc b/src/tint/ast/texel_format_test.cc
index 9603961..606d78f 100644
--- a/src/tint/ast/texel_format_test.cc
+++ b/src/tint/ast/texel_format_test.cc
@@ -42,73 +42,41 @@
 }
 
 static constexpr Case kValidCases[] = {
-    {"rgba8unorm", TexelFormat::kRgba8Unorm},
-    {"rgba8snorm", TexelFormat::kRgba8Snorm},
-    {"rgba8uint", TexelFormat::kRgba8Uint},
-    {"rgba8sint", TexelFormat::kRgba8Sint},
-    {"rgba16uint", TexelFormat::kRgba16Uint},
-    {"rgba16sint", TexelFormat::kRgba16Sint},
-    {"rgba16float", TexelFormat::kRgba16Float},
-    {"r32uint", TexelFormat::kR32Uint},
-    {"r32sint", TexelFormat::kR32Sint},
-    {"r32float", TexelFormat::kR32Float},
-    {"rg32uint", TexelFormat::kRg32Uint},
-    {"rg32sint", TexelFormat::kRg32Sint},
-    {"rg32float", TexelFormat::kRg32Float},
-    {"rgba32uint", TexelFormat::kRgba32Uint},
-    {"rgba32sint", TexelFormat::kRgba32Sint},
-    {"rgba32float", TexelFormat::kRgba32Float},
+    {"rgba8unorm", TexelFormat::kRgba8Unorm},   {"rgba8snorm", TexelFormat::kRgba8Snorm},
+    {"rgba8uint", TexelFormat::kRgba8Uint},     {"rgba8sint", TexelFormat::kRgba8Sint},
+    {"rgba16uint", TexelFormat::kRgba16Uint},   {"rgba16sint", TexelFormat::kRgba16Sint},
+    {"rgba16float", TexelFormat::kRgba16Float}, {"r32uint", TexelFormat::kR32Uint},
+    {"r32sint", TexelFormat::kR32Sint},         {"r32float", TexelFormat::kR32Float},
+    {"rg32uint", TexelFormat::kRg32Uint},       {"rg32sint", TexelFormat::kRg32Sint},
+    {"rg32float", TexelFormat::kRg32Float},     {"rgba32uint", TexelFormat::kRgba32Uint},
+    {"rgba32sint", TexelFormat::kRgba32Sint},   {"rgba32float", TexelFormat::kRgba32Float},
 };
 
 static constexpr Case kInvalidCases[] = {
-    {"rgbaunccrm", TexelFormat::kInvalid},
-    {"rlbanr3", TexelFormat::kInvalid},
-    {"rVba8unorm", TexelFormat::kInvalid},
-    {"rgba1snorm", TexelFormat::kInvalid},
-    {"rgbJqqnorm", TexelFormat::kInvalid},
-    {"rgb7ll8snorm", TexelFormat::kInvalid},
-    {"rgbauippqHH", TexelFormat::kInvalid},
-    {"rgbaun", TexelFormat::kInvalid},
-    {"rba8Gint", TexelFormat::kInvalid},
-    {"rgvia8sint", TexelFormat::kInvalid},
-    {"rgba8WWint", TexelFormat::kInvalid},
-    {"rgbasxxMt", TexelFormat::kInvalid},
-    {"rXba16ungg", TexelFormat::kInvalid},
-    {"rba1XuVt", TexelFormat::kInvalid},
-    {"rgba16uin3", TexelFormat::kInvalid},
-    {"rgba16sinE", TexelFormat::kInvalid},
-    {"TTgba16sPPn", TexelFormat::kInvalid},
-    {"rgbad6xxint", TexelFormat::kInvalid},
-    {"rgba446float", TexelFormat::kInvalid},
-    {"SSVVba16float", TexelFormat::kInvalid},
-    {"rgbRR6float", TexelFormat::kInvalid},
-    {"rFui9t", TexelFormat::kInvalid},
-    {"r32int", TexelFormat::kInvalid},
-    {"VOORRHnt", TexelFormat::kInvalid},
-    {"r3siyt", TexelFormat::kInvalid},
-    {"lln3rrs77nt", TexelFormat::kInvalid},
-    {"r32s4n00", TexelFormat::kInvalid},
-    {"32ooat", TexelFormat::kInvalid},
-    {"r32fzzt", TexelFormat::kInvalid},
-    {"r3iippl1a", TexelFormat::kInvalid},
-    {"XXg32uint", TexelFormat::kInvalid},
-    {"rII39955nnnt", TexelFormat::kInvalid},
-    {"aagHH2uinYSS", TexelFormat::kInvalid},
-    {"rkk3it", TexelFormat::kInvalid},
-    {"gj3sRRn", TexelFormat::kInvalid},
-    {"r3bsnt", TexelFormat::kInvalid},
-    {"rg32flojt", TexelFormat::kInvalid},
-    {"r32floa", TexelFormat::kInvalid},
-    {"rg32lot", TexelFormat::kInvalid},
-    {"rgb3uit", TexelFormat::kInvalid},
-    {"rgjj3uint", TexelFormat::kInvalid},
-    {"rgb2urnff", TexelFormat::kInvalid},
-    {"rgba32sijt", TexelFormat::kInvalid},
-    {"NNgba32ww2t", TexelFormat::kInvalid},
-    {"rgba32snt", TexelFormat::kInvalid},
-    {"rgba32rrloat", TexelFormat::kInvalid},
-    {"rgGa32float", TexelFormat::kInvalid},
-    {"FFgba32float", TexelFormat::kInvalid},
+    {"rgbaunccrm", TexelFormat::kInvalid},   {"rlbanr3", TexelFormat::kInvalid},
+    {"rVba8unorm", TexelFormat::kInvalid},   {"rgba1snorm", TexelFormat::kInvalid},
+    {"rgbJqqnorm", TexelFormat::kInvalid},   {"rgb7ll8snorm", TexelFormat::kInvalid},
+    {"rgbauippqHH", TexelFormat::kInvalid},  {"rgbaun", TexelFormat::kInvalid},
+    {"rba8Gint", TexelFormat::kInvalid},     {"rgvia8sint", TexelFormat::kInvalid},
+    {"rgba8WWint", TexelFormat::kInvalid},   {"rgbasxxMt", TexelFormat::kInvalid},
+    {"rXba16ungg", TexelFormat::kInvalid},   {"rba1XuVt", TexelFormat::kInvalid},
+    {"rgba16uin3", TexelFormat::kInvalid},   {"rgba16sinE", TexelFormat::kInvalid},
+    {"TTgba16sPPn", TexelFormat::kInvalid},  {"rgbad6xxint", TexelFormat::kInvalid},
+    {"rgba446float", TexelFormat::kInvalid}, {"SSVVba16float", TexelFormat::kInvalid},
+    {"rgbRR6float", TexelFormat::kInvalid},  {"rFui9t", TexelFormat::kInvalid},
+    {"r32int", TexelFormat::kInvalid},       {"VOORRHnt", TexelFormat::kInvalid},
+    {"r3siyt", TexelFormat::kInvalid},       {"lln3rrs77nt", TexelFormat::kInvalid},
+    {"r32s4n00", TexelFormat::kInvalid},     {"32ooat", TexelFormat::kInvalid},
+    {"r32fzzt", TexelFormat::kInvalid},      {"r3iippl1a", TexelFormat::kInvalid},
+    {"XXg32uint", TexelFormat::kInvalid},    {"rII39955nnnt", TexelFormat::kInvalid},
+    {"aagHH2uinYSS", TexelFormat::kInvalid}, {"rkk3it", TexelFormat::kInvalid},
+    {"gj3sRRn", TexelFormat::kInvalid},      {"r3bsnt", TexelFormat::kInvalid},
+    {"rg32flojt", TexelFormat::kInvalid},    {"r32floa", TexelFormat::kInvalid},
+    {"rg32lot", TexelFormat::kInvalid},      {"rgb3uit", TexelFormat::kInvalid},
+    {"rgjj3uint", TexelFormat::kInvalid},    {"rgb2urnff", TexelFormat::kInvalid},
+    {"rgba32sijt", TexelFormat::kInvalid},   {"NNgba32ww2t", TexelFormat::kInvalid},
+    {"rgba32snt", TexelFormat::kInvalid},    {"rgba32rrloat", TexelFormat::kInvalid},
+    {"rgGa32float", TexelFormat::kInvalid},  {"FFgba32float", TexelFormat::kInvalid},
 };
 
 using TexelFormatParseTest = testing::TestWithParam<Case>;
diff --git a/tools/src/cmd/gen/main.go b/tools/src/cmd/gen/main.go
index b624c65..595f77c 100644
--- a/tools/src/cmd/gen/main.go
+++ b/tools/src/cmd/gen/main.go
@@ -23,9 +23,11 @@
 	"io/ioutil"
 	"math/rand"
 	"os"
+	"os/exec"
 	"path/filepath"
 	"reflect"
 	"regexp"
+	"runtime"
 	"strconv"
 	"strings"
 	"text/template"
@@ -72,6 +74,12 @@
 func run() error {
 	projectRoot := fileutils.ProjectRoot()
 
+	// Find clang-format
+	clangFormatPath := findClangFormat(projectRoot)
+	if clangFormatPath == "" {
+		return fmt.Errorf("cannot find clang-format in <dawn>/buildtools nor PATH")
+	}
+
 	// Recursively find all the template files in the <tint>/src directory
 	files, err := glob.Scan(projectRoot, glob.MustParseConfig(`{
 		"paths": [{"include": [
@@ -132,6 +140,15 @@
 		if body := sb.String(); body != "" {
 			_, tmplFileName := filepath.Split(tmplPath)
 			outFileName := strings.TrimSuffix(tmplFileName, ".tmpl")
+
+			switch filepath.Ext(outFileName) {
+			case ".cc", ".h", ".inl":
+				body, err = clangFormat(body, clangFormatPath)
+				if err != nil {
+					return err
+				}
+			}
+
 			if err := writeFile(outFileName, body); err != nil {
 				return err
 			}
@@ -486,3 +503,37 @@
 	}
 	return b.String()
 }
+
+// Invokes the clang-format executable at 'exe' to format the file content 'in'.
+// Returns the formatted file.
+func clangFormat(in, exe string) (string, error) {
+	cmd := exec.Command(exe)
+	cmd.Stdin = strings.NewReader(in)
+	out, err := cmd.CombinedOutput()
+	if err != nil {
+		return "", fmt.Errorf("clang-format failed:\n%v\n%v", string(out), err)
+	}
+	return string(out), nil
+}
+
+// Looks for clang-format in the 'buildtools' directory, falling back to PATH
+func findClangFormat(projectRoot string) string {
+	var path string
+	switch runtime.GOOS {
+	case "linux":
+		path = filepath.Join(projectRoot, "buildtools/linux64/clang-format")
+	case "darwin":
+		path = filepath.Join(projectRoot, "buildtools/mac/clang-format")
+	case "windows":
+		path = filepath.Join(projectRoot, "buildtools/win/clang-format.exe")
+	}
+	if fileutils.IsExe(path) {
+		return path
+	}
+	var err error
+	path, err = exec.LookPath("clang-format")
+	if err == nil {
+		return path
+	}
+	return ""
+}