From c9172b169a7c9686cb8603e9314102428fea8dc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 11 Apr 2019 21:02:49 +0200 Subject: [PATCH] Improved handling of paths when importing cppcheck gui project --- lib/importproject.cpp | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/importproject.cpp b/lib/importproject.cpp index 4e3c8b5b9..f3d206a3f 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -908,7 +908,14 @@ void ImportProject::importBcb6Prj(const std::string &projectFilename) } } -static std::list readXmlStringList(const tinyxml2::XMLElement *node, const char name[], const char attribute[]) +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 readXmlStringList(const tinyxml2::XMLElement *node, const std::string &path, const char name[], const char attribute[]) { std::list ret; for (const tinyxml2::XMLElement *child = node->FirstChildElement(); child; child = child->NextSiblingElement()) { @@ -916,7 +923,7 @@ static std::list readXmlStringList(const tinyxml2::XMLElement *node continue; const char *attr = attribute ? child->Attribute(attribute) : child->GetText(); if (attr) - ret.push_back(attr); + ret.push_back(joinRelativePath(path, attr)); } return ret; } @@ -994,40 +1001,42 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *setti (void)ProjectFileVersion; (void)ProjectVersionAttrib; + const std::string &path = mPath; + std::list paths; std::list suppressions; Settings temp; for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) { if (strcmp(node->Name(), RootPathName) == 0 && node->Attribute(RootPathNameAttrib)) - temp.basePaths.push_back(node->Attribute(RootPathNameAttrib)); + temp.basePaths.push_back(joinRelativePath(path, node->Attribute(RootPathNameAttrib))); else if (strcmp(node->Name(), BuildDirElementName) == 0) - temp.buildDir = node->GetText() ? node->GetText() : ""; + temp.buildDir = joinRelativePath(path, node->GetText() ? node->GetText() : ""); else if (strcmp(node->Name(), IncludeDirElementName) == 0) - temp.includePaths = readXmlStringList(node, DirElementName, DirNameAttrib); + temp.includePaths = readXmlStringList(node, path, DirElementName, DirNameAttrib); else if (strcmp(node->Name(), DefinesElementName) == 0) - temp.userDefines = join(readXmlStringList(node, DefineName, DefineNameAttrib), ";"); + temp.userDefines = join(readXmlStringList(node, "", DefineName, DefineNameAttrib), ";"); else if (strcmp(node->Name(), UndefinesElementName) == 0) { - for (const std::string &u : readXmlStringList(node, UndefineName, nullptr)) + for (const std::string &u : readXmlStringList(node, "", UndefineName, nullptr)) temp.userUndefs.insert(u); } else if (strcmp(node->Name(), ImportProjectElementName) == 0) - guiProject.projectFile = node->GetText() ? node->GetText() : ""; + guiProject.projectFile = path + (node->GetText() ? node->GetText() : ""); else if (strcmp(node->Name(), PathsElementName) == 0) - paths = readXmlStringList(node, PathName, PathNameAttrib); + paths = readXmlStringList(node, path, PathName, PathNameAttrib); else if (strcmp(node->Name(), ExcludeElementName) == 0) - guiProject.excludedPaths = readXmlStringList(node, ExcludePathName, ExcludePathNameAttrib); + guiProject.excludedPaths = readXmlStringList(node, "", ExcludePathName, ExcludePathNameAttrib); else if (strcmp(node->Name(), IgnoreElementName) == 0) - guiProject.excludedPaths = readXmlStringList(node, IgnorePathName, IgnorePathNameAttrib); + guiProject.excludedPaths = readXmlStringList(node, "", IgnorePathName, IgnorePathNameAttrib); else if (strcmp(node->Name(), LibrariesElementName) == 0) - guiProject.libraries = readXmlStringList(node, LibraryElementName, nullptr); + guiProject.libraries = readXmlStringList(node, "", LibraryElementName, nullptr); else if (strcmp(node->Name(), SuppressionsElementName) == 0) - suppressions = readXmlStringList(node, SuppressionElementName, nullptr); + suppressions = readXmlStringList(node, "", SuppressionElementName, nullptr); else if (strcmp(node->Name(), PlatformElementName) == 0) guiProject.platform = node->GetText(); else if (strcmp(node->Name(), AnalyzeAllVsConfigsElementName) == 0) ; // FIXME: Write some warning else if (strcmp(node->Name(), AddonsElementName) == 0) - temp.addons = readXmlStringList(node, AddonElementName, nullptr); + temp.addons = readXmlStringList(node, "", AddonElementName, nullptr); else if (strcmp(node->Name(), TagsElementName) == 0) node->Attribute(TagElementName); // FIXME: Write some warning else if (strcmp(node->Name(), ToolsElementName) == 0)