2016-08-13 10:50:03 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2022-02-05 11:45:17 +01:00
|
|
|
* Copyright (C) 2007-2022 Cppcheck team.
|
2016-08-13 10:50:03 +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 "importproject.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2016-08-13 10:50:03 +02:00
|
|
|
#include "path.h"
|
|
|
|
#include "settings.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "standards.h"
|
2020-04-13 13:44:48 +02:00
|
|
|
#include "suppressions.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "token.h"
|
|
|
|
#include "tokenize.h"
|
2017-04-01 18:14:18 +02:00
|
|
|
#include "utils.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <cstring>
|
2022-02-01 17:19:19 +01:00
|
|
|
#include <fstream> // IWYU pragma: keep
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <iterator>
|
2022-09-16 07:15:49 +02:00
|
|
|
#include <sstream> // IWYU pragma: keep
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <utility>
|
2017-09-24 22:36:20 +02:00
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <tinyxml2.h>
|
|
|
|
|
2022-04-15 16:17:36 +02:00
|
|
|
#include <simplecpp.h>
|
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
#define PICOJSON_USE_INT64
|
|
|
|
#include <picojson.h>
|
2019-08-09 21:15:02 +02:00
|
|
|
|
2020-04-19 18:19:28 +02:00
|
|
|
ImportProject::ImportProject()
|
|
|
|
{
|
|
|
|
projectType = Type::UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2016-12-06 20:02:43 +01:00
|
|
|
void ImportProject::ignorePaths(const std::vector<std::string> &ipaths)
|
2016-08-13 10:50:03 +02:00
|
|
|
{
|
|
|
|
for (std::list<FileSettings>::iterator it = fileSettings.begin(); it != fileSettings.end();) {
|
|
|
|
bool ignore = false;
|
2019-04-11 18:46:57 +02:00
|
|
|
for (std::string i : ipaths) {
|
2019-04-07 11:58:42 +02:00
|
|
|
if (it->filename.size() > i.size() && it->filename.compare(0,i.size(),i)==0) {
|
2016-08-13 10:50:03 +02:00
|
|
|
ignore = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-24 07:06:09 +01:00
|
|
|
if (isValidGlobPattern(i) && matchglob(i, it->filename)) {
|
|
|
|
ignore = true;
|
|
|
|
break;
|
|
|
|
}
|
2019-09-12 09:32:24 +02:00
|
|
|
if (!Path::isAbsolute(i)) {
|
|
|
|
i = mPath + i;
|
|
|
|
if (it->filename.size() > i.size() && it->filename.compare(0,i.size(),i)==0) {
|
|
|
|
ignore = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
if (ignore)
|
2022-10-02 07:13:31 +02:00
|
|
|
it = fileSettings.erase(it);
|
2016-08-13 10:50:03 +02:00
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 15:57:38 +02:00
|
|
|
void ImportProject::ignoreOtherConfigs(const std::string &cfg)
|
|
|
|
{
|
|
|
|
for (std::list<FileSettings>::iterator it = fileSettings.begin(); it != fileSettings.end();) {
|
|
|
|
if (it->cfg != cfg)
|
2022-10-02 07:13:31 +02:00
|
|
|
it = fileSettings.erase(it);
|
2016-08-21 15:57:38 +02:00
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 10:50:03 +02:00
|
|
|
void ImportProject::FileSettings::setDefines(std::string defs)
|
|
|
|
{
|
|
|
|
while (defs.find(";%(") != std::string::npos) {
|
2018-04-04 21:51:31 +02:00
|
|
|
const std::string::size_type pos1 = defs.find(";%(");
|
|
|
|
const std::string::size_type pos2 = defs.find(';', pos1+1);
|
2016-08-13 10:50:03 +02:00
|
|
|
defs.erase(pos1, pos2 == std::string::npos ? pos2 : (pos2-pos1));
|
|
|
|
}
|
|
|
|
while (defs.find(";;") != std::string::npos)
|
|
|
|
defs.erase(defs.find(";;"),1);
|
2018-05-20 18:19:20 +02:00
|
|
|
while (!defs.empty() && defs[0] == ';')
|
|
|
|
defs.erase(0, 1);
|
|
|
|
while (!defs.empty() && endsWith(defs,';'))
|
2016-12-05 22:55:57 +01:00
|
|
|
defs.erase(defs.size() - 1U); // TODO: Use std::string::pop_back() as soon as travis supports it
|
2016-08-13 10:50:03 +02:00
|
|
|
bool eq = false;
|
|
|
|
for (std::size_t pos = 0; pos < defs.size(); ++pos) {
|
|
|
|
if (defs[pos] == '(' || defs[pos] == '=')
|
|
|
|
eq = true;
|
|
|
|
else if (defs[pos] == ';') {
|
|
|
|
if (!eq) {
|
|
|
|
defs.insert(pos,"=1");
|
|
|
|
pos += 3;
|
|
|
|
}
|
|
|
|
if (pos < defs.size())
|
|
|
|
eq = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!eq && !defs.empty())
|
|
|
|
defs += "=1";
|
|
|
|
defines.swap(defs);
|
|
|
|
}
|
|
|
|
|
2017-09-24 22:36:20 +02:00
|
|
|
static bool simplifyPathWithVariables(std::string &s, std::map<std::string, std::string, cppcheck::stricmp> &variables)
|
2016-11-20 22:44:50 +01:00
|
|
|
{
|
2017-09-24 22:36:20 +02:00
|
|
|
std::set<std::string, cppcheck::stricmp> expanded;
|
2016-11-20 22:44:50 +01:00
|
|
|
std::string::size_type start = 0;
|
|
|
|
while ((start = s.find("$(")) != std::string::npos) {
|
2018-04-04 21:51:31 +02:00
|
|
|
const std::string::size_type end = s.find(')',start);
|
2016-11-20 22:44:50 +01:00
|
|
|
if (end == std::string::npos)
|
|
|
|
break;
|
2016-12-06 20:02:43 +01:00
|
|
|
const std::string var = s.substr(start+2,end-start-2);
|
2016-11-20 22:44:50 +01:00
|
|
|
if (expanded.find(var) != expanded.end())
|
|
|
|
break;
|
|
|
|
expanded.insert(var);
|
2017-09-24 22:36:20 +02:00
|
|
|
std::map<std::string, std::string, cppcheck::stricmp>::const_iterator it1 = variables.find(var);
|
2017-06-27 12:41:25 +02:00
|
|
|
// variable was not found within defined variables
|
|
|
|
if (it1 == variables.end()) {
|
|
|
|
const char *envValue = std::getenv(var.c_str());
|
|
|
|
if (!envValue) {
|
2018-06-07 08:33:32 +02:00
|
|
|
//! \todo generate a debug/info message about undefined variable
|
2017-06-27 12:41:25 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
variables[var] = std::string(envValue);
|
|
|
|
it1 = variables.find(var);
|
|
|
|
}
|
2022-07-11 23:07:37 +02:00
|
|
|
s.replace(start, end - start + 1, it1->second);
|
2016-11-20 22:44:50 +01:00
|
|
|
}
|
|
|
|
if (s.find("$(") != std::string::npos)
|
|
|
|
return false;
|
|
|
|
s = Path::simplifyPath(Path::fromNativeSeparators(s));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-24 22:36:20 +02:00
|
|
|
void ImportProject::FileSettings::setIncludePaths(const std::string &basepath, const std::list<std::string> &in, std::map<std::string, std::string, cppcheck::stricmp> &variables)
|
2016-08-13 10:50:03 +02:00
|
|
|
{
|
2022-03-24 22:50:04 +01:00
|
|
|
std::set<std::string> found;
|
2022-05-15 12:42:29 +02:00
|
|
|
// NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
|
2022-03-24 22:50:04 +01:00
|
|
|
const std::list<std::string> copyIn(in);
|
|
|
|
includePaths.clear();
|
|
|
|
for (const std::string &ipath : copyIn) {
|
|
|
|
if (ipath.empty())
|
2016-08-13 10:50:03 +02:00
|
|
|
continue;
|
2022-03-24 22:50:04 +01:00
|
|
|
if (ipath.compare(0,2,"%(")==0)
|
|
|
|
continue;
|
|
|
|
std::string s(Path::fromNativeSeparators(ipath));
|
|
|
|
if (!found.insert(s).second)
|
2016-08-13 10:50:03 +02:00
|
|
|
continue;
|
|
|
|
if (s[0] == '/' || (s.size() > 1U && s.compare(1,2,":/") == 0)) {
|
2017-04-01 18:14:18 +02:00
|
|
|
if (!endsWith(s,'/'))
|
2016-08-13 10:50:03 +02:00
|
|
|
s += '/';
|
2022-09-08 09:21:35 +02:00
|
|
|
includePaths.push_back(std::move(s));
|
2016-08-13 10:50:03 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-11-11 16:22:14 +01:00
|
|
|
|
2017-04-01 18:14:18 +02:00
|
|
|
if (endsWith(s,'/')) // this is a temporary hack, simplifyPath can crash if path ends with '/'
|
2016-12-05 22:55:57 +01:00
|
|
|
s.erase(s.size() - 1U); // TODO: Use std::string::pop_back() as soon as travis supports it
|
2016-11-11 16:22:14 +01:00
|
|
|
|
2018-08-19 17:27:41 +02:00
|
|
|
if (s.find("$(") == std::string::npos) {
|
2016-11-11 16:22:14 +01:00
|
|
|
s = Path::simplifyPath(basepath + s);
|
2016-11-13 12:16:09 +01:00
|
|
|
} else {
|
2018-08-19 17:27:41 +02:00
|
|
|
if (!simplifyPathWithVariables(s, variables))
|
2016-11-13 12:16:09 +01:00
|
|
|
continue;
|
2016-11-11 16:22:14 +01:00
|
|
|
}
|
2016-08-13 10:50:03 +02:00
|
|
|
if (s.empty())
|
|
|
|
continue;
|
2022-03-24 22:50:04 +01:00
|
|
|
includePaths.push_back(s + '/');
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 20:40:15 +01:00
|
|
|
ImportProject::Type ImportProject::import(const std::string &filename, Settings *settings)
|
2016-08-13 10:50:03 +02:00
|
|
|
{
|
|
|
|
std::ifstream fin(filename);
|
|
|
|
if (!fin.is_open())
|
2019-01-31 20:40:15 +01:00
|
|
|
return ImportProject::Type::MISSING;
|
2019-04-11 18:46:57 +02:00
|
|
|
|
|
|
|
mPath = Path::getPathFromFilename(Path::fromNativeSeparators(filename));
|
|
|
|
if (!mPath.empty() && !endsWith(mPath,'/'))
|
|
|
|
mPath += '/';
|
|
|
|
|
2021-11-23 22:51:45 +01:00
|
|
|
const std::vector<std::string> fileFilters =
|
|
|
|
settings ? settings->fileFilters : std::vector<std::string>();
|
2020-05-24 21:39:18 +02:00
|
|
|
|
2021-09-28 20:34:21 +02:00
|
|
|
if (endsWith(filename, ".json")) {
|
2021-12-19 12:36:11 +01:00
|
|
|
if (importCompileCommands(fin)) {
|
|
|
|
setRelativePaths(filename);
|
|
|
|
return ImportProject::Type::COMPILE_DB;
|
|
|
|
}
|
2021-09-28 20:34:21 +02:00
|
|
|
} else if (endsWith(filename, ".sln")) {
|
2021-12-19 12:36:11 +01:00
|
|
|
if (importSln(fin, mPath, fileFilters)) {
|
|
|
|
setRelativePaths(filename);
|
|
|
|
return ImportProject::Type::VS_SLN;
|
|
|
|
}
|
2021-09-28 20:34:21 +02:00
|
|
|
} else if (endsWith(filename, ".vcxproj")) {
|
2017-09-24 22:36:20 +02:00
|
|
|
std::map<std::string, std::string, cppcheck::stricmp> variables;
|
2021-12-19 12:36:11 +01:00
|
|
|
if (importVcxproj(filename, variables, emptyString, fileFilters)) {
|
|
|
|
setRelativePaths(filename);
|
|
|
|
return ImportProject::Type::VS_VCXPROJ;
|
|
|
|
}
|
2021-09-28 20:34:21 +02:00
|
|
|
} else if (endsWith(filename, ".bpr")) {
|
2021-12-19 12:36:11 +01:00
|
|
|
if (importBcb6Prj(filename)) {
|
|
|
|
setRelativePaths(filename);
|
|
|
|
return ImportProject::Type::BORLAND;
|
|
|
|
}
|
2021-09-28 20:34:21 +02:00
|
|
|
} else if (settings && endsWith(filename, ".cppcheck")) {
|
2021-12-19 12:36:11 +01:00
|
|
|
if (importCppcheckGuiProject(fin, settings)) {
|
|
|
|
setRelativePaths(filename);
|
|
|
|
return ImportProject::Type::CPPCHECK_GUI;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ImportProject::Type::UNKNOWN;
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
2021-12-19 12:36:11 +01:00
|
|
|
return ImportProject::Type::FAILURE;
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
|
2020-12-25 11:51:24 +01:00
|
|
|
static std::string readUntil(const std::string &command, std::string::size_type *pos, const char until[])
|
2016-08-13 10:50:03 +02:00
|
|
|
{
|
2019-01-05 17:45:07 +01:00
|
|
|
std::string ret;
|
2020-12-25 14:45:48 +01:00
|
|
|
bool escapedString = false;
|
2019-01-05 17:45:07 +01:00
|
|
|
bool str = false;
|
2020-12-25 11:51:24 +01:00
|
|
|
bool escape = false;
|
|
|
|
for (; *pos < command.size() && (str || !std::strchr(until, command[*pos])); (*pos)++) {
|
2020-12-25 14:45:48 +01:00
|
|
|
if (escape)
|
2020-12-25 11:51:24 +01:00
|
|
|
escape = false;
|
2020-12-25 14:45:48 +01:00
|
|
|
else if (command[*pos] == '\\') {
|
|
|
|
if (str)
|
|
|
|
escape = true;
|
|
|
|
else if (command[*pos + 1] == '"') {
|
|
|
|
if (escapedString)
|
|
|
|
return ret + "\\\"";
|
|
|
|
escapedString = true;
|
|
|
|
ret += "\\\"";
|
|
|
|
(*pos)++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (command[*pos] == '\"')
|
|
|
|
str = !str;
|
|
|
|
ret += command[*pos];
|
2020-09-06 15:35:14 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-12-25 12:16:06 +01:00
|
|
|
static std::string unescape(const std::string &in)
|
|
|
|
{
|
2020-12-25 11:51:24 +01:00
|
|
|
std::string out;
|
|
|
|
bool escape = false;
|
2022-10-02 07:12:40 +02:00
|
|
|
for (const char c: in) {
|
2020-12-25 11:51:24 +01:00
|
|
|
if (escape) {
|
|
|
|
escape = false;
|
2020-12-25 14:45:48 +01:00
|
|
|
if (!std::strchr("\\\"\'",c))
|
|
|
|
out += "\\";
|
2020-12-25 11:51:24 +01:00
|
|
|
out += c;
|
|
|
|
} else if (c == '\\')
|
|
|
|
escape = true;
|
|
|
|
else
|
|
|
|
out += c;
|
2020-09-06 15:35:14 +02:00
|
|
|
}
|
2020-12-25 11:51:24 +01:00
|
|
|
return out;
|
2020-09-06 15:35:14 +02:00
|
|
|
}
|
|
|
|
|
2020-12-25 11:51:24 +01:00
|
|
|
void ImportProject::FileSettings::parseCommand(std::string command)
|
2020-09-06 15:35:14 +02:00
|
|
|
{
|
2019-01-05 17:45:07 +01:00
|
|
|
std::string defs;
|
2016-08-13 10:50:03 +02:00
|
|
|
|
2020-12-25 11:51:24 +01:00
|
|
|
// Parse command..
|
2019-01-05 17:45:07 +01:00
|
|
|
std::string::size_type pos = 0;
|
|
|
|
while (std::string::npos != (pos = command.find(' ',pos))) {
|
2020-12-25 11:51:24 +01:00
|
|
|
while (pos < command.size() && command[pos] == ' ')
|
|
|
|
pos++;
|
2019-01-05 17:45:07 +01:00
|
|
|
if (pos >= command.size())
|
|
|
|
break;
|
2020-12-25 11:51:24 +01:00
|
|
|
if (command[pos] != '/' && command[pos] != '-')
|
|
|
|
continue;
|
|
|
|
pos++;
|
2020-09-06 15:35:14 +02:00
|
|
|
if (pos >= command.size())
|
|
|
|
break;
|
2020-12-25 11:51:24 +01:00
|
|
|
const char F = command[pos++];
|
|
|
|
if (std::strchr("DUI", F)) {
|
|
|
|
while (pos < command.size() && command[pos] == ' ')
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
const std::string fval = readUntil(command, &pos, " =");
|
|
|
|
if (F=='D') {
|
2020-12-25 14:45:48 +01:00
|
|
|
std::string defval = readUntil(command, &pos, " ");
|
2020-12-25 11:51:24 +01:00
|
|
|
defs += fval;
|
2020-12-25 14:45:48 +01:00
|
|
|
if (defval.size() >= 3 && defval.compare(0,2,"=\"")==0 && defval.back()=='\"')
|
|
|
|
defval = "=" + unescape(defval.substr(2, defval.size() - 3));
|
2021-09-28 20:34:21 +02:00
|
|
|
else if (defval.size() >= 5 && defval.compare(0, 3, "=\\\"") == 0 && endsWith(defval, "\\\""))
|
2020-12-25 14:45:48 +01:00
|
|
|
defval = "=\"" + unescape(defval.substr(3, defval.size() - 5)) + "\"";
|
2020-12-25 11:51:24 +01:00
|
|
|
if (!defval.empty())
|
|
|
|
defs += defval;
|
|
|
|
defs += ';';
|
|
|
|
} else if (F=='U')
|
|
|
|
undefs.insert(fval);
|
|
|
|
else if (F=='I') {
|
|
|
|
std::string i = fval;
|
|
|
|
if (i.size() > 1 && i[0] == '\"' && i.back() == '\"')
|
|
|
|
i = unescape(i.substr(1, i.size() - 2));
|
2022-12-30 15:13:47 +01:00
|
|
|
if (std::find(includePaths.cbegin(), includePaths.cend(), i) == includePaths.cend())
|
2022-09-08 09:21:35 +02:00
|
|
|
includePaths.push_back(std::move(i));
|
2020-12-25 11:51:24 +01:00
|
|
|
} else if (F=='s' && fval.compare(0,2,"td") == 0) {
|
|
|
|
++pos;
|
|
|
|
const std::string stdval = readUntil(command, &pos, " ");
|
|
|
|
standard = stdval;
|
2022-04-15 16:17:36 +02:00
|
|
|
// TODO: use simplecpp::DUI::std instead of specifying it manually
|
2020-12-25 11:51:24 +01:00
|
|
|
if (standard.compare(0, 3, "c++") || standard.compare(0, 5, "gnu++")) {
|
2022-04-15 16:17:36 +02:00
|
|
|
const std::string stddef = simplecpp::getCppStdString(standard);
|
2020-12-25 11:51:24 +01:00
|
|
|
if (stddef.empty()) {
|
|
|
|
// TODO: log error
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
defs += "__cplusplus=";
|
|
|
|
defs += stddef;
|
|
|
|
defs += ";";
|
|
|
|
} else if (standard.compare(0, 1, "c") || standard.compare(0, 3, "gnu")) {
|
2022-04-15 16:17:36 +02:00
|
|
|
const std::string stddef = simplecpp::getCStdString(standard);
|
2020-12-25 11:51:24 +01:00
|
|
|
if (stddef.empty()) {
|
|
|
|
// TODO: log error
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
defs += "__STDC_VERSION__=";
|
|
|
|
defs += stddef;
|
|
|
|
defs += ";";
|
|
|
|
}
|
|
|
|
} else if (F == 'i' && fval == "system") {
|
|
|
|
++pos;
|
2022-09-08 09:21:35 +02:00
|
|
|
std::string isystem = readUntil(command, &pos, " ");
|
|
|
|
systemIncludePaths.push_back(std::move(isystem));
|
2020-12-25 11:51:24 +01:00
|
|
|
} else if (F=='m') {
|
|
|
|
if (fval == "unicode") {
|
|
|
|
defs += "UNICODE";
|
|
|
|
defs += ";";
|
|
|
|
}
|
|
|
|
} else if (F=='f') {
|
|
|
|
if (fval == "pic") {
|
|
|
|
defs += "__pic__";
|
|
|
|
defs += ";";
|
|
|
|
} else if (fval == "PIC") {
|
|
|
|
defs += "__PIC__";
|
|
|
|
defs += ";";
|
|
|
|
} else if (fval == "pie") {
|
|
|
|
defs += "__pie__";
|
|
|
|
defs += ";";
|
|
|
|
} else if (fval == "PIE") {
|
|
|
|
defs += "__PIE__";
|
|
|
|
defs += ";";
|
|
|
|
}
|
|
|
|
}
|
2019-01-05 17:45:07 +01:00
|
|
|
}
|
|
|
|
setDefines(defs);
|
|
|
|
}
|
2018-04-05 15:40:59 +02:00
|
|
|
|
2021-12-19 12:36:11 +01:00
|
|
|
bool ImportProject::importCompileCommands(std::istream &istr)
|
2019-01-05 17:45:07 +01:00
|
|
|
{
|
2019-01-21 06:36:23 +01:00
|
|
|
picojson::value compileCommands;
|
|
|
|
istr >> compileCommands;
|
2021-12-19 12:36:11 +01:00
|
|
|
if (!compileCommands.is<picojson::array>()) {
|
|
|
|
printError("compilation database is not a JSON array");
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-05 17:45:07 +01:00
|
|
|
|
2019-01-21 06:36:23 +01:00
|
|
|
for (const picojson::value &fileInfo : compileCommands.get<picojson::array>()) {
|
2019-01-05 17:45:07 +01:00
|
|
|
picojson::object obj = fileInfo.get<picojson::object>();
|
2019-01-21 06:36:23 +01:00
|
|
|
std::string dirpath = Path::fromNativeSeparators(obj["directory"].get<std::string>());
|
2019-01-11 16:36:55 +01:00
|
|
|
|
|
|
|
/* CMAKE produces the directory without trailing / so add it if not
|
|
|
|
* there - it is needed by setIncludePaths() */
|
2019-01-11 16:37:12 +01:00
|
|
|
if (!endsWith(dirpath, '/'))
|
2019-01-11 16:36:55 +01:00
|
|
|
dirpath += '/';
|
|
|
|
|
2019-01-21 06:36:23 +01:00
|
|
|
const std::string directory = dirpath;
|
2019-04-15 20:03:42 +02:00
|
|
|
|
2022-02-16 08:34:33 +01:00
|
|
|
std::string command;
|
|
|
|
if (obj.count("arguments")) {
|
2021-08-07 20:51:18 +02:00
|
|
|
if (obj["arguments"].is<picojson::array>()) {
|
|
|
|
for (const picojson::value& arg : obj["arguments"].get<picojson::array>()) {
|
|
|
|
if (arg.is<std::string>()) {
|
2022-03-23 21:13:51 +01:00
|
|
|
std::string str = arg.get<std::string>();
|
|
|
|
if (str.find(" ") != std::string::npos)
|
|
|
|
str = "\"" + str + "\"";
|
|
|
|
command += str + " ";
|
2019-04-15 20:03:42 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-16 19:07:44 +02:00
|
|
|
} else {
|
2021-12-19 12:36:11 +01:00
|
|
|
printError("'arguments' field in compilation database entry is not a JSON array");
|
|
|
|
return false;
|
2019-04-15 20:03:42 +02:00
|
|
|
}
|
2022-02-16 08:34:33 +01:00
|
|
|
} else if (obj.count("command")) {
|
2021-08-07 20:51:18 +02:00
|
|
|
if (obj["command"].is<std::string>()) {
|
2022-02-16 08:34:33 +01:00
|
|
|
command = obj["command"].get<std::string>();
|
|
|
|
} else {
|
|
|
|
printError("'command' field in compilation database entry is not a string");
|
|
|
|
return false;
|
2019-04-15 20:03:42 +02:00
|
|
|
}
|
2019-04-16 19:07:44 +02:00
|
|
|
} else {
|
2021-12-19 12:36:11 +01:00
|
|
|
printError("no 'arguments' or 'command' field found in compilation database entry");
|
|
|
|
return false;
|
2019-04-15 20:03:42 +02:00
|
|
|
}
|
|
|
|
|
2022-02-16 08:34:33 +01:00
|
|
|
if (!obj.count("file") || !obj["file"].is<std::string>()) {
|
|
|
|
printError("skip compilation database entry because it does not have a proper 'file' field");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-05 17:45:07 +01:00
|
|
|
const std::string file = Path::fromNativeSeparators(obj["file"].get<std::string>());
|
|
|
|
|
2019-01-21 06:36:23 +01:00
|
|
|
// Accept file?
|
|
|
|
if (!Path::acceptFile(file))
|
|
|
|
continue;
|
|
|
|
|
2019-01-05 17:45:07 +01:00
|
|
|
struct FileSettings fs;
|
2021-01-31 10:14:22 +01:00
|
|
|
if (Path::isAbsolute(file))
|
|
|
|
fs.filename = Path::simplifyPath(file);
|
|
|
|
#ifdef _WIN32
|
|
|
|
else if (file[0] == '/' && directory.size() > 2 && std::isalpha(directory[0]) && directory[1] == ':')
|
|
|
|
// directory: C:\foo\bar
|
|
|
|
// file: /xy/z.c
|
|
|
|
// => c:/xy/z.c
|
|
|
|
fs.filename = Path::simplifyPath(directory.substr(0,2) + file);
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
fs.filename = Path::simplifyPath(directory + file);
|
2021-12-19 12:36:11 +01:00
|
|
|
if (!sourceFileExists(fs.filename)) {
|
|
|
|
printError("'" + fs.filename + "' from compilation database does not exist");
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-27 10:23:04 +01:00
|
|
|
fs.parseCommand(command); // read settings; -D, -I, -U, -std, -m*, -f*
|
2019-01-05 17:45:07 +01:00
|
|
|
std::map<std::string, std::string, cppcheck::stricmp> variables;
|
|
|
|
fs.setIncludePaths(directory, fs.includePaths, variables);
|
2022-09-08 09:21:35 +02:00
|
|
|
fileSettings.push_back(std::move(fs));
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
2021-12-19 12:36:11 +01:00
|
|
|
|
|
|
|
return true;
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
|
2021-12-19 12:36:11 +01:00
|
|
|
bool ImportProject::importSln(std::istream &istr, const std::string &path, const std::vector<std::string> &fileFilters)
|
2016-08-13 10:50:03 +02:00
|
|
|
{
|
2021-12-19 12:36:11 +01:00
|
|
|
std::string line;
|
|
|
|
|
|
|
|
if (!std::getline(istr,line)) {
|
|
|
|
printError("Visual Studio solution file is empty");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-08 08:52:27 +01:00
|
|
|
if (line.find("Microsoft Visual Studio Solution File") != 0) {
|
|
|
|
// Skip BOM
|
|
|
|
if (!std::getline(istr, line) || line.find("Microsoft Visual Studio Solution File") != 0) {
|
|
|
|
printError("Visual Studio solution file header not found");
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-19 12:36:11 +01:00
|
|
|
}
|
|
|
|
|
2017-09-24 22:36:20 +02:00
|
|
|
std::map<std::string,std::string,cppcheck::stricmp> variables;
|
2016-11-11 16:22:14 +01:00
|
|
|
variables["SolutionDir"] = path;
|
2016-11-13 12:16:09 +01:00
|
|
|
|
2021-12-19 12:36:11 +01:00
|
|
|
bool found = false;
|
|
|
|
|
2016-08-13 10:50:03 +02:00
|
|
|
while (std::getline(istr,line)) {
|
|
|
|
if (line.compare(0,8,"Project(")!=0)
|
|
|
|
continue;
|
|
|
|
const std::string::size_type pos = line.find(".vcxproj");
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
continue;
|
2016-11-30 12:01:22 +01:00
|
|
|
const std::string::size_type pos1 = line.rfind('\"',pos);
|
2017-08-01 11:45:51 +02:00
|
|
|
if (pos1 == std::string::npos)
|
2016-08-13 10:50:03 +02:00
|
|
|
continue;
|
2017-07-10 11:12:48 +02:00
|
|
|
std::string vcxproj(line.substr(pos1+1, pos-pos1+7));
|
|
|
|
if (!Path::isAbsolute(vcxproj))
|
|
|
|
vcxproj = path + vcxproj;
|
2021-12-19 12:36:11 +01:00
|
|
|
if (!importVcxproj(Path::fromNativeSeparators(vcxproj), variables, emptyString, fileFilters)) {
|
|
|
|
printError("failed to load '" + vcxproj + "' from Visual Studio solution");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
found = true;
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
2021-12-19 12:36:11 +01:00
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
printError("no projects found in Visual Studio solution file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct ProjectConfiguration {
|
2017-08-02 07:59:25 +02:00
|
|
|
explicit ProjectConfiguration(const tinyxml2::XMLElement *cfg) : platform(Unknown) {
|
2016-08-13 10:50:03 +02:00
|
|
|
const char *a = cfg->Attribute("Include");
|
|
|
|
if (a)
|
|
|
|
name = a;
|
|
|
|
for (const tinyxml2::XMLElement *e = cfg->FirstChildElement(); e; e = e->NextSiblingElement()) {
|
2017-08-02 08:00:52 +02:00
|
|
|
if (!e->GetText())
|
|
|
|
continue;
|
|
|
|
if (std::strcmp(e->Name(),"Configuration")==0)
|
|
|
|
configuration = e->GetText();
|
|
|
|
else if (std::strcmp(e->Name(),"Platform")==0) {
|
|
|
|
platformStr = e->GetText();
|
|
|
|
if (platformStr == "Win32")
|
|
|
|
platform = Win32;
|
|
|
|
else if (platformStr == "x64")
|
|
|
|
platform = x64;
|
|
|
|
else
|
|
|
|
platform = Unknown;
|
2016-08-24 12:17:54 +02:00
|
|
|
}
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
std::string name;
|
|
|
|
std::string configuration;
|
2017-06-01 13:13:23 +02:00
|
|
|
enum { Win32, x64, Unknown } platform;
|
|
|
|
std::string platformStr;
|
2016-08-13 10:50:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ItemDefinitionGroup {
|
2022-07-28 22:51:45 +02:00
|
|
|
explicit ItemDefinitionGroup(const tinyxml2::XMLElement *idg, std::string includePaths) : additionalIncludePaths(std::move(includePaths)) {
|
2016-08-13 10:50:03 +02:00
|
|
|
const char *condAttr = idg->Attribute("Condition");
|
|
|
|
if (condAttr)
|
|
|
|
condition = condAttr;
|
|
|
|
for (const tinyxml2::XMLElement *e1 = idg->FirstChildElement(); e1; e1 = e1->NextSiblingElement()) {
|
2023-01-18 20:52:33 +01:00
|
|
|
if (std::strcmp(e1->Name(), "ClCompile") == 0) {
|
|
|
|
enhancedInstructionSet = "StreamingSIMDExtensions2";
|
|
|
|
for (const tinyxml2::XMLElement *e = e1->FirstChildElement(); e; e = e->NextSiblingElement()) {
|
|
|
|
if (e->GetText()) {
|
|
|
|
if (std::strcmp(e->Name(), "PreprocessorDefinitions") == 0)
|
|
|
|
preprocessorDefinitions = e->GetText();
|
|
|
|
else if (std::strcmp(e->Name(), "AdditionalIncludeDirectories") == 0) {
|
|
|
|
if (!additionalIncludePaths.empty())
|
|
|
|
additionalIncludePaths += ';';
|
|
|
|
additionalIncludePaths += e->GetText();
|
|
|
|
} else if (std::strcmp(e->Name(), "LanguageStandard") == 0) {
|
|
|
|
if (std::strcmp(e->GetText(), "stdcpp14") == 0)
|
|
|
|
cppstd = Standards::CPP14;
|
|
|
|
else if (std::strcmp(e->GetText(), "stdcpp17") == 0)
|
|
|
|
cppstd = Standards::CPP17;
|
|
|
|
else if (std::strcmp(e->GetText(), "stdcpp20") == 0)
|
|
|
|
cppstd = Standards::CPP20;
|
|
|
|
else if (std::strcmp(e->GetText(), "stdcpplatest") == 0)
|
|
|
|
cppstd = Standards::CPPLatest;
|
|
|
|
} else if (std::strcmp(e->Name(), "EnableEnhancedInstructionSet") == 0) {
|
|
|
|
enhancedInstructionSet = e->GetText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (std::strcmp(e1->Name(), "Link") == 0) {
|
|
|
|
for (const tinyxml2::XMLElement *e = e1->FirstChildElement(); e; e = e->NextSiblingElement()) {
|
|
|
|
if (!e->GetText())
|
|
|
|
continue;
|
|
|
|
if (std::strcmp(e->Name(), "EntryPointSymbol") == 0) {
|
|
|
|
entryPointSymbol = e->GetText();
|
2016-11-13 12:16:09 +01:00
|
|
|
}
|
2016-08-24 12:17:54 +02:00
|
|
|
}
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-20 13:00:47 +02:00
|
|
|
|
|
|
|
static void replaceAll(std::string &c, const std::string &from, const std::string &to) {
|
|
|
|
std::string::size_type pos;
|
|
|
|
while ((pos = c.find(from)) != std::string::npos) {
|
|
|
|
c.erase(pos,from.size());
|
|
|
|
c.insert(pos,to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 10:50:03 +02:00
|
|
|
bool conditionIsTrue(const ProjectConfiguration &p) const {
|
2016-12-07 17:03:46 +01:00
|
|
|
if (condition.empty())
|
|
|
|
return true;
|
2016-11-20 22:44:50 +01:00
|
|
|
std::string c = '(' + condition + ");";
|
2016-08-20 13:00:47 +02:00
|
|
|
replaceAll(c, "$(Configuration)", p.configuration);
|
2017-06-01 13:13:23 +02:00
|
|
|
replaceAll(c, "$(Platform)", p.platformStr);
|
2016-08-20 13:00:47 +02:00
|
|
|
|
2016-08-13 10:50:03 +02:00
|
|
|
// TODO : Better evaluation
|
|
|
|
Settings s;
|
|
|
|
std::istringstream istr(c);
|
2016-08-20 13:00:47 +02:00
|
|
|
Tokenizer tokenizer(&s, nullptr);
|
|
|
|
tokenizer.tokenize(istr,"vcxproj");
|
|
|
|
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) {
|
|
|
|
if (tok->str() == "(" && tok->astOperand1() && tok->astOperand2()) {
|
|
|
|
if (tok->astOperand1()->expressionString() == "Configuration.Contains")
|
|
|
|
return ('\'' + p.configuration + '\'') == tok->astOperand2()->str();
|
|
|
|
}
|
2016-08-13 10:50:03 +02:00
|
|
|
if (tok->str() == "==" && tok->astOperand1() && tok->astOperand2() && tok->astOperand1()->str() == tok->astOperand2()->str())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string condition;
|
2022-01-20 21:09:39 +01:00
|
|
|
std::string enhancedInstructionSet;
|
2016-08-13 10:50:03 +02:00
|
|
|
std::string preprocessorDefinitions;
|
|
|
|
std::string additionalIncludePaths;
|
2023-01-18 20:52:33 +01:00
|
|
|
std::string entryPointSymbol; // TODO: use this
|
2020-05-25 21:43:24 +02:00
|
|
|
Standards::cppstd_t cppstd = Standards::CPPLatest;
|
2016-08-13 10:50:03 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::list<std::string> toStringList(const std::string &s)
|
|
|
|
{
|
|
|
|
std::list<std::string> ret;
|
|
|
|
std::string::size_type pos1 = 0;
|
|
|
|
std::string::size_type pos2;
|
2016-11-30 12:01:22 +01:00
|
|
|
while ((pos2 = s.find(';',pos1)) != std::string::npos) {
|
2016-08-13 10:50:03 +02:00
|
|
|
ret.push_back(s.substr(pos1, pos2-pos1));
|
|
|
|
pos1 = pos2 + 1;
|
|
|
|
if (pos1 >= s.size())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (pos1 < s.size())
|
|
|
|
ret.push_back(s.substr(pos1));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-24 22:36:20 +02:00
|
|
|
static void importPropertyGroup(const tinyxml2::XMLElement *node, std::map<std::string,std::string,cppcheck::stricmp> *variables, std::string *includePath, bool *useOfMfc)
|
2016-12-04 18:50:59 +01:00
|
|
|
{
|
2016-12-04 19:15:04 +01:00
|
|
|
if (useOfMfc) {
|
|
|
|
for (const tinyxml2::XMLElement *e = node->FirstChildElement(); e; e = e->NextSiblingElement()) {
|
2017-08-24 17:26:53 +02:00
|
|
|
if (std::strcmp(e->Name(), "UseOfMfc") == 0) {
|
2016-12-04 19:15:04 +01:00
|
|
|
*useOfMfc = true;
|
2017-08-24 17:26:53 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-12-04 19:15:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-24 17:28:52 +02:00
|
|
|
const char* labelAttribute = node->Attribute("Label");
|
|
|
|
if (labelAttribute && std::strcmp(labelAttribute, "UserMacros") == 0) {
|
2016-12-04 18:50:59 +01:00
|
|
|
for (const tinyxml2::XMLElement *propertyGroup = node->FirstChildElement(); propertyGroup; propertyGroup = propertyGroup->NextSiblingElement()) {
|
|
|
|
const std::string name(propertyGroup->Name());
|
|
|
|
const char *text = propertyGroup->GetText();
|
|
|
|
(*variables)[name] = std::string(text ? text : "");
|
|
|
|
}
|
2016-12-04 19:15:04 +01:00
|
|
|
|
2017-08-24 17:28:52 +02:00
|
|
|
} else if (!labelAttribute) {
|
2016-12-04 18:50:59 +01:00
|
|
|
for (const tinyxml2::XMLElement *propertyGroup = node->FirstChildElement(); propertyGroup; propertyGroup = propertyGroup->NextSiblingElement()) {
|
|
|
|
if (std::strcmp(propertyGroup->Name(), "IncludePath") != 0)
|
|
|
|
continue;
|
|
|
|
const char *text = propertyGroup->GetText();
|
|
|
|
if (!text)
|
|
|
|
continue;
|
2017-08-24 17:28:52 +02:00
|
|
|
std::string path(text);
|
2018-04-04 21:51:31 +02:00
|
|
|
const std::string::size_type pos = path.find("$(IncludePath)");
|
2016-12-04 18:50:59 +01:00
|
|
|
if (pos != std::string::npos)
|
2022-07-11 23:07:37 +02:00
|
|
|
path.replace(pos, 14U, *includePath);
|
2017-08-24 17:28:52 +02:00
|
|
|
*includePath = path;
|
2016-12-04 18:50:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-24 22:36:20 +02:00
|
|
|
static void loadVisualStudioProperties(const std::string &props, std::map<std::string,std::string,cppcheck::stricmp> *variables, std::string *includePath, const std::string &additionalIncludeDirectories, std::list<ItemDefinitionGroup> &itemDefinitionGroupList)
|
2016-12-04 18:50:59 +01:00
|
|
|
{
|
|
|
|
std::string filename(props);
|
2018-02-04 20:53:43 +01:00
|
|
|
// variables can't be resolved
|
2017-06-27 12:41:25 +02:00
|
|
|
if (!simplifyPathWithVariables(filename, *variables))
|
2016-12-04 18:50:59 +01:00
|
|
|
return;
|
2017-11-11 11:13:18 +01:00
|
|
|
|
2017-11-11 11:13:45 +01:00
|
|
|
// prepend project dir (if it exists) to transform relative paths into absolute ones
|
|
|
|
if (!Path::isAbsolute(filename) && variables->count("ProjectDir") > 0)
|
|
|
|
filename = Path::getAbsoluteFilePath(variables->at("ProjectDir") + filename);
|
2017-11-11 11:13:18 +01:00
|
|
|
|
2016-12-04 18:50:59 +01:00
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
if (doc.LoadFile(filename.c_str()) != tinyxml2::XML_SUCCESS)
|
|
|
|
return;
|
|
|
|
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
|
|
|
|
if (rootnode == nullptr)
|
|
|
|
return;
|
|
|
|
for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
|
2017-08-28 18:11:04 +02:00
|
|
|
if (std::strcmp(node->Name(), "ImportGroup") == 0) {
|
|
|
|
const char *labelAttribute = node->Attribute("Label");
|
|
|
|
if (labelAttribute == nullptr || std::strcmp(labelAttribute, "PropertySheets") != 0)
|
|
|
|
continue;
|
2016-12-04 18:50:59 +01:00
|
|
|
for (const tinyxml2::XMLElement *importGroup = node->FirstChildElement(); importGroup; importGroup = importGroup->NextSiblingElement()) {
|
2017-08-28 18:11:04 +02:00
|
|
|
if (std::strcmp(importGroup->Name(), "Import") == 0) {
|
|
|
|
const char *projectAttribute = importGroup->Attribute("Project");
|
|
|
|
if (projectAttribute == nullptr)
|
|
|
|
continue;
|
|
|
|
std::string loadprj(projectAttribute);
|
2016-12-04 18:50:59 +01:00
|
|
|
if (loadprj.find('$') == std::string::npos) {
|
|
|
|
loadprj = Path::getPathFromFilename(filename) + loadprj;
|
|
|
|
}
|
|
|
|
loadVisualStudioProperties(loadprj, variables, includePath, additionalIncludeDirectories, itemDefinitionGroupList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (std::strcmp(node->Name(),"PropertyGroup")==0) {
|
2016-12-04 19:15:04 +01:00
|
|
|
importPropertyGroup(node, variables, includePath, nullptr);
|
2016-12-04 18:50:59 +01:00
|
|
|
} else if (std::strcmp(node->Name(),"ItemDefinitionGroup")==0) {
|
2018-04-11 09:44:35 +02:00
|
|
|
itemDefinitionGroupList.emplace_back(node, additionalIncludeDirectories);
|
2016-12-04 18:50:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-19 12:36:11 +01:00
|
|
|
bool ImportProject::importVcxproj(const std::string &filename, std::map<std::string, std::string, cppcheck::stricmp> &variables, const std::string &additionalIncludeDirectories, const std::vector<std::string> &fileFilters)
|
2016-08-13 10:50:03 +02:00
|
|
|
{
|
2016-11-20 22:44:50 +01:00
|
|
|
variables["ProjectDir"] = Path::simplifyPath(Path::getPathFromFilename(filename));
|
2016-11-11 16:22:14 +01:00
|
|
|
|
2016-08-13 10:50:03 +02:00
|
|
|
std::list<ProjectConfiguration> projectConfigurationList;
|
|
|
|
std::list<std::string> compileList;
|
|
|
|
std::list<ItemDefinitionGroup> itemDefinitionGroupList;
|
2016-11-20 22:44:50 +01:00
|
|
|
std::string includePath;
|
2016-08-13 10:50:03 +02:00
|
|
|
|
2016-08-20 12:20:04 +02:00
|
|
|
bool useOfMfc = false;
|
|
|
|
|
2016-08-13 10:50:03 +02:00
|
|
|
tinyxml2::XMLDocument doc;
|
2018-04-04 21:51:31 +02:00
|
|
|
const tinyxml2::XMLError error = doc.LoadFile(filename.c_str());
|
2021-12-19 12:36:11 +01:00
|
|
|
if (error != tinyxml2::XML_SUCCESS) {
|
|
|
|
printError(std::string("Visual Studio project file is not a valid XML - ") + tinyxml2::XMLDocument::ErrorIDToName(error));
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-13 10:50:03 +02:00
|
|
|
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
|
2021-12-19 12:36:11 +01:00
|
|
|
if (rootnode == nullptr) {
|
|
|
|
printError("Visual Studio project file has no XML root node");
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-13 10:50:03 +02:00
|
|
|
for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
|
|
|
|
if (std::strcmp(node->Name(), "ItemGroup") == 0) {
|
2017-08-28 18:11:04 +02:00
|
|
|
const char *labelAttribute = node->Attribute("Label");
|
|
|
|
if (labelAttribute && std::strcmp(labelAttribute, "ProjectConfigurations") == 0) {
|
2016-08-13 10:50:03 +02:00
|
|
|
for (const tinyxml2::XMLElement *cfg = node->FirstChildElement(); cfg; cfg = cfg->NextSiblingElement()) {
|
2017-06-01 13:13:23 +02:00
|
|
|
if (std::strcmp(cfg->Name(), "ProjectConfiguration") == 0) {
|
2018-04-04 21:51:31 +02:00
|
|
|
const ProjectConfiguration p(cfg);
|
2020-01-31 07:08:38 +01:00
|
|
|
if (p.platform != ProjectConfiguration::Unknown) {
|
2018-04-11 09:44:35 +02:00
|
|
|
projectConfigurationList.emplace_back(cfg);
|
2020-01-31 07:08:38 +01:00
|
|
|
mAllVSConfigs.insert(p.configuration);
|
|
|
|
}
|
2017-06-01 13:13:23 +02:00
|
|
|
}
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (const tinyxml2::XMLElement *e = node->FirstChildElement(); e; e = e->NextSiblingElement()) {
|
2017-08-02 22:51:22 +02:00
|
|
|
if (std::strcmp(e->Name(), "ClCompile") == 0) {
|
2017-08-02 07:56:22 +02:00
|
|
|
const char *include = e->Attribute("Include");
|
2017-08-02 22:51:22 +02:00
|
|
|
if (include && Path::acceptFile(include))
|
2019-09-25 15:25:19 +02:00
|
|
|
compileList.emplace_back(include);
|
2017-08-02 07:56:22 +02:00
|
|
|
}
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (std::strcmp(node->Name(), "ItemDefinitionGroup") == 0) {
|
2018-04-11 09:44:35 +02:00
|
|
|
itemDefinitionGroupList.emplace_back(node, additionalIncludeDirectories);
|
2016-08-20 12:20:04 +02:00
|
|
|
} else if (std::strcmp(node->Name(), "PropertyGroup") == 0) {
|
2016-12-04 19:15:04 +01:00
|
|
|
importPropertyGroup(node, &variables, &includePath, &useOfMfc);
|
2016-11-20 22:44:50 +01:00
|
|
|
} else if (std::strcmp(node->Name(), "ImportGroup") == 0) {
|
2017-08-28 18:11:04 +02:00
|
|
|
const char *labelAttribute = node->Attribute("Label");
|
|
|
|
if (labelAttribute && std::strcmp(labelAttribute, "PropertySheets") == 0) {
|
2016-11-20 22:44:50 +01:00
|
|
|
for (const tinyxml2::XMLElement *e = node->FirstChildElement(); e; e = e->NextSiblingElement()) {
|
|
|
|
if (std::strcmp(e->Name(), "Import") == 0) {
|
2017-08-28 18:11:04 +02:00
|
|
|
const char *projectAttribute = e->Attribute("Project");
|
|
|
|
if (projectAttribute)
|
|
|
|
loadVisualStudioProperties(projectAttribute, &variables, &includePath, additionalIncludeDirectories, itemDefinitionGroupList);
|
2016-11-20 22:44:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-19 17:27:41 +02:00
|
|
|
for (const std::string &c : compileList) {
|
2020-05-24 21:23:49 +02:00
|
|
|
const std::string cfilename = Path::simplifyPath(Path::isAbsolute(c) ? c : Path::getPathFromFilename(filename) + c);
|
2021-11-23 22:51:45 +01:00
|
|
|
if (!fileFilters.empty() && !matchglobs(fileFilters, cfilename))
|
2020-05-24 21:23:49 +02:00
|
|
|
continue;
|
|
|
|
|
2018-08-19 17:27:41 +02:00
|
|
|
for (const ProjectConfiguration &p : projectConfigurationList) {
|
2020-01-31 07:08:38 +01:00
|
|
|
|
|
|
|
if (!guiProject.checkVsConfigs.empty()) {
|
2022-12-30 15:13:47 +01:00
|
|
|
const bool doChecking = std::any_of(guiProject.checkVsConfigs.cbegin(), guiProject.checkVsConfigs.cend(), [&](const std::string& c) {
|
2022-10-16 13:46:26 +02:00
|
|
|
return c == p.configuration;
|
|
|
|
});
|
2020-01-31 14:13:52 +01:00
|
|
|
if (!doChecking)
|
2020-01-31 07:08:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-12-07 17:03:46 +01:00
|
|
|
FileSettings fs;
|
2020-05-24 21:23:49 +02:00
|
|
|
fs.filename = cfilename;
|
2018-08-19 17:27:41 +02:00
|
|
|
fs.cfg = p.name;
|
2017-08-08 22:57:11 +02:00
|
|
|
fs.msc = true;
|
|
|
|
fs.useMfc = useOfMfc;
|
2017-08-08 22:26:42 +02:00
|
|
|
fs.defines = "_WIN32=1";
|
2018-08-19 17:27:41 +02:00
|
|
|
if (p.platform == ProjectConfiguration::Win32)
|
2016-12-07 17:03:46 +01:00
|
|
|
fs.platformType = cppcheck::Platform::Win32W;
|
2018-08-19 17:27:41 +02:00
|
|
|
else if (p.platform == ProjectConfiguration::x64) {
|
2016-12-07 17:03:46 +01:00
|
|
|
fs.platformType = cppcheck::Platform::Win64;
|
|
|
|
fs.defines += ";_WIN64=1";
|
|
|
|
}
|
|
|
|
std::string additionalIncludePaths;
|
2018-08-19 17:27:41 +02:00
|
|
|
for (const ItemDefinitionGroup &i : itemDefinitionGroupList) {
|
|
|
|
if (!i.conditionIsTrue(p))
|
2016-08-13 10:50:03 +02:00
|
|
|
continue;
|
2022-04-15 16:17:36 +02:00
|
|
|
fs.standard = Standards::getCPP(i.cppstd);
|
2018-08-19 17:27:41 +02:00
|
|
|
fs.defines += ';' + i.preprocessorDefinitions;
|
2022-01-20 21:09:39 +01:00
|
|
|
if (i.enhancedInstructionSet == "StreamingSIMDExtensions")
|
|
|
|
fs.defines += ";__SSE__";
|
|
|
|
else if (i.enhancedInstructionSet == "StreamingSIMDExtensions2")
|
|
|
|
fs.defines += ";__SSE2__";
|
|
|
|
else if (i.enhancedInstructionSet == "AdvancedVectorExtensions")
|
|
|
|
fs.defines += ";__AVX__";
|
|
|
|
else if (i.enhancedInstructionSet == "AdvancedVectorExtensions2")
|
|
|
|
fs.defines += ";__AVX2__";
|
|
|
|
else if (i.enhancedInstructionSet == "AdvancedVectorExtensions512")
|
|
|
|
fs.defines += ";__AVX512__";
|
2018-08-19 17:27:41 +02:00
|
|
|
additionalIncludePaths += ';' + i.additionalIncludePaths;
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
2016-12-07 17:03:46 +01:00
|
|
|
fs.setDefines(fs.defines);
|
|
|
|
fs.setIncludePaths(Path::getPathFromFilename(filename), toStringList(includePath + ';' + additionalIncludePaths), variables);
|
2022-09-08 09:21:35 +02:00
|
|
|
fileSettings.push_back(std::move(fs));
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-19 12:36:11 +01:00
|
|
|
|
|
|
|
return true;
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
2018-05-20 18:19:20 +02:00
|
|
|
|
2021-12-19 12:36:11 +01:00
|
|
|
bool ImportProject::importBcb6Prj(const std::string &projectFilename)
|
2018-05-20 18:19:20 +02:00
|
|
|
{
|
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
const tinyxml2::XMLError error = doc.LoadFile(projectFilename.c_str());
|
2021-12-19 12:36:11 +01:00
|
|
|
if (error != tinyxml2::XML_SUCCESS) {
|
|
|
|
printError(std::string("Borland project file is not a valid XML - ") + tinyxml2::XMLDocument::ErrorIDToName(error));
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-20 18:19:20 +02:00
|
|
|
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
|
2021-12-19 12:36:11 +01:00
|
|
|
if (rootnode == nullptr) {
|
|
|
|
printError("Borland project file has no XML root node");
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-20 18:19:20 +02:00
|
|
|
|
|
|
|
const std::string& projectDir = Path::simplifyPath(Path::getPathFromFilename(projectFilename));
|
|
|
|
|
|
|
|
std::list<std::string> compileList;
|
|
|
|
std::string includePath;
|
|
|
|
std::string userdefines;
|
|
|
|
std::string sysdefines;
|
|
|
|
std::string cflag1;
|
|
|
|
|
|
|
|
for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
|
|
|
|
if (std::strcmp(node->Name(), "FILELIST") == 0) {
|
|
|
|
for (const tinyxml2::XMLElement *f = node->FirstChildElement(); f; f = f->NextSiblingElement()) {
|
|
|
|
if (std::strcmp(f->Name(), "FILE") == 0) {
|
|
|
|
const char *filename = f->Attribute("FILENAME");
|
|
|
|
if (filename && Path::acceptFile(filename))
|
2019-09-25 15:25:19 +02:00
|
|
|
compileList.emplace_back(filename);
|
2018-05-20 18:19:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (std::strcmp(node->Name(), "MACROS") == 0) {
|
|
|
|
for (const tinyxml2::XMLElement *m = node->FirstChildElement(); m; m = m->NextSiblingElement()) {
|
|
|
|
if (std::strcmp(m->Name(), "INCLUDEPATH") == 0) {
|
|
|
|
const char *v = m->Attribute("value");
|
|
|
|
if (v)
|
|
|
|
includePath = v;
|
|
|
|
} else if (std::strcmp(m->Name(), "USERDEFINES") == 0) {
|
|
|
|
const char *v = m->Attribute("value");
|
|
|
|
if (v)
|
|
|
|
userdefines = v;
|
|
|
|
} else if (std::strcmp(m->Name(), "SYSDEFINES") == 0) {
|
|
|
|
const char *v = m->Attribute("value");
|
|
|
|
if (v)
|
|
|
|
sysdefines = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (std::strcmp(node->Name(), "OPTIONS") == 0) {
|
|
|
|
for (const tinyxml2::XMLElement *m = node->FirstChildElement(); m; m = m->NextSiblingElement()) {
|
|
|
|
if (std::strcmp(m->Name(), "CFLAG1") == 0) {
|
|
|
|
const char *v = m->Attribute("value");
|
|
|
|
if (v)
|
|
|
|
cflag1 = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<std::string> cflags;
|
|
|
|
|
|
|
|
// parse cflag1 and fill the cflags set
|
|
|
|
{
|
|
|
|
std::string arg;
|
|
|
|
|
2022-10-02 07:12:40 +02:00
|
|
|
for (const char i : cflag1) {
|
2019-09-19 20:29:33 +02:00
|
|
|
if (i == ' ' && !arg.empty()) {
|
2018-05-20 18:19:20 +02:00
|
|
|
cflags.insert(arg);
|
|
|
|
arg.clear();
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-19 20:29:33 +02:00
|
|
|
arg += i;
|
2018-05-20 18:19:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!arg.empty()) {
|
|
|
|
cflags.insert(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup: -t is "An alternate name for the -Wxxx switches; there is no difference"
|
|
|
|
// -> Remove every known -txxx argument and replace it with its -Wxxx counterpart.
|
|
|
|
// This way, we know what we have to check for later on.
|
|
|
|
static const std::map<std::string, std::string> synonyms = {
|
|
|
|
{ "-tC","-WC" },
|
|
|
|
{ "-tCDR","-WCDR" },
|
|
|
|
{ "-tCDV","-WCDV" },
|
|
|
|
{ "-tW","-W" },
|
|
|
|
{ "-tWC","-WC" },
|
|
|
|
{ "-tWCDR","-WCDR" },
|
|
|
|
{ "-tWCDV","-WCDV" },
|
|
|
|
{ "-tWD","-WD" },
|
|
|
|
{ "-tWDR","-WDR" },
|
|
|
|
{ "-tWDV","-WDV" },
|
|
|
|
{ "-tWM","-WM" },
|
|
|
|
{ "-tWP","-WP" },
|
|
|
|
{ "-tWR","-WR" },
|
|
|
|
{ "-tWU","-WU" },
|
|
|
|
{ "-tWV","-WV" }
|
|
|
|
};
|
|
|
|
|
2022-12-20 20:32:16 +01:00
|
|
|
for (std::map<std::string, std::string>::const_iterator i = synonyms.cbegin(); i != synonyms.cend(); ++i) {
|
2018-05-20 18:19:20 +02:00
|
|
|
if (cflags.erase(i->first) > 0) {
|
|
|
|
cflags.insert(i->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string predefines;
|
|
|
|
std::string cppPredefines;
|
|
|
|
|
|
|
|
// Collecting predefines. See BCB6 help topic "Predefined macros"
|
|
|
|
{
|
|
|
|
cppPredefines +=
|
|
|
|
// Defined if you've selected C++ compilation; will increase in later releases.
|
|
|
|
// value 0x0560 (but 0x0564 for our BCB6 SP4)
|
|
|
|
// @see http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Predefined_Macros#C.2B.2B_Compiler_Versions_in_Predefined_Macros
|
|
|
|
";__BCPLUSPLUS__=0x0560"
|
|
|
|
|
|
|
|
// Defined if in C++ mode; otherwise, undefined.
|
|
|
|
";__cplusplus=1"
|
|
|
|
|
|
|
|
// Defined as 1 for C++ files(meaning that templates are supported); otherwise, it is undefined.
|
|
|
|
";__TEMPLATES__=1"
|
|
|
|
|
|
|
|
// Defined only for C++ programs to indicate that wchar_t is an intrinsically defined data type.
|
|
|
|
";_WCHAR_T"
|
|
|
|
|
|
|
|
// Defined only for C++ programs to indicate that wchar_t is an intrinsically defined data type.
|
|
|
|
";_WCHAR_T_DEFINED"
|
|
|
|
|
|
|
|
// Defined in any compiler that has an optimizer.
|
|
|
|
";__BCOPT__=1"
|
|
|
|
|
|
|
|
// Version number.
|
|
|
|
// BCB6 is 0x056X (SP4 is 0x0564)
|
|
|
|
// @see http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Predefined_Macros#C.2B.2B_Compiler_Versions_in_Predefined_Macros
|
|
|
|
";__BORLANDC__=0x0560"
|
|
|
|
";__TCPLUSPLUS__=0x0560"
|
|
|
|
";__TURBOC__=0x0560";
|
|
|
|
|
|
|
|
// Defined if Calling Convention is set to cdecl; otherwise undefined.
|
|
|
|
const bool useCdecl = (cflags.find("-p") == cflags.end()
|
|
|
|
&& cflags.find("-pm") == cflags.end()
|
|
|
|
&& cflags.find("-pr") == cflags.end()
|
|
|
|
&& cflags.find("-ps") == cflags.end());
|
|
|
|
if (useCdecl)
|
|
|
|
predefines += ";__CDECL=1";
|
|
|
|
|
|
|
|
// Defined by default indicating that the default char is unsigned char. Use the -K compiler option to undefine this macro.
|
|
|
|
const bool treatCharAsUnsignedChar = (cflags.find("-K") != cflags.end());
|
|
|
|
if (treatCharAsUnsignedChar)
|
|
|
|
predefines += ";_CHAR_UNSIGNED=1";
|
|
|
|
|
|
|
|
// Defined whenever one of the CodeGuard compiler options is used; otherwise it is undefined.
|
|
|
|
const bool codeguardUsed = (cflags.find("-vGd") != cflags.end()
|
|
|
|
|| cflags.find("-vGt") != cflags.end()
|
|
|
|
|| cflags.find("-vGc") != cflags.end());
|
|
|
|
if (codeguardUsed)
|
|
|
|
predefines += ";__CODEGUARD__";
|
|
|
|
|
|
|
|
// When defined, the macro indicates that the program is a console application.
|
|
|
|
const bool isConsoleApp = (cflags.find("-WC") != cflags.end());
|
|
|
|
if (isConsoleApp)
|
|
|
|
predefines += ";__CONSOLE__=1";
|
|
|
|
|
|
|
|
// Enable stack unwinding. This is true by default; use -xd- to disable.
|
|
|
|
const bool enableStackUnwinding = (cflags.find("-xd-") == cflags.end());
|
|
|
|
if (enableStackUnwinding)
|
|
|
|
predefines += ";_CPPUNWIND=1";
|
|
|
|
|
|
|
|
// Defined whenever the -WD compiler option is used; otherwise it is undefined.
|
|
|
|
const bool isDLL = (cflags.find("-WD") != cflags.end());
|
|
|
|
if (isDLL)
|
|
|
|
predefines += ";__DLL__=1";
|
|
|
|
|
|
|
|
// Defined when compiling in 32-bit flat memory model.
|
|
|
|
// TODO: not sure how to switch to another memory model or how to read configuration from project file
|
|
|
|
predefines += ";__FLAT__=1";
|
|
|
|
|
|
|
|
// Always defined. The default value is 300. You can change the value to 400 or 500 by using the /4 or /5 compiler options.
|
|
|
|
if (cflags.find("-6") != cflags.end())
|
|
|
|
predefines += ";_M_IX86=600";
|
|
|
|
else if (cflags.find("-5") != cflags.end())
|
|
|
|
predefines += ";_M_IX86=500";
|
|
|
|
else if (cflags.find("-4") != cflags.end())
|
|
|
|
predefines += ";_M_IX86=400";
|
|
|
|
else
|
|
|
|
predefines += ";_M_IX86=300";
|
|
|
|
|
|
|
|
// Defined only if the -WM option is used. It specifies that the multithread library is to be linked.
|
|
|
|
const bool linkMtLib = (cflags.find("-WM") != cflags.end());
|
|
|
|
if (linkMtLib)
|
|
|
|
predefines += ";__MT__=1";
|
|
|
|
|
|
|
|
// Defined if Calling Convention is set to Pascal; otherwise undefined.
|
|
|
|
const bool usePascalCallingConvention = (cflags.find("-p") != cflags.end());
|
|
|
|
if (usePascalCallingConvention)
|
|
|
|
predefines += ";__PASCAL__=1";
|
|
|
|
|
|
|
|
// Defined if you compile with the -A compiler option; otherwise, it is undefined.
|
|
|
|
const bool useAnsiKeywordExtensions = (cflags.find("-A") != cflags.end());
|
|
|
|
if (useAnsiKeywordExtensions)
|
|
|
|
predefines += ";__STDC__=1";
|
|
|
|
|
|
|
|
// Thread Local Storage. Always true in C++Builder.
|
|
|
|
predefines += ";__TLC__=1";
|
|
|
|
|
|
|
|
// Defined for Windows-only code.
|
|
|
|
const bool isWindowsTarget = (cflags.find("-WC") != cflags.end()
|
|
|
|
|| cflags.find("-WCDR") != cflags.end()
|
|
|
|
|| cflags.find("-WCDV") != cflags.end()
|
|
|
|
|| cflags.find("-WD") != cflags.end()
|
|
|
|
|| cflags.find("-WDR") != cflags.end()
|
|
|
|
|| cflags.find("-WDV") != cflags.end()
|
|
|
|
|| cflags.find("-WM") != cflags.end()
|
|
|
|
|| cflags.find("-WP") != cflags.end()
|
|
|
|
|| cflags.find("-WR") != cflags.end()
|
|
|
|
|| cflags.find("-WU") != cflags.end()
|
|
|
|
|| cflags.find("-WV") != cflags.end());
|
|
|
|
if (isWindowsTarget)
|
|
|
|
predefines += ";_Windows";
|
|
|
|
|
|
|
|
// Defined for console and GUI applications.
|
|
|
|
// TODO: I'm not sure about the difference to define "_Windows".
|
|
|
|
// From description, I would assume __WIN32__ is only defined for
|
|
|
|
// executables, while _Windows would also be defined for DLLs, etc.
|
|
|
|
// However, in a newly created DLL project, both __WIN32__ and
|
|
|
|
// _Windows are defined. -> treating them the same for now.
|
|
|
|
// Also boost uses __WIN32__ for OS identification.
|
|
|
|
const bool isConsoleOrGuiApp = isWindowsTarget;
|
|
|
|
if (isConsoleOrGuiApp)
|
|
|
|
predefines += ";__WIN32__=1";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include paths may contain variables like "$(BCB)\include" or "$(BCB)\include\vcl".
|
|
|
|
// Those get resolved by ImportProject::FileSettings::setIncludePaths by
|
|
|
|
// 1. checking the provided variables map ("BCB" => "C:\\Program Files (x86)\\Borland\\CBuilder6")
|
|
|
|
// 2. checking env variables as a fallback
|
|
|
|
// Setting env is always possible. Configuring the variables via cli might be an addition.
|
|
|
|
// Reading the BCB6 install location from registry in windows environments would also be possible,
|
|
|
|
// but I didn't see any such functionality around the source. Not in favor of adding it only
|
|
|
|
// for the BCB6 project loading.
|
|
|
|
std::map<std::string, std::string, cppcheck::stricmp> variables;
|
|
|
|
const std::string defines = predefines + ";" + sysdefines + ";" + userdefines;
|
|
|
|
const std::string cppDefines = cppPredefines + ";" + defines;
|
|
|
|
const bool forceCppMode = (cflags.find("-P") != cflags.end());
|
|
|
|
|
2018-08-19 17:27:41 +02:00
|
|
|
for (const std::string &c : compileList) {
|
2018-05-20 18:19:20 +02:00
|
|
|
// C++ compilation is selected by file extension by default, so these
|
|
|
|
// defines have to be configured on a per-file base.
|
|
|
|
//
|
|
|
|
// > Files with the .CPP extension compile as C++ files. Files with a .C
|
|
|
|
// > extension, with no extension, or with extensions other than .CPP,
|
|
|
|
// > .OBJ, .LIB, or .ASM compile as C files.
|
|
|
|
// (http://docwiki.embarcadero.com/RADStudio/Tokyo/en/BCC32.EXE,_the_C%2B%2B_32-bit_Command-Line_Compiler)
|
|
|
|
//
|
|
|
|
// We can also force C++ compilation for all files using the -P command line switch.
|
2018-08-19 17:27:41 +02:00
|
|
|
const bool cppMode = forceCppMode || Path::getFilenameExtensionInLowerCase(c) == ".cpp";
|
2018-05-20 18:19:20 +02:00
|
|
|
FileSettings fs;
|
|
|
|
fs.setIncludePaths(projectDir, toStringList(includePath), variables);
|
|
|
|
fs.setDefines(cppMode ? cppDefines : defines);
|
2018-08-19 17:27:41 +02:00
|
|
|
fs.filename = Path::simplifyPath(Path::isAbsolute(c) ? c : projectDir + c);
|
2022-09-08 09:21:35 +02:00
|
|
|
fileSettings.push_back(std::move(fs));
|
2018-05-20 18:19:20 +02:00
|
|
|
}
|
2021-12-19 12:36:11 +01:00
|
|
|
|
|
|
|
return true;
|
2018-05-20 18:19:20 +02:00
|
|
|
}
|
2019-01-31 20:40:15 +01:00
|
|
|
|
2019-04-11 21:02:49 +02:00
|
|
|
static std::string joinRelativePath(const std::string &path1, const std::string &path2)
|
|
|
|
{
|
|
|
|
if (!path1.empty() && !Path::isAbsolute(path2))
|
|
|
|
return path1 + path2;
|
|
|
|
return path2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::list<std::string> readXmlStringList(const tinyxml2::XMLElement *node, const std::string &path, const char name[], const char attribute[])
|
2019-01-31 20:40:15 +01:00
|
|
|
{
|
|
|
|
std::list<std::string> ret;
|
|
|
|
for (const tinyxml2::XMLElement *child = node->FirstChildElement(); child; child = child->NextSiblingElement()) {
|
|
|
|
if (strcmp(child->Name(), name) != 0)
|
|
|
|
continue;
|
|
|
|
const char *attr = attribute ? child->Attribute(attribute) : child->GetText();
|
|
|
|
if (attr)
|
2019-04-11 21:02:49 +02:00
|
|
|
ret.push_back(joinRelativePath(path, attr));
|
2019-01-31 20:40:15 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string join(const std::list<std::string> &strlist, const char *sep)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
for (const std::string &s : strlist) {
|
|
|
|
ret += (ret.empty() ? "" : sep) + s;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-01 00:09:07 +01:00
|
|
|
static std::string istream_to_string(std::istream &istr)
|
|
|
|
{
|
|
|
|
std::istreambuf_iterator<char> eos;
|
|
|
|
return std::string(std::istreambuf_iterator<char>(istr), eos);
|
|
|
|
}
|
2019-01-31 20:40:15 +01:00
|
|
|
|
2020-01-31 07:08:38 +01:00
|
|
|
|
2019-01-31 20:40:15 +01:00
|
|
|
bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *settings)
|
|
|
|
{
|
|
|
|
tinyxml2::XMLDocument doc;
|
2019-02-01 00:09:07 +01:00
|
|
|
const std::string xmldata = istream_to_string(istr);
|
2021-12-19 12:36:11 +01:00
|
|
|
const tinyxml2::XMLError error = doc.Parse(xmldata.data(), xmldata.size());
|
|
|
|
if (error != tinyxml2::XML_SUCCESS) {
|
|
|
|
printError(std::string("Cppcheck GUI project file is not a valid XML - ") + tinyxml2::XMLDocument::ErrorIDToName(error));
|
2019-01-31 20:40:15 +01:00
|
|
|
return false;
|
2021-12-19 12:36:11 +01:00
|
|
|
}
|
2019-01-31 20:40:15 +01:00
|
|
|
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
|
2021-12-19 12:36:11 +01:00
|
|
|
if (rootnode == nullptr || strcmp(rootnode->Name(), CppcheckXml::ProjectElementName) != 0) {
|
|
|
|
printError("Cppcheck GUI project file has no XML root node");
|
2019-01-31 20:40:15 +01:00
|
|
|
return false;
|
2021-12-19 12:36:11 +01:00
|
|
|
}
|
2019-01-31 20:40:15 +01:00
|
|
|
|
2019-04-11 21:02:49 +02:00
|
|
|
const std::string &path = mPath;
|
|
|
|
|
2019-01-31 20:40:15 +01:00
|
|
|
std::list<std::string> paths;
|
2020-07-16 15:27:07 +02:00
|
|
|
std::list<Suppressions::Suppression> suppressions;
|
2019-01-31 20:40:15 +01:00
|
|
|
Settings temp;
|
|
|
|
|
2019-04-14 07:27:23 +02:00
|
|
|
guiProject.analyzeAllVsConfigs.clear();
|
2019-04-13 20:01:40 +02:00
|
|
|
|
2019-01-31 20:40:15 +01:00
|
|
|
for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
|
2019-08-09 21:15:02 +02:00
|
|
|
if (strcmp(node->Name(), CppcheckXml::RootPathName) == 0 && node->Attribute(CppcheckXml::RootPathNameAttrib)) {
|
|
|
|
temp.basePaths.push_back(joinRelativePath(path, node->Attribute(CppcheckXml::RootPathNameAttrib)));
|
2019-04-15 19:00:57 +02:00
|
|
|
temp.relativePaths = true;
|
2020-04-27 09:08:50 +02:00
|
|
|
} else if (strcmp(node->Name(), CppcheckXml::BugHunting) == 0)
|
2022-04-11 07:30:55 +02:00
|
|
|
;
|
2020-04-27 09:08:50 +02:00
|
|
|
else if (strcmp(node->Name(), CppcheckXml::BuildDirElementName) == 0)
|
2019-04-11 21:02:49 +02:00
|
|
|
temp.buildDir = joinRelativePath(path, node->GetText() ? node->GetText() : "");
|
2019-08-09 21:15:02 +02:00
|
|
|
else if (strcmp(node->Name(), CppcheckXml::IncludeDirElementName) == 0)
|
|
|
|
temp.includePaths = readXmlStringList(node, path, CppcheckXml::DirElementName, CppcheckXml::DirNameAttrib);
|
|
|
|
else if (strcmp(node->Name(), CppcheckXml::DefinesElementName) == 0)
|
|
|
|
temp.userDefines = join(readXmlStringList(node, "", CppcheckXml::DefineName, CppcheckXml::DefineNameAttrib), ";");
|
|
|
|
else if (strcmp(node->Name(), CppcheckXml::UndefinesElementName) == 0) {
|
|
|
|
for (const std::string &u : readXmlStringList(node, "", CppcheckXml::UndefineName, nullptr))
|
2019-01-31 20:40:15 +01:00
|
|
|
temp.userUndefs.insert(u);
|
2019-08-09 21:15:02 +02:00
|
|
|
} else if (strcmp(node->Name(), CppcheckXml::ImportProjectElementName) == 0)
|
2019-04-11 21:02:49 +02:00
|
|
|
guiProject.projectFile = path + (node->GetText() ? node->GetText() : "");
|
2019-08-09 21:15:02 +02:00
|
|
|
else if (strcmp(node->Name(), CppcheckXml::PathsElementName) == 0)
|
|
|
|
paths = readXmlStringList(node, path, CppcheckXml::PathName, CppcheckXml::PathNameAttrib);
|
|
|
|
else if (strcmp(node->Name(), CppcheckXml::ExcludeElementName) == 0)
|
|
|
|
guiProject.excludedPaths = readXmlStringList(node, "", CppcheckXml::ExcludePathName, CppcheckXml::ExcludePathNameAttrib);
|
2022-04-11 07:30:55 +02:00
|
|
|
else if (strcmp(node->Name(), CppcheckXml::FunctionContracts) == 0)
|
|
|
|
;
|
|
|
|
else if (strcmp(node->Name(), CppcheckXml::VariableContractsElementName) == 0)
|
|
|
|
;
|
|
|
|
else if (strcmp(node->Name(), CppcheckXml::IgnoreElementName) == 0)
|
2019-08-09 21:15:02 +02:00
|
|
|
guiProject.excludedPaths = readXmlStringList(node, "", CppcheckXml::IgnorePathName, CppcheckXml::IgnorePathNameAttrib);
|
|
|
|
else if (strcmp(node->Name(), CppcheckXml::LibrariesElementName) == 0)
|
|
|
|
guiProject.libraries = readXmlStringList(node, "", CppcheckXml::LibraryElementName, nullptr);
|
2020-07-16 15:27:07 +02:00
|
|
|
else if (strcmp(node->Name(), CppcheckXml::SuppressionsElementName) == 0) {
|
|
|
|
for (const tinyxml2::XMLElement *child = node->FirstChildElement(); child; child = child->NextSiblingElement()) {
|
2020-07-17 11:26:03 +02:00
|
|
|
if (strcmp(child->Name(), CppcheckXml::SuppressionElementName) != 0)
|
2020-07-16 15:27:07 +02:00
|
|
|
continue;
|
2020-07-16 16:36:11 +02:00
|
|
|
auto read = [](const char *s, const char *def) {
|
|
|
|
return s ? s : def;
|
|
|
|
};
|
2020-07-16 15:27:07 +02:00
|
|
|
Suppressions::Suppression s;
|
|
|
|
s.errorId = read(child->GetText(), "");
|
|
|
|
s.fileName = read(child->Attribute("fileName"), "");
|
2020-07-17 11:26:03 +02:00
|
|
|
if (!s.fileName.empty())
|
|
|
|
s.fileName = joinRelativePath(path, s.fileName);
|
2020-07-16 15:27:07 +02:00
|
|
|
s.lineNumber = child->IntAttribute("lineNumber", Suppressions::Suppression::NO_LINE);
|
|
|
|
s.symbolName = read(child->Attribute("symbolName"), "");
|
2020-07-21 11:27:03 +02:00
|
|
|
std::istringstream(read(child->Attribute("hash"), "0")) >> s.hash;
|
2022-09-08 09:21:35 +02:00
|
|
|
suppressions.push_back(std::move(s));
|
2020-07-16 15:27:07 +02:00
|
|
|
}
|
2020-07-16 16:36:11 +02:00
|
|
|
} else if (strcmp(node->Name(), CppcheckXml::VSConfigurationElementName) == 0)
|
2022-07-10 10:57:29 +02:00
|
|
|
guiProject.checkVsConfigs = readXmlStringList(node, emptyString, CppcheckXml::VSConfigurationName, nullptr);
|
2019-08-09 21:15:02 +02:00
|
|
|
else if (strcmp(node->Name(), CppcheckXml::PlatformElementName) == 0)
|
2019-01-31 20:40:15 +01:00
|
|
|
guiProject.platform = node->GetText();
|
2019-08-09 21:15:02 +02:00
|
|
|
else if (strcmp(node->Name(), CppcheckXml::AnalyzeAllVsConfigsElementName) == 0)
|
2019-04-13 20:01:40 +02:00
|
|
|
guiProject.analyzeAllVsConfigs = node->GetText();
|
2021-04-30 16:47:02 +02:00
|
|
|
else if (strcmp(node->Name(), CppcheckXml::Parser) == 0)
|
|
|
|
temp.clang = true;
|
2022-10-31 15:04:16 +01:00
|
|
|
else if (strcmp(node->Name(), CppcheckXml::AddonsElementName) == 0) {
|
|
|
|
const auto& addons = readXmlStringList(node, emptyString, CppcheckXml::AddonElementName, nullptr);
|
|
|
|
temp.addons.insert(addons.cbegin(), addons.cend());
|
|
|
|
}
|
2019-08-09 21:15:02 +02:00
|
|
|
else if (strcmp(node->Name(), CppcheckXml::TagsElementName) == 0)
|
|
|
|
node->Attribute(CppcheckXml::TagElementName); // FIXME: Write some warning
|
2020-01-30 07:14:17 +01:00
|
|
|
else if (strcmp(node->Name(), CppcheckXml::ToolsElementName) == 0) {
|
2022-07-10 10:57:29 +02:00
|
|
|
const std::list<std::string> toolList = readXmlStringList(node, emptyString, CppcheckXml::ToolElementName, nullptr);
|
2020-01-30 07:14:17 +01:00
|
|
|
for (const std::string &toolName : toolList) {
|
2022-08-14 12:44:09 +02:00
|
|
|
if (toolName == CppcheckXml::ClangTidy)
|
2020-01-30 07:14:17 +01:00
|
|
|
temp.clangTidy = true;
|
|
|
|
}
|
|
|
|
} else if (strcmp(node->Name(), CppcheckXml::CheckHeadersElementName) == 0)
|
2019-04-10 17:04:18 +02:00
|
|
|
temp.checkHeaders = (strcmp(node->GetText(), "true") == 0);
|
2019-08-09 21:15:02 +02:00
|
|
|
else if (strcmp(node->Name(), CppcheckXml::CheckUnusedTemplatesElementName) == 0)
|
2019-04-10 17:04:18 +02:00
|
|
|
temp.checkUnusedTemplates = (strcmp(node->GetText(), "true") == 0);
|
2019-08-09 21:15:02 +02:00
|
|
|
else if (strcmp(node->Name(), CppcheckXml::MaxCtuDepthElementName) == 0)
|
2019-04-10 17:04:18 +02:00
|
|
|
temp.maxCtuDepth = std::atoi(node->GetText());
|
2020-06-27 10:34:02 +02:00
|
|
|
else if (strcmp(node->Name(), CppcheckXml::MaxTemplateRecursionElementName) == 0)
|
|
|
|
temp.maxTemplateRecursion = std::atoi(node->GetText());
|
2019-08-09 21:15:02 +02:00
|
|
|
else if (strcmp(node->Name(), CppcheckXml::CheckUnknownFunctionReturn) == 0)
|
|
|
|
; // TODO
|
2019-07-23 11:54:38 +02:00
|
|
|
else if (strcmp(node->Name(), Settings::SafeChecks::XmlRootName) == 0) {
|
|
|
|
for (const tinyxml2::XMLElement *child = node->FirstChildElement(); child; child = child->NextSiblingElement()) {
|
|
|
|
if (strcmp(child->Name(), Settings::SafeChecks::XmlClasses) == 0)
|
|
|
|
temp.safeChecks.classes = true;
|
|
|
|
else if (strcmp(child->Name(), Settings::SafeChecks::XmlExternalFunctions) == 0)
|
|
|
|
temp.safeChecks.externalFunctions = true;
|
|
|
|
else if (strcmp(child->Name(), Settings::SafeChecks::XmlInternalFunctions) == 0)
|
2019-07-23 14:10:34 +02:00
|
|
|
temp.safeChecks.internalFunctions = true;
|
2019-07-23 11:54:38 +02:00
|
|
|
else if (strcmp(child->Name(), Settings::SafeChecks::XmlExternalVariables) == 0)
|
|
|
|
temp.safeChecks.externalVariables = true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-19 08:07:10 +02:00
|
|
|
} else if (strcmp(node->Name(), CppcheckXml::TagWarningsElementName) == 0)
|
|
|
|
; // TODO
|
2022-08-23 20:13:03 +02:00
|
|
|
// Cppcheck Premium features
|
|
|
|
else if (strcmp(node->Name(), CppcheckXml::BughuntingElementName) == 0)
|
|
|
|
temp.premiumArgs += " --bughunting";
|
|
|
|
else if (strcmp(node->Name(), CppcheckXml::CertIntPrecisionElementName) == 0)
|
|
|
|
temp.premiumArgs += std::string(" --cert-c-int-precision=") + (node->GetText() ? node->GetText() : "0");
|
|
|
|
else if (strcmp(node->Name(), CppcheckXml::CodingStandardsElementName) == 0) {
|
|
|
|
for (const tinyxml2::XMLElement *child = node->FirstChildElement(); child; child = child->NextSiblingElement()) {
|
|
|
|
if (strcmp(child->Name(), CppcheckXml::CodingStandardElementName) == 0 && child->GetText())
|
|
|
|
temp.premiumArgs += std::string(" --") + child->GetText();
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 08:07:10 +02:00
|
|
|
else
|
2019-01-31 20:40:15 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
settings->basePaths = temp.basePaths;
|
2019-04-15 19:00:57 +02:00
|
|
|
settings->relativePaths |= temp.relativePaths;
|
2019-01-31 20:40:15 +01:00
|
|
|
settings->buildDir = temp.buildDir;
|
|
|
|
settings->includePaths = temp.includePaths;
|
|
|
|
settings->userDefines = temp.userDefines;
|
|
|
|
settings->userUndefs = temp.userUndefs;
|
2019-04-07 17:01:59 +02:00
|
|
|
settings->addons = temp.addons;
|
2021-04-30 16:47:02 +02:00
|
|
|
settings->clang = temp.clang;
|
2020-01-30 07:14:17 +01:00
|
|
|
settings->clangTidy = temp.clangTidy;
|
2020-01-31 07:08:38 +01:00
|
|
|
|
2022-08-23 20:13:03 +02:00
|
|
|
if (!settings->premiumArgs.empty())
|
|
|
|
settings->premiumArgs += temp.premiumArgs;
|
|
|
|
else if (!temp.premiumArgs.empty())
|
|
|
|
settings->premiumArgs = temp.premiumArgs.substr(1);
|
|
|
|
|
2019-04-30 20:19:21 +02:00
|
|
|
for (const std::string &p : paths)
|
|
|
|
guiProject.pathNames.push_back(p);
|
2020-07-16 15:27:07 +02:00
|
|
|
for (const Suppressions::Suppression &supp : suppressions)
|
|
|
|
settings->nomsg.addSuppression(supp);
|
2019-04-10 17:04:18 +02:00
|
|
|
settings->checkHeaders = temp.checkHeaders;
|
|
|
|
settings->checkUnusedTemplates = temp.checkUnusedTemplates;
|
|
|
|
settings->maxCtuDepth = temp.maxCtuDepth;
|
2020-06-27 12:57:09 +02:00
|
|
|
settings->maxTemplateRecursion = temp.maxTemplateRecursion;
|
2019-07-23 14:10:34 +02:00
|
|
|
settings->safeChecks = temp.safeChecks;
|
2021-12-19 12:36:11 +01:00
|
|
|
|
2019-01-31 20:40:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
2019-04-13 20:01:40 +02:00
|
|
|
|
|
|
|
void ImportProject::selectOneVsConfig(Settings::PlatformType platform)
|
|
|
|
{
|
|
|
|
std::set<std::string> filenames;
|
|
|
|
for (std::list<ImportProject::FileSettings>::iterator it = fileSettings.begin(); it != fileSettings.end();) {
|
|
|
|
if (it->cfg.empty()) {
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const ImportProject::FileSettings &fs = *it;
|
|
|
|
bool remove = false;
|
|
|
|
if (fs.cfg.compare(0,5,"Debug") != 0)
|
|
|
|
remove = true;
|
|
|
|
if (platform == Settings::Win64 && fs.platformType != platform)
|
|
|
|
remove = true;
|
|
|
|
else if ((platform == Settings::Win32A || platform == Settings::Win32W) && fs.platformType == Settings::Win64)
|
|
|
|
remove = true;
|
|
|
|
else if (fs.platformType != Settings::Win64 && platform == Settings::Win64)
|
|
|
|
remove = true;
|
|
|
|
else if (filenames.find(fs.filename) != filenames.end())
|
|
|
|
remove = true;
|
|
|
|
if (remove) {
|
|
|
|
it = fileSettings.erase(it);
|
|
|
|
} else {
|
|
|
|
filenames.insert(fs.filename);
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 07:08:38 +01:00
|
|
|
|
|
|
|
std::list<std::string> ImportProject::getVSConfigs()
|
|
|
|
{
|
2022-12-30 15:13:47 +01:00
|
|
|
return std::list<std::string>(mAllVSConfigs.cbegin(), mAllVSConfigs.cend());
|
2020-02-02 12:15:23 +01:00
|
|
|
}
|
2021-06-12 11:10:35 +02:00
|
|
|
|
|
|
|
void ImportProject::setRelativePaths(const std::string &filename)
|
|
|
|
{
|
|
|
|
if (Path::isAbsolute(filename))
|
|
|
|
return;
|
2021-06-12 12:45:31 +02:00
|
|
|
const std::vector<std::string> basePaths{Path::fromNativeSeparators(Path::getCurrentPath())};
|
2021-06-12 11:10:35 +02:00
|
|
|
for (auto &fs: fileSettings) {
|
|
|
|
fs.filename = Path::getRelativePath(fs.filename, basePaths);
|
|
|
|
for (auto &includePath: fs.includePaths)
|
|
|
|
includePath = Path::getRelativePath(includePath, basePaths);
|
|
|
|
}
|
|
|
|
}
|
2021-12-19 12:36:11 +01:00
|
|
|
|
|
|
|
void ImportProject::printError(const std::string &message)
|
|
|
|
{
|
|
|
|
std::cout << "cppcheck: error: " << message << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImportProject::sourceFileExists(const std::string &file)
|
|
|
|
{
|
|
|
|
return Path::fileExists(file);
|
|
|
|
}
|