[llvm] [clang] Fix handling of adding a file with the same name as an existing dir to VFS (PR #94461)

kadir çetinkaya via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 5 21:55:41 PDT 2024


================
@@ -893,6 +893,10 @@ bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime,
     }
 
     if (auto *NewDir = dyn_cast<detail::InMemoryDirectory>(Node)) {
+      // Trying to insert a file in place of a directory.
+      if (I == E)
----------------
kadircet wrote:

instead of adding some more branches, can we refactor this logic a little bit? maybe something like:


```cpp
...
StringRef Name = *I;
while (true) {
    ++I;
    if (I == E)
       break;
    detail::InMemoryNode *Node = Dir->getChild(Name);
    if (!Node) {
        // just create the dir, we know this isn't the last element
    }
    // Creating file under another file.
    if (!isa<detail::InMemoryDirectory>(Node))
       return false;
    Dir = Node;
    Name = *I;
}
detail::InMemoryNode *Node = Dir->getChild(Name);
if (!Node) {
  // just insert the new node
  return true;
}
// Return true iff `Node` is same as what we're trying to insert.
```

https://github.com/llvm/llvm-project/pull/94461


More information about the llvm-commits mailing list