diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Format/Format.cpp | 4 | ||||
-rw-r--r-- | lib/Format/UnwrappedLineParser.cpp | 37 | ||||
-rw-r--r-- | lib/Format/UnwrappedLineParser.h | 8 |
3 files changed, 34 insertions, 15 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 51db69c5ae..ebb49498da 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -57,6 +57,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.PointerAndReferenceBindToType = false; LLVMStyle.AccessModifierOffset = -2; LLVMStyle.SplitTemplateClosingGreater = true; + LLVMStyle.IndentCaseLabels = false; return LLVMStyle; } @@ -67,6 +68,7 @@ FormatStyle getGoogleStyle() { GoogleStyle.PointerAndReferenceBindToType = true; GoogleStyle.AccessModifierOffset = -1; GoogleStyle.SplitTemplateClosingGreater = false; + GoogleStyle.IndentCaseLabels = true; return GoogleStyle; } @@ -760,7 +762,7 @@ public: } tooling::Replacements format() { - UnwrappedLineParser Parser(Lex, SourceMgr, *this); + UnwrappedLineParser Parser(Style, Lex, SourceMgr, *this); StructuralError = Parser.parse(); for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(), E = UnwrappedLines.end(); diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 425e15b81f..ebebece7eb 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -22,9 +22,11 @@ namespace clang { namespace format { -UnwrappedLineParser::UnwrappedLineParser(Lexer &Lex, SourceManager &SourceMgr, +UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, Lexer &Lex, + SourceManager &SourceMgr, UnwrappedLineConsumer &Callback) : GreaterStashed(false), + Style(Style), Lex(Lex), SourceMgr(SourceMgr), IdentTable(Lex.getLangOpts()), @@ -62,20 +64,16 @@ bool UnwrappedLineParser::parseLevel() { return Error; } -bool UnwrappedLineParser::parseBlock() { +bool UnwrappedLineParser::parseBlock(unsigned AddLevels) { assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected"); nextToken(); - // FIXME: Remove this hack to handle namespaces. - bool IsNamespace = Line.Tokens[0].Tok.is(tok::kw_namespace); - addUnwrappedLine(); - if (!IsNamespace) - ++Line.Level; + Line.Level += AddLevels; parseLevel(); - if (!IsNamespace) - --Line.Level; + Line.Level -= AddLevels; + // FIXME: Add error handling. if (!FormatTok.Tok.is(tok::r_brace)) return true; @@ -114,6 +112,9 @@ void UnwrappedLineParser::parseStatement() { } switch (FormatTok.Tok.getKind()) { + case tok::kw_namespace: + parseNamespace(); + return; case tok::kw_public: case tok::kw_protected: case tok::kw_private: @@ -224,6 +225,18 @@ void UnwrappedLineParser::parseIfThenElse() { } } +void UnwrappedLineParser::parseNamespace() { + assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected"); + nextToken(); + if (FormatTok.Tok.is(tok::identifier)) + nextToken(); + if (FormatTok.Tok.is(tok::l_brace)) { + parseBlock(0); + addUnwrappedLine(); + } + // FIXME: Add error handling. +} + void UnwrappedLineParser::parseForOrWhileLoop() { assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) && "'for' or 'while' expected"); @@ -290,13 +303,13 @@ void UnwrappedLineParser::parseSwitch() { nextToken(); parseParens(); if (FormatTok.Tok.is(tok::l_brace)) { - parseBlock(); + parseBlock(Style.IndentCaseLabels ? 2 : 1); addUnwrappedLine(); } else { addUnwrappedLine(); - ++Line.Level; + Line.Level += (Style.IndentCaseLabels ? 2 : 1); parseStatement(); - --Line.Level; + Line.Level -= (Style.IndentCaseLabels ? 2 : 1); } } diff --git a/lib/Format/UnwrappedLineParser.h b/lib/Format/UnwrappedLineParser.h index 3fbc73fd75..03dda9957d 100644 --- a/lib/Format/UnwrappedLineParser.h +++ b/lib/Format/UnwrappedLineParser.h @@ -21,6 +21,7 @@ #include "clang/Basic/IdentifierTable.h" #include "clang/Basic/SourceManager.h" +#include "clang/Format/Format.h" #include "clang/Lex/Lexer.h" namespace clang { @@ -78,7 +79,8 @@ public: class UnwrappedLineParser { public: - UnwrappedLineParser(Lexer &Lex, SourceManager &SourceMgr, + UnwrappedLineParser(const FormatStyle &Style, Lexer &Lex, + SourceManager &SourceMgr, UnwrappedLineConsumer &Callback); /// Returns true in case of a structural error. @@ -86,7 +88,7 @@ public: private: bool parseLevel(); - bool parseBlock(); + bool parseBlock(unsigned AddLevels = 1); void parsePPDirective(); void parseComment(); void parseStatement(); @@ -97,6 +99,7 @@ private: void parseLabel(); void parseCaseLabel(); void parseSwitch(); + void parseNamespace(); void parseAccessSpecifier(); void parseEnum(); void addUnwrappedLine(); @@ -111,6 +114,7 @@ private: FormatToken FormatTok; bool GreaterStashed; + const FormatStyle &Style; Lexer &Lex; SourceManager &SourceMgr; IdentifierTable IdentTable; |