Summaries: Moved to its own files

This commit is contained in:
Daniel Marjamäki 2020-12-20 19:53:58 +01:00
parent 5701f6d368
commit 37a5ec8cd5
12 changed files with 295 additions and 192 deletions

View File

@ -203,6 +203,7 @@ LIBOBJ = $(libcppdir)/analyzerinfo.o \
$(libcppdir)/programmemory.o \ $(libcppdir)/programmemory.o \
$(libcppdir)/reverseanalyzer.o \ $(libcppdir)/reverseanalyzer.o \
$(libcppdir)/settings.o \ $(libcppdir)/settings.o \
$(libcppdir)/summaries.o \
$(libcppdir)/suppressions.o \ $(libcppdir)/suppressions.o \
$(libcppdir)/symboldatabase.o \ $(libcppdir)/symboldatabase.o \
$(libcppdir)/templatesimplifier.o \ $(libcppdir)/templatesimplifier.o \
@ -270,6 +271,7 @@ TESTOBJ = test/options.o \
test/teststl.o \ test/teststl.o \
test/teststring.o \ test/teststring.o \
test/testsuite.o \ test/testsuite.o \
test/testsummaries.o \
test/testsuppressions.o \ test/testsuppressions.o \
test/testsymboldatabase.o \ test/testsymboldatabase.o \
test/testthreadexecutor.o \ test/testthreadexecutor.o \
@ -542,9 +544,12 @@ $(libcppdir)/programmemory.o: lib/programmemory.cpp lib/astutils.h lib/config.h
$(libcppdir)/reverseanalyzer.o: lib/reverseanalyzer.cpp lib/analyzer.h lib/astutils.h lib/config.h lib/errortypes.h lib/forwardanalyzer.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/reverseanalyzer.h lib/settings.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/utils.h lib/valueflow.h lib/valueptr.h $(libcppdir)/reverseanalyzer.o: lib/reverseanalyzer.cpp lib/analyzer.h lib/astutils.h lib/config.h lib/errortypes.h lib/forwardanalyzer.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/reverseanalyzer.h lib/settings.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/utils.h lib/valueflow.h lib/valueptr.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(libcppdir)/reverseanalyzer.o $(libcppdir)/reverseanalyzer.cpp $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(libcppdir)/reverseanalyzer.o $(libcppdir)/reverseanalyzer.cpp
$(libcppdir)/settings.o: lib/settings.cpp lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h lib/valueflow.h $(libcppdir)/settings.o: lib/settings.cpp lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/summaries.h lib/suppressions.h lib/timer.h lib/utils.h lib/valueflow.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(libcppdir)/settings.o $(libcppdir)/settings.cpp $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(libcppdir)/settings.o $(libcppdir)/settings.cpp
$(libcppdir)/summaries.o: lib/summaries.cpp lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/summaries.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/utils.h lib/valueflow.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(libcppdir)/summaries.o $(libcppdir)/summaries.cpp
$(libcppdir)/suppressions.o: lib/suppressions.cpp externals/tinyxml2/tinyxml2.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/mathlib.h lib/path.h lib/suppressions.h lib/utils.h $(libcppdir)/suppressions.o: lib/suppressions.cpp externals/tinyxml2/tinyxml2.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/mathlib.h lib/path.h lib/suppressions.h lib/utils.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(libcppdir)/suppressions.o $(libcppdir)/suppressions.cpp $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(libcppdir)/suppressions.o $(libcppdir)/suppressions.cpp
@ -560,7 +565,7 @@ $(libcppdir)/timer.o: lib/timer.cpp lib/config.h lib/timer.h
$(libcppdir)/token.o: lib/token.cpp lib/astutils.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenlist.h lib/utils.h lib/valueflow.h $(libcppdir)/token.o: lib/token.cpp lib/astutils.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenlist.h lib/utils.h lib/valueflow.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(libcppdir)/token.o $(libcppdir)/token.cpp $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(libcppdir)/token.o $(libcppdir)/token.cpp
$(libcppdir)/tokenize.o: lib/tokenize.cpp externals/simplecpp/simplecpp.h lib/check.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h $(libcppdir)/tokenize.o: lib/tokenize.cpp externals/simplecpp/simplecpp.h lib/analyzerinfo.h lib/check.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(libcppdir)/tokenize.o $(libcppdir)/tokenize.cpp $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(libcppdir)/tokenize.o $(libcppdir)/tokenize.cpp
$(libcppdir)/tokenlist.o: lib/tokenlist.cpp externals/simplecpp/simplecpp.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenlist.h lib/utils.h lib/valueflow.h $(libcppdir)/tokenlist.o: lib/tokenlist.cpp externals/simplecpp/simplecpp.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenlist.h lib/utils.h lib/valueflow.h
@ -731,6 +736,9 @@ test/teststring.o: test/teststring.cpp lib/check.h lib/checkstring.h lib/config.
test/testsuite.o: test/testsuite.cpp lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h test/options.h test/redirect.h test/testsuite.h test/testsuite.o: test/testsuite.cpp lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h test/options.h test/redirect.h test/testsuite.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testsuite.o test/testsuite.cpp $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testsuite.o test/testsuite.cpp
test/testsummaries.o: test/testsummaries.cpp lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/summaries.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testsummaries.o test/testsummaries.cpp
test/testsuppressions.o: test/testsuppressions.cpp cli/threadexecutor.h lib/analyzerinfo.h lib/check.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h test/testsuite.h test/testsuppressions.o: test/testsuppressions.cpp cli/threadexecutor.h lib/analyzerinfo.h lib/check.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h test/testsuite.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testsuppressions.o test/testsuppressions.cpp $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testsuppressions.o test/testsuppressions.cpp

