• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,int byte)15 Location::Location(const InputFile* file,
16                    int line_number,
17                    int column_number,
18                    int byte)
19     : file_(file),
20       line_number_(line_number),
21       column_number_(column_number),
22       byte_(byte) {}
23 
operator ==(const Location & other) const24 bool Location::operator==(const Location& other) const {
25   return other.file_ == file_ && other.line_number_ == line_number_ &&
26          other.column_number_ == column_number_;
27 }
28 
operator !=(const Location & other) const29 bool Location::operator!=(const Location& other) const {
30   return !operator==(other);
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