Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(552)

Side by Side Diff: tools/gn/parser.cc

Issue 2271383003: gn: Don't read unitialized memory on files containing just a comment. (Closed)
Patch Set: _EQ Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "tools/gn/parser.h" 5 #include "tools/gn/parser.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 } 446 }
447 left = (this->*infix)(std::move(left), token); 447 left = (this->*infix)(std::move(left), token);
448 if (has_error()) 448 if (has_error())
449 return std::unique_ptr<ParseNode>(); 449 return std::unique_ptr<ParseNode>();
450 } 450 }
451 451
452 return left; 452 return left;
453 } 453 }
454 454
455 std::unique_ptr<ParseNode> Parser::Block(Token token) { 455 std::unique_ptr<ParseNode> Parser::Block(Token token) {
456 // This entrypoing into ParseBlock means its part of an expression and we 456 // This entrypoint into ParseBlock means it's part of an expression and we
457 // always want the result. 457 // always want the result.
458 return ParseBlock(token, BlockNode::RETURNS_SCOPE); 458 return ParseBlock(token, BlockNode::RETURNS_SCOPE);
459 } 459 }
460 460
461 std::unique_ptr<ParseNode> Parser::Literal(Token token) { 461 std::unique_ptr<ParseNode> Parser::Literal(Token token) {
462 return base::WrapUnique(new LiteralNode(token)); 462 return base::WrapUnique(new LiteralNode(token));
463 } 463 }
464 464
465 std::unique_ptr<ParseNode> Parser::Name(Token token) { 465 std::unique_ptr<ParseNode> Parser::Name(Token token) {
466 return IdentifierOrCall(std::unique_ptr<ParseNode>(), token); 466 return IdentifierOrCall(std::unique_ptr<ParseNode>(), token);
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
812 void Parser::AssignComments(ParseNode* file) { 812 void Parser::AssignComments(ParseNode* file) {
813 // Start by generating a pre- and post- order traversal of the tree so we 813 // Start by generating a pre- and post- order traversal of the tree so we
814 // can determine what's before and after comments. 814 // can determine what's before and after comments.
815 std::vector<const ParseNode*> pre; 815 std::vector<const ParseNode*> pre;
816 std::vector<const ParseNode*> post; 816 std::vector<const ParseNode*> post;
817 TraverseOrder(file, &pre, &post); 817 TraverseOrder(file, &pre, &post);
818 818
819 // Assign line comments to syntax immediately following. 819 // Assign line comments to syntax immediately following.
820 int cur_comment = 0; 820 int cur_comment = 0;
821 for (auto* node : pre) { 821 for (auto* node : pre) {
822 if (node->GetRange().is_null()) {
823 CHECK_EQ(node, file) << "Only expected on top file node";
824 continue;
825 }
822 const Location& start = node->GetRange().begin(); 826 const Location& start = node->GetRange().begin();
823 while (cur_comment < static_cast<int>(line_comment_tokens_.size())) { 827 while (cur_comment < static_cast<int>(line_comment_tokens_.size())) {
824 if (start.byte() >= line_comment_tokens_[cur_comment].location().byte()) { 828 if (start.byte() >= line_comment_tokens_[cur_comment].location().byte()) {
825 const_cast<ParseNode*>(node)->comments_mutable()->append_before( 829 const_cast<ParseNode*>(node)->comments_mutable()->append_before(
826 line_comment_tokens_[cur_comment]); 830 line_comment_tokens_[cur_comment]);
827 ++cur_comment; 831 ++cur_comment;
828 } else { 832 } else {
829 break; 833 break;
830 } 834 }
831 } 835 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 break; 872 break;
869 } 873 }
870 } 874 }
871 875
872 // Suffix comments were assigned in reverse, so if there were multiple on 876 // Suffix comments were assigned in reverse, so if there were multiple on
873 // the same node, they need to be reversed. 877 // the same node, they need to be reversed.
874 if ((*i)->comments() && !(*i)->comments()->suffix().empty()) 878 if ((*i)->comments() && !(*i)->comments()->suffix().empty())
875 const_cast<ParseNode*>(*i)->comments_mutable()->ReverseSuffix(); 879 const_cast<ParseNode*>(*i)->comments_mutable()->ReverseSuffix();
876 } 880 }
877 } 881 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698