Skip to content

fix: use both absolute and relative header paths in header matching #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jan 1, 2025
Merged
Changes from 1 commit
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
Next Next commit
fix: always use absolute (and simplified) header path whenever possible
This fix an issue of importing the same header by different addresses.
The issue is trickier than seems, because if a user used the include dir flag `-I...` with an absolute path, then every matched header was detected to be absolute, but if the import was a relative import to the source file and the source file isn't with an absolute path, than the header is identified by the combined relative path.
See https://sourceforge.net/p/cppcheck/discussion/general/thread/c01f80556b/
  • Loading branch information
Tal500 authored Aug 6, 2024
commit 907c44354a9bcf2509bc073ec30d8fd84e0b7026
29 changes: 26 additions & 3 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#ifdef SIMPLECPP_WINDOWS
#include <windows.h>
#undef ERROR
#else
#include <unistd.h>
#endif

#if __cplusplus >= 201103L
Expand Down Expand Up @@ -3087,11 +3089,29 @@ static std::string openHeader(std::ifstream &f, const std::string &path)
return "";
}

static std::string currentDirectory() {
#ifdef SIMPLECPP_WINDOWS
TCHAR NPath[MAX_PATH];
GetCurrentDirectory(MAX_PATH, NPath);
return NPath;
#else
const std::size_t size = 1024;
char the_path[size];
getcwd(the_path, size);
return the_path;
#endif
}

static std::string getRelativeFileName(const std::string &sourcefile, const std::string &header)
{
std::string path;
if (sourcefile.find_first_of("\\/") != std::string::npos)
return simplecpp::simplifyPath(sourcefile.substr(0, sourcefile.find_last_of("\\/") + 1U) + header);
return simplecpp::simplifyPath(header);
path = sourcefile.substr(0, sourcefile.find_last_of("\\/") + 1U) + header;
else
path = header;
if (!isAbsolutePath(path))
path = currentDirectory() + "/" + path;
return simplecpp::simplifyPath(path);
}

static std::string openHeaderRelative(std::ifstream &f, const std::string &sourcefile, const std::string &header)
Expand All @@ -3102,6 +3122,8 @@ static std::string openHeaderRelative(std::ifstream &f, const std::string &sourc
static std::string getIncludePathFileName(const std::string &includePath, const std::string &header)
{
std::string path = includePath;
if (!isAbsolutePath(includePath))
path = currentDirectory() + "/" + path;
if (!path.empty() && path[path.size()-1U]!='/' && path[path.size()-1U]!='\\')
path += '/';
return path + header;
Expand Down Expand Up @@ -3141,7 +3163,8 @@ static std::string getFileName(const std::map<std::string, simplecpp::TokenList
return "";
}
if (isAbsolutePath(header)) {
return (filedata.find(header) != filedata.end()) ? simplecpp::simplifyPath(header) : "";
const std::string simplifiedHeaderPath = simplecpp::simplifyPath(header);
return (filedata.find(simplifiedHeaderPath) != filedata.end()) ? simplifiedHeaderPath : "";
}

if (!systemheader) {
Expand Down
Loading