Coverage Report

Created: 2025-09-04 07:34

/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
29
SourceLocation solidity::langutil::parseSourceLocation(std::string const& _input, std::vector<std::shared_ptr<std::string const>> const& _sourceNames)
30
0
{
31
  // Expected input: "start:length:sourceindex"
32
0
  enum SrcElem: size_t { Start, Length, Index };
33
34
0
  std::vector<std::string> pos;
35
36
0
  boost::algorithm::split(pos, _input, boost::is_any_of(":"));
37
38
0
  solAssert(pos.size() == 3, "SourceLocation string must have 3 colon separated numeric fields.");
39
0
  auto const sourceIndex = stoi(pos[Index]);
40
41
0
  astAssert(
42
0
    sourceIndex == -1 || (0 <= sourceIndex && static_cast<size_t>(sourceIndex) < _sourceNames.size()),
43
0
    "'src'-field ill-formatted or src-index too high"
44
0
  );
45
46
0
  int start = stoi(pos[Start]);
47
0
  int end = start + stoi(pos[Length]);
48
49
0
  SourceLocation result{start, end, {}};
50
0
  if (sourceIndex != -1)
51
0
    result.sourceName = _sourceNames.at(static_cast<size_t>(sourceIndex));
52
0
  return result;
53
0
}
54
55
std::ostream& solidity::langutil::operator<<(std::ostream& _out, SourceLocation const& _location)
56
0
{
57
0
  if (!_location.isValid())
58
0
    return _out << "NO_LOCATION_SPECIFIED";
59
60
0
  if (_location.sourceName)
61
0
    _out << *_location.sourceName;
62
63
0
  _out << "[" << _location.start << "," << _location.end << "]";
64
65
0
  return _out;
66
0
}