Cppcheck
summaries.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 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 #include "summaries.h"
20 
21 #include "analyzerinfo.h"
22 #include "settings.h"
23 #include "symboldatabase.h"
24 #include "token.h"
25 #include "tokenize.h"
26 #include "tokenlist.h"
27 
28 #include <algorithm>
29 #include <fstream>
30 #include <map>
31 #include <sstream>
32 #include <utility>
33 #include <vector>
34 
35 
36 
37 std::string Summaries::create(const Tokenizer &tokenizer, const std::string &cfg)
38 {
39  const SymbolDatabase *symbolDatabase = tokenizer.getSymbolDatabase();
40  const Settings &settings = tokenizer.getSettings();
41 
42  std::ostringstream ostr;
43  for (const Scope *scope : symbolDatabase->functionScopes) {
44  const Function *f = scope->function;
45  if (!f)
46  continue;
47 
48  // Summarize function
49  std::set<std::string> noreturn;
50  std::set<std::string> globalVars;
51  std::set<std::string> calledFunctions;
52  for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
53  if (tok->variable() && tok->variable()->isGlobal())
54  globalVars.insert(tok->variable()->name());
55  if (Token::Match(tok, "%name% (") && !Token::simpleMatch(tok->linkAt(1), ") {")) {
56  calledFunctions.insert(tok->str());
57  if (Token::simpleMatch(tok->linkAt(1), ") ; }"))
58  noreturn.insert(tok->str());
59  }
60  }
61 
62  // Write summary for function
63  auto join = [](const std::set<std::string> &data) -> std::string {
64  std::string ret;
65  const char *sep = "";
66  for (const std::string &d: data)
67  {
68  ret += sep + d;
69  sep = ",";
70  }
71  return ret;
72  };
73 
74  ostr << f->name();
75  if (!globalVars.empty())
76  ostr << " global:[" << join(globalVars) << "]";
77  if (!calledFunctions.empty())
78  ostr << " call:[" << join(calledFunctions) << "]";
79  if (!noreturn.empty())
80  ostr << " noreturn:[" << join(noreturn) << "]";
81  ostr << std::endl;
82  }
83 
84  if (!settings.buildDir.empty()) {
85  std::string filename = AnalyzerInformation::getAnalyzerInfoFile(settings.buildDir, tokenizer.list.getSourceFilePath(), cfg);
86  const std::string::size_type pos = filename.rfind(".a");
87  if (pos != std::string::npos) {
88  filename[pos+1] = 's';
89  std::ofstream fout(filename);
90  fout << ostr.str();
91  }
92  }
93 
94  return ostr.str();
95 }
96 
97 
98 
99 
100 static std::vector<std::string> getSummaryFiles(const std::string &filename)
101 {
102  std::vector<std::string> ret;
103  std::ifstream fin(filename);
104  if (!fin.is_open())
105  return ret;
106  std::string line;
107  while (std::getline(fin, line)) {
108  const std::string::size_type dotA = line.find(".a");
109  const std::string::size_type colon = line.find(':');
110  if (colon > line.size() || dotA > colon)
111  continue;
112  std::string f = line.substr(0,colon);
113  f[dotA + 1] = 's';
114  ret.push_back(std::move(f));
115  }
116  return ret;
117 }
118 
119 static std::vector<std::string> getSummaryData(const std::string &line, const std::string &data)
120 {
121  std::vector<std::string> ret;
122  const std::string::size_type start = line.find(" " + data + ":[");
123  if (start == std::string::npos)
124  return ret;
125  const std::string::size_type end = line.find(']', start);
126  if (end >= line.size())
127  return ret;
128 
129  std::string::size_type pos1 = start + 3 + data.size();
130  while (pos1 < end) {
131  const std::string::size_type pos2 = line.find_first_of(",]",pos1);
132  ret.push_back(line.substr(pos1, pos2-pos1-1));
133  pos1 = pos2 + 1;
134  }
135 
136  return ret;
137 }
138 
139 static void removeFunctionCalls(const std::string& calledFunction,
140  std::map<std::string, std::vector<std::string>> &functionCalledBy,
141  std::map<std::string, std::vector<std::string>> &functionCalls,
142  std::vector<std::string> &add)
143 {
144  std::vector<std::string> calledBy = functionCalledBy[calledFunction];
145  functionCalledBy.erase(calledFunction);
146  for (const std::string &c: calledBy) {
147  std::vector<std::string> &calls = functionCalls[c];
148  calls.erase(std::remove(calls.begin(), calls.end(), calledFunction), calls.end());
149  if (calls.empty()) {
150  add.push_back(calledFunction);
151  removeFunctionCalls(c, functionCalledBy, functionCalls, add);
152  }
153  }
154 }
155 
156 void Summaries::loadReturn(const std::string &buildDir, std::set<std::string> &summaryReturn)
157 {
158  if (buildDir.empty())
159  return;
160 
161  std::vector<std::string> return1;
162  std::map<std::string, std::vector<std::string>> functionCalls;
163  std::map<std::string, std::vector<std::string>> functionCalledBy;
164 
165  // extract "functionNoreturn" and "functionCalledBy" from summaries
166  std::vector<std::string> summaryFiles = getSummaryFiles(buildDir + "/files.txt");
167  for (const std::string &filename: summaryFiles) {
168  std::ifstream fin(buildDir + '/' + filename);
169  if (!fin.is_open())
170  continue;
171  std::string line;
172  while (std::getline(fin, line)) {
173  // Get function name
174  constexpr std::string::size_type pos1 = 0;
175  const std::string::size_type pos2 = line.find(' ', pos1);
176  const std::string functionName = (pos2 == std::string::npos) ? line : line.substr(0, pos2);
177  std::vector<std::string> call = getSummaryData(line, "call");
178  functionCalls[functionName] = call;
179  if (call.empty())
180  return1.push_back(functionName);
181  else {
182  for (const std::string &c: call) {
183  functionCalledBy[c].push_back(functionName);
184  }
185  }
186  }
187  }
188  summaryReturn.insert(return1.cbegin(), return1.cend());
189 
190  // recursively set "summaryNoreturn"
191  for (const std::string &f: return1) {
192  std::vector<std::string> return2;
193  removeFunctionCalls(f, functionCalledBy, functionCalls, return2);
194  summaryReturn.insert(return2.cbegin(), return2.cend());
195  }
196 }
static std::string getAnalyzerInfoFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg)
const std::string & name() const
Function * function
function info for this function
const Token * bodyStart
'{' token
const Token * bodyEnd
'}' token
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:95
std::string buildDir
–cppcheck-build-dir.
Definition: settings.h:121
std::vector< const Scope * > functionScopes
Fast access to function scopes.
const std::string & getSourceFilePath() const
Definition: tokenlist.cpp:75
The token list that the TokenList generates is a linked-list of this class.
Definition: token.h:150
static bool Match(const Token *tok, const char pattern[], nonneg int varid=0)
Match given token (or list of tokens) to a pattern list.
Definition: token.cpp:722
Token * next()
Definition: token.h:830
static bool simpleMatch(const Token *tok, const char(&pattern)[count])
Match given token (or list of tokens) to a pattern list.
Definition: token.h:252
The main purpose is to tokenize the source code.
Definition: tokenize.h:46
const Settings & getSettings() const
Definition: tokenize.h:615
TokenList list
Token list: stores all tokens.
Definition: tokenize.h:590
const SymbolDatabase * getSymbolDatabase() const
Definition: tokenize.h:563
static std::string join(const std::list< std::string > &strlist, const char *sep)
CPPCHECKLIB std::string create(const Tokenizer &tokenizer, const std::string &cfg)
Definition: summaries.cpp:37
CPPCHECKLIB void loadReturn(const std::string &buildDir, std::set< std::string > &summaryReturn)
Definition: summaries.cpp:156
static std::string cfg(const std::vector< std::string > &configs, const std::string &userDefines)
static void removeFunctionCalls(const std::string &calledFunction, std::map< std::string, std::vector< std::string >> &functionCalledBy, std::map< std::string, std::vector< std::string >> &functionCalls, std::vector< std::string > &add)
Definition: summaries.cpp:139
static std::vector< std::string > getSummaryData(const std::string &line, const std::string &data)
Definition: summaries.cpp:119
static std::vector< std::string > getSummaryFiles(const std::string &filename)
Definition: summaries.cpp:100