Skip to content

Commit 1155084

Browse files
author
Tal Hadad
committed
fix: preserve the absolutness/relativeness of the included file.
Note: if relative, it will be normalized always relatively to the current directory Note: still avoid cases of double header detection (relative vs. absolute)
1 parent 0775731 commit 1155084

1 file changed

Lines changed: 28 additions & 20 deletions

File tree

simplecpp.cpp

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3145,11 +3145,11 @@ static std::string openHeader(std::ifstream &f, const std::string &path)
31453145
return "";
31463146
}
31473147

3148-
static std::string getRelativeFileName(const std::string &sourcefile, const std::string &header)
3148+
static std::string getRelativeFileName(const std::string &baseFile, const std::string &header)
31493149
{
31503150
std::string path;
3151-
if (sourcefile.find_first_of("\\/") != std::string::npos)
3152-
path = sourcefile.substr(0, sourcefile.find_last_of("\\/") + 1U) + header;
3151+
if (baseFile.find_first_of("\\/") != std::string::npos)
3152+
path = baseFile.substr(0, baseFile.find_last_of("\\/") + 1U) + header;
31533153
else
31543154
path = header;
31553155
return simplecpp::simplifyPath(path);
@@ -3160,12 +3160,22 @@ static std::string openHeaderRelative(std::ifstream &f, const std::string &sourc
31603160
return openHeader(f, getRelativeFileName(sourcefile, header));
31613161
}
31623162

3163+
// returns the simplified header path:
3164+
// * If the header path is absolute, returns it in absolute path
3165+
// * Otherwise, returns it in relative path with respect to the current directory
31633166
static std::string getIncludePathFileName(const std::string &includePath, const std::string &header)
31643167
{
3165-
std::string path = toAbsolutePath(includePath);
3166-
if (!path.empty() && path[path.size()-1U]!='/' && path[path.size()-1U]!='\\')
3167-
path += '/';
3168-
return path + header;
3168+
std::string simplifiedHeader = simplecpp::simplifyPath(header);
3169+
3170+
if (isAbsolutePath(simplifiedHeader)) {
3171+
return simplifiedHeader;
3172+
}
3173+
3174+
std::string basePath = toAbsolutePath(includePath);
3175+
if (!basePath.empty() && basePath[basePath.size()-1U]!='/' && basePath[basePath.size()-1U]!='\\')
3176+
basePath += '/';
3177+
std::string absolutesimplifiedHeaderPath = basePath + simplifiedHeader;
3178+
return extractRelativePathFromAbsolute(absolutesimplifiedHeaderPath).first;
31693179
}
31703180

31713181
static std::string openHeaderIncludePath(std::ifstream &f, const simplecpp::DUI &dui, const std::string &header)
@@ -3183,17 +3193,16 @@ static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const
31833193
if (isAbsolutePath(header))
31843194
return openHeader(f, header);
31853195

3186-
if (systemheader) {
3187-
// always return absolute path for systemheaders
3188-
return toAbsolutePath(openHeaderIncludePath(f, dui, header));
3196+
// prefer first to search the header relatively to source file if found, when not a system header
3197+
if (!systemheader) {
3198+
std::string relativeHeader = openHeaderRelative(f, sourcefile, header);
3199+
if (!relativeHeader.empty()) {
3200+
return relativeHeader;
3201+
}
31893202
}
31903203

3191-
std::string ret;
3192-
3193-
ret = openHeaderRelative(f, sourcefile, header);
3194-
if (ret.empty())
3195-
return toAbsolutePath(openHeaderIncludePath(f, dui, header));// in a similar way to system headers
3196-
return ret;
3204+
// search the header on the include paths (provided by the flags "-I...")
3205+
return openHeaderIncludePath(f, dui, header);
31973206
}
31983207

31993208
static std::string findPathInMapBothRelativeAndAbsolute(const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string& path) {
@@ -3235,18 +3244,17 @@ static std::string getFileIdPath(const std::map<std::string, simplecpp::TokenLis
32353244
if (!match.empty()) {
32363245
return match;
32373246
}
3247+
} else if (filedata.find(header) != filedata.end()) {
3248+
return header;// system header that its file is already in the filedata - return that as is
32383249
}
32393250

32403251
for (std::list<std::string>::const_iterator it = dui.includePaths.begin(); it != dui.includePaths.end(); ++it) {
3241-
const std::string match = findPathInMapBothRelativeAndAbsolute(filedata, simplecpp::simplifyPath(getIncludePathFileName(*it, header)));
3252+
const std::string match = findPathInMapBothRelativeAndAbsolute(filedata, getIncludePathFileName(*it, header));
32423253
if (!match.empty()) {
32433254
return match;
32443255
}
32453256
}
32463257

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

0 commit comments

Comments
 (0)