2016-10-29 12:18:11 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2018-01-14 15:37:52 +01:00
|
|
|
* Copyright (C) 2007-2017 Cppcheck team.
|
2016-10-29 12:18:11 +02:00
|
|
|
*
|
|
|
|
* 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 "analyzerinfo.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2016-10-29 12:18:11 +02:00
|
|
|
#include "path.h"
|
2017-04-01 18:14:18 +02:00
|
|
|
#include "utils.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2016-10-29 12:18:11 +02:00
|
|
|
#include <tinyxml2.h>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <map>
|
2016-11-14 20:50:08 +01:00
|
|
|
#include <sstream>
|
2016-10-29 12:18:11 +02:00
|
|
|
|
|
|
|
AnalyzerInformation::~AnalyzerInformation()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2016-12-08 23:12:59 +01:00
|
|
|
static std::string getFilename(const std::string &fullpath)
|
|
|
|
{
|
2016-12-08 22:46:44 +01:00
|
|
|
std::string::size_type pos1 = fullpath.find_last_of("/\\");
|
|
|
|
pos1 = (pos1 == std::string::npos) ? 0U : (pos1 + 1U);
|
|
|
|
std::string::size_type pos2 = fullpath.rfind('.');
|
|
|
|
if (pos2 < pos1)
|
|
|
|
pos2 = std::string::npos;
|
|
|
|
if (pos2 != std::string::npos)
|
|
|
|
pos2 = pos2 - pos1;
|
|
|
|
return fullpath.substr(pos1,pos2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnalyzerInformation::writeFilesTxt(const std::string &buildDir, const std::list<std::string> &sourcefiles, const std::list<ImportProject::FileSettings> &fileSettings)
|
|
|
|
{
|
|
|
|
std::map<std::string, unsigned int> fileCount;
|
|
|
|
|
|
|
|
const std::string filesTxt(buildDir + "/files.txt");
|
|
|
|
std::ofstream fout(filesTxt.c_str());
|
|
|
|
for (std::list<std::string>::const_iterator f = sourcefiles.begin(); f != sourcefiles.end(); ++f) {
|
|
|
|
const std::string afile = getFilename(*f);
|
|
|
|
if (fileCount.find(afile) == fileCount.end())
|
|
|
|
fileCount[afile] = 0;
|
|
|
|
fout << afile << ".a" << (++fileCount[afile]) << "::" << Path::fromNativeSeparators(*f) << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::list<ImportProject::FileSettings>::const_iterator fs = fileSettings.begin(); fs != fileSettings.end(); ++fs) {
|
|
|
|
const std::string afile = getFilename(fs->filename);
|
|
|
|
if (fileCount.find(afile) == fileCount.end())
|
|
|
|
fileCount[afile] = 0;
|
|
|
|
fout << afile << ".a" << (++fileCount[afile]) << ":" << fs->cfg << ":" << Path::fromNativeSeparators(fs->filename) << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-29 12:18:11 +02:00
|
|
|
void AnalyzerInformation::close()
|
|
|
|
{
|
|
|
|
analyzerInfoFile.clear();
|
|
|
|
if (fout.is_open()) {
|
|
|
|
fout << "</analyzerinfo>\n";
|
|
|
|
fout.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-29 18:18:29 +02:00
|
|
|
static bool skipAnalysis(const std::string &analyzerInfoFile, unsigned long long checksum, std::list<ErrorLogger::ErrorMessage> *errors)
|
2016-10-29 12:18:11 +02:00
|
|
|
{
|
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
tinyxml2::XMLError error = doc.LoadFile(analyzerInfoFile.c_str());
|
|
|
|
if (error != tinyxml2::XML_SUCCESS)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const tinyxml2::XMLElement * const rootNode = doc.FirstChildElement();
|
|
|
|
if (rootNode == nullptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const char *attr = rootNode->Attribute("checksum");
|
|
|
|
if (!attr || attr != std::to_string(checksum))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (const tinyxml2::XMLElement *e = rootNode->FirstChildElement(); e; e = e->NextSiblingElement()) {
|
|
|
|
if (std::strcmp(e->Name(), "error") == 0)
|
|
|
|
errors->push_back(ErrorLogger::ErrorMessage(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 22:46:44 +01:00
|
|
|
std::string AnalyzerInformation::getAnalyzerInfoFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg)
|
2016-11-05 21:26:56 +01:00
|
|
|
{
|
2016-11-14 20:50:08 +01:00
|
|
|
const std::string files(buildDir + "/files.txt");
|
|
|
|
std::ifstream fin(files.c_str());
|
|
|
|
if (fin.is_open()) {
|
|
|
|
std::string line;
|
2016-12-08 22:46:44 +01:00
|
|
|
const std::string endsWith(':' + cfg + ':' + sourcefile);
|
2016-11-14 20:50:08 +01:00
|
|
|
while (std::getline(fin,line)) {
|
2016-12-08 22:46:44 +01:00
|
|
|
if (line.size() <= endsWith.size() + 2U)
|
|
|
|
continue;
|
|
|
|
if (line.compare(line.size()-endsWith.size(), endsWith.size(), endsWith) != 0)
|
|
|
|
continue;
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << buildDir << '/' << line.substr(0,line.find(':'));
|
|
|
|
return ostr.str();
|
2016-11-14 20:50:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 21:26:56 +01:00
|
|
|
std::string filename = Path::fromNativeSeparators(buildDir);
|
2017-04-01 18:14:18 +02:00
|
|
|
if (!endsWith(filename, '/'))
|
2016-11-05 21:26:56 +01:00
|
|
|
filename += '/';
|
2016-11-30 12:01:22 +01:00
|
|
|
const std::string::size_type pos = sourcefile.rfind('/');
|
2016-11-05 21:26:56 +01:00
|
|
|
if (pos == std::string::npos)
|
|
|
|
filename += sourcefile;
|
|
|
|
else
|
|
|
|
filename += sourcefile.substr(pos+1);
|
|
|
|
filename += ".analyzerinfo";
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2016-12-08 22:46:44 +01:00
|
|
|
bool AnalyzerInformation::analyzeFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, unsigned long long checksum, std::list<ErrorLogger::ErrorMessage> *errors)
|
2016-10-29 12:18:11 +02:00
|
|
|
{
|
|
|
|
if (buildDir.empty() || sourcefile.empty())
|
|
|
|
return true;
|
|
|
|
close();
|
|
|
|
|
2016-12-08 22:46:44 +01:00
|
|
|
analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(buildDir,sourcefile,cfg);
|
2016-10-29 12:18:11 +02:00
|
|
|
|
2016-12-06 20:02:43 +01:00
|
|
|
if (skipAnalysis(analyzerInfoFile, checksum, errors))
|
2016-10-29 12:18:11 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
fout.open(analyzerInfoFile);
|
|
|
|
if (fout.is_open()) {
|
|
|
|
fout << "<?xml version=\"1.0\"?>\n";
|
2016-12-06 20:02:43 +01:00
|
|
|
fout << "<analyzerinfo checksum=\"" << checksum << "\">\n";
|
2016-10-29 12:18:11 +02:00
|
|
|
} else {
|
|
|
|
analyzerInfoFile.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-29 18:56:22 +02:00
|
|
|
void AnalyzerInformation::reportErr(const ErrorLogger::ErrorMessage &msg, bool /*verbose*/)
|
2016-10-29 12:18:11 +02:00
|
|
|
{
|
|
|
|
if (fout.is_open())
|
2017-07-29 18:56:22 +02:00
|
|
|
fout << msg.toXML() << '\n';
|
2016-10-29 12:18:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnalyzerInformation::setFileInfo(const std::string &check, const std::string &fileInfo)
|
|
|
|
{
|
|
|
|
if (fout.is_open() && !fileInfo.empty())
|
|
|
|
fout << " <FileInfo check=\"" << check << "\">\n" << fileInfo << " </FileInfo>\n";
|
|
|
|
}
|