Author: djasper
Date: Wed Sep 30 08:59:29 2015
New Revision: 248904

URL: http://llvm.org/viewvc/llvm-project?rev=248904&view=rev
Log:
clang-format: Use Rewriter again to write the output files.

This has two advantages:
1. Atomic writes.
2. Proper handling of line endings (hopefully solving llvm.org/PR24999

Modified:
    cfe/trunk/tools/clang-format/ClangFormat.cpp

Modified: cfe/trunk/tools/clang-format/ClangFormat.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/ClangFormat.cpp?rev=248904&r1=248903&r2=248904&view=diff
==============================================================================
--- cfe/trunk/tools/clang-format/ClangFormat.cpp (original)
+++ cfe/trunk/tools/clang-format/ClangFormat.cpp Wed Sep 30 08:59:29 2015
@@ -19,6 +19,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/Version.h"
 #include "clang/Format/Format.h"
+#include "clang/Rewrite/Core/Rewriter.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
@@ -77,7 +78,7 @@ AssumeFileName("assume-filename",
                cl::desc("When reading from stdin, clang-format assumes this\n"
                         "filename to look for a style config file (with\n"
                         "-style=file) and to determine the language."),
-               cl::cat(ClangFormatCategory));
+               cl::init("<stdin>"), cl::cat(ClangFormatCategory));
 
 static cl::opt<bool> Inplace("i",
                              cl::desc("Inplace edit <file>s, if specified."),
@@ -109,8 +110,7 @@ namespace format {
 
 static FileID createInMemoryFile(StringRef FileName, MemoryBuffer *Source,
                                  SourceManager &Sources, FileManager &Files) {
-  const FileEntry *Entry = Files.getVirtualFile(FileName == "-" ? "<stdin>" :
-                                                    FileName,
+  const FileEntry *Entry = Files.getVirtualFile(FileName,
                                                 Source->getBufferSize(), 0);
   Sources.overrideFileContents(Entry, Source, true);
   return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
@@ -132,7 +132,7 @@ static bool fillRanges(MemoryBuffer *Cod
       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
       new DiagnosticOptions);
   SourceManager Sources(Diagnostics, Files);
-  FileID ID = createInMemoryFile("-", Code, Sources, Files);
+  FileID ID = createInMemoryFile("<irrelevant>", Code, Sources, Files);
   if (!LineRanges.empty()) {
     if (!Offsets.empty() || !Lengths.empty()) {
       llvm::errs() << "error: cannot use -lines with -offset/-length\n";
@@ -255,8 +255,8 @@ static bool format(StringRef FileName) {
 
   bool IncompleteFormat = false;
   Replaces = tooling::mergeReplacements(
-      Replaces,
-      reformat(FormatStyle, ChangedCode, Ranges, FileName, &IncompleteFormat));
+      Replaces, reformat(FormatStyle, ChangedCode, Ranges, AssumedFileName,
+                         &IncompleteFormat));
   if (OutputXML) {
     llvm::outs() << "<?xml version='1.0'?>\n<replacements "
                     "xml:space='preserve' incomplete_format='"
@@ -269,27 +269,26 @@ static bool format(StringRef FileName) {
     outputReplacementsXML(Replaces); 
     llvm::outs() << "</replacements>\n";
   } else {
-    std::string FormattedCode =
-        applyAllReplacements(Code->getBuffer(), Replaces);
+    FileManager Files((FileSystemOptions()));
+    DiagnosticsEngine Diagnostics(
+        IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
+        new DiagnosticOptions);
+    SourceManager Sources(Diagnostics, Files);
+    FileID ID = createInMemoryFile(AssumedFileName, Code.get(), Sources, 
Files);
+    Rewriter Rewrite(Sources, LangOptions());
+    tooling::applyAllReplacements(Replaces, Rewrite);
     if (Inplace) {
       if (FileName == "-")
         llvm::errs() << "error: cannot use -i when reading from stdin.\n";
-      else {
-        std::error_code EC;
-        raw_fd_ostream FileOut(FileName, EC, llvm::sys::fs::F_Text);
-        if (EC) {
-          llvm::errs() << EC.message() << "\n";
-          return true;
-        }
-        FileOut << FormattedCode;
-      }
+      else if (Rewrite.overwriteChangedFiles())
+        return true;
     } else {
       if (Cursor.getNumOccurrences() != 0)
         outs() << "{ \"Cursor\": "
                << tooling::shiftedCodePosition(Replaces, Cursor)
                << ", \"IncompleteFormat\": "
                << (IncompleteFormat ? "true" : "false") << " }\n";
-      outs() << FormattedCode;
+      Rewrite.getEditBuffer(ID).write(outs());
     }
   }
   return false;
@@ -350,3 +349,4 @@ int main(int argc, const char **argv) {
   }
   return Error ? 1 : 0;
 }
+


_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to