Cppcheck
pathanalysis.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef GUARD_PATHANALYSIS_H
20 #define GUARD_PATHANALYSIS_H
21 
22 #include "errortypes.h"
23 
24 #include <functional>
25 #include <list>
26 #include <utility>
27 
28 class Library;
29 class Scope;
30 class Token;
31 
32 struct PathAnalysis {
33  enum class Progress {
34  Continue,
35  Break
36  };
39  {}
40  const Token * start;
41  const Library * library;
42 
43  struct Info {
44  const Token* tok;
46  bool known;
47  };
48 
49  void forward(const std::function<Progress(const Info&)>& f) const;
50 
51  Info forwardFind(std::function<bool(const Info&)> pred) const {
52  Info result{};
53  forward([&](const Info& info) {
54  if (pred(info)) {
55  result = info;
56  return Progress::Break;
57  }
58  return Progress::Continue;
59  });
60  return result;
61  }
62 private:
63 
64  static Progress forwardRecursive(const Token* tok, Info info, const std::function<PathAnalysis::Progress(const Info&)>& f);
65  Progress forwardRange(const Token* startToken, const Token* endToken, Info info, const std::function<Progress(const Info&)>& f) const;
66 
67  static const Scope* findOuterScope(const Scope * scope);
68 
69  static std::pair<bool, bool> checkCond(const Token * tok, bool& known);
70 };
71 
72 /**
73  * @brief Returns true if there is a path between the two tokens
74  *
75  * @param start Starting point of the path
76  * @param dest The path destination
77  * @param errorPath Adds the path traversal to the errorPath
78  */
79 bool reaches(const Token * start, const Token * dest, const Library& library, ErrorPath* errorPath);
80 
81 #endif
82 
Library definitions handling.
Definition: library.h:52
The token list that the TokenList generates is a linked-list of this class.
Definition: token.h:150
std::list< ErrorPathItem > ErrorPath
Definition: errortypes.h:130
bool reaches(const Token *start, const Token *dest, const Library &library, ErrorPath *errorPath)
Returns true if there is a path between the two tokens.
const Token * tok
Definition: pathanalysis.h:44
ErrorPath errorPath
Definition: pathanalysis.h:45
static Progress forwardRecursive(const Token *tok, Info info, const std::function< PathAnalysis::Progress(const Info &)> &f)
Progress forwardRange(const Token *startToken, const Token *endToken, Info info, const std::function< Progress(const Info &)> &f) const
Info forwardFind(std::function< bool(const Info &)> pred) const
Definition: pathanalysis.h:51
const Token * start
Definition: pathanalysis.h:40
const Library * library
Definition: pathanalysis.h:41
PathAnalysis(const Token *start, const Library &library)
Definition: pathanalysis.h:37
static const Scope * findOuterScope(const Scope *scope)
static std::pair< bool, bool > checkCond(const Token *tok, bool &known)
void forward(const std::function< Progress(const Info &)> &f) const