diff options
Diffstat (limited to 'include/clang/Format/Format.h')
-rw-r--r-- | include/clang/Format/Format.h | 709 |
1 files changed, 420 insertions, 289 deletions
diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index d27d934f76798..85dda948c11f5 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -17,7 +17,9 @@ #include "clang/Basic/LangOptions.h" #include "clang/Tooling/Core/Replacement.h" +#include "clang/Tooling/Inclusions/IncludeStyle.h" #include "llvm/ADT/ArrayRef.h" +#include "llvm/Support/Regex.h" #include <system_error> namespace clang { @@ -41,27 +43,27 @@ public: const std::error_category &getParseCategory(); std::error_code make_error_code(ParseError e); -/// \brief The ``FormatStyle`` is used to configure the formatting to follow +/// The ``FormatStyle`` is used to configure the formatting to follow /// specific guidelines. struct FormatStyle { - /// \brief The extra indent or outdent of access modifiers, e.g. ``public:``. + /// The extra indent or outdent of access modifiers, e.g. ``public:``. int AccessModifierOffset; - /// \brief Different styles for aligning after open brackets. + /// Different styles for aligning after open brackets. enum BracketAlignmentStyle { - /// \brief Align parameters on the open bracket, e.g.: + /// Align parameters on the open bracket, e.g.: /// \code /// someLongFunction(argument1, /// argument2); /// \endcode BAS_Align, - /// \brief Don't align, instead use ``ContinuationIndentWidth``, e.g.: + /// Don't align, instead use ``ContinuationIndentWidth``, e.g.: /// \code /// someLongFunction(argument1, /// argument2); /// \endcode BAS_DontAlign, - /// \brief Always break after an open bracket, if the parameters don't fit + /// Always break after an open bracket, if the parameters don't fit /// on a single line, e.g.: /// \code /// someLongFunction( @@ -70,13 +72,13 @@ struct FormatStyle { BAS_AlwaysBreak, }; - /// \brief If ``true``, horizontally aligns arguments after an open bracket. + /// If ``true``, horizontally aligns arguments after an open bracket. /// /// This applies to round brackets (parentheses), angle brackets and square /// brackets. BracketAlignmentStyle AlignAfterOpenBracket; - /// \brief If ``true``, aligns consecutive assignments. + /// If ``true``, aligns consecutive assignments. /// /// This will align the assignment operators of consecutive lines. This /// will result in formattings like @@ -87,7 +89,7 @@ struct FormatStyle { /// \endcode bool AlignConsecutiveAssignments; - /// \brief If ``true``, aligns consecutive declarations. + /// If ``true``, aligns consecutive declarations. /// /// This will align the declaration names of consecutive lines. This /// will result in formattings like @@ -98,9 +100,9 @@ struct FormatStyle { /// \endcode bool AlignConsecutiveDeclarations; - /// \brief Different styles for aligning escaped newlines. + /// Different styles for aligning escaped newlines. enum EscapedNewlineAlignmentStyle { - /// \brief Don't align escaped newlines. + /// Don't align escaped newlines. /// \code /// #define A \ /// int aaaa; \ @@ -108,7 +110,7 @@ struct FormatStyle { /// int dddddddddd; /// \endcode ENAS_DontAlign, - /// \brief Align escaped newlines as far left as possible. + /// Align escaped newlines as far left as possible. /// \code /// true: /// #define A \ @@ -119,7 +121,7 @@ struct FormatStyle { /// false: /// \endcode ENAS_Left, - /// \brief Align escaped newlines in the right-most column. + /// Align escaped newlines in the right-most column. /// \code /// #define A \ /// int aaaa; \ @@ -129,10 +131,10 @@ struct FormatStyle { ENAS_Right, }; - /// \brief Options for aligning backslashes in escaped newlines. + /// Options for aligning backslashes in escaped newlines. EscapedNewlineAlignmentStyle AlignEscapedNewlines; - /// \brief If ``true``, horizontally align operands of binary and ternary + /// If ``true``, horizontally align operands of binary and ternary /// expressions. /// /// Specifically, this aligns operands of a single expression that needs to be @@ -143,7 +145,7 @@ struct FormatStyle { /// \endcode bool AlignOperands; - /// \brief If ``true``, aligns trailing comments. + /// If ``true``, aligns trailing comments. /// \code /// true: false: /// int a; // My comment a vs. int a; // My comment a @@ -151,7 +153,7 @@ struct FormatStyle { /// \endcode bool AlignTrailingComments; - /// \brief If the function declaration doesn't fit on a line, + /// If the function declaration doesn't fit on a line, /// allow putting all parameters of a function declaration onto /// the next line even if ``BinPackParameters`` is ``false``. /// \code @@ -168,12 +170,12 @@ struct FormatStyle { /// \endcode bool AllowAllParametersOfDeclarationOnNextLine; - /// \brief Allows contracting simple braced statements to a single line. + /// Allows contracting simple braced statements to a single line. /// /// E.g., this allows ``if (a) { return; }`` to be put on a single line. bool AllowShortBlocksOnASingleLine; - /// \brief If ``true``, short case labels will be contracted to a single line. + /// If ``true``, short case labels will be contracted to a single line. /// \code /// true: false: /// switch (a) { vs. switch (a) { @@ -186,12 +188,12 @@ struct FormatStyle { /// \endcode bool AllowShortCaseLabelsOnASingleLine; - /// \brief Different styles for merging short functions containing at most one + /// Different styles for merging short functions containing at most one /// statement. enum ShortFunctionStyle { - /// \brief Never merge functions into a single line. + /// Never merge functions into a single line. SFS_None, - /// \brief Only merge functions defined inside a class. Same as "inline", + /// Only merge functions defined inside a class. Same as "inline", /// except it does not implies "empty": i.e. top level empty functions /// are not merged either. /// \code @@ -205,7 +207,7 @@ struct FormatStyle { /// } /// \endcode SFS_InlineOnly, - /// \brief Only merge empty functions. + /// Only merge empty functions. /// \code /// void f() {} /// void f2() { @@ -213,7 +215,7 @@ struct FormatStyle { /// } /// \endcode SFS_Empty, - /// \brief Only merge functions defined inside a class. Implies "empty". + /// Only merge functions defined inside a class. Implies "empty". /// \code /// class Foo { /// void f() { foo(); } @@ -224,7 +226,7 @@ struct FormatStyle { /// void f() {} /// \endcode SFS_Inline, - /// \brief Merge all functions fitting on a single line. + /// Merge all functions fitting on a single line. /// \code /// class Foo { /// void f() { foo(); } @@ -234,18 +236,18 @@ struct FormatStyle { SFS_All, }; - /// \brief Dependent on the value, ``int f() { return 0; }`` can be put on a + /// Dependent on the value, ``int f() { return 0; }`` can be put on a /// single line. ShortFunctionStyle AllowShortFunctionsOnASingleLine; - /// \brief If ``true``, ``if (a) return;`` can be put on a single line. + /// If ``true``, ``if (a) return;`` can be put on a single line. bool AllowShortIfStatementsOnASingleLine; - /// \brief If ``true``, ``while (true) continue;`` can be put on a single + /// If ``true``, ``while (true) continue;`` can be put on a single /// line. bool AllowShortLoopsOnASingleLine; - /// \brief Different ways to break after the function definition return type. + /// Different ways to break after the function definition return type. /// This option is **deprecated** and is retained for backwards compatibility. enum DefinitionReturnTypeBreakingStyle { /// Break after return type automatically. @@ -257,7 +259,7 @@ struct FormatStyle { DRTBS_TopLevel, }; - /// \brief Different ways to break after the function definition or + /// Different ways to break after the function definition or /// declaration return type. enum ReturnTypeBreakingStyle { /// Break after return type automatically. @@ -328,14 +330,14 @@ struct FormatStyle { RTBS_TopLevelDefinitions, }; - /// \brief The function definition return type breaking style to use. This + /// The function definition return type breaking style to use. This /// option is **deprecated** and is retained for backwards compatibility. DefinitionReturnTypeBreakingStyle AlwaysBreakAfterDefinitionReturnType; - /// \brief The function declaration return type breaking style to use. + /// The function declaration return type breaking style to use. ReturnTypeBreakingStyle AlwaysBreakAfterReturnType; - /// \brief If ``true``, always break before multiline string literals. + /// If ``true``, always break before multiline string literals. /// /// This flag is mean to make cases where there are multiple multiline strings /// in a file look more consistent. Thus, it will only take effect if wrapping @@ -349,16 +351,46 @@ struct FormatStyle { /// \endcode bool AlwaysBreakBeforeMultilineStrings; - /// \brief If ``true``, always break after the ``template<...>`` of a template - /// declaration. - /// \code - /// true: false: - /// template <typename T> vs. template <typename T> class C {}; - /// class C {}; - /// \endcode - bool AlwaysBreakTemplateDeclarations; + /// Different ways to break after the template declaration. + enum BreakTemplateDeclarationsStyle { + /// Do not force break before declaration. + /// ``PenaltyBreakTemplateDeclaration`` is taken into account. + /// \code + /// template <typename T> T foo() { + /// } + /// template <typename T> T foo(int aaaaaaaaaaaaaaaaaaaaa, + /// int bbbbbbbbbbbbbbbbbbbbb) { + /// } + /// \endcode + BTDS_No, + /// Force break after template declaration only when the following + /// declaration spans multiple lines. + /// \code + /// template <typename T> T foo() { + /// } + /// template <typename T> + /// T foo(int aaaaaaaaaaaaaaaaaaaaa, + /// int bbbbbbbbbbbbbbbbbbbbb) { + /// } + /// \endcode + BTDS_MultiLine, + /// Always break after template declaration. + /// \code + /// template <typename T> + /// T foo() { + /// } + /// template <typename T> + /// T foo(int aaaaaaaaaaaaaaaaaaaaa, + /// int bbbbbbbbbbbbbbbbbbbbb) { + /// } + /// \endcode + BTDS_Yes + }; + + /// The template declaration breaking style to use. + BreakTemplateDeclarationsStyle AlwaysBreakTemplateDeclarations; - /// \brief If ``false``, a function call's arguments will either be all on the + /// If ``false``, a function call's arguments will either be all on the /// same line or will have one line each. /// \code /// true: @@ -376,7 +408,7 @@ struct FormatStyle { /// \endcode bool BinPackArguments; - /// \brief If ``false``, a function declaration's or function definition's + /// If ``false``, a function declaration's or function definition's /// parameters will either all be on the same line or will have one line each. /// \code /// true: @@ -390,7 +422,18 @@ struct FormatStyle { /// \endcode bool BinPackParameters; - /// \brief The style of breaking before or after binary operators. + /// The style of wrapping parameters on the same line (bin-packed) or + /// on one line each. + enum BinPackStyle { + /// Automatically determine parameter bin-packing behavior. + BPS_Auto, + /// Always bin-pack parameters. + BPS_Always, + /// Never bin-pack parameters. + BPS_Never, + }; + + /// The style of breaking before or after binary operators. enum BinaryOperatorStyle { /// Break after operators. /// \code @@ -430,10 +473,10 @@ struct FormatStyle { BOS_All, }; - /// \brief The way to wrap binary operators. + /// The way to wrap binary operators. BinaryOperatorStyle BreakBeforeBinaryOperators; - /// \brief Different ways to attach braces to their surrounding context. + /// Different ways to attach braces to their surrounding context. enum BraceBreakingStyle { /// Always attach braces to surrounding context. /// \code @@ -568,10 +611,10 @@ struct FormatStyle { BS_Custom }; - /// \brief The brace breaking style to use. + /// The brace breaking style to use. BraceBreakingStyle BreakBeforeBraces; - /// \brief Precise control over the wrapping of braces. + /// Precise control over the wrapping of braces. /// \code /// # Should be declared this way: /// BreakBeforeBraces: Custom @@ -579,7 +622,7 @@ struct FormatStyle { /// AfterClass: true /// \endcode struct BraceWrappingFlags { - /// \brief Wrap class definitions. + /// Wrap class definitions. /// \code /// true: /// class foo {}; @@ -589,7 +632,7 @@ struct FormatStyle { /// {}; /// \endcode bool AfterClass; - /// \brief Wrap control statements (``if``/``for``/``while``/``switch``/..). + /// Wrap control statements (``if``/``for``/``while``/``switch``/..). /// \code /// true: /// if (foo()) @@ -607,7 +650,7 @@ struct FormatStyle { /// } /// \endcode bool AfterControlStatement; - /// \brief Wrap enum definitions. + /// Wrap enum definitions. /// \code /// true: /// enum X : int @@ -619,7 +662,7 @@ struct FormatStyle { /// enum X : int { B }; /// \endcode bool AfterEnum; - /// \brief Wrap function definitions. + /// Wrap function definitions. /// \code /// true: /// void foo() @@ -635,7 +678,7 @@ struct FormatStyle { /// } /// \endcode bool AfterFunction; - /// \brief Wrap namespace definitions. + /// Wrap namespace definitions. /// \code /// true: /// namespace @@ -651,9 +694,11 @@ struct FormatStyle { /// } /// \endcode bool AfterNamespace; - /// \brief Wrap ObjC definitions (``@autoreleasepool``, interfaces, ..). + /// Wrap ObjC definitions (interfaces, implementations...). + /// \note @autoreleasepool and @synchronized blocks are wrapped + /// according to `AfterControlStatement` flag. bool AfterObjCDeclaration; - /// \brief Wrap struct definitions. + /// Wrap struct definitions. /// \code /// true: /// struct foo @@ -667,7 +712,7 @@ struct FormatStyle { /// }; /// \endcode bool AfterStruct; - /// \brief Wrap union definitions. + /// Wrap union definitions. /// \code /// true: /// union foo @@ -681,7 +726,7 @@ struct FormatStyle { /// } /// \endcode bool AfterUnion; - /// \brief Wrap extern blocks. + /// Wrap extern blocks. /// \code /// true: /// extern "C" @@ -695,7 +740,7 @@ struct FormatStyle { /// } /// \endcode bool AfterExternBlock; - /// \brief Wrap before ``catch``. + /// Wrap before ``catch``. /// \code /// true: /// try { @@ -711,7 +756,7 @@ struct FormatStyle { /// } /// \endcode bool BeforeCatch; - /// \brief Wrap before ``else``. + /// Wrap before ``else``. /// \code /// true: /// if (foo()) { @@ -725,9 +770,9 @@ struct FormatStyle { /// } /// \endcode bool BeforeElse; - /// \brief Indent the wrapped braces themselves. + /// Indent the wrapped braces themselves. bool IndentBraces; - /// \brief If ``false``, empty function body can be put on a single line. + /// If ``false``, empty function body can be put on a single line. /// This option is used only if the opening brace of the function has /// already been wrapped, i.e. the `AfterFunction` brace wrapping mode is /// set, and the function could/should not be put on a single line (as per @@ -739,7 +784,7 @@ struct FormatStyle { /// \endcode /// bool SplitEmptyFunction; - /// \brief If ``false``, empty record (e.g. class, struct or union) body + /// If ``false``, empty record (e.g. class, struct or union) body /// can be put on a single line. This option is used only if the opening /// brace of the record has already been wrapped, i.e. the `AfterClass` /// (for classes) brace wrapping mode is set. @@ -750,7 +795,7 @@ struct FormatStyle { /// \endcode /// bool SplitEmptyRecord; - /// \brief If ``false``, empty namespace body can be put on a single line. + /// If ``false``, empty namespace body can be put on a single line. /// This option is used only if the opening brace of the namespace has /// already been wrapped, i.e. the `AfterNamespace` brace wrapping mode is /// set. @@ -763,7 +808,7 @@ struct FormatStyle { bool SplitEmptyNamespace; }; - /// \brief Control of individual brace wrapping cases. + /// Control of individual brace wrapping cases. /// /// If ``BreakBeforeBraces`` is set to ``BS_Custom``, use this to specify how /// each individual brace case should be handled. Otherwise, this is ignored. @@ -777,7 +822,7 @@ struct FormatStyle { /// \endcode BraceWrappingFlags BraceWrapping; - /// \brief If ``true``, ternary operators will be placed after line breaks. + /// If ``true``, ternary operators will be placed after line breaks. /// \code /// true: /// veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription @@ -791,36 +836,36 @@ struct FormatStyle { /// \endcode bool BreakBeforeTernaryOperators; - /// \brief Different ways to break initializers. + /// Different ways to break initializers. enum BreakConstructorInitializersStyle { /// Break constructor initializers before the colon and after the commas. /// \code - /// Constructor() - /// : initializer1(), - /// initializer2() + /// Constructor() + /// : initializer1(), + /// initializer2() /// \endcode BCIS_BeforeColon, /// Break constructor initializers before the colon and commas, and align /// the commas with the colon. /// \code - /// Constructor() - /// : initializer1() - /// , initializer2() + /// Constructor() + /// : initializer1() + /// , initializer2() /// \endcode BCIS_BeforeComma, /// Break constructor initializers after the colon and commas. /// \code - /// Constructor() : - /// initializer1(), - /// initializer2() + /// Constructor() : + /// initializer1(), + /// initializer2() /// \endcode BCIS_AfterColon }; - /// \brief The constructor initializers style to use. + /// The constructor initializers style to use. BreakConstructorInitializersStyle BreakConstructorInitializers; - /// \brief Break after each annotation on a field in Java files. + /// Break after each annotation on a field in Java files. /// \code{.java} /// true: false: /// @Partial vs. @Partial @Mock DataLoad loader; @@ -829,17 +874,17 @@ struct FormatStyle { /// \endcode bool BreakAfterJavaFieldAnnotations; - /// \brief Allow breaking string literals when formatting. + /// Allow breaking string literals when formatting. bool BreakStringLiterals; - /// \brief The column limit. + /// The column limit. /// /// A column limit of ``0`` means that there is no column limit. In this case, /// clang-format will respect the input's line breaking decisions within /// statements unless they contradict other rules. unsigned ColumnLimit; - /// \brief A regular expression that describes comments with special meaning, + /// A regular expression that describes comments with special meaning, /// which should not be split into lines or otherwise changed. /// \code /// // CommentPragmas: '^ FOOBAR pragma:' @@ -848,18 +893,39 @@ struct FormatStyle { /// \endcode std::string CommentPragmas; - /// \brief If ``true``, in the class inheritance expression clang-format will - /// break before ``:`` and ``,`` if there is multiple inheritance. - /// \code - /// true: false: - /// class MyClass vs. class MyClass : public X, public Y { - /// : public X }; - /// , public Y { - /// }; - /// \endcode - bool BreakBeforeInheritanceComma; + /// Different ways to break inheritance list. + enum BreakInheritanceListStyle { + /// Break inheritance list before the colon and after the commas. + /// \code + /// class Foo + /// : Base1, + /// Base2 + /// {}; + /// \endcode + BILS_BeforeColon, + /// Break inheritance list before the colon and commas, and align + /// the commas with the colon. + /// \code + /// class Foo + /// : Base1 + /// , Base2 + /// {}; + /// \endcode + BILS_BeforeComma, + /// Break inheritance list after the colon and commas. + /// \code + /// class Foo : + /// Base1, + /// Base2 + /// {}; + /// \endcode + BILS_AfterColon + }; + + /// The inheritance list style to use. + BreakInheritanceListStyle BreakInheritanceList; - /// \brief If ``true``, consecutive namespace declarations will be on the same + /// If ``true``, consecutive namespace declarations will be on the same /// line. If ``false``, each namespace is declared on a new line. /// \code /// true: @@ -882,7 +948,7 @@ struct FormatStyle { /// \endcode bool CompactNamespaces; - /// \brief If the constructor initializers don't fit on a line, put each + /// If the constructor initializers don't fit on a line, put each /// initializer on its own line. /// \code /// true: @@ -900,11 +966,11 @@ struct FormatStyle { /// \endcode bool ConstructorInitializerAllOnOneLineOrOnePerLine; - /// \brief The number of characters to use for indentation of constructor - /// initializer lists. + /// The number of characters to use for indentation of constructor + /// initializer lists as well as inheritance lists. unsigned ConstructorInitializerIndentWidth; - /// \brief Indent width for line continuations. + /// Indent width for line continuations. /// \code /// ContinuationIndentWidth: 2 /// @@ -914,7 +980,7 @@ struct FormatStyle { /// \endcode unsigned ContinuationIndentWidth; - /// \brief If ``true``, format braced lists as best suited for C++11 braced + /// If ``true``, format braced lists as best suited for C++11 braced /// lists. /// /// Important differences: @@ -936,17 +1002,17 @@ struct FormatStyle { /// \endcode bool Cpp11BracedListStyle; - /// \brief If ``true``, analyze the formatted file for the most common + /// If ``true``, analyze the formatted file for the most common /// alignment of ``&`` and ``*``. /// Pointer and reference alignment styles are going to be updated according /// to the preferences found in the file. /// ``PointerAlignment`` is then used only as fallback. bool DerivePointerAlignment; - /// \brief Disables formatting completely. + /// Disables formatting completely. bool DisableFormat; - /// \brief If ``true``, clang-format detects whether function calls and + /// If ``true``, clang-format detects whether function calls and /// definitions are formatted with one parameter per line. /// /// Each call can be bin-packed, one-per-line or inconclusive. If it is @@ -958,7 +1024,7 @@ struct FormatStyle { /// not use this in config files, etc. Use at your own risk. bool ExperimentalAutoDetectBinPacking; - /// \brief If ``true``, clang-format adds missing namespace end comments and + /// If ``true``, clang-format adds missing namespace end comments and /// fixes invalid existing ones. /// \code /// true: false: @@ -968,7 +1034,7 @@ struct FormatStyle { /// \endcode bool FixNamespaceComments; - /// \brief A vector of macros that should be interpreted as foreach loops + /// A vector of macros that should be interpreted as foreach loops /// instead of as function calls. /// /// These are expected to be macros of the form: @@ -985,93 +1051,9 @@ struct FormatStyle { /// For example: BOOST_FOREACH. std::vector<std::string> ForEachMacros; - /// \brief Styles for sorting multiple ``#include`` blocks. - enum IncludeBlocksStyle { - /// \brief Sort each ``#include`` block separately. - /// \code - /// #include "b.h" into #include "b.h" - /// - /// #include <lib/main.h> #include "a.h" - /// #include "a.h" #include <lib/main.h> - /// \endcode - IBS_Preserve, - /// \brief Merge multiple ``#include`` blocks together and sort as one. - /// \code - /// #include "b.h" into #include "a.h" - /// #include "b.h" - /// #include <lib/main.h> #include <lib/main.h> - /// #include "a.h" - /// \endcode - IBS_Merge, - /// \brief Merge multiple ``#include`` blocks together and sort as one. - /// Then split into groups based on category priority. See - /// ``IncludeCategories``. - /// \code - /// #include "b.h" into #include "a.h" - /// #include "b.h" - /// #include <lib/main.h> - /// #include "a.h" #include <lib/main.h> - /// \endcode - IBS_Regroup, - }; - - /// \brief Dependent on the value, multiple ``#include`` blocks can be sorted - /// as one and divided based on category. - IncludeBlocksStyle IncludeBlocks; - - /// \brief See documentation of ``IncludeCategories``. - struct IncludeCategory { - /// \brief The regular expression that this category matches. - std::string Regex; - /// \brief The priority to assign to this category. - int Priority; - bool operator==(const IncludeCategory &Other) const { - return Regex == Other.Regex && Priority == Other.Priority; - } - }; + tooling::IncludeStyle IncludeStyle; - /// \brief Regular expressions denoting the different ``#include`` categories - /// used for ordering ``#includes``. - /// - /// These regular expressions are matched against the filename of an include - /// (including the <> or "") in order. The value belonging to the first - /// matching regular expression is assigned and ``#includes`` are sorted first - /// according to increasing category number and then alphabetically within - /// each category. - /// - /// If none of the regular expressions match, INT_MAX is assigned as - /// category. The main header for a source file automatically gets category 0. - /// so that it is generally kept at the beginning of the ``#includes`` - /// (http://llvm.org/docs/CodingStandards.html#include-style). However, you - /// can also assign negative priorities if you have certain headers that - /// always need to be first. - /// - /// To configure this in the .clang-format file, use: - /// \code{.yaml} - /// IncludeCategories: - /// - Regex: '^"(llvm|llvm-c|clang|clang-c)/' - /// Priority: 2 - /// - Regex: '^(<|"(gtest|gmock|isl|json)/)' - /// Priority: 3 - /// - Regex: '.*' - /// Priority: 1 - /// \endcode - std::vector<IncludeCategory> IncludeCategories; - - /// \brief Specify a regular expression of suffixes that are allowed in the - /// file-to-main-include mapping. - /// - /// When guessing whether a #include is the "main" include (to assign - /// category 0, see above), use this regex of allowed suffixes to the header - /// stem. A partial match is done, so that: - /// - "" means "arbitrary suffix" - /// - "$" means "no suffix" - /// - /// For example, if configured to "(_test)?$", then a header a.h would be seen - /// as the "main" include in both a.cc and a_test.cc. - std::string IncludeIsMainRegex; - - /// \brief Indent case labels one level from the switch statement. + /// Indent case labels one level from the switch statement. /// /// When ``false``, use the same indentation level as for the switch statement. /// Switch statement body is always indented one level more than case labels. @@ -1087,7 +1069,7 @@ struct FormatStyle { /// \endcode bool IndentCaseLabels; - /// \brief Options for indenting preprocessor directives. + /// Options for indenting preprocessor directives. enum PPDirectiveIndentStyle { /// Does not indent any directives. /// \code @@ -1109,10 +1091,10 @@ struct FormatStyle { PPDIS_AfterHash }; - /// \brief The preprocessor directive indenting style to use. + /// The preprocessor directive indenting style to use. PPDirectiveIndentStyle IndentPPDirectives; - /// \brief The number of columns to use for indentation. + /// The number of columns to use for indentation. /// \code /// IndentWidth: 3 /// @@ -1125,7 +1107,7 @@ struct FormatStyle { /// \endcode unsigned IndentWidth; - /// \brief Indent if a function definition or declaration is wrapped after the + /// Indent if a function definition or declaration is wrapped after the /// type. /// \code /// true: @@ -1138,7 +1120,7 @@ struct FormatStyle { /// \endcode bool IndentWrappedFunctionNames; - /// \brief Quotation styles for JavaScript strings. Does not affect template + /// Quotation styles for JavaScript strings. Does not affect template /// strings. enum JavaScriptQuoteStyle { /// Leave string quotes as they are. @@ -1161,10 +1143,10 @@ struct FormatStyle { JSQS_Double }; - /// \brief The JavaScriptQuoteStyle to use for JavaScript strings. + /// The JavaScriptQuoteStyle to use for JavaScript strings. JavaScriptQuoteStyle JavaScriptQuotes; - /// \brief Whether to wrap JavaScript import/export statements. + /// Whether to wrap JavaScript import/export statements. /// \code{.js} /// true: /// import { @@ -1178,7 +1160,7 @@ struct FormatStyle { /// \endcode bool JavaScriptWrapImports; - /// \brief If true, the empty line at the start of blocks is kept. + /// If true, the empty line at the start of blocks is kept. /// \code /// true: false: /// if (foo) { vs. if (foo) { @@ -1188,7 +1170,7 @@ struct FormatStyle { /// \endcode bool KeepEmptyLinesAtTheStartOfBlocks; - /// \brief Supported languages. + /// Supported languages. /// /// When stored in a configuration file, specifies the language, that the /// configuration targets. When passed to the ``reformat()`` function, enables @@ -1215,10 +1197,10 @@ struct FormatStyle { }; bool isCpp() const { return Language == LK_Cpp || Language == LK_ObjC; } - /// \brief Language, this format style is targeted at. + /// Language, this format style is targeted at. LanguageKind Language; - /// \brief A regular expression matching macros that start a block. + /// A regular expression matching macros that start a block. /// \code /// # With: /// MacroBlockBegin: "^NS_MAP_BEGIN|\ @@ -1246,10 +1228,10 @@ struct FormatStyle { /// \endcode std::string MacroBlockBegin; - /// \brief A regular expression matching macros that end a block. + /// A regular expression matching macros that end a block. std::string MacroBlockEnd; - /// \brief The maximum number of consecutive empty lines to keep. + /// The maximum number of consecutive empty lines to keep. /// \code /// MaxEmptyLinesToKeep: 1 vs. MaxEmptyLinesToKeep: 0 /// int f() { int f() { @@ -1262,7 +1244,7 @@ struct FormatStyle { /// \endcode unsigned MaxEmptyLinesToKeep; - /// \brief Different ways to indent namespace contents. + /// Different ways to indent namespace contents. enum NamespaceIndentationKind { /// Don't indent in namespaces. /// \code @@ -1296,10 +1278,42 @@ struct FormatStyle { NI_All }; - /// \brief The indentation used for namespaces. + /// The indentation used for namespaces. NamespaceIndentationKind NamespaceIndentation; - /// \brief The number of characters to use for indentation of ObjC blocks. + /// Controls bin-packing Objective-C protocol conformance list + /// items into as few lines as possible when they go over ``ColumnLimit``. + /// + /// If ``Auto`` (the default), delegates to the value in + /// ``BinPackParameters``. If that is ``true``, bin-packs Objective-C + /// protocol conformance list items into as few lines as possible + /// whenever they go over ``ColumnLimit``. + /// + /// If ``Always``, always bin-packs Objective-C protocol conformance + /// list items into as few lines as possible whenever they go over + /// ``ColumnLimit``. + /// + /// If ``Never``, lays out Objective-C protocol conformance list items + /// onto individual lines whenever they go over ``ColumnLimit``. + /// + /// \code{.objc} + /// Always (or Auto, if BinPackParameters=true): + /// @interface ccccccccccccc () < + /// ccccccccccccc, ccccccccccccc, + /// ccccccccccccc, ccccccccccccc> { + /// } + /// + /// Never (or Auto, if BinPackParameters=false): + /// @interface ddddddddddddd () < + /// ddddddddddddd, + /// ddddddddddddd, + /// ddddddddddddd, + /// ddddddddddddd> { + /// } + /// \endcode + BinPackStyle ObjCBinPackProtocolList; + + /// The number of characters to use for indentation of ObjC blocks. /// \code{.objc} /// ObjCBlockIndentWidth: 4 /// @@ -1309,37 +1323,40 @@ struct FormatStyle { /// \endcode unsigned ObjCBlockIndentWidth; - /// \brief Add a space after ``@property`` in Objective-C, i.e. use + /// Add a space after ``@property`` in Objective-C, i.e. use /// ``@property (readonly)`` instead of ``@property(readonly)``. bool ObjCSpaceAfterProperty; - /// \brief Add a space in front of an Objective-C protocol list, i.e. use + /// Add a space in front of an Objective-C protocol list, i.e. use /// ``Foo <Protocol>`` instead of ``Foo<Protocol>``. bool ObjCSpaceBeforeProtocolList; - /// \brief The penalty for breaking around an assignment operator. + /// The penalty for breaking around an assignment operator. unsigned PenaltyBreakAssignment; - /// \brief The penalty for breaking a function call after ``call(``. + /// The penalty for breaking a function call after ``call(``. unsigned PenaltyBreakBeforeFirstCallParameter; - /// \brief The penalty for each line break introduced inside a comment. + /// The penalty for each line break introduced inside a comment. unsigned PenaltyBreakComment; - /// \brief The penalty for breaking before the first ``<<``. + /// The penalty for breaking before the first ``<<``. unsigned PenaltyBreakFirstLessLess; - /// \brief The penalty for each line break introduced inside a string literal. + /// The penalty for each line break introduced inside a string literal. unsigned PenaltyBreakString; - /// \brief The penalty for each character outside of the column limit. + /// The penalty for breaking after template declaration. + unsigned PenaltyBreakTemplateDeclaration; + + /// The penalty for each character outside of the column limit. unsigned PenaltyExcessCharacter; - /// \brief Penalty for putting the return type of a function onto its own + /// Penalty for putting the return type of a function onto its own /// line. unsigned PenaltyReturnTypeOnItsOwnLine; - /// \brief The ``&`` and ``*`` alignment style. + /// The ``&`` and ``*`` alignment style. enum PointerAlignmentStyle { /// Align pointer to the left. /// \code @@ -1358,45 +1375,69 @@ struct FormatStyle { PAS_Middle }; - /// \brief Pointer and reference alignment style. + /// Pointer and reference alignment style. PointerAlignmentStyle PointerAlignment; /// See documentation of ``RawStringFormats``. struct RawStringFormat { - /// \brief The delimiter that this raw string format matches. - std::string Delimiter; - /// \brief The language of this raw string. + /// The language of this raw string. LanguageKind Language; - /// \brief The style name on which this raw string format is based on. + /// A list of raw string delimiters that match this language. + std::vector<std::string> Delimiters; + /// A list of enclosing function names that match this language. + std::vector<std::string> EnclosingFunctions; + /// The canonical delimiter for this language. + std::string CanonicalDelimiter; + /// The style name on which this raw string format is based on. /// If not specified, the raw string format is based on the style that this /// format is based on. std::string BasedOnStyle; bool operator==(const RawStringFormat &Other) const { - return Delimiter == Other.Delimiter && Language == Other.Language && + return Language == Other.Language && Delimiters == Other.Delimiters && + EnclosingFunctions == Other.EnclosingFunctions && + CanonicalDelimiter == Other.CanonicalDelimiter && BasedOnStyle == Other.BasedOnStyle; } }; - /// \brief Raw string delimiters denoting that the raw string contents are - /// code in a particular language and can be reformatted. + /// Defines hints for detecting supported languages code blocks in raw + /// strings. + /// + /// A raw string with a matching delimiter or a matching enclosing function + /// name will be reformatted assuming the specified language based on the + /// style for that language defined in the .clang-format file. If no style has + /// been defined in the .clang-format file for the specific language, a + /// predefined style given by 'BasedOnStyle' is used. If 'BasedOnStyle' is not + /// found, the formatting is based on llvm style. A matching delimiter takes + /// precedence over a matching enclosing function name for determining the + /// language of the raw string contents. /// - /// A raw string with a matching delimiter will be reformatted assuming the - /// specified language based on a predefined style given by 'BasedOnStyle'. - /// If 'BasedOnStyle' is not found, the formatting is based on llvm style. + /// If a canonical delimiter is specified, occurrences of other delimiters for + /// the same language will be updated to the canonical if possible. + /// + /// There should be at most one specification per language and each delimiter + /// and enclosing function should not occur in multiple specifications. /// /// To configure this in the .clang-format file, use: /// \code{.yaml} /// RawStringFormats: - /// - Delimiter: 'pb' - /// Language: TextProto - /// BasedOnStyle: llvm - /// - Delimiter: 'proto' - /// Language: TextProto - /// BasedOnStyle: google + /// - Language: TextProto + /// Delimiters: + /// - 'pb' + /// - 'proto' + /// EnclosingFunctions: + /// - 'PARSE_TEXT_PROTO' + /// BasedOnStyle: google + /// - Language: Cpp + /// Delimiters: + /// - 'cc' + /// - 'cpp' + /// BasedOnStyle: llvm + /// CanonicalDelimiter: 'cc' /// \endcode std::vector<RawStringFormat> RawStringFormats; - /// \brief If ``true``, clang-format will attempt to re-flow comments. + /// If ``true``, clang-format will attempt to re-flow comments. /// \code /// false: /// // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information @@ -1410,7 +1451,7 @@ struct FormatStyle { /// \endcode bool ReflowComments; - /// \brief If ``true``, clang-format will sort ``#includes``. + /// If ``true``, clang-format will sort ``#includes``. /// \code /// false: true: /// #include "b.h" vs. #include "a.h" @@ -1418,7 +1459,7 @@ struct FormatStyle { /// \endcode bool SortIncludes; - /// \brief If ``true``, clang-format will sort using declarations. + /// If ``true``, clang-format will sort using declarations. /// /// The order of using declarations is defined as follows: /// Split the strings by "::" and discard any initial empty strings. The last @@ -1434,21 +1475,21 @@ struct FormatStyle { /// \endcode bool SortUsingDeclarations; - /// \brief If ``true``, a space is inserted after C style casts. + /// If ``true``, a space is inserted after C style casts. /// \code /// true: false: - /// (int)i; vs. (int) i; + /// (int) i; vs. (int)i; /// \endcode bool SpaceAfterCStyleCast; - /// \brief If \c true, a space will be inserted after the 'template' keyword. + /// If \c true, a space will be inserted after the 'template' keyword. /// \code /// true: false: /// template <int> void foo(); vs. template<int> void foo(); /// \endcode bool SpaceAfterTemplateKeyword; - /// \brief If ``false``, spaces will be removed before assignment operators. + /// If ``false``, spaces will be removed before assignment operators. /// \code /// true: false: /// int a = 5; vs. int a=5; @@ -1456,7 +1497,33 @@ struct FormatStyle { /// \endcode bool SpaceBeforeAssignmentOperators; - /// \brief Different ways to put a space before opening parentheses. + /// If ``true``, a space will be inserted before a C++11 braced list + /// used to initialize an object (after the preceding identifier or type). + /// \code + /// true: false: + /// Foo foo { bar }; vs. Foo foo{ bar }; + /// Foo {}; Foo{}; + /// vector<int> { 1, 2, 3 }; vector<int>{ 1, 2, 3 }; + /// new int[3] { 1, 2, 3 }; new int[3]{ 1, 2, 3 }; + /// \endcode + bool SpaceBeforeCpp11BracedList; + + /// If ``false``, spaces will be removed before constructor initializer + /// colon. + /// \code + /// true: false: + /// Foo::Foo() : a(a) {} Foo::Foo(): a(a) {} + /// \endcode + bool SpaceBeforeCtorInitializerColon; + + /// If ``false``, spaces will be removed before inheritance colon. + /// \code + /// true: false: + /// class Foo : Bar {} vs. class Foo: Bar {} + /// \endcode + bool SpaceBeforeInheritanceColon; + + /// Different ways to put a space before opening parentheses. enum SpaceBeforeParensOptions { /// Never put a space before opening parentheses. /// \code @@ -1491,10 +1558,18 @@ struct FormatStyle { SBPO_Always }; - /// \brief Defines in which cases to put a space before opening parentheses. + /// Defines in which cases to put a space before opening parentheses. SpaceBeforeParensOptions SpaceBeforeParens; - /// \brief If ``true``, spaces may be inserted into ``()``. + /// If ``false``, spaces will be removed before range-based for loop + /// colon. + /// \code + /// true: false: + /// for (auto v : values) {} vs. for(auto v: values) {} + /// \endcode + bool SpaceBeforeRangeBasedForLoopColon; + + /// If ``true``, spaces may be inserted into ``()``. /// \code /// true: false: /// void f( ) { vs. void f() { @@ -1506,7 +1581,7 @@ struct FormatStyle { /// \endcode bool SpaceInEmptyParentheses; - /// \brief The number of spaces before trailing line comments + /// The number of spaces before trailing line comments /// (``//`` - comments). /// /// This does not affect trailing block comments (``/*`` - comments) as @@ -1522,7 +1597,7 @@ struct FormatStyle { /// \endcode unsigned SpacesBeforeTrailingComments; - /// \brief If ``true``, spaces will be inserted after ``<`` and before ``>`` + /// If ``true``, spaces will be inserted after ``<`` and before ``>`` /// in template argument lists. /// \code /// true: false: @@ -1531,7 +1606,7 @@ struct FormatStyle { /// \endcode bool SpacesInAngles; - /// \brief If ``true``, spaces are inserted inside container literals (e.g. + /// If ``true``, spaces are inserted inside container literals (e.g. /// ObjC and Javascript array and dict literals). /// \code{.js} /// true: false: @@ -1540,21 +1615,21 @@ struct FormatStyle { /// \endcode bool SpacesInContainerLiterals; - /// \brief If ``true``, spaces may be inserted into C style casts. + /// If ``true``, spaces may be inserted into C style casts. /// \code /// true: false: /// x = ( int32 )y vs. x = (int32)y /// \endcode bool SpacesInCStyleCastParentheses; - /// \brief If ``true``, spaces will be inserted after ``(`` and before ``)``. + /// If ``true``, spaces will be inserted after ``(`` and before ``)``. /// \code /// true: false: /// t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete; /// \endcode bool SpacesInParentheses; - /// \brief If ``true``, spaces will be inserted after ``[`` and before ``]``. + /// If ``true``, spaces will be inserted after ``[`` and before ``]``. /// Lambdas or unspecified size array declarations will not be affected. /// \code /// true: false: @@ -1563,7 +1638,7 @@ struct FormatStyle { /// \endcode bool SpacesInSquareBrackets; - /// \brief Supported language standards. + /// Supported language standards. enum LanguageStandard { /// Use C++03-compatible syntax. LS_Cpp03, @@ -1574,14 +1649,14 @@ struct FormatStyle { LS_Auto }; - /// \brief Format compatible with this standard, e.g. use ``A<A<int> >`` + /// Format compatible with this standard, e.g. use ``A<A<int> >`` /// instead of ``A<A<int>>`` for ``LS_Cpp03``. LanguageStandard Standard; - /// \brief The number of columns used for tab stops. + /// The number of columns used for tab stops. unsigned TabWidth; - /// \brief Different ways to use tab in formatting. + /// Different ways to use tab in formatting. enum UseTabStyle { /// Never use tab. UT_Never, @@ -1594,7 +1669,7 @@ struct FormatStyle { UT_Always }; - /// \brief The way to use tab characters in the resulting file. + /// The way to use tab characters in the resulting file. UseTabStyle UseTab; bool operator==(const FormatStyle &R) const { @@ -1630,7 +1705,7 @@ struct FormatStyle { BreakAfterJavaFieldAnnotations == R.BreakAfterJavaFieldAnnotations && BreakStringLiterals == R.BreakStringLiterals && ColumnLimit == R.ColumnLimit && CommentPragmas == R.CommentPragmas && - BreakBeforeInheritanceComma == R.BreakBeforeInheritanceComma && + BreakInheritanceList == R.BreakInheritanceList && ConstructorInitializerAllOnOneLineOrOnePerLine == R.ConstructorInitializerAllOnOneLineOrOnePerLine && ConstructorInitializerIndentWidth == @@ -1643,8 +1718,8 @@ struct FormatStyle { R.ExperimentalAutoDetectBinPacking && FixNamespaceComments == R.FixNamespaceComments && ForEachMacros == R.ForEachMacros && - IncludeBlocks == R.IncludeBlocks && - IncludeCategories == R.IncludeCategories && + IncludeStyle.IncludeBlocks == R.IncludeStyle.IncludeBlocks && + IncludeStyle.IncludeCategories == R.IncludeStyle.IncludeCategories && IndentCaseLabels == R.IndentCaseLabels && IndentPPDirectives == R.IndentPPDirectives && IndentWidth == R.IndentWidth && Language == R.Language && @@ -1657,11 +1732,11 @@ struct FormatStyle { MacroBlockEnd == R.MacroBlockEnd && MaxEmptyLinesToKeep == R.MaxEmptyLinesToKeep && NamespaceIndentation == R.NamespaceIndentation && + ObjCBinPackProtocolList == R.ObjCBinPackProtocolList && ObjCBlockIndentWidth == R.ObjCBlockIndentWidth && ObjCSpaceAfterProperty == R.ObjCSpaceAfterProperty && ObjCSpaceBeforeProtocolList == R.ObjCSpaceBeforeProtocolList && - PenaltyBreakAssignment == - R.PenaltyBreakAssignment && + PenaltyBreakAssignment == R.PenaltyBreakAssignment && PenaltyBreakBeforeFirstCallParameter == R.PenaltyBreakBeforeFirstCallParameter && PenaltyBreakComment == R.PenaltyBreakComment && @@ -1669,12 +1744,20 @@ struct FormatStyle { PenaltyBreakString == R.PenaltyBreakString && PenaltyExcessCharacter == R.PenaltyExcessCharacter && PenaltyReturnTypeOnItsOwnLine == R.PenaltyReturnTypeOnItsOwnLine && + PenaltyBreakTemplateDeclaration == + R.PenaltyBreakTemplateDeclaration && PointerAlignment == R.PointerAlignment && RawStringFormats == R.RawStringFormats && SpaceAfterCStyleCast == R.SpaceAfterCStyleCast && SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword && SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators && + SpaceBeforeCpp11BracedList == R.SpaceBeforeCpp11BracedList && + SpaceBeforeCtorInitializerColon == + R.SpaceBeforeCtorInitializerColon && + SpaceBeforeInheritanceColon == R.SpaceBeforeInheritanceColon && SpaceBeforeParens == R.SpaceBeforeParens && + SpaceBeforeRangeBasedForLoopColon == + R.SpaceBeforeRangeBasedForLoopColon && SpaceInEmptyParentheses == R.SpaceInEmptyParentheses && SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments && SpacesInAngles == R.SpacesInAngles && @@ -1685,38 +1768,75 @@ struct FormatStyle { Standard == R.Standard && TabWidth == R.TabWidth && UseTab == R.UseTab; } + + llvm::Optional<FormatStyle> GetLanguageStyle(LanguageKind Language) const; + + // Stores per-language styles. A FormatStyle instance inside has an empty + // StyleSet. A FormatStyle instance returned by the Get method has its + // StyleSet set to a copy of the originating StyleSet, effectively keeping the + // internal representation of that StyleSet alive. + // + // The memory management and ownership reminds of a birds nest: chicks + // leaving the nest take photos of the nest with them. + struct FormatStyleSet { + typedef std::map<FormatStyle::LanguageKind, FormatStyle> MapType; + + llvm::Optional<FormatStyle> Get(FormatStyle::LanguageKind Language) const; + + // Adds \p Style to this FormatStyleSet. Style must not have an associated + // FormatStyleSet. + // Style.Language should be different than LK_None. If this FormatStyleSet + // already contains an entry for Style.Language, that gets replaced with the + // passed Style. + void Add(FormatStyle Style); + + // Clears this FormatStyleSet. + void Clear(); + + private: + std::shared_ptr<MapType> Styles; + }; + + static FormatStyleSet BuildStyleSetFromConfiguration( + const FormatStyle &MainStyle, + const std::vector<FormatStyle> &ConfigurationStyles); + +private: + FormatStyleSet StyleSet; + + friend std::error_code parseConfiguration(StringRef Text, FormatStyle *Style); }; -/// \brief Returns a format style complying with the LLVM coding standards: +/// Returns a format style complying with the LLVM coding standards: /// http://llvm.org/docs/CodingStandards.html. FormatStyle getLLVMStyle(); -/// \brief Returns a format style complying with one of Google's style guides: +/// Returns a format style complying with one of Google's style guides: /// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml. /// http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml. /// https://developers.google.com/protocol-buffers/docs/style. FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language); -/// \brief Returns a format style complying with Chromium's style guide: +/// Returns a format style complying with Chromium's style guide: /// http://www.chromium.org/developers/coding-style. FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language); -/// \brief Returns a format style complying with Mozilla's style guide: +/// Returns a format style complying with Mozilla's style guide: /// https://developer.mozilla.org/en-US/docs/Developer_Guide/Coding_Style. FormatStyle getMozillaStyle(); -/// \brief Returns a format style complying with Webkit's style guide: +/// Returns a format style complying with Webkit's style guide: /// http://www.webkit.org/coding/coding-style.html FormatStyle getWebKitStyle(); -/// \brief Returns a format style complying with GNU Coding Standards: +/// Returns a format style complying with GNU Coding Standards: /// http://www.gnu.org/prep/standards/standards.html FormatStyle getGNUStyle(); -/// \brief Returns style indicating formatting should be not applied at all. +/// Returns style indicating formatting should be not applied at all. FormatStyle getNoStyle(); -/// \brief Gets a predefined style for the specified language by name. +/// Gets a predefined style for the specified language by name. /// /// Currently supported names: LLVM, Google, Chromium, Mozilla. Names are /// compared case-insensitively. @@ -1725,66 +1845,64 @@ FormatStyle getNoStyle(); bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language, FormatStyle *Style); -/// \brief Parse configuration from YAML-formatted text. +/// Parse configuration from YAML-formatted text. /// /// Style->Language is used to get the base style, if the ``BasedOnStyle`` /// option is present. /// +/// The FormatStyleSet of Style is reset. +/// /// When ``BasedOnStyle`` is not present, options not present in the YAML /// document, are retained in \p Style. std::error_code parseConfiguration(StringRef Text, FormatStyle *Style); -/// \brief Gets configuration in a YAML string. +/// Gets configuration in a YAML string. std::string configurationAsText(const FormatStyle &Style); -/// \brief Returns the replacements necessary to sort all ``#include`` blocks +/// Returns the replacements necessary to sort all ``#include`` blocks /// that are affected by ``Ranges``. tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code, ArrayRef<tooling::Range> Ranges, StringRef FileName, unsigned *Cursor = nullptr); -/// \brief Returns the replacements corresponding to applying and formatting +/// Returns the replacements corresponding to applying and formatting /// \p Replaces on success; otheriwse, return an llvm::Error carrying /// llvm::StringError. llvm::Expected<tooling::Replacements> formatReplacements(StringRef Code, const tooling::Replacements &Replaces, const FormatStyle &Style); -/// \brief Returns the replacements corresponding to applying \p Replaces and +/// Returns the replacements corresponding to applying \p Replaces and /// cleaning up the code after that on success; otherwise, return an llvm::Error /// carrying llvm::StringError. /// This also supports inserting/deleting C++ #include directives: /// - If a replacement has offset UINT_MAX, length 0, and a replacement text /// that is an #include directive, this will insert the #include into the -/// correct block in the \p Code. When searching for points to insert new -/// header, this ignores #include's after the #include block(s) in the -/// beginning of a file to avoid inserting headers into code sections where -/// new #include's should not be added by default. These code sections -/// include: -/// - raw string literals (containing #include). -/// - #if blocks. -/// - Special #include's among declarations (e.g. functions). +/// correct block in the \p Code. /// - If a replacement has offset UINT_MAX, length 1, and a replacement text /// that is the name of the header to be removed, the header will be removed /// from \p Code if it exists. +/// The include manipulation is done via `tooling::HeaderInclude`, see its +/// documentation for more details on how include insertion points are found and +/// what edits are produced. llvm::Expected<tooling::Replacements> cleanupAroundReplacements(StringRef Code, const tooling::Replacements &Replaces, const FormatStyle &Style); -/// \brief Represents the status of a formatting attempt. +/// Represents the status of a formatting attempt. struct FormattingAttemptStatus { - /// \brief A value of ``false`` means that any of the affected ranges were not + /// A value of ``false`` means that any of the affected ranges were not /// formatted due to a non-recoverable syntax error. bool FormatComplete = true; - /// \brief If ``FormatComplete`` is false, ``Line`` records a one-based + /// If ``FormatComplete`` is false, ``Line`` records a one-based /// original line number at which a syntax error might have occurred. This is /// based on a best-effort analysis and could be imprecise. unsigned Line = 0; }; -/// \brief Reformats the given \p Ranges in \p Code. +/// Reformats the given \p Ranges in \p Code. /// /// Each range is extended on either end to its next bigger logic unit, i.e. /// everything that might influence its formatting or might be influenced by its @@ -1800,7 +1918,7 @@ tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, StringRef FileName = "<stdin>", FormattingAttemptStatus *Status = nullptr); -/// \brief Same as above, except if ``IncompleteFormat`` is non-null, its value +/// Same as above, except if ``IncompleteFormat`` is non-null, its value /// will be set to true if any of the affected ranges were not formatted due to /// a non-recoverable syntax error. tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, @@ -1808,7 +1926,7 @@ tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, StringRef FileName, bool *IncompleteFormat); -/// \brief Clean up any erroneous/redundant code in the given \p Ranges in \p +/// Clean up any erroneous/redundant code in the given \p Ranges in \p /// Code. /// /// Returns the ``Replacements`` that clean up all \p Ranges in \p Code. @@ -1816,7 +1934,7 @@ tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code, ArrayRef<tooling::Range> Ranges, StringRef FileName = "<stdin>"); -/// \brief Fix namespace end comments in the given \p Ranges in \p Code. +/// Fix namespace end comments in the given \p Ranges in \p Code. /// /// Returns the ``Replacements`` that fix the namespace comments in all /// \p Ranges in \p Code. @@ -1825,7 +1943,7 @@ tooling::Replacements fixNamespaceEndComments(const FormatStyle &Style, ArrayRef<tooling::Range> Ranges, StringRef FileName = "<stdin>"); -/// \brief Sort consecutive using declarations in the given \p Ranges in +/// Sort consecutive using declarations in the given \p Ranges in /// \p Code. /// /// Returns the ``Replacements`` that sort the using declarations in all @@ -1835,17 +1953,26 @@ tooling::Replacements sortUsingDeclarations(const FormatStyle &Style, ArrayRef<tooling::Range> Ranges, StringRef FileName = "<stdin>"); -/// \brief Returns the ``LangOpts`` that the formatter expects you to set. +/// Returns the ``LangOpts`` that the formatter expects you to set. /// /// \param Style determines specific settings for lexing mode. LangOptions getFormattingLangOpts(const FormatStyle &Style = getLLVMStyle()); -/// \brief Description to be used for help text for a ``llvm::cl`` option for +/// Description to be used for help text for a ``llvm::cl`` option for /// specifying format style. The description is closely related to the operation /// of ``getStyle()``. extern const char *StyleOptionHelpDescription; -/// \brief Construct a FormatStyle based on ``StyleName``. +/// The suggested format style to use by default. This allows tools using +/// `getStyle` to have a consistent default style. +/// Different builds can modify the value to the preferred styles. +extern const char *DefaultFormatStyle; + +/// The suggested predefined style to use as the fallback style in `getStyle`. +/// Different builds can modify the value to the preferred styles. +extern const char *DefaultFallbackStyle; + +/// Construct a FormatStyle based on ``StyleName``. /// /// ``StyleName`` can take several forms: /// * "{<key>: <value>, ...}" - Set specic style parameters. @@ -1874,7 +2001,11 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName, StringRef Code = "", vfs::FileSystem *FS = nullptr); -// \brief Returns a string representation of ``Language``. +// Guesses the language from the ``FileName`` and ``Code`` to be formatted. +// Defaults to FormatStyle::LK_Cpp. +FormatStyle::LanguageKind guessLanguage(StringRef FileName, StringRef Code); + +// Returns a string representation of ``Language``. inline StringRef getLanguageName(FormatStyle::LanguageKind Language) { switch (Language) { case FormatStyle::LK_Cpp: |