diff options
Diffstat (limited to 'tools/clang-format')
-rw-r--r-- | tools/clang-format/CMakeLists.txt | 3 | ||||
-rw-r--r-- | tools/clang-format/ClangFormat.cpp | 41 | ||||
-rw-r--r-- | tools/clang-format/clang-format.el | 45 | ||||
-rw-r--r-- | tools/clang-format/clang-format.py | 2 | ||||
-rw-r--r-- | tools/clang-format/fuzzer/CMakeLists.txt | 7 | ||||
-rw-r--r-- | tools/clang-format/fuzzer/ClangFormatFuzzer.cpp | 7 |
6 files changed, 64 insertions, 41 deletions
diff --git a/tools/clang-format/CMakeLists.txt b/tools/clang-format/CMakeLists.txt index a13633eaefc4..a295e8cd0b2a 100644 --- a/tools/clang-format/CMakeLists.txt +++ b/tools/clang-format/CMakeLists.txt @@ -12,10 +12,11 @@ set(CLANG_FORMAT_LIB_DEPS ) target_link_libraries(clang-format + PRIVATE ${CLANG_FORMAT_LIB_DEPS} ) -if( LLVM_USE_SANITIZE_COVERAGE ) +if( LLVM_LIB_FUZZING_ENGINE OR LLVM_USE_SANITIZE_COVERAGE ) add_subdirectory(fuzzer) endif() diff --git a/tools/clang-format/ClangFormat.cpp b/tools/clang-format/ClangFormat.cpp index 14bff19a1a0c..b7179ffd6416 100644 --- a/tools/clang-format/ClangFormat.cpp +++ b/tools/clang-format/ClangFormat.cpp @@ -102,6 +102,10 @@ static cl::opt<bool> SortIncludes( "SortIncludes style flag"), cl::cat(ClangFormatCategory)); +static cl::opt<bool> + Verbose("verbose", cl::desc("If set, shows the list of processed files"), + cl::cat(ClangFormatCategory)); + static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"), cl::cat(ClangFormatCategory)); @@ -285,7 +289,7 @@ static bool format(StringRef FileName) { "xml:space='preserve' incomplete_format='" << (Status.FormatComplete ? "false" : "true") << "'"; if (!Status.FormatComplete) - outs() << " line=" << Status.Line; + outs() << " line='" << Status.Line << "'"; outs() << ">\n"; if (Cursor.getNumOccurrences() != 0) outs() << "<cursor>" @@ -328,8 +332,7 @@ static bool format(StringRef FileName) { } // namespace format } // namespace clang -static void PrintVersion() { - raw_ostream &OS = outs(); +static void PrintVersion(raw_ostream &OS) { OS << clang::getClangToolFullVersion("clang-format") << '\n'; } @@ -348,8 +351,10 @@ int main(int argc, const char **argv) { "together with <file>s, the files are edited in-place. Otherwise, the\n" "result is written to the standard output.\n"); - if (Help) + if (Help) { cl::PrintHelpMessage(); + return 0; + } if (DumpConfig) { llvm::Expected<clang::format::FormatStyle> FormatStyle = @@ -366,23 +371,19 @@ int main(int argc, const char **argv) { } bool Error = false; - switch (FileNames.size()) { - case 0: + if (FileNames.empty()) { Error = clang::format::format("-"); - break; - case 1: - Error = clang::format::format(FileNames[0]); - break; - default: - if (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty()) { - errs() << "error: -offset, -length and -lines can only be used for " - "single file.\n"; - return 1; - } - for (unsigned i = 0; i < FileNames.size(); ++i) - Error |= clang::format::format(FileNames[i]); - break; + return Error ? 1 : 0; + } + if (FileNames.size() != 1 && (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) { + errs() << "error: -offset, -length and -lines can only be used for " + "single file.\n"; + return 1; + } + for (const auto &FileName : FileNames) { + if (Verbose) + errs() << "Formatting " << FileName << "\n"; + Error |= clang::format::format(FileName); } return Error ? 1 : 0; } - diff --git a/tools/clang-format/clang-format.el b/tools/clang-format/clang-format.el index aa9c3ff4ca0b..6c626e0b83d1 100644 --- a/tools/clang-format/clang-format.el +++ b/tools/clang-format/clang-format.el @@ -119,10 +119,12 @@ is a zero-based file offset, assuming ‘utf-8-unix’ coding." (byte-to-position (1+ byte))))) ;;;###autoload -(defun clang-format-region (start end &optional style) +(defun clang-format-region (start end &optional style assume-file-name) "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'." +If called interactively uses the region or the current statement if there is no +no active region. If no STYLE is given uses `clang-format-style'. Use +ASSUME-FILE-NAME to locate a style config file, if no ASSUME-FILE-NAME is given +uses the function `buffer-file-name'." (interactive (if (use-region-p) (list (region-beginning) (region-end)) @@ -131,6 +133,9 @@ is no active region. If no style is given uses `clang-format-style'." (unless style (setq style clang-format-style)) + (unless assume-file-name + (setq assume-file-name buffer-file-name)) + (let ((file-start (clang-format--bufferpos-to-filepos start 'approximate 'utf-8-unix)) (file-end (clang-format--bufferpos-to-filepos end 'approximate @@ -144,16 +149,21 @@ is no active region. If no style is given uses `clang-format-style'." ;; always use ‘utf-8-unix’ and ignore the buffer coding system. (default-process-coding-system '(utf-8-unix . utf-8-unix))) (unwind-protect - (let ((status (call-process-region - nil nil clang-format-executable - nil `(,temp-buffer ,temp-file) nil - - "-output-replacements-xml" - "-assume-filename" (or (buffer-file-name) "") - "-style" style - "-offset" (number-to-string file-start) - "-length" (number-to-string (- file-end file-start)) - "-cursor" (number-to-string cursor))) + (let ((status (apply #'call-process-region + nil nil clang-format-executable + nil `(,temp-buffer ,temp-file) nil + `("-output-replacements-xml" + ;; Gaurd against a nil assume-file-name. + ;; If the clang-format option -assume-filename + ;; is given a blank string it will crash as per + ;; the following bug report + ;; https://bugs.llvm.org/show_bug.cgi?id=34667 + ,@(and assume-file-name + (list "-assume-filename" assume-file-name)) + "-style" ,style + "-offset" ,(number-to-string file-start) + "-length" ,(number-to-string (- file-end file-start)) + "-cursor" ,(number-to-string cursor)))) (stderr (with-temp-buffer (unless (zerop (cadr (insert-file-contents temp-file))) (insert ": ")) @@ -181,10 +191,13 @@ is no active region. If no style is given uses `clang-format-style'." (when (buffer-name temp-buffer) (kill-buffer temp-buffer))))) ;;;###autoload -(defun clang-format-buffer (&optional style) - "Use clang-format to format the current buffer according to STYLE." +(defun clang-format-buffer (&optional style assume-file-name) + "Use clang-format to format the current buffer according to STYLE. +If no STYLE is given uses `clang-format-style'. Use ASSUME-FILE-NAME +to locate a style config file. If no ASSUME-FILE-NAME is given uses +the function `buffer-file-name'." (interactive) - (clang-format-region (point-min) (point-max) style)) + (clang-format-region (point-min) (point-max) style assume-file-name)) ;;;###autoload (defalias 'clang-format 'clang-format-region) diff --git a/tools/clang-format/clang-format.py b/tools/clang-format/clang-format.py index 187125ed09a8..5fe592a9202b 100644 --- a/tools/clang-format/clang-format.py +++ b/tools/clang-format/clang-format.py @@ -92,7 +92,7 @@ def main(): # Call formatter. command = [binary, '-style', style, '-cursor', str(cursor)] - if lines != 'all': + if lines != ['-lines', 'all']: command += lines if fallback_style: command.extend(['-fallback-style', fallback_style]) diff --git a/tools/clang-format/fuzzer/CMakeLists.txt b/tools/clang-format/fuzzer/CMakeLists.txt index c7772fcb2f01..87ae05b62d19 100644 --- a/tools/clang-format/fuzzer/CMakeLists.txt +++ b/tools/clang-format/fuzzer/CMakeLists.txt @@ -1,11 +1,16 @@ set(LLVM_LINK_COMPONENTS support) +if(LLVM_USE_SANITIZE_COVERAGE) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer") +endif() + add_clang_executable(clang-format-fuzzer EXCLUDE_FROM_ALL ClangFormatFuzzer.cpp ) target_link_libraries(clang-format-fuzzer + PRIVATE ${CLANG_FORMAT_LIB_DEPS} - LLVMFuzzer + ${LLVM_LIB_FUZZING_ENGINE} ) diff --git a/tools/clang-format/fuzzer/ClangFormatFuzzer.cpp b/tools/clang-format/fuzzer/ClangFormatFuzzer.cpp index 5334ce873eca..d440a6124b67 100644 --- a/tools/clang-format/fuzzer/ClangFormatFuzzer.cpp +++ b/tools/clang-format/fuzzer/ClangFormatFuzzer.cpp @@ -20,7 +20,10 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { 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())})); + auto Replaces = reformat(Style, s, clang::tooling::Range(0, s.size())); + auto Result = applyAllReplacements(s, Replaces); + + // Output must be checked, as otherwise we crash. + if (!Result) {} return 0; } |