diff options
author | Krasimir Georgiev <krasimir@google.com> | 2017-02-02 15:32:19 +0000 |
---|---|---|
committer | Krasimir Georgiev <krasimir@google.com> | 2017-02-02 15:32:19 +0000 |
commit | 00c5c72d0ec97708a4e6ad6d07591d0c3e6b6643 (patch) | |
tree | 8a02c1f21da3612b94cec92fb70faa5ac9bfe5eb /clang/lib/Format/BreakableToken.cpp | |
parent | ae7de7117a088f6ca6bd176f995c405b87f8cf1e (diff) | |
download | bcm5719-llvm-00c5c72d0ec97708a4e6ad6d07591d0c3e6b6643.tar.gz bcm5719-llvm-00c5c72d0ec97708a4e6ad6d07591d0c3e6b6643.zip |
[clang-format] Don't reflow across comment pragmas.
Summary:
The comment reflower wasn't taking comment pragmas as reflow stoppers. This patch fixes that.
source:
```
// long long long long
// IWYU pragma:
```
format with column limit = 20 before:
```
// long long long
// long IWYU pragma:
```
format with column limit = 20 after:
```
// long long long
// long
// IWYU pragma:
```
Reviewers: djasper
Reviewed By: djasper
Subscribers: cfe-commits, klimek
Differential Revision: https://reviews.llvm.org/D29450
llvm-svn: 293898
Diffstat (limited to 'clang/lib/Format/BreakableToken.cpp')
-rw-r--r-- | clang/lib/Format/BreakableToken.cpp | 47 |
1 files changed, 34 insertions, 13 deletions
diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp index e9dd253f3ec..14557a7e370 100644 --- a/clang/lib/Format/BreakableToken.cpp +++ b/clang/lib/Format/BreakableToken.cpp @@ -321,13 +321,6 @@ static bool mayReflowContent(StringRef Content) { (!isPunctuation(Content[0]) || !isPunctuation(Content[1])); } -bool BreakableComment::mayReflow(unsigned LineIndex) const { - return LineIndex > 0 && mayReflowContent(Content[LineIndex]) && - !Tok.Finalized && !switchesFormatting(tokenAt(LineIndex)) && - (!Tok.is(TT_LineComment) || - OriginalPrefix[LineIndex] == OriginalPrefix[LineIndex - 1]); -} - BreakableBlockComment::BreakableBlockComment( const FormatToken &Token, unsigned StartColumn, unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective, @@ -501,8 +494,9 @@ void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset, BreakableToken::Split BreakableBlockComment::getSplitBefore( unsigned LineIndex, unsigned PreviousEndColumn, - unsigned ColumnLimit) const { - if (!mayReflow(LineIndex)) + unsigned ColumnLimit, + llvm::Regex &CommentPragmasRegex) const { + if (!mayReflow(LineIndex, CommentPragmasRegex)) return Split(StringRef::npos, 0); StringRef TrimmedContent = Content[LineIndex].ltrim(Blanks); return getReflowSplit(TrimmedContent, ReflowPrefix, PreviousEndColumn, @@ -622,6 +616,19 @@ void BreakableBlockComment::replaceWhitespaceBefore( InPPDirective, /*Newlines=*/1, ContentColumn[LineIndex] - Prefix.size()); } +bool BreakableBlockComment::mayReflow(unsigned LineIndex, + llvm::Regex &CommentPragmasRegex) const { + // Content[LineIndex] may exclude the indent after the '*' decoration. In that + // case, we compute the start of the comment pragma manually. + StringRef IndentContent = Content[LineIndex]; + if (Lines[LineIndex].ltrim(Blanks).startswith("*")) { + IndentContent = Lines[LineIndex].ltrim(Blanks).substr(1); + } + return LineIndex > 0 && !CommentPragmasRegex.match(IndentContent) && + mayReflowContent(Content[LineIndex]) && !Tok.Finalized && + !switchesFormatting(tokenAt(LineIndex)); +} + unsigned BreakableBlockComment::getContentStartColumn(unsigned LineIndex, unsigned TailOffset) const { @@ -748,10 +755,10 @@ void BreakableLineCommentSection::insertBreak(unsigned LineIndex, } BreakableComment::Split BreakableLineCommentSection::getSplitBefore( - unsigned LineIndex, - unsigned PreviousEndColumn, - unsigned ColumnLimit) const { - if (!mayReflow(LineIndex)) return Split(StringRef::npos, 0); + unsigned LineIndex, unsigned PreviousEndColumn, unsigned ColumnLimit, + llvm::Regex &CommentPragmasRegex) const { + if (!mayReflow(LineIndex, CommentPragmasRegex)) + return Split(StringRef::npos, 0); return getReflowSplit(Content[LineIndex], ReflowPrefix, PreviousEndColumn, ColumnLimit); } @@ -850,6 +857,20 @@ void BreakableLineCommentSection::updateNextToken(LineState& State) const { } } +bool BreakableLineCommentSection::mayReflow( + unsigned LineIndex, llvm::Regex &CommentPragmasRegex) const { + // Line comments have the indent as part of the prefix, so we need to + // recompute the start of the line. + StringRef IndentContent = Content[LineIndex]; + if (Lines[LineIndex].startswith("//")) { + IndentContent = Lines[LineIndex].substr(2); + } + return LineIndex > 0 && !CommentPragmasRegex.match(IndentContent) && + mayReflowContent(Content[LineIndex]) && !Tok.Finalized && + !switchesFormatting(tokenAt(LineIndex)) && + OriginalPrefix[LineIndex] == OriginalPrefix[LineIndex - 1]; +} + unsigned BreakableLineCommentSection::getContentStartColumn(unsigned LineIndex, unsigned TailOffset) const { |