summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format
diff options
context:
space:
mode:
Diffstat (limited to 'clang/lib/Format')
-rw-r--r--clang/lib/Format/Format.cpp5
-rw-r--r--clang/lib/Format/FormatToken.h19
-rw-r--r--clang/lib/Format/UnwrappedLineParser.cpp33
-rw-r--r--clang/lib/Format/UnwrappedLineParser.h12
4 files changed, 36 insertions, 33 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 23f6f5b3e89..34ed8ea07c4 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -514,7 +514,10 @@ private:
if (Newline) {
State.Stack.back().ContainsLineBreak = true;
if (Current.is(tok::r_brace)) {
- State.Column = Line.Level * Style.IndentWidth;
+ if (Current.BlockKind == BK_BracedInit)
+ State.Column = State.Stack[State.Stack.size() - 2].LastSpace;
+ else
+ State.Column = Line.Level * Style.IndentWidth;
} else if (Current.is(tok::string_literal) &&
State.StartOfStringLiteral != 0) {
State.Column = State.StartOfStringLiteral;
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 69afaac965d..e77ac016be0 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -56,16 +56,24 @@ enum TokenType {
TT_Unknown
};
+// Represents what type of block a set of braces open.
+enum BraceBlockKind {
+ BK_Unknown,
+ BK_Block,
+ BK_BracedInit
+};
+
/// \brief A wrapper around a \c Token storing information about the
/// whitespace characters preceeding it.
struct FormatToken {
FormatToken()
: NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0),
CodePointCount(0), IsFirst(false), MustBreakBefore(false),
- Type(TT_Unknown), SpacesRequiredBefore(0), CanBreakBefore(false),
- ClosesTemplateDeclaration(false), ParameterCount(0), TotalLength(0),
- UnbreakableTailLength(0), BindingStrength(0), SplitPenalty(0),
- LongestObjCSelectorName(0), FakeRParens(0), LastInChainOfCalls(false),
+ BlockKind(BK_Unknown), Type(TT_Unknown), SpacesRequiredBefore(0),
+ CanBreakBefore(false), ClosesTemplateDeclaration(false),
+ ParameterCount(0), TotalLength(0), UnbreakableTailLength(0),
+ BindingStrength(0), SplitPenalty(0), LongestObjCSelectorName(0),
+ FakeRParens(0), LastInChainOfCalls(false),
PartOfMultiVariableDeclStmt(false), MatchingParen(NULL), Previous(NULL),
Next(NULL) {}
@@ -117,6 +125,9 @@ struct FormatToken {
/// escaped newlines.
StringRef TokenText;
+ /// \brief Contains the kind of block if this token is a brace.
+ BraceBlockKind BlockKind;
+
TokenType Type;
unsigned SpacesRequiredBefore;
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 98a5a8ae075..77f98bffda2 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -183,9 +183,7 @@ UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
UnwrappedLineConsumer &Callback)
: Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(NULL),
- Callback(Callback), AllTokens(Tokens) {
- LBraces.resize(Tokens.size(), BS_Unknown);
-}
+ Callback(Callback), AllTokens(Tokens) {}
bool UnwrappedLineParser::parse() {
DEBUG(llvm::dbgs() << "----\n");
@@ -252,7 +250,7 @@ void UnwrappedLineParser::calculateBraceTypes() {
// 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.
- SmallVector<unsigned, 8> LBraceStack;
+ SmallVector<FormatToken *, 8> LBraceStack;
assert(Tok->Tok.is(tok::l_brace));
do {
// Get next none-comment token.
@@ -265,11 +263,11 @@ void UnwrappedLineParser::calculateBraceTypes() {
switch (Tok->Tok.getKind()) {
case tok::l_brace:
- LBraceStack.push_back(Position);
+ LBraceStack.push_back(Tok);
break;
case tok::r_brace:
if (!LBraceStack.empty()) {
- if (LBraces[LBraceStack.back()] == BS_Unknown) {
+ if (LBraceStack.back()->BlockKind == BK_Unknown) {
// If there is a comma, semicolon or right paren after the closing
// brace, we assume this is a braced initializer list.
@@ -279,10 +277,13 @@ void UnwrappedLineParser::calculateBraceTypes() {
// brace blocks inside it braced init list. That works good enough
// for now, but we will need to fix it to correctly handle lambdas.
if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren,
- tok::l_brace, tok::colon))
- LBraces[LBraceStack.back()] = BS_BracedInit;
- else
- LBraces[LBraceStack.back()] = BS_Block;
+ tok::l_brace, tok::colon)) {
+ Tok->BlockKind = BK_BracedInit;
+ LBraceStack.back()->BlockKind = BK_BracedInit;
+ } else {
+ Tok->BlockKind = BK_Block;
+ LBraceStack.back()->BlockKind = BK_Block;
+ }
}
LBraceStack.pop_back();
}
@@ -294,7 +295,7 @@ void UnwrappedLineParser::calculateBraceTypes() {
case tok::kw_switch:
case tok::kw_try:
if (!LBraceStack.empty())
- LBraces[LBraceStack.back()] = BS_Block;
+ LBraceStack.back()->BlockKind = BK_Block;
break;
default:
break;
@@ -304,8 +305,8 @@ void UnwrappedLineParser::calculateBraceTypes() {
} while (Tok->Tok.isNot(tok::eof));
// Assume other blocks for all unclosed opening braces.
for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
- if (LBraces[LBraceStack[i]] == BS_Unknown)
- LBraces[LBraceStack[i]] = BS_Block;
+ if (LBraceStack[i]->BlockKind == BK_Unknown)
+ LBraceStack[i]->BlockKind = BK_Block;
}
FormatTok = Tokens->setPosition(StoredPosition);
}
@@ -632,10 +633,10 @@ void UnwrappedLineParser::parseStructuralElement() {
}
bool UnwrappedLineParser::tryToParseBracedList() {
- if (LBraces[Tokens->getPosition()] == BS_Unknown)
+ if (FormatTok->BlockKind == BK_Unknown)
calculateBraceTypes();
- assert(LBraces[Tokens->getPosition()] != BS_Unknown);
- if (LBraces[Tokens->getPosition()] == BS_Block)
+ assert(FormatTok->BlockKind != BK_Unknown);
+ if (FormatTok->BlockKind == BK_Block)
return false;
parseBracedList();
return true;
diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h
index fb65078c330..0624c4fd15f 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -102,13 +102,6 @@ private:
void calculateBraceTypes();
void pushPPConditional();
- // Represents what type of block a left brace opens.
- enum LBraceState {
- BS_Unknown,
- BS_Block,
- BS_BracedInit
- };
-
// FIXME: We are constantly running into bugs where Line.Level is incorrectly
// subtracted from beyond 0. Introduce a method to subtract from Line.Level
// and use that everywhere in the Parser.
@@ -153,11 +146,6 @@ private:
// owned outside of and handed into the UnwrappedLineParser.
ArrayRef<FormatToken *> AllTokens;
- // FIXME: Currently we cannot store attributes with tokens, as we treat
- // them as read-only; thus, we now store the brace state indexed by the
- // position of the token in the stream (see \c AllTokens).
- SmallVector<LBraceState, 16> LBraces;
-
// Represents preprocessor branch type, so we can find matching
// #if/#else/#endif directives.
enum PPBranchKind {
OpenPOWER on IntegriCloud