Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions lib/addoninfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,36 @@ static std::string getFullPath(const std::string &fileName, const std::string &e
return "";

const std::string exepath = Path::getPathFromFilename(exename);
if (debug)
std::cout << "looking for addon '" << (exepath + fileName) << "'" << std::endl;
if (Path::isFile(exepath + fileName))
return exepath + fileName;
if (debug)
std::cout << "looking for addon '" << (exepath + "addons/" + fileName) << "'" << std::endl;
if (Path::isFile(exepath + "addons/" + fileName))
return exepath + "addons/" + fileName;
{
std::string p = Path::join(exepath, fileName);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p can be const

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It cannot because of NRVO.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it can - but it shouldn't.

if (debug)
std::cout << "looking for addon '" << p << "'" << std::endl;
if (Path::isFile(p))
return p;
}
{
std::string p = Path::join(exepath, "addons", fileName);
if (debug)
std::cout << "looking for addon '" << p << "'" << std::endl;
if (Path::isFile(p))
return p;
}

#ifdef FILESDIR
if (debug)
std::cout << "looking for addon '" << (FILESDIR + ("/" + fileName)) << "'" << std::endl;
if (Path::isFile(FILESDIR + ("/" + fileName)))
return FILESDIR + ("/" + fileName);
if (debug)
std::cout << "looking for addon '" << (FILESDIR + ("/addons/" + fileName)) << "'" << std::endl;
if (Path::isFile(FILESDIR + ("/addons/" + fileName)))
return FILESDIR + ("/addons/" + fileName);
{
std::string p = Path::join(FILESDIR, fileName);
if (debug)
std::cout << "looking for addon '" << p << "'" << std::endl;
if (Path::isFile(p))
return p;
}
{
std::string p = Path::join(FILESDIR, "addons", fileName);
if (debug)
std::cout << "looking for addon '" << p << "'" << std::endl;
if (Path::isFile(p))
return p;
}
#endif
return "";
}
Expand Down
5 changes: 5 additions & 0 deletions lib/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,8 @@ std::string Path::join(std::string path1, std::string path2)
return path2;
return ((path1.back() == '/') ? path1 : (path1 + "/")) + path2;
}

std::string Path::join(std::string path1, std::string path2, std::string path3)
{
return Path::join(Path::join(std::move(path1), std::move(path2)), std::move(path3));
}
7 changes: 7 additions & 0 deletions lib/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ class CPPCHECKLIB Path {
* @return the joined path with normalized slashes
*/
static std::string join(std::string path1, std::string path2);

/**
* @brief join 3 paths with '/' separators
* if path2 is an absolute path path1 will be dismissed.
* @return the joined path with normalized slashes
*/
static std::string join(std::string path1, std::string path2, std::string path3);
};

/// @}
Expand Down
36 changes: 27 additions & 9 deletions test/cli/lookup_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,12 +644,14 @@ def test_addon_lookup(tmpdir):
exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=misra', test_file])
exepath = os.path.dirname(exe)
exepath_sep = exepath + os.path.sep
if sys.platform == 'win32':
exepath_sep = exepath_sep.replace('\\', '/')
assert exitcode == 0, stdout if stdout else stderr
lines = stdout.splitlines()
assert lines == [
"looking for addon 'misra.py'",
"looking for addon '{}misra.py'".format(exepath_sep),
"looking for addon '{}addons/misra.py'".format(exepath_sep), # TODO: mixed separators
"looking for addon '{}addons/misra.py'".format(exepath_sep),
'Checking {} ...'.format(test_file)
]

Expand All @@ -662,12 +664,14 @@ def test_addon_lookup_ext(tmpdir):
exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=misra.py', test_file])
exepath = os.path.dirname(exe)
exepath_sep = exepath + os.path.sep
if sys.platform == 'win32':
exepath_sep = exepath_sep.replace('\\', '/')
assert exitcode == 0, stdout if stdout else stderr
lines = stdout.splitlines()
assert lines == [
"looking for addon 'misra.py'",
"looking for addon '{}misra.py'".format(exepath_sep),
"looking for addon '{}addons/misra.py'".format(exepath_sep), # TODO: mixed separators
"looking for addon '{}addons/misra.py'".format(exepath_sep),
'Checking {} ...'.format(test_file)
]

Expand All @@ -680,12 +684,14 @@ def test_addon_lookup_notfound(tmpdir):
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=none', test_file])
exepath = os.path.dirname(exe)
exepath_sep = exepath + os.path.sep
if sys.platform == 'win32':
exepath_sep = exepath_sep.replace('\\', '/')
assert exitcode == 1, stdout
lines = stdout.splitlines()
assert lines == [
"looking for addon 'none.py'",
"looking for addon '{}none.py'".format(exepath_sep),
"looking for addon '{}addons/none.py'".format(exepath_sep), # TODO: mixed separators
"looking for addon '{}addons/none.py'".format(exepath_sep),
'Did not find addon none.py'
]

Expand All @@ -696,13 +702,15 @@ def test_addon_lookup_notfound_project(tmpdir): # #13940 / #13941
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=none', '--project={}'.format(project_file)])
exepath = os.path.dirname(exe)
exepath_sep = exepath + os.path.sep
if sys.platform == 'win32':
exepath_sep = exepath_sep.replace('\\', '/')
assert exitcode == 1, stdout
lines = stdout.splitlines()
assert lines == [
# TODO: needs to look relative to the project file first
"looking for addon 'none.py'",
"looking for addon '{}none.py'".format(exepath_sep),
"looking for addon '{}addons/none.py'".format(exepath_sep), # TODO: mixed separators
"looking for addon '{}addons/none.py'".format(exepath_sep),
'Did not find addon none.py'
]

Expand All @@ -713,12 +721,14 @@ def test_addon_lookup_notfound_compdb(tmpdir):
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=none', '--project={}'.format(compdb_file)])
exepath = os.path.dirname(exe)
exepath_sep = exepath + os.path.sep
if sys.platform == 'win32':
exepath_sep = exepath_sep.replace('\\', '/')
assert exitcode == 1, stdout
lines = stdout.splitlines()
assert lines == [
"looking for addon 'none.py'",
"looking for addon '{}none.py'".format(exepath_sep),
"looking for addon '{}addons/none.py'".format(exepath_sep), # TODO: mixed separators
"looking for addon '{}addons/none.py'".format(exepath_sep),
'Did not find addon none.py'
]

Expand All @@ -731,12 +741,14 @@ def test_addon_lookup_ext_notfound(tmpdir):
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=none.py', test_file])
exepath = os.path.dirname(exe)
exepath_sep = exepath + os.path.sep
if sys.platform == 'win32':
exepath_sep = exepath_sep.replace('\\', '/')
assert exitcode == 1, stdout
lines = stdout.splitlines()
assert lines == [
"looking for addon 'none.py'",
"looking for addon '{}none.py'".format(exepath_sep),
"looking for addon '{}addons/none.py'".format(exepath_sep), # TODO: mixed separators
"looking for addon '{}addons/none.py'".format(exepath_sep),
'Did not find addon none.py'
]

Expand All @@ -749,12 +761,14 @@ def test_addon_lookup_relative_notfound(tmpdir):
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=addon/misra.py', test_file])
exepath = os.path.dirname(exe)
exepath_sep = exepath + os.path.sep
if sys.platform == 'win32':
exepath_sep = exepath_sep.replace('\\', '/')
assert exitcode == 1, stdout
lines = stdout.splitlines()
assert lines == [
"looking for addon 'addon/misra.py'",
"looking for addon '{}addon/misra.py'".format(exepath_sep),
"looking for addon '{}addons/addon/misra.py'".format(exepath_sep), # TODO: mixed separators
"looking for addon '{}addons/addon/misra.py'".format(exepath_sep),
'Did not find addon addon/misra.py'
]

Expand All @@ -767,12 +781,14 @@ def test_addon_lookup_relative_noext_notfound(tmpdir):
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=addon/misra', test_file])
exepath = os.path.dirname(exe)
exepath_sep = exepath + os.path.sep
if sys.platform == 'win32':
exepath_sep = exepath_sep.replace('\\', '/')
assert exitcode == 1, stdout
lines = stdout.splitlines()
assert lines == [
"looking for addon 'addon/misra.py'",
"looking for addon '{}addon/misra.py'".format(exepath_sep),
"looking for addon '{}addons/addon/misra.py'".format(exepath_sep), # TODO: mixed separators
"looking for addon '{}addons/addon/misra.py'".format(exepath_sep),
'Did not find addon addon/misra.py'
]

Expand Down Expand Up @@ -825,12 +841,14 @@ def test_addon_lookup_nofile(tmpdir):
exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=misra', test_file])
exepath = os.path.dirname(exe)
exepath_sep = exepath + os.path.sep
if sys.platform == 'win32':
exepath_sep = exepath_sep.replace('\\', '/')
assert exitcode == 0, stdout if stdout else stderr
lines = stdout.splitlines()
assert lines == [
"looking for addon 'misra.py'",
"looking for addon '{}misra.py'".format(exepath_sep),
"looking for addon '{}addons/misra.py'".format(exepath_sep), # TODO: mixed separators
"looking for addon '{}addons/misra.py'".format(exepath_sep),
'Checking {} ...'.format(test_file)
]

Expand Down
2 changes: 2 additions & 0 deletions test/testpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ class TestPath : public TestFixture {
//ASSERT_EQUALS("", Path::join("S:/a", "S:/b"));
//ASSERT_EQUALS("", Path::join("S:/a", "S:\\b"));
//ASSERT_EQUALS("", Path::join("S:/a", "/b"));

ASSERT_EQUALS("a/b/c", Path::join("a", "b", "c"));
}

void isDirectory() const {
Expand Down
Loading