View File

@ -91,6 +91,7 @@
<ClCompile Include="importproject.cpp" /> <ClCompile Include="importproject.cpp" />
<ClCompile Include="programmemory.cpp" /> <ClCompile Include="programmemory.cpp" />
<ClCompile Include="settings.cpp" /> <ClCompile Include="settings.cpp" />
<ClCompile Include="summaries.cpp" />
<ClCompile Include="suppressions.cpp" /> <ClCompile Include="suppressions.cpp" />
<ClCompile Include="symboldatabase.cpp" /> <ClCompile Include="symboldatabase.cpp" />
<ClCompile Include="templatesimplifier.cpp" /> <ClCompile Include="templatesimplifier.cpp" />
@ -150,6 +151,7 @@
<ClInclude Include="importproject.h" /> <ClInclude Include="importproject.h" />
<ClInclude Include="programmemory.h" /> <ClInclude Include="programmemory.h" />
<ClInclude Include="settings.h" /> <ClInclude Include="settings.h" />
<ClInclude Include="summaries.h" />
<ClInclude Include="suppressions.h" /> <ClInclude Include="suppressions.h" />
<ClInclude Include="symboldatabase.h" /> <ClInclude Include="symboldatabase.h" />
<ClInclude Include="templatesimplifier.h" /> <ClInclude Include="templatesimplifier.h" />

View File

@ -50,6 +50,7 @@ HEADERS += $${PWD}/analyzerinfo.h \
$${PWD}/programmemory.h \ $${PWD}/programmemory.h \
$${PWD}/reverseanalyzer.h \ $${PWD}/reverseanalyzer.h \
$${PWD}/settings.h \ $${PWD}/settings.h \
$${PWD}/summaries.h \
$${PWD}/suppressions.h \ $${PWD}/suppressions.h \
$${PWD}/symboldatabase.h \ $${PWD}/symboldatabase.h \
$${PWD}/templatesimplifier.h \ $${PWD}/templatesimplifier.h \
@ -107,6 +108,7 @@ SOURCES += $${PWD}/analyzerinfo.cpp \
$${PWD}/programmemory.cpp \ $${PWD}/programmemory.cpp \
$${PWD}/reverseanalyzer.cpp \ $${PWD}/reverseanalyzer.cpp \
$${PWD}/settings.cpp \ $${PWD}/settings.cpp \
$${PWD}/summaries.cpp \
$${PWD}/suppressions.cpp \ $${PWD}/suppressions.cpp \
$${PWD}/symboldatabase.cpp \ $${PWD}/symboldatabase.cpp \
$${PWD}/templatesimplifier.cpp \ $${PWD}/templatesimplifier.cpp \

