[PATCH] clang-replace: Afford applying replacements in memory

Edwin Vane edwin.vane at intel.com
Mon Aug 26 11:57:59 PDT 2013


Hi klimek,

For users of libclangReplace, this patch affords the ability to apply
replacements in memory instead of writing to disk.

http://llvm-reviews.chandlerc.com/D1519

Files:
  clang-replace/ApplyReplacements.cpp
  clang-replace/ApplyReplacements.h

Index: clang-replace/ApplyReplacements.cpp
===================================================================
--- clang-replace/ApplyReplacements.cpp
+++ clang-replace/ApplyReplacements.cpp
@@ -189,7 +189,8 @@
 }
 
 bool applyReplacements(const FileToReplacementsMap &GroupedReplacements,
-                       clang::SourceManager &SM) {
+                       clang::SourceManager &SM,
+                       llvm::StringMap<std::string> *OutputState) {
   Rewriter Rewrites(SM, LangOptions());
 
   // Apply all changes
@@ -200,19 +201,26 @@
       return false;
   }
 
-  // Write all changes to disk
+  // Write all changes to preferred destination
   for (Rewriter::buffer_iterator BufferI = Rewrites.buffer_begin(),
                                  BufferE = Rewrites.buffer_end();
        BufferI != BufferE; ++BufferI) {
-    std::string ErrorInfo;
     const char *FileName = SM.getFileEntryForID(BufferI->first)->getName();
-    llvm::raw_fd_ostream FileStream(FileName, ErrorInfo,
-                                    llvm::sys::fs::F_Binary);
-    if (!ErrorInfo.empty()) {
-      errs() << "Unable to open " << FileName << " for writing\n";
-      return false;
+
+    if (OutputState) {
+      llvm::raw_string_ostream StringStream((*OutputState)[FileName]);
+      BufferI->second.write(StringStream);
+      StringStream.flush();
+    } else {
+      std::string ErrorInfo;
+      llvm::raw_fd_ostream FileStream(FileName, ErrorInfo,
+                                      llvm::sys::fs::F_Binary);
+      if (!ErrorInfo.empty()) {
+        errs() << "Unable to open " << FileName << " for writing\n";
+        return false;
+      }
+      BufferI->second.write(FileStream);
     }
-    BufferI->second.write(FileStream);
   }
 
   return true;
Index: clang-replace/ApplyReplacements.h
===================================================================
--- clang-replace/ApplyReplacements.h
+++ clang-replace/ApplyReplacements.h
@@ -76,11 +76,15 @@
 /// \param[in] GroupedReplacements Deduplicated and conflict free Replacements
 /// to apply.
 /// \param[in] SM SourceManager required to construct clang::Rewriter.
+/// \param[out] OutputState The results of applying replacements
+/// will be written into this map if it's provided. The map keys are filenames
+/// and the map values are file contents.
 ///
 /// \returns \li true If all changes were applied successfully.
 ///          \li false If a replacement failed to apply.
 bool applyReplacements(const FileToReplacementsMap &GroupedReplacements,
-                       clang::SourceManager &SM);
+                       clang::SourceManager &SM,
+                       llvm::StringMap<std::string> *OutputState = 0);
 
 } // end namespace replace
 } // end namespace clang
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1519.1.patch
Type: text/x-patch
Size: 2761 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130826/a0130508/attachment.bin>


More information about the cfe-commits mailing list