• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <tuple>
21 
22 namespace android {
23 
Position(std::string filename,size_t line,size_t column)24 Position::Position(std::string filename, size_t line, size_t column)
25     : mFilename(filename), mLine(line), mColumn(column) {}
26 
filename() const27 const std::string& Position::filename() const {
28     return mFilename;
29 }
30 
line() const31 size_t Position::line() const {
32     return mLine;
33 }
34 
column() const35 size_t Position::column() const {
36     return mColumn;
37 }
38 
inSameFile(const Position & lhs,const Position & rhs)39 bool Position::inSameFile(const Position& lhs, const Position& rhs) {
40     return lhs.mFilename == rhs.mFilename;
41 }
42 
operator <(const Position & pos) const43 bool Position::operator<(const Position& pos) const {
44     return std::tie(mFilename, mLine, mColumn) < std::tie(pos.mFilename, pos.mLine, pos.mColumn);
45 }
46 
operator <<(std::ostream & ostr,const Position & pos)47 std::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 
Location(const Position & begin,const Position & end)56 Location::Location(const Position& begin, const Position& end)
57     : mIsValid(true), mBegin(begin), mEnd(end) {}
58 
setLocation(const Position & begin,const Position & end)59 void Location::setLocation(const Position& begin, const Position& end) {
60     mIsValid = true;
61     mBegin = begin;
62     mEnd = end;
63 }
64 
isValid() const65 bool Location::isValid() const {
66     return mIsValid;
67 }
68 
begin() const69 const Position& Location::begin() const {
70     return mBegin;
71 }
72 
end() const73 const Position& Location::end() const {
74     return mEnd;
75 }
76 
startOf(const std::string & path)77 Location Location::startOf(const std::string& path) {
78     return Location(Position(path, 1, 1), Position(path, 1, 1));
79 }
80 
inSameFile(const Location & lhs,const Location & rhs)81 bool Location::inSameFile(const Location& lhs, const Location& rhs) {
82     return Position::inSameFile(lhs.mBegin, rhs.mBegin);
83 }
84 
intersect(const Location & lhs,const Location & rhs)85 bool 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 
operator <(const Location & loc) const90 bool Location::operator<(const Location& loc) const {
91     return std::tie(mBegin, mEnd) < std::tie(loc.mBegin, loc.mEnd);
92 }
93 
operator <<(std::ostream & ostr,const Location & loc)94 std::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
109