2013-01-30 05:01:14 +08:00
|
|
|
//===--- TokenAnnotator.h - Format C++ code ---------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-01-30 05:01:14 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-09 09:00:01 +08:00
|
|
|
/// This file implements a token annotator, i.e. creates
|
2013-01-30 05:01:14 +08:00
|
|
|
/// \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"
|
|
|
|
|
|
|
|
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),
|
2017-02-27 21:28:36 +08:00
|
|
|
MatchingOpeningBlockLineIndex(Line.MatchingOpeningBlockLineIndex),
|
2018-04-23 17:34:26 +08:00
|
|
|
MatchingClosingBlockLineIndex(Line.MatchingClosingBlockLineIndex),
|
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),
|
2017-10-30 22:01:50 +08:00
|
|
|
LeadingEmptyLinesAffected(false), ChildrenAffected(false),
|
|
|
|
FirstStartColumn(Line.FirstStartColumn) {
|
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 {
|
2016-05-29 22:41:02 +08:00
|
|
|
return First && First->startsSequence(Tokens...);
|
2015-06-17 17:43:56 +08:00
|
|
|
}
|
|
|
|
|
2016-04-25 23:09:22 +08:00
|
|
|
/// \c true if this line ends with the given tokens in reversed order,
|
|
|
|
/// ignoring comments.
|
|
|
|
/// For example, given tokens [T1, T2, T3, ...], the function returns true if
|
|
|
|
/// this line is like "... T3 T2 T1".
|
|
|
|
template <typename... Ts> bool endsWith(Ts... Tokens) const {
|
2016-05-29 22:41:02 +08:00
|
|
|
return Last && Last->endsSequence(Tokens...);
|
2016-04-25 23:09:22 +08:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:44:02 +08:00
|
|
|
/// \c true if this line starts a namespace definition.
|
|
|
|
bool startsWithNamespace() const {
|
|
|
|
return startsWith(tok::kw_namespace) ||
|
|
|
|
startsWith(tok::kw_inline, tok::kw_namespace) ||
|
|
|
|
startsWith(tok::kw_export, tok::kw_namespace);
|
|
|
|
}
|
|
|
|
|
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;
|
2017-02-27 21:28:36 +08:00
|
|
|
size_t MatchingOpeningBlockLineIndex;
|
2018-04-23 17:34:26 +08:00
|
|
|
size_t MatchingClosingBlockLineIndex;
|
2013-01-30 05:01:14 +08:00
|
|
|
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;
|
|
|
|
|
2017-02-09 17:02:13 +08:00
|
|
|
/// \c True if one of this line's children intersects with an input range.
|
2013-11-28 23:58:55 +08:00
|
|
|
bool ChildrenAffected;
|
|
|
|
|
2017-10-30 22:01:50 +08:00
|
|
|
unsigned FirstStartColumn;
|
|
|
|
|
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;
|
2013-01-30 05:01:14 +08:00
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Determines extra information about the tokens comprising an
|
2013-01-30 05:01:14 +08:00
|
|
|
/// \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
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Adapts the indent levels of comment lines to the indent of the
|
2013-09-06 15:54:20 +08:00
|
|
|
/// 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:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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
|
|
|
|
2019-03-30 20:32:35 +08:00
|
|
|
bool spaceRequiredBeforeParens(const FormatToken &Right) const;
|
|
|
|
|
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
|
|
|
|
2018-04-23 18:02:59 +08:00
|
|
|
bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Right);
|
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
|