Skip to content

Commit 96caaed

Browse files
committed
Generate basic function summaries
1 parent a770342 commit 96caaed

6 files changed

Lines changed: 150 additions & 0 deletions

File tree

cli/cppcheckexecutor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,8 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
896896
}
897897

898898
if (!settings.buildDir.empty()) {
899+
settings.loadSummaries();
900+
899901
std::list<std::string> fileNames;
900902
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.begin(); i != mFiles.end(); ++i)
901903
fileNames.emplace_back(i->first);

lib/settings.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include "valueflow.h"
2222

23+
#include <fstream>
24+
2325
std::atomic<bool> Settings::mTerminated;
2426

2527
const char Settings::SafeChecks::XmlRootName[] = "safe-checks";
@@ -155,3 +157,48 @@ bool Settings::isEnabled(const ValueFlow::Value *value, bool inconclusiveCheck)
155157
return false;
156158
return true;
157159
}
160+
161+
static std::vector<std::string> getSummaryFiles(const std::string &filename)
162+
{
163+
std::vector<std::string> ret;
164+
std::ifstream fin(filename);
165+
if (!fin.is_open())
166+
return ret;
167+
std::string line;
168+
while (std::getline(fin, line)) {
169+
std::string::size_type dotA = line.find(".a");
170+
std::string::size_type colon = line.find(":");
171+
if (colon > line.size() || dotA > colon)
172+
continue;
173+
std::string f = line.substr(0,colon);
174+
f[dotA + 1] = 's';
175+
ret.push_back(f);
176+
}
177+
return ret;
178+
}
179+
180+
void Settings::loadSummaries()
181+
{
182+
if (buildDir.empty())
183+
return;
184+
185+
std::vector<std::string> summaryFiles = getSummaryFiles(buildDir + "/files.txt");
186+
for (const std::string &filename: summaryFiles) {
187+
std::ifstream fin(buildDir + '/' + filename);
188+
if (!fin.is_open())
189+
continue;
190+
std::string line;
191+
while (std::getline(fin, line)) {
192+
// Get function name
193+
const std::string::size_type pos1 = 0;
194+
const std::string::size_type pos2 = line.find(" ", pos1);
195+
const std::string functionName = (pos2 == std::string::npos) ? line : line.substr(0, pos2);
196+
197+
// noreturn..
198+
if (line.find(" noreturn:[") != std::string::npos || line.find(" call:[") != std::string::npos)
199+
summaryNoreturn[functionName] = true;
200+
else if (summaryNoreturn.find(functionName) == summaryNoreturn.end())
201+
summaryNoreturn[functionName] = false;
202+
}
203+
}
204+
}

lib/settings.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ class CPPCHECKLIB Settings : public cppcheck::Platform {
415415
static bool terminated() {
416416
return Settings::mTerminated;
417417
}
418+
419+
std::map<std::string, bool> summaryNoreturn;
420+
421+
void loadSummaries();
422+
418423
};
419424

420425
/// @}

lib/tokenize.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//---------------------------------------------------------------------------
2020
#include "tokenize.h"
2121

22+
#include "analyzerinfo.h"
2223
#include "check.h"
2324
#include "errorlogger.h"
2425
#include "library.h"
@@ -2377,6 +2378,9 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration)
23772378
mSymbolDatabase->setValueTypeInTokenList(true);
23782379
}
23792380

2381+
if (!mSettings->buildDir.empty())
2382+
createSummaries(configuration);
2383+
23802384
if (mTimerResults) {
23812385
Timer t("Tokenizer::simplifyTokens1::ValueFlow", mSettings->showtime, mTimerResults);
23822386
ValueFlow::setValues(&list, mSymbolDatabase, mErrorLogger, mSettings);
@@ -8720,6 +8724,11 @@ bool Tokenizer::isScopeNoReturn(const Token *endScopeToken, bool *unknown) const
87208724
{
87218725
std::string unknownFunc;
87228726
const bool ret = mSettings->library.isScopeNoReturn(endScopeToken,&unknownFunc);
8727+
if (!unknownFunc.empty()) {
8728+
const std::map<std::string, bool>::const_iterator it = mSettings->summaryNoreturn.find(unknownFunc);
8729+
if (it != mSettings->summaryNoreturn.end() && !it->second)
8730+
return false;
8731+
}
87238732
if (unknown)
87248733
*unknown = !unknownFunc.empty();
87258734
if (!unknownFunc.empty() && mSettings->checkLibrary && mSettings->isEnabled(Settings::INFORMATION)) {
@@ -12064,3 +12073,60 @@ bool Tokenizer::hasIfdef(const Token *start, const Token *end) const
1206412073
return false;
1206512074
}
1206612075

12076+
std::string Tokenizer::createSummaries(const std::string &configuration) const
12077+
{
12078+
std::ostringstream ostr;
12079+
for (const Scope *scope : mSymbolDatabase->functionScopes) {
12080+
const Function *f = scope->function;
12081+
if (!f)
12082+
continue;
12083+
12084+
// Summarize function
12085+
std::set<std::string> noreturn;
12086+
std::set<std::string> globalVars;
12087+
std::set<std::string> calledFunctions;
12088+
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
12089+
if (tok->variable() && tok->variable()->isGlobal())
12090+
globalVars.insert(tok->variable()->name());
12091+
if (Token::Match(tok, "%name% (") && !Token::simpleMatch(tok->linkAt(1), ") {")) {
12092+
calledFunctions.insert(tok->str());
12093+
if (Token::simpleMatch(tok->linkAt(1), ") ; }"))
12094+
noreturn.insert(tok->str());
12095+
}
12096+
}
12097+
12098+
// Write summary for function
12099+
auto join = [](const std::set<std::string> &data) -> std::string {
12100+
std::string ret;
12101+
const char *sep = "";
12102+
for (std::string d: data)
12103+
{
12104+
ret += sep + d;
12105+
sep = ",";
12106+
}
12107+
return ret;
12108+
};
12109+
12110+
ostr << f->name();
12111+
if (!globalVars.empty())
12112+
ostr << " global:[" << join(globalVars) << "]";
12113+
if (!calledFunctions.empty())
12114+
ostr << " call:[" << join(calledFunctions) << "]";
12115+
if (!noreturn.empty())
12116+
ostr << " noreturn:[" << join(noreturn) << "]";
12117+
ostr << std::endl;
12118+
}
12119+
12120+
if (!mSettings->buildDir.empty()) {
12121+
std::string filename = AnalyzerInformation::getAnalyzerInfoFile(mSettings->buildDir, list.getSourceFilePath(), configuration);
12122+
std::string::size_type pos = filename.rfind(".a");
12123+
if (pos != std::string::npos) {
12124+
filename[pos+1] = 's';
12125+
std::ofstream fout(filename);
12126+
fout << ostr.str();
12127+
}
12128+
}
12129+
12130+
return ostr.str();
12131+
}
12132+

lib/tokenize.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,8 @@ class CPPCHECKLIB Tokenizer {
585585

586586
bool hasIfdef(const Token *start, const Token *end) const;
587587

588+
std::string createSummaries(const std::string &configuration) const;
589+
588590
private:
589591

590592
/**

test/testtokenize.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,10 @@ class TestTokenizer : public TestFixture {
520520
TEST_CASE(checkHeader1);
521521

522522
TEST_CASE(removeExtraTemplateKeywords);
523+
524+
TEST_CASE(createSummaries1);
525+
TEST_CASE(createSummariesGlobal);
526+
TEST_CASE(createSummariesNoreturn);
523527
}
524528

525529
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) {
@@ -8749,6 +8753,30 @@ class TestTokenizer : public TestFixture {
87498753
const char expected2[] = "GridView :: Codim < 0 > :: Iterator it ; it = gv . begin < 0 > ( ) ;";
87508754
ASSERT_EQUALS(expected2, tokenizeAndStringify(code2, false));
87518755
}
8756+
8757+
std::string createSummaries(const char code[], const char filename[] = "test.cpp") {
8758+
// Clear the error buffer..
8759+
errout.str("");
8760+
8761+
// tokenize..
8762+
Settings settings;
8763+
Tokenizer tokenizer(&settings, this);
8764+
std::istringstream istr(code);
8765+
tokenizer.tokenize(istr, filename);
8766+
return tokenizer.createSummaries("");
8767+
}
8768+
8769+
void createSummaries1() {
8770+
ASSERT_EQUALS("foo\n", createSummaries("void foo() {}"));
8771+
}
8772+
8773+
void createSummariesGlobal() {
8774+
ASSERT_EQUALS("foo global:[x]\n", createSummaries("int x; void foo() { x=0; }"));
8775+
}
8776+
8777+
void createSummariesNoreturn() {
8778+
ASSERT_EQUALS("foo call:[bar] noreturn:[bar]\n", createSummaries("void foo() { bar(); }"));
8779+
}
87528780
};
87538781

87548782
REGISTER_TEST(TestTokenizer)

0 commit comments

Comments
 (0)