[cfe-commits] r95067 - /cfe/trunk/tools/CIndex/CIndexer.cpp

Daniel Dunbar daniel at zuster.org
Mon Feb 1 21:20:01 PST 2010


Author: ddunbar
Date: Mon Feb  1 23:19:57 2010
New Revision: 95067

URL: http://llvm.org/viewvc/llvm-project?rev=95067&view=rev
Log:
CIndex: Respect TMPDIR/TEMP/TMP when making temporary files for remapping. As a
side effect, this also fixes some cases on Windows where the file would end up
on a different drive, because tmpnam doesn't include the drive component. PR3837
strikes again.

Modified:
    cfe/trunk/tools/CIndex/CIndexer.cpp

Modified: cfe/trunk/tools/CIndex/CIndexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndexer.cpp?rev=95067&r1=95066&r2=95067&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndexer.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndexer.cpp Mon Feb  1 23:19:57 2010
@@ -95,16 +95,38 @@
   return P.str();
 }
 
+static llvm::sys::Path GetTemporaryPath() {
+  // FIXME: This is lame; sys::Path should provide this function (in particular,
+  // it should know how to find the temporary files dir).
+  std::string Error;
+  const char *TmpDir = ::getenv("TMPDIR");
+  if (!TmpDir)
+    TmpDir = ::getenv("TEMP");
+  if (!TmpDir)
+    TmpDir = ::getenv("TMP");
+  if (!TmpDir)
+    TmpDir = "/tmp";
+  llvm::sys::Path P(TmpDir);
+  P.appendComponent("remap");
+  if (P.makeUnique(false, &Error))
+    return llvm::sys::Path("");
+
+  // FIXME: Grumble, makeUnique sometimes leaves the file around!?  PR3837.
+  P.eraseFromDisk(false, 0);
+
+  return P;
+}
+
 bool clang::RemapFiles(unsigned num_unsaved_files,
                        struct CXUnsavedFile *unsaved_files,
                        std::vector<std::string> &RemapArgs,
                        std::vector<llvm::sys::Path> &TemporaryFiles) {
   for (unsigned i = 0; i != num_unsaved_files; ++i) {
-    char tmpFile[L_tmpnam];
-    char *tmpFileName = tmpnam(tmpFile);
-    
     // Write the contents of this unsaved file into the temporary file.
-    llvm::sys::Path SavedFile(tmpFileName);
+    llvm::sys::Path SavedFile(GetTemporaryPath());
+    if (SavedFile.empty())
+      return true;
+
     std::string ErrorInfo;
     llvm::raw_fd_ostream OS(SavedFile.c_str(), ErrorInfo);
     if (!ErrorInfo.empty())
@@ -120,7 +142,7 @@
     // Remap the file.
     std::string RemapArg = unsaved_files[i].Filename;
     RemapArg += ';';
-    RemapArg += tmpFileName;
+    RemapArg += SavedFile.str();
     RemapArgs.push_back("-Xclang");
     RemapArgs.push_back("-remap-file");
     RemapArgs.push_back("-Xclang");





More information about the cfe-commits mailing list