2016-10-29 12:18:11 +02:00
|
|
|
/*
|
2021-08-07 20:51:18 +02:00
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2021-08-07 20:51:18 +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/>.
|
|
|
|
*/
|
2016-10-29 12:18:11 +02:00
|
|
|
|
|
|
|
#include "analyzerinfo.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
#include "errorlogger.h"
|
2023-11-02 17:42:41 +01:00
|
|
|
#include "filesettings.h"
|
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
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <map>
|
2022-02-01 17:19:19 +01:00
|
|
|
#include <sstream> // IWYU pragma: keep
|
2016-10-29 12:18:11 +02:00
|
|
|
|
2023-11-26 14:04:35 +01:00
|
|
|
#include "xml.h"
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-11-02 17:42:41 +01:00
|
|
|
void AnalyzerInformation::writeFilesTxt(const std::string &buildDir, const std::list<std::string> &sourcefiles, const std::string &userDefines, const std::list<FileSettings> &fileSettings)
|
2016-12-08 22:46:44 +01:00
|
|
|
{
|
|
|
|
std::map<std::string, unsigned int> fileCount;
|
|
|
|
|
|
|
|
const std::string filesTxt(buildDir + "/files.txt");
|
2018-04-11 09:50:42 +02:00
|
|
|
std::ofstream fout(filesTxt);
|
2018-07-13 22:56:20 +02:00
|
|
|
for (const std::string &f : sourcefiles) {
|
|
|
|
const std::string afile = getFilename(f);
|
2019-04-03 06:43:56 +02:00
|
|
|
fout << afile << ".a" << (++fileCount[afile]) << "::" << Path::simplifyPath(Path::fromNativeSeparators(f)) << '\n';
|
2020-12-19 20:45:51 +01:00
|
|
|
if (!userDefines.empty())
|
|
|
|
fout << afile << ".a" << (++fileCount[afile]) << ":" << userDefines << ":" << Path::simplifyPath(Path::fromNativeSeparators(f)) << '\n';
|
2016-12-08 22:46:44 +01:00
|
|
|
}
|
|
|
|
|
2023-11-02 17:42:41 +01:00
|
|
|
for (const FileSettings &fs : fileSettings) {
|
2018-07-13 22:56:20 +02:00
|
|
|
const std::string afile = getFilename(fs.filename);
|
2019-04-03 06:43:56 +02:00
|
|
|
fout << afile << ".a" << (++fileCount[afile]) << ":" << fs.cfg << ":" << Path::simplifyPath(Path::fromNativeSeparators(fs.filename)) << std::endl;
|
2016-12-08 22:46:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-29 12:18:11 +02:00
|
|
|
void AnalyzerInformation::close()
|
|
|
|
{
|
2018-06-17 08:11:48 +02:00
|
|
|
mAnalyzerInfoFile.clear();
|
2018-06-17 08:09:59 +02:00
|
|
|
if (mOutputStream.is_open()) {
|
|
|
|
mOutputStream << "</analyzerinfo>\n";
|
|
|
|
mOutputStream.close();
|
2016-10-29 12:18:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 22:05:41 +01:00
|
|
|
static bool skipAnalysis(const std::string &analyzerInfoFile, std::size_t hash, std::list<ErrorMessage> &errors)
|
2016-10-29 12:18:11 +02:00
|
|
|
{
|
|
|
|
tinyxml2::XMLDocument doc;
|
2018-04-04 21:51:31 +02:00
|
|
|
const tinyxml2::XMLError error = doc.LoadFile(analyzerInfoFile.c_str());
|
2016-10-29 12:18:11 +02:00
|
|
|
if (error != tinyxml2::XML_SUCCESS)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const tinyxml2::XMLElement * const rootNode = doc.FirstChildElement();
|
|
|
|
if (rootNode == nullptr)
|
|
|
|
return false;
|
|
|
|
|
2022-07-07 12:16:01 +02:00
|
|
|
const char *attr = rootNode->Attribute("hash");
|
|
|
|
if (!attr || attr != std::to_string(hash))
|
2016-10-29 12:18:11 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for (const tinyxml2::XMLElement *e = rootNode->FirstChildElement(); e; e = e->NextSiblingElement()) {
|
|
|
|
if (std::strcmp(e->Name(), "error") == 0)
|
2023-03-02 22:05:41 +01:00
|
|
|
errors.emplace_back(e);
|
2016-10-29 12:18:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-12 22:58:52 +02:00
|
|
|
std::string AnalyzerInformation::getAnalyzerInfoFileFromFilesTxt(std::istream& filesTxt, const std::string &sourcefile, const std::string &cfg)
|
|
|
|
{
|
|
|
|
std::string line;
|
|
|
|
const std::string end(':' + cfg + ':' + Path::simplifyPath(sourcefile));
|
|
|
|
while (std::getline(filesTxt,line)) {
|
|
|
|
if (line.size() <= end.size() + 2U)
|
|
|
|
continue;
|
|
|
|
if (!endsWith(line, end.c_str(), end.size()))
|
|
|
|
continue;
|
|
|
|
return line.substr(0,line.find(':'));
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-07-12 22:58:52 +02:00
|
|
|
std::ifstream fin(Path::join(buildDir, "files.txt"));
|
2016-11-14 20:50:08 +01:00
|
|
|
if (fin.is_open()) {
|
2022-07-12 22:58:52 +02:00
|
|
|
const std::string& ret = getAnalyzerInfoFileFromFilesTxt(fin, sourcefile, cfg);
|
|
|
|
if (!ret.empty())
|
|
|
|
return Path::join(buildDir, ret);
|
2016-11-14 20:50:08 +01:00
|
|
|
}
|
|
|
|
|
2016-11-30 12:01:22 +01:00
|
|
|
const std::string::size_type pos = sourcefile.rfind('/');
|
2022-07-12 22:58:52 +02:00
|
|
|
std::string filename;
|
2022-09-21 17:38:23 +02:00
|
|
|
if (pos == std::string::npos)
|
2022-07-12 22:58:52 +02:00
|
|
|
filename = sourcefile;
|
2016-11-05 21:26:56 +01:00
|
|
|
else
|
2022-07-12 22:58:52 +02:00
|
|
|
filename = sourcefile.substr(pos + 1);
|
|
|
|
return Path::join(buildDir, filename) + ".analyzerinfo";
|
2016-11-05 21:26:56 +01:00
|
|
|
}
|
|
|
|
|
2023-03-02 22:05:41 +01:00
|
|
|
bool AnalyzerInformation::analyzeFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t hash, std::list<ErrorMessage> &errors)
|
2016-10-29 12:18:11 +02:00
|
|
|
{
|
|
|
|
if (buildDir.empty() || sourcefile.empty())
|
|
|
|
return true;
|
|
|
|
close();
|
|
|
|
|
2018-06-17 08:11:48 +02:00
|
|
|
mAnalyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(buildDir,sourcefile,cfg);
|
2016-10-29 12:18:11 +02:00
|
|
|
|
2022-07-07 12:16:01 +02:00
|
|
|
if (skipAnalysis(mAnalyzerInfoFile, hash, errors))
|
2016-10-29 12:18:11 +02:00
|
|
|
return false;
|
|
|
|
|
2018-06-17 08:11:48 +02:00
|
|
|
mOutputStream.open(mAnalyzerInfoFile);
|
2018-06-17 08:09:59 +02:00
|
|
|
if (mOutputStream.is_open()) {
|
|
|
|
mOutputStream << "<?xml version=\"1.0\"?>\n";
|
2022-07-07 12:16:01 +02:00
|
|
|
mOutputStream << "<analyzerinfo hash=\"" << hash << "\">\n";
|
2016-10-29 12:18:11 +02:00
|
|
|
} else {
|
2018-06-17 08:11:48 +02:00
|
|
|
mAnalyzerInfoFile.clear();
|
2016-10-29 12:18:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-11 21:27:16 +01:00
|
|
|
void AnalyzerInformation::reportErr(const ErrorMessage &msg)
|
2016-10-29 12:18:11 +02:00
|
|
|
{
|
2018-06-17 08:09:59 +02:00
|
|
|
if (mOutputStream.is_open())
|
|
|
|
mOutputStream << msg.toXML() << '\n';
|
2016-10-29 12:18:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnalyzerInformation::setFileInfo(const std::string &check, const std::string &fileInfo)
|
|
|
|
{
|
2018-06-17 08:09:59 +02:00
|
|
|
if (mOutputStream.is_open() && !fileInfo.empty())
|
|
|
|
mOutputStream << " <FileInfo check=\"" << check << "\">\n" << fileInfo << " </FileInfo>\n";
|
2016-10-29 12:18:11 +02:00
|
|
|
}
|