clang API Documentation

Namespaces | Classes | Enumerations | Functions | Variables
clang::format Namespace Reference

Namespaces

namespace  encoding

Classes

class  final
struct  FormatStyle
 The FormatStyle is used to configure the formatting to follow specific guidelines. More...
class  BreakableToken
 Base class for strategies on how to break tokens. More...
class  BreakableSingleLineToken
 Base class for single line tokens that can be broken. More...
class  BreakableStringLiteral
class  BreakableLineComment
class  BreakableBlockComment
class  ContinuationIndenter
struct  ParenState
struct  LineState
 The current state when indenting a unwrapped line. More...
struct  FormatToken
 A wrapper around a Token storing information about the whitespace characters preceding it. More...
class  TokenRole
class  CommaSeparatedList
struct  AdditionalKeywords
 Encapsulates keywords that are context sensitive or for languages not properly supported by Clang's lexer. More...
class  AnnotatedLine
class  TokenAnnotator
 Determines extra information about the tokens comprising an UnwrappedLine. More...
class  FormatTokenSource
class  ScopedLineState
class  CompoundStatementIndenter
struct  UnwrappedLine
 An unwrapped line is a sequence of Token, that we would like to put on a single line if there was no column limit. More...
class  UnwrappedLineConsumer
class  UnwrappedLineParser
struct  UnwrappedLineNode
class  WhitespaceManager
 Manages the whitespaces around tokens and their replacements. More...

Enumerations

enum  ParseError { Success = 0, Error, Unsuitable }
enum  TokenType {
  TT_ArrayInitializerLSquare, TT_ArraySubscriptLSquare, TT_AttributeParen, TT_BinaryOperator,
  TT_BitFieldColon, TT_BlockComment, TT_CastRParen, TT_ConditionalExpr,
  TT_ConflictAlternative, TT_ConflictEnd, TT_ConflictStart, TT_CtorInitializerColon,
  TT_CtorInitializerComma, TT_DesignatedInitializerPeriod, TT_DictLiteral, TT_FunctionDeclarationName,
  TT_FunctionLBrace, TT_FunctionTypeLParen, TT_ImplicitStringLiteral, TT_InheritanceColon,
  TT_InlineASMColon, TT_JavaAnnotation, TT_LambdaLSquare, TT_LeadingJavaAnnotation,
  TT_LineComment, TT_ObjCBlockLBrace, TT_ObjCBlockLParen, TT_ObjCDecl,
  TT_ObjCForIn, TT_ObjCMethodExpr, TT_ObjCMethodSpecifier, TT_ObjCProperty,
  TT_OverloadedOperator, TT_OverloadedOperatorLParen, TT_PointerOrReference, TT_PureVirtualSpecifier,
  TT_RangeBasedForLoopColon, TT_RegexLiteral, TT_SelectorName, TT_StartOfName,
  TT_TemplateCloser, TT_TemplateOpener, TT_TrailingAnnotation, TT_TrailingReturnArrow,
  TT_TrailingUnaryOperator, TT_UnaryOperator, TT_Unknown
}
enum  BraceBlockKind { BK_Unknown, BK_Block, BK_BracedInit }
enum  ParameterPackingKind { PPK_BinPacked, PPK_OnePerLine, PPK_Inconclusive }
enum  FormatDecision { FD_Unformatted, FD_Continue, FD_Break }
enum  LineType {
  LT_Invalid, LT_Other, LT_PreprocessorDirective, LT_VirtualFunctionDecl,
  LT_ObjCDecl, LT_ObjCMethodDecl, LT_ObjCProperty
}

Functions

const std::error_category & getParseCategory ()
std::error_code make_error_code (ParseError e)
FormatStyle getLLVMStyle ()
 Returns a format style complying with the LLVM coding standards: http://llvm.org/docs/CodingStandards.html.
FormatStyle getGoogleStyle (FormatStyle::LanguageKind Language)
 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 getChromiumStyle (FormatStyle::LanguageKind Language)
 Returns a format style complying with Chromium's style guide: http://www.chromium.org/developers/coding-style.
FormatStyle getMozillaStyle ()
 Returns a format style complying with Mozilla's style guide: https://developer.mozilla.org/en-US/docs/Developer_Guide/Coding_Style.
FormatStyle getWebKitStyle ()
 Returns a format style complying with Webkit's style guide: http://www.webkit.org/coding/coding-style.html.
FormatStyle getGNUStyle ()
 Returns a format style complying with GNU Coding Standards: http://www.gnu.org/prep/standards/standards.html.
FormatStyle getNoStyle ()
 Returns style indicating formatting should be not applied at all.
bool getPredefinedStyle (StringRef Name, FormatStyle::LanguageKind Language, FormatStyle *Style)
 Gets a predefined style for the specified language by name.
std::error_code parseConfiguration (StringRef Text, FormatStyle *Style)
 Parse configuration from YAML-formatted text.
std::string configurationAsText (const FormatStyle &Style)
 Gets configuration in a YAML string.
tooling::Replacements reformat (const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr, ArrayRef< CharSourceRange > Ranges)
 Reformats the given Ranges in the token stream coming out of Lex.
tooling::Replacements reformat (const FormatStyle &Style, SourceManager &SourceMgr, FileID ID, ArrayRef< CharSourceRange > Ranges)
 Reformats the given Ranges in the file ID.
tooling::Replacements reformat (const FormatStyle &Style, StringRef Code, ArrayRef< tooling::Range > Ranges, StringRef FileName="<stdin>")
 Reformats the given Ranges in Code.
LangOptions getFormattingLangOpts (const FormatStyle &Style=getLLVMStyle())
 Returns the LangOpts that the formatter expects you to set.
FormatStyle getStyle (StringRef StyleName, StringRef FileName, StringRef FallbackStyle)
 Construct a FormatStyle based on StyleName.
static bool IsBlank (char C)
static BreakableToken::Split getCommentSplit (StringRef Text, unsigned ContentStartColumn, unsigned ColumnLimit, unsigned TabWidth, encoding::Encoding Encoding)
static BreakableToken::Split getStringSplit (StringRef Text, unsigned UsedColumns, unsigned ColumnLimit, unsigned TabWidth, encoding::Encoding Encoding)
static StringRef getLineCommentIndentPrefix (StringRef Comment)
static unsigned getLengthToMatchingParen (const FormatToken &Tok)
static bool startsSegmentOfBuilderTypeCall (const FormatToken &Tok)
static bool startsNextParameter (const FormatToken &Current, const FormatStyle &Style)
static void consumeRParens (LineState &State, const FormatToken &Tok)
static bool fakeRParenSpecialCase (const LineState &State)
static bool getRawStringLiteralPrefixPostfix (StringRef Text, StringRef &Prefix, StringRef &Postfix)
static FormatStyle::LanguageKind getLanguageByFileName (StringRef FileName)
static unsigned CodePointsBetween (const FormatToken *Begin, const FormatToken *End)
static bool isFunctionDeclarationName (const FormatToken &Current)
static bool isAllmanBrace (const FormatToken &Tok)
static bool IsGoogScope (const UnwrappedLine &Line)
static bool ShouldBreakBeforeBrace (const FormatStyle &Style, const FormatToken &InitialToken)
bool tokenCanStartNewLine (clang::Token Tok)
static LLVM_ATTRIBUTE_UNUSED void printDebugInfo (const UnwrappedLine &Line, StringRef Prefix="")

Variables

const char * StyleOptionHelpDescription = " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""
 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().
static const char *const Blanks = " \t\v\f\r"

Enumeration Type Documentation

Enumerator:
BK_Unknown 
BK_Block 
BK_BracedInit 

Definition at line 79 of file FormatToken.h.

Enumerator:
FD_Unformatted 
FD_Continue 
FD_Break 

Definition at line 92 of file FormatToken.h.

Enumerator:
LT_Invalid 
LT_Other 
LT_PreprocessorDirective 
LT_VirtualFunctionDecl 
LT_ObjCDecl 
LT_ObjCMethodDecl 
LT_ObjCProperty 

Definition at line 28 of file TokenAnnotator.h.

Enumerator:
PPK_BinPacked 
PPK_OnePerLine 
PPK_Inconclusive 

Definition at line 86 of file FormatToken.h.

Enumerator:
Success 
Error 
Unsuitable 

Definition at line 31 of file Format.h.

Enumerator:
TT_ArrayInitializerLSquare 
TT_ArraySubscriptLSquare 
TT_AttributeParen 
TT_BinaryOperator 
TT_BitFieldColon 
TT_BlockComment 
TT_CastRParen 
TT_ConditionalExpr 
TT_ConflictAlternative 
TT_ConflictEnd 
TT_ConflictStart 
TT_CtorInitializerColon 
TT_CtorInitializerComma 
TT_DesignatedInitializerPeriod 
TT_DictLiteral 
TT_FunctionDeclarationName 
TT_FunctionLBrace 
TT_FunctionTypeLParen 
TT_ImplicitStringLiteral 
TT_InheritanceColon 
TT_InlineASMColon 
TT_JavaAnnotation 
TT_LambdaLSquare 
TT_LeadingJavaAnnotation 
TT_LineComment 
TT_ObjCBlockLBrace 
TT_ObjCBlockLParen 
TT_ObjCDecl 
TT_ObjCForIn 
TT_ObjCMethodExpr 
TT_ObjCMethodSpecifier 
TT_ObjCProperty 
TT_OverloadedOperator 
TT_OverloadedOperatorLParen 
TT_PointerOrReference 
TT_PureVirtualSpecifier 
TT_RangeBasedForLoopColon 
TT_RegexLiteral 
TT_SelectorName 
TT_StartOfName 
TT_TemplateCloser 
TT_TemplateOpener 
TT_TrailingAnnotation 
TT_TrailingReturnArrow 
TT_TrailingUnaryOperator 
TT_UnaryOperator 
TT_Unknown 

Definition at line 28 of file FormatToken.h.


Function Documentation

static unsigned clang::format::CodePointsBetween ( const FormatToken *  Begin,
const FormatToken *  End 
) [static]
std::string clang::format::configurationAsText ( const FormatStyle Style)

Gets configuration in a YAML string.

Definition at line 565 of file Format.cpp.

References Style.

static void clang::format::consumeRParens ( LineState &  State,
const FormatToken &  Tok 
) [static]
static bool clang::format::fakeRParenSpecialCase ( const LineState &  State) [static]
FormatStyle clang::format::getChromiumStyle ( FormatStyle::LanguageKind  Language)
static BreakableToken::Split clang::format::getCommentSplit ( StringRef  Text,
unsigned  ContentStartColumn,
unsigned  ColumnLimit,
unsigned  TabWidth,
encoding::Encoding  Encoding 
) [static]
LangOptions clang::format::getFormattingLangOpts ( const FormatStyle Style = getLLVMStyle())

Returns the LangOpts that the formatter expects you to set.

Parameters:
Styledetermines specific settings for lexing mode.

Definition at line 2130 of file Format.cpp.

References clang::format::FormatStyle::Language, and clang::format::FormatStyle::Standard.

FormatStyle clang::format::getGoogleStyle ( FormatStyle::LanguageKind  Language)

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.

Definition at line 390 of file Format.cpp.

References clang::format::FormatStyle::AccessModifierOffset, clang::format::FormatStyle::AlignEscapedNewlinesLeft, clang::format::FormatStyle::AllowShortFunctionsOnASingleLine, clang::format::FormatStyle::AllowShortIfStatementsOnASingleLine, clang::format::FormatStyle::AllowShortLoopsOnASingleLine, clang::format::FormatStyle::AlwaysBreakBeforeMultilineStrings, clang::format::FormatStyle::AlwaysBreakTemplateDeclarations, clang::format::FormatStyle::BOS_NonAssignment, clang::format::FormatStyle::BreakBeforeBinaryOperators, clang::format::FormatStyle::BreakBeforeTernaryOperators, clang::format::FormatStyle::ColumnLimit, clang::format::FormatStyle::ConstructorInitializerAllOnOneLineOrOnePerLine, clang::format::FormatStyle::DerivePointerAlignment, getLLVMStyle(), clang::format::FormatStyle::IndentCaseLabels, clang::format::FormatStyle::KeepEmptyLinesAtTheStartOfBlocks, clang::format::FormatStyle::Language, clang::format::FormatStyle::LK_Java, clang::format::FormatStyle::LK_JavaScript, clang::format::FormatStyle::LK_Proto, clang::format::FormatStyle::LS_Auto, clang::format::FormatStyle::MaxEmptyLinesToKeep, clang::format::FormatStyle::ObjCSpaceAfterProperty, clang::format::FormatStyle::ObjCSpaceBeforeProtocolList, clang::format::FormatStyle::PAS_Left, clang::format::FormatStyle::PenaltyBreakBeforeFirstCallParameter, clang::format::FormatStyle::PenaltyReturnTypeOnItsOwnLine, clang::format::FormatStyle::PointerAlignment, clang::format::FormatStyle::SFS_Inline, clang::format::FormatStyle::SFS_None, clang::format::FormatStyle::SpaceAfterCStyleCast, clang::format::FormatStyle::SpacesBeforeTrailingComments, clang::format::FormatStyle::SpacesInContainerLiterals, and clang::format::FormatStyle::Standard.

Referenced by getChromiumStyle(), and getPredefinedStyle().

static FormatStyle::LanguageKind clang::format::getLanguageByFileName ( StringRef  FileName) [static]

Definition at line 2156 of file Format.cpp.

Referenced by getStyle().

static unsigned clang::format::getLengthToMatchingParen ( const FormatToken &  Tok) [static]
static StringRef clang::format::getLineCommentIndentPrefix ( StringRef  Comment) [static]

Definition at line 185 of file BreakableToken.cpp.

Returns a format style complying with the LLVM coding standards: http://llvm.org/docs/CodingStandards.html.

Definition at line 322 of file Format.cpp.

References clang::format::FormatStyle::AccessModifierOffset, clang::format::FormatStyle::AlignEscapedNewlinesLeft, clang::format::FormatStyle::AlignTrailingComments, clang::format::FormatStyle::AllowAllParametersOfDeclarationOnNextLine, clang::format::FormatStyle::AllowShortBlocksOnASingleLine, clang::format::FormatStyle::AllowShortCaseLabelsOnASingleLine, clang::format::FormatStyle::AllowShortFunctionsOnASingleLine, clang::format::FormatStyle::AllowShortIfStatementsOnASingleLine, clang::format::FormatStyle::AllowShortLoopsOnASingleLine, clang::format::FormatStyle::AlwaysBreakAfterDefinitionReturnType, clang::format::FormatStyle::AlwaysBreakBeforeMultilineStrings, clang::format::FormatStyle::AlwaysBreakTemplateDeclarations, clang::format::FormatStyle::BinPackArguments, clang::format::FormatStyle::BinPackParameters, clang::format::FormatStyle::BOS_None, clang::format::FormatStyle::BreakBeforeBinaryOperators, clang::format::FormatStyle::BreakBeforeBraces, clang::format::FormatStyle::BreakBeforeTernaryOperators, clang::format::FormatStyle::BreakConstructorInitializersBeforeComma, clang::format::FormatStyle::BS_Attach, clang::format::FormatStyle::ColumnLimit, clang::format::FormatStyle::CommentPragmas, clang::format::FormatStyle::ConstructorInitializerAllOnOneLineOrOnePerLine, clang::format::FormatStyle::ConstructorInitializerIndentWidth, clang::format::FormatStyle::ContinuationIndentWidth, clang::format::FormatStyle::Cpp11BracedListStyle, clang::format::FormatStyle::DerivePointerAlignment, clang::format::FormatStyle::DisableFormat, clang::format::FormatStyle::ExperimentalAutoDetectBinPacking, clang::format::FormatStyle::ForEachMacros, clang::format::FormatStyle::IndentCaseLabels, clang::format::FormatStyle::IndentWidth, clang::format::FormatStyle::IndentWrappedFunctionNames, clang::format::FormatStyle::KeepEmptyLinesAtTheStartOfBlocks, clang::format::FormatStyle::Language, clang::format::FormatStyle::LK_Cpp, clang::format::FormatStyle::LS_Cpp11, clang::format::FormatStyle::MaxEmptyLinesToKeep, clang::format::FormatStyle::NamespaceIndentation, clang::format::FormatStyle::NI_None, clang::format::FormatStyle::ObjCBlockIndentWidth, clang::format::FormatStyle::ObjCSpaceAfterProperty, clang::format::FormatStyle::ObjCSpaceBeforeProtocolList, clang::format::FormatStyle::PAS_Right, clang::format::FormatStyle::PenaltyBreakBeforeFirstCallParameter, clang::format::FormatStyle::PenaltyBreakComment, clang::format::FormatStyle::PenaltyBreakFirstLessLess, clang::format::FormatStyle::PenaltyBreakString, clang::format::FormatStyle::PenaltyExcessCharacter, clang::format::FormatStyle::PenaltyReturnTypeOnItsOwnLine, clang::format::FormatStyle::PointerAlignment, clang::format::FormatStyle::SBPO_ControlStatements, clang::format::FormatStyle::SFS_All, clang::format::FormatStyle::SpaceAfterCStyleCast, clang::format::FormatStyle::SpaceBeforeAssignmentOperators, clang::format::FormatStyle::SpaceBeforeParens, clang::format::FormatStyle::SpaceInEmptyParentheses, clang::format::FormatStyle::SpacesBeforeTrailingComments, clang::format::FormatStyle::SpacesInAngles, clang::format::FormatStyle::SpacesInContainerLiterals, clang::format::FormatStyle::SpacesInCStyleCastParentheses, clang::format::FormatStyle::SpacesInParentheses, clang::format::FormatStyle::SpacesInSquareBrackets, clang::format::FormatStyle::Standard, clang::format::FormatStyle::TabWidth, clang::format::FormatStyle::UseTab, and clang::format::FormatStyle::UT_Never.

Referenced by getGNUStyle(), getGoogleStyle(), getMozillaStyle(), getNoStyle(), getPredefinedStyle(), getStyle(), and getWebKitStyle().

Returns style indicating formatting should be not applied at all.

Definition at line 489 of file Format.cpp.

References clang::format::FormatStyle::DisableFormat, and getLLVMStyle().

Referenced by getPredefinedStyle().

const std::error_category & clang::format::getParseCategory ( )

Definition at line 298 of file Format.cpp.

References AttributeLangSupport::C.

Referenced by make_error_code().

bool clang::format::getPredefinedStyle ( StringRef  Name,
FormatStyle::LanguageKind  Language,
FormatStyle Style 
)

Gets a predefined style for the specified language by name.

Currently supported names: LLVM, Google, Chromium, Mozilla. Names are compared case-insensitively.

Returns true if the Style has been set.

Definition at line 495 of file Format.cpp.

References getChromiumStyle(), getGNUStyle(), getGoogleStyle(), getLLVMStyle(), getMozillaStyle(), getNoStyle(), getWebKitStyle(), and clang::format::FormatStyle::Language.

Referenced by getStyle(), and llvm::yaml::MappingTraits< FormatStyle >::mapping().

static bool clang::format::getRawStringLiteralPrefixPostfix ( StringRef  Text,
StringRef &  Prefix,
StringRef &  Postfix 
) [static]

Definition at line 962 of file ContinuationIndenter.cpp.

static BreakableToken::Split clang::format::getStringSplit ( StringRef  Text,
unsigned  UsedColumns,
unsigned  ColumnLimit,
unsigned  TabWidth,
encoding::Encoding  Encoding 
) [static]
FormatStyle clang::format::getStyle ( StringRef  StyleName,
StringRef  FileName,
StringRef  FallbackStyle 
)

Construct a FormatStyle based on StyleName.

StyleName can take several forms:

  • "{<key>: <value>, ...}" - Set specic style parameters.
  • "<style name>" - One of the style names supported by getPredefinedStyle().
  • "file" - Load style configuration from a file called '.clang-format' located in one of the parent directories of FileName or the current directory if FileName is empty.
Parameters:
[in]StyleNameStyle name to interpret according to the description above.
[in]FileNamePath to start search for .clang-format if StyleName == "file".
[in]FallbackStyleThe name of a predefined style used to fallback to in case the style can't be determined from StyleName.
Returns:
FormatStyle as specified by StyleName. If no style could be determined, the default is LLVM Style (see getLLVMStyle()).

Definition at line 2168 of file Format.cpp.

References getLanguageByFileName(), getLLVMStyle(), getPredefinedStyle(), clang::format::FormatStyle::Language, parseConfiguration(), Style, and Unsuitable.

static bool clang::format::isAllmanBrace ( const FormatToken &  Tok) [static]
static bool clang::format::IsBlank ( char  C) [static]

Definition at line 29 of file BreakableToken.cpp.

Referenced by getStringSplit().

static bool clang::format::isFunctionDeclarationName ( const FormatToken &  Current) [static]
static bool clang::format::IsGoogScope ( const UnwrappedLine &  Line) [static]

Definition at line 420 of file UnwrappedLineParser.cpp.

References clang::format::UnwrappedLine::Tokens.

std::error_code clang::format::make_error_code ( ParseError  e)
std::error_code clang::format::parseConfiguration ( StringRef  Text,
FormatStyle Style 
)

Parse configuration from YAML-formatted text.

Style->Language is used to get the base style, if the BasedOnStyle option is present.

When BasedOnStyle is not present, options not present in the YAML document, are retained in Style.

Definition at line 519 of file Format.cpp.

References clang::diag::Error, Input, clang::format::FormatStyle::Language, clang::format::FormatStyle::LK_None, make_error_code(), Success, and Unsuitable.

Referenced by getStyle().

static LLVM_ATTRIBUTE_UNUSED void clang::format::printDebugInfo ( const UnwrappedLine &  Line,
StringRef  Prefix = "" 
) [static]
tooling::Replacements clang::format::reformat ( const FormatStyle Style,
Lexer &  Lex,
SourceManager &  SourceMgr,
ArrayRef< CharSourceRange >  Ranges 
)

Reformats the given Ranges in the token stream coming out of Lex.

DEPRECATED: Do not use.

Definition at line 2084 of file Format.cpp.

References clang::format::FormatStyle::DisableFormat, clang::SourceManager::getFileID(), and clang::Lexer::getSourceLocation().

Referenced by reformat().

tooling::Replacements clang::format::reformat ( const FormatStyle Style,
SourceManager &  SourceMgr,
FileID  ID,
ArrayRef< CharSourceRange >  Ranges 
)

Reformats the given Ranges in the file ID.

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 formatting.

Returns the Replacements necessary to make all Ranges comply with Style.

Definition at line 2093 of file Format.cpp.

References clang::format::FormatStyle::DisableFormat.

tooling::Replacements clang::format::reformat ( const FormatStyle Style,
StringRef  Code,
ArrayRef< tooling::Range >  Ranges,
StringRef  FileName = "<stdin>" 
)
static bool clang::format::ShouldBreakBeforeBrace ( const FormatStyle &  Style,
const FormatToken &  InitialToken 
) [static]
static bool clang::format::startsNextParameter ( const FormatToken &  Current,
const FormatStyle &  Style 
) [static]
static bool clang::format::startsSegmentOfBuilderTypeCall ( const FormatToken &  Tok) [static]

Definition at line 603 of file UnwrappedLineParser.cpp.

References clang::Token::isNot().


Variable Documentation

const char* const clang::format::Blanks = " \t\v\f\r" [static]

Definition at line 28 of file BreakableToken.cpp.

Referenced by getCommentSplit().

const char * clang::format::StyleOptionHelpDescription = " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""

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().

Definition at line 2145 of file Format.cpp.