forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzerinfo.cpp
163 lines (138 loc) · 5.4 KB
/
analyzerinfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2024 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 "analyzerinfo.h"
#include "errorlogger.h"
#include "filesettings.h"
#include "path.h"
#include "utils.h"
#include <cstring>
#include <map>
#include "xml.h"
AnalyzerInformation::~AnalyzerInformation()
{
close();
}
static std::string getFilename(const std::string &fullpath)
{
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::string &userDefines, const std::list<FileSettings> &fileSettings)
{
std::map<std::string, unsigned int> fileCount;
const std::string filesTxt(buildDir + "/files.txt");
std::ofstream fout(filesTxt);
for (const std::string &f : sourcefiles) {
const std::string afile = getFilename(f);
fout << afile << ".a" << (++fileCount[afile]) << "::" << Path::simplifyPath(f) << '\n';
if (!userDefines.empty())
fout << afile << ".a" << (++fileCount[afile]) << ":" << userDefines << ":" << Path::simplifyPath(f) << '\n';
}
for (const FileSettings &fs : fileSettings) {
const std::string afile = getFilename(fs.filename());
fout << afile << ".a" << (++fileCount[afile]) << ":" << fs.cfg << ":" << Path::simplifyPath(fs.filename()) << std::endl;
}
}
void AnalyzerInformation::close()
{
mAnalyzerInfoFile.clear();
if (mOutputStream.is_open()) {
mOutputStream << "</analyzerinfo>\n";
mOutputStream.close();
}
}
static bool skipAnalysis(const std::string &analyzerInfoFile, std::size_t hash, std::list<ErrorMessage> &errors)
{
tinyxml2::XMLDocument doc;
const 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("hash");
if (!attr || attr != std::to_string(hash))
return false;
for (const tinyxml2::XMLElement *e = rootNode->FirstChildElement(); e; e = e->NextSiblingElement()) {
if (std::strcmp(e->Name(), "error") == 0)
errors.emplace_back(e);
}
return true;
}
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 "";
}
std::string AnalyzerInformation::getAnalyzerInfoFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg)
{
std::ifstream fin(Path::join(buildDir, "files.txt"));
if (fin.is_open()) {
const std::string& ret = getAnalyzerInfoFileFromFilesTxt(fin, sourcefile, cfg);
if (!ret.empty())
return Path::join(buildDir, ret);
}
const std::string::size_type pos = sourcefile.rfind('/');
std::string filename;
if (pos == std::string::npos)
filename = sourcefile;
else
filename = sourcefile.substr(pos + 1);
return Path::join(buildDir, filename) + ".analyzerinfo";
}
bool AnalyzerInformation::analyzeFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t hash, std::list<ErrorMessage> &errors)
{
if (buildDir.empty() || sourcefile.empty())
return true;
close();
mAnalyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(buildDir,sourcefile,cfg);
if (skipAnalysis(mAnalyzerInfoFile, hash, errors))
return false;
mOutputStream.open(mAnalyzerInfoFile);
if (mOutputStream.is_open()) {
mOutputStream << "<?xml version=\"1.0\"?>\n";
mOutputStream << "<analyzerinfo hash=\"" << hash << "\">\n";
} else {
mAnalyzerInfoFile.clear();
}
return true;
}
void AnalyzerInformation::reportErr(const ErrorMessage &msg)
{
if (mOutputStream.is_open())
mOutputStream << msg.toXML() << '\n';
}
void AnalyzerInformation::setFileInfo(const std::string &check, const std::string &fileInfo)
{
if (mOutputStream.is_open() && !fileInfo.empty())
mOutputStream << " <FileInfo check=\"" << check << "\">\n" << fileInfo << " </FileInfo>\n";
}