add ast::Module::to_str

This makes it easier to see the bug.

Bug: tint:22
Change-Id: Ic5acc0b8299ef31eb73b49863bc42ac09de6e9bf
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17203
Reviewed-by: dan sinclair <dsinclair@google.com>
This commit is contained in:
David Neto 2020-03-18 20:09:44 +00:00 committed by dan sinclair
parent 9d9d7cd29d
commit 0afb943ebf
2 changed files with 26 additions and 5 deletions

View File

@ -59,21 +59,24 @@ bool Module::IsValid() const {
std::string Module::to_str() const { std::string Module::to_str() const {
std::ostringstream out; std::ostringstream out;
out << "Module{" << std::endl;
const auto indent = 2;
for (const auto& import : imports_) { for (const auto& import : imports_) {
import->to_str(out, 0); import->to_str(out, indent);
} }
for (const auto& var : global_variables_) { for (const auto& var : global_variables_) {
var->to_str(out, 0); var->to_str(out, indent);
} }
for (const auto& ep : entry_points_) { for (const auto& ep : entry_points_) {
ep->to_str(out, 0); ep->to_str(out, indent);
} }
for (const auto& alias : alias_types_) { for (const auto& alias : alias_types_) {
out << alias->name() << " -> " << alias->type()->type_name() << std::endl; out << alias->name() << " -> " << alias->type()->type_name() << std::endl;
} }
for (const auto& func : functions_) { for (const auto& func : functions_) {
func->to_str(out, 0); func->to_str(out, indent);
} }
out << "}" << std::endl;
return out.str(); return out.str();
} }

View File

@ -14,9 +14,10 @@
#include "src/ast/module.h" #include "src/ast/module.h"
#include <sstream>
#include <utility> #include <utility>
#include "gtest/gtest.h" #include "gmock/gmock.h"
#include "src/ast/entry_point.h" #include "src/ast/entry_point.h"
#include "src/ast/function.h" #include "src/ast/function.h"
#include "src/ast/import.h" #include "src/ast/import.h"
@ -34,6 +35,13 @@ TEST_F(ModuleTest, Creation) {
EXPECT_EQ(m.imports().size(), 0); EXPECT_EQ(m.imports().size(), 0);
} }
TEST_F(ModuleTest, ToStrEmitsPreambleAndPostamble) {
Module m;
const auto str = m.to_str();
const auto expected = "Module{\n}\n";
EXPECT_EQ(str, expected);
}
TEST_F(ModuleTest, Imports) { TEST_F(ModuleTest, Imports) {
Module m; Module m;
@ -44,6 +52,16 @@ TEST_F(ModuleTest, Imports) {
EXPECT_EQ("std::glsl", m.imports()[0]->name()); EXPECT_EQ("std::glsl", m.imports()[0]->name());
} }
TEST_F(ModuleTest, ToStrWithImport) {
Module m;
m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl"));
const auto str = m.to_str();
EXPECT_EQ(str, R"(Module{
Import{"GLSL.std.430" as std::glsl}
}
)");
}
TEST_F(ModuleTest, LookupImport) { TEST_F(ModuleTest, LookupImport) {
Module m; Module m;