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

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

Issue 1496653002: Sort "deps" and "public_deps" when running the "gn format" command. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments & add some test cases Created 5 years 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 | « tools/gn/format_test_data/063.golden ('k') | 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/parse_tree.h" 5 #include "tools/gn/parse_tree.h"
6 6
7 #include <string> 7 #include <string>
8 #include <tuple> 8 #include <tuple>
9 9
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "tools/gn/functions.h" 12 #include "tools/gn/functions.h"
13 #include "tools/gn/operators.h" 13 #include "tools/gn/operators.h"
14 #include "tools/gn/scope.h" 14 #include "tools/gn/scope.h"
15 #include "tools/gn/string_utils.h" 15 #include "tools/gn/string_utils.h"
16 16
17 namespace { 17 namespace {
18 18
19 enum DepsCategory {
20 DEPS_CATEGORY_LOCAL,
21 DEPS_CATEGORY_RELATIVE,
22 DEPS_CATEGORY_ABSOLUTE,
23 DEPS_CATEGORY_OTHER,
24 };
25
26 DepsCategory GetDepsCategory(base::StringPiece deps) {
27 if (deps.length() < 2 || deps[0] != '"' || deps[deps.size() - 1] != '"')
28 return DEPS_CATEGORY_OTHER;
29
30 if (deps[1] == ':')
31 return DEPS_CATEGORY_LOCAL;
32
33 if (deps[1] == '/')
34 return DEPS_CATEGORY_ABSOLUTE;
35
36 return DEPS_CATEGORY_RELATIVE;
37 }
38
19 std::tuple<base::StringPiece, base::StringPiece> SplitAtFirst( 39 std::tuple<base::StringPiece, base::StringPiece> SplitAtFirst(
20 base::StringPiece str, 40 base::StringPiece str,
21 char c) { 41 char c) {
22 if (!str.starts_with("\"") || !str.ends_with("\"")) 42 if (!str.starts_with("\"") || !str.ends_with("\""))
23 return std::make_tuple(str, base::StringPiece()); 43 return std::make_tuple(str, base::StringPiece());
24 44
25 str = str.substr(1, str.length() - 2); 45 str = str.substr(1, str.length() - 2);
26 size_t index_of_first = str.find(c); 46 size_t index_of_first = str.find(c);
27 return std::make_tuple(str.substr(0, index_of_first), 47 return std::make_tuple(str.substr(0, index_of_first),
28 index_of_first != base::StringPiece::npos 48 index_of_first != base::StringPiece::npos
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 return astr < bstr; 603 return astr < bstr;
584 }); 604 });
585 } 605 }
586 606
587 void ListNode::SortAsDepsList() { 607 void ListNode::SortAsDepsList() {
588 // Sorts first relative targets, then absolute, each group is sorted 608 // Sorts first relative targets, then absolute, each group is sorted
589 // alphabetically. 609 // alphabetically.
590 SortList([](const ParseNode* a, const ParseNode* b) { 610 SortList([](const ParseNode* a, const ParseNode* b) {
591 base::StringPiece astr = GetStringRepresentation(a); 611 base::StringPiece astr = GetStringRepresentation(a);
592 base::StringPiece bstr = GetStringRepresentation(b); 612 base::StringPiece bstr = GetStringRepresentation(b);
593 return SplitAtFirst(astr, ':') < SplitAtFirst(bstr, ':'); 613 return std::make_pair(GetDepsCategory(astr), SplitAtFirst(astr, ':')) <
614 std::make_pair(GetDepsCategory(bstr), SplitAtFirst(bstr, ':'));
594 }); 615 });
595 } 616 }
596 617
597 // Breaks the ParseNodes of |contents| up by ranges that should be separately 618 // Breaks the ParseNodes of |contents| up by ranges that should be separately
598 // sorted. In particular, we break at a block comment, or an item that has an 619 // sorted. In particular, we break at a block comment, or an item that has an
599 // attached "before" comment and is separated by a blank line from the item 620 // attached "before" comment and is separated by a blank line from the item
600 // before it. The assumption is that both of these indicate a separate 'section' 621 // before it. The assumption is that both of these indicate a separate 'section'
601 // of a sources block across which items should not be inter-sorted. 622 // of a sources block across which items should not be inter-sorted.
602 std::vector<ListNode::SortRange> ListNode::GetSortRanges() const { 623 std::vector<ListNode::SortRange> ListNode::GetSortRanges() const {
603 std::vector<SortRange> ranges; 624 std::vector<SortRange> ranges;
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
807 828
808 Err EndNode::MakeErrorDescribing(const std::string& msg, 829 Err EndNode::MakeErrorDescribing(const std::string& msg,
809 const std::string& help) const { 830 const std::string& help) const {
810 return Err(value_, msg, help); 831 return Err(value_, msg, help);
811 } 832 }
812 833
813 void EndNode::Print(std::ostream& out, int indent) const { 834 void EndNode::Print(std::ostream& out, int indent) const {
814 out << IndentFor(indent) << "END(" << value_.value() << ")\n"; 835 out << IndentFor(indent) << "END(" << value_.value() << ")\n";
815 PrintComments(out, indent); 836 PrintComments(out, indent);
816 } 837 }
OLDNEW
« no previous file with comments | « tools/gn/format_test_data/063.golden ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698