1 //===--- Format.h - Format C++ code -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Various functions to configurably format source code.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_FORMAT_FORMAT_H
16 #define LLVM_CLANG_FORMAT_FORMAT_H
18 #include "clang/Frontend/FrontendAction.h"
19 #include "clang/Tooling/Refactoring.h"
25 class DiagnosticConsumer;
29 /// \brief The \c FormatStyle is used to configure the formatting to follow
30 /// specific guidelines.
32 /// \brief The column limit.
35 /// \brief The penalty for each character outside of the column limit.
36 unsigned PenaltyExcessCharacter;
38 /// \brief The maximum number of consecutive empty lines to keep.
39 unsigned MaxEmptyLinesToKeep;
41 /// \brief Set whether & and * bind to the type as opposed to the variable.
42 bool PointerBindsToType;
44 /// \brief If \c true, analyze the formatted file for the most common binding.
45 bool DerivePointerBinding;
47 /// \brief The extra indent or outdent of access modifiers (e.g.: public:).
48 int AccessModifierOffset;
50 enum LanguageStandard {
56 /// \brief Format compatible with this standard, e.g. use \c A<A<int> >
57 /// instead of \c A<A<int>> for LS_Cpp03.
58 LanguageStandard Standard;
60 /// \brief Indent case labels one level from the switch statement.
62 /// When false, use the same indentation level as for the switch statement.
63 /// Switch statement body is always indented one level more than case labels.
64 bool IndentCaseLabels;
66 /// \brief The number of spaces to before trailing line comments.
67 unsigned SpacesBeforeTrailingComments;
69 /// \brief If false, a function call's or function definition's parameters
70 /// will either all be on the same line or will have one line each.
71 bool BinPackParameters;
73 /// \brief Allow putting all parameters of a function declaration onto
74 /// the next line even if \c BinPackParameters is \c false.
75 bool AllowAllParametersOfDeclarationOnNextLine;
77 /// \brief Penalty for putting the return type of a function onto its own
79 unsigned PenaltyReturnTypeOnItsOwnLine;
81 /// \brief If the constructor initializers don't fit on a line, put each
82 /// initializer on its own line.
83 bool ConstructorInitializerAllOnOneLineOrOnePerLine;
85 /// \brief If true, "if (a) return;" can be put on a single line.
86 bool AllowShortIfStatementsOnASingleLine;
88 /// \brief Add a space in front of an Objective-C protocol list, i.e. use
89 /// Foo <Protocol> instead of Foo<Protocol>.
90 bool ObjCSpaceBeforeProtocolList;
92 /// \brief If \c true, aligns escaped newlines as far left as possible.
93 /// Otherwise puts them into the right-most column.
94 bool AlignEscapedNewlinesLeft;
97 /// \brief Returns a format style complying with the LLVM coding standards:
98 /// http://llvm.org/docs/CodingStandards.html.
99 FormatStyle getLLVMStyle();
101 /// \brief Returns a format style complying with Google's C++ style guide:
102 /// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml.
103 FormatStyle getGoogleStyle();
105 /// \brief Returns a format style complying with Chromium's style guide:
106 /// http://www.chromium.org/developers/coding-style.
107 FormatStyle getChromiumStyle();
109 /// \brief Returns a format style complying with Mozilla's style guide:
110 /// https://developer.mozilla.org/en-US/docs/Developer_Guide/Coding_Style.
111 FormatStyle getMozillaStyle();
113 /// \brief Reformats the given \p Ranges in the token stream coming out of
116 /// Each range is extended on either end to its next bigger logic unit, i.e.
117 /// everything that might influence its formatting or might be influenced by its
120 /// \param DiagClient A custom DiagnosticConsumer. Can be 0, in this case
121 /// diagnostic is output to llvm::errs().
123 /// Returns the \c Replacements necessary to make all \p Ranges comply with
125 tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
126 SourceManager &SourceMgr,
127 std::vector<CharSourceRange> Ranges,
128 DiagnosticConsumer *DiagClient = 0);
130 /// \brief Returns the \c LangOpts that the formatter expects you to set.
131 LangOptions getFormattingLangOpts();
133 } // end namespace format
134 } // end namespace clang
136 #endif // LLVM_CLANG_FORMAT_FORMAT_H