Skip to content

Commit 75feeb5

Browse files
committed
fix hang when header includes itself
1 parent 11a9809 commit 75feeb5

1 file changed

Lines changed: 24 additions & 33 deletions

File tree

simplecpp.cpp

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,30 +2363,30 @@ static std::string _openHeader(std::ifstream &f, const std::string &path)
23632363
#endif
23642364
}
23652365

2366+
static std::string getRelativeFileName(const std::string &sourcefile, const std::string &header)
2367+
{
2368+
if (sourcefile.find_first_of("\\/") != std::string::npos)
2369+
return simplecpp::simplifyPath(sourcefile.substr(0, sourcefile.find_last_of("\\/") + 1U) + header);
2370+
return simplecpp::simplifyPath(header);
2371+
}
2372+
23662373
static std::string openHeaderRelative(std::ifstream &f, const std::string &sourcefile, const std::string &header)
23672374
{
2368-
if (sourcefile.find_first_of("\\/") != std::string::npos) {
2369-
const std::string s = sourcefile.substr(0, sourcefile.find_last_of("\\/") + 1U) + header;
2370-
std::string simplePath = _openHeader(f, s);
2371-
if (!simplePath.empty())
2372-
return simplePath;
2373-
} else {
2374-
std::string simplePath = _openHeader(f, header);
2375-
if (!simplePath.empty())
2376-
return simplePath;
2377-
}
2378-
return "";
2375+
return _openHeader(f, getRelativeFileName(sourcefile, header));
2376+
}
2377+
2378+
static std::string getIncludePathFileName(const std::string &includePath, const std::string &header)
2379+
{
2380+
std::string path = includePath;
2381+
if (!path.empty() && path[path.size()-1U]!='/' && path[path.size()-1U]!='\\')
2382+
path += '/';
2383+
return path + header;
23792384
}
23802385

23812386
static std::string openHeaderIncludePath(std::ifstream &f, const simplecpp::DUI &dui, const std::string &header)
23822387
{
23832388
for (std::list<std::string>::const_iterator it = dui.includePaths.begin(); it != dui.includePaths.end(); ++it) {
2384-
std::string s = *it;
2385-
if (!s.empty() && s[s.size()-1U]!='/' && s[s.size()-1U]!='\\')
2386-
s += '/';
2387-
s += header;
2388-
2389-
std::string simplePath = _openHeader(f, s);
2389+
std::string simplePath = _openHeader(f, getIncludePathFileName(*it, header));
23902390
if (!simplePath.empty())
23912391
return simplePath;
23922392
}
@@ -2418,28 +2418,19 @@ static std::string getFileName(const std::map<std::string, simplecpp::TokenList
24182418
return (filedata.find(header) != filedata.end()) ? simplecpp::simplifyPath(header) : "";
24192419
}
24202420

2421-
if (!systemheader) {
2422-
if (sourcefile.find_first_of("\\/") != std::string::npos) {
2423-
const std::string s(simplecpp::simplifyPath(sourcefile.substr(0, sourcefile.find_last_of("\\/") + 1U) + header));
2424-
if (filedata.find(s) != filedata.end())
2425-
return s;
2426-
} else {
2427-
std::string s = simplecpp::simplifyPath(header);
2428-
if (filedata.find(s) != filedata.end())
2429-
return s;
2430-
}
2431-
}
2421+
const std::string relativeFilename = getRelativeFileName(sourcefile, header);
2422+
if (!systemheader && filedata.find(relativeFilename) != filedata.end())
2423+
return relativeFilename;
24322424

24332425
for (std::list<std::string>::const_iterator it = dui.includePaths.begin(); it != dui.includePaths.end(); ++it) {
2434-
std::string s = *it;
2435-
if (!s.empty() && s[s.size()-1U]!='/' && s[s.size()-1U]!='\\')
2436-
s += '/';
2437-
s += header;
2438-
s = simplecpp::simplifyPath(s);
2426+
std::string s = simplecpp::simplifyPath(getIncludePathFileName(*it, header));
24392427
if (filedata.find(s) != filedata.end())
24402428
return s;
24412429
}
24422430

2431+
if (filedata.find(relativeFilename) != filedata.end())
2432+
return relativeFilename;
2433+
24432434
return "";
24442435
}
24452436

0 commit comments

Comments
 (0)