From: Ilya Biryukov Date: Tue, 6 Aug 2019 17:07:58 +0000 (+0000) Subject: [Syntax] Do not add a node for 'eof' into the tree X-Git-Tag: llvmorg-11-init~12601 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bfbf6b6cab9bc4e51bae95e2665bfa1aacdbe174;p=platform%2Fupstream%2Fllvm.git [Syntax] Do not add a node for 'eof' into the tree Summary: While useful as a sentinel value when iterating over tokens, having 'eof' in the tree, seems to do more harm than good. Reviewers: sammccall Reviewed By: sammccall Subscribers: javed.absar, kristof.beyls, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64576 llvm-svn: 368062 --- diff --git a/clang/lib/Tooling/Syntax/BuildTree.cpp b/clang/lib/Tooling/Syntax/BuildTree.cpp index 03c439c..a0b653d 100644 --- a/clang/lib/Tooling/Syntax/BuildTree.cpp +++ b/clang/lib/Tooling/Syntax/BuildTree.cpp @@ -58,8 +58,11 @@ public: /// Finish building the tree and consume the root node. syntax::TranslationUnit *finalize() && { auto Tokens = Arena.tokenBuffer().expandedTokens(); + assert(!Tokens.empty()); + assert(Tokens.back().kind() == tok::eof); + // Build the root of the tree, consuming all the children. - Pending.foldChildren(Tokens, + Pending.foldChildren(Tokens.drop_back(), new (Arena.allocator()) syntax::TranslationUnit); return cast(std::move(Pending).finalize()); @@ -96,10 +99,11 @@ private: /// Ensures that added nodes properly nest and cover the whole token stream. struct Forest { Forest(syntax::Arena &A) { - // FIXME: do not add 'eof' to the tree. - + assert(!A.tokenBuffer().expandedTokens().empty()); + assert(A.tokenBuffer().expandedTokens().back().kind() == tok::eof); // Create all leaf nodes. - for (auto &T : A.tokenBuffer().expandedTokens()) + // Note that we do not have 'eof' in the tree. + for (auto &T : A.tokenBuffer().expandedTokens().drop_back()) Trees.insert(Trees.end(), {&T, NodeAndRole{new (A.allocator()) syntax::Leaf(&T)}}); } diff --git a/clang/unittests/Tooling/Syntax/TreeTest.cpp b/clang/unittests/Tooling/Syntax/TreeTest.cpp index 1c42d29..e83d2b72 100644 --- a/clang/unittests/Tooling/Syntax/TreeTest.cpp +++ b/clang/unittests/Tooling/Syntax/TreeTest.cpp @@ -138,15 +138,14 @@ void foo() {} | `-CompoundStatement | |-2: { | `-3: } -|-TopLevelDeclaration -| |-void -| |-foo -| |-( -| |-) -| `-CompoundStatement -| |-2: { -| `-3: } -`- +`-TopLevelDeclaration + |-void + |-foo + |-( + |-) + `-CompoundStatement + |-2: { + `-3: } )txt"}, };