From 4a4808e0ff9eaeaff1573d4827449f4f5b66fabf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 12 Jun 2021 11:10:35 +0200 Subject: [PATCH] ImportProject; Try to use relative paths --- lib/importproject.cpp | 20 +++++++++++++++++++- lib/importproject.h | 2 ++ test/cli/test-proj2.py | 18 +++++++++--------- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/importproject.cpp b/lib/importproject.cpp index e2c525de0..27743a906 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -199,19 +199,25 @@ ImportProject::Type ImportProject::import(const std::string &filename, Settings if (endsWith(filename, ".json", 5)) { importCompileCommands(fin); + setRelativePaths(filename); return ImportProject::Type::COMPILE_DB; } else if (endsWith(filename, ".sln", 4)) { importSln(fin, mPath, fileFilter); + setRelativePaths(filename); return ImportProject::Type::VS_SLN; } else if (endsWith(filename, ".vcxproj", 8)) { std::map variables; importVcxproj(filename, variables, emptyString, fileFilter); + setRelativePaths(filename); return ImportProject::Type::VS_VCXPROJ; } else if (endsWith(filename, ".bpr", 4)) { importBcb6Prj(filename); + setRelativePaths(filename); return ImportProject::Type::BORLAND; } else if (settings && endsWith(filename, ".cppcheck", 9)) { - return importCppcheckGuiProject(fin, settings) ? ImportProject::Type::CPPCHECK_GUI : ImportProject::Type::MISSING; + const bool success = importCppcheckGuiProject(fin, settings); + setRelativePaths(filename); + return success ? ImportProject::Type::CPPCHECK_GUI : ImportProject::Type::MISSING; } return ImportProject::Type::UNKNOWN; } @@ -1240,3 +1246,15 @@ std::list ImportProject::getVSConfigs() { return std::list (mAllVSConfigs.begin(), mAllVSConfigs.end()); } + +void ImportProject::setRelativePaths(const std::string &filename) +{ + if (Path::isAbsolute(filename)) + return; + const std::vector basePaths{Path::getCurrentPath()}; + for (auto &fs: fileSettings) { + fs.filename = Path::getRelativePath(fs.filename, basePaths); + for (auto &includePath: fs.includePaths) + includePath = Path::getRelativePath(includePath, basePaths); + } +} diff --git a/lib/importproject.h b/lib/importproject.h index 7f0b99779..ad5caefcf 100644 --- a/lib/importproject.h +++ b/lib/importproject.h @@ -113,6 +113,8 @@ private: void importVcxproj(const std::string &filename, std::map &variables, const std::string &additionalIncludeDirectories, const std::string &fileFilter); void importBcb6Prj(const std::string &projectFilename); + void setRelativePaths(const std::string &filename); + std::string mPath; std::set mAllVSConfigs; }; diff --git a/test/cli/test-proj2.py b/test/cli/test-proj2.py index e49452655..edfa9495f 100644 --- a/test/cli/test-proj2.py +++ b/test/cli/test-proj2.py @@ -44,8 +44,8 @@ def test_file_filter(): def test_local_path(): create_compile_commands() ret, stdout, stderr = cppcheck_local(['--project=compile_commands.json']) - file1 = os.path.realpath('proj2/a/a.c') - file2 = os.path.realpath('proj2/b/b.c') + file1 = 'a/a.c' + file2 = 'b/b.c' assert ret == 0, stdout assert stdout.find('Checking %s ...' % file1) >= 0 assert stdout.find('Checking %s ...' % file2) >= 0 @@ -65,8 +65,8 @@ def test_local_path_maxconfigs(): def test_relative_path(): create_compile_commands() ret, stdout, stderr = cppcheck(['--project=proj2/' + COMPILE_COMMANDS_JSON]) - file1 = os.path.realpath('proj2/a/a.c') - file2 = os.path.realpath('proj2/b/b.c') + file1 = 'proj2/a/a.c' + file2 = 'proj2/b/b.c' assert ret == 0, stdout assert stdout.find('Checking %s ...' % file1) >= 0 assert stdout.find('Checking %s ...' % file2) >= 0 @@ -83,21 +83,21 @@ def test_absolute_path(): def test_gui_project_loads_compile_commands_1(): create_compile_commands() ret, stdout, stderr = cppcheck(['--project=proj2/proj2.cppcheck']) - file1 = os.path.realpath('proj2/a/a.c') - file2 = os.path.realpath('proj2/b/b.c') + file1 = 'proj2/a/a.c' + file2 = 'proj2/b/b.c' assert ret == 0, stdout assert stdout.find('Checking %s ...' % file1) >= 0 assert stdout.find('Checking %s ...' % file2) >= 0 def test_gui_project_loads_compile_commands_2(): create_compile_commands() - exclude_path_1 = realpath('proj2/b') + exclude_path_1 = 'proj2/b' create_gui_project_file('proj2/test.cppcheck', import_project='compile_commands.json', exclude_paths=[exclude_path_1]) ret, stdout, stderr = cppcheck(['--project=proj2/test.cppcheck']) - file1 = os.path.realpath('proj2/a/a.c') - file2 = os.path.realpath('proj2/b/b.c') # Excluded by test.cppcheck + file1 = 'proj2/a/a.c' + file2 = 'proj2/b/b.c' # Excluded by test.cppcheck assert ret == 0, stdout assert stdout.find('Checking %s ...' % file1) >= 0 assert stdout.find('Checking %s ...' % file2) < 0