Timur Iskhakov | 0e7c2a9 | 2017-09-06 11:40:59 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 20 | |
| 21 | namespace android { |
| 22 | |
| 23 | Position::Position(std::string filename, size_t line, size_t column) |
| 24 | : mFilename(filename), mLine(line), mColumn(column) {} |
| 25 | |
| 26 | const std::string& Position::filename() const { |
| 27 | return mFilename; |
| 28 | } |
| 29 | |
| 30 | size_t Position::line() const { |
| 31 | return mLine; |
| 32 | } |
| 33 | |
| 34 | size_t Position::column() const { |
| 35 | return mColumn; |
| 36 | } |
| 37 | |
| 38 | bool Position::inSameFile(const Position& lhs, const Position& rhs) { |
| 39 | return lhs.mFilename == rhs.mFilename; |
| 40 | } |
| 41 | |
| 42 | bool Position::operator<(const Position& pos) const { |
| 43 | CHECK(inSameFile(*this, pos)) << "Cannot compare positions in different files"; |
| 44 | if (mLine == pos.mLine) { |
| 45 | return mColumn < pos.mColumn; |
| 46 | } |
| 47 | return mLine < pos.mLine; |
| 48 | } |
| 49 | |
| 50 | std::ostream& operator<<(std::ostream& ostr, const Position& pos) { |
| 51 | if (!pos.filename().empty()) { |
| 52 | ostr << pos.filename() << ":"; |
| 53 | } |
| 54 | return ostr << pos.line() << "." << pos.column(); |
| 55 | } |
| 56 | |
| 57 | //////////////////////////////////////// |
| 58 | |
| 59 | Location::Location(const Position& begin, const Position& end) |
| 60 | : mIsValid(true), mBegin(begin), mEnd(end) {} |
| 61 | |
| 62 | void Location::setLocation(const Position& begin, const Position& end) { |
| 63 | mIsValid = true; |
| 64 | mBegin = begin; |
| 65 | mEnd = end; |
| 66 | } |
| 67 | |
| 68 | bool Location::isValid() const { |
| 69 | return mIsValid; |
| 70 | } |
| 71 | |
| 72 | const Position& Location::begin() const { |
| 73 | return mBegin; |
| 74 | } |
| 75 | |
| 76 | const Position& Location::end() const { |
| 77 | return mEnd; |
| 78 | } |
| 79 | |
| 80 | Location Location::startOf(const std::string& path) { |
| 81 | return Location(Position(path, 1, 1), Position(path, 1, 1)); |
| 82 | } |
| 83 | |
| 84 | bool Location::inSameFile(const Location& lhs, const Location& rhs) { |
| 85 | return Position::inSameFile(lhs.mBegin, rhs.mBegin); |
| 86 | } |
| 87 | |
| 88 | bool Location::intersect(const Location& lhs, const Location& rhs) { |
| 89 | if (!inSameFile(lhs, rhs)) return false; |
| 90 | return !(lhs.mEnd < rhs.mBegin || rhs.mEnd < lhs.mBegin); |
| 91 | } |
| 92 | |
| 93 | bool Location::operator<(const Location& loc) const { |
| 94 | CHECK(inSameFile(*this, loc)) << "Cannot compare locations in different files"; |
| 95 | CHECK(!intersect(*this, loc)); |
| 96 | return mEnd < loc.mBegin; |
| 97 | } |
| 98 | |
| 99 | std::ostream& operator<<(std::ostream& ostr, const Location& loc) { |
| 100 | Position last = Position(loc.end().filename(), loc.end().line(), |
| 101 | std::max<size_t>(1u, loc.end().column() - 1)); |
| 102 | ostr << loc.begin(); |
| 103 | if (loc.begin().filename() != last.filename()) { |
| 104 | ostr << "-" << last; |
| 105 | } else if (loc.begin().line() != last.line()) { |
| 106 | ostr << "-" << last.line() << "." << last.column(); |
| 107 | } else if (loc.begin().column() != last.column()) { |
| 108 | ostr << "-" << last.column(); |
| 109 | } |
| 110 | return ostr; |
| 111 | } |
| 112 | |
| 113 | } // namespace android |