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 If \c true, analyze the formatted file for C++03 compatibility.
61 bool DeriveBackwardsCompatibility;
63 /// \brief Indent case labels one level from the switch statement.
65 /// When false, use the same indentation level as for the switch statement.
66 /// Switch statement body is always indented one level more than case labels.
67 bool IndentCaseLabels;
69 /// \brief The number of spaces to before trailing line comments.
70 unsigned SpacesBeforeTrailingComments;
72 /// \brief If false, a function call's or function definition's parameters
73 /// will either all be on the same line or will have one line each.
74 bool BinPackParameters;
76 /// \brief Allow putting all parameters of a function declaration onto
77 /// the next line even if \c BinPackParameters is \c false.
78 bool AllowAllParametersOfDeclarationOnNextLine;
80 /// \brief Penalty for putting the return type of a function onto its own
82 unsigned PenaltyReturnTypeOnItsOwnLine;
84 /// \brief If the constructor initializers don't fit on a line, put each
85 /// initializer on its own line.
86 bool ConstructorInitializerAllOnOneLineOrOnePerLine;
88 /// \brief If true, "if (a) return;" can be put on a single line.
89 bool AllowShortIfStatementsOnASingleLine;
91 /// \brief Add a space in front of an Objective-C protocol list, i.e. use
92 /// Foo <Protocol> instead of Foo<Protocol>.
93 bool ObjCSpaceBeforeProtocolList;
96 /// \brief Returns a format style complying with the LLVM coding standards:
97 /// http://llvm.org/docs/CodingStandards.html.
98 FormatStyle getLLVMStyle();
100 /// \brief Returns a format style complying with Google's C++ style guide:
101 /// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml.
102 FormatStyle getGoogleStyle();
104 /// \brief Returns a format style complying with Chromium's style guide:
105 /// http://www.chromium.org/developers/coding-style.
106 FormatStyle getChromiumStyle();
108 /// \brief Reformats the given \p Ranges in the token stream coming out of
111 /// Each range is extended on either end to its next bigger logic unit, i.e.
112 /// everything that might influence its formatting or might be influenced by its
115 /// \param DiagClient A custom DiagnosticConsumer. Can be 0, in this case
116 /// diagnostic is output to llvm::errs().
118 /// Returns the \c Replacements necessary to make all \p Ranges comply with
120 tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
121 SourceManager &SourceMgr,
122 std::vector<CharSourceRange> Ranges,
123 DiagnosticConsumer *DiagClient = 0);
125 /// \brief Returns the \c LangOpts that the formatter expects you to set.
126 LangOptions getFormattingLangOpts();
128 } // end namespace format
129 } // end namespace clang
131 #endif // LLVM_CLANG_FORMAT_FORMAT_H