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>
This commit is contained in:
Ben Clayton 2022-08-01 19:29:14 +00:00 committed by Dawn LUCI CQ
parent 4aa360db5d
commit 92f2cb79b5
6 changed files with 123 additions and 231 deletions

View File

@ -31,41 +31,12 @@ namespace {
void StorageClassParser(::benchmark::State& state) { void StorageClassParser(::benchmark::State& state) {
std::array kStrings{ std::array kStrings{
"fccnctin", "fccnctin", "ucti3", "functVon", "function", "1unction", "unJtqqon",
"ucti3", "llun77tion", "ppqqivtHH", "prcv", "bivaGe", "private", "priviive",
"functVon", "8WWivate", "pxxvate", "wXkgrggup", "worXVup", "3orkgroup", "workgroup",
"function", "workgroEp", "woTTPkroup", "ddorkroxxp", "u44iform", "unSSfoVVm", "RniR22m",
"1unction", "uniform", "uFfo9m", "uniorm", "VOORRHrm", "straye", "llntrrr77ge",
"unJtqqon", "stor4g00", "storage", "trooe", "zzrage", "siioppa1",
"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 _ : state) {
for (auto& str : kStrings) { for (auto& str : kStrings) {

View File

@ -42,28 +42,19 @@ inline std::ostream& operator<<(std::ostream& out, Case c) {
} }
static constexpr Case kValidCases[] = { static constexpr Case kValidCases[] = {
{"function", StorageClass::kFunction}, {"function", StorageClass::kFunction}, {"private", StorageClass::kPrivate},
{"private", StorageClass::kPrivate}, {"workgroup", StorageClass::kWorkgroup}, {"uniform", StorageClass::kUniform},
{"workgroup", StorageClass::kWorkgroup},
{"uniform", StorageClass::kUniform},
{"storage", StorageClass::kStorage}, {"storage", StorageClass::kStorage},
}; };
static constexpr Case kInvalidCases[] = { static constexpr Case kInvalidCases[] = {
{"fccnctin", StorageClass::kInvalid}, {"fccnctin", StorageClass::kInvalid}, {"ucti3", StorageClass::kInvalid},
{"ucti3", StorageClass::kInvalid}, {"functVon", StorageClass::kInvalid}, {"priv1te", StorageClass::kInvalid},
{"functVon", StorageClass::kInvalid}, {"pqiJate", StorageClass::kInvalid}, {"privat7ll", StorageClass::kInvalid},
{"priv1te", StorageClass::kInvalid}, {"workroppqHH", StorageClass::kInvalid}, {"workru", StorageClass::kInvalid},
{"pqiJate", StorageClass::kInvalid}, {"wbkgGoup", StorageClass::kInvalid}, {"unifiivm", StorageClass::kInvalid},
{"privat7ll", StorageClass::kInvalid}, {"8WWiform", StorageClass::kInvalid}, {"uxxform", StorageClass::kInvalid},
{"workroppqHH", StorageClass::kInvalid}, {"sXraggg", StorageClass::kInvalid}, {"traXe", 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}, {"stor3ge", StorageClass::kInvalid},
}; };

View File

@ -31,118 +31,29 @@ namespace {
void TexelFormatParser(::benchmark::State& state) { void TexelFormatParser(::benchmark::State& state) {
std::array kStrings{ std::array kStrings{
"rgbaunccrm", "rgbaunccrm", "rlbanr3", "rVba8unorm", "rgba8unorm", "rgba1unorm",
"rlbanr3", "rgbJqqnorm", "rgb7ll8unorm", "rgqqappnoHHm", "rv8scor", "rgbbGsnrm",
"rVba8unorm", "rgba8snorm", "rgba8vniirm", "rg8a8snoWWm", "Mgbaxxnorm", "rXa8uggnt",
"rgba8unorm", "rgbXVut", "3gba8uint", "rgba8uint", "rgba8uiEt", "rgTTPauint",
"rgba1unorm", "ddgbauixxt", "44gba8sint", "VVgbaSSsint", "rba8si2Rt", "rgba8sint",
"rgbJqqnorm", "r9bFsint", "rgba8int", "rgVROOsHnt", "ryba1uint", "r77ba1nnullrrt",
"rgb7ll8unorm", "rgb4006uint", "rgba16uint", "rb1uioot", "rga1uzznt", "r11b1uppiit",
"rgqqappnoHHm", "XXgba16sint", "IIgb9916nni55t", "rYbaSSrrsiHHat", "rgba16sint", "rbkk6Hit",
"rv8scor", "jgba1sgRR", "rgbab6si", "rgba16fljat", "rgba6float", "rbq6float",
"rgbbGsnrm", "rgba16float", "rgba1NNloat", "rgbvv6flot", "rgbaQQ6foat", "r3ffir",
"rgba8snorm", "r32uijt", "rNNwuin8", "r32uint", "r32int", "rrr2uint",
"rgba8vniirm", "G32uint", "r32sinFF", "32st", "r3rrint", "r32sint",
"rg8a8snoWWm", "2sint", "D3siJJt", "r38n", "r211lk", "r32floa",
"Mgbaxxnorm", "r3flJat", "r32float", "r32fcoat", "r32floOt", "r32floKK_vtt",
"rXa8uggnt", "rxx32ui8", "Fg3qq__n", "rg32iqqt", "rg32uint", "rg333uin6",
"rgbXVut", "rtto62u9QQt", "rg366uin", "rOx2si6zz", "rg3yysint", "rHHsint",
"3gba8uint", "rg32sint", "qWW432snt", "rg3OOsnt", "g32siYt", "g32flo",
"rgba8uint", "rg32foaF", "rg32fwat", "rg32float", "G3fKoaff", "KKgq2float",
"rgba8uiEt", "rg32mmlo3t", "rgba32uit", "rqba3uint", "rgbabb2uin", "rgba32uint",
"rgTTPauint", "rba32iint", "qgba32uiOt", "rgba32uiTTvv", "rgFFa32sint", "rg00Q2sPnt",
"ddgbauixxt", "rgbaP2sint", "rgba32sint", "rgb77s2sint", "rgba32sbbRRC", "rgbXX32sint",
"44gba8sint", "rOOOba3CCqoat", "rgbu32fsLt", "rgba3Xfloat", "rgba32float", "rba32float",
"VVgbaSSsint", "qqb3float", "rgba32fl22at",
"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 _ : state) {
for (auto& str : kStrings) { for (auto& str : kStrings) {

View File

@ -42,73 +42,41 @@ inline std::ostream& operator<<(std::ostream& out, Case c) {
} }
static constexpr Case kValidCases[] = { static constexpr Case kValidCases[] = {
{"rgba8unorm", TexelFormat::kRgba8Unorm}, {"rgba8unorm", TexelFormat::kRgba8Unorm}, {"rgba8snorm", TexelFormat::kRgba8Snorm},
{"rgba8snorm", TexelFormat::kRgba8Snorm}, {"rgba8uint", TexelFormat::kRgba8Uint}, {"rgba8sint", TexelFormat::kRgba8Sint},
{"rgba8uint", TexelFormat::kRgba8Uint}, {"rgba16uint", TexelFormat::kRgba16Uint}, {"rgba16sint", TexelFormat::kRgba16Sint},
{"rgba8sint", TexelFormat::kRgba8Sint}, {"rgba16float", TexelFormat::kRgba16Float}, {"r32uint", TexelFormat::kR32Uint},
{"rgba16uint", TexelFormat::kRgba16Uint}, {"r32sint", TexelFormat::kR32Sint}, {"r32float", TexelFormat::kR32Float},
{"rgba16sint", TexelFormat::kRgba16Sint}, {"rg32uint", TexelFormat::kRg32Uint}, {"rg32sint", TexelFormat::kRg32Sint},
{"rgba16float", TexelFormat::kRgba16Float}, {"rg32float", TexelFormat::kRg32Float}, {"rgba32uint", TexelFormat::kRgba32Uint},
{"r32uint", TexelFormat::kR32Uint}, {"rgba32sint", TexelFormat::kRgba32Sint}, {"rgba32float", TexelFormat::kRgba32Float},
{"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[] = { static constexpr Case kInvalidCases[] = {
{"rgbaunccrm", TexelFormat::kInvalid}, {"rgbaunccrm", TexelFormat::kInvalid}, {"rlbanr3", TexelFormat::kInvalid},
{"rlbanr3", TexelFormat::kInvalid}, {"rVba8unorm", TexelFormat::kInvalid}, {"rgba1snorm", TexelFormat::kInvalid},
{"rVba8unorm", TexelFormat::kInvalid}, {"rgbJqqnorm", TexelFormat::kInvalid}, {"rgb7ll8snorm", TexelFormat::kInvalid},
{"rgba1snorm", TexelFormat::kInvalid}, {"rgbauippqHH", TexelFormat::kInvalid}, {"rgbaun", TexelFormat::kInvalid},
{"rgbJqqnorm", TexelFormat::kInvalid}, {"rba8Gint", TexelFormat::kInvalid}, {"rgvia8sint", TexelFormat::kInvalid},
{"rgb7ll8snorm", TexelFormat::kInvalid}, {"rgba8WWint", TexelFormat::kInvalid}, {"rgbasxxMt", TexelFormat::kInvalid},
{"rgbauippqHH", TexelFormat::kInvalid}, {"rXba16ungg", TexelFormat::kInvalid}, {"rba1XuVt", TexelFormat::kInvalid},
{"rgbaun", TexelFormat::kInvalid}, {"rgba16uin3", TexelFormat::kInvalid}, {"rgba16sinE", TexelFormat::kInvalid},
{"rba8Gint", TexelFormat::kInvalid}, {"TTgba16sPPn", TexelFormat::kInvalid}, {"rgbad6xxint", TexelFormat::kInvalid},
{"rgvia8sint", TexelFormat::kInvalid}, {"rgba446float", TexelFormat::kInvalid}, {"SSVVba16float", TexelFormat::kInvalid},
{"rgba8WWint", TexelFormat::kInvalid}, {"rgbRR6float", TexelFormat::kInvalid}, {"rFui9t", TexelFormat::kInvalid},
{"rgbasxxMt", TexelFormat::kInvalid}, {"r32int", TexelFormat::kInvalid}, {"VOORRHnt", TexelFormat::kInvalid},
{"rXba16ungg", TexelFormat::kInvalid}, {"r3siyt", TexelFormat::kInvalid}, {"lln3rrs77nt", TexelFormat::kInvalid},
{"rba1XuVt", TexelFormat::kInvalid}, {"r32s4n00", TexelFormat::kInvalid}, {"32ooat", TexelFormat::kInvalid},
{"rgba16uin3", TexelFormat::kInvalid}, {"r32fzzt", TexelFormat::kInvalid}, {"r3iippl1a", TexelFormat::kInvalid},
{"rgba16sinE", TexelFormat::kInvalid}, {"XXg32uint", TexelFormat::kInvalid}, {"rII39955nnnt", TexelFormat::kInvalid},
{"TTgba16sPPn", TexelFormat::kInvalid}, {"aagHH2uinYSS", TexelFormat::kInvalid}, {"rkk3it", TexelFormat::kInvalid},
{"rgbad6xxint", TexelFormat::kInvalid}, {"gj3sRRn", TexelFormat::kInvalid}, {"r3bsnt", TexelFormat::kInvalid},
{"rgba446float", TexelFormat::kInvalid}, {"rg32flojt", TexelFormat::kInvalid}, {"r32floa", TexelFormat::kInvalid},
{"SSVVba16float", TexelFormat::kInvalid}, {"rg32lot", TexelFormat::kInvalid}, {"rgb3uit", TexelFormat::kInvalid},
{"rgbRR6float", TexelFormat::kInvalid}, {"rgjj3uint", TexelFormat::kInvalid}, {"rgb2urnff", TexelFormat::kInvalid},
{"rFui9t", TexelFormat::kInvalid}, {"rgba32sijt", TexelFormat::kInvalid}, {"NNgba32ww2t", TexelFormat::kInvalid},
{"r32int", TexelFormat::kInvalid}, {"rgba32snt", TexelFormat::kInvalid}, {"rgba32rrloat", TexelFormat::kInvalid},
{"VOORRHnt", TexelFormat::kInvalid}, {"rgGa32float", TexelFormat::kInvalid}, {"FFgba32float", 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>; using TexelFormatParseTest = testing::TestWithParam<Case>;

View File

@ -23,9 +23,11 @@ import (
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"reflect" "reflect"
"regexp" "regexp"
"runtime"
"strconv" "strconv"
"strings" "strings"
"text/template" "text/template"
@ -72,6 +74,12 @@ optional flags:`)
func run() error { func run() error {
projectRoot := fileutils.ProjectRoot() 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 // Recursively find all the template files in the <tint>/src directory
files, err := glob.Scan(projectRoot, glob.MustParseConfig(`{ files, err := glob.Scan(projectRoot, glob.MustParseConfig(`{
"paths": [{"include": [ "paths": [{"include": [
@ -132,6 +140,15 @@ func run() error {
if body := sb.String(); body != "" { if body := sb.String(); body != "" {
_, tmplFileName := filepath.Split(tmplPath) _, tmplFileName := filepath.Split(tmplPath)
outFileName := strings.TrimSuffix(tmplFileName, ".tmpl") 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 { if err := writeFile(outFileName, body); err != nil {
return err return err
} }
@ -486,3 +503,37 @@ func pascalCase(s string) string {
} }
return b.String() 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 ""
}