Fix DisownOpener and related tests.
[chromium-blink-merge.git] / tools / gn / location.cc
blob61f22b5e079d2c273b189192f09eb361e6acf7ea
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
3 // found in the LICENSE file.
5 #include "tools/gn/location.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "tools/gn/input_file.h"
11 Location::Location()
12 : file_(NULL),
13 line_number_(-1),
14 char_offset_(-1) {
17 Location::Location(const InputFile* file, int line_number, int char_offset)
18 : file_(file),
19 line_number_(line_number),
20 char_offset_(char_offset) {
23 bool Location::operator==(const Location& other) const {
24 return other.file_ == file_ &&
25 other.line_number_ == line_number_ &&
26 other.char_offset_ == char_offset_;
29 bool Location::operator!=(const Location& other) const {
30 return !operator==(other);
33 bool Location::operator<(const Location& other) const {
34 DCHECK(file_ == other.file_);
35 if (line_number_ != other.line_number_)
36 return line_number_ < other.line_number_;
37 return char_offset_ < other.char_offset_;
40 std::string Location::Describe(bool include_char_offset) const {
41 if (!file_)
42 return std::string();
44 std::string ret;
45 if (file_->friendly_name().empty())
46 ret = file_->name().value();
47 else
48 ret = file_->friendly_name();
50 ret += ":";
51 ret += base::IntToString(line_number_);
52 if (include_char_offset) {
53 ret += ":";
54 ret += base::IntToString(char_offset_);
56 return ret;
59 LocationRange::LocationRange() {
62 LocationRange::LocationRange(const Location& begin, const Location& end)
63 : begin_(begin),
64 end_(end) {
65 DCHECK(begin_.file() == end_.file());
68 LocationRange LocationRange::Union(const LocationRange& other) const {
69 DCHECK(begin_.file() == other.begin_.file());
70 return LocationRange(
71 begin_ < other.begin_ ? begin_ : other.begin_,
72 end_ < other.end_ ? other.end_ : end_);