xref: /openbsd-src/gnu/llvm/clang/lib/Format/FormatInternal.h (revision a9ac8606c53d55cee9c3a39778b249c51df111ef)
1*e5dd7070Spatrick //===--- FormatInternal.h - Format C++ code ---------------------*- C++ -*-===//
2*e5dd7070Spatrick //
3*e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e5dd7070Spatrick //
7*e5dd7070Spatrick //===----------------------------------------------------------------------===//
8*e5dd7070Spatrick ///
9*e5dd7070Spatrick /// \file
10*e5dd7070Spatrick /// This file declares Format APIs to be used internally by the
11*e5dd7070Spatrick /// formatting library implementation.
12*e5dd7070Spatrick ///
13*e5dd7070Spatrick //===----------------------------------------------------------------------===//
14*e5dd7070Spatrick 
15*e5dd7070Spatrick #ifndef LLVM_CLANG_LIB_FORMAT_FORMATINTERNAL_H
16*e5dd7070Spatrick #define LLVM_CLANG_LIB_FORMAT_FORMATINTERNAL_H
17*e5dd7070Spatrick 
18*e5dd7070Spatrick #include "BreakableToken.h"
19*e5dd7070Spatrick #include <utility>
20*e5dd7070Spatrick 
21*e5dd7070Spatrick namespace clang {
22*e5dd7070Spatrick namespace format {
23*e5dd7070Spatrick namespace internal {
24*e5dd7070Spatrick 
25*e5dd7070Spatrick /// Reformats the given \p Ranges in the code fragment \p Code.
26*e5dd7070Spatrick ///
27*e5dd7070Spatrick /// A fragment of code could conceptually be surrounded by other code that might
28*e5dd7070Spatrick /// constrain how that fragment is laid out.
29*e5dd7070Spatrick /// For example, consider the fragment of code between 'R"(' and ')"',
30*e5dd7070Spatrick /// exclusive, in the following code:
31*e5dd7070Spatrick ///
32*e5dd7070Spatrick /// void outer(int x) {
33*e5dd7070Spatrick ///   string inner = R"(name: data
34*e5dd7070Spatrick ///                     ^ FirstStartColumn
35*e5dd7070Spatrick ///     value: {
36*e5dd7070Spatrick ///       x: 1
37*e5dd7070Spatrick ///     ^ NextStartColumn
38*e5dd7070Spatrick ///     }
39*e5dd7070Spatrick ///   )";
40*e5dd7070Spatrick ///   ^ LastStartColumn
41*e5dd7070Spatrick /// }
42*e5dd7070Spatrick ///
43*e5dd7070Spatrick /// The outer code can influence the inner fragment as follows:
44*e5dd7070Spatrick ///   * \p FirstStartColumn specifies the column at which \p Code starts.
45*e5dd7070Spatrick ///   * \p NextStartColumn specifies the additional indent dictated by the
46*e5dd7070Spatrick ///     surrounding code. It is applied to the rest of the lines of \p Code.
47*e5dd7070Spatrick ///   * \p LastStartColumn specifies the column at which the last line of
48*e5dd7070Spatrick ///     \p Code should end, in case the last line is an empty line.
49*e5dd7070Spatrick ///
50*e5dd7070Spatrick ///     In the case where the last line of the fragment contains content,
51*e5dd7070Spatrick ///     the fragment ends at the end of that content and \p LastStartColumn is
52*e5dd7070Spatrick ///     not taken into account, for example in:
53*e5dd7070Spatrick ///
54*e5dd7070Spatrick ///     void block() {
55*e5dd7070Spatrick ///       string inner = R"(name: value)";
56*e5dd7070Spatrick ///     }
57*e5dd7070Spatrick ///
58*e5dd7070Spatrick /// Each range is extended on either end to its next bigger logic unit, i.e.
59*e5dd7070Spatrick /// everything that might influence its formatting or might be influenced by its
60*e5dd7070Spatrick /// formatting.
61*e5dd7070Spatrick ///
62*e5dd7070Spatrick /// Returns a pair P, where:
63*e5dd7070Spatrick ///   * P.first are the ``Replacements`` necessary to make all \p Ranges comply
64*e5dd7070Spatrick ///     with \p Style.
65*e5dd7070Spatrick ///   * P.second is the penalty induced by formatting the fragment \p Code.
66*e5dd7070Spatrick ///     If the formatting of the fragment doesn't have a notion of penalty,
67*e5dd7070Spatrick ///     returns 0.
68*e5dd7070Spatrick ///
69*e5dd7070Spatrick /// If ``Status`` is non-null, its value will be populated with the status of
70*e5dd7070Spatrick /// this formatting attempt. See \c FormattingAttemptStatus.
71*e5dd7070Spatrick std::pair<tooling::Replacements, unsigned>
72*e5dd7070Spatrick reformat(const FormatStyle &Style, StringRef Code,
73*e5dd7070Spatrick          ArrayRef<tooling::Range> Ranges, unsigned FirstStartColumn,
74*e5dd7070Spatrick          unsigned NextStartColumn, unsigned LastStartColumn, StringRef FileName,
75*e5dd7070Spatrick          FormattingAttemptStatus *Status);
76*e5dd7070Spatrick 
77*e5dd7070Spatrick } // namespace internal
78*e5dd7070Spatrick } // namespace format
79*e5dd7070Spatrick } // namespace clang
80*e5dd7070Spatrick 
81*e5dd7070Spatrick #endif
82