ImportProject; Try to use relative paths

This commit is contained in:
Daniel Marjamäki 2021-06-12 11:10:35 +02:00
parent ed05a5c3b3
commit 4a4808e0ff
3 changed files with 30 additions and 10 deletions

View File

@ -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<std::string, std::string, cppcheck::stricmp> 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<std::string> ImportProject::getVSConfigs()
{
return std::list<std::string> (mAllVSConfigs.begin(), mAllVSConfigs.end());
}
void ImportProject::setRelativePaths(const std::string &filename)
{
if (Path::isAbsolute(filename))
return;
const std::vector<std::string> basePaths{Path::getCurrentPath()};
for (auto &fs: fileSettings) {
fs.filename = Path::getRelativePath(fs.filename, basePaths);
for (auto &includePath: fs.includePaths)
includePath = Path::getRelativePath(includePath, basePaths);
}
}

View File

@ -113,6 +113,8 @@ private:
void importVcxproj(const std::string &filename, std::map<std::string, std::string, cppcheck::stricmp> &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<std::string> mAllVSConfigs;
};

View File

@ -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