Coverage Report

Created: 2022-08-24 06:52

/src/solidity/liblangutil/SourceLocation.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
  This file is part of solidity.
3
4
  solidity is free software: you can redistribute it and/or modify
5
  it under the terms of the GNU General Public License as published by
6
  the Free Software Foundation, either version 3 of the License, or
7
  (at your option) any later version.
8
9
  solidity is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
  GNU General Public License for more details.
13
14
  You should have received a copy of the GNU General Public License
15
  along with solidity.  If not, see <http://www.gnu.org/licenses/>.
16
*/
17
// SPDX-License-Identifier: GPL-3.0
18
19
#include <liblangutil/Exceptions.h>
20
21
#include <boost/algorithm/string/split.hpp>
22
#include <boost/algorithm/string.hpp>
23
24
#include <iostream>
25
26
using namespace solidity;
27
using namespace solidity::langutil;
28
using namespace std;
29
30
SourceLocation solidity::langutil::parseSourceLocation(string const& _input, vector<shared_ptr<string const>> const& _sourceNames)
31
0
{
32
  // Expected input: "start:length:sourceindex"
33
0
  enum SrcElem: size_t { Start, Length, Index };
34
35
0
  vector<string> pos;
36
37
0
  boost::algorithm::split(pos, _input, boost::is_any_of(":"));
38
39
0
  solAssert(pos.size() == 3, "SourceLocation string must have 3 colon separated numeric fields.");
40
0
  auto const sourceIndex = stoi(pos[Index]);
41
42
0
  astAssert(
43
0
    sourceIndex == -1 || (0 <= sourceIndex && static_cast<size_t>(sourceIndex) < _sourceNames.size()),
44
0
    "'src'-field ill-formatted or src-index too high"
45
0
  );
46
47
0
  int start = stoi(pos[Start]);
48
0
  int end = start + stoi(pos[Length]);
49
50
0
  SourceLocation result{start, end, {}};
51
0
  if (sourceIndex != -1)
52
0
    result.sourceName = _sourceNames.at(static_cast<size_t>(sourceIndex));
53
0
  return result;
54
0
}
55
56
std::ostream& solidity::langutil::operator<<(std::ostream& _out, SourceLocation const& _location)
57
0
{
58
0
  if (!_location.isValid())
59
0
    return _out << "NO_LOCATION_SPECIFIED";
60
61
0
  if (_location.sourceName)
62
0
    _out << *_location.sourceName;
63
64
0
  _out << "[" << _location.start << "," << _location.end << "]";
65
66
0
  return _out;
67
0
}