Skip to content

Commit 9ce981c

Browse files
authored
Revert "fix: use both absolute and relative header paths in header matching (#362)" (#415)
1 parent 9b0c842 commit 9ce981c

File tree

6 files changed

+24
-286
lines changed

6 files changed

+24
-286
lines changed

.github/workflows/CI-unixish.yml

+1-16
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,7 @@ jobs:
3030
run: |
3131
sudo apt-get update
3232
sudo apt-get install libc++-18-dev
33-
34-
- name: Install missing software on macos
35-
if: contains(matrix.os, 'macos')
36-
run: |
37-
brew install python3
38-
39-
- name: Install missing Python packages
40-
run: |
41-
python3 -m pip config set global.break-system-packages true
42-
python3 -m pip install pip --upgrade
43-
python3 -m pip install pytest
44-
33+
4534
- name: make simplecpp
4635
run: make -j$(nproc)
4736

@@ -52,10 +41,6 @@ jobs:
5241
run: |
5342
make -j$(nproc) selfcheck
5443
55-
- name: integration test
56-
run: |
57-
python3 -m pytest integration_test.py
58-
5944
- name: Run CMake
6045
run: |
6146
cmake -S . -B cmake.output

.github/workflows/CI-windows.yml

+1-17
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,7 @@ jobs:
2626

2727
- name: Setup msbuild.exe
2828
uses: microsoft/setup-msbuild@v2
29-
30-
- name: Set up Python 3.13
31-
uses: actions/setup-python@v5
32-
with:
33-
python-version: '3.13'
34-
check-latest: true
35-
36-
- name: Install missing Python packages
37-
run: |
38-
python -m pip install pip --upgrade || exit /b !errorlevel!
39-
python -m pip install pytest || exit /b !errorlevel!
40-
29+
4130
- name: Run cmake
4231
if: matrix.os == 'windows-2019'
4332
run: |
@@ -59,9 +48,4 @@ jobs:
5948
- name: Selfcheck
6049
run: |
6150
.\${{ matrix.config }}\simplecpp.exe simplecpp.cpp -e || exit /b !errorlevel!
62-
63-
- name: integration test
64-
run: |
65-
set SIMPLECPP_EXE_PATH=.\${{ matrix.config }}\simplecpp.exe
66-
python -m pytest integration_test.py || exit /b !errorlevel!
6751

.gitignore

-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,3 @@ testrunner
3232
# CLion
3333
/.idea
3434
/cmake-build-*
35-
36-
# python
37-
__pycache__/

integration_test.py

-94
This file was deleted.

simplecpp.cpp

+22-99
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
#ifdef SIMPLECPP_WINDOWS
4444
#include <windows.h>
4545
#undef ERROR
46-
#else
47-
#include <unistd.h>
4846
#endif
4947

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

152-
static bool startsWith(const std::string &s, const std::string &p)
153-
{
154-
return (s.size() >= p.size()) && std::equal(p.begin(), p.end(), s.begin());
155-
}
156-
157150
static bool endsWith(const std::string &s, const std::string &e)
158151
{
159152
return (s.size() >= e.size()) && std::equal(e.rbegin(), e.rend(), s.rbegin());
@@ -2718,46 +2711,6 @@ static bool isCpp17OrLater(const simplecpp::DUI &dui)
27182711
return !std_ver.empty() && (std_ver >= "201703L");
27192712
}
27202713

2721-
2722-
static std::string currentDirectoryOSCalc() {
2723-
#ifdef SIMPLECPP_WINDOWS
2724-
TCHAR NPath[MAX_PATH];
2725-
GetCurrentDirectory(MAX_PATH, NPath);
2726-
return NPath;
2727-
#else
2728-
const std::size_t size = 1024;
2729-
char the_path[size];
2730-
getcwd(the_path, size);
2731-
return the_path;
2732-
#endif
2733-
}
2734-
2735-
static const std::string& currentDirectory() {
2736-
static const std::string curdir = simplecpp::simplifyPath(currentDirectoryOSCalc());
2737-
return curdir;
2738-
}
2739-
2740-
static std::string toAbsolutePath(const std::string& path) {
2741-
if (path.empty()) {
2742-
return path;// preserve error file path that is indicated by an empty string
2743-
}
2744-
if (!isAbsolutePath(path)) {
2745-
return currentDirectory() + "/" + path;
2746-
}
2747-
// otherwise
2748-
return path;
2749-
}
2750-
2751-
static std::pair<std::string, bool> extractRelativePathFromAbsolute(const std::string& absolutepath) {
2752-
static const std::string prefix = currentDirectory() + "/";
2753-
if (startsWith(absolutepath, prefix)) {
2754-
const std::size_t size = prefix.size();
2755-
return std::make_pair(absolutepath.substr(size, absolutepath.size() - size), true);
2756-
}
2757-
// otherwise
2758-
return std::make_pair("", false);
2759-
}
2760-
27612714
static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const std::string &sourcefile, const std::string &header, bool systemheader);
27622715
static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI &dui)
27632716
{
@@ -3176,12 +3129,9 @@ static std::string openHeader(std::ifstream &f, const std::string &path)
31763129

31773130
static std::string getRelativeFileName(const std::string &sourcefile, const std::string &header)
31783131
{
3179-
std::string path;
31803132
if (sourcefile.find_first_of("\\/") != std::string::npos)
3181-
path = sourcefile.substr(0, sourcefile.find_last_of("\\/") + 1U) + header;
3182-
else
3183-
path = header;
3184-
return simplecpp::simplifyPath(path);
3133+
return simplecpp::simplifyPath(sourcefile.substr(0, sourcefile.find_last_of("\\/") + 1U) + header);
3134+
return simplecpp::simplifyPath(header);
31853135
}
31863136

31873137
static std::string openHeaderRelative(std::ifstream &f, const std::string &sourcefile, const std::string &header)
@@ -3191,7 +3141,7 @@ static std::string openHeaderRelative(std::ifstream &f, const std::string &sourc
31913141

31923142
static std::string getIncludePathFileName(const std::string &includePath, const std::string &header)
31933143
{
3194-
std::string path = toAbsolutePath(includePath);
3144+
std::string path = includePath;
31953145
if (!path.empty() && path[path.size()-1U]!='/' && path[path.size()-1U]!='\\')
31963146
path += '/';
31973147
return path + header;
@@ -3200,9 +3150,9 @@ static std::string getIncludePathFileName(const std::string &includePath, const
32003150
static std::string openHeaderIncludePath(std::ifstream &f, const simplecpp::DUI &dui, const std::string &header)
32013151
{
32023152
for (std::list<std::string>::const_iterator it = dui.includePaths.begin(); it != dui.includePaths.end(); ++it) {
3203-
std::string path = openHeader(f, getIncludePathFileName(*it, header));
3204-
if (!path.empty())
3205-
return path;
3153+
std::string simplePath = openHeader(f, getIncludePathFileName(*it, header));
3154+
if (!simplePath.empty())
3155+
return simplePath;
32063156
}
32073157
return "";
32083158
}
@@ -3212,76 +3162,49 @@ static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const
32123162
if (isAbsolutePath(header))
32133163
return openHeader(f, header);
32143164

3165+
std::string ret;
3166+
32153167
if (systemheader) {
3216-
// always return absolute path for systemheaders
3217-
return toAbsolutePath(openHeaderIncludePath(f, dui, header));
3168+
ret = openHeaderIncludePath(f, dui, header);
3169+
return ret;
32183170
}
32193171

3220-
std::string ret;
3221-
32223172
ret = openHeaderRelative(f, sourcefile, header);
32233173
if (ret.empty())
3224-
return toAbsolutePath(openHeaderIncludePath(f, dui, header));// in a similar way to system headers
3174+
return openHeaderIncludePath(f, dui, header);
32253175
return ret;
32263176
}
32273177

3228-
static std::string findPathInMapBothRelativeAndAbsolute(const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string& path) {
3229-
// here there are two possibilities - either we match this from absolute path or from a relative one
3230-
if (filedata.find(path) != filedata.end()) {// try first to respect the exact match
3231-
return path;
3232-
}
3233-
// otherwise - try to use the normalize to the correct representation
3234-
if (isAbsolutePath(path)) {
3235-
const std::pair<std::string, bool> relativeExtractedResult = extractRelativePathFromAbsolute(path);
3236-
if (relativeExtractedResult.second) {
3237-
const std::string relativePath = relativeExtractedResult.first;
3238-
if (filedata.find(relativePath) != filedata.end()) {
3239-
return relativePath;
3240-
}
3241-
}
3242-
} else {
3243-
const std::string absolutePath = toAbsolutePath(path);
3244-
if (filedata.find(absolutePath) != filedata.end())
3245-
return absolutePath;
3246-
}
3247-
// otherwise
3248-
return "";
3249-
}
3250-
3251-
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)
3178+
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)
32523179
{
32533180
if (filedata.empty()) {
32543181
return "";
32553182
}
32563183
if (isAbsolutePath(header)) {
3257-
const std::string simplifiedHeaderPath = simplecpp::simplifyPath(header);
3258-
return (filedata.find(simplifiedHeaderPath) != filedata.end()) ? simplifiedHeaderPath : "";
3184+
return (filedata.find(header) != filedata.end()) ? simplecpp::simplifyPath(header) : "";
32593185
}
32603186

32613187
if (!systemheader) {
3262-
const std::string relativeOrAbsoluteFilename = getRelativeFileName(sourcefile, header);// unknown if absolute or relative, but always simplified
3263-
const std::string match = findPathInMapBothRelativeAndAbsolute(filedata, relativeOrAbsoluteFilename);
3264-
if (!match.empty()) {
3265-
return match;
3266-
}
3188+
const std::string relativeFilename = getRelativeFileName(sourcefile, header);
3189+
if (filedata.find(relativeFilename) != filedata.end())
3190+
return relativeFilename;
32673191
}
32683192

32693193
for (std::list<std::string>::const_iterator it = dui.includePaths.begin(); it != dui.includePaths.end(); ++it) {
3270-
const std::string match = findPathInMapBothRelativeAndAbsolute(filedata, simplecpp::simplifyPath(getIncludePathFileName(*it, header)));
3271-
if (!match.empty()) {
3272-
return match;
3273-
}
3194+
std::string s = simplecpp::simplifyPath(getIncludePathFileName(*it, header));
3195+
if (filedata.find(s) != filedata.end())
3196+
return s;
32743197
}
32753198

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

32793202
return "";
32803203
}
32813204

32823205
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)
32833206
{
3284-
return !getFileIdPath(filedata, sourcefile, header, dui, systemheader).empty();
3207+
return !getFileName(filedata, sourcefile, header, dui, systemheader).empty();
32853208
}
32863209

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

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

0 commit comments

Comments
 (0)