================
@@ -253,6 +253,80 @@ std::optional<P1689Rule> 
DependencyScanningTool::getP1689ModuleDependencyFile(
   return Rule;
 }
 
+static std::pair<IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>,
+                 std::vector<std::string>>
+initVFSForTUBufferScanning(IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
+                           ArrayRef<std::string> CommandLine,
+                           StringRef WorkingDirectory,
+                           llvm::MemoryBufferRef TUBuffer) {
+  // Reset what might have been modified in the previous worker invocation.
+  BaseFS->setCurrentWorkingDirectory(WorkingDirectory);
+
+  auto OverlayFS =
+      llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(BaseFS);
+  auto InMemoryFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
+  InMemoryFS->setCurrentWorkingDirectory(WorkingDirectory);
+  auto InputPath = TUBuffer.getBufferIdentifier();
+  InMemoryFS->addFile(
+      InputPath, 0, 
llvm::MemoryBuffer::getMemBufferCopy(TUBuffer.getBuffer()));
+  IntrusiveRefCntPtr<llvm::vfs::FileSystem> InMemoryOverlay = InMemoryFS;
+
+  OverlayFS->pushOverlay(InMemoryOverlay);
+  std::vector<std::string> ModifiedCommandLine(CommandLine);
+  ModifiedCommandLine.emplace_back(InputPath);
+
+  return std::make_pair(OverlayFS, ModifiedCommandLine);
+}
+
+namespace {
+struct FakeInputBuffer {
+  // A null terminated buffer generated at compile time.
+  // Using this buffer we can avoid initializing a std::string on the
+  // heap, which triggers the AddressSanitizer.
+  char Data[CompilerInstanceWithContext::MaxNumOfQueries + 1];
+  constexpr FakeInputBuffer() : Data{} {
+    for (int I = 0; I < CompilerInstanceWithContext::MaxNumOfQueries; ++I)
+      Data[I] = ' ';
+    Data[CompilerInstanceWithContext::MaxNumOfQueries] = '\0';
+  }
+};
+constexpr FakeInputBuffer FakeInputBuf;
+} // namespace
+
+// The fake input buffer is read-only, and it is used to produce
+// unique source locations for the diagnostics. Therefore sharing
+// this global buffer across threads is ok.
+static const StringRef FakeInput(FakeInputBuf.Data,
+                                 CompilerInstanceWithContext::MaxNumOfQueries +
+                                     1);
+
+static std::pair<IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>,
+                 std::vector<std::string>>
+initVFSForByNameScanning(IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
+                         ArrayRef<std::string> CommandLine,
+                         StringRef WorkingDirectory) {
+  // Reset what might have been modified in the previous worker invocation.
+  BaseFS->setCurrentWorkingDirectory(WorkingDirectory);
+
+  // If we're scanning based on a module name alone, we don't expect the client
+  // to provide us with an input file. However, the driver really wants to have
+  // one. Let's just make it up to make the driver happy.
+  auto OverlayFS =
+      llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(BaseFS);
+  auto InMemoryFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
+  InMemoryFS->setCurrentWorkingDirectory(WorkingDirectory);
+  StringRef FakeInputPath("module-include.input");
+  InMemoryFS->addFile(FakeInputPath, 0,
+                      llvm::MemoryBuffer::getMemBuffer(FakeInput));
----------------
naveen-seth wrote:

Instead of generating the comptime string, could we do something like this?

```cpp
static std::string *FakeInputBuf =
  new std::string(CompilerInstanceWithContext::MaxNumOfQueries, ' ');
StringRef FakeInput(FakeInputBuf->c_str(), FakeInputBuf->size() + 1);
InMemoryFS->addFile(FakeInputPath, 0,
                    llvm::MemoryBuffer::getMemBuffer(FakeInput));
```

That would address @jansvoboda11 concern and the pattern does not trigger ASan.

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

Reply via email to