================
@@ -24,22 +25,72 @@ namespace clang {
 namespace clangd {
 namespace {
 
+// Apply path mapping to file:// URIs or raw file paths. Return unmodified
+// path if no mapping was applied.
+std::string applyPathMapping(llvm::StringRef S,
+                             PathMapping::Direction Direction,
+                             const PathMappings &Mappings) {
+  // First, attempt URI mapping
+  if (auto Mapped = doPathMapping(S, Direction, Mappings))
+    return std::move(*Mapped);
+  // If that didn't match, attempt file path mapping. Paths processed here may
+  // be standalone or after a flag: -I, -isystem, and so on.
+  for (const auto &Mapping : Mappings) {
+    const std::string &From =
+        Direction == PathMapping::Direction::ClientToServer
+            ? Mapping.ClientPath
+            : Mapping.ServerPath;
+    const std::string &To = Direction == PathMapping::Direction::ClientToServer
+                                ? Mapping.ServerPath
+                                : Mapping.ClientPath;
+    size_t Pos = S.find(From);
+    if (Pos != llvm::StringRef::npos) {
+      llvm::StringRef After = S.substr(Pos + From.size());
+      if (After.empty() || After.front() == '/')
+        return (S.substr(0, Pos) + To + After).str();
+    }
----------------
ArcsinX wrote:

If we try to apply it to every string, this can be a problem. E.g. we have 
documentation of a symbol in the strings table, which can contain some paths 
inside.

Also, we have the following problems here:
- if we failed to apply a path mapping to URI, we will try to apply this path 
mapping to the URI again (but using file paths approach)
- maybe we can find suitable mapping in the middle of a file path and replace it

https://github.com/llvm/llvm-project/pull/180285
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to