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_BuilderTypeCall,
|
|
|
|
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)
|
|
|
|
: First(Line.Tokens.front()), Level(Line.Level),
|
|
|
|
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-05-29 22:47:47 +08:00
|
|
|
FormatToken *Current = First;
|
2013-05-28 21:42:28 +08:00
|
|
|
for (std::list<FormatToken *>::const_iterator I = ++Line.Tokens.begin(),
|
|
|
|
E = Line.Tokens.end();
|
2013-01-30 05:01:14 +08:00
|
|
|
I != E; ++I) {
|
2013-05-29 22:47:47 +08:00
|
|
|
Current->Next = *I;
|
|
|
|
(*I)->Previous = Current;
|
|
|
|
Current = Current->Next;
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
Last = Current;
|
|
|
|
}
|
|
|
|
|
2013-05-29 22:47:47 +08:00
|
|
|
FormatToken *First;
|
|
|
|
FormatToken *Last;
|
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-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-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-05-29 22:47:47 +08:00
|
|
|
unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok);
|
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-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
|