]> granicus.if.org Git - clang/commitdiff
Frontend: Add support for reading named pipes as the main file.
authorDaniel Dunbar <daniel@zuster.org>
Mon, 5 Nov 2012 22:53:33 +0000 (22:53 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Mon, 5 Nov 2012 22:53:33 +0000 (22:53 +0000)
 - The whole {File,Source}Manager is built around wanting to pre-determine the
   size of files, so we can't fit this in naturally. Instead, we handle it like
   we do STDIN, where we just replace the main file contents upfront.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@167419 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/FileManager.h
lib/Basic/FileManager.cpp
lib/Frontend/CompilerInstance.cpp

index b00f2b78270944a53ffad7a2f060f5a18adc927b..b2f578da7b49db85dded67e365fd6a184407b7ef 100644 (file)
@@ -103,6 +103,10 @@ public:
   bool operator<(const FileEntry &RHS) const {
     return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode);
   }
+
+  /// \brief Check whether the file is a named pipe (and thus can't be opened by
+  /// the native FileManager methods).
+  bool isNamedPipe() const;
 };
 
 /// \brief Implements support for file system lookup, file system caching,
index c6b894c7e2fe66830fca5c83408f62a17d562de7..6a0fbf80b905c57b1bc69a8248641d6a0e069c04 100644 (file)
@@ -57,6 +57,10 @@ FileEntry::~FileEntry() {
   if (FD != -1) ::close(FD);
 }
 
+bool FileEntry::isNamedPipe() const {
+  return FileMode & S_IFIFO;
+}
+
 //===----------------------------------------------------------------------===//
 // Windows.
 //===----------------------------------------------------------------------===//
index c6c5fb5fe2d58fe06a072b66b107cea1a6f1c9a5..b85832208b57125d3aed217a07d047a75d0c3a56 100644 (file)
@@ -610,6 +610,19 @@ bool CompilerInstance::InitializeSourceManager(StringRef InputFile,
       return false;
     }
     SourceMgr.createMainFileID(File, Kind);
+
+    // The natural SourceManager infrastructure can't currently handle named
+    // pipes, but we would at least like to accept them for the main
+    // file. Detect them here, read them with the more generic MemoryBuffer
+    // function, and simply override their contents as we do for STDIN.
+    if (File->isNamedPipe()) {
+      OwningPtr<llvm::MemoryBuffer> MB;
+      if (llvm::error_code ec = llvm::MemoryBuffer::getFile(InputFile, MB)) {
+        Diags.Report(diag::err_cannot_open_file) << InputFile << ec.message();
+        return false;
+      }
+      SourceMgr.overrideFileContents(File, MB.take());
+    }
   } else {
     OwningPtr<llvm::MemoryBuffer> SB;
     if (llvm::MemoryBuffer::getSTDIN(SB)) {