//===--- Format.cpp - Format C++ code -------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file implements functions declared in Format.h. This will be
/// split into separate files as we go.
///
/// This is EXPERIMENTAL code under heavy development. It is not in a state yet,
/// where it can be used to format real code.
///
//===----------------------------------------------------------------------===//
#include "clang/Format/Format.h"
#include "UnwrappedLineParser.h"
#include "clang/Basic/OperatorPrecedence.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
#include <string>
namespace clang {
namespace format {
enum TokenType {
TT_BinaryOperator,
TT_BlockComment,
TT_CastRParen,
TT_ConditionalExpr,
TT_CtorInitializerColon,
TT_DirectorySeparator,
TT_LineComment,
TT_ObjCMethodSpecifier,
TT_OverloadedOperator,
TT_PointerOrReference,
TT_PureVirtualSpecifier,
TT_TemplateCloser,
TT_TemplateOpener,
TT_TrailingUnaryOperator,
TT_UnaryOperator,
TT_Unknown
};
enum LineType {
LT_Invalid,
LT_Other,
LT_PreprocessorDirective,
LT_VirtualFunctionDecl,
LT_ObjCMethodDecl
};
class AnnotatedToken {
public:
AnnotatedToken(const FormatToken &FormatTok)
: FormatTok(FormatTok), Type(TT_Unknown),
ClosesTemplateDeclaration(false), Parent(NULL) {
}
bool is(tok::TokenKind Kind) const {
return FormatTok.Tok.is(Kind);
}
bool isNot(tok::TokenKind Kind) const {
return FormatTok.Tok.isNot(Kind);
}
bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
return FormatTok.Tok.isObjCAtKeyword(Kind);
}
FormatToken FormatTok;
TokenType Type;
bool SpaceRequiredBefore;
bool CanBreakBefore;
bool MustBreakBefore;
bool ClosesTemplateDeclaration;
std::vector<AnnotatedToken> Children;
AnnotatedToken *Parent;
};
static prec::Level getPrecedence(const AnnotatedToken &Tok) {
return getBinOpPrecedence(Tok.FormatTok.Tok.getKind(), true, true);
}
using llvm::MutableArrayRef;
FormatStyle getLLVMStyle() {
FormatStyle LLVMStyle;
LLVMStyle.ColumnLimit = 80;
LLVMStyle.MaxEmptyLinesToKeep = 1;
LLVMStyle.PointerAndReferenceBindToType = false;
LLVMStyle.AccessModifierOffset = -2;
LLVMStyle.SplitTemplateClosingGreater = true;
LLVMStyle.IndentCaseLabels = false;
LLVMStyle.SpacesBeforeTrailingComments = 1;
return LLVMStyle;
}
FormatStyle