View File

@ -17,12 +17,9 @@
*/ */
#include "settings.h" #include "settings.h"
#include "summaries.h"
#include "valueflow.h" #include "valueflow.h"
#include <algorithm>
#include <fstream>
std::atomic<bool> Settings::mTerminated; std::atomic<bool> Settings::mTerminated;
const char Settings::SafeChecks::XmlRootName[] = "safe-checks"; const char Settings::SafeChecks::XmlRootName[] = "safe-checks";
@ -159,100 +156,7 @@ bool Settings::isEnabled(const ValueFlow::Value *value, bool inconclusiveCheck)
return true; return true;
} }
static std::vector<std::string> getSummaryFiles(const std::string &filename)
{
std::vector<std::string> ret;
std::ifstream fin(filename);
if (!fin.is_open())
return ret;
std::string line;
while (std::getline(fin, line)) {
std::string::size_type dotA = line.find(".a");
std::string::size_type colon = line.find(":");
if (colon > line.size() || dotA > colon)
continue;
std::string f = line.substr(0,colon);
f[dotA + 1] = 's';
ret.push_back(f);
}
return ret;
}
static std::vector<std::string> getSummaryData(const std::string &line, const std::string &data)
{
std::vector<std::string> ret;
const std::string::size_type start = line.find(" " + data + ":[");
if (start == std::string::npos)
return ret;
const std::string::size_type end = line.find("]", start);
if (end >= line.size())
return ret;
std::string::size_type pos1 = start + 3 + data.size();
while (pos1 < end) {
std::string::size_type pos2 = line.find_first_of(",]",pos1);
ret.push_back(line.substr(pos1, pos2-pos1-1));
pos1 = pos2 + 1;
}
return ret;
}
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)
{
std::vector<std::string> calledBy = functionCalledBy[calledFunction];
functionCalledBy.erase(calledFunction);
for (const std::string &c: calledBy) {
std::vector<std::string> &calls = functionCalls[c];
calls.erase(std::remove(calls.begin(), calls.end(), calledFunction), calls.end());
if (calls.empty()) {
add.push_back(calledFunction);
removeFunctionCalls(c, functionCalledBy, functionCalls, add);
}
}
}
void Settings::loadSummaries() void Settings::loadSummaries()
{ {
if (buildDir.empty()) Summaries::loadReturn(buildDir, summaryReturn);
return;
std::vector<std::string> return1;
std::map<std::string, std::vector<std::string>> functionCalls;
std::map<std::string, std::vector<std::string>> functionCalledBy;
// extract "functionNoreturn" and "functionCalledBy" from summaries
std::vector<std::string> summaryFiles = getSummaryFiles(buildDir + "/files.txt");
for (const std::string &filename: summaryFiles) {
std::ifstream fin(buildDir + '/' + filename);
if (!fin.is_open())
continue;
std::string line;
while (std::getline(fin, line)) {
// Get function name
const std::string::size_type pos1 = 0;
const std::string::size_type pos2 = line.find(" ", pos1);
const std::string functionName = (pos2 == std::string::npos) ? line : line.substr(0, pos2);
std::vector<std::string> call = getSummaryData(line, "call");
functionCalls[functionName] = call;
if (call.empty())
return1.push_back(functionName);
else {
for (const std::string &c: call) {
functionCalledBy[c].push_back(functionName);
}
}
}
}
summaryReturn.insert(return1.cbegin(), return1.cend());
// recursively set "summaryNoreturn"
for (const std::string &f: return1) {
std::vector<std::string> return2;
removeFunctionCalls(f, functionCalledBy, functionCalls, return2);
summaryReturn.insert(return2.cbegin(), return2.cend());
}
} }

View File

@ -419,7 +419,6 @@ public:
std::set<std::string> summaryReturn; std::set<std::string> summaryReturn;
void loadSummaries(); void loadSummaries();
}; };
/// @} /// @}

175
lib/summaries.cpp Normal file
View File

@ -0,0 +1,175 @@
#include "summaries.h"
#include "analyzerinfo.h"
#include "settings.h"
#include "symboldatabase.h"
#include "tokenize.h"
#include <algorithm>
#include <fstream>
#include <map>
#include <vector>
std::string Summaries::create(const Tokenizer *tokenizer, const std::string &cfg)
{
const SymbolDatabase *symbolDatabase = tokenizer->getSymbolDatabase();
const Settings *settings = tokenizer->getSettings();
std::ostringstream ostr;
for (const Scope *scope : symbolDatabase->functionScopes) {
const Function *f = scope->function;
if (!f)
continue;
// Summarize function
std::set<std::string> noreturn;
std::set<std::string> globalVars;
std::set<std::string> calledFunctions;
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
if (tok->variable() && tok->variable()->isGlobal())
globalVars.insert(tok->variable()->name());
if (Token::Match(tok, "%name% (") && !Token::simpleMatch(tok->linkAt(1), ") {")) {
calledFunctions.insert(tok->str());
if (Token::simpleMatch(tok->linkAt(1), ") ; }"))
noreturn.insert(tok->str());
}
}
// Write summary for function
auto join = [](const std::set<std::string> &data) -> std::string {
std::string ret;
const char *sep = "";
for (std::string d: data)
{
ret += sep + d;
sep = ",";
}
return ret;
};
ostr << f->name();
if (!globalVars.empty())
ostr << " global:[" << join(globalVars) << "]";
if (!calledFunctions.empty())
ostr << " call:[" << join(calledFunctions) << "]";
if (!noreturn.empty())
ostr << " noreturn:[" << join(noreturn) << "]";
ostr << std::endl;
}
if (!settings->buildDir.empty()) {
std::string filename = AnalyzerInformation::getAnalyzerInfoFile(settings->buildDir, tokenizer->list.getSourceFilePath(), cfg);
std::string::size_type pos = filename.rfind(".a");
if (pos != std::string::npos) {
filename[pos+1] = 's';
std::ofstream fout(filename);
fout << ostr.str();
}
}
return ostr.str();
}
static std::vector<std::string> getSummaryFiles(const std::string &filename)
{
std::vector<std::string> ret;
std::ifstream fin(filename);
if (!fin.is_open())
return ret;
std::string line;
while (std::getline(fin, line)) {
std::string::size_type dotA = line.find(".a");
std::string::size_type colon = line.find(":");
if (colon > line.size() || dotA > colon)
continue;
std::string f = line.substr(0,colon);
f[dotA + 1] = 's';
ret.push_back(f);
}
return ret;
}
static std::vector<std::string> getSummaryData(const std::string &line, const std::string &data)
{
std::vector<std::string> ret;
const std::string::size_type start = line.find(" " + data + ":[");
if (start == std::string::npos)
return ret;
const std::string::size_type end = line.find("]", start);
if (end >= line.size())
return ret;
std::string::size_type pos1 = start + 3 + data.size();
while (pos1 < end) {
std::string::size_type pos2 = line.find_first_of(",]",pos1);
ret.push_back(line.substr(pos1, pos2-pos1-1));
pos1 = pos2 + 1;
}
return ret;
}
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)
{
std::vector<std::string> calledBy = functionCalledBy[calledFunction];
functionCalledBy.erase(calledFunction);
for (const std::string &c: calledBy) {
std::vector<std::string> &calls = functionCalls[c];
calls.erase(std::remove(calls.begin(), calls.end(), calledFunction), calls.end());
if (calls.empty()) {
add.push_back(calledFunction);
removeFunctionCalls(c, functionCalledBy, functionCalls, add);
}
}
}
void Summaries::loadReturn(const std::string &buildDir, std::set<std::string> &summaryReturn)
{
if (buildDir.empty())
return;
std::vector<std::string> return1;
std::map<std::string, std::vector<std::string>> functionCalls;
std::map<std::string, std::vector<std::string>> functionCalledBy;
// extract "functionNoreturn" and "functionCalledBy" from summaries
std::vector<std::string> summaryFiles = getSummaryFiles(buildDir + "/files.txt");
for (const std::string &filename: summaryFiles) {
std::ifstream fin(buildDir + '/' + filename);
if (!fin.is_open())
continue;
std::string line;
while (std::getline(fin, line)) {
// Get function name
const std::string::size_type pos1 = 0;
const std::string::size_type pos2 = line.find(" ", pos1);
const std::string functionName = (pos2 == std::string::npos) ? line : line.substr(0, pos2);
std::vector<std::string> call = getSummaryData(line, "call");
functionCalls[functionName] = call;
if (call.empty())
return1.push_back(functionName);
else {
for (const std::string &c: call) {
functionCalledBy[c].push_back(functionName);
}
}
}
}
summaryReturn.insert(return1.cbegin(), return1.cend());
// recursively set "summaryNoreturn"
for (const std::string &f: return1) {
std::vector<std::string> return2;
removeFunctionCalls(f, functionCalledBy, functionCalls, return2);
summaryReturn.insert(return2.cbegin(), return2.cend());
}
}

36
lib/summaries.h Normal file
View File

@ -0,0 +1,36 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2020 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
#ifndef summariesH
#define summariesH
//---------------------------------------------------------------------------
#include <set>
#include <string>
class Tokenizer;
namespace Summaries {
std::string create(const Tokenizer *tokenizer, const std::string &cfg);
void loadReturn(const std::string &buildDir, std::set<std::string> &summaryReturn);
}
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------

View File

@ -19,7 +19,6 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "tokenize.h" #include "tokenize.h"
#include "analyzerinfo.h"
#include "check.h" #include "check.h"
#include "errorlogger.h" #include "errorlogger.h"
#include "library.h" #include "library.h"
@ -28,6 +27,7 @@
#include "preprocessor.h" #include "preprocessor.h"
#include "settings.h" #include "settings.h"
#include "standards.h" #include "standards.h"
#include "summaries.h"
#include "symboldatabase.h" #include "symboldatabase.h"
#include "templatesimplifier.h" #include "templatesimplifier.h"
#include "timer.h" #include "timer.h"
@ -2379,7 +2379,7 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration)
} }
if (!mSettings->buildDir.empty()) if (!mSettings->buildDir.empty())
createSummaries(configuration); Summaries::create(this, configuration);
if (mTimerResults) { if (mTimerResults) {
Timer t("Tokenizer::simplifyTokens1::ValueFlow", mSettings->showtime, mTimerResults); Timer t("Tokenizer::simplifyTokens1::ValueFlow", mSettings->showtime, mTimerResults);
@ -12070,61 +12070,3 @@ bool Tokenizer::hasIfdef(const Token *start, const Token *end) const
} }
return false; return false;
} }
std::string Tokenizer::createSummaries(const std::string &configuration) const
{
std::ostringstream ostr;
for (const Scope *scope : mSymbolDatabase->functionScopes) {
const Function *f = scope->function;
if (!f)
continue;
// Summarize function
std::set<std::string> noreturn;
std::set<std::string> globalVars;
std::set<std::string> calledFunctions;
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
if (tok->variable() && tok->variable()->isGlobal())
globalVars.insert(tok->variable()->name());
if (Token::Match(tok, "%name% (") && !Token::simpleMatch(tok->linkAt(1), ") {")) {
calledFunctions.insert(tok->str());
if (Token::simpleMatch(tok->linkAt(1), ") ; }"))
noreturn.insert(tok->str());
}
}
// Write summary for function
auto join = [](const std::set<std::string> &data) -> std::string {
std::string ret;
const char *sep = "";
for (std::string d: data)
{
ret += sep + d;
sep = ",";
}
return ret;
};
ostr << f->name();
if (!globalVars.empty())
ostr << " global:[" << join(globalVars) << "]";
if (!calledFunctions.empty())
ostr << " call:[" << join(calledFunctions) << "]";
if (!noreturn.empty())
ostr << " noreturn:[" << join(noreturn) << "]";
ostr << std::endl;
}
if (!mSettings->buildDir.empty()) {
std::string filename = AnalyzerInformation::getAnalyzerInfoFile(mSettings->buildDir, list.getSourceFilePath(), configuration);
std::string::size_type pos = filename.rfind(".a");
if (pos != std::string::npos) {
filename[pos+1] = 's';
std::ofstream fout(filename);
fout << ostr.str();
}
}
return ostr.str();
}

View File

@ -585,8 +585,6 @@ public:
bool hasIfdef(const Token *start, const Token *end) const; bool hasIfdef(const Token *start, const Token *end) const;
std::string createSummaries(const std::string &configuration) const;
private: private:
/** /**

View File

@ -80,6 +80,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile> </ClCompile>
<ClCompile Include="testsummaries.cpp" />
<ClCompile Include="testsuppressions.cpp" /> <ClCompile Include="testsuppressions.cpp" />
<ClCompile Include="testsymboldatabase.cpp" /> <ClCompile Include="testsymboldatabase.cpp" />
<ClCompile Include="testthreadexecutor.cpp" /> <ClCompile Include="testthreadexecutor.cpp" />

64
test/testsummaries.cpp Normal file
View File

@ -0,0 +1,64 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2020 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "summaries.h"
#include "testsuite.h"
#include "settings.h"
#include "tokenize.h"
class TestSummaries : public TestFixture {
public:
TestSummaries() : TestFixture("TestSummaries") {}
private:
void run() OVERRIDE {
TEST_CASE(createSummaries1);
TEST_CASE(createSummariesGlobal);
TEST_CASE(createSummariesNoreturn);
}
std::string createSummaries(const char code[], const char filename[] = "test.cpp") {
// Clear the error buffer..
errout.str("");
// tokenize..
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
return Summaries::create(&tokenizer, "");
}
void createSummaries1() {
ASSERT_EQUALS("foo\n", createSummaries("void foo() {}"));
}
void createSummariesGlobal() {
ASSERT_EQUALS("foo global:[x]\n", createSummaries("int x; void foo() { x=0; }"));
}
void createSummariesNoreturn() {
ASSERT_EQUALS("foo call:[bar] noreturn:[bar]\n", createSummaries("void foo() { bar(); }"));
}
};
REGISTER_TEST(TestSummaries)

View File

@ -520,10 +520,6 @@ private:
TEST_CASE(checkHeader1); TEST_CASE(checkHeader1);
TEST_CASE(removeExtraTemplateKeywords); TEST_CASE(removeExtraTemplateKeywords);
TEST_CASE(createSummaries1);
TEST_CASE(createSummariesGlobal);
TEST_CASE(createSummariesNoreturn);
} }
std::string tokenizeAndStringify(const char code[], bool simplify = false, bool expand = true, Settings::PlatformType platform = Settings::Native, const char* filename = "test.cpp", bool cpp11 = true) { std::string tokenizeAndStringify(const char code[], bool simplify = false, bool expand = true, Settings::PlatformType platform = Settings::Native, const char* filename = "test.cpp", bool cpp11 = true) {
@ -8753,30 +8749,6 @@ private:
const char expected2[] = "GridView :: Codim < 0 > :: Iterator it ; it = gv . begin < 0 > ( ) ;"; const char expected2[] = "GridView :: Codim < 0 > :: Iterator it ; it = gv . begin < 0 > ( ) ;";
ASSERT_EQUALS(expected2, tokenizeAndStringify(code2, false)); ASSERT_EQUALS(expected2, tokenizeAndStringify(code2, false));
} }
std::string createSummaries(const char code[], const char filename[] = "test.cpp") {
// Clear the error buffer..
errout.str("");
// tokenize..
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
return tokenizer.createSummaries("");
}
void createSummaries1() {
ASSERT_EQUALS("foo\n", createSummaries("void foo() {}"));
}
void createSummariesGlobal() {
ASSERT_EQUALS("foo global:[x]\n", createSummaries("int x; void foo() { x=0; }"));
}
void createSummariesNoreturn() {
ASSERT_EQUALS("foo call:[bar] noreturn:[bar]\n", createSummaries("void foo() { bar(); }"));
}
}; };
REGISTER_TEST(TestTokenizer) REGISTER_TEST(TestTokenizer)