From: Ilya Biryukov Date: Tue, 9 Jul 2019 13:31:43 +0000 (+0000) Subject: [Syntax] Move roles into a separate enum X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bce07a85e14d248967867d046524ab55aefda394;p=clang [Syntax] Move roles into a separate enum To align with reviewer's suggestions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@365479 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Tooling/Syntax/Nodes.h b/include/clang/Tooling/Syntax/Nodes.h index d4bab5cb1e..d20c7cb7b1 100644 --- a/include/clang/Tooling/Syntax/Nodes.h +++ b/include/clang/Tooling/Syntax/Nodes.h @@ -31,6 +31,18 @@ enum class NodeKind : uint16_t { /// For debugging purposes. llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, NodeKind K); +/// A relation between a parent and child node. Used for implementing accessors. +enum class NodeRole : uint8_t { + // A node without a parent. + Detached, + // Children of an unknown semantic nature, e.g. skipped tokens, comments. + Unknown, + // FIXME: should this be shared for all other nodes with braces, e.g. init + // lists? + CompoundStatement_lbrace, + CompoundStatement_rbrace +}; + /// A root node for a translation unit. Parent is always null. class TranslationUnit final : public Tree { public: @@ -73,11 +85,6 @@ public: } syntax::Leaf *lbrace(); syntax::Leaf *rbrace(); - - struct Roles { - static constexpr NodeRole lbrace = 1; - static constexpr NodeRole rbrace = 2; - }; }; } // namespace syntax diff --git a/include/clang/Tooling/Syntax/Tree.h b/include/clang/Tooling/Syntax/Tree.h index f7ce8a66f4..b66777561a 100644 --- a/include/clang/Tooling/Syntax/Tree.h +++ b/include/clang/Tooling/Syntax/Tree.h @@ -66,17 +66,7 @@ private: class Tree; class TreeBuilder; enum class NodeKind : uint16_t; - -/// Represents a relation of this node to its parent, e.g. 'lbrace inside a -/// compound statement'. -/// -/// Each node type defines a set of roles for its children. -using NodeRole = uint8_t; - -/// Role for detached nodes, i.e. the ones that do not have parent nodes. -constexpr NodeRole NodeRoleDetached = 0; -/// Role for children of unknown semantic nature, e.g. skipped tokens, comments. -constexpr NodeRole NodeRoleUnknown = 255; +enum class NodeRole : uint8_t; /// A node in a syntax tree. Each node is either a Leaf (representing tokens) or /// a Tree (representing language constructrs). @@ -84,12 +74,10 @@ class Node { public: /// Newly created nodes are detached from a tree, parent and sibling links are /// set when the node is added as a child to another one. - Node(NodeKind Kind) - : Parent(nullptr), NextSibling(nullptr), - Kind(static_cast(Kind)), Role(NodeRoleDetached) {} + Node(NodeKind Kind); NodeKind kind() const { return static_cast(Kind); } - NodeRole role() const { return Role; } + NodeRole role() const { return static_cast(Role); } const Tree *parent() const { return Parent; } Tree *parent() { return Parent; } diff --git a/lib/Tooling/Syntax/BuildTree.cpp b/lib/Tooling/Syntax/BuildTree.cpp index dc682ff677..03c439c59e 100644 --- a/lib/Tooling/Syntax/BuildTree.cpp +++ b/lib/Tooling/Syntax/BuildTree.cpp @@ -170,7 +170,7 @@ private: /// A with a role that should be assigned to it when adding to a parent. struct NodeAndRole { explicit NodeAndRole(syntax::Node *Node) - : Node(Node), Role(NodeRoleUnknown) {} + : Node(Node), Role(NodeRole::Unknown) {} syntax::Node *Node; NodeRole Role; @@ -221,10 +221,12 @@ public: } bool WalkUpFromCompoundStmt(CompoundStmt *S) { - using Roles = syntax::CompoundStatement::Roles; + using NodeRole = syntax::NodeRole; - Builder.markChildToken(S->getLBracLoc(), tok::l_brace, Roles::lbrace); - Builder.markChildToken(S->getRBracLoc(), tok::r_brace, Roles::rbrace); + Builder.markChildToken(S->getLBracLoc(), tok::l_brace, + NodeRole::CompoundStatement_lbrace); + Builder.markChildToken(S->getRBracLoc(), tok::r_brace, + NodeRole::CompoundStatement_rbrace); Builder.foldNode(Builder.getRange(S), new (allocator()) syntax::CompoundStatement); diff --git a/lib/Tooling/Syntax/Nodes.cpp b/lib/Tooling/Syntax/Nodes.cpp index 5dd793ccc2..061ed73bbe 100644 --- a/lib/Tooling/Syntax/Nodes.cpp +++ b/lib/Tooling/Syntax/Nodes.cpp @@ -25,9 +25,11 @@ llvm::raw_ostream &syntax::operator<<(llvm::raw_ostream &OS, NodeKind K) { } syntax::Leaf *syntax::CompoundStatement::lbrace() { - return llvm::cast_or_null(findChild(Roles::lbrace)); + return llvm::cast_or_null( + findChild(NodeRole::CompoundStatement_lbrace)); } syntax::Leaf *syntax::CompoundStatement::rbrace() { - return llvm::cast_or_null(findChild(Roles::rbrace)); + return llvm::cast_or_null( + findChild(NodeRole::CompoundStatement_rbrace)); } diff --git a/lib/Tooling/Syntax/Tree.cpp b/lib/Tooling/Syntax/Tree.cpp index fb7645786c..1549b6724f 100644 --- a/lib/Tooling/Syntax/Tree.cpp +++ b/lib/Tooling/Syntax/Tree.cpp @@ -38,17 +38,21 @@ bool syntax::Leaf::classof(const Node *N) { return N->kind() == NodeKind::Leaf; } +syntax::Node::Node(NodeKind Kind) + : Parent(nullptr), NextSibling(nullptr), Kind(static_cast(Kind)), + Role(static_cast(NodeRole::Detached)) {} + bool syntax::Tree::classof(const Node *N) { return N->kind() > NodeKind::Leaf; } void syntax::Tree::prependChildLowLevel(Node *Child, NodeRole Role) { assert(Child->Parent == nullptr); assert(Child->NextSibling == nullptr); - assert(Child->Role == NodeRoleDetached); - assert(Role != NodeRoleDetached); + assert(Child->role() == NodeRole::Detached); + assert(Role != NodeRole::Detached); Child->Parent = this; Child->NextSibling = this->FirstChild; - Child->Role = Role; + Child->Role = static_cast(Role); this->FirstChild = Child; } @@ -81,9 +85,9 @@ static void dumpTokens(llvm::raw_ostream &OS, ArrayRef Tokens, static void dumpTree(llvm::raw_ostream &OS, const syntax::Node *N, const syntax::Arena &A, std::vector IndentMask) { - if (N->role() != syntax::NodeRoleUnknown) { + if (N->role() != syntax::NodeRole::Unknown) { // FIXME: print the symbolic name of a role. - if (N->role() == syntax::NodeRoleDetached) + if (N->role() == syntax::NodeRole::Detached) OS << "*: "; else OS << static_cast(N->role()) << ": "; @@ -138,7 +142,7 @@ std::string syntax::Node::dumpTokens(const Arena &A) const { syntax::Node *syntax::Tree::findChild(NodeRole R) { for (auto *C = FirstChild; C; C = C->nextSibling()) { - if (C->Role == R) + if (C->role() == R) return C; } return nullptr; diff --git a/unittests/Tooling/Syntax/TreeTest.cpp b/unittests/Tooling/Syntax/TreeTest.cpp index 5ce00de306..1c42d2965a 100644 --- a/unittests/Tooling/Syntax/TreeTest.cpp +++ b/unittests/Tooling/Syntax/TreeTest.cpp @@ -136,16 +136,16 @@ void foo() {} | |-( | |-) | `-CompoundStatement -| |-1: { -| `-2: } +| |-2: { +| `-3: } |-TopLevelDeclaration | |-void | |-foo | |-( | |-) | `-CompoundStatement -| |-1: { -| `-2: } +| |-2: { +| `-3: } `- )txt"}, };