Coverage Report

Created: 2025-09-04 07:34

/src/solidity/libsolidity/analysis/ViewPureChecker.h
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
#pragma once
20
21
#include <libsolidity/ast/ASTEnums.h>
22
#include <libsolidity/ast/ASTForward.h>
23
#include <libsolidity/ast/ASTVisitor.h>
24
25
#include <map>
26
#include <memory>
27
#include <optional>
28
29
namespace solidity::langutil
30
{
31
class ErrorReporter;
32
struct SourceLocation;
33
}
34
35
namespace solidity::frontend
36
{
37
38
class ViewPureChecker: private ASTConstVisitor
39
{
40
public:
41
  ViewPureChecker(std::vector<std::shared_ptr<ASTNode>> const& _ast, langutil::ErrorReporter& _errorReporter):
42
15.2k
    m_ast(_ast), m_errorReporter(_errorReporter) {}
43
44
  bool check();
45
46
private:
47
  struct MutabilityAndLocation
48
  {
49
    StateMutability mutability;
50
    langutil::SourceLocation location;
51
  };
52
53
  bool visit(ImportDirective const&) override;
54
55
  bool visit(FunctionDefinition const& _funDef) override;
56
  void endVisit(FunctionDefinition const& _funDef) override;
57
  void endVisit(BinaryOperation const& _binaryOperation) override;
58
  void endVisit(UnaryOperation const& _unaryOperation) override;
59
  bool visit(ModifierDefinition const& _modifierDef) override;
60
  void endVisit(ModifierDefinition const& _modifierDef) override;
61
  void endVisit(Identifier const& _identifier) override;
62
  bool visit(MemberAccess const& _memberAccess) override;
63
  void endVisit(MemberAccess const& _memberAccess) override;
64
  void endVisit(IndexAccess const& _indexAccess) override;
65
  void endVisit(IndexRangeAccess const& _indexAccess) override;
66
  void endVisit(ModifierInvocation const& _modifier) override;
67
  void endVisit(FunctionCall const& _functionCall) override;
68
  void endVisit(InlineAssembly const& _inlineAssembly) override;
69
70
  /// Called when an element of mutability @a _mutability is encountered.
71
  /// Creates appropriate warnings and errors and sets @a m_currentBestMutability.
72
  void reportMutability(
73
    StateMutability _mutability,
74
    langutil::SourceLocation const& _location,
75
    std::optional<langutil::SourceLocation> const& _nestedLocation = {}
76
  );
77
78
  void reportFunctionCallMutability(StateMutability _mutability, langutil::SourceLocation const& _location);
79
80
  /// Determines the mutability of modifier if not already cached.
81
  MutabilityAndLocation const& modifierMutability(ModifierDefinition const& _modifier);
82
83
  std::vector<std::shared_ptr<ASTNode>> const& m_ast;
84
  langutil::ErrorReporter& m_errorReporter;
85
86
  bool m_errors = false;
87
  MutabilityAndLocation m_bestMutabilityAndLocation = MutabilityAndLocation{StateMutability::Payable, langutil::SourceLocation()};
88
  FunctionDefinition const* m_currentFunction = nullptr;
89
  std::map<ModifierDefinition const*, MutabilityAndLocation> m_inferredMutability;
90
};
91
92
}