|
| 1 | + |
| 2 | +#include "summaries.h" |
| 3 | + |
| 4 | +#include "analyzerinfo.h" |
| 5 | +#include "settings.h" |
| 6 | +#include "symboldatabase.h" |
| 7 | +#include "tokenize.h" |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | +#include <fstream> |
| 11 | +#include <map> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +std::string Summaries::create(const Tokenizer *tokenizer, const std::string &cfg) |
| 17 | +{ |
| 18 | + const SymbolDatabase *symbolDatabase = tokenizer->getSymbolDatabase(); |
| 19 | + const Settings *settings = tokenizer->getSettings(); |
| 20 | + |
| 21 | + std::ostringstream ostr; |
| 22 | + for (const Scope *scope : symbolDatabase->functionScopes) { |
| 23 | + const Function *f = scope->function; |
| 24 | + if (!f) |
| 25 | + continue; |
| 26 | + |
| 27 | + // Summarize function |
| 28 | + std::set<std::string> noreturn; |
| 29 | + std::set<std::string> globalVars; |
| 30 | + std::set<std::string> calledFunctions; |
| 31 | + for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { |
| 32 | + if (tok->variable() && tok->variable()->isGlobal()) |
| 33 | + globalVars.insert(tok->variable()->name()); |
| 34 | + if (Token::Match(tok, "%name% (") && !Token::simpleMatch(tok->linkAt(1), ") {")) { |
| 35 | + calledFunctions.insert(tok->str()); |
| 36 | + if (Token::simpleMatch(tok->linkAt(1), ") ; }")) |
| 37 | + noreturn.insert(tok->str()); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // Write summary for function |
| 42 | + auto join = [](const std::set<std::string> &data) -> std::string { |
| 43 | + std::string ret; |
| 44 | + const char *sep = ""; |
| 45 | + for (std::string d: data) |
| 46 | + { |
| 47 | + ret += sep + d; |
| 48 | + sep = ","; |
| 49 | + } |
| 50 | + return ret; |
| 51 | + }; |
| 52 | + |
| 53 | + ostr << f->name(); |
| 54 | + if (!globalVars.empty()) |
| 55 | + ostr << " global:[" << join(globalVars) << "]"; |
| 56 | + if (!calledFunctions.empty()) |
| 57 | + ostr << " call:[" << join(calledFunctions) << "]"; |
| 58 | + if (!noreturn.empty()) |
| 59 | + ostr << " noreturn:[" << join(noreturn) << "]"; |
| 60 | + ostr << std::endl; |
| 61 | + } |
| 62 | + |
| 63 | + if (!settings->buildDir.empty()) { |
| 64 | + std::string filename = AnalyzerInformation::getAnalyzerInfoFile(settings->buildDir, tokenizer->list.getSourceFilePath(), cfg); |
| 65 | + std::string::size_type pos = filename.rfind(".a"); |
| 66 | + if (pos != std::string::npos) { |
| 67 | + filename[pos+1] = 's'; |
| 68 | + std::ofstream fout(filename); |
| 69 | + fout << ostr.str(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return ostr.str(); |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +static std::vector<std::string> getSummaryFiles(const std::string &filename) |
| 80 | +{ |
| 81 | + std::vector<std::string> ret; |
| 82 | + std::ifstream fin(filename); |
| 83 | + if (!fin.is_open()) |
| 84 | + return ret; |
| 85 | + std::string line; |
| 86 | + while (std::getline(fin, line)) { |
| 87 | + std::string::size_type dotA = line.find(".a"); |
| 88 | + std::string::size_type colon = line.find(":"); |
| 89 | + if (colon > line.size() || dotA > colon) |
| 90 | + continue; |
| 91 | + std::string f = line.substr(0,colon); |
| 92 | + f[dotA + 1] = 's'; |
| 93 | + ret.push_back(f); |
| 94 | + } |
| 95 | + return ret; |
| 96 | +} |
| 97 | + |
| 98 | +static std::vector<std::string> getSummaryData(const std::string &line, const std::string &data) |
| 99 | +{ |
| 100 | + std::vector<std::string> ret; |
| 101 | + const std::string::size_type start = line.find(" " + data + ":["); |
| 102 | + if (start == std::string::npos) |
| 103 | + return ret; |
| 104 | + const std::string::size_type end = line.find("]", start); |
| 105 | + if (end >= line.size()) |
| 106 | + return ret; |
| 107 | + |
| 108 | + std::string::size_type pos1 = start + 3 + data.size(); |
| 109 | + while (pos1 < end) { |
| 110 | + std::string::size_type pos2 = line.find_first_of(",]",pos1); |
| 111 | + ret.push_back(line.substr(pos1, pos2-pos1-1)); |
| 112 | + pos1 = pos2 + 1; |
| 113 | + } |
| 114 | + |
| 115 | + return ret; |
| 116 | +} |
| 117 | + |
| 118 | +static void removeFunctionCalls(const std::string& calledFunction, |
| 119 | + std::map<std::string, std::vector<std::string>> &functionCalledBy, |
| 120 | + std::map<std::string, std::vector<std::string>> &functionCalls, |
| 121 | + std::vector<std::string> &add) |
| 122 | +{ |
| 123 | + std::vector<std::string> calledBy = functionCalledBy[calledFunction]; |
| 124 | + functionCalledBy.erase(calledFunction); |
| 125 | + for (const std::string &c: calledBy) { |
| 126 | + std::vector<std::string> &calls = functionCalls[c]; |
| 127 | + calls.erase(std::remove(calls.begin(), calls.end(), calledFunction), calls.end()); |
| 128 | + if (calls.empty()) { |
| 129 | + add.push_back(calledFunction); |
| 130 | + removeFunctionCalls(c, functionCalledBy, functionCalls, add); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +void Summaries::loadReturn(const std::string &buildDir, std::set<std::string> &summaryReturn) |
| 136 | +{ |
| 137 | + if (buildDir.empty()) |
| 138 | + return; |
| 139 | + |
| 140 | + std::vector<std::string> return1; |
| 141 | + std::map<std::string, std::vector<std::string>> functionCalls; |
| 142 | + std::map<std::string, std::vector<std::string>> functionCalledBy; |
| 143 | + |
| 144 | + // extract "functionNoreturn" and "functionCalledBy" from summaries |
| 145 | + std::vector<std::string> summaryFiles = getSummaryFiles(buildDir + "/files.txt"); |
| 146 | + for (const std::string &filename: summaryFiles) { |
| 147 | + std::ifstream fin(buildDir + '/' + filename); |
| 148 | + if (!fin.is_open()) |
| 149 | + continue; |
| 150 | + std::string line; |
| 151 | + while (std::getline(fin, line)) { |
| 152 | + // Get function name |
| 153 | + const std::string::size_type pos1 = 0; |
| 154 | + const std::string::size_type pos2 = line.find(" ", pos1); |
| 155 | + const std::string functionName = (pos2 == std::string::npos) ? line : line.substr(0, pos2); |
| 156 | + std::vector<std::string> call = getSummaryData(line, "call"); |
| 157 | + functionCalls[functionName] = call; |
| 158 | + if (call.empty()) |
| 159 | + return1.push_back(functionName); |
| 160 | + else { |
| 161 | + for (const std::string &c: call) { |
| 162 | + functionCalledBy[c].push_back(functionName); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + summaryReturn.insert(return1.cbegin(), return1.cend()); |
| 168 | + |
| 169 | + // recursively set "summaryNoreturn" |
| 170 | + for (const std::string &f: return1) { |
| 171 | + std::vector<std::string> return2; |
| 172 | + removeFunctionCalls(f, functionCalledBy, functionCalls, return2); |
| 173 | + summaryReturn.insert(return2.cbegin(), return2.cend()); |
| 174 | + } |
| 175 | +} |
0 commit comments