Add TINT_SYMBOL_STORE_DEBUG_NAME option to help debugging the AST

If TINT_SYMBOL_STORE_DEBUG_NAME is 1, Symbol instances store a
`debug_name_` member initialized with the name of the identifier they
represent. This member is not exposed, but is useful for debugging
purposes.

Bug: tint:1331
Change-Id: Ia98e266aefc1ca26bbf30c6ece73d9eac8afdbd7
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/71780
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 400ccd2..fafeef3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -95,6 +95,8 @@
 
 option_if_not_defined(TINT_CHECK_CHROMIUM_STYLE "Check for [chromium-style] issues during build" OFF)
 
+option_if_not_defined(TINT_SYMBOL_STORE_DEBUG_NAME "Enable storing of name in tint::ast::Symbol to help debugging the AST" OFF)
+
 message(STATUS "Tint build samples: ${TINT_BUILD_SAMPLES}")
 message(STATUS "Tint build docs: ${TINT_BUILD_DOCS}")
 message(STATUS "Tint build docs with warn as error: ${TINT_DOCS_WARN_AS_ERROR}")
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index de4d2d4..4367306 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -559,6 +559,9 @@
 if (${COMPILER_IS_LIKE_GNU})
   target_compile_options(libtint PRIVATE -fvisibility=hidden)
 endif()
+if (${TINT_SYMBOL_STORE_DEBUG_NAME})
+    target_compile_definitions(libtint PUBLIC "TINT_SYMBOL_STORE_DEBUG_NAME=1")
+endif()
 set_target_properties(libtint PROPERTIES OUTPUT_NAME "tint")
 
 if (${TINT_BUILD_FUZZERS})
diff --git a/src/symbol.cc b/src/symbol.cc
index 9dc5726..19224ee 100644
--- a/src/symbol.cc
+++ b/src/symbol.cc
@@ -14,6 +14,8 @@
 
 #include "src/symbol.h"
 
+#include <utility>
+
 namespace tint {
 
 Symbol::Symbol() = default;
@@ -21,6 +23,11 @@
 Symbol::Symbol(uint32_t val, tint::ProgramID program_id)
     : val_(val), program_id_(program_id) {}
 
+#if TINT_SYMBOL_STORE_DEBUG_NAME
+Symbol::Symbol(uint32_t val, tint::ProgramID program_id, std::string debug_name)
+    : val_(val), program_id_(program_id), debug_name_(std::move(debug_name)) {}
+#endif
+
 Symbol::Symbol(const Symbol& o) = default;
 
 Symbol::Symbol(Symbol&& o) = default;
diff --git a/src/symbol.h b/src/symbol.h
index 4567ca8..51704ab 100644
--- a/src/symbol.h
+++ b/src/symbol.h
@@ -15,6 +15,13 @@
 #ifndef SRC_SYMBOL_H_
 #define SRC_SYMBOL_H_
 
+// If TINT_SYMBOL_STORE_DEBUG_NAME is 1, Symbol instances store a `debug_name_`
+// member initialized with the name of the identifier they represent. This
+// member is not exposed, but is useful for debugging purposes.
+#ifndef TINT_SYMBOL_STORE_DEBUG_NAME
+#define TINT_SYMBOL_STORE_DEBUG_NAME 0
+#endif
+
 #include <string>
 
 #include "src/program_id.h"
@@ -31,6 +38,13 @@
   /// @param val the symbol value
   /// @param program_id the identifier of the program that owns this Symbol
   Symbol(uint32_t val, tint::ProgramID program_id);
+#if TINT_SYMBOL_STORE_DEBUG_NAME
+  /// Constructor
+  /// @param val the symbol value
+  /// @param program_id the identifier of the program that owns this Symbol
+  /// @param debug_name name of symbols used only for debugging
+  Symbol(uint32_t val, tint::ProgramID program_id, std::string debug_name);
+#endif
   /// Copy constructor
   /// @param o the symbol to copy
   Symbol(const Symbol& o);
@@ -75,6 +89,9 @@
  private:
   uint32_t val_ = static_cast<uint32_t>(-1);
   tint::ProgramID program_id_;
+#if TINT_SYMBOL_STORE_DEBUG_NAME
+  std::string debug_name_;
+#endif
 };
 
 /// @param sym the Symbol
diff --git a/src/symbol_table.cc b/src/symbol_table.cc
index 330917d..07c7fa1 100644
--- a/src/symbol_table.cc
+++ b/src/symbol_table.cc
@@ -38,7 +38,11 @@
   if (it != name_to_symbol_.end())
     return it->second;
 
+#if TINT_SYMBOL_STORE_DEBUG_NAME
+  Symbol sym(next_symbol_, program_id_, name);
+#else
   Symbol sym(next_symbol_, program_id_);
+#endif
   ++next_symbol_;
 
   name_to_symbol_[name] = sym;