Add --demangle option to command example

The --demangle option preserves original source names
when dumping the AST or emitting text-based shader languages.

Change-Id: Iac450158c6b9f0ac780b7e5580cb02c41dd5df17
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/36480
Commit-Queue: David Neto <dneto@google.com>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Auto-Submit: David Neto <dneto@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
diff --git a/include/tint/tint.h b/include/tint/tint.h
index 592a178..e9e7c9d 100644
--- a/include/tint/tint.h
+++ b/include/tint/tint.h
@@ -20,6 +20,7 @@
 
 #include "src/ast/pipeline_stage.h"
 #include "src/ast/type_manager.h"
+#include "src/demangler.h"
 #include "src/diagnostic/printer.h"
 #include "src/inspector/inspector.h"
 #include "src/namer.h"
diff --git a/samples/main.cc b/samples/main.cc
index 1c4f9c9..21bf811 100644
--- a/samples/main.cc
+++ b/samples/main.cc
@@ -46,6 +46,7 @@
   bool parse_only = false;
   bool dump_ast = false;
   bool dawn_validation = false;
+  bool demangle = false;
 
   Format format = Format::kNone;
 
@@ -80,6 +81,8 @@
   --dump-ast                -- Dump the generated AST to stdout
   --dawn-validation         -- SPIRV outputs are validated with the same flags
                                as Dawn does. Has no effect on non-SPIRV outputs.
+  --demangle                -- Preserve original source names. Demangle them.
+                               Affects AST dumping, and text-based output languages.
   -h                        -- This help text)";
 
 #pragma clang diagnostic push
@@ -233,6 +236,8 @@
       opts->dump_ast = true;
     } else if (arg == "--dawn-validation") {
       opts->dawn_validation = true;
+    } else if (arg == "--demangle") {
+      opts->demangle = true;
     } else if (!arg.empty()) {
       if (arg[0] == '-') {
         std::cerr << "Unrecognized option: " << arg << std::endl;
@@ -496,7 +501,11 @@
   }
 
   if (options.dump_ast) {
-    std::cout << std::endl << mod.to_str() << std::endl;
+    auto ast_str = mod.to_str();
+    if (options.demangle) {
+      ast_str = tint::Demangler().Demangle(mod, ast_str);
+    }
+    std::cout << std::endl << ast_str << std::endl;
   }
   if (options.parse_only) {
     return 1;
@@ -622,7 +631,11 @@
 
   if (options.format != Format::kSpvAsm && options.format != Format::kSpirv) {
     auto* w = static_cast<tint::writer::Text*>(writer.get());
-    if (!WriteFile(options.output_file, "w", w->result())) {
+    auto output = w->result();
+    if (options.demangle) {
+      output = tint::Demangler().Demangle(mod, output);
+    }
+    if (!WriteFile(options.output_file, "w", output)) {
       return 1;
     }
   }