Skip to content

Revert "fix: use both absolute and relative header paths in header matching (#362)" #415

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 1 commit into from
Feb 12, 2025
Merged
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
17 changes: 1 addition & 16 deletions .github/workflows/CI-unixish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,7 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install libc++-18-dev

- name: Install missing software on macos
if: contains(matrix.os, 'macos')
run: |
brew install python3

- name: Install missing Python packages
run: |
python3 -m pip config set global.break-system-packages true
python3 -m pip install pip --upgrade
python3 -m pip install pytest


- name: make simplecpp
run: make -j$(nproc)

Expand All @@ -52,10 +41,6 @@ jobs:
run: |
make -j$(nproc) selfcheck

- name: integration test
run: |
python3 -m pytest integration_test.py

- name: Run CMake
run: |
cmake -S . -B cmake.output
Expand Down
18 changes: 1 addition & 17 deletions .github/workflows/CI-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,7 @@ jobs:

- name: Setup msbuild.exe
uses: microsoft/setup-msbuild@v2

- name: Set up Python 3.13
uses: actions/setup-python@v5
with:
python-version: '3.13'
check-latest: true

- name: Install missing Python packages
run: |
python -m pip install pip --upgrade || exit /b !errorlevel!
python -m pip install pytest || exit /b !errorlevel!


- name: Run cmake
if: matrix.os == 'windows-2019'
run: |
Expand All @@ -59,9 +48,4 @@ jobs:
- name: Selfcheck
run: |
.\${{ matrix.config }}\simplecpp.exe simplecpp.cpp -e || exit /b !errorlevel!

- name: integration test
run: |
set SIMPLECPP_EXE_PATH=.\${{ matrix.config }}\simplecpp.exe
python -m pytest integration_test.py || exit /b !errorlevel!

3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,3 @@ testrunner
# CLion
/.idea
/cmake-build-*

# python
__pycache__/
94 changes: 0 additions & 94 deletions integration_test.py

This file was deleted.

121 changes: 22 additions & 99 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
#ifdef SIMPLECPP_WINDOWS
#include <windows.h>
#undef ERROR
#else
#include <unistd.h>
#endif

#if __cplusplus >= 201103L
Expand Down Expand Up @@ -149,11 +147,6 @@ static unsigned long long stringToULL(const std::string &s)
return ret;
}

static bool startsWith(const std::string &s, const std::string &p)
{
return (s.size() >= p.size()) && std::equal(p.begin(), p.end(), s.begin());
}

static bool endsWith(const std::string &s, const std::string &e)
{
return (s.size() >= e.size()) && std::equal(e.rbegin(), e.rend(), s.rbegin());
Expand Down Expand Up @@ -2718,46 +2711,6 @@ static bool isCpp17OrLater(const simplecpp::DUI &dui)
return !std_ver.empty() && (std_ver >= "201703L");
}


static std::string currentDirectoryOSCalc() {
#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 const std::string& currentDirectory() {
static const std::string curdir = simplecpp::simplifyPath(currentDirectoryOSCalc());
return curdir;
}

static std::string toAbsolutePath(const std::string& path) {
if (path.empty()) {
return path;// preserve error file path that is indicated by an empty string
}
if (!isAbsolutePath(path)) {
return currentDirectory() + "/" + path;
}
// otherwise
return path;
}

static std::pair<std::string, bool> extractRelativePathFromAbsolute(const std::string& absolutepath) {
static const std::string prefix = currentDirectory() + "/";
if (startsWith(absolutepath, prefix)) {
const std::size_t size = prefix.size();
return std::make_pair(absolutepath.substr(size, absolutepath.size() - size), true);
}
// otherwise
return std::make_pair("", false);
}

static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const std::string &sourcefile, const std::string &header, bool systemheader);
static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI &dui)
{
Expand Down Expand Up @@ -3176,12 +3129,9 @@ static std::string openHeader(std::ifstream &f, const std::string &path)

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

static std::string openHeaderRelative(std::ifstream &f, const std::string &sourcefile, const std::string &header)
Expand All @@ -3191,7 +3141,7 @@ 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 = toAbsolutePath(includePath);
std::string path = includePath;
if (!path.empty() && path[path.size()-1U]!='/' && path[path.size()-1U]!='\\')
path += '/';
return path + header;
Expand All @@ -3200,9 +3150,9 @@ static std::string getIncludePathFileName(const std::string &includePath, const
static std::string openHeaderIncludePath(std::ifstream &f, const simplecpp::DUI &dui, const std::string &header)
{
for (std::list<std::string>::const_iterator it = dui.includePaths.begin(); it != dui.includePaths.end(); ++it) {
std::string path = openHeader(f, getIncludePathFileName(*it, header));
if (!path.empty())
return path;
std::string simplePath = openHeader(f, getIncludePathFileName(*it, header));
if (!simplePath.empty())
return simplePath;
}
return "";
}
Expand All @@ -3212,76 +3162,49 @@ static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const
if (isAbsolutePath(header))
return openHeader(f, header);

std::string ret;

if (systemheader) {
// always return absolute path for systemheaders
return toAbsolutePath(openHeaderIncludePath(f, dui, header));
ret = openHeaderIncludePath(f, dui, header);
return ret;
}

std::string ret;

ret = openHeaderRelative(f, sourcefile, header);
if (ret.empty())
return toAbsolutePath(openHeaderIncludePath(f, dui, header));// in a similar way to system headers
return openHeaderIncludePath(f, dui, header);
return ret;
}

static std::string findPathInMapBothRelativeAndAbsolute(const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string& path) {
// here there are two possibilities - either we match this from absolute path or from a relative one
if (filedata.find(path) != filedata.end()) {// try first to respect the exact match
return path;
}
// otherwise - try to use the normalize to the correct representation
if (isAbsolutePath(path)) {
const std::pair<std::string, bool> relativeExtractedResult = extractRelativePathFromAbsolute(path);
if (relativeExtractedResult.second) {
const std::string relativePath = relativeExtractedResult.first;
if (filedata.find(relativePath) != filedata.end()) {
return relativePath;
}
}
} else {
const std::string absolutePath = toAbsolutePath(path);
if (filedata.find(absolutePath) != filedata.end())
return absolutePath;
}
// otherwise
return "";
}

static std::string getFileIdPath(const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string &sourcefile, const std::string &header, const simplecpp::DUI &dui, bool systemheader)
static std::string getFileName(const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string &sourcefile, const std::string &header, const simplecpp::DUI &dui, bool systemheader)
{
if (filedata.empty()) {
return "";
}
if (isAbsolutePath(header)) {
const std::string simplifiedHeaderPath = simplecpp::simplifyPath(header);
return (filedata.find(simplifiedHeaderPath) != filedata.end()) ? simplifiedHeaderPath : "";
return (filedata.find(header) != filedata.end()) ? simplecpp::simplifyPath(header) : "";
}

if (!systemheader) {
const std::string relativeOrAbsoluteFilename = getRelativeFileName(sourcefile, header);// unknown if absolute or relative, but always simplified
const std::string match = findPathInMapBothRelativeAndAbsolute(filedata, relativeOrAbsoluteFilename);
if (!match.empty()) {
return match;
}
const std::string relativeFilename = getRelativeFileName(sourcefile, header);
if (filedata.find(relativeFilename) != filedata.end())
return relativeFilename;
}

for (std::list<std::string>::const_iterator it = dui.includePaths.begin(); it != dui.includePaths.end(); ++it) {
const std::string match = findPathInMapBothRelativeAndAbsolute(filedata, simplecpp::simplifyPath(getIncludePathFileName(*it, header)));
if (!match.empty()) {
return match;
}
std::string s = simplecpp::simplifyPath(getIncludePathFileName(*it, header));
if (filedata.find(s) != filedata.end())
return s;
}

if (systemheader && filedata.find(header) != filedata.end())
return header;// system header that its file wasn't found in the included paths but alreasy in the filedata - return this as is
return header;

return "";
}

static bool hasFile(const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string &sourcefile, const std::string &header, const simplecpp::DUI &dui, bool systemheader)
{
return !getFileIdPath(filedata, sourcefile, header, dui, systemheader).empty();
return !getFileName(filedata, sourcefile, header, dui, systemheader).empty();
}

std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::TokenList &rawtokens, std::vector<std::string> &filenames, const simplecpp::DUI &dui, simplecpp::OutputList *outputList)
Expand Down Expand Up @@ -3637,7 +3560,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL

const bool systemheader = (inctok->str()[0] == '<');
const std::string header(realFilename(inctok->str().substr(1U, inctok->str().size() - 2U)));
std::string header2 = getFileIdPath(filedata, rawtok->location.file(), header, dui, systemheader);
std::string header2 = getFileName(filedata, rawtok->location.file(), header, dui, systemheader);
if (header2.empty()) {
// try to load file..
std::ifstream f;
Expand Down
Loading