Coverage Report

Created: 2026-06-30 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/solidity/libyul/optimiser/UnusedFunctionsCommon.cpp
Line
Count
Source
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 <libyul/optimiser/UnusedFunctionsCommon.h>
20
21
#include <libyul/Dialect.h>
22
23
#include <libsolutil/CommonData.h>
24
25
using namespace solidity;
26
using namespace solidity::util;
27
using namespace solidity::yul;
28
using namespace solidity::yul::unusedFunctionsCommon;
29
30
FunctionDefinition unusedFunctionsCommon::createLinkingFunction(
31
  FunctionDefinition const& _original,
32
  std::pair<std::vector<bool>, std::vector<bool>> const& _usedParametersAndReturns,
33
  YulName const& _originalFunctionName,
34
  YulName const& _linkingFunctionName,
35
  NameDispenser& _nameDispenser
36
)
37
64.9k
{
38
64.9k
  auto generateTypedName = [&](NameWithDebugData t)
39
303k
  {
40
303k
    return NameWithDebugData{
41
303k
      t.debugData,
42
303k
      _nameDispenser.newName(t.name)
43
303k
    };
44
303k
  };
45
46
64.9k
  FunctionDefinition linkingFunction{
47
64.9k
    _original.debugData,
48
64.9k
    _linkingFunctionName,
49
64.9k
    util::applyMap(_original.parameters, generateTypedName),
50
64.9k
    util::applyMap(_original.returnVariables, generateTypedName),
51
64.9k
    {_original.debugData, {}} // body
52
64.9k
  };
53
54
64.9k
  FunctionCall call{_original.debugData, Identifier{_original.debugData, _originalFunctionName}, {}};
55
64.9k
  for (auto const& p: filter(linkingFunction.parameters, _usedParametersAndReturns.first))
56
18.4k
    call.arguments.emplace_back(Identifier{_original.debugData, p.name});
57
58
64.9k
  Assignment assignment{_original.debugData, {}, nullptr};
59
60
64.9k
  for (auto const& r: filter(linkingFunction.returnVariables, _usedParametersAndReturns.second))
61
8.07k
    assignment.variableNames.emplace_back(Identifier{_original.debugData, r.name});
62
63
64.9k
  if (assignment.variableNames.empty())
64
57.7k
    linkingFunction.body.statements.emplace_back(ExpressionStatement{_original.debugData, std::move(call)});
65
7.19k
  else
66
7.19k
  {
67
7.19k
    assignment.value = std::make_unique<Expression>(std::move(call));
68
7.19k
    linkingFunction.body.statements.emplace_back(std::move(assignment));
69
7.19k
  }
70
71
64.9k
  return linkingFunction;
72
64.9k
}