main: Print the available transforms when mistyped

Declare the list of transforms once, and use this for the usage message and when a transform does not match.
Simplifies adding new transforms to the command line tool.

Change-Id: I57ec433d8a570dae465d1837f5a4c1d89b8cff05
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/79280
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2022-02-08 00:09:26 +00:00 committed by Tint LUCI CQ
parent 85f7df55eb
commit c6d8cadd33
1 changed files with 49 additions and 17 deletions

View File

@ -30,6 +30,7 @@
#endif // TINT_BUILD_SPV_READER #endif // TINT_BUILD_SPV_READER
#include "src/utils/io/command.h" #include "src/utils/io/command.h"
#include "src/utils/string.h"
#include "src/val/val.h" #include "src/val/val.h"
#include "tint/tint.h" #include "tint/tint.h"
@ -103,10 +104,7 @@ const char kUsage[] = R"(Usage: tint [options] <input-file>
-o <name> -- Output file name. Use "-" for standard output -o <name> -- Output file name. Use "-" for standard output
--transform <name list> -- Runs transforms, name list is comma separated --transform <name list> -- Runs transforms, name list is comma separated
Available transforms: Available transforms:
first_index_offset ${transforms}
fold_trivial_single_use_lets
renamer
robustness
--parse-only -- Stop after parsing the input --parse-only -- Stop after parsing the input
--disable-workgroup-init -- Disable workgroup memory zero initialization. --disable-workgroup-init -- Disable workgroup memory zero initialization.
--demangle -- Preserve original source names. Demangle them. --demangle -- Preserve original source names. Demangle them.
@ -909,8 +907,43 @@ int main(int argc, const char** argv) {
return 1; return 1;
} }
struct TransformFactory {
const char* name;
std::function<void(tint::transform::Manager& manager,
tint::transform::DataMap& inputs)>
make;
};
std::vector<TransformFactory> transforms = {
{"first_index_offset",
[](tint::transform::Manager& m, tint::transform::DataMap& i) {
i.Add<tint::transform::FirstIndexOffset::BindingPoint>(0, 0);
m.Add<tint::transform::FirstIndexOffset>();
}},
{"fold_trivial_single_use_lets",
[](tint::transform::Manager& m, tint::transform::DataMap&) {
m.Add<tint::transform::FoldTrivialSingleUseLets>();
}},
{"renamer",
[](tint::transform::Manager& m, tint::transform::DataMap&) {
m.Add<tint::transform::Renamer>();
}},
{"robustness",
[](tint::transform::Manager& m, tint::transform::DataMap&) {
m.Add<tint::transform::Robustness>();
}},
};
auto transform_names = [&] {
std::stringstream names;
for (auto& t : transforms) {
names << " " << t.name << std::endl;
}
return names.str();
};
if (options.show_help) { if (options.show_help) {
std::cout << kUsage << std::endl; std::string usage =
tint::utils::ReplaceAll(kUsage, "${transforms}", transform_names());
std::cout << usage << std::endl;
return 0; return 0;
} }
@ -1043,18 +1076,17 @@ int main(int argc, const char** argv) {
// be run that needs user input. Should we find a way to support that here // be run that needs user input. Should we find a way to support that here
// maybe through a provided file? // maybe through a provided file?
if (name == "first_index_offset") { bool found = false;
transform_inputs.Add<tint::transform::FirstIndexOffset::BindingPoint>(0, for (auto& t : transforms) {
0); if (t.name == name) {
transform_manager.Add<tint::transform::FirstIndexOffset>(); t.make(transform_manager, transform_inputs);
} else if (name == "fold_trivial_single_use_lets") { found = true;
transform_manager.Add<tint::transform::FoldTrivialSingleUseLets>(); break;
} else if (name == "renamer") { }
transform_manager.Add<tint::transform::Renamer>(); }
} else if (name == "robustness") { if (!found) {
transform_manager.Add<tint::transform::Robustness>(); std::cerr << "Unknown transform: " << name << std::endl;
} else { std::cerr << "Available transforms: " << std::endl << transform_names();
std::cerr << "Unknown transform name: " << name << std::endl;
return 1; return 1;
} }
} }