Home | History | Annotate | Download | only in Format
      1 //===--- Format.h - Format C++ code -----------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 ///
     10 /// \file
     11 /// Various functions to configurably format source code.
     12 ///
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_CLANG_FORMAT_FORMAT_H
     16 #define LLVM_CLANG_FORMAT_FORMAT_H
     17 
     18 #include "clang/Frontend/FrontendAction.h"
     19 #include "clang/Tooling/Refactoring.h"
     20 
     21 namespace clang {
     22 
     23 class Lexer;
     24 class SourceManager;
     25 class DiagnosticConsumer;
     26 
     27 namespace format {
     28 
     29 /// \brief The \c FormatStyle is used to configure the formatting to follow
     30 /// specific guidelines.
     31 struct FormatStyle {
     32   /// \brief The column limit.
     33   unsigned ColumnLimit;
     34 
     35   /// \brief The penalty for each character outside of the column limit.
     36   unsigned PenaltyExcessCharacter;
     37 
     38   /// \brief The maximum number of consecutive empty lines to keep.
     39   unsigned MaxEmptyLinesToKeep;
     40 
     41   /// \brief Set whether & and * bind to the type as opposed to the variable.
     42   bool PointerBindsToType;
     43 
     44   /// \brief If \c true, analyze the formatted file for the most common binding.
     45   bool DerivePointerBinding;
     46 
     47   /// \brief The extra indent or outdent of access modifiers (e.g.: public:).
     48   int AccessModifierOffset;
     49 
     50   enum LanguageStandard {
     51     LS_Cpp03,
     52     LS_Cpp11,
     53     LS_Auto
     54   };
     55 
     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;
     59 
     60   /// \brief If \c true, analyze the formatted file for C++03 compatibility.
     61   bool DeriveBackwardsCompatibility;
     62 
     63   /// \brief Indent case labels one level from the switch statement.
     64   ///
     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;
     68 
     69   /// \brief The number of spaces to before trailing line comments.
     70   unsigned SpacesBeforeTrailingComments;
     71 
     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;
     75 
     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;
     79 
     80   /// \brief Penalty for putting the return type of a function onto its own
     81   /// line.
     82   unsigned PenaltyReturnTypeOnItsOwnLine;
     83 
     84   /// \brief If the constructor initializers don't fit on a line, put each
     85   /// initializer on its own line.
     86   bool ConstructorInitializerAllOnOneLineOrOnePerLine;
     87 
     88   /// \brief If true, "if (a) return;" can be put on a single line.
     89   bool AllowShortIfStatementsOnASingleLine;
     90 
     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;
     94 };
     95 
     96 /// \brief Returns a format style complying with the LLVM coding standards:
     97 /// http://llvm.org/docs/CodingStandards.html.
     98 FormatStyle getLLVMStyle();
     99 
    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();
    103 
    104 /// \brief Returns a format style complying with Chromium's style guide:
    105 /// http://www.chromium.org/developers/coding-style.
    106 FormatStyle getChromiumStyle();
    107 
    108 /// \brief Reformats the given \p Ranges in the token stream coming out of
    109 /// \c Lex.
    110 ///
    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
    113 /// formatting.
    114 ///
    115 /// \param DiagClient A custom DiagnosticConsumer. Can be 0, in this case
    116 /// diagnostic is output to llvm::errs().
    117 ///
    118 /// Returns the \c Replacements necessary to make all \p Ranges comply with
    119 /// \p Style.
    120 tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
    121                                SourceManager &SourceMgr,
    122                                std::vector<CharSourceRange> Ranges,
    123                                DiagnosticConsumer *DiagClient = 0);
    124 
    125 /// \brief Returns the \c LangOpts that the formatter expects you to set.
    126 LangOptions getFormattingLangOpts();
    127 
    128 } // end namespace format
    129 } // end namespace clang
    130 
    131 #endif // LLVM_CLANG_FORMAT_FORMAT_H
    132