xref: /freebsd/contrib/llvm-project/clang/lib/Format/FormatInternal.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===--- FormatInternal.h - Format C++ code ---------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// This file declares Format APIs to be used internally by the
110b57cec5SDimitry Andric /// formatting library implementation.
120b57cec5SDimitry Andric ///
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #ifndef LLVM_CLANG_LIB_FORMAT_FORMATINTERNAL_H
160b57cec5SDimitry Andric #define LLVM_CLANG_LIB_FORMAT_FORMATINTERNAL_H
170b57cec5SDimitry Andric 
18*0fca6ea1SDimitry Andric #include "clang/Basic/LLVM.h"
19*0fca6ea1SDimitry Andric #include "clang/Format/Format.h"
20*0fca6ea1SDimitry Andric #include "clang/Tooling/Core/Replacement.h"
210b57cec5SDimitry Andric #include <utility>
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric namespace clang {
240b57cec5SDimitry Andric namespace format {
250b57cec5SDimitry Andric namespace internal {
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric /// Reformats the given \p Ranges in the code fragment \p Code.
280b57cec5SDimitry Andric ///
290b57cec5SDimitry Andric /// A fragment of code could conceptually be surrounded by other code that might
300b57cec5SDimitry Andric /// constrain how that fragment is laid out.
310b57cec5SDimitry Andric /// For example, consider the fragment of code between 'R"(' and ')"',
320b57cec5SDimitry Andric /// exclusive, in the following code:
330b57cec5SDimitry Andric ///
340b57cec5SDimitry Andric /// void outer(int x) {
350b57cec5SDimitry Andric ///   string inner = R"(name: data
360b57cec5SDimitry Andric ///                     ^ FirstStartColumn
370b57cec5SDimitry Andric ///     value: {
380b57cec5SDimitry Andric ///       x: 1
390b57cec5SDimitry Andric ///     ^ NextStartColumn
400b57cec5SDimitry Andric ///     }
410b57cec5SDimitry Andric ///   )";
420b57cec5SDimitry Andric ///   ^ LastStartColumn
430b57cec5SDimitry Andric /// }
440b57cec5SDimitry Andric ///
450b57cec5SDimitry Andric /// The outer code can influence the inner fragment as follows:
460b57cec5SDimitry Andric ///   * \p FirstStartColumn specifies the column at which \p Code starts.
470b57cec5SDimitry Andric ///   * \p NextStartColumn specifies the additional indent dictated by the
480b57cec5SDimitry Andric ///     surrounding code. It is applied to the rest of the lines of \p Code.
490b57cec5SDimitry Andric ///   * \p LastStartColumn specifies the column at which the last line of
500b57cec5SDimitry Andric ///     \p Code should end, in case the last line is an empty line.
510b57cec5SDimitry Andric ///
520b57cec5SDimitry Andric ///     In the case where the last line of the fragment contains content,
530b57cec5SDimitry Andric ///     the fragment ends at the end of that content and \p LastStartColumn is
540b57cec5SDimitry Andric ///     not taken into account, for example in:
550b57cec5SDimitry Andric ///
560b57cec5SDimitry Andric ///     void block() {
570b57cec5SDimitry Andric ///       string inner = R"(name: value)";
580b57cec5SDimitry Andric ///     }
590b57cec5SDimitry Andric ///
600b57cec5SDimitry Andric /// Each range is extended on either end to its next bigger logic unit, i.e.
610b57cec5SDimitry Andric /// everything that might influence its formatting or might be influenced by its
620b57cec5SDimitry Andric /// formatting.
630b57cec5SDimitry Andric ///
640b57cec5SDimitry Andric /// Returns a pair P, where:
650b57cec5SDimitry Andric ///   * P.first are the ``Replacements`` necessary to make all \p Ranges comply
660b57cec5SDimitry Andric ///     with \p Style.
670b57cec5SDimitry Andric ///   * P.second is the penalty induced by formatting the fragment \p Code.
680b57cec5SDimitry Andric ///     If the formatting of the fragment doesn't have a notion of penalty,
690b57cec5SDimitry Andric ///     returns 0.
700b57cec5SDimitry Andric ///
710b57cec5SDimitry Andric /// If ``Status`` is non-null, its value will be populated with the status of
720b57cec5SDimitry Andric /// this formatting attempt. See \c FormattingAttemptStatus.
730b57cec5SDimitry Andric std::pair<tooling::Replacements, unsigned>
740b57cec5SDimitry Andric reformat(const FormatStyle &Style, StringRef Code,
750b57cec5SDimitry Andric          ArrayRef<tooling::Range> Ranges, unsigned FirstStartColumn,
760b57cec5SDimitry Andric          unsigned NextStartColumn, unsigned LastStartColumn, StringRef FileName,
770b57cec5SDimitry Andric          FormattingAttemptStatus *Status);
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric } // namespace internal
800b57cec5SDimitry Andric } // namespace format
810b57cec5SDimitry Andric } // namespace clang
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric #endif
84