From eedbff6b679d9db2e7c1344ee704df2b71469e79 Mon Sep 17 00:00:00 2001 From: Daniel Jasper <djasper@google.com> Date: Sun, 10 May 2015 07:47:19 +0000 Subject: [PATCH] Reapply r236854 and fixed r236867. Makes emacs show a different message when clang-format encountered a syntax error. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@236943 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/Format/cursor.cpp | 2 +- test/Format/incomplete.cpp | 8 ++++++++ tools/clang-format/ClangFormat.cpp | 14 ++++++++++---- tools/clang-format/clang-format.el | 17 +++++++++++------ 4 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 test/Format/incomplete.cpp diff --git a/test/Format/cursor.cpp b/test/Format/cursor.cpp index d9aab5acb63..c7d576ba6e6 100644 --- a/test/Format/cursor.cpp +++ b/test/Format/cursor.cpp @@ -1,6 +1,6 @@ // RUN: grep -Ev "// *[A-Z-]+:" %s > %t2.cpp // RUN: clang-format -style=LLVM %t2.cpp -cursor=6 > %t.cpp // RUN: FileCheck -strict-whitespace -input-file=%t.cpp %s -// CHECK: {{^\{ "Cursor": 4 \}$}} +// CHECK: {{^\{ "Cursor": 4, }} // CHECK: {{^int\ \i;$}} int i; diff --git a/test/Format/incomplete.cpp b/test/Format/incomplete.cpp new file mode 100644 index 00000000000..bd2c74ca3c1 --- /dev/null +++ b/test/Format/incomplete.cpp @@ -0,0 +1,8 @@ +// RUN: grep -Ev "// *[A-Z-]+:" %s > %t2.cpp +// RUN: clang-format -style=LLVM %t2.cpp -cursor=0 > %t.cpp +// RUN: FileCheck -strict-whitespace -input-file=%t.cpp %s +// CHECK: {{"IncompleteFormat": true}} +// CHECK: {{^int\ \i;$}} + int i; +// CHECK: {{^f \( g \(;$}} +f ( g (; diff --git a/tools/clang-format/ClangFormat.cpp b/tools/clang-format/ClangFormat.cpp index fe7d678852d..5037e901f3b 100644 --- a/tools/clang-format/ClangFormat.cpp +++ b/tools/clang-format/ClangFormat.cpp @@ -225,14 +225,18 @@ static bool format(StringRef FileName) { FormatStyle FormatStyle = getStyle( Style, (FileName == "-") ? AssumeFilename : FileName, FallbackStyle); - tooling::Replacements Replaces = reformat(FormatStyle, Sources, ID, Ranges); + bool IncompleteFormat = false; + tooling::Replacements Replaces = + reformat(FormatStyle, Sources, ID, Ranges, &IncompleteFormat); if (OutputXML) { - llvm::outs() - << "<?xml version='1.0'?>\n<replacements xml:space='preserve'>\n"; + llvm::outs() << "<?xml version='1.0'?>\n<replacements " + "xml:space='preserve' incomplete_format='" + << (IncompleteFormat ? "true" : "false") << "'>\n"; if (Cursor.getNumOccurrences() != 0) llvm::outs() << "<cursor>" << tooling::shiftedCodePosition(Replaces, Cursor) << "</cursor>\n"; + for (tooling::Replacements::const_iterator I = Replaces.begin(), E = Replaces.end(); I != E; ++I) { @@ -254,7 +258,9 @@ static bool format(StringRef FileName) { } else { if (Cursor.getNumOccurrences() != 0) outs() << "{ \"Cursor\": " - << tooling::shiftedCodePosition(Replaces, Cursor) << " }\n"; + << tooling::shiftedCodePosition(Replaces, Cursor) + << ", \"IncompleteFormat\": " + << (IncompleteFormat ? "true" : "false") << " }\n"; Rewrite.getEditBuffer(ID).write(outs()); } } diff --git a/tools/clang-format/clang-format.el b/tools/clang-format/clang-format.el index add2436bbe7..ca461444e22 100644 --- a/tools/clang-format/clang-format.el +++ b/tools/clang-format/clang-format.el @@ -61,6 +61,7 @@ of the buffer." (unless (and (listp xml-node) (eq (xml-node-name xml-node) 'replacements)) (error "Expected <replacements> node")) (let ((nodes (xml-node-children xml-node)) + (incomplete-format (xml-get-attribute xml-node 'incomplete_format)) replacements cursor) (dolist (node nodes) @@ -89,7 +90,7 @@ of the buffer." (and (= (car a) (car b)) (> (cadr a) (cadr b))))))) - (cons replacements cursor))) + (list replacements cursor (string= incomplete-format "true")))) (defun clang-format--replace (offset length &optional text) (let ((start (byte-to-position (1+ offset))) @@ -142,20 +143,24 @@ is no active region. If no style is given uses `clang-format-style'." ((stringp status) (error "(clang-format killed by signal %s%s)" status stderr)) ((not (equal 0 status)) - (error "(clang-format failed with code %d%s)" status stderr)) - (t (message "(clang-format succeeded%s)" stderr))) + (error "(clang-format failed with code %d%s)" status stderr))) (with-current-buffer temp-buffer (setq operations (clang-format--extract (car (xml-parse-region))))) - (let ((replacements (car operations)) - (cursor (cdr operations))) + (let ((replacements (nth 0 operations)) + (cursor (nth 1 operations)) + (incomplete-format (nth 2 operations))) (save-excursion (mapc (lambda (rpl) (apply #'clang-format--replace rpl)) replacements)) (when cursor - (goto-char (byte-to-position (1+ cursor)))))) + (goto-char (byte-to-position (1+ cursor)))) + (message "%s" incomplete-format) + (if incomplete-format + (message "(clang-format: incomplete (syntax errors)%s)" stderr) + (message "(clang-format: success%s)" stderr)))) (delete-file temp-file) (when (buffer-name temp-buffer) (kill-buffer temp-buffer))))) -- GitLab