[PATCH] D19717: [find-all-symbols] Fix racy yaml file writing.

Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 29 03:22:23 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL268021: [find-all-symbols] Fix racy yaml file writing. (authored by d0k).

Changed prior to commit:
  http://reviews.llvm.org/D19717?vs=55559&id=55562#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D19717

Files:
  clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.cpp
  clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.h
  clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp

Index: clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.h
===================================================================
--- clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.h
+++ clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.h
@@ -12,6 +12,7 @@
 
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/raw_ostream.h"
 #include <set>
 #include <string>
 #include <vector>
@@ -87,9 +88,9 @@
   bool operator<(const SymbolInfo &Symbol) const;
 };
 
-/// \brief Write SymbolInfos to a single file (YAML format).
-bool WriteSymboInfosToFile(llvm::StringRef FilePath,
-                           const std::set<SymbolInfo> &Symbols);
+/// \brief Write SymbolInfos to a stream (YAML format).
+bool WriteSymbolInfosToStream(llvm::raw_ostream &OS,
+                              const std::set<SymbolInfo> &Symbols);
 
 /// \brief Read SymbolInfos from a YAML document.
 std::vector<SymbolInfo> ReadSymbolInfosFromYAML(llvm::StringRef Yaml);
Index: clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.cpp
===================================================================
--- clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.cpp
+++ clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.cpp
@@ -97,16 +97,11 @@
          std::tie(Symbol.Name, Symbol.FilePath, Symbol.LineNumber);
 }
 
-bool WriteSymboInfosToFile(llvm::StringRef FilePath,
-                           const std::set<SymbolInfo> &Symbols) {
-  int FD = 0;
-  if (llvm::sys::fs::openFileForWrite(FilePath, FD, llvm::sys::fs::F_None))
-    return false;
-  llvm::raw_fd_ostream OS(FD, true);
+bool WriteSymbolInfosToStream(llvm::raw_ostream &OS,
+                              const std::set<SymbolInfo> &Symbols) {
   llvm::yaml::Output yout(OS);
   for (auto Symbol : Symbols)
     yout << Symbol;
-  OS.close();
   return true;
 }
 
Index: clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
===================================================================
--- clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
+++ clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
@@ -59,10 +59,13 @@
 
   void Write(const std::string &Dir) {
     for (const auto &Symbol : Symbols) {
-      SmallString<256> FilePath(Dir);
-      llvm::sys::path::append(
-          FilePath, llvm::sys::path::filename(Symbol.first) + ".yaml");
-      WriteSymboInfosToFile(FilePath, Symbol.second);
+      int FD;
+      SmallString<128> ResultPath;
+      llvm::sys::fs::createUniqueFile(
+          Dir + "/" + llvm::sys::path::filename(Symbol.first) + "-%%%%%%.yaml",
+          FD, ResultPath);
+      llvm::raw_fd_ostream OS(FD, /*shouldClose=*/true);
+      WriteSymbolInfosToStream(OS, Symbol.second);
     }
   }
 
@@ -90,7 +93,13 @@
       UniqueSymbols.insert(Symbol);
   }
 
-  WriteSymboInfosToFile(OutputFile, UniqueSymbols);
+  llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_None);
+  if (EC) {
+    llvm::errs() << "Cann't open '" << OutputFile << "': " << EC.message()
+                 << '\n';
+    return false;
+  }
+  WriteSymbolInfosToStream(OS, UniqueSymbols);
   return true;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19717.55562.patch
Type: text/x-patch
Size: 3279 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160429/46089a4a/attachment-0001.bin>


More information about the cfe-commits mailing list