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 #ifndef TOOLS_GN_LOCATION_H_ | 5 #ifndef TOOLS_GN_LOCATION_H_ |
6 #define TOOLS_GN_LOCATION_H_ | 6 #define TOOLS_GN_LOCATION_H_ |
7 | 7 |
8 #include <algorithm> | 8 #include <string> |
9 | |
10 #include "base/logging.h" | |
11 | 9 |
12 class InputFile; | 10 class InputFile; |
13 | 11 |
14 // Represents a place in a source file. Used for error reporting. | 12 // Represents a place in a source file. Used for error reporting. |
15 class Location { | 13 class Location { |
16 public: | 14 public: |
17 Location() | 15 Location(); |
18 : file_(NULL), | 16 Location(const InputFile* file, int line_number, int char_offset); |
19 line_number_(-1), | |
20 char_offset_(-1) { | |
21 } | |
22 Location(const InputFile* file, int line_number, int char_offset) | |
23 : file_(file), | |
24 line_number_(line_number), | |
25 char_offset_(char_offset) { | |
26 } | |
27 | 17 |
28 const InputFile* file() const { return file_; } | 18 const InputFile* file() const { return file_; } |
29 int line_number() const { return line_number_; } | 19 int line_number() const { return line_number_; } |
30 int char_offset() const { return char_offset_; } | 20 int char_offset() const { return char_offset_; } |
31 | 21 |
32 bool operator==(const Location& other) const { | 22 bool operator==(const Location& other) const; |
33 return other.file_ == file_ && | 23 bool operator!=(const Location& other) const; |
34 other.line_number_ == line_number_ && | 24 bool operator<(const Location& other) const; |
35 other.char_offset_ == char_offset_; | |
36 } | |
37 | 25 |
38 bool operator<(const Location& other) const { | 26 // Returns a string with the file, line, and (optionally) the character |
39 DCHECK(file_ == other.file_); | 27 // offset for this location. If this location is null, returns an empty |
40 if (line_number_ != other.line_number_) | 28 // string. |
41 return line_number_ < other.line_number_; | 29 std::string Describe(bool include_char_offset) const; |
42 return char_offset_ < other.char_offset_; | |
43 } | |
44 | 30 |
45 private: | 31 private: |
46 const InputFile* file_; // Null when unset. | 32 const InputFile* file_; // Null when unset. |
47 int line_number_; // -1 when unset. | 33 int line_number_; // -1 when unset. |
48 int char_offset_; // -1 when unset. | 34 int char_offset_; // -1 when unset. |
49 }; | 35 }; |
50 | 36 |
51 // Represents a range in a source file. Used for error reporting. | 37 // Represents a range in a source file. Used for error reporting. |
52 // The end is exclusive i.e. [begin, end) | 38 // The end is exclusive i.e. [begin, end) |
53 class LocationRange { | 39 class LocationRange { |
54 public: | 40 public: |
55 LocationRange() {} | 41 LocationRange(); |
56 LocationRange(const Location& begin, const Location& end) | 42 LocationRange(const Location& begin, const Location& end); |
57 : begin_(begin), | |
58 end_(end) { | |
59 DCHECK(begin_.file() == end_.file()); | |
60 } | |
61 | 43 |
62 const Location& begin() const { return begin_; } | 44 const Location& begin() const { return begin_; } |
63 const Location& end() const { return end_; } | 45 const Location& end() const { return end_; } |
64 | 46 |
65 LocationRange Union(const LocationRange& other) const { | 47 LocationRange Union(const LocationRange& other) const; |
66 DCHECK(begin_.file() == other.begin_.file()); | |
67 return LocationRange( | |
68 begin_ < other.begin_ ? begin_ : other.begin_, | |
69 end_ < other.end_ ? other.end_ : end_); | |
70 } | |
71 | 48 |
72 private: | 49 private: |
73 Location begin_; | 50 Location begin_; |
74 Location end_; | 51 Location end_; |
75 }; | 52 }; |
76 | 53 |
77 #endif // TOOLS_GN_LOCATION_H_ | 54 #endif // TOOLS_GN_LOCATION_H_ |
OLD | NEW |