2013-01-30 05:01:14 +08:00
|
|
|
//===--- TokenAnnotator.h - Format C++ code ---------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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 a token annotator, i.e. creates
|
|
|
|
/// \c AnnotatedTokens out of \c FormatTokens with required extra information.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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, // An @interface, @implementation, or @protocol line.
|
|
|
|
LT_ObjCMethodDecl,
|
|
|
|
LT_ObjCProperty // An @property line.
|
|
|
|
};
|
|
|
|
|
|
|
|
class AnnotatedLine {
|
|
|
|
public:
|
|
|
|
AnnotatedLine(const UnwrappedLine &Line)
|
2013-09-05 17:29:45 +08:00
|
|
|
: First(Line.Tokens.front().Tok), Level(Line.Level),
|
2013-01-30 05:01:14 +08:00
|
|
|
InPPDirective(Line.InPPDirective),
|
2013-05-06 16:27:33 +08:00
|
|
|
MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
|
|
|
|
StartsDefinition(false) {
|
2013-01-30 05:01:14 +08:00
|
|
|
assert(!Line.Tokens.empty());
|
2013-10-21 16:11:15 +08:00
|
|
|
|
|
|
|
// Calculate Next and Previous for all tokens. Note that we must overwrite
|
|
|
|
// Next and Previous for every token, as previous formatting runs might have
|
|
|
|
// left them in a different state.
|
|
|
|
First->Previous = NULL;
|
2013-05-29 22:47:47 +08:00
|
|
|
FormatToken *Current = First;
|
2013-09-05 17:29:45 +08:00
|
|
|
for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
|
|
|
|
E = Line.Tokens.end();
|
2013-01-30 05:01:14 +08:00
|
|
|
I != E; ++I) {
|
2013-09-05 17:29:45 +08:00
|
|
|
const UnwrappedLineNode &Node = *I;
|
|
|
|
Current->Next = I->Tok;
|
|
|
|
I->Tok->Previous = Current;
|
2013-05-29 22:47:47 +08:00
|
|
|
Current = Current->Next;
|
2013-10-24 23:23:11 +08:00
|
|
|
Current->Children.clear();
|
2013-09-05 17:29:45 +08:00
|
|
|
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());
|
|
|
|
}
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
Last = Current;
|
2013-10-21 16:11:15 +08:00
|
|
|
Last->Next = NULL;
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
~AnnotatedLine() {
|
|
|
|
for (unsigned i = 0, e = Children.size(); i != e; ++i) {
|
|
|
|
delete Children[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-29 22:47:47 +08:00
|
|
|
FormatToken *First;
|
|
|
|
FormatToken *Last;
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2013-09-06 15:54:20 +08:00
|
|
|
SmallVector<AnnotatedLine *, 0> Children;
|
2013-09-05 17:29:45 +08:00
|
|
|
|
2013-01-30 05:01:14 +08:00
|
|
|
LineType Type;
|
|
|
|
unsigned Level;
|
|
|
|
bool InPPDirective;
|
|
|
|
bool MustBeDeclaration;
|
Allow breaking between a type and name in variable declarations.
This fixes llvm.org/PR14967 and is generall necessary to avoid
situations where the column limit is exceeded. The challenge is
restricting such lines splits, otherwise clang-format suddenly starts
breaking at bad places.
Before:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string,
SomeOtherTemplateParameter> &ReallyReallyLongParameterName,
const SomeType<string,
SomeOtherTemplateParameter> &AnotherLongParameterName) {}
After:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string, SomeOtherTemplateParameter> &
ReallyReallyLongParameterName,
const SomeType<string, SomeOtherTemplateParameter> &
AnotherLongParameterName) {}
llvm-svn: 175999
2013-02-25 02:54:32 +08:00
|
|
|
bool MightBeFunctionDecl;
|
2013-05-06 16:27:33 +08:00
|
|
|
bool StartsDefinition;
|
2013-09-05 17:29:45 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Disallow copying.
|
|
|
|
AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
|
|
|
|
void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
|
2013-01-30 05:01:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Determines extra information about the tokens comprising an
|
|
|
|
/// \c UnwrappedLine.
|
|
|
|
class TokenAnnotator {
|
|
|
|
public:
|
2013-06-05 22:09:10 +08:00
|
|
|
TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
|
|
|
|
: Style(Style), Ident_in(Ident_in) {}
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2013-09-06 15:54:20 +08:00
|
|
|
/// \brief Adapts the indent levels of comment lines to the indent of the
|
|
|
|
/// subsequent line.
|
|
|
|
// FIXME: Can/should this be done in the UnwrappedLineParser?
|
|
|
|
void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
|
|
|
|
|
2013-02-06 22:22:40 +08:00
|
|
|
void annotate(AnnotatedLine &Line);
|
|
|
|
void calculateFormattingInformation(AnnotatedLine &Line);
|
2013-01-30 05:01:14 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// \brief Calculate the penalty for splitting before \c Tok.
|
2013-11-08 01:52:51 +08:00
|
|
|
unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
|
|
|
|
bool InFunctionDecl);
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2013-05-29 22:47:47 +08:00
|
|
|
bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
|
|
|
|
const FormatToken &Right);
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2013-05-29 22:47:47 +08:00
|
|
|
bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2013-09-17 17:52:48 +08:00
|
|
|
bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
|
|
|
|
|
2013-05-29 22:47:47 +08:00
|
|
|
bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2013-04-09 04:33:42 +08:00
|
|
|
void printDebugInfo(const AnnotatedLine &Line);
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
void calculateUnbreakableTailLengths(AnnotatedLine &Line);
|
|
|
|
|
2013-02-06 22:22:40 +08:00
|
|
|
const FormatStyle &Style;
|
2013-02-11 23:32:15 +08:00
|
|
|
|
|
|
|
// Contextual keywords:
|
|
|
|
IdentifierInfo &Ident_in;
|
2013-01-30 05:01:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace format
|
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
|