summaryrefslogtreecommitdiffstats
path: root/tools/gn/location.cc
blob: 49ca3ffe717f7c1550eae1f25f4c28f73b63b6ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "tools/gn/location.h"

#include <tuple>

#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "tools/gn/input_file.h"

Location::Location()
    : file_(nullptr),
      line_number_(-1),
      column_number_(-1) {
}

Location::Location(const InputFile* file,
                   int line_number,
                   int column_number,
                   int byte)
    : file_(file),
      line_number_(line_number),
      column_number_(column_number),
      byte_(byte) {
}

bool Location::operator==(const Location& other) const {
  return other.file_ == file_ &&
         other.line_number_ == line_number_ &&
         other.column_number_ == column_number_;
}

bool Location::operator!=(const Location& other) const {
  return !operator==(other);
}

bool Location::operator<(const Location& other) const {
  DCHECK(file_ == other.file_);
  return std::tie(line_number_, column_number_) <
         std::tie(other.line_number_, other.column_number_);
}

std::string Location::Describe(bool include_column_number) const {
  if (!file_)
    return std::string();

  std::string ret;
  if (file_->friendly_name().empty())
    ret = file_->name().value();
  else
    ret = file_->friendly_name();

  ret += ":";
  ret += base::IntToString(line_number_);
  if (include_column_number) {
    ret += ":";
    ret += base::IntToString(column_number_);
  }
  return ret;
}

LocationRange::LocationRange() {
}

LocationRange::LocationRange(const Location& begin, const Location& end)
    : begin_(begin),
      end_(end) {
  DCHECK(begin_.file() == end_.file());
}

LocationRange LocationRange::Union(const LocationRange& other) const {
  DCHECK(begin_.file() == other.begin_.file());
  return LocationRange(
      begin_ < other.begin_ ? begin_ : other.begin_,
      end_ < other.end_ ? other.end_ : end_);
}