diff options
author | dim <dim@FreeBSD.org> | 2015-05-27 18:47:56 +0000 |
---|---|---|
committer | dim <dim@FreeBSD.org> | 2015-05-27 18:47:56 +0000 |
commit | 3191b2b32a96e1a6ee833fcca73e5c8e0c67ba65 (patch) | |
tree | dbbd4047878da71c1a706e26ce05b4e7791b14cc /tools/clang-format | |
parent | 38d6f2e7f2ce51a5b3836d26596c6c34a3288752 (diff) | |
download | FreeBSD-src-3191b2b32a96e1a6ee833fcca73e5c8e0c67ba65.zip FreeBSD-src-3191b2b32a96e1a6ee833fcca73e5c8e0c67ba65.tar.gz |
Vendor import of clang trunk r238337:
https://llvm.org/svn/llvm-project/cfe/trunk@238337
Diffstat (limited to 'tools/clang-format')
-rw-r--r-- | tools/clang-format/CMakeLists.txt | 10 | ||||
-rw-r--r-- | tools/clang-format/ClangFormat.cpp | 28 | ||||
-rwxr-xr-x | tools/clang-format/clang-format-diff.py | 2 | ||||
-rw-r--r-- | tools/clang-format/clang-format.el | 42 | ||||
-rw-r--r-- | tools/clang-format/clang-format.py | 5 | ||||
-rw-r--r-- | tools/clang-format/fuzzer/CMakeLists.txt | 11 | ||||
-rw-r--r-- | tools/clang-format/fuzzer/ClangFormatFuzzer.cpp | 25 |
7 files changed, 91 insertions, 32 deletions
diff --git a/tools/clang-format/CMakeLists.txt b/tools/clang-format/CMakeLists.txt index 6a24e13..6ef0c22 100644 --- a/tools/clang-format/CMakeLists.txt +++ b/tools/clang-format/CMakeLists.txt @@ -4,13 +4,21 @@ add_clang_executable(clang-format ClangFormat.cpp ) -target_link_libraries(clang-format +set(CLANG_FORMAT_LIB_DEPS clangBasic clangFormat clangRewrite clangToolingCore ) +target_link_libraries(clang-format + ${CLANG_FORMAT_LIB_DEPS} + ) + +if( LLVM_USE_SANITIZE_COVERAGE ) + add_subdirectory(fuzzer) +endif() + install(TARGETS clang-format RUNTIME DESTINATION bin) install(PROGRAMS clang-format-bbedit.applescript DESTINATION share/clang) install(PROGRAMS clang-format-diff.py DESTINATION share/clang) diff --git a/tools/clang-format/ClangFormat.cpp b/tools/clang-format/ClangFormat.cpp index d44d407..5037e90 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) { @@ -247,12 +251,16 @@ static bool format(StringRef FileName) { Rewriter Rewrite(Sources, LangOptions()); tooling::applyAllReplacements(Replaces, Rewrite); if (Inplace) { - if (Rewrite.overwriteChangedFiles()) + if (FileName == "-") + llvm::errs() << "error: cannot use -i when reading from stdin.\n"; + else if (Rewrite.overwriteChangedFiles()) return true; } 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()); } } @@ -270,15 +278,7 @@ static void PrintVersion() { int main(int argc, const char **argv) { llvm::sys::PrintStackTraceOnErrorSignal(); - // Hide unrelated options. - StringMap<cl::Option*> Options; - cl::getRegisteredOptions(Options); - for (StringMap<cl::Option *>::iterator I = Options.begin(), E = Options.end(); - I != E; ++I) { - if (I->second->Category != &ClangFormatCategory && I->first() != "help" && - I->first() != "version") - I->second->setHiddenFlag(cl::ReallyHidden); - } + cl::HideUnrelatedOptions(ClangFormatCategory); cl::SetVersionPrinter(PrintVersion); cl::ParseCommandLineOptions( diff --git a/tools/clang-format/clang-format-diff.py b/tools/clang-format/clang-format-diff.py index 23adb07..64efb83 100755 --- a/tools/clang-format/clang-format-diff.py +++ b/tools/clang-format/clang-format-diff.py @@ -48,7 +48,7 @@ def main(): help='custom pattern selecting file paths to reformat ' '(case sensitive, overrides -iregex)') parser.add_argument('-iregex', metavar='PATTERN', default= - r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc|js|proto' + r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc|js|ts|proto' r'|protodevel|java)', help='custom pattern selecting file paths to reformat ' '(case insensitive, overridden by -regex)') diff --git a/tools/clang-format/clang-format.el b/tools/clang-format/clang-format.el index ab0991b..ca46144 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) @@ -76,11 +77,11 @@ of the buffer." (when (cdr children) (error "More than one child node in <replacement> node")) - (setq offset (1+ (string-to-number offset))) + (setq offset (string-to-number offset)) (setq length (string-to-number length)) (push (list offset length text) replacements))) ('cursor - (setq cursor (1+ (string-to-number text)))))))) + (setq cursor (string-to-number text))))))) ;; Sort by decreasing offset, length. (setq replacements (sort (delq nil replacements) @@ -89,16 +90,18 @@ 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) - (goto-char offset) - (delete-char length) - (when text - (insert text))) + (let ((start (byte-to-position (1+ offset))) + (end (byte-to-position (+ 1 offset length)))) + (goto-char start) + (delete-region start end) + (when text + (insert text)))) ;;;###autoload -(defun clang-format-region (start end &optional style) +(defun clang-format-region (char-start char-end &optional style) "Use clang-format to format the code between START and END according to STYLE. If called interactively uses the region or the current statement if there is no active region. If no style is given uses `clang-format-style'." @@ -110,7 +113,10 @@ is no active region. If no style is given uses `clang-format-style'." (unless style (setq style clang-format-style)) - (let ((temp-buffer (generate-new-buffer " *clang-format-temp*")) + (let ((start (1- (position-bytes char-start))) + (end (1- (position-bytes char-end))) + (cursor (1- (position-bytes (point)))) + (temp-buffer (generate-new-buffer " *clang-format-temp*")) (temp-file (make-temp-file "clang-format"))) (unwind-protect (let (status stderr operations) @@ -122,9 +128,9 @@ is no active region. If no style is given uses `clang-format-style'." "-output-replacements-xml" "-assume-filename" (or (buffer-file-name) "") "-style" style - "-offset" (number-to-string (1- start)) + "-offset" (number-to-string start) "-length" (number-to-string (- end start)) - "-cursor" (number-to-string (1- (point))))) + "-cursor" (number-to-string cursor))) (setq stderr (with-temp-buffer (insert-file-contents temp-file) @@ -137,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 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))))) diff --git a/tools/clang-format/clang-format.py b/tools/clang-format/clang-format.py index a79205a..56a6e5d 100644 --- a/tools/clang-format/clang-format.py +++ b/tools/clang-format/clang-format.py @@ -34,6 +34,9 @@ if vim.eval('exists("g:clang_format_path")') == "1": # a '.clang-format' or '_clang-format' file to indicate the style that should be # used. style = 'file' +fallback_style = None +if vim.eval('exists("g:clang_format_fallback_style")') == "1": + fallback_style = vim.eval('g:clang_format_fallback_style') def main(): # Get the current text. @@ -58,6 +61,8 @@ def main(): # Call formatter. command = [binary, '-lines', lines, '-style', style, '-cursor', str(cursor)] + if fallback_style: + command.extend(['-fallback-style', fallback_style]) if vim.current.buffer.name: command.extend(['-assume-filename', vim.current.buffer.name]) p = subprocess.Popen(command, diff --git a/tools/clang-format/fuzzer/CMakeLists.txt b/tools/clang-format/fuzzer/CMakeLists.txt new file mode 100644 index 0000000..c7772fc --- /dev/null +++ b/tools/clang-format/fuzzer/CMakeLists.txt @@ -0,0 +1,11 @@ +set(LLVM_LINK_COMPONENTS support) + +add_clang_executable(clang-format-fuzzer + EXCLUDE_FROM_ALL + ClangFormatFuzzer.cpp + ) + +target_link_libraries(clang-format-fuzzer + ${CLANG_FORMAT_LIB_DEPS} + LLVMFuzzer + ) diff --git a/tools/clang-format/fuzzer/ClangFormatFuzzer.cpp b/tools/clang-format/fuzzer/ClangFormatFuzzer.cpp new file mode 100644 index 0000000..fe4941a --- /dev/null +++ b/tools/clang-format/fuzzer/ClangFormatFuzzer.cpp @@ -0,0 +1,25 @@ +//===-- ClangFormatFuzzer.cpp - Fuzz the Clang format tool ----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// \brief This file implements a function that runs Clang format on a single +/// input. This function is then linked into the Fuzzer library. +/// +//===----------------------------------------------------------------------===// + +#include "clang/Format/Format.h" + +extern "C" void LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { + // FIXME: fuzz more things: different styles, different style features. + std::string s((const char *)data, size); + auto Style = getGoogleStyle(clang::format::FormatStyle::LK_Cpp); + Style.ColumnLimit = 60; + applyAllReplacements(s, clang::format::reformat( + Style, s, {clang::tooling::Range(0, s.size())})); +} |