diff options
author | Stephen Kelly <steveire@gmail.com> | 2019-01-08 22:32:48 +0000 |
---|---|---|
committer | Stephen Kelly <steveire@gmail.com> | 2019-01-08 22:32:48 +0000 |
commit | 63beca62d94c0442c16aeb7d176ae93f5c90017f (patch) | |
tree | 9259ba3fd777d15483938f268206d88a4fc51b77 /include | |
parent | 72d635358db729a936c80b9be7316faac848d36c (diff) | |
download | clang-63beca62d94c0442c16aeb7d176ae93f5c90017f.tar.gz |
Implement the TreeStructure interface through the TextNodeDumper
Summary:
This way, when the generic ASTTraverser is extracted from ASTDumper,
there can't be any problem related to ordering of class members, a
concern that was raised in https://reviews.llvm.org/D55337.
This will also preserve the property that the generic traverser does not
enforce any coupling between the NodeDumper and the TreeStructure.
https://godbolt.org/z/PEtT1_
Reviewers: aaron.ballman, erichkeane
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D56407
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@350665 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include')
-rw-r--r-- | include/clang/AST/ASTDumperUtils.h | 84 | ||||
-rw-r--r-- | include/clang/AST/TextNodeDumper.h | 87 |
2 files changed, 86 insertions, 85 deletions
diff --git a/include/clang/AST/ASTDumperUtils.h b/include/clang/AST/ASTDumperUtils.h index 129c40ce3e..71cf5c93bc 100644 --- a/include/clang/AST/ASTDumperUtils.h +++ b/include/clang/AST/ASTDumperUtils.h @@ -92,90 +92,6 @@ public: } }; -class TextTreeStructure { - raw_ostream &OS; - const bool ShowColors; - - /// Pending[i] is an action to dump an entity at level i. - llvm::SmallVector<std::function<void(bool isLastChild)>, 32> Pending; - - /// Indicates whether we're at the top level. - bool TopLevel = true; - - /// Indicates if we're handling the first child after entering a new depth. - bool FirstChild = true; - - /// Prefix for currently-being-dumped entity. - std::string Prefix; - -public: - /// Add a child of the current node. Calls doAddChild without arguments - template <typename Fn> void addChild(Fn doAddChild) { - // If we're at the top level, there's nothing interesting to do; just - // run the dumper. - if (TopLevel) { - TopLevel = false; - doAddChild(); - while (!Pending.empty()) { - Pending.back()(true); - Pending.pop_back(); - } - Prefix.clear(); - OS << "\n"; - TopLevel = true; - return; - } - - auto dumpWithIndent = [this, doAddChild](bool isLastChild) { - // Print out the appropriate tree structure and work out the prefix for - // children of this node. For instance: - // - // A Prefix = "" - // |-B Prefix = "| " - // | `-C Prefix = "| " - // `-D Prefix = " " - // |-E Prefix = " | " - // `-F Prefix = " " - // G Prefix = "" - // - // Note that the first level gets no prefix. - { - OS << '\n'; - ColorScope Color(OS, ShowColors, IndentColor); - OS << Prefix << (isLastChild ? '`' : '|') << '-'; - this->Prefix.push_back(isLastChild ? ' ' : '|'); - this->Prefix.push_back(' '); - } - - FirstChild = true; - unsigned Depth = Pending.size(); - - doAddChild(); - - // If any children are left, they're the last at their nesting level. - // Dump those ones out now. - while (Depth < Pending.size()) { - Pending.back()(true); - this->Pending.pop_back(); - } - - // Restore the old prefix. - this->Prefix.resize(Prefix.size() - 2); - }; - - if (FirstChild) { - Pending.push_back(std::move(dumpWithIndent)); - } else { - Pending.back()(false); - Pending.back() = std::move(dumpWithIndent); - } - FirstChild = false; - } - - TextTreeStructure(raw_ostream &OS, bool ShowColors) - : OS(OS), ShowColors(ShowColors) {} -}; - } // namespace clang #endif // LLVM_CLANG_AST_ASTDUMPERUTILS_H diff --git a/include/clang/AST/TextNodeDumper.h b/include/clang/AST/TextNodeDumper.h index 5a1dbb40fa..3634d1c41f 100644 --- a/include/clang/AST/TextNodeDumper.h +++ b/include/clang/AST/TextNodeDumper.h @@ -22,9 +22,94 @@ namespace clang { +class TextTreeStructure { + raw_ostream &OS; + const bool ShowColors; + + /// Pending[i] is an action to dump an entity at level i. + llvm::SmallVector<std::function<void(bool isLastChild)>, 32> Pending; + + /// Indicates whether we're at the top level. + bool TopLevel = true; + + /// Indicates if we're handling the first child after entering a new depth. + bool FirstChild = true; + + /// Prefix for currently-being-dumped entity. + std::string Prefix; + +public: + /// Add a child of the current node. Calls doAddChild without arguments + template <typename Fn> void addChild(Fn doAddChild) { + // If we're at the top level, there's nothing interesting to do; just + // run the dumper. + if (TopLevel) { + TopLevel = false; + doAddChild(); + while (!Pending.empty()) { + Pending.back()(true); + Pending.pop_back(); + } + Prefix.clear(); + OS << "\n"; + TopLevel = true; + return; + } + + auto dumpWithIndent = [this, doAddChild](bool isLastChild) { + // Print out the appropriate tree structure and work out the prefix for + // children of this node. For instance: + // + // A Prefix = "" + // |-B Prefix = "| " + // | `-C Prefix = "| " + // `-D Prefix = " " + // |-E Prefix = " | " + // `-F Prefix = " " + // G Prefix = "" + // + // Note that the first level gets no prefix. + { + OS << '\n'; + ColorScope Color(OS, ShowColors, IndentColor); + OS << Prefix << (isLastChild ? '`' : '|') << '-'; + this->Prefix.push_back(isLastChild ? ' ' : '|'); + this->Prefix.push_back(' '); + } + + FirstChild = true; + unsigned Depth = Pending.size(); + + doAddChild(); + + // If any children are left, they're the last at their nesting level. + // Dump those ones out now. + while (Depth < Pending.size()) { + Pending.back()(true); + this->Pending.pop_back(); + } + + // Restore the old prefix. + this->Prefix.resize(Prefix.size() - 2); + }; + + if (FirstChild) { + Pending.push_back(std::move(dumpWithIndent)); + } else { + Pending.back()(false); + Pending.back() = std::move(dumpWithIndent); + } + FirstChild = false; + } + + TextTreeStructure(raw_ostream &OS, bool ShowColors) + : OS(OS), ShowColors(ShowColors) {} +}; + class TextNodeDumper : public comments::ConstCommentVisitor<TextNodeDumper, void, - const comments::FullComment *> { + const comments::FullComment *>, + public TextTreeStructure { raw_ostream &OS; const bool ShowColors; |