clang-apply-replacements doesn't handle windows newlines correctly

Nikola Smiljanic popizdeh at gmail.com
Wed Dec 3 18:43:39 PST 2014


I think it'll only happen when you run it on Windows. Writing files in text
mode will translate each \n into CRLF on this silly platform :) I'm
honestly surprised that nobody noticed this before. I filed a bug for
clang-modernize for this https://cpp11-migrate.atlassian.net/browse/CM-171

On Thu, Dec 4, 2014 at 1:29 AM, Alexander Kornienko <alexfh at google.com>
wrote:

> That may be a good solution, however, could you provide a bit more
> details: does this problem happen when you run clang-apply-replacements on
> Windows or on Linux or Mac as well? It would also be nice if we could have
> a test for this.
>
> On Wed, Dec 3, 2014 at 4:13 AM, Nikola Smiljanic <popizdeh at gmail.com>
> wrote:
>
>> I'm not entirely sure this is the right way to fix the issue, but since
>> SourceManager reads \r \n we should probably write them without newline
>> translation. What happens now is, newline is read as \r \n and when it's
>> written out the \n is translated so we end up with duplicated \r.
>>
>>
>>
>> _______________________________________________
>> cfe-commits mailing list
>> cfe-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20141204/8bdb294f/attachment.html>
-------------- next part --------------
diff --git a/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp b/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
index fcd1a55..607b665 100644
--- a/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
+++ b/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
@@ -270,7 +270,7 @@ int main(int argc, char **argv) {
 
     // Write new file to disk
     std::error_code EC;
-    llvm::raw_fd_ostream FileStream(FileName, EC, llvm::sys::fs::F_Text);
+    llvm::raw_fd_ostream FileStream(FileName, EC, llvm::sys::fs::F_None);
     if (EC) {
       llvm::errs() << "Could not open " << FileName << " for writing\n";
       continue;
diff --git a/test/clang-apply-replacements/Inputs/crlf/crlf.cpp b/test/clang-apply-replacements/Inputs/crlf/crlf.cpp
new file mode 100644
index 0000000..c531b16
--- /dev/null
+++ b/test/clang-apply-replacements/Inputs/crlf/crlf.cpp
@@ -0,0 +1,4 @@
+
+void foo() {
+  int *x = 0;
+}
diff --git a/test/clang-apply-replacements/Inputs/crlf/crlf.cpp.expected b/test/clang-apply-replacements/Inputs/crlf/crlf.cpp.expected
new file mode 100644
index 0000000..45044cf
--- /dev/null
+++ b/test/clang-apply-replacements/Inputs/crlf/crlf.cpp.expected
@@ -0,0 +1,4 @@
+
+void foo() {
+  int *x = nullptr;
+}
diff --git a/test/clang-apply-replacements/Inputs/crlf/file1.yaml b/test/clang-apply-replacements/Inputs/crlf/file1.yaml
new file mode 100644
index 0000000..846f299
--- /dev/null
+++ b/test/clang-apply-replacements/Inputs/crlf/file1.yaml
@@ -0,0 +1,8 @@
+---
+MainSourceFile:      source1.cpp
+Replacements:    
+  - FilePath:        $(path)/crlf.cpp
+    Offset:          27
+    Length:          1
+    ReplacementText: nullptr
+...
diff --git a/test/clang-apply-replacements/crlf.cpp b/test/clang-apply-replacements/crlf.cpp
new file mode 100644
index 0000000..a2d6ad6
--- /dev/null
+++ b/test/clang-apply-replacements/crlf.cpp
@@ -0,0 +1,6 @@
+// RUN: mkdir -p %T/Inputs/crlf
+// RUN: cp %S/Inputs/crlf/crlf.cpp %T/Inputs/crlf/crlf.cpp
+// RUN: cp %S/Inputs/crlf/crlf.cpp.expected %T/Inputs/crlf/crlf.cpp.expected
+// RUN: sed "s#\$(path)#%/T/Inputs/crlf#" %S/Inputs/crlf/file1.yaml > %T/Inputs/crlf/file1.yaml
+// RUN: clang-apply-replacements %T/Inputs/crlf
+// RUN: diff %T/Inputs/crlf/crlf.cpp %T/Inputs/crlf/crlf.cpp.expected


More information about the cfe-commits mailing list