GN: Replace vector<ParseNode*> with vector<unique_ptr<ParseNode>> in parse_tree.h.

Chromium can now use std::unique_ptr.

BUG=602726

Review URL: https://codereview.chromium.org/1884503003

Cr-Original-Commit-Position: refs/heads/master@{#387958}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 6ce99c7025ebaca2747f659da3700a388f627919
diff --git a/tools/gn/command_format.cc b/tools/gn/command_format.cc
index a054b21..7b8f2f3 100644
--- a/tools/gn/command_format.cc
+++ b/tools/gn/command_format.cc
@@ -181,7 +181,7 @@
   // bracket.
   template <class PARSENODE>  // Just for const covariance.
   void Sequence(SequenceStyle style,
-                const std::vector<PARSENODE*>& list,
+                const std::vector<std::unique_ptr<PARSENODE>>& list,
                 const ParseNode* end,
                 bool force_multiline);
 
@@ -194,7 +194,7 @@
   void InitializeSub(Printer* sub);
 
   template <class PARSENODE>
-  bool ListWillBeMultiline(const std::vector<PARSENODE*>& list,
+  bool ListWillBeMultiline(const std::vector<std::unique_ptr<PARSENODE>>& list,
                            const ParseNode* end);
 
   std::string output_;           // Output buffer.
@@ -385,7 +385,7 @@
 
   size_t i = 0;
   for (const auto& stmt : block->statements()) {
-    Expr(stmt, kPrecedenceLowest, std::string());
+    Expr(stmt.get(), kPrecedenceLowest, std::string());
     Newline();
     if (stmt->comments()) {
       // Why are before() not printed here too? before() are handled inside
@@ -399,8 +399,8 @@
       }
     }
     if (i < block->statements().size() - 1 &&
-        (ShouldAddBlankLineInBetween(block->statements()[i],
-                                     block->statements()[i + 1]))) {
+        (ShouldAddBlankLineInBetween(block->statements()[i].get(),
+                                     block->statements()[i + 1].get()))) {
       Newline();
     }
     ++i;
@@ -648,7 +648,7 @@
 
 template <class PARSENODE>
 void Printer::Sequence(SequenceStyle style,
-                       const std::vector<PARSENODE*>& list,
+                       const std::vector<std::unique_ptr<PARSENODE>>& list,
                        const ParseNode* end,
                        bool force_multiline) {
   if (style == kSequenceStyleList)
@@ -665,7 +665,7 @@
     // No elements, and not forcing newlines, print nothing.
   } else if (list.size() == 1 && !force_multiline) {
     Print(" ");
-    Expr(list[0], kPrecedenceLowest, std::string());
+    Expr(list[0].get(), kPrecedenceLowest, std::string());
     CHECK(!list[0]->comments() || list[0]->comments()->after().empty());
     Print(" ");
   } else {
@@ -687,11 +687,11 @@
       bool body_of_list = i < list.size() - 1 || style == kSequenceStyleList;
       bool want_comma =
           body_of_list && (style == kSequenceStyleList && !x->AsBlockComment());
-      Expr(x, kPrecedenceLowest, want_comma ? "," : std::string());
+      Expr(x.get(), kPrecedenceLowest, want_comma ? "," : std::string());
       CHECK(!x->comments() || x->comments()->after().empty());
       if (body_of_list) {
         if (i < list.size() - 1 &&
-            ShouldAddBlankLineInBetween(list[i], list[i + 1]))
+            ShouldAddBlankLineInBetween(list[i].get(), list[i + 1].get()))
           Newline();
       }
       ++i;
@@ -734,7 +734,7 @@
   bool have_block = func_call->block() != nullptr;
   bool force_multiline = false;
 
-  const std::vector<const ParseNode*>& list = func_call->args()->contents();
+  const auto& list = func_call->args()->contents();
   const ParseNode* end = func_call->args()->End();
 
   if (end && end->comments() && !end->comments()->before().empty())
@@ -767,7 +767,7 @@
       IndentState(CurrentColumn(), continuation_requires_indent, false));
   int penalty_one_line = 0;
   for (size_t i = 0; i < list.size(); ++i) {
-    penalty_one_line += sub1.Expr(list[i], kPrecedenceLowest,
+    penalty_one_line += sub1.Expr(list[i].get(), kPrecedenceLowest,
                                   i < list.size() - 1 ? ", " : std::string());
   }
   sub1.Print(terminator);
@@ -785,8 +785,9 @@
       IndentState(CurrentColumn(), continuation_requires_indent, false));
   int penalty_multiline_start_same_line = 0;
   for (size_t i = 0; i < list.size(); ++i) {
-    penalty_multiline_start_same_line += sub2.Expr(
-        list[i], kPrecedenceLowest, i < list.size() - 1 ? "," : std::string());
+    penalty_multiline_start_same_line +=
+        sub2.Expr(list[i].get(), kPrecedenceLowest,
+                  i < list.size() - 1 ? "," : std::string());
     if (i < list.size() - 1) {
       sub2.Newline();
     }
@@ -807,8 +808,9 @@
           std::abs(sub3.CurrentColumn() - start_column) *
           kPenaltyHorizontalSeparation;
     }
-    penalty_multiline_start_next_line += sub3.Expr(
-        list[i], kPrecedenceLowest, i < list.size() - 1 ? "," : std::string());
+    penalty_multiline_start_next_line +=
+        sub3.Expr(list[i].get(), kPrecedenceLowest,
+                  i < list.size() - 1 ? "," : std::string());
     if (i < list.size() - 1) {
       sub3.Newline();
     }
@@ -852,7 +854,7 @@
           Newline();
       }
       bool want_comma = i < list.size() - 1 && !x->AsBlockComment();
-      Expr(x, kPrecedenceLowest, want_comma ? "," : std::string());
+      Expr(x.get(), kPrecedenceLowest, want_comma ? "," : std::string());
       CHECK(!x->comments() || x->comments()->after().empty());
       if (i < list.size() - 1) {
         if (!want_comma)
@@ -900,8 +902,9 @@
 }
 
 template <class PARSENODE>
-bool Printer::ListWillBeMultiline(const std::vector<PARSENODE*>& list,
-                                  const ParseNode* end) {
+bool Printer::ListWillBeMultiline(
+    const std::vector<std::unique_ptr<PARSENODE>>& list,
+    const ParseNode* end) {
   if (list.size() > 1)
     return true;
 
diff --git a/tools/gn/function_foreach.cc b/tools/gn/function_foreach.cc
index 549ebe4..b74fc18 100644
--- a/tools/gn/function_foreach.cc
+++ b/tools/gn/function_foreach.cc
@@ -47,7 +47,7 @@
                  const FunctionCallNode* function,
                  const ListNode* args_list,
                  Err* err) {
-  const std::vector<const ParseNode*>& args_vector = args_list->contents();
+  const auto& args_vector = args_list->contents();
   if (args_vector.size() != 2) {
     *err = Err(function, "Wrong number of arguments to foreach().",
                "Expecting exactly two.");
@@ -57,7 +57,8 @@
   // Extract the loop variable.
   const IdentifierNode* identifier = args_vector[0]->AsIdentifier();
   if (!identifier) {
-    *err = Err(args_vector[0], "Expected an identifier for the loop var.");
+    *err =
+        Err(args_vector[0].get(), "Expected an identifier for the loop var.");
     return Value();
   }
   base::StringPiece loop_var(identifier->value().value());
@@ -69,7 +70,7 @@
   if (list_identifier) {
     list_value = scope->GetValue(list_identifier->value().value(), true);
     if (!list_value) {
-      *err = Err(args_vector[1], "Undefined identifier.");
+      *err = Err(args_vector[1].get(), "Undefined identifier.");
       return Value();
     }
   } else {
diff --git a/tools/gn/function_forward_variables_from.cc b/tools/gn/function_forward_variables_from.cc
index ee14128..dd3629b 100644
--- a/tools/gn/function_forward_variables_from.cc
+++ b/tools/gn/function_forward_variables_from.cc
@@ -153,7 +153,7 @@
                               const FunctionCallNode* function,
                               const ListNode* args_list,
                               Err* err) {
-  const std::vector<const ParseNode*>& args_vector = args_list->contents();
+  const auto& args_vector = args_list->contents();
   if (args_vector.size() != 2 && args_vector.size() != 3) {
     *err = Err(function, "Wrong number of arguments.",
                "Expecting two or three arguments.");
@@ -166,7 +166,7 @@
   // to execute the ParseNode and get the value out if it's not an identifer.
   const IdentifierNode* identifier = args_vector[0]->AsIdentifier();
   if (!identifier) {
-    *err = Err(args_vector[0], "Expected an identifier for the scope.");
+    *err = Err(args_vector[0].get(), "Expected an identifier for the scope.");
     return Value();
   }
 
diff --git a/tools/gn/functions.cc b/tools/gn/functions.cc
index f6d405d..af413d6 100644
--- a/tools/gn/functions.cc
+++ b/tools/gn/functions.cc
@@ -464,7 +464,7 @@
                  const FunctionCallNode* function,
                  const ListNode* args_list,
                  Err* err) {
-  const std::vector<const ParseNode*>& args_vector = args_list->contents();
+  const auto& args_vector = args_list->contents();
   if (args_vector.size() != 1) {
     *err = Err(function, "Wrong number of arguments to defined().",
                "Expecting exactly one.");
diff --git a/tools/gn/parse_tree.cc b/tools/gn/parse_tree.cc
index 167531c..dc80514 100644
--- a/tools/gn/parse_tree.cc
+++ b/tools/gn/parse_tree.cc
@@ -297,7 +297,6 @@
 }
 
 BlockNode::~BlockNode() {
-  STLDeleteContainerPointers(statements_.begin(), statements_.end());
 }
 
 const BlockNode* BlockNode::AsBlock() const {
@@ -307,7 +306,7 @@
 Value BlockNode::Execute(Scope* scope, Err* err) const {
   for (size_t i = 0; i < statements_.size() && !err->has_error(); i++) {
     // Check for trying to execute things with no side effects in a block.
-    const ParseNode* cur = statements_[i];
+    const ParseNode* cur = statements_[i].get();
     if (cur->AsList() || cur->AsLiteral() || cur->AsUnaryOp() ||
         cur->AsIdentifier()) {
       *err = cur->MakeErrorDescribing(
@@ -492,7 +491,6 @@
 }
 
 ListNode::~ListNode() {
-  STLDeleteContainerPointers(contents_.begin(), contents_.end());
 }
 
 const ListNode* ListNode::AsList() const {
@@ -547,7 +545,7 @@
     bool skip = false;
     for (size_t i = sr.begin; i != sr.end; ++i) {
       // Bails out if any of the nodes are unsupported.
-      const ParseNode* node = contents_[i];
+      const ParseNode* node = contents_[i].get();
       if (!node->AsLiteral() && !node->AsIdentifier() && !node->AsAccessor()) {
         skip = true;
         continue;
@@ -561,15 +559,19 @@
     // to determine whether two nodes were initially separated by a blank line
     // or not.
     int start_line = contents_[sr.begin]->GetRange().begin().line_number();
-    const ParseNode* original_first = contents_[sr.begin];
+    const ParseNode* original_first = contents_[sr.begin].get();
     std::sort(contents_.begin() + sr.begin, contents_.begin() + sr.end,
-              comparator);
+              [&comparator](const std::unique_ptr<const ParseNode>& a,
+                            const std::unique_ptr<const ParseNode>& b) {
+                return comparator(a.get(), b.get());
+              });
     // If the beginning of the range had before comments, and the first node
     // moved during the sort, then move its comments to the new head of the
     // range.
-    if (original_first->comments() && contents_[sr.begin] != original_first) {
+    if (original_first->comments() &&
+        contents_[sr.begin].get() != original_first) {
       for (const auto& hc : original_first->comments()->before()) {
-        const_cast<ParseNode*>(contents_[sr.begin])
+        const_cast<ParseNode*>(contents_[sr.begin].get())
             ->comments_mutable()
             ->append_before(hc);
       }
@@ -579,7 +581,7 @@
     }
     const ParseNode* prev = nullptr;
     for (size_t i = sr.begin; i != sr.end; ++i) {
-      const ParseNode* node = contents_[i];
+      const ParseNode* node = contents_[i].get();
       DCHECK(node->AsLiteral() || node->AsIdentifier() || node->AsAccessor());
       int line_number =
           prev ? prev->GetRange().end().line_number() + 1 : start_line;
@@ -627,8 +629,8 @@
   std::vector<SortRange> ranges;
   const ParseNode* prev = nullptr;
   size_t begin = 0;
-  for (size_t i = begin; i < contents_.size(); prev = contents_[i++]) {
-    if (IsSortRangeSeparator(contents_[i], prev)) {
+  for (size_t i = begin; i < contents_.size(); prev = contents_[i++].get()) {
+    if (IsSortRangeSeparator(contents_[i].get(), prev)) {
       if (i > begin) {
         ranges.push_back(SortRange(begin, i));
         // If |i| is an item with an attached comment, then we start the next
diff --git a/tools/gn/parse_tree.h b/tools/gn/parse_tree.h
index 2853799..48313be 100644
--- a/tools/gn/parse_tree.h
+++ b/tools/gn/parse_tree.h
@@ -227,9 +227,11 @@
   void set_end(std::unique_ptr<EndNode> e) { end_ = std::move(e); }
   const EndNode* End() const { return end_.get(); }
 
-  const std::vector<ParseNode*>& statements() const { return statements_; }
+  const std::vector<std::unique_ptr<ParseNode>>& statements() const {
+    return statements_;
+  }
   void append_statement(std::unique_ptr<ParseNode> s) {
-    statements_.push_back(s.release());
+    statements_.push_back(std::move(s));
   }
 
  private:
@@ -238,8 +240,7 @@
   Token begin_token_;
   std::unique_ptr<EndNode> end_;
 
-  // Owning pointers, use unique_ptr when we can use C++11.
-  std::vector<ParseNode*> statements_;
+  std::vector<std::unique_ptr<ParseNode>> statements_;
 
   DISALLOW_COPY_AND_ASSIGN(BlockNode);
 };
@@ -364,9 +365,11 @@
   const EndNode* End() const { return end_.get(); }
 
   void append_item(std::unique_ptr<ParseNode> s) {
-    contents_.push_back(s.release());
+    contents_.push_back(std::move(s));
   }
-  const std::vector<const ParseNode*>& contents() const { return contents_; }
+  const std::vector<std::unique_ptr<const ParseNode>>& contents() const {
+    return contents_;
+  }
 
   void SortAsStringsList();
   void SortAsDepsList();
@@ -397,8 +400,7 @@
   std::unique_ptr<EndNode> end_;
   bool prefer_multiline_;
 
-  // Owning pointers, use unique_ptr when we can use C++11.
-  std::vector<const ParseNode*> contents_;
+  std::vector<std::unique_ptr<const ParseNode>> contents_;
 
   DISALLOW_COPY_AND_ASSIGN(ListNode);
 };
diff --git a/tools/gn/parser.cc b/tools/gn/parser.cc
index 33abfd0..977ddad 100644
--- a/tools/gn/parser.cc
+++ b/tools/gn/parser.cc
@@ -671,7 +671,7 @@
       TraverseOrder(binop->right(), pre, post);
     } else if (const BlockNode* block = root->AsBlock()) {
       for (const auto& statement : block->statements())
-        TraverseOrder(statement, pre, post);
+        TraverseOrder(statement.get(), pre, post);
       TraverseOrder(block->End(), pre, post);
     } else if (const ConditionNode* condition = root->AsConditionNode()) {
       TraverseOrder(condition->condition(), pre, post);
@@ -684,7 +684,7 @@
       // Nothing.
     } else if (const ListNode* list = root->AsList()) {
       for (const auto& node : list->contents())
-        TraverseOrder(node, pre, post);
+        TraverseOrder(node.get(), pre, post);
       TraverseOrder(list->End(), pre, post);
     } else if (root->AsLiteral()) {
       // Nothing.