From b2bbb14bd4133f2e67cc54f21ad9702bfc2254b3 Mon Sep 17 00:00:00 2001 From: Antonio Maiorano Date: Tue, 7 Dec 2021 14:43:17 +0000 Subject: [PATCH] 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 Commit-Queue: Antonio Maiorano Reviewed-by: Ben Clayton --- CMakeLists.txt | 2 ++ src/CMakeLists.txt | 3 +++ src/symbol.cc | 7 +++++++ src/symbol.h | 17 +++++++++++++++++ src/symbol_table.cc | 4 ++++ 5 files changed, 33 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 400ccd2ddb..fafeef3bbd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,6 +95,8 @@ option_if_not_defined(TINT_EMIT_COVERAGE "Emit code coverage information" OFF) 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 de4d2d4b66..4367306ee7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -559,6 +559,9 @@ tint_default_compile_options(libtint) 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 9dc5726908..19224eecf1 100644 --- a/src/symbol.cc +++ b/src/symbol.cc @@ -14,6 +14,8 @@ #include "src/symbol.h" +#include + namespace tint { Symbol::Symbol() = default; @@ -21,6 +23,11 @@ Symbol::Symbol() = default; 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 4567ca8568..51704ab8b7 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 #include "src/program_id.h" @@ -31,6 +38,13 @@ class Symbol { /// @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 @@ class Symbol { private: uint32_t val_ = static_cast(-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 330917dda1..07c7fa1beb 100644 --- a/src/symbol_table.cc +++ b/src/symbol_table.cc @@ -38,7 +38,11 @@ Symbol SymbolTable::Register(const std::string& name) { 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;