diff options
| author | Krasimir Georgiev <krasimir@google.com> | 2017-07-12 15:21:43 +0000 |
|---|---|---|
| committer | Krasimir Georgiev <krasimir@google.com> | 2017-07-12 15:21:43 +0000 |
| commit | e092634f31afdf0fa5f82d81223d4726cfc7c537 (patch) | |
| tree | 8a4795489b51dd1e187e96ff26fd489e5b128688 /clang/lib | |
| parent | 8c804d97467964bee14f47c72ec6b89f8d231f99 (diff) | |
| download | bcm5719-llvm-e092634f31afdf0fa5f82d81223d4726cfc7c537.tar.gz bcm5719-llvm-e092634f31afdf0fa5f82d81223d4726cfc7c537.zip | |
[clang-format] Keep level of comment before an empty line
Summary:
This patch fixes bug https://bugs.llvm.org/show_bug.cgi?id=3313: a comment line
was aligned with the next #ifdef even in the presence of an empty line between
them.
Reviewers: djasper, klimek
Reviewed By: djasper
Subscribers: klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D35296
llvm-svn: 307795
Diffstat (limited to 'clang/lib')
| -rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index b4d20a7bd6e..c6e90a9175e 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1694,17 +1694,26 @@ void TokenAnnotator::setCommentLineLevels( for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(), E = Lines.rend(); I != E; ++I) { - bool CommentLine = (*I)->First; + bool CommentLine = true; for (const FormatToken *Tok = (*I)->First; Tok; Tok = Tok->Next) { if (!Tok->is(tok::comment)) { CommentLine = false; break; } } - if (NextNonCommentLine && CommentLine) - (*I)->Level = NextNonCommentLine->Level; - else + + if (NextNonCommentLine && CommentLine) { + // If the comment is currently aligned with the line immediately following + // it, that's probably intentional and we should keep it. + bool AlignedWithNextLine = + NextNonCommentLine->First->NewlinesBefore <= 1 && + NextNonCommentLine->First->OriginalColumn == + (*I)->First->OriginalColumn; + if (AlignedWithNextLine) + (*I)->Level = NextNonCommentLine->Level; + } else { NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : nullptr; + } setCommentLineLevels((*I)->Children); } |

