| 69 | } |
| 70 | |
| 71 | std::string ProjectManager::includeRecovery(llvm::StringRef includeName, llvm::StringRef from) |
| 72 | { |
| 73 | #if CLANG_VERSION_MAJOR != 3 || CLANG_VERSION_MINOR >= 5 |
| 74 | if (includeRecoveryCache.empty()) { |
| 75 | for (const auto &proj : projects) { |
| 76 | // skip sub project |
| 77 | llvm::StringRef sourcePath(proj.source_path); |
| 78 | auto parentPath = sourcePath.substr(0, sourcePath.rfind('/')); |
| 79 | if (projectForFile(parentPath)) |
| 80 | continue; |
| 81 | |
| 82 | std::error_code EC; |
| 83 | for (llvm::sys::fs::recursive_directory_iterator it(sourcePath, EC), DirEnd; |
| 84 | it != DirEnd && !EC; it.increment(EC)) { |
| 85 | auto fileName = llvm::sys::path::filename(it->path()); |
| 86 | if (fileName.startswith(".")) { |
| 87 | it.no_push(); |
| 88 | continue; |
| 89 | } |
| 90 | includeRecoveryCache.insert({std::string(fileName), it->path()}); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | llvm::StringRef includeFileName = llvm::sys::path::filename(includeName); |
| 95 | std::string resolved; |
| 96 | int weight = -1000; |
| 97 | auto range = includeRecoveryCache.equal_range(includeFileName); |
| 98 | for (auto it = range.first; it != range.second; ++it) { |
| 99 | llvm::StringRef candidate(it->second); |
| 100 | uint suf_len = 0; |
| 101 | while (suf_len < std::min(candidate.size(), includeName.size())) { |
| 102 | if(candidate[candidate.size()-suf_len-1] != includeName[candidate.size()-suf_len-1]) |
| 103 | break; |
| 104 | suf_len++; |
| 105 | } |
| 106 | //Each paths part that are similar from the expected name are weighted 1000 points f |
| 107 | int w = includeName.substr(candidate.size()-suf_len).count('/') * 1000; |
| 108 | if (w + 1000 < weight) |
| 109 | continue; |
| 110 | |
| 111 | // after that, order by similarity with the from url |
| 112 | uint pref_len = 0; |
| 113 | while (pref_len < std::min(candidate.size(), from.size())) { |
| 114 | if(candidate[pref_len] != from[pref_len]) |
| 115 | break; |
| 116 | pref_len++; |
| 117 | } |
| 118 | w += candidate.substr(0, pref_len).count('/') * 10; |
| 119 | |
| 120 | // and the smaller the path, the better |
| 121 | w -= candidate.count('/'); |
| 122 | |
| 123 | if (w < weight) |
| 124 | continue; |
| 125 | |
| 126 | weight = w; |
| 127 | resolved = candidate; |
| 128 | } |