1d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// found in the LICENSE file.
4d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "tools/gn/location.h"
6d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
7d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/logging.h"
8d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
9d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "tools/gn/input_file.h"
10d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
11d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)Location::Location()
12d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    : file_(NULL),
13d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      line_number_(-1),
14d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      char_offset_(-1) {
15d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
16d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
17d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)Location::Location(const InputFile* file, int line_number, int char_offset)
18d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    : file_(file),
19d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      line_number_(line_number),
20d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      char_offset_(char_offset) {
21d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
22d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
23d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)bool Location::operator==(const Location& other) const {
24d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return other.file_ == file_ &&
25d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)         other.line_number_ == line_number_ &&
26d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)         other.char_offset_ == char_offset_;
27d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
28d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
29d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)bool Location::operator!=(const Location& other) const {
30d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return !operator==(other);
31d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
32d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
33d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)bool Location::operator<(const Location& other) const {
34d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  DCHECK(file_ == other.file_);
35d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (line_number_ != other.line_number_)
36d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return line_number_ < other.line_number_;
37d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return char_offset_ < other.char_offset_;
38d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
39d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
40d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)std::string Location::Describe(bool include_char_offset) const {
41d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (!file_)
42d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return std::string();
43d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
44d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  std::string ret;
45d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (file_->friendly_name().empty())
46d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ret = file_->name().value();
47d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  else
48d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ret = file_->friendly_name();
49d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
50d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  ret += ":";
51d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  ret += base::IntToString(line_number_);
52d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (include_char_offset) {
53d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ret += ":";
54d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ret += base::IntToString(char_offset_);
55d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
56d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return ret;
57d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
58d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
59d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)LocationRange::LocationRange() {
60d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
61d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
62d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)LocationRange::LocationRange(const Location& begin, const Location& end)
63d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    : begin_(begin),
64d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      end_(end) {
65d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  DCHECK(begin_.file() == end_.file());
66d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
67d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
68d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)LocationRange LocationRange::Union(const LocationRange& other) const {
69d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  DCHECK(begin_.file() == other.begin_.file());
70d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return LocationRange(
71d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      begin_ < other.begin_ ? begin_ : other.begin_,
72d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      end_ < other.end_ ? other.end_ : end_);
73d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
74