43 changes: 21 additions & 22 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> {
}
};

template <> struct ScalarEnumerationTraits<FormatStyle::BreakConstructorInitializersStyle> {
static void enumeration(IO &IO, FormatStyle::BreakConstructorInitializersStyle &Value) {
template <>
struct ScalarEnumerationTraits<FormatStyle::BreakConstructorInitializersStyle> {
static void
enumeration(IO &IO, FormatStyle::BreakConstructorInitializersStyle &Value) {
IO.enumCase(Value, "BeforeColon", FormatStyle::BCIS_BeforeColon);
IO.enumCase(Value, "BeforeComma", FormatStyle::BCIS_BeforeComma);
IO.enumCase(Value, "AfterColon", FormatStyle::BCIS_AfterColon);
Expand Down Expand Up @@ -189,8 +191,10 @@ template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle> {
}
};

template <> struct ScalarEnumerationTraits<FormatStyle::EscapedNewlineAlignmentStyle> {
static void enumeration(IO &IO, FormatStyle::EscapedNewlineAlignmentStyle &Value) {
template <>
struct ScalarEnumerationTraits<FormatStyle::EscapedNewlineAlignmentStyle> {
static void enumeration(IO &IO,
FormatStyle::EscapedNewlineAlignmentStyle &Value) {
IO.enumCase(Value, "DontAlign", FormatStyle::ENAS_DontAlign);
IO.enumCase(Value, "Left", FormatStyle::ENAS_Left);
IO.enumCase(Value, "Right", FormatStyle::ENAS_Right);
Expand Down Expand Up @@ -374,8 +378,7 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty);
IO.mapOptional("ObjCSpaceBeforeProtocolList",
Style.ObjCSpaceBeforeProtocolList);
IO.mapOptional("PenaltyBreakAssignment",
Style.PenaltyBreakAssignment);
IO.mapOptional("PenaltyBreakAssignment", Style.PenaltyBreakAssignment);
IO.mapOptional("PenaltyBreakBeforeFirstCallParameter",
Style.PenaltyBreakBeforeFirstCallParameter);
IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
Expand All @@ -390,7 +393,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("SortIncludes", Style.SortIncludes);
IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
IO.mapOptional("SpaceAfterTemplateKeyword", Style.SpaceAfterTemplateKeyword);
IO.mapOptional("SpaceAfterTemplateKeyword",
Style.SpaceAfterTemplateKeyword);
IO.mapOptional("SpaceBeforeAssignmentOperators",
Style.SpaceBeforeAssignmentOperators);
IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens);
Expand Down Expand Up @@ -501,9 +505,9 @@ static FormatStyle expandPresets(const FormatStyle &Style) {
if (Style.BreakBeforeBraces == FormatStyle::BS_Custom)
return Style;
FormatStyle Expanded = Style;
Expanded.BraceWrapping = {false, false, false, false, false, false,
false, false, false, false, false, false,
true, true, true};
Expanded.BraceWrapping = {false, false, false, false, false,
false, false, false, false, false,
false, false, true, true, true};
switch (Style.BreakBeforeBraces) {
case FormatStyle::BS_Linux:
Expanded.BraceWrapping.AfterClass = true;
Expand Down Expand Up @@ -538,9 +542,8 @@ static FormatStyle expandPresets(const FormatStyle &Style) {
Expanded.BraceWrapping.BeforeElse = true;
break;
case FormatStyle::BS_GNU:
Expanded.BraceWrapping = {true, true, true, true, true, true,
true, true, true, true, true, true,
true, true, true};
Expanded.BraceWrapping = {true, true, true, true, true, true, true, true,
true, true, true, true, true, true, true};
break;
case FormatStyle::BS_WebKit:
Expanded.BraceWrapping.AfterFunction = true;
Expand Down Expand Up @@ -576,9 +579,9 @@ FormatStyle getLLVMStyle() {
LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
LLVMStyle.BreakBeforeTernaryOperators = true;
LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
LLVMStyle.BraceWrapping = {false, false, false, false, false, false,
false, false, false, false, false, false,
true, true, true};
LLVMStyle.BraceWrapping = {false, false, false, false, false,
false, false, false, false, false,
false, false, true, true, true};
LLVMStyle.BreakAfterJavaFieldAnnotations = false;
LLVMStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
LLVMStyle.BreakBeforeInheritanceComma = false;
Expand Down Expand Up @@ -738,8 +741,7 @@ FormatStyle getMozillaStyle() {
FormatStyle MozillaStyle = getLLVMStyle();
MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
MozillaStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
MozillaStyle.AlwaysBreakAfterReturnType =
FormatStyle::RTBS_TopLevel;
MozillaStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
MozillaStyle.AlwaysBreakAfterDefinitionReturnType =
FormatStyle::DRTBS_TopLevel;
MozillaStyle.AlwaysBreakTemplateDeclarations = true;
Expand Down Expand Up @@ -1010,7 +1012,6 @@ class Formatter : public TokenAnalyzer {
}

private:

static bool inputUsesCRLF(StringRef Text) {
return Text.count('\r') * 2 > Text.count('\n');
}
Expand Down Expand Up @@ -1552,9 +1553,7 @@ bool isMpegTS(StringRef Code) {
return Code.size() > 188 && Code[0] == 0x47 && Code[188] == 0x47;
}

bool isLikelyXml(StringRef Code) {
return Code.ltrim().startswith("<");
}
bool isLikelyXml(StringRef Code) { return Code.ltrim().startswith("<"); }

tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
ArrayRef<tooling::Range> Ranges,
Expand Down
5 changes: 2 additions & 3 deletions clang/lib/Format/FormatToken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ namespace format {
const char *getTokenTypeName(TokenType Type) {
static const char *const TokNames[] = {
#define TYPE(X) #X,
LIST_TOKEN_TYPES
LIST_TOKEN_TYPES
#undef TYPE
nullptr
};
nullptr};

if (Type < NUM_TOKEN_TYPES)
return TokNames[Type];
Expand Down
153 changes: 76 additions & 77 deletions clang/lib/Format/FormatToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,79 +26,79 @@
namespace clang {
namespace format {

#define LIST_TOKEN_TYPES \
TYPE(ArrayInitializerLSquare) \
TYPE(ArraySubscriptLSquare) \
TYPE(AttributeParen) \
TYPE(BinaryOperator) \
TYPE(BitFieldColon) \
TYPE(BlockComment) \
TYPE(CastRParen) \
TYPE(ConditionalExpr) \
TYPE(ConflictAlternative) \
TYPE(ConflictEnd) \
TYPE(ConflictStart) \
TYPE(CtorInitializerColon) \
TYPE(CtorInitializerComma) \
TYPE(DesignatedInitializerLSquare) \
TYPE(DesignatedInitializerPeriod) \
TYPE(DictLiteral) \
TYPE(ForEachMacro) \
TYPE(FunctionAnnotationRParen) \
TYPE(FunctionDeclarationName) \
TYPE(FunctionLBrace) \
TYPE(FunctionTypeLParen) \
TYPE(ImplicitStringLiteral) \
TYPE(InheritanceColon) \
TYPE(InheritanceComma) \
TYPE(InlineASMBrace) \
TYPE(InlineASMColon) \
TYPE(JavaAnnotation) \
TYPE(JsComputedPropertyName) \
TYPE(JsExponentiation) \
TYPE(JsExponentiationEqual) \
TYPE(JsFatArrow) \
TYPE(JsNonNullAssertion) \
TYPE(JsTypeColon) \
TYPE(JsTypeOperator) \
TYPE(JsTypeOptionalQuestion) \
TYPE(LambdaArrow) \
TYPE(LambdaLSquare) \
TYPE(LeadingJavaAnnotation) \
TYPE(LineComment) \
TYPE(MacroBlockBegin) \
TYPE(MacroBlockEnd) \
TYPE(ObjCBlockLBrace) \
TYPE(ObjCBlockLParen) \
TYPE(ObjCDecl) \
TYPE(ObjCForIn) \
TYPE(ObjCMethodExpr) \
TYPE(ObjCMethodSpecifier) \
TYPE(ObjCProperty) \
TYPE(ObjCStringLiteral) \
TYPE(OverloadedOperator) \
TYPE(OverloadedOperatorLParen) \
TYPE(PointerOrReference) \
TYPE(PureVirtualSpecifier) \
TYPE(RangeBasedForLoopColon) \
TYPE(RegexLiteral) \
TYPE(SelectorName) \
TYPE(StartOfName) \
TYPE(StructuredBindingLSquare) \
TYPE(TemplateCloser) \
TYPE(TemplateOpener) \
TYPE(TemplateString) \
TYPE(TrailingAnnotation) \
TYPE(TrailingReturnArrow) \
TYPE(TrailingUnaryOperator) \
TYPE(UnaryOperator) \
#define LIST_TOKEN_TYPES \
TYPE(ArrayInitializerLSquare) \
TYPE(ArraySubscriptLSquare) \
TYPE(AttributeParen) \
TYPE(BinaryOperator) \
TYPE(BitFieldColon) \
TYPE(BlockComment) \
TYPE(CastRParen) \
TYPE(ConditionalExpr) \
TYPE(ConflictAlternative) \
TYPE(ConflictEnd) \
TYPE(ConflictStart) \
TYPE(CtorInitializerColon) \
TYPE(CtorInitializerComma) \
TYPE(DesignatedInitializerLSquare) \
TYPE(DesignatedInitializerPeriod) \
TYPE(DictLiteral) \
TYPE(ForEachMacro) \
TYPE(FunctionAnnotationRParen) \
TYPE(FunctionDeclarationName) \
TYPE(FunctionLBrace) \
TYPE(FunctionTypeLParen) \
TYPE(ImplicitStringLiteral) \
TYPE(InheritanceColon) \
TYPE(InheritanceComma) \
TYPE(InlineASMBrace) \
TYPE(InlineASMColon) \
TYPE(JavaAnnotation) \
TYPE(JsComputedPropertyName) \
TYPE(JsExponentiation) \
TYPE(JsExponentiationEqual) \
TYPE(JsFatArrow) \
TYPE(JsNonNullAssertion) \
TYPE(JsTypeColon) \
TYPE(JsTypeOperator) \
TYPE(JsTypeOptionalQuestion) \
TYPE(LambdaArrow) \
TYPE(LambdaLSquare) \
TYPE(LeadingJavaAnnotation) \
TYPE(LineComment) \
TYPE(MacroBlockBegin) \
TYPE(MacroBlockEnd) \
TYPE(ObjCBlockLBrace) \
TYPE(ObjCBlockLParen) \
TYPE(ObjCDecl) \
TYPE(ObjCForIn) \
TYPE(ObjCMethodExpr) \
TYPE(ObjCMethodSpecifier) \
TYPE(ObjCProperty) \
TYPE(ObjCStringLiteral) \
TYPE(OverloadedOperator) \
TYPE(OverloadedOperatorLParen) \
TYPE(PointerOrReference) \
TYPE(PureVirtualSpecifier) \
TYPE(RangeBasedForLoopColon) \
TYPE(RegexLiteral) \
TYPE(SelectorName) \
TYPE(StartOfName) \
TYPE(StructuredBindingLSquare) \
TYPE(TemplateCloser) \
TYPE(TemplateOpener) \
TYPE(TemplateString) \
TYPE(TrailingAnnotation) \
TYPE(TrailingReturnArrow) \
TYPE(TrailingUnaryOperator) \
TYPE(UnaryOperator) \
TYPE(Unknown)

enum TokenType {
#define TYPE(X) TT_##X,
LIST_TOKEN_TYPES
LIST_TOKEN_TYPES
#undef TYPE
NUM_TOKEN_TYPES
NUM_TOKEN_TYPES
};

/// \brief Determines the name of a token type.
Expand Down Expand Up @@ -341,10 +341,11 @@ struct FormatToken {
bool isSimpleTypeSpecifier() const;

bool isObjCAccessSpecifier() const {
return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) ||
Next->isObjCAtKeyword(tok::objc_protected) ||
Next->isObjCAtKeyword(tok::objc_package) ||
Next->isObjCAtKeyword(tok::objc_private));
return is(tok::at) && Next &&
(Next->isObjCAtKeyword(tok::objc_public) ||
Next->isObjCAtKeyword(tok::objc_protected) ||
Next->isObjCAtKeyword(tok::objc_package) ||
Next->isObjCAtKeyword(tok::objc_private));
}

/// \brief Returns whether \p Tok is ([{ or a template opening <.
Expand Down Expand Up @@ -477,7 +478,7 @@ struct FormatToken {
bool isCppStructuredBinding(const FormatStyle &Style) const {
if (!Style.isCpp() || isNot(tok::l_square))
return false;
const FormatToken* T = this;
const FormatToken *T = this;
do {
T = T->getPreviousNonComment();
} while (T && T->isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
Expand Down Expand Up @@ -517,15 +518,13 @@ struct FormatToken {
return is(K1) && Next && Next->startsSequenceInternal(Tokens...);
}

template <typename A>
bool startsSequenceInternal(A K1) const {
template <typename A> bool startsSequenceInternal(A K1) const {
if (is(tok::comment) && Next)
return Next->startsSequenceInternal(K1);
return is(K1);
}

template <typename A, typename... Ts>
bool endsSequenceInternal(A K1) const {
template <typename A, typename... Ts> bool endsSequenceInternal(A K1) const {
if (is(tok::comment) && Previous)
return Previous->endsSequenceInternal(K1);
return is(K1);
Expand Down
15 changes: 6 additions & 9 deletions clang/lib/Format/FormatTokenLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,10 @@ void FormatTokenLexer::tryMergePreviousTokens() {
}

if (Style.Language == FormatStyle::LK_Java) {
static const tok::TokenKind JavaRightLogicalShift[] = {tok::greater,
tok::greater,
tok::greater};
static const tok::TokenKind JavaRightLogicalShiftAssign[] = {tok::greater,
tok::greater,
tok::greaterequal};
static const tok::TokenKind JavaRightLogicalShift[] = {
tok::greater, tok::greater, tok::greater};
static const tok::TokenKind JavaRightLogicalShiftAssign[] = {
tok::greater, tok::greater, tok::greaterequal};
if (tryMergeTokens(JavaRightLogicalShift, TT_BinaryOperator))
return;
if (tryMergeTokens(JavaRightLogicalShiftAssign, TT_BinaryOperator))
Expand Down Expand Up @@ -162,9 +160,8 @@ bool FormatTokenLexer::tryMergeTokens(ArrayRef<tok::TokenKind> Kinds,
return false;
unsigned AddLength = 0;
for (unsigned i = 1; i < Kinds.size(); ++i) {
if (!First[i]->is(Kinds[i]) ||
First[i]->WhitespaceRange.getBegin() !=
First[i]->WhitespaceRange.getEnd())
if (!First[i]->is(Kinds[i]) || First[i]->WhitespaceRange.getBegin() !=
First[i]->WhitespaceRange.getEnd())
return false;
AddLength += First[i]->TokenText.size();
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/SortJavaScriptImports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class JavaScriptImportSorter : public TokenAnalyzer {
// Parses module references in the given lines. Returns the module references,
// and a pointer to the first "main code" line if that is adjacent to the
// affected lines of module references, nullptr otherwise.
std::pair<SmallVector<JsModuleReference, 16>, AnnotatedLine*>
std::pair<SmallVector<JsModuleReference, 16>, AnnotatedLine *>
parseModuleReferences(const AdditionalKeywords &Keywords,
SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
SmallVector<JsModuleReference, 16> References;
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Format/TokenAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ Environment::CreateVirtualEnvironment(StringRef Code, StringRef FileName,
std::unique_ptr<SourceManager> VirtualSM(
new SourceManager(*Diagnostics, *FileMgr));
InMemoryFileSystem->addFile(
FileName, 0, llvm::MemoryBuffer::getMemBuffer(
Code, FileName, /*RequiresNullTerminator=*/false));
FileName, 0,
llvm::MemoryBuffer::getMemBuffer(Code, FileName,
/*RequiresNullTerminator=*/false));
FileID ID = VirtualSM->createFileID(FileMgr->getFile(FileName),
SourceLocation(), clang::SrcMgr::C_User);
assert(ID.isValid());
Expand Down
103 changes: 50 additions & 53 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class AnnotatingParser {
if (NonTemplateLess.count(CurrentToken->Previous))
return false;

const FormatToken& Previous = *CurrentToken->Previous;
const FormatToken &Previous = *CurrentToken->Previous;
if (Previous.Previous) {
if (Previous.Previous->Tok.isLiteral())
return false;
Expand Down Expand Up @@ -152,11 +152,11 @@ class AnnotatingParser {
// export type X = (...);
Contexts.back().IsExpression = false;
} else if (Left->Previous &&
(Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_decltype,
tok::kw_if, tok::kw_while, tok::l_paren,
tok::comma) ||
Left->Previous->endsSequence(tok::kw_constexpr, tok::kw_if) ||
Left->Previous->is(TT_BinaryOperator))) {
(Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_decltype,
tok::kw_if, tok::kw_while, tok::l_paren,
tok::comma) ||
Left->Previous->endsSequence(tok::kw_constexpr, tok::kw_if) ||
Left->Previous->is(TT_BinaryOperator))) {
// static_assert, if and while usually contain expressions.
Contexts.back().IsExpression = true;
} else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous &&
Expand Down Expand Up @@ -325,8 +325,7 @@ class AnnotatingParser {
// In C++, this can happen either in array of templates (foo<int>[10])
// or when array is a nested template type (unique_ptr<type1<type2>[]>).
bool CppArrayTemplates =
Style.isCpp() && Parent &&
Parent->is(TT_TemplateCloser) &&
Style.isCpp() && Parent && Parent->is(TT_TemplateCloser) &&
(Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
Contexts.back().InTemplateArgument);

Expand Down Expand Up @@ -607,7 +606,8 @@ class AnnotatingParser {
break;
case tok::kw_if:
case tok::kw_while:
if (Tok->is(tok::kw_if) && CurrentToken && CurrentToken->is(tok::kw_constexpr))
if (Tok->is(tok::kw_if) && CurrentToken &&
CurrentToken->is(tok::kw_constexpr))
next();
if (CurrentToken && CurrentToken->is(tok::l_paren)) {
next();
Expand All @@ -633,8 +633,7 @@ class AnnotatingParser {
// marks the first l_paren as a OverloadedOperatorLParen. Here, we make
// the first two parens OverloadedOperators and the second l_paren an
// OverloadedOperatorLParen.
if (Tok->Previous &&
Tok->Previous->is(tok::r_paren) &&
if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
Tok->Previous->MatchingParen &&
Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
Tok->Previous->Type = TT_OverloadedOperator;
Expand All @@ -657,7 +656,7 @@ class AnnotatingParser {
break;
case tok::l_brace:
if (Style.Language == FormatStyle::LK_TextProto) {
FormatToken *Previous =Tok->getPreviousNonComment();
FormatToken *Previous = Tok->getPreviousNonComment();
if (Previous && Previous->Type != TT_DictLiteral)
Previous->Type = TT_SelectorName;
}
Expand Down Expand Up @@ -754,8 +753,8 @@ class AnnotatingParser {

void parseIncludeDirective() {
if (CurrentToken && CurrentToken->is(tok::less)) {
next();
while (CurrentToken) {
next();
while (CurrentToken) {
// Mark tokens up to the trailing line comments as implicit string
// literals.
if (CurrentToken->isNot(tok::comment) &&
Expand Down Expand Up @@ -795,9 +794,9 @@ class AnnotatingParser {
void parseHasInclude() {
if (!CurrentToken || !CurrentToken->is(tok::l_paren))
return;
next(); // '('
next(); // '('
parseIncludeDirective();
next(); // ')'
next(); // ')'
}

LineType parsePreprocessorDirective() {
Expand Down Expand Up @@ -856,7 +855,7 @@ class AnnotatingParser {
if (Tok->is(tok::l_paren))
parseParens();
else if (Tok->isOneOf(Keywords.kw___has_include,
Keywords.kw___has_include_next))
Keywords.kw___has_include_next))
parseHasInclude();
}
return Type;
Expand Down Expand Up @@ -947,10 +946,11 @@ class AnnotatingParser {
// FIXME: Closure-library specific stuff should not be hard-coded but be
// configurable.
return Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) &&
Tok.Next->Next && (Tok.Next->Next->TokenText == "module" ||
Tok.Next->Next->TokenText == "provide" ||
Tok.Next->Next->TokenText == "require" ||
Tok.Next->Next->TokenText == "forwardDeclare") &&
Tok.Next->Next &&
(Tok.Next->Next->TokenText == "module" ||
Tok.Next->Next->TokenText == "provide" ||
Tok.Next->Next->TokenText == "require" ||
Tok.Next->Next->TokenText == "forwardDeclare") &&
Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
}

Expand Down Expand Up @@ -1067,8 +1067,7 @@ class AnnotatingParser {
Current.Previous->is(TT_CtorInitializerColon)) {
Contexts.back().IsExpression = true;
Contexts.back().InCtorInitializer = true;
} else if (Current.Previous &&
Current.Previous->is(TT_InheritanceColon)) {
} else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
Contexts.back().InInheritanceList = true;
} else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
for (FormatToken *Previous = Current.Previous;
Expand Down Expand Up @@ -1131,10 +1130,10 @@ class AnnotatingParser {
Current.NestingLevel == 0) {
Current.Type = TT_TrailingReturnArrow;
} else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
Current.Type =
determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
Contexts.back().IsExpression,
Contexts.back().InTemplateArgument);
Current.Type = determineStarAmpUsage(Current,
Contexts.back().CanBeExpression &&
Contexts.back().IsExpression,
Contexts.back().InTemplateArgument);
} else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
Current.Type = determinePlusMinusCaretUsage(Current);
if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
Expand Down Expand Up @@ -1742,7 +1741,7 @@ void TokenAnnotator::setCommentLineLevels(

static unsigned maxNestingDepth(const AnnotatedLine &Line) {
unsigned Result = 0;
for (const auto* Tok = Line.First; Tok != nullptr; Tok = Tok->Next)
for (const auto *Tok = Line.First; Tok != nullptr; Tok = Tok->Next)
Result = std::max(Result, Tok->NestingLevel);
return Result;
}
Expand Down Expand Up @@ -1784,16 +1783,16 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
// function declaration.
static bool isFunctionDeclarationName(const FormatToken &Current,
const AnnotatedLine &Line) {
auto skipOperatorName = [](const FormatToken* Next) -> const FormatToken* {
auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
for (; Next; Next = Next->Next) {
if (Next->is(TT_OverloadedOperatorLParen))
return Next;
if (Next->is(TT_OverloadedOperator))
continue;
if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
// For 'new[]' and 'delete[]'.
if (Next->Next && Next->Next->is(tok::l_square) &&
Next->Next->Next && Next->Next->Next->is(tok::r_square))
if (Next->Next && Next->Next->is(tok::l_square) && Next->Next->Next &&
Next->Next->Next->is(tok::r_square))
Next = Next->Next->Next;
continue;
}
Expand Down Expand Up @@ -2072,7 +2071,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
if (Left.is(tok::comment))
return 1000;

if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon, TT_CtorInitializerColon))
if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
TT_CtorInitializerColon))
return 2;

if (Right.isMemberAccess()) {
Expand Down Expand Up @@ -2130,8 +2130,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign)
return 100;
if (Left.is(tok::l_paren) && Left.Previous &&
(Left.Previous->isOneOf(tok::kw_if, tok::kw_for)
|| Left.Previous->endsSequence(tok::kw_constexpr, tok::kw_if)))
(Left.Previous->isOneOf(tok::kw_if, tok::kw_for) ||
Left.Previous->endsSequence(tok::kw_constexpr, tok::kw_if)))
return 1000;
if (Left.is(tok::equal) && InFunctionDecl)
return 110;
Expand Down Expand Up @@ -2201,8 +2201,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
: Style.SpacesInParentheses;
if (Right.isOneOf(tok::semi, tok::comma))
return false;
if (Right.is(tok::less) &&
Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)
if (Right.is(tok::less) && Line.Type == LT_ObjCDecl &&
Style.ObjCSpaceBeforeProtocolList)
return true;
if (Right.is(tok::less) && Left.is(tok::kw_template))
return Style.SpaceAfterTemplateKeyword;
Expand Down Expand Up @@ -2351,8 +2351,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
if (Left.is(TT_JsFatArrow))
return true;
// for await ( ...
if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) &&
Left.Previous && Left.Previous->is(tok::kw_for))
if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous &&
Left.Previous->is(tok::kw_for))
return true;
if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
Right.MatchingParen) {
Expand Down Expand Up @@ -2509,7 +2509,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
return (Left.is(TT_TemplateOpener) &&
Style.Standard == FormatStyle::LS_Cpp03) ||
!(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
tok::kw___super, TT_TemplateCloser, TT_TemplateOpener));
tok::kw___super, TT_TemplateCloser,
TT_TemplateOpener));
if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
return Style.SpacesInAngles;
if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) ||
Expand Down Expand Up @@ -2634,19 +2635,16 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
!Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
return true;
// Break only if we have multiple inheritance.
if (Style.BreakBeforeInheritanceComma &&
Right.is(TT_InheritanceComma))
return true;
if (Style.BreakBeforeInheritanceComma && Right.is(TT_InheritanceComma))
return true;
if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\""))
// Raw string literals are special wrt. line breaks. The author has made a
// deliberate choice and might have aligned the contents of the string
// literal accordingly. Thus, we try keep existing line breaks.
return Right.NewlinesBefore > 0;
if ((Right.Previous->is(tok::l_brace) ||
(Right.Previous->is(tok::less) &&
Right.Previous->Previous &&
Right.Previous->Previous->is(tok::equal))
) &&
(Right.Previous->is(tok::less) && Right.Previous->Previous &&
Right.Previous->Previous->is(tok::equal))) &&
Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
// Don't put enums or option definitions onto single lines in protocol
// buffers.
Expand All @@ -2656,7 +2654,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
return Right.HasUnescapedNewline;
if (isAllmanBrace(Left) || isAllmanBrace(Right))
return (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) ||
(Line.startsWith(tok::kw_typedef, tok::kw_enum) && Style.BraceWrapping.AfterEnum) ||
(Line.startsWith(tok::kw_typedef, tok::kw_enum) &&
Style.BraceWrapping.AfterEnum) ||
(Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) ||
(Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct);
if (Left.is(TT_ObjCBlockLBrace) && !Style.AllowShortBlocksOnASingleLine)
Expand Down Expand Up @@ -2750,8 +2749,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
// list.
return Left.BlockKind == BK_BracedInit ||
(Left.is(TT_CtorInitializerColon) &&
Style.BreakConstructorInitializers ==
FormatStyle::BCIS_AfterColon);
Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
if (Left.is(tok::question) && Right.is(tok::colon))
return false;
if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
Expand Down Expand Up @@ -2876,10 +2874,9 @@ void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
<< " T=" << getTokenTypeName(Tok->Type)
<< " S=" << Tok->SpacesRequiredBefore
<< " B=" << Tok->BlockParameterCount
<< " BK=" << Tok->BlockKind
<< " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
<< " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind
<< " FakeLParens=";
<< " BK=" << Tok->BlockKind << " P=" << Tok->SplitPenalty
<< " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength
<< " PPK=" << Tok->PackingKind << " FakeLParens=";
for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
llvm::errs() << Tok->FakeLParens[i] << "/";
llvm::errs() << " FakeRParens=" << Tok->FakeRParens;
Expand Down
15 changes: 8 additions & 7 deletions clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ class LineJoiner {
return nullptr;
const AnnotatedLine *Current = *Next;
IndentTracker.nextLine(*Current);
unsigned MergedLines =
tryFitMultipleLinesInOne(IndentTracker, Next, End);
unsigned MergedLines = tryFitMultipleLinesInOne(IndentTracker, Next, End);
if (MergedLines > 0 && Style.ColumnLimit == 0)
// Disallow line merging if there is a break at the start of one of the
// input lines.
Expand Down Expand Up @@ -228,7 +227,8 @@ class LineJoiner {

if (Tok && Tok->getNamespaceToken())
return !Style.BraceWrapping.SplitEmptyNamespace && EmptyBlock
? tryMergeSimpleBlock(I, E, Limit) : 0;
? tryMergeSimpleBlock(I, E, Limit)
: 0;

if (Tok && Tok->is(tok::kw_typedef))
Tok = Tok->getNextNonComment();
Expand Down Expand Up @@ -311,8 +311,8 @@ class LineJoiner {
// Try to merge a block with left brace wrapped that wasn't yet covered
if (TheLine->Last->is(tok::l_brace)) {
return !Style.BraceWrapping.AfterFunction ||
(I[1]->First->is(tok::r_brace) &&
!Style.BraceWrapping.SplitEmptyRecord)
(I[1]->First->is(tok::r_brace) &&
!Style.BraceWrapping.SplitEmptyRecord)
? tryMergeSimpleBlock(I, E, Limit)
: 0;
}
Expand Down Expand Up @@ -849,7 +849,8 @@ class OptimizingLineFormatter : public LineFormatter {

/// \brief The BFS queue type.
typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
std::greater<QueueItem>> QueueType;
std::greater<QueueItem>>
QueueType;

/// \brief Analyze the entire solution space starting from \p InitialState.
///
Expand Down Expand Up @@ -1085,7 +1086,7 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
void UnwrappedLineFormatter::formatFirstToken(const AnnotatedLine &Line,
const AnnotatedLine *PreviousLine,
unsigned Indent) {
FormatToken& RootToken = *Line.First;
FormatToken &RootToken = *Line.First;
if (RootToken.is(tok::eof)) {
unsigned Newlines = std::min(RootToken.NewlinesBefore, 1u);
Whitespaces->replaceWhitespace(RootToken, Newlines, /*Spaces=*/0,
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Format/UnwrappedLineFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class UnwrappedLineFormatter {
const SourceManager &SourceMgr,
FormattingAttemptStatus *Status)
: Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
Keywords(Keywords), SourceMgr(SourceMgr),
Status(Status) {}
Keywords(Keywords), SourceMgr(SourceMgr), Status(Status) {}

/// \brief Format the current block and return the penalty.
unsigned format(const SmallVectorImpl<AnnotatedLine *> &Lines,
Expand Down
34 changes: 16 additions & 18 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ class ScopedDeclarationState {
};

static bool isLineComment(const FormatToken &FormatTok) {
return FormatTok.is(tok::comment) &&
FormatTok.TokenText.startswith("//");
return FormatTok.is(tok::comment) && FormatTok.TokenText.startswith("//");
}

// Checks if \p FormatTok is a line comment that continues the line comment
Expand Down Expand Up @@ -331,7 +330,8 @@ void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
break;
case tok::kw_default:
case tok::kw_case:
if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) {
if (Style.Language == FormatStyle::LK_JavaScript &&
Line->MustBeDeclaration) {
// A 'case: string' style field declaration.
parseStructuralElement();
break;
Expand All @@ -356,7 +356,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
// definitions, too.
unsigned StoredPosition = Tokens->getPosition();
FormatToken *Tok = FormatTok;
const FormatToken *PrevTok = Tok->Previous;
const FormatToken *PrevTok = Tok->Previous;
// Keep a stack of positions of lbrace tokens. We will
// update information about whether an lbrace starts a
// braced init list or a different block during the loop.
Expand Down Expand Up @@ -591,9 +591,8 @@ void UnwrappedLineParser::parseChildBlock() {
FormatTok->BlockKind = BK_Block;
nextToken();
{
bool SkipIndent =
(Style.Language == FormatStyle::LK_JavaScript &&
(isGoogScope(*Line) || isIIFE(*Line, Keywords)));
bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript &&
(isGoogScope(*Line) || isIIFE(*Line, Keywords)));
ScopedLineState LineState(*this);
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
/*MustBeDeclaration=*/false);
Expand Down Expand Up @@ -835,8 +834,8 @@ static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
Keywords.kw_let, Keywords.kw_var, tok::kw_const,
Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
Keywords.kw_instanceof, Keywords.kw_interface,
Keywords.kw_throws, Keywords.kw_from));
Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws,
Keywords.kw_from));
}

static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
Expand Down Expand Up @@ -1290,7 +1289,7 @@ void UnwrappedLineParser::parseStructuralElement() {
nextToken();
parseBracedList();
} else if (Style.Language == FormatStyle::LK_Proto &&
FormatTok->Tok.is(tok::less)) {
FormatTok->Tok.is(tok::less)) {
nextToken();
parseBracedList(/*ContinueOnSemicolons=*/false,
/*ClosingBraceKind=*/tok::greater);
Expand Down Expand Up @@ -1356,12 +1355,12 @@ bool UnwrappedLineParser::tryToParseLambda() {
}

bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
const FormatToken* Previous = FormatTok->Previous;
const FormatToken *Previous = FormatTok->Previous;
if (Previous &&
(Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
tok::kw_delete) ||
FormatTok->isCppStructuredBinding(Style) ||
Previous->closesScope() || Previous->isSimpleTypeSpecifier())) {
FormatTok->isCppStructuredBinding(Style) || Previous->closesScope() ||
Previous->isSimpleTypeSpecifier())) {
nextToken();
return false;
}
Expand Down Expand Up @@ -2200,8 +2199,8 @@ LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
E = Line.Tokens.end();
I != E; ++I) {
llvm::dbgs() << I->Tok->Tok.getName() << "["
<< "T=" << I->Tok->Type
<< ", OC=" << I->Tok->OriginalColumn << "] ";
<< "T=" << I->Tok->Type << ", OC=" << I->Tok->OriginalColumn
<< "] ";
}
for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
E = Line.Tokens.end();
Expand Down Expand Up @@ -2381,7 +2380,7 @@ void UnwrappedLineParser::nextToken(int LevelDifference) {
return;
flushComments(isOnNewLine(*FormatTok));
pushToken(FormatTok);
FormatToken* Previous = FormatTok;
FormatToken *Previous = FormatTok;
if (Style.Language != FormatStyle::LK_JavaScript)
readToken(LevelDifference);
else
Expand Down Expand Up @@ -2426,8 +2425,7 @@ void UnwrappedLineParser::distributeComments(
}
for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
FormatToken *FormatTok = Comments[i];
if (HasTrailAlignedWithNextToken &&
i == StartOfTrailAlignedWithNextToken) {
if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
FormatTok->ContinuesLineCommentSection = false;
} else {
FormatTok->ContinuesLineCommentSection =
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Format/UnwrappedLineParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,9 @@ struct UnwrappedLineNode {
SmallVector<UnwrappedLine, 0> Children;
};

inline UnwrappedLine::UnwrappedLine() : Level(0), InPPDirective(false),
MustBeDeclaration(false), MatchingOpeningBlockLineIndex(kInvalidIndex) {}
inline UnwrappedLine::UnwrappedLine()
: Level(0), InPPDirective(false), MustBeDeclaration(false),
MatchingOpeningBlockLineIndex(kInvalidIndex) {}

} // end namespace format
} // end namespace clang
Expand Down
16 changes: 7 additions & 9 deletions clang/lib/Format/WhitespaceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ void WhitespaceManager::calculateLineBreakInformation() {
// BreakableLineCommentSection does comment reflow changes and here is
// the aligning of trailing comments. Consider the case where we reflow
// the second line up in this example:
//
//
// // line 1
// // line 2
//
//
// That amounts to 2 changes by BreakableLineCommentSection:
// - the first, delimited by (), for the whitespace between the tokens,
// - and second, delimited by [], for the whitespace at the beginning
// of the second token:
//
//
// // line 1(
// )[// ]line 2
//
Expand Down Expand Up @@ -622,8 +622,7 @@ void WhitespaceManager::generateChanges() {
}
}

void WhitespaceManager::storeReplacement(SourceRange Range,
StringRef Text) {
void WhitespaceManager::storeReplacement(SourceRange Range, StringRef Text) {
unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
SourceMgr.getFileOffset(Range.getBegin());
// Don't create a replacement, if it does not change anything.
Expand All @@ -646,10 +645,9 @@ void WhitespaceManager::appendNewlineText(std::string &Text,
Text.append(UseCRLF ? "\r\n" : "\n");
}

void WhitespaceManager::appendEscapedNewlineText(std::string &Text,
unsigned Newlines,
unsigned PreviousEndOfTokenColumn,
unsigned EscapedNewlineColumn) {
void WhitespaceManager::appendEscapedNewlineText(
std::string &Text, unsigned Newlines, unsigned PreviousEndOfTokenColumn,
unsigned EscapedNewlineColumn) {
if (Newlines > 0) {
unsigned Spaces =
std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
Expand Down