4040#include < utility>
4141#include < vector>
4242
43+ #ifdef _WIN32
44+ #include < direct.h>
45+ #else
46+ #include < unistd.h>
47+ #endif
48+
4349#ifdef SIMPLECPP_WINDOWS
4450#include < windows.h>
4551#undef ERROR
@@ -147,6 +153,12 @@ static unsigned long long stringToULL(const std::string &s)
147153 return ret;
148154}
149155
156+ // TODO: added an undercore since this conflicts with a function of the same name in utils.h from Cppcheck source when building Cppcheck with MSBuild
157+ static bool startsWith_ (const std::string &s, const std::string &p)
158+ {
159+ return (s.size () >= p.size ()) && std::equal (p.begin (), p.end (), s.begin ());
160+ }
161+
150162static bool endsWith (const std::string &s, const std::string &e)
151163{
152164 return (s.size () >= e.size ()) && std::equal (e.rbegin (), e.rend (), s.rbegin ());
@@ -2334,17 +2346,12 @@ namespace simplecpp {
23342346namespace simplecpp {
23352347
23362348#ifdef __CYGWIN__
2337- bool startsWith (const std::string &str, const std::string &s)
2338- {
2339- return (str.size () >= s.size () && str.compare (0 , s.size (), s) == 0 );
2340- }
2341-
23422349 std::string convertCygwinToWindowsPath (const std::string &cygwinPath)
23432350 {
23442351 std::string windowsPath;
23452352
23462353 std::string::size_type pos = 0 ;
2347- if (cygwinPath.size () >= 11 && startsWith (cygwinPath, " /cygdrive/" )) {
2354+ if (cygwinPath.size () >= 11 && startsWith_ (cygwinPath, " /cygdrive/" )) {
23482355 const unsigned char driveLetter = cygwinPath[10 ];
23492356 if (std::isalpha (driveLetter)) {
23502357 if (cygwinPath.size () == 11 ) {
@@ -2681,6 +2688,47 @@ static bool isCpp17OrLater(const simplecpp::DUI &dui)
26812688 return !std_ver.empty () && (std_ver >= " 201703L" );
26822689}
26832690
2691+
2692+ static std::string currentDirectoryOSCalc () {
2693+ const std::size_t size = 4096 ;
2694+ char currentPath[size];
2695+
2696+ #ifndef _WIN32
2697+ if (getcwd (currentPath, size) != nullptr )
2698+ #else
2699+ if (_getcwd (currentPath, size) != nullptr )
2700+ #endif
2701+ return std::string (currentPath);
2702+
2703+ return " " ;
2704+ }
2705+
2706+ static const std::string& currentDirectory () {
2707+ static const std::string curdir = simplecpp::simplifyPath (currentDirectoryOSCalc ());
2708+ return curdir;
2709+ }
2710+
2711+ static std::string toAbsolutePath (const std::string& path) {
2712+ if (path.empty ()) {
2713+ return path;// preserve error file path that is indicated by an empty string
2714+ }
2715+ if (!isAbsolutePath (path)) {
2716+ return simplecpp::simplifyPath (currentDirectory () + " /" + path);
2717+ }
2718+ // otherwise
2719+ return simplecpp::simplifyPath (path);
2720+ }
2721+
2722+ static std::pair<std::string, bool > extractRelativePathFromAbsolute (const std::string& absolutepath) {
2723+ static const std::string prefix = currentDirectory () + " /" ;
2724+ if (startsWith_ (absolutepath, prefix)) {
2725+ const std::size_t size = prefix.size ();
2726+ return std::make_pair (absolutepath.substr (size, absolutepath.size () - size), true );
2727+ }
2728+ // otherwise
2729+ return std::make_pair (" " , false );
2730+ }
2731+
26842732static std::string openHeader (std::ifstream &f, const simplecpp::DUI &dui, const std::string &sourcefile, const std::string &header, bool systemheader);
26852733static void simplifyHasInclude (simplecpp::TokenList &expr, const simplecpp::DUI &dui)
26862734{
@@ -3099,9 +3147,12 @@ static std::string openHeader(std::ifstream &f, const std::string &path)
30993147
31003148static std::string getRelativeFileName (const std::string &sourcefile, const std::string &header)
31013149{
3150+ std::string path;
31023151 if (sourcefile.find_first_of (" \\ /" ) != std::string::npos)
3103- return simplecpp::simplifyPath (sourcefile.substr (0 , sourcefile.find_last_of (" \\ /" ) + 1U ) + header);
3104- return simplecpp::simplifyPath (header);
3152+ path = sourcefile.substr (0 , sourcefile.find_last_of (" \\ /" ) + 1U ) + header;
3153+ else
3154+ path = header;
3155+ return simplecpp::simplifyPath (path);
31053156}
31063157
31073158static std::string openHeaderRelative (std::ifstream &f, const std::string &sourcefile, const std::string &header)
@@ -3111,7 +3162,7 @@ static std::string openHeaderRelative(std::ifstream &f, const std::string &sourc
31113162
31123163static std::string getIncludePathFileName (const std::string &includePath, const std::string &header)
31133164{
3114- std::string path = includePath;
3165+ std::string path = toAbsolutePath ( includePath) ;
31153166 if (!path.empty () && path[path.size ()-1U ]!=' /' && path[path.size ()-1U ]!=' \\ ' )
31163167 path += ' /' ;
31173168 return path + header;
@@ -3120,9 +3171,9 @@ static std::string getIncludePathFileName(const std::string &includePath, const
31203171static std::string openHeaderIncludePath (std::ifstream &f, const simplecpp::DUI &dui, const std::string &header)
31213172{
31223173 for (std::list<std::string>::const_iterator it = dui.includePaths .begin (); it != dui.includePaths .end (); ++it) {
3123- std::string simplePath = openHeader (f, getIncludePathFileName (*it, header));
3124- if (!simplePath .empty ())
3125- return simplePath ;
3174+ std::string path = openHeader (f, getIncludePathFileName (*it, header));
3175+ if (!path .empty ())
3176+ return path ;
31263177 }
31273178 return " " ;
31283179}
@@ -3132,49 +3183,76 @@ static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const
31323183 if (isAbsolutePath (header))
31333184 return openHeader (f, header);
31343185
3135- std::string ret;
3136-
31373186 if (systemheader) {
3138- ret = openHeaderIncludePath (f, dui, header);
3139- return ret ;
3187+ // always return absolute path for systemheaders
3188+ return toAbsolutePath ( openHeaderIncludePath (f, dui, header)) ;
31403189 }
31413190
3191+ std::string ret;
3192+
31423193 ret = openHeaderRelative (f, sourcefile, header);
31433194 if (ret.empty ())
3144- return openHeaderIncludePath (f, dui, header);
3195+ return toAbsolutePath ( openHeaderIncludePath (f, dui, header)); // in a similar way to system headers
31453196 return ret;
31463197}
31473198
3148- 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)
3199+ static std::string findPathInMapBothRelativeAndAbsolute (const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string& path) {
3200+ // here there are two possibilities - either we match this from absolute path or from a relative one
3201+ if (filedata.find (path) != filedata.end ()) {// try first to respect the exact match
3202+ return path;
3203+ }
3204+ // otherwise - try to use the normalize to the correct representation
3205+ if (isAbsolutePath (path)) {
3206+ const std::pair<std::string, bool > relativeExtractedResult = extractRelativePathFromAbsolute (path);
3207+ if (relativeExtractedResult.second ) {
3208+ const std::string relativePath = relativeExtractedResult.first ;
3209+ if (filedata.find (relativePath) != filedata.end ()) {
3210+ return relativePath;
3211+ }
3212+ }
3213+ } else {
3214+ const std::string absolutePath = toAbsolutePath (path);
3215+ if (filedata.find (absolutePath) != filedata.end ())
3216+ return absolutePath;
3217+ }
3218+ // otherwise
3219+ return " " ;
3220+ }
3221+
3222+ 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)
31493223{
31503224 if (filedata.empty ()) {
31513225 return " " ;
31523226 }
31533227 if (isAbsolutePath (header)) {
3154- return (filedata.find (header) != filedata.end ()) ? simplecpp::simplifyPath (header) : " " ;
3228+ const std::string simplifiedHeaderPath = simplecpp::simplifyPath (header);
3229+ return (filedata.find (simplifiedHeaderPath) != filedata.end ()) ? simplifiedHeaderPath : " " ;
31553230 }
31563231
31573232 if (!systemheader) {
3158- const std::string relativeFilename = getRelativeFileName (sourcefile, header);
3159- if (filedata.find (relativeFilename) != filedata.end ())
3160- return relativeFilename;
3233+ const std::string relativeOrAbsoluteFilename = getRelativeFileName (sourcefile, header);// unknown if absolute or relative, but always simplified
3234+ const std::string match = findPathInMapBothRelativeAndAbsolute (filedata, relativeOrAbsoluteFilename);
3235+ if (!match.empty ()) {
3236+ return match;
3237+ }
31613238 }
31623239
31633240 for (std::list<std::string>::const_iterator it = dui.includePaths .begin (); it != dui.includePaths .end (); ++it) {
3164- std::string s = simplecpp::simplifyPath (getIncludePathFileName (*it, header));
3165- if (filedata.find (s) != filedata.end ())
3166- return s;
3241+ const std::string match = findPathInMapBothRelativeAndAbsolute (filedata, simplecpp::simplifyPath (getIncludePathFileName (*it, header)));
3242+ if (!match.empty ()) {
3243+ return match;
3244+ }
31673245 }
31683246
31693247 if (systemheader && filedata.find (header) != filedata.end ())
3170- return header;
3248+ return header;// system header that its file wasn't found in the included paths but alreasy in the filedata - return this as is
31713249
31723250 return " " ;
31733251}
31743252
31753253static bool hasFile (const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string &sourcefile, const std::string &header, const simplecpp::DUI &dui, bool systemheader)
31763254{
3177- return !getFileName (filedata, sourcefile, header, dui, systemheader).empty ();
3255+ return !getFileIdPath (filedata, sourcefile, header, dui, systemheader).empty ();
31783256}
31793257
31803258std::map<std::string, simplecpp::TokenList*> simplecpp::load (const simplecpp::TokenList &rawtokens, std::vector<std::string> &filenames, const simplecpp::DUI &dui, simplecpp::OutputList *outputList)
@@ -3530,7 +3608,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
35303608
35313609 const bool systemheader = (inctok->str ()[0 ] == ' <' );
35323610 const std::string header (realFilename (inctok->str ().substr (1U , inctok->str ().size () - 2U )));
3533- std::string header2 = getFileName (filedata, rawtok->location .file (), header, dui, systemheader);
3611+ std::string header2 = getFileIdPath (filedata, rawtok->location .file (), header, dui, systemheader);
35343612 if (header2.empty ()) {
35353613 // try to load file..
35363614 std::ifstream f;
0 commit comments