43
43
#ifdef SIMPLECPP_WINDOWS
44
44
#include < windows.h>
45
45
#undef ERROR
46
+ #else
47
+ #include < unistd.h>
46
48
#endif
47
49
48
50
#if __cplusplus >= 201103L
@@ -147,6 +149,12 @@ static unsigned long long stringToULL(const std::string &s)
147
149
return ret;
148
150
}
149
151
152
+ // 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
153
+ static bool startsWith_ (const std::string &s, const std::string &p)
154
+ {
155
+ return (s.size () >= p.size ()) && std::equal (p.begin (), p.end (), s.begin ());
156
+ }
157
+
150
158
static bool endsWith (const std::string &s, const std::string &e)
151
159
{
152
160
return (s.size () >= e.size ()) && std::equal (e.rbegin (), e.rend (), s.rbegin ());
@@ -2334,17 +2342,12 @@ namespace simplecpp {
2334
2342
namespace simplecpp {
2335
2343
2336
2344
#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
-
2342
2345
std::string convertCygwinToWindowsPath (const std::string &cygwinPath)
2343
2346
{
2344
2347
std::string windowsPath;
2345
2348
2346
2349
std::string::size_type pos = 0 ;
2347
- if (cygwinPath.size () >= 11 && startsWith (cygwinPath, " /cygdrive/" )) {
2350
+ if (cygwinPath.size () >= 11 && startsWith_ (cygwinPath, " /cygdrive/" )) {
2348
2351
const unsigned char driveLetter = cygwinPath[10 ];
2349
2352
if (std::isalpha (driveLetter)) {
2350
2353
if (cygwinPath.size () == 11 ) {
@@ -2681,6 +2684,54 @@ static bool isCpp17OrLater(const simplecpp::DUI &dui)
2681
2684
return !std_ver.empty () && (std_ver >= " 201703L" );
2682
2685
}
2683
2686
2687
+
2688
+ static std::string currentDirectoryOSCalc () {
2689
+ #ifdef SIMPLECPP_WINDOWS
2690
+ TCHAR NPath[MAX_PATH];
2691
+ GetCurrentDirectory (MAX_PATH, NPath);
2692
+ #ifdef _UNICODE
2693
+ // convert the result from TCHAR* to char*
2694
+ char NPathA[MAX_PATH];
2695
+ ::WideCharToMultiByte (CP_ACP, 0 , NPath, lstrlen(NPath), NPathA, MAX_PATH, NULL, NULL);
2696
+ return NPathA;
2697
+ #else
2698
+ // in this case, TCHAR* is just defined to be a char*
2699
+ return NPath;
2700
+ #endif
2701
+ #else
2702
+ const std::size_t size = 1024 ;
2703
+ char the_path[size];
2704
+ getcwd (the_path, size);
2705
+ return the_path;
2706
+ #endif
2707
+ }
2708
+
2709
+ static const std::string& currentDirectory () {
2710
+ static const std::string curdir = simplecpp::simplifyPath (currentDirectoryOSCalc ());
2711
+ return curdir;
2712
+ }
2713
+
2714
+ static std::string toAbsolutePath (const std::string& path) {
2715
+ if (path.empty ()) {
2716
+ return path;// preserve error file path that is indicated by an empty string
2717
+ }
2718
+ if (!isAbsolutePath (path)) {
2719
+ return simplecpp::simplifyPath (currentDirectory () + " /" + path);
2720
+ }
2721
+ // otherwise
2722
+ return simplecpp::simplifyPath (path);
2723
+ }
2724
+
2725
+ static std::pair<std::string, bool > extractRelativePathFromAbsolute (const std::string& absolutepath) {
2726
+ static const std::string prefix = currentDirectory () + " /" ;
2727
+ if (startsWith_ (absolutepath, prefix)) {
2728
+ const std::size_t size = prefix.size ();
2729
+ return std::make_pair (absolutepath.substr (size, absolutepath.size () - size), true );
2730
+ }
2731
+ // otherwise
2732
+ return std::make_pair (" " , false );
2733
+ }
2734
+
2684
2735
static std::string openHeader (std::ifstream &f, const simplecpp::DUI &dui, const std::string &sourcefile, const std::string &header, bool systemheader);
2685
2736
static void simplifyHasInclude (simplecpp::TokenList &expr, const simplecpp::DUI &dui)
2686
2737
{
@@ -3099,9 +3150,12 @@ static std::string openHeader(std::ifstream &f, const std::string &path)
3099
3150
3100
3151
static std::string getRelativeFileName (const std::string &sourcefile, const std::string &header)
3101
3152
{
3153
+ std::string path;
3102
3154
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);
3155
+ path = sourcefile.substr (0 , sourcefile.find_last_of (" \\ /" ) + 1U ) + header;
3156
+ else
3157
+ path = header;
3158
+ return simplecpp::simplifyPath (path);
3105
3159
}
3106
3160
3107
3161
static std::string openHeaderRelative (std::ifstream &f, const std::string &sourcefile, const std::string &header)
@@ -3111,7 +3165,7 @@ static std::string openHeaderRelative(std::ifstream &f, const std::string &sourc
3111
3165
3112
3166
static std::string getIncludePathFileName (const std::string &includePath, const std::string &header)
3113
3167
{
3114
- std::string path = includePath;
3168
+ std::string path = toAbsolutePath ( includePath) ;
3115
3169
if (!path.empty () && path[path.size ()-1U ]!=' /' && path[path.size ()-1U ]!=' \\ ' )
3116
3170
path += ' /' ;
3117
3171
return path + header;
@@ -3120,9 +3174,9 @@ static std::string getIncludePathFileName(const std::string &includePath, const
3120
3174
static std::string openHeaderIncludePath (std::ifstream &f, const simplecpp::DUI &dui, const std::string &header)
3121
3175
{
3122
3176
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 ;
3177
+ std::string path = openHeader (f, getIncludePathFileName (*it, header));
3178
+ if (!path .empty ())
3179
+ return path ;
3126
3180
}
3127
3181
return " " ;
3128
3182
}
@@ -3132,49 +3186,76 @@ static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const
3132
3186
if (isAbsolutePath (header))
3133
3187
return openHeader (f, header);
3134
3188
3135
- std::string ret;
3136
-
3137
3189
if (systemheader) {
3138
- ret = openHeaderIncludePath (f, dui, header);
3139
- return ret ;
3190
+ // always return absolute path for systemheaders
3191
+ return toAbsolutePath ( openHeaderIncludePath (f, dui, header)) ;
3140
3192
}
3141
3193
3194
+ std::string ret;
3195
+
3142
3196
ret = openHeaderRelative (f, sourcefile, header);
3143
3197
if (ret.empty ())
3144
- return openHeaderIncludePath (f, dui, header);
3198
+ return toAbsolutePath ( openHeaderIncludePath (f, dui, header)); // in a similar way to system headers
3145
3199
return ret;
3146
3200
}
3147
3201
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)
3202
+ static std::string findPathInMapBothRelativeAndAbsolute (const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string& path) {
3203
+ // here there are two possibilities - either we match this from absolute path or from a relative one
3204
+ if (filedata.find (path) != filedata.end ()) {// try first to respect the exact match
3205
+ return path;
3206
+ }
3207
+ // otherwise - try to use the normalize to the correct representation
3208
+ if (isAbsolutePath (path)) {
3209
+ const std::pair<std::string, bool > relativeExtractedResult = extractRelativePathFromAbsolute (path);
3210
+ if (relativeExtractedResult.second ) {
3211
+ const std::string relativePath = relativeExtractedResult.first ;
3212
+ if (filedata.find (relativePath) != filedata.end ()) {
3213
+ return relativePath;
3214
+ }
3215
+ }
3216
+ } else {
3217
+ const std::string absolutePath = toAbsolutePath (path);
3218
+ if (filedata.find (absolutePath) != filedata.end ())
3219
+ return absolutePath;
3220
+ }
3221
+ // otherwise
3222
+ return " " ;
3223
+ }
3224
+
3225
+ 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)
3149
3226
{
3150
3227
if (filedata.empty ()) {
3151
3228
return " " ;
3152
3229
}
3153
3230
if (isAbsolutePath (header)) {
3154
- return (filedata.find (header) != filedata.end ()) ? simplecpp::simplifyPath (header) : " " ;
3231
+ const std::string simplifiedHeaderPath = simplecpp::simplifyPath (header);
3232
+ return (filedata.find (simplifiedHeaderPath) != filedata.end ()) ? simplifiedHeaderPath : " " ;
3155
3233
}
3156
3234
3157
3235
if (!systemheader) {
3158
- const std::string relativeFilename = getRelativeFileName (sourcefile, header);
3159
- if (filedata.find (relativeFilename) != filedata.end ())
3160
- return relativeFilename;
3236
+ const std::string relativeOrAbsoluteFilename = getRelativeFileName (sourcefile, header);// unknown if absolute or relative, but always simplified
3237
+ const std::string match = findPathInMapBothRelativeAndAbsolute (filedata, relativeOrAbsoluteFilename);
3238
+ if (!match.empty ()) {
3239
+ return match;
3240
+ }
3161
3241
}
3162
3242
3163
3243
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;
3244
+ const std::string match = findPathInMapBothRelativeAndAbsolute (filedata, simplecpp::simplifyPath (getIncludePathFileName (*it, header)));
3245
+ if (!match.empty ()) {
3246
+ return match;
3247
+ }
3167
3248
}
3168
3249
3169
3250
if (systemheader && filedata.find (header) != filedata.end ())
3170
- return header;
3251
+ return header;// system header that its file wasn't found in the included paths but alreasy in the filedata - return this as is
3171
3252
3172
3253
return " " ;
3173
3254
}
3174
3255
3175
3256
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)
3176
3257
{
3177
- return !getFileName (filedata, sourcefile, header, dui, systemheader).empty ();
3258
+ return !getFileIdPath (filedata, sourcefile, header, dui, systemheader).empty ();
3178
3259
}
3179
3260
3180
3261
std::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 +3611,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
3530
3611
3531
3612
const bool systemheader = (inctok->str ()[0 ] == ' <' );
3532
3613
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);
3614
+ std::string header2 = getFileIdPath (filedata, rawtok->location .file (), header, dui, systemheader);
3534
3615
if (header2.empty ()) {
3535
3616
// try to load file..
3536
3617
std::ifstream f;
0 commit comments