#ifndef LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
#define LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
#include "UnwrappedLineParser.h"
#include "clang/Format/Format.h"
#include <string>
namespace clang {
class SourceManager;
namespace format {
enum LineType {
LT_Invalid,
LT_Other,
LT_PreprocessorDirective,
LT_VirtualFunctionDecl,
LT_ObjCDecl, LT_ObjCMethodDecl,
LT_ObjCProperty };
class AnnotatedLine {
public:
AnnotatedLine(const UnwrappedLine &Line)
: First(Line.Tokens.front().Tok), Level(Line.Level),
InPPDirective(Line.InPPDirective),
MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
StartsDefinition(false), Affected(false),
LeadingEmptyLinesAffected(false), ChildrenAffected(false) {
assert(!Line.Tokens.empty());
First->Previous = NULL;
FormatToken *Current = First;
for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
E = Line.Tokens.end();
I != E; ++I) {
const UnwrappedLineNode &Node = *I;
Current->Next = I->Tok;
I->Tok->Previous = Current;
Current = Current->Next;
Current->Children.clear();
for (SmallVectorImpl<UnwrappedLine>::const_iterator
I = Node.Children.begin(),
E = Node.Children.end();
I != E; ++I) {
Children.push_back(new AnnotatedLine(*I));
Current->Children.push_back(Children.back());
}
}
Last = Current;
Last->Next = NULL;
}
~AnnotatedLine() {
for (unsigned i = 0, e = Children.size(); i != e; ++i) {
delete Children[i];
}
}
FormatToken *First;
FormatToken *Last;
SmallVector<AnnotatedLine *, 0> Children;
LineType Type;
unsigned Level;
bool InPPDirective;
bool MustBeDeclaration;
bool MightBeFunctionDecl;
bool StartsDefinition;
bool Affected;
bool LeadingEmptyLinesAffected;
bool ChildrenAffected;
private:
AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
};
class TokenAnnotator {
public:
TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
: Style(Style), Ident_in(Ident_in) {}
void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
void annotate(AnnotatedLine &Line);
void calculateFormattingInformation(AnnotatedLine &Line);
private:
unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
bool InFunctionDecl);
bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
const FormatToken &Right);
bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
void printDebugInfo(const AnnotatedLine &Line);
void calculateUnbreakableTailLengths(AnnotatedLine &Line);
const FormatStyle &Style;
IdentifierInfo &Ident_in;
};
} }
#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H