blob: 0bff1b2cf3b32da06f9a604eaf45785fc6af3122 [file] [log] [blame]
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Location.h"
18
19#include <android-base/logging.h>
Neel Mehtaf9de4832019-06-12 19:38:09 -070020#include <tuple>
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070021
22namespace android {
23
24Position::Position(std::string filename, size_t line, size_t column)
25 : mFilename(filename), mLine(line), mColumn(column) {}
26
27const std::string& Position::filename() const {
28 return mFilename;
29}
30
31size_t Position::line() const {
32 return mLine;
33}
34
35size_t Position::column() const {
36 return mColumn;
37}
38
39bool Position::inSameFile(const Position& lhs, const Position& rhs) {
40 return lhs.mFilename == rhs.mFilename;
41}
42
43bool Position::operator<(const Position& pos) const {
Neel Mehtaf9de4832019-06-12 19:38:09 -070044 return std::tie(mFilename, mLine, mColumn) < std::tie(pos.mFilename, pos.mLine, pos.mColumn);
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070045}
46
47std::ostream& operator<<(std::ostream& ostr, const Position& pos) {
48 if (!pos.filename().empty()) {
49 ostr << pos.filename() << ":";
50 }
51 return ostr << pos.line() << "." << pos.column();
52}
53
54////////////////////////////////////////
55
56Location::Location(const Position& begin, const Position& end)
57 : mIsValid(true), mBegin(begin), mEnd(end) {}
58
59void Location::setLocation(const Position& begin, const Position& end) {
60 mIsValid = true;
61 mBegin = begin;
62 mEnd = end;
63}
64
65bool Location::isValid() const {
66 return mIsValid;
67}
68
69const Position& Location::begin() const {
70 return mBegin;
71}
72
73const Position& Location::end() const {
74 return mEnd;
75}
76
77Location Location::startOf(const std::string& path) {
78 return Location(Position(path, 1, 1), Position(path, 1, 1));
79}
80
81bool Location::inSameFile(const Location& lhs, const Location& rhs) {
82 return Position::inSameFile(lhs.mBegin, rhs.mBegin);
83}
84
85bool Location::intersect(const Location& lhs, const Location& rhs) {
86 if (!inSameFile(lhs, rhs)) return false;
87 return !(lhs.mEnd < rhs.mBegin || rhs.mEnd < lhs.mBegin);
88}
89
90bool Location::operator<(const Location& loc) const {
Neel Mehtaf9de4832019-06-12 19:38:09 -070091 return std::tie(mBegin, mEnd) < std::tie(loc.mBegin, loc.mEnd);
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070092}
93
94std::ostream& operator<<(std::ostream& ostr, const Location& loc) {
95 Position last = Position(loc.end().filename(), loc.end().line(),
96 std::max<size_t>(1u, loc.end().column() - 1));
97 ostr << loc.begin();
98 if (loc.begin().filename() != last.filename()) {
99 ostr << "-" << last;
100 } else if (loc.begin().line() != last.line()) {
101 ostr << "-" << last.line() << "." << last.column();
102 } else if (loc.begin().column() != last.column()) {
103 ostr << "-" << last.column();
104 }
105 return ostr;
106}
107
108} // namespace android