2016-08-13 10:50:03 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
|
|
|
* Copyright (C) 2007-2016 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 "importproject.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2016-08-13 10:50:03 +02:00
|
|
|
#include "path.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "tinyxml2.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "token.h"
|
|
|
|
#include "tokenize.h"
|
|
|
|
#include "tokenlist.h"
|
2017-04-01 18:14:18 +02:00
|
|
|
#include "utils.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
|
|
|
#include <cstring>
|
2016-08-13 10:50:03 +02:00
|
|
|
#include <fstream>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <utility>
|
2016-08-13 10:50:03 +02:00
|
|
|
|
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;
|
|
|
|
for (std::size_t i = 0; i < ipaths.size(); ++i) {
|
|
|
|
if (it->filename.size() > ipaths[i].size() && it->filename.compare(0,ipaths[i].size(),ipaths[i])==0) {
|
|
|
|
ignore = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ignore)
|
|
|
|
fileSettings.erase(it++);
|
|
|
|
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)
|
|
|
|
fileSettings.erase(it++);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-20 13:47:25 +02:00
|
|
|
void ImportProject::ignoreOtherPlatforms(cppcheck::Platform::PlatformType platformType)
|
|
|
|
{
|
|
|
|
for (std::list<FileSettings>::iterator it = fileSettings.begin(); it != fileSettings.end();) {
|
|
|
|
if (it->platformType != cppcheck::Platform::Unspecified && it->platformType != platformType)
|
|
|
|
fileSettings.erase(it++);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 10:50:03 +02:00
|
|
|
void ImportProject::FileSettings::setDefines(std::string defs)
|
|
|
|
{
|
|
|
|
while (defs.find(";%(") != std::string::npos) {
|
|
|
|
std::string::size_type pos1 = defs.find(";%(");
|
2016-11-30 12:01:22 +01:00
|
|
|
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);
|
2017-04-01 18:14:18 +02:00
|
|
|
if (!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-06-27 12:41:25 +02:00
|
|
|
static bool simplifyPathWithVariables(std::string &s, std::map<std::string, std::string> &variables)
|
2016-11-20 22:44:50 +01:00
|
|
|
{
|
|
|
|
std::set<std::string> expanded;
|
|
|
|
std::string::size_type start = 0;
|
|
|
|
while ((start = s.find("$(")) != std::string::npos) {
|
2016-11-30 12:01:22 +01:00
|
|
|
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);
|
|
|
|
std::map<std::string, std::string>::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) {
|
|
|
|
//! @TODO generate a debug/info message about undefined variable
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
variables[var] = std::string(envValue);
|
|
|
|
it1 = variables.find(var);
|
|
|
|
}
|
|
|
|
s = s.substr(0, start) + it1->second + s.substr(end + 1);
|
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-06-27 12:41:25 +02:00
|
|
|
void ImportProject::FileSettings::setIncludePaths(const std::string &basepath, const std::list<std::string> &in, std::map<std::string, std::string> &variables)
|
2016-08-13 10:50:03 +02:00
|
|
|
{
|
|
|
|
std::list<std::string> I;
|
2017-06-22 16:49:36 +02:00
|
|
|
// only parse each includePath once - so remove duplicates
|
|
|
|
std::list<std::string> uniqueIncludePaths = in;
|
|
|
|
uniqueIncludePaths.sort();
|
|
|
|
uniqueIncludePaths.unique();
|
|
|
|
|
2017-06-22 16:53:13 +02:00
|
|
|
for (std::list<std::string>::const_iterator it = uniqueIncludePaths.begin(); it != uniqueIncludePaths.end(); ++it) {
|
2016-08-13 10:50:03 +02:00
|
|
|
if (it->empty())
|
|
|
|
continue;
|
|
|
|
if (it->compare(0,2,"%(")==0)
|
|
|
|
continue;
|
|
|
|
std::string s(Path::fromNativeSeparators(*it));
|
|
|
|
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 += '/';
|
|
|
|
I.push_back(s);
|
|
|
|
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
|
|
|
|
2016-11-13 12:16:09 +01: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 {
|
2016-11-20 22:44:50 +01: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;
|
|
|
|
I.push_back(s + '/');
|
|
|
|
}
|
|
|
|
includePaths.swap(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImportProject::import(const std::string &filename)
|
|
|
|
{
|
|
|
|
std::ifstream fin(filename);
|
|
|
|
if (!fin.is_open())
|
|
|
|
return;
|
|
|
|
if (filename.find("compile_commands.json") != std::string::npos) {
|
|
|
|
importCompileCommands(fin);
|
|
|
|
} else if (filename.find(".sln") != std::string::npos) {
|
|
|
|
std::string path(Path::getPathFromFilename(Path::fromNativeSeparators(filename)));
|
2017-04-01 18:14:18 +02:00
|
|
|
if (!path.empty() && !endsWith(path,'/'))
|
2016-08-13 10:50:03 +02:00
|
|
|
path += '/';
|
|
|
|
importSln(fin,path);
|
|
|
|
} else if (filename.find(".vcxproj") != std::string::npos) {
|
2016-11-11 16:22:14 +01:00
|
|
|
std::map<std::string, std::string> variables;
|
2017-03-01 10:50:50 +01:00
|
|
|
importVcxproj(filename, variables, emptyString);
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImportProject::importCompileCommands(std::istream &istr)
|
|
|
|
{
|
|
|
|
std::map<std::string, std::string> values;
|
|
|
|
|
|
|
|
// TODO: Use a JSON parser
|
|
|
|
|
|
|
|
Settings settings;
|
|
|
|
TokenList tokenList(&settings);
|
|
|
|
tokenList.createTokens(istr);
|
|
|
|
for (const Token *tok = tokenList.front(); tok; tok = tok->next()) {
|
|
|
|
if (Token::Match(tok, "%str% : %str% [,}]")) {
|
2016-12-06 20:02:43 +01:00
|
|
|
const std::string& key = tok->str();
|
|
|
|
const std::string& value = tok->strAt(2);
|
2016-08-13 10:50:03 +02:00
|
|
|
values[key.substr(1, key.size() - 2U)] = value.substr(1, value.size() - 2U);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (tok->str() == "}") {
|
|
|
|
if (!values["file"].empty() && !values["command"].empty()) {
|
|
|
|
struct FileSettings fs;
|
|
|
|
fs.filename = Path::fromNativeSeparators(values["file"]);
|
2016-12-06 20:02:43 +01:00
|
|
|
const std::string& command = values["command"];
|
2016-08-13 10:50:03 +02:00
|
|
|
const std::string directory = Path::fromNativeSeparators(values["directory"]);
|
|
|
|
std::string::size_type pos = 0;
|
2016-11-30 12:01:22 +01:00
|
|
|
while (std::string::npos != (pos = command.find(' ',pos))) {
|
2016-08-13 10:50:03 +02:00
|
|
|
pos++;
|
|
|
|
if (pos >= command.size())
|
|
|
|
break;
|
|
|
|
if (command[pos] != '/' && command[pos] != '-')
|
|
|
|
continue;
|
|
|
|
pos++;
|
|
|
|
if (pos >= command.size())
|
|
|
|
break;
|
|
|
|
char F = command[pos++];
|
|
|
|
std::string fval;
|
|
|
|
while (pos < command.size() && command[pos] != ' ') {
|
|
|
|
if (command[pos] != '\\')
|
|
|
|
fval += command[pos];
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
if (F=='D')
|
|
|
|
fs.defines += fval + ";";
|
|
|
|
else if (F=='U')
|
|
|
|
fs.undefs.insert(fval);
|
|
|
|
else if (F=='I')
|
|
|
|
fs.includePaths.push_back(fval);
|
|
|
|
}
|
2016-11-11 16:22:14 +01:00
|
|
|
std::map<std::string, std::string> variables;
|
|
|
|
fs.setIncludePaths(directory, fs.includePaths, variables);
|
2016-08-13 10:50:03 +02:00
|
|
|
fs.setDefines(fs.defines);
|
|
|
|
fileSettings.push_back(fs);
|
|
|
|
}
|
|
|
|
values.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImportProject::importSln(std::istream &istr, const std::string &path)
|
|
|
|
{
|
2016-11-11 16:22:14 +01:00
|
|
|
std::map<std::string,std::string> variables;
|
|
|
|
variables["SolutionDir"] = path;
|
2016-11-13 12:16:09 +01:00
|
|
|
|
2016-08-13 10:50:03 +02:00
|
|
|
std::string line;
|
|
|
|
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;
|
|
|
|
importVcxproj(Path::fromNativeSeparators(vcxproj), variables, emptyString);
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct ProjectConfiguration {
|
|
|
|
explicit ProjectConfiguration(const tinyxml2::XMLElement *cfg) {
|
|
|
|
const char *a = cfg->Attribute("Include");
|
|
|
|
if (a)
|
|
|
|
name = a;
|
|
|
|
for (const tinyxml2::XMLElement *e = cfg->FirstChildElement(); e; e = e->NextSiblingElement()) {
|
2016-08-24 12:17:54 +02:00
|
|
|
if (e->GetText()) {
|
|
|
|
if (std::strcmp(e->Name(),"Configuration")==0)
|
|
|
|
configuration = e->GetText();
|
2017-06-01 13:13:23 +02:00
|
|
|
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 {
|
2016-11-13 12:16:09 +01:00
|
|
|
explicit ItemDefinitionGroup(const tinyxml2::XMLElement *idg, const std::string &includePaths) : additionalIncludePaths(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()) {
|
|
|
|
if (std::strcmp(e1->Name(), "ClCompile") != 0)
|
|
|
|
continue;
|
|
|
|
for (const tinyxml2::XMLElement *e = e1->FirstChildElement(); e; e = e->NextSiblingElement()) {
|
2016-08-24 12:17:54 +02:00
|
|
|
if (e->GetText()) {
|
|
|
|
if (std::strcmp(e->Name(), "PreprocessorDefinitions") == 0)
|
|
|
|
preprocessorDefinitions = e->GetText();
|
2016-11-13 12:16:09 +01:00
|
|
|
else if (std::strcmp(e->Name(), "AdditionalIncludeDirectories") == 0) {
|
|
|
|
if (!additionalIncludePaths.empty())
|
|
|
|
additionalIncludePaths += ';';
|
|
|
|
additionalIncludePaths += e->GetText();
|
|
|
|
}
|
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;
|
|
|
|
std::string preprocessorDefinitions;
|
|
|
|
std::string additionalIncludePaths;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-12-04 19:15:04 +01:00
|
|
|
static void importPropertyGroup(const tinyxml2::XMLElement *node, std::map<std::string,std::string> *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()) {
|
|
|
|
if (std::strcmp(e->Name(), "UseOfMfc") == 0)
|
|
|
|
*useOfMfc = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-04 18:50:59 +01:00
|
|
|
if (node->Attribute("Label") && std::strcmp(node->Attribute("Label"),"UserMacros")==0) {
|
|
|
|
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
|
|
|
|
2016-12-04 18:50:59 +01:00
|
|
|
} else if (!node->Attribute("Label")) {
|
|
|
|
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;
|
|
|
|
std::string s(text);
|
|
|
|
std::string::size_type pos = s.find("$(IncludePath)");
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
s = s.substr(0,pos) + *includePath + s.substr(pos+14U);
|
|
|
|
*includePath = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void loadVisualStudioProperties(const std::string &props, std::map<std::string,std::string> *variables, std::string *includePath, const std::string &additionalIncludeDirectories, std::list<ItemDefinitionGroup> &itemDefinitionGroupList)
|
|
|
|
{
|
|
|
|
std::string filename(props);
|
2017-06-27 12:41:25 +02:00
|
|
|
if (!simplifyPathWithVariables(filename, *variables))
|
2016-12-04 18:50:59 +01:00
|
|
|
return;
|
|
|
|
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()) {
|
|
|
|
if (std::strcmp(node->Name(), "ImportGroup") == 0 && node->Attribute("Label") && std::strcmp(node->Attribute("Label"),"PropertySheets")==0) {
|
|
|
|
for (const tinyxml2::XMLElement *importGroup = node->FirstChildElement(); importGroup; importGroup = importGroup->NextSiblingElement()) {
|
|
|
|
if (std::strcmp(importGroup->Name(), "Import") == 0 && importGroup->Attribute("Project")) {
|
|
|
|
std::string loadprj = importGroup->Attribute("Project");
|
|
|
|
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) {
|
|
|
|
itemDefinitionGroupList.push_back(ItemDefinitionGroup(node, additionalIncludeDirectories));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 12:41:25 +02:00
|
|
|
void ImportProject::importVcxproj(const std::string &filename, std::map<std::string, std::string> &variables, const std::string &additionalIncludeDirectories)
|
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;
|
|
|
|
tinyxml2::XMLError error = doc.LoadFile(filename.c_str());
|
|
|
|
if (error != 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()) {
|
|
|
|
if (std::strcmp(node->Name(), "ItemGroup") == 0) {
|
|
|
|
if (node->Attribute("Label") && std::strcmp(node->Attribute("Label"), "ProjectConfigurations") == 0) {
|
|
|
|
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) {
|
|
|
|
ProjectConfiguration p(cfg);
|
|
|
|
if (p.platform != ProjectConfiguration::Unknown)
|
|
|
|
projectConfigurationList.push_back(ProjectConfiguration(cfg));
|
|
|
|
}
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (const tinyxml2::XMLElement *e = node->FirstChildElement(); e; e = e->NextSiblingElement()) {
|
|
|
|
if (std::strcmp(e->Name(), "ClCompile") == 0)
|
|
|
|
compileList.push_back(e->Attribute("Include"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (std::strcmp(node->Name(), "ItemDefinitionGroup") == 0) {
|
2016-11-13 12:16:09 +01:00
|
|
|
itemDefinitionGroupList.push_back(ItemDefinitionGroup(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) {
|
|
|
|
if (node->Attribute("Label") && std::strcmp(node->Attribute("Label"), "PropertySheets") == 0) {
|
|
|
|
for (const tinyxml2::XMLElement *e = node->FirstChildElement(); e; e = e->NextSiblingElement()) {
|
|
|
|
if (std::strcmp(e->Name(), "Import") == 0) {
|
|
|
|
const char *Project = e->Attribute("Project");
|
|
|
|
if (Project)
|
2016-12-04 18:50:59 +01:00
|
|
|
loadVisualStudioProperties(Project, &variables, &includePath, additionalIncludeDirectories, itemDefinitionGroupList);
|
2016-11-20 22:44:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::list<std::string>::const_iterator c = compileList.begin(); c != compileList.end(); ++c) {
|
|
|
|
for (std::list<ProjectConfiguration>::const_iterator p = projectConfigurationList.begin(); p != projectConfigurationList.end(); ++p) {
|
2016-12-07 17:03:46 +01:00
|
|
|
FileSettings fs;
|
|
|
|
fs.filename = Path::simplifyPath(Path::getPathFromFilename(filename) + *c);
|
|
|
|
fs.cfg = p->name;
|
|
|
|
fs.defines = "_MSC_VER=1900;_WIN32=1";
|
2017-06-01 13:13:23 +02:00
|
|
|
if (p->platform == ProjectConfiguration::Win32)
|
2016-12-07 17:03:46 +01:00
|
|
|
fs.platformType = cppcheck::Platform::Win32W;
|
2017-06-01 13:13:23 +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";
|
|
|
|
}
|
|
|
|
if (useOfMfc)
|
|
|
|
fs.defines += ";__AFXWIN_H__";
|
|
|
|
std::string additionalIncludePaths;
|
2016-08-13 10:50:03 +02:00
|
|
|
for (std::list<ItemDefinitionGroup>::const_iterator i = itemDefinitionGroupList.begin(); i != itemDefinitionGroupList.end(); ++i) {
|
|
|
|
if (!i->conditionIsTrue(*p))
|
|
|
|
continue;
|
2016-12-07 17:03:46 +01:00
|
|
|
fs.defines += ';' + i->preprocessorDefinitions;
|
|
|
|
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);
|
|
|
|
fileSettings.push_back(fs);
|
2016-08-13 10:50:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|