clang-format: clang-format (NFC)

llvm-svn: 239903
This commit is contained in:
Daniel Jasper 2015-06-17 13:08:06 +00:00
parent 3b6304fa1a
commit e6fcf7d372
12 changed files with 66 additions and 80 deletions

View File

@ -183,7 +183,7 @@ void BreakableStringLiteral::insertBreak(unsigned LineIndex,
} }
static StringRef getLineCommentIndentPrefix(StringRef Comment) { static StringRef getLineCommentIndentPrefix(StringRef Comment) {
static const char *const KnownPrefixes[] = { "///", "//", "//!" }; static const char *const KnownPrefixes[] = {"///", "//", "//!"};
StringRef LongestPrefix; StringRef LongestPrefix;
for (StringRef KnownPrefix : KnownPrefixes) { for (StringRef KnownPrefix : KnownPrefixes) {
if (Comment.startswith(KnownPrefix)) { if (Comment.startswith(KnownPrefix)) {
@ -239,9 +239,8 @@ void BreakableLineComment::replaceWhitespace(unsigned LineIndex,
/*Spaces=*/1); /*Spaces=*/1);
} }
void void BreakableLineComment::replaceWhitespaceBefore(
BreakableLineComment::replaceWhitespaceBefore(unsigned LineIndex, unsigned LineIndex, WhitespaceManager &Whitespaces) {
WhitespaceManager &Whitespaces) {
if (OriginalPrefix != Prefix) { if (OriginalPrefix != Prefix) {
Whitespaces.replaceWhitespaceInToken(Tok, OriginalPrefix.size(), 0, "", "", Whitespaces.replaceWhitespaceInToken(Tok, OriginalPrefix.size(), 0, "", "",
/*InPPDirective=*/false, /*InPPDirective=*/false,
@ -415,9 +414,8 @@ void BreakableBlockComment::replaceWhitespace(unsigned LineIndex,
/*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1); /*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1);
} }
void void BreakableBlockComment::replaceWhitespaceBefore(
BreakableBlockComment::replaceWhitespaceBefore(unsigned LineIndex, unsigned LineIndex, WhitespaceManager &Whitespaces) {
WhitespaceManager &Whitespaces) {
if (LineIndex == 0) if (LineIndex == 0)
return; return;
StringRef Prefix = Decoration; StringRef Prefix = Decoration;

View File

@ -714,7 +714,7 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
if (Current.is(TT_ObjCStringLiteral) && State.StartOfStringLiteral == 0) if (Current.is(TT_ObjCStringLiteral) && State.StartOfStringLiteral == 0)
State.StartOfStringLiteral = State.Column + 1; State.StartOfStringLiteral = State.Column + 1;
else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) && else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) &&
!Current.isStringLiteral()) !Current.isStringLiteral())
State.StartOfStringLiteral = 0; State.StartOfStringLiteral = 0;
State.Column += Current.ColumnWidth; State.Column += Current.ColumnWidth;
@ -892,7 +892,7 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
// be a line break within this call. // be a line break within this call.
for (const FormatToken *Tok = &Current; for (const FormatToken *Tok = &Current;
Tok && Tok != Current.MatchingParen; Tok = Tok->Next) { Tok && Tok != Current.MatchingParen; Tok = Tok->Next) {
if (Tok->MustBreakBefore || if (Tok->MustBreakBefore ||
(Tok->CanBreakBefore && Tok->NewlinesBefore > 0)) { (Tok->CanBreakBefore && Tok->NewlinesBefore > 0)) {
BreakBeforeParameter = true; BreakBeforeParameter = true;
break; break;

View File

@ -174,7 +174,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft); IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
IO.mapOptional("AlignOperands", Style.AlignOperands); IO.mapOptional("AlignOperands", Style.AlignOperands);
IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments); IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
IO.mapOptional("AlignConsecutiveAssignments", Style.AlignConsecutiveAssignments); IO.mapOptional("AlignConsecutiveAssignments",
Style.AlignConsecutiveAssignments);
IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
Style.AllowAllParametersOfDeclarationOnNextLine); Style.AllowAllParametersOfDeclarationOnNextLine);
IO.mapOptional("AllowShortBlocksOnASingleLine", IO.mapOptional("AllowShortBlocksOnASingleLine",
@ -1258,7 +1259,8 @@ public:
} }
tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
FormatTokenLexer &Tokens, bool *IncompleteFormat) { FormatTokenLexer &Tokens,
bool *IncompleteFormat) {
TokenAnnotator Annotator(Style, Tokens.getKeywords()); TokenAnnotator Annotator(Style, Tokens.getKeywords());
for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Annotator.annotate(*AnnotatedLines[i]); Annotator.annotate(*AnnotatedLines[i]);
@ -1504,8 +1506,7 @@ tooling::Replacements reformat(const FormatStyle &Style,
tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
ArrayRef<tooling::Range> Ranges, ArrayRef<tooling::Range> Ranges,
StringRef FileName, StringRef FileName, bool *IncompleteFormat) {
bool *IncompleteFormat) {
if (Style.DisableFormat) if (Style.DisableFormat)
return tooling::Replacements(); return tooling::Replacements();

View File

@ -86,24 +86,12 @@ enum TokenType {
}; };
// Represents what type of block a set of braces open. // Represents what type of block a set of braces open.
enum BraceBlockKind { enum BraceBlockKind { BK_Unknown, BK_Block, BK_BracedInit };
BK_Unknown,
BK_Block,
BK_BracedInit
};
// The packing kind of a function's parameters. // The packing kind of a function's parameters.
enum ParameterPackingKind { enum ParameterPackingKind { PPK_BinPacked, PPK_OnePerLine, PPK_Inconclusive };
PPK_BinPacked,
PPK_OnePerLine,
PPK_Inconclusive
};
enum FormatDecision { enum FormatDecision { FD_Unformatted, FD_Continue, FD_Break };
FD_Unformatted,
FD_Continue,
FD_Break
};
class TokenRole; class TokenRole;
class AnnotatedLine; class AnnotatedLine;

View File

@ -1844,7 +1844,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
tok::kw_new, tok::kw_delete) && tok::kw_new, tok::kw_delete) &&
(!Left.Previous || Left.Previous->isNot(tok::period))))) || (!Left.Previous || Left.Previous->isNot(tok::period))))) ||
(Style.SpaceBeforeParens == FormatStyle::SBPO_Always && (Style.SpaceBeforeParens == FormatStyle::SBPO_Always &&
(Left.is(tok::identifier) || Left.isFunctionLikeKeyword() || Left.is(tok::r_paren)) && (Left.is(tok::identifier) || Left.isFunctionLikeKeyword() ||
Left.is(tok::r_paren)) &&
Line.Type != LT_PreprocessorDirective); Line.Type != LT_PreprocessorDirective);
} }
if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword) if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
@ -2015,11 +2016,10 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous && if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous &&
Left.Previous->is(tok::char_constant)) Left.Previous->is(tok::char_constant))
return true; return true;
if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
Line.Level == 0 && Left.Previous && Left.Previous && Left.Previous->is(tok::equal) &&
Left.Previous->is(tok::equal) && Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_const) &&
tok::kw_export, tok::kw_const) &&
// kw_var is a pseudo-token that's a tok::identifier, so matches above. // kw_var is a pseudo-token that's a tok::identifier, so matches above.
!Line.startsWith(Keywords.kw_var)) !Line.startsWith(Keywords.kw_var))
// Object literals on the top level of a file are treated as "enum-style". // Object literals on the top level of a file are treated as "enum-style".
@ -2064,7 +2064,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
Left.isNot(TT_CtorInitializerColon) && Left.isNot(TT_CtorInitializerColon) &&
(Right.NewlinesBefore > 0 && Right.HasUnescapedNewline); (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
if (Left.isTrailingComment()) if (Left.isTrailingComment())
return true; return true;
if (Left.isStringLiteral() && if (Left.isStringLiteral() &&
(Right.isStringLiteral() || Right.is(TT_ObjCStringLiteral))) (Right.isStringLiteral() || Right.is(TT_ObjCStringLiteral)))
return true; return true;

View File

@ -445,9 +445,9 @@ private:
const FormatStyle &Style; const FormatStyle &Style;
const AdditionalKeywords &Keywords; const AdditionalKeywords &Keywords;
const SmallVectorImpl<AnnotatedLine*>::const_iterator End; const SmallVectorImpl<AnnotatedLine *>::const_iterator End;
SmallVectorImpl<AnnotatedLine*>::const_iterator Next; SmallVectorImpl<AnnotatedLine *>::const_iterator Next;
}; };
static void markFinalized(FormatToken *Tok) { static void markFinalized(FormatToken *Tok) {

View File

@ -52,7 +52,8 @@ private:
/// \brief Returns the column limit for a line, taking into account whether we /// \brief Returns the column limit for a line, taking into account whether we
/// need an escaped newline due to a continued preprocessor directive. /// need an escaped newline due to a continued preprocessor directive.
unsigned getColumnLimit(bool InPPDirective, const AnnotatedLine *NextLine) const; unsigned getColumnLimit(bool InPPDirective,
const AnnotatedLine *NextLine) const;
// Cache to store the penalty of formatting a vector of AnnotatedLines // Cache to store the penalty of formatting a vector of AnnotatedLines
// starting from a specific additional offset. Improves performance if there // starting from a specific additional offset. Improves performance if there

View File

@ -251,7 +251,6 @@ void UnwrappedLineParser::parse() {
assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back()); assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
} }
} while (!PPLevelBranchIndex.empty()); } while (!PPLevelBranchIndex.empty());
} }
void UnwrappedLineParser::parseFile() { void UnwrappedLineParser::parseFile() {
@ -887,8 +886,7 @@ void UnwrappedLineParser::parseStructuralElement() {
? FormatTok->NewlinesBefore > 0 ? FormatTok->NewlinesBefore > 0
: CommentsBeforeNextToken.front()->NewlinesBefore > 0; : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
if (FollowedByNewline && if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
(Text.size() >= 5 || FunctionLike) &&
tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) { tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
addUnwrappedLine(); addUnwrappedLine();
return; return;
@ -1048,7 +1046,7 @@ void UnwrappedLineParser::tryToParseJSFunction() {
if (FormatTok->is(tok::l_brace)) if (FormatTok->is(tok::l_brace))
tryToParseBracedList(); tryToParseBracedList();
else else
while(FormatTok->isNot(tok::l_brace) && !eof()) while (FormatTok->isNot(tok::l_brace) && !eof())
nextToken(); nextToken();
} }
@ -1081,7 +1079,7 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
nextToken(); nextToken();
// Fat arrows can be followed by simple expressions or by child blocks // Fat arrows can be followed by simple expressions or by child blocks
// in curly braces. // in curly braces.
if (FormatTok->is(tok::l_brace)){ if (FormatTok->is(tok::l_brace)) {
parseChildBlock(); parseChildBlock();
continue; continue;
} }
@ -1591,7 +1589,6 @@ void UnwrappedLineParser::parseRecord() {
const FormatToken &InitialToken = *FormatTok; const FormatToken &InitialToken = *FormatTok;
nextToken(); nextToken();
// The actual identifier can be a nested name specifier, and in macros // The actual identifier can be a nested name specifier, and in macros
// it is often token-pasted. // it is often token-pasted.
while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash, while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,

View File

@ -167,9 +167,11 @@ private:
/// \brief Align consecutive assignments over all \c Changes. /// \brief Align consecutive assignments over all \c Changes.
void alignConsecutiveAssignments(); void alignConsecutiveAssignments();
/// \brief Align consecutive assignments from change \p Start to change \p End at /// \brief Align consecutive assignments from change \p Start to change \p End
/// at
/// the specified \p Column. /// the specified \p Column.
void alignConsecutiveAssignments(unsigned Start, unsigned End, unsigned Column); void alignConsecutiveAssignments(unsigned Start, unsigned End,
unsigned Column);
/// \brief Align trailing comments over all \c Changes. /// \brief Align trailing comments over all \c Changes.
void alignTrailingComments(); void alignTrailingComments();

View File

@ -150,8 +150,8 @@ TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
"\r\n" "\r\n"
"*/\r\n", "*/\r\n",
format("/*\r\n" format("/*\r\n"
"\r\n" "\r\n"
"*/\r\n")); "*/\r\n"));
EXPECT_EQ(0, ReplacementCount); EXPECT_EQ(0, ReplacementCount);
} }
@ -2664,7 +2664,8 @@ TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
"#define b \\\n" "#define b \\\n"
" } \\\n" " } \\\n"
" a\n" " a\n"
"a", getLLVMStyleWithColumns(15)); "a",
getLLVMStyleWithColumns(15));
verifyFormat("#define A \\\n" verifyFormat("#define A \\\n"
" { \\\n" " { \\\n"
" {\n" " {\n"
@ -2892,8 +2893,7 @@ TEST_F(FormatTest, EscapedNewlines) {
EXPECT_EQ( EXPECT_EQ(
"#define A \\\n int i; \\\n int j;", "#define A \\\n int i; \\\n int j;",
format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
EXPECT_EQ( EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
"#define A\n\nint i;", format("#define A \\\n\n int i;"));
EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
@ -6115,20 +6115,19 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
"std::this_thread::sleep_for(\n" "std::this_thread::sleep_for(\n"
" std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
ExtraSpaces); ExtraSpaces);
verifyFormat( verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
"std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n" " aaaaaaa,\n"
" aaaaaaa,\n" " aaaaaaaaaa,\n"
" aaaaaaaaaa,\n" " aaaaa,\n"
" aaaaa,\n" " aaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaa,\n" " aaa,\n"
" aaa,\n" " aaaaaaaaaa,\n"
" aaaaaaaaaa,\n" " a,\n"
" a,\n" " aaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaa,\n"
" aaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" " aaaaaaa,\n"
" aaaaaaa,\n" " a};");
" a};");
verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
} }
@ -8945,13 +8944,15 @@ TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
Styles[2].Language = FormatStyle::LK_JavaScript; Styles[2].Language = FormatStyle::LK_JavaScript;
EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
"BasedOnStyle: Google", "BasedOnStyle: Google",
&Styles[2]).value()); &Styles[2])
.value());
Styles[3] = getLLVMStyle(); Styles[3] = getLLVMStyle();
Styles[3].Language = FormatStyle::LK_JavaScript; Styles[3].Language = FormatStyle::LK_JavaScript;
EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
"Language: JavaScript", "Language: JavaScript",
&Styles[3]).value()); &Styles[3])
.value());
Styles[4] = getLLVMStyle(); Styles[4] = getLLVMStyle();
Styles[4].Language = FormatStyle::LK_JavaScript; Styles[4].Language = FormatStyle::LK_JavaScript;
@ -8961,7 +8962,8 @@ TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
"---\n" "---\n"
"BasedOnStyle: Google\n" "BasedOnStyle: Google\n"
"Language: JavaScript", "Language: JavaScript",
&Styles[4]).value()); &Styles[4])
.value());
EXPECT_ALL_STYLES_EQUAL(Styles); EXPECT_ALL_STYLES_EQUAL(Styles);
} }
@ -9260,7 +9262,8 @@ TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
"Language: JavaScript\n" "Language: JavaScript\n"
"IndentWidth: 76\n" "IndentWidth: 76\n"
"...\n", "...\n",
&Style).value()); &Style)
.value());
EXPECT_FALSE(Style.BreakBeforeTernaryOperators); EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
EXPECT_EQ(76u, Style.IndentWidth); EXPECT_EQ(76u, Style.IndentWidth);
EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
@ -9933,8 +9936,7 @@ TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
ZeroColumn.AllowShortBlocksOnASingleLine = true; ZeroColumn.AllowShortBlocksOnASingleLine = true;
EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
format("void (^largeBlock)(void) = ^{ int i; };", format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
ZeroColumn));
ZeroColumn.AllowShortBlocksOnASingleLine = false; ZeroColumn.AllowShortBlocksOnASingleLine = false;
EXPECT_EQ("void (^largeBlock)(void) = ^{\n" EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
" int i;\n" " int i;\n"

View File

@ -256,9 +256,8 @@ TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
} }
TEST_F(FormatTestJS, ArrayLiterals) { TEST_F(FormatTestJS, ArrayLiterals) {
verifyFormat( verifyFormat("var aaaaa: List<SomeThing> =\n"
"var aaaaa: List<SomeThing> =\n" " [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];");
" [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];");
verifyFormat("return [\n" verifyFormat("return [\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" " bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
@ -856,9 +855,7 @@ TEST_F(FormatTestJS, TemplateStrings) {
"var y;")); "var y;"));
} }
TEST_F(FormatTestJS, CastSyntax) { TEST_F(FormatTestJS, CastSyntax) { verifyFormat("var x = <type>foo;"); }
verifyFormat("var x = <type>foo;");
}
TEST_F(FormatTestJS, TypeArguments) { TEST_F(FormatTestJS, TypeArguments) {
verifyFormat("class X<Y> {}"); verifyFormat("class X<Y> {}");

View File

@ -31,9 +31,9 @@ protected:
return Result; return Result;
} }
static std::string format( static std::string
llvm::StringRef Code, format(llvm::StringRef Code,
const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) { const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
return format(Code, 0, Code.size(), Style); return format(Code, 0, Code.size(), Style);
} }