diff options
author | Daniel Jasper <djasper@google.com> | 2014-05-08 11:58:24 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2014-05-08 11:58:24 +0000 |
commit | 04a71a45ff8e8b8a8493d394dfbc4205724b9353 (patch) | |
tree | 2dde5bab1312d53165023a15165af43165c0d5ae /clang/lib/Format | |
parent | 8dcb116a3eb6d58636ce765e4d89b9a383c7cd5b (diff) | |
download | bcm5719-llvm-04a71a45ff8e8b8a8493d394dfbc4205724b9353.tar.gz bcm5719-llvm-04a71a45ff8e8b8a8493d394dfbc4205724b9353.zip |
clang-format: Initial support for try-catch.
Most of this patch was created by Alexander Rojas in
http://reviews.llvm.org/D2555
Thank you!
Synced and addressed review comments.
llvm-svn: 208302
Diffstat (limited to 'clang/lib/Format')
-rw-r--r-- | clang/lib/Format/UnwrappedLineParser.cpp | 73 | ||||
-rw-r--r-- | clang/lib/Format/UnwrappedLineParser.h | 1 |
2 files changed, 74 insertions, 0 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 940fcc8ae2f..e229ff02f5b 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -675,6 +675,9 @@ void UnwrappedLineParser::parseStructuralElement() { case tok::kw_case: parseCaseLabel(); return; + case tok::kw_try: + parseTryCatch(); + return; case tok::kw_extern: nextToken(); if (FormatTok->Tok.is(tok::string_literal)) { @@ -755,6 +758,10 @@ void UnwrappedLineParser::parseStructuralElement() { // Otherwise this was a braced init list, and the structural // element continues. break; + case tok::kw_try: + // We arrive here when parsing function-try blocks. + parseTryCatch(); + return; case tok::identifier: { StringRef Text = FormatTok->TokenText; nextToken(); @@ -1071,6 +1078,72 @@ void UnwrappedLineParser::parseIfThenElse() { } } +void UnwrappedLineParser::parseTryCatch() { + assert(FormatTok->is(tok::kw_try) && "'try' expected"); + nextToken(); + bool NeedsUnwrappedLine = false; + if (FormatTok->is(tok::colon)) { + // We are in a function try block, what comes is an initializer list. + nextToken(); + while (FormatTok->is(tok::identifier)) { + nextToken(); + if (FormatTok->is(tok::l_paren)) + parseParens(); + else + StructuralError = true; + if (FormatTok->is(tok::comma)) + nextToken(); + } + } + if (FormatTok->is(tok::l_brace)) { + CompoundStatementIndenter Indenter(this, Style, Line->Level); + parseBlock(/*MustBeDeclaration=*/false); + if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || + Style.BreakBeforeBraces == FormatStyle::BS_GNU || + Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) { + addUnwrappedLine(); + } else { + NeedsUnwrappedLine = true; + } + } else if (!FormatTok->is(tok::kw_catch)) { + // The C++ standard requires a compound-statement after a try. + // If there's none, we try to assume there's a structuralElement + // and try to continue. + StructuralError = true; + addUnwrappedLine(); + ++Line->Level; + parseStructuralElement(); + --Line->Level; + } + while (FormatTok->is(tok::kw_catch) || + (Style.Language == FormatStyle::LK_JavaScript && + FormatTok->TokenText == "finally")) { + nextToken(); + while (FormatTok->isNot(tok::l_brace)) { + if (FormatTok->is(tok::l_paren)) { + parseParens(); + continue; + } + if (FormatTok->isOneOf(tok::semi, tok::r_brace)) + return; + nextToken(); + } + NeedsUnwrappedLine = false; + CompoundStatementIndenter Indenter(this, Style, Line->Level); + parseBlock(/*MustBeDeclaration=*/false); + if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || + Style.BreakBeforeBraces == FormatStyle::BS_GNU || + Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) { + addUnwrappedLine(); + } else { + NeedsUnwrappedLine = true; + } + } + if (NeedsUnwrappedLine) { + addUnwrappedLine(); + } +} + void UnwrappedLineParser::parseNamespace() { assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected"); nextToken(); diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h index 9eb56011629..63898539b27 100644 --- a/clang/lib/Format/UnwrappedLineParser.h +++ b/clang/lib/Format/UnwrappedLineParser.h @@ -85,6 +85,7 @@ private: void parseParens(); void parseSquare(); void parseIfThenElse(); + void parseTryCatch(); void parseForOrWhileLoop(); void parseDoWhile(); void parseLabel(); |