@@ -3145,11 +3145,11 @@ static std::string openHeader(std::ifstream &f, const std::string &path)
3145
3145
return " " ;
3146
3146
}
3147
3147
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)
3149
3149
{
3150
3150
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;
3153
3153
else
3154
3154
path = header;
3155
3155
return simplecpp::simplifyPath (path);
@@ -3160,12 +3160,22 @@ static std::string openHeaderRelative(std::ifstream &f, const std::string &sourc
3160
3160
return openHeader (f, getRelativeFileName (sourcefile, header));
3161
3161
}
3162
3162
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
3163
3166
static std::string getIncludePathFileName (const std::string &includePath, const std::string &header)
3164
3167
{
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
+ const std::string absolutesimplifiedHeaderPath = basePath + simplifiedHeader;
3178
+ return extractRelativePathFromAbsolute (absolutesimplifiedHeaderPath).first ;
3169
3179
}
3170
3180
3171
3181
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
3183
3193
if (isAbsolutePath (header))
3184
3194
return openHeader (f, header);
3185
3195
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
+ }
3189
3202
}
3190
3203
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);
3197
3206
}
3198
3207
3199
3208
static std::string findPathInMapBothRelativeAndAbsolute (const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string& path) {
@@ -3212,8 +3221,9 @@ static std::string findPathInMapBothRelativeAndAbsolute(const std::map<std::stri
3212
3221
}
3213
3222
} else {
3214
3223
const std::string absolutePath = toAbsolutePath (path);
3215
- if (filedata.find (absolutePath) != filedata.end ())
3224
+ if (filedata.find (absolutePath) != filedata.end ()) {
3216
3225
return absolutePath;
3226
+ }
3217
3227
}
3218
3228
// otherwise
3219
3229
return " " ;
@@ -3226,7 +3236,10 @@ static std::string getFileIdPath(const std::map<std::string, simplecpp::TokenLis
3226
3236
}
3227
3237
if (isAbsolutePath (header)) {
3228
3238
const std::string simplifiedHeaderPath = simplecpp::simplifyPath (header);
3229
- return (filedata.find (simplifiedHeaderPath) != filedata.end ()) ? simplifiedHeaderPath : " " ;
3239
+ const std::string match = findPathInMapBothRelativeAndAbsolute (filedata, simplifiedHeaderPath);
3240
+ if (!match.empty ()) {
3241
+ return match;
3242
+ }
3230
3243
}
3231
3244
3232
3245
if (!systemheader) {
@@ -3235,18 +3248,17 @@ static std::string getFileIdPath(const std::map<std::string, simplecpp::TokenLis
3235
3248
if (!match.empty ()) {
3236
3249
return match;
3237
3250
}
3251
+ } else if (filedata.find (header) != filedata.end ()) {
3252
+ return header;// system header that its file is already in the filedata - return that as is
3238
3253
}
3239
3254
3240
3255
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) ));
3256
+ const std::string match = findPathInMapBothRelativeAndAbsolute (filedata, getIncludePathFileName (*it, header));
3242
3257
if (!match.empty ()) {
3243
3258
return match;
3244
3259
}
3245
3260
}
3246
3261
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
-
3250
3262
return " " ;
3251
3263
}
3252
3264
0 commit comments