@@ -138,12 +138,6 @@ static unsigned long long stringToULL(const std::string &s)
138138 return ret;
139139}
140140
141- // 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
142- static bool startsWith_ (const std::string &s, const std::string &p)
143- {
144- return (s.size () >= p.size ()) && std::equal (p.begin (), p.end (), s.begin ());
145- }
146-
147141static bool endsWith (const std::string &s, const std::string &e)
148142{
149143 return (s.size () >= e.size ()) && std::equal (e.rbegin (), e.rend (), s.rbegin ());
@@ -2551,39 +2545,6 @@ static bool isCpp17OrLater(const simplecpp::DUI &dui)
25512545}
25522546
25532547
2554- static std::string currentDirectoryOSCalc ()
2555- {
2556- const std::size_t size = 4096 ;
2557- char currentPath[size];
2558-
2559- #ifndef _WIN32
2560- if (getcwd (currentPath, size) != nullptr )
2561- #else
2562- if (_getcwd (currentPath, size) != nullptr )
2563- #endif
2564- return std::string (currentPath);
2565-
2566- return " " ;
2567- }
2568-
2569- static const std::string& currentDirectory ()
2570- {
2571- static const std::string curdir = simplecpp::simplifyPath (currentDirectoryOSCalc ());
2572- return curdir;
2573- }
2574-
2575- static std::string toAbsolutePath (const std::string& path)
2576- {
2577- if (path.empty ()) {
2578- return path;// preserve error file path that is indicated by an empty string
2579- }
2580- if (!isAbsolutePath (path)) {
2581- return simplecpp::simplifyPath (currentDirectory () + " /" + path);
2582- }
2583- // otherwise
2584- return simplecpp::simplifyPath (path);
2585- }
2586-
25872548static std::string dirPath (const std::string& path, bool withTrailingSlash=true )
25882549{
25892550 const std::size_t lastSlash = path.find_last_of (" \\ /" );
@@ -2593,38 +2554,6 @@ static std::string dirPath(const std::string& path, bool withTrailingSlash=true)
25932554 return path.substr (0 , lastSlash + (withTrailingSlash ? 1U : 0U ));
25942555}
25952556
2596- static std::string omitPathTrailingSlash (const std::string& path)
2597- {
2598- if (endsWith (path, " /" )) {
2599- return path.substr (0 , path.size () - 1U );
2600- }
2601- return path;
2602- }
2603-
2604- static std::string extractRelativePathFromAbsolute (const std::string& absoluteSimplifiedPath, const std::string& prefixSimplifiedAbsoluteDir = currentDirectory())
2605- {
2606- const std::string normalizedAbsolutePath = omitPathTrailingSlash (absoluteSimplifiedPath);
2607- std::string currentPrefix = omitPathTrailingSlash (prefixSimplifiedAbsoluteDir);
2608- std::string leadingParenting;
2609- while (!startsWith_ (normalizedAbsolutePath, currentPrefix)) {
2610- leadingParenting = " ../" + leadingParenting;
2611- currentPrefix = dirPath (currentPrefix, false );
2612- }
2613- const std::size_t size = currentPrefix.size ();
2614- std::string relativeFromMeetingPath = normalizedAbsolutePath.substr (size, normalizedAbsolutePath.size () - size);
2615- if (currentPrefix.empty () && !(startsWith_ (absoluteSimplifiedPath, " /" ) && startsWith_ (prefixSimplifiedAbsoluteDir, " /" ))) {
2616- // In the case that there is no common prefix path,
2617- // and at not both of the paths start with `/` (can happen only in Windows paths on distinct partitions),
2618- // return the absolute simplified path as is because no relative path can match.
2619- return absoluteSimplifiedPath;
2620- }
2621- if (startsWith_ (relativeFromMeetingPath, " /" )) {
2622- // omit the leading slash
2623- relativeFromMeetingPath = relativeFromMeetingPath.substr (1 , relativeFromMeetingPath.size ());
2624- }
2625- return leadingParenting + relativeFromMeetingPath;
2626- }
2627-
26282557static std::string openHeader (std::ifstream &f, const simplecpp::DUI &dui, const std::string &sourcefile, const std::string &header, bool systemheader);
26292558static void simplifyHasInclude (simplecpp::TokenList &expr, const simplecpp::DUI &dui)
26302559{
@@ -3023,86 +2952,41 @@ static NonExistingFilesCache nonExistingFilesCache;
30232952
30242953#endif
30252954
3026- static std::string openHeader (std::ifstream &f, const std::string &path)
2955+ static std::string openHeaderDirect (std::ifstream &f, const std::string &path)
30272956{
3028- std::string simplePath = simplecpp::simplifyPath (path);
30292957#ifdef SIMPLECPP_WINDOWS
3030- if (nonExistingFilesCache.contains (simplePath ))
2958+ if (nonExistingFilesCache.contains (path ))
30312959 return " " ; // file is known not to exist, skip expensive file open call
30322960#endif
3033- f.open (simplePath .c_str ());
2961+ f.open (path .c_str ());
30342962 if (f.is_open ())
3035- return simplePath ;
2963+ return path ;
30362964#ifdef SIMPLECPP_WINDOWS
3037- nonExistingFilesCache.add (simplePath );
2965+ nonExistingFilesCache.add (path );
30382966#endif
30392967 return " " ;
30402968}
30412969
3042- static std::string getRelativeFileName (const std::string &baseFile, const std::string &header, bool returnAbsolutePath)
3043- {
3044- const std::string baseFileSimplified = simplecpp::simplifyPath (baseFile);
3045- const std::string baseFileAbsolute = isAbsolutePath (baseFileSimplified) ?
3046- baseFileSimplified :
3047- simplecpp::simplifyPath (currentDirectory () + " /" + baseFileSimplified);
3048-
3049- const std::string headerSimplified = simplecpp::simplifyPath (header);
3050- const std::string path = isAbsolutePath (headerSimplified) ?
3051- headerSimplified :
3052- simplecpp::simplifyPath (dirPath (baseFileAbsolute) + headerSimplified);
3053-
3054- return returnAbsolutePath ? toAbsolutePath (path) : extractRelativePathFromAbsolute (path);
3055- }
3056-
3057- static std::string openHeaderRelative (std::ifstream &f, const std::string &sourcefile, const std::string &header)
3058- {
3059- return openHeader (f, getRelativeFileName (sourcefile, header, isAbsolutePath (sourcefile)));
3060- }
3061-
3062- // returns the simplified header path:
3063- // * If the header path is absolute, returns it in absolute path
3064- // * Otherwise, returns it in relative path with respect to the current directory
3065- static std::string getIncludePathFileName (const std::string &includePath, const std::string &header)
3066- {
3067- std::string simplifiedHeader = simplecpp::simplifyPath (header);
3068-
3069- if (isAbsolutePath (simplifiedHeader)) {
3070- return simplifiedHeader;
3071- }
3072-
3073- std::string basePath = toAbsolutePath (includePath);
3074- if (!basePath.empty () && basePath[basePath.size ()-1U ]!=' /' && basePath[basePath.size ()-1U ]!=' \\ ' )
3075- basePath += ' /' ;
3076- const std::string absoluteSimplifiedHeaderPath = simplecpp::simplifyPath (basePath + simplifiedHeader);
3077- // preserve absoluteness/relativieness of the including dir
3078- return isAbsolutePath (includePath) ? absoluteSimplifiedHeaderPath : extractRelativePathFromAbsolute (absoluteSimplifiedHeaderPath);
3079- }
3080-
3081- static std::string openHeaderIncludePath (std::ifstream &f, const simplecpp::DUI &dui, const std::string &header)
3082- {
3083- for (std::list<std::string>::const_iterator it = dui.includePaths .begin (); it != dui.includePaths .end (); ++it) {
3084- std::string path = openHeader (f, getIncludePathFileName (*it, header));
3085- if (!path.empty ())
3086- return path;
3087- }
3088- return " " ;
3089- }
3090-
30912970static std::string openHeader (std::ifstream &f, const simplecpp::DUI &dui, const std::string &sourcefile, const std::string &header, bool systemheader)
30922971{
30932972 if (isAbsolutePath (header))
3094- return openHeader (f, header);
2973+ return openHeaderDirect (f, simplecpp::simplifyPath ( header) );
30952974
30962975 // prefer first to search the header relatively to source file if found, when not a system header
30972976 if (!systemheader) {
3098- std::string relativeHeader = openHeaderRelative (f, sourcefile, header);
3099- if (!relativeHeader .empty ()) {
3100- return relativeHeader ;
2977+ std::string path = openHeaderDirect (f, simplecpp::simplifyPath ( dirPath ( sourcefile) + header) );
2978+ if (!path .empty ()) {
2979+ return path ;
31012980 }
31022981 }
31032982
31042983 // search the header on the include paths (provided by the flags "-I...")
3105- return openHeaderIncludePath (f, dui, header);
2984+ for (const auto &includePath : dui.includePaths ) {
2985+ std::string path = openHeaderDirect (f, simplecpp::simplifyPath (includePath + " /" + header));
2986+ if (!path.empty ())
2987+ return path;
2988+ }
2989+ return " " ;
31062990}
31072991
31082992simplecpp::FileData *simplecpp::FileDataCache::lookup (const std::string &sourcefile, const std::string &header, const simplecpp::DUI &dui, bool systemheader)
@@ -3116,24 +3000,26 @@ simplecpp::FileData *simplecpp::FileDataCache::lookup(const std::string &sourcef
31163000
31173001 if (name_it != mNameMap .end ())
31183002 return name_it->second ;
3003+
3004+ return nullptr ;
31193005 }
31203006
31213007 if (!systemheader) {
3122- const std::string path = getRelativeFileName ( sourcefile, header, true );
3008+ const std::string path = simplecpp::simplifyPath ( dirPath ( sourcefile) + header );
31233009 const auto name_it = mNameMap .find (path);
31243010
31253011 if (name_it != mNameMap .end ())
31263012 return name_it->second ;
31273013
31283014 // If the file exists but hasn't been loaded yet then we need to stop searching here or we could get a false match
31293015 std::ifstream f;
3130- openHeader (f, path);
3016+ openHeaderDirect (f, path);
31313017 if (f.is_open ())
31323018 return nullptr ;
31333019 }
31343020
3135- for (std::list<std::string>::const_iterator it = dui. includePaths . begin (); it != dui.includePaths . end (); ++it ) {
3136- const std::string path = getIncludePathFileName (*it, header);
3021+ for (const auto &includePath : dui.includePaths ) {
3022+ const std::string path = simplecpp::simplifyPath (includePath + " / " + header);
31373023 const auto name_it = mNameMap .find (path);
31383024
31393025 if (name_it != mNameMap .end ())
0 commit comments