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