summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format/UnwrappedLineParser.cpp
diff options
context:
space:
mode:
authorKrasimir Georgiev <krasimir@google.com>2017-01-30 19:18:55 +0000
committerKrasimir Georgiev <krasimir@google.com>2017-01-30 19:18:55 +0000
commit8432161f1dbd61d8acb88e906cbe9535cc56748c (patch)
tree94776c64c329792fe2fe7096f0e3dae865f8f248 /clang/lib/Format/UnwrappedLineParser.cpp
parent3695d06a10df6dca79758150c5d0bca882f42d7a (diff)
downloadbcm5719-llvm-8432161f1dbd61d8acb88e906cbe9535cc56748c.tar.gz
bcm5719-llvm-8432161f1dbd61d8acb88e906cbe9535cc56748c.zip
[clang-format] Separate line comment sections after a right brace from comment sections in the scope.
Summary: The following two comment lines form a single comment section: ``` if (1) { // line 1 // line 2 } ``` This is because the break of a comment section was based on the original column of the first token of the previous line (in this case, the 'if'). This patch splits these two comment lines into different sections by taking into account the original column of the right brace preceding the first line comment where applicable. Reviewers: djasper Reviewed By: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D29291 llvm-svn: 293539
Diffstat (limited to 'clang/lib/Format/UnwrappedLineParser.cpp')
-rw-r--r--clang/lib/Format/UnwrappedLineParser.cpp46
1 files changed, 39 insertions, 7 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 5e67bde1f24..c10a13f39e0 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2051,14 +2051,19 @@ static bool continuesLineComment(const FormatToken &FormatTok,
const UnwrappedLine &Line) {
if (Line.Tokens.empty())
return false;
- const FormatToken &FirstLineTok = *Line.Tokens.front().Tok;
+
// If Line starts with a line comment, then FormatTok continues the comment
- // section if its original column is greater or equal to the original start
+ // section if its original column is greater or equal to the original start
// column of the line.
//
- // If Line starts with a a different token, then FormatTok continues the
- // comment section if its original column greater than the original start
- // column of the line.
+ // Define the min column token of a line as follows: if a line ends in '{' or
+ // contains a '{' followed by a line comment, then the min column token is
+ // that '{'. Otherwise, the min column token of the line is the first token of
+ // the line.
+ //
+ // If Line starts with a token other than a line comment, then FormatTok
+ // continues the comment section if its original column is greater than the
+ // original start column of the min column token of the line.
//
// For example, the second line comment continues the first in these cases:
// // first line
@@ -2069,6 +2074,11 @@ static bool continuesLineComment(const FormatToken &FormatTok,
// and:
// int i; // first line
// // second line
+ // and:
+ // do { // first line
+ // // second line
+ // int i;
+ // } while (true);
//
// The second line comment doesn't continue the first in these cases:
// // first line
@@ -2076,9 +2086,31 @@ static bool continuesLineComment(const FormatToken &FormatTok,
// and:
// int i; // first line
// // second line
+ // and:
+ // do { // first line
+ // // second line
+ // int i;
+ // } while (true);
+ const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
+
+ // Scan for '{//'. If found, use the column of '{' as a min column for line
+ // comment section continuation.
+ const FormatToken *PreviousToken = nullptr;
+ for (const UnwrappedLineNode Node : Line.Tokens) {
+ if (PreviousToken && PreviousToken->is(tok::l_brace) &&
+ isLineComment(*Node.Tok)) {
+ MinColumnToken = PreviousToken;
+ break;
+ }
+ PreviousToken = Node.Tok;
+ }
+ if (PreviousToken && PreviousToken->is(tok::l_brace)) {
+ MinColumnToken = PreviousToken;
+ }
+
unsigned MinContinueColumn =
- FirstLineTok.OriginalColumn +
- ((isLineComment(FirstLineTok) && !FirstLineTok.Next) ? 0 : 1);
+ MinColumnToken->OriginalColumn +
+ (isLineComment(*MinColumnToken) ? 0 : 1);
return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
isLineComment(*(Line.Tokens.back().Tok)) &&
FormatTok.OriginalColumn >= MinContinueColumn;
OpenPOWER on IntegriCloud