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.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#ifndef LLVM_CLANG_LIB_FORMAT_TOKENANNOTATOR_H
|
|
|
|
#define LLVM_CLANG_LIB_FORMAT_TOKENANNOTATOR_H
|
2013-01-30 05:01:14 +08:00
|
|
|
|
|
|
|
#include "UnwrappedLineParser.h"
|
|
|
|
#include "clang/Format/Format.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
class SourceManager;
|
|
|
|
|
|
|
|
namespace format {
|
|
|
|
|
|
|
|
enum LineType {
|
|
|
|
LT_Invalid,
|
2014-11-24 00:46:28 +08:00
|
|
|
LT_ImportStatement,
|
2013-01-30 05:01:14 +08:00
|
|
|
LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
|
|
|
|
LT_ObjCMethodDecl,
|
2014-11-24 00:46:28 +08:00
|
|
|
LT_ObjCProperty, // An @property line.
|
|
|
|
LT_Other,
|
|
|
|
LT_PreprocessorDirective,
|
|
|
|
LT_VirtualFunctionDecl
|
2013-01-30 05:01:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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),
|
2015-03-02 02:55:26 +08:00
|
|
|
IsMultiVariableDeclStmt(false), Affected(false),
|
|
|
|
LeadingEmptyLinesAffected(false), ChildrenAffected(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.
|
2014-05-09 16:15:10 +08:00
|
|
|
First->Previous = nullptr;
|
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();
|
2015-06-17 17:43:56 +08:00
|
|
|
for (const auto &Child : Node.Children) {
|
2015-01-22 01:35:29 +08:00
|
|
|
Children.push_back(new AnnotatedLine(Child));
|
2013-09-05 17:29:45 +08:00
|
|
|
Current->Children.push_back(Children.back());
|
|
|
|
}
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
Last = Current;
|
2014-05-09 16:15:10 +08:00
|
|
|
Last->Next = nullptr;
|
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];
|
|
|
|
}
|
2015-01-22 01:35:29 +08:00
|
|
|
FormatToken *Current = First;
|
|
|
|
while (Current) {
|
|
|
|
Current->Children.clear();
|
2015-01-22 02:35:47 +08:00
|
|
|
Current->Role.reset();
|
2015-01-22 01:35:29 +08:00
|
|
|
Current = Current->Next;
|
|
|
|
}
|
2013-09-05 17:29:45 +08:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:43:56 +08:00
|
|
|
/// \c true if this line starts with the given tokens in order, ignoring
|
|
|
|
/// comments.
|
|
|
|
template <typename... Ts> bool startsWith(Ts... Tokens) const {
|
|
|
|
return startsWith(First, Tokens...);
|
|
|
|
}
|
|
|
|
|
2015-12-19 06:20:15 +08:00
|
|
|
/// \c true if this line looks like a function definition instead of a
|
|
|
|
/// function declaration. Asserts MightBeFunctionDecl.
|
|
|
|
bool mightBeFunctionDefinition() const {
|
|
|
|
assert(MightBeFunctionDecl);
|
|
|
|
// FIXME: Line.Last points to other characters than tok::semi
|
|
|
|
// and tok::lbrace.
|
|
|
|
return !Last->isOneOf(tok::semi, tok::comment);
|
|
|
|
}
|
|
|
|
|
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;
|
2015-03-02 02:55:26 +08:00
|
|
|
bool IsMultiVariableDeclStmt;
|
2013-09-05 17:29:45 +08:00
|
|
|
|
2013-11-25 19:08:59 +08:00
|
|
|
/// \c True if this line should be formatted, i.e. intersects directly or
|
|
|
|
/// indirectly with one of the input ranges.
|
|
|
|
bool Affected;
|
|
|
|
|
|
|
|
/// \c True if the leading empty lines of this line intersect with one of the
|
|
|
|
/// input ranges.
|
|
|
|
bool LeadingEmptyLinesAffected;
|
|
|
|
|
2013-11-28 23:58:55 +08:00
|
|
|
/// \c True if a one of this line's children intersects with an input range.
|
|
|
|
bool ChildrenAffected;
|
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
private:
|
|
|
|
// Disallow copying.
|
2015-02-16 06:54:08 +08:00
|
|
|
AnnotatedLine(const AnnotatedLine &) = delete;
|
|
|
|
void operator=(const AnnotatedLine &) = delete;
|
2015-06-17 17:43:56 +08:00
|
|
|
|
|
|
|
template <typename A, typename... Ts>
|
|
|
|
bool startsWith(FormatToken *Tok, A K1) const {
|
|
|
|
while (Tok && Tok->is(tok::comment))
|
|
|
|
Tok = Tok->Next;
|
|
|
|
return Tok && Tok->is(K1);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename A, typename... Ts>
|
|
|
|
bool startsWith(FormatToken *Tok, A K1, Ts... Tokens) const {
|
|
|
|
return startsWith(Tok, K1) && startsWith(Tok->Next, Tokens...);
|
|
|
|
}
|
2013-01-30 05:01:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Determines extra information about the tokens comprising an
|
|
|
|
/// \c UnwrappedLine.
|
|
|
|
class TokenAnnotator {
|
|
|
|
public:
|
2014-11-04 20:41:02 +08:00
|
|
|
TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
|
|
|
|
: Style(Style), Keywords(Keywords) {}
|
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
|
|
|
|
2015-12-19 06:20:15 +08:00
|
|
|
bool mustBreakForReturnType(const AnnotatedLine &Line) const;
|
|
|
|
|
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
|
|
|
|
2014-11-04 20:41:02 +08:00
|
|
|
const AdditionalKeywords &Keywords;
|
2013-01-30 05:01:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace format
|
|
|
|
} // end namespace clang
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#endif
|