OLD | NEW |
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/location.h" | 5 #include "tools/gn/location.h" |
6 | 6 |
7 #include <tuple> | 7 #include <tuple> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "tools/gn/input_file.h" | 11 #include "tools/gn/input_file.h" |
12 | 12 |
13 Location::Location() | 13 Location::Location() |
14 : file_(nullptr), | 14 : file_(nullptr), |
15 line_number_(-1), | 15 line_number_(-1), |
16 char_offset_(-1) { | 16 column_number_(-1) { |
17 } | 17 } |
18 | 18 |
19 Location::Location(const InputFile* file, | 19 Location::Location(const InputFile* file, |
20 int line_number, | 20 int line_number, |
21 int char_offset, | 21 int column_number, |
22 int byte) | 22 int byte) |
23 : file_(file), | 23 : file_(file), |
24 line_number_(line_number), | 24 line_number_(line_number), |
25 char_offset_(char_offset), | 25 column_number_(column_number), |
26 byte_(byte) { | 26 byte_(byte) { |
27 } | 27 } |
28 | 28 |
29 bool Location::operator==(const Location& other) const { | 29 bool Location::operator==(const Location& other) const { |
30 return other.file_ == file_ && | 30 return other.file_ == file_ && |
31 other.line_number_ == line_number_ && | 31 other.line_number_ == line_number_ && |
32 other.char_offset_ == char_offset_; | 32 other.column_number_ == column_number_; |
33 } | 33 } |
34 | 34 |
35 bool Location::operator!=(const Location& other) const { | 35 bool Location::operator!=(const Location& other) const { |
36 return !operator==(other); | 36 return !operator==(other); |
37 } | 37 } |
38 | 38 |
39 bool Location::operator<(const Location& other) const { | 39 bool Location::operator<(const Location& other) const { |
40 DCHECK(file_ == other.file_); | 40 DCHECK(file_ == other.file_); |
41 return std::tie(line_number_, char_offset_) < | 41 return std::tie(line_number_, column_number_) < |
42 std::tie(other.line_number_, other.char_offset_); | 42 std::tie(other.line_number_, other.column_number_); |
43 } | 43 } |
44 | 44 |
45 std::string Location::Describe(bool include_char_offset) const { | 45 std::string Location::Describe(bool include_column_number) const { |
46 if (!file_) | 46 if (!file_) |
47 return std::string(); | 47 return std::string(); |
48 | 48 |
49 std::string ret; | 49 std::string ret; |
50 if (file_->friendly_name().empty()) | 50 if (file_->friendly_name().empty()) |
51 ret = file_->name().value(); | 51 ret = file_->name().value(); |
52 else | 52 else |
53 ret = file_->friendly_name(); | 53 ret = file_->friendly_name(); |
54 | 54 |
55 ret += ":"; | 55 ret += ":"; |
56 ret += base::IntToString(line_number_); | 56 ret += base::IntToString(line_number_); |
57 if (include_char_offset) { | 57 if (include_column_number) { |
58 ret += ":"; | 58 ret += ":"; |
59 ret += base::IntToString(char_offset_); | 59 ret += base::IntToString(column_number_); |
60 } | 60 } |
61 return ret; | 61 return ret; |
62 } | 62 } |
63 | 63 |
64 LocationRange::LocationRange() { | 64 LocationRange::LocationRange() { |
65 } | 65 } |
66 | 66 |
67 LocationRange::LocationRange(const Location& begin, const Location& end) | 67 LocationRange::LocationRange(const Location& begin, const Location& end) |
68 : begin_(begin), | 68 : begin_(begin), |
69 end_(end) { | 69 end_(end) { |
70 DCHECK(begin_.file() == end_.file()); | 70 DCHECK(begin_.file() == end_.file()); |
71 } | 71 } |
72 | 72 |
73 LocationRange LocationRange::Union(const LocationRange& other) const { | 73 LocationRange LocationRange::Union(const LocationRange& other) const { |
74 DCHECK(begin_.file() == other.begin_.file()); | 74 DCHECK(begin_.file() == other.begin_.file()); |
75 return LocationRange( | 75 return LocationRange( |
76 begin_ < other.begin_ ? begin_ : other.begin_, | 76 begin_ < other.begin_ ? begin_ : other.begin_, |
77 end_ < other.end_ ? other.end_ : end_); | 77 end_ < other.end_ ? other.end_ : end_); |
78 } | 78 } |
OLD | NEW |