blob: 1cfe4b7e39d76d20d0bd5460d17d921366bdf4b3 [file]
// 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 "gn/location.h"
#include <tuple>
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "gn/input_file.h"
#include "gn/tokenizer.h"
Location::Location() = default;
Location::Location(const InputFile* file, int line_number, int column_number)
: file_(file), line_number_(line_number), column_number_(column_number) {}
bool Location::operator==(const Location& other) const {
return (file_ == other.file_ && line_number_ == other.line_number_ &&
column_number_ == other.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_);
}
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() = default;
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_);
}
std::string_view LocationRange::GetText() const {
if (is_null() || !begin_.file()) {
return {};
}
const std::string& contents = begin_.file()->contents();
size_t begin_line_off =
Tokenizer::ByteOffsetOfNthLine(contents, begin_.line_number());
if (begin_line_off == static_cast<size_t>(-1)) {
return {};
}
size_t begin_off = begin_line_off + (begin_.column_number() - 1);
size_t end_line_off =
Tokenizer::ByteOffsetOfNthLine(contents, end_.line_number());
if (end_line_off == static_cast<size_t>(-1)) {
return {};
}
size_t end_off = end_line_off + (end_.column_number() - 1);
if (begin_off > end_off || end_off > contents.size()) {
return {};
}
return std::string_view(contents).substr(begin_off, end_off - begin_off);
}