summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format/UnwrappedLineParser.cpp
diff options
context:
space:
mode:
authorKrasimir Georgiev <krasimir@google.com>2017-02-02 15:32:19 +0000
committerKrasimir Georgiev <krasimir@google.com>2017-02-02 15:32:19 +0000
commit00c5c72d0ec97708a4e6ad6d07591d0c3e6b6643 (patch)
tree8a02c1f21da3612b94cec92fb70faa5ac9bfe5eb /clang/lib/Format/UnwrappedLineParser.cpp
parentae7de7117a088f6ca6bd176f995c405b87f8cf1e (diff)
downloadbcm5719-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/UnwrappedLineParser.cpp')
-rw-r--r--clang/lib/Format/UnwrappedLineParser.cpp18
1 files changed, 14 insertions, 4 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 2c7b87ee014..66da1feee9d 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -202,7 +202,8 @@ UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
ArrayRef<FormatToken *> Tokens,
UnwrappedLineConsumer &Callback)
: Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
- CurrentLines(&Lines), Style(Style), Keywords(Keywords), Tokens(nullptr),
+ CurrentLines(&Lines), Style(Style), Keywords(Keywords),
+ CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
void UnwrappedLineParser::reset() {
@@ -2048,10 +2049,18 @@ static bool isLineComment(const FormatToken &FormatTok) {
// Checks if \p FormatTok is a line comment that continues the line comment
// section on \p Line.
static bool continuesLineComment(const FormatToken &FormatTok,
- const UnwrappedLine &Line) {
+ const UnwrappedLine &Line,
+ llvm::Regex &CommentPragmasRegex) {
if (Line.Tokens.empty())
return false;
+ StringRef IndentContent = FormatTok.TokenText;
+ if (FormatTok.TokenText.startswith("//") ||
+ FormatTok.TokenText.startswith("/*"))
+ IndentContent = FormatTok.TokenText.substr(2);
+ if (CommentPragmasRegex.match(IndentContent))
+ return false;
+
// 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
// column of the line.
@@ -2162,7 +2171,8 @@ void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
//
// FIXME: Consider putting separate line comment sections as children to the
// unwrapped line instead.
- (*I)->ContinuesLineCommentSection = continuesLineComment(**I, *Line);
+ (*I)->ContinuesLineCommentSection =
+ continuesLineComment(**I, *Line, CommentPragmasRegex);
if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
addUnwrappedLine();
pushToken(*I);
@@ -2230,7 +2240,7 @@ void UnwrappedLineParser::readToken() {
if (!FormatTok->Tok.is(tok::comment))
return;
FormatTok->ContinuesLineCommentSection =
- continuesLineComment(*FormatTok, *Line);
+ continuesLineComment(*FormatTok, *Line, CommentPragmasRegex);
if (!FormatTok->ContinuesLineCommentSection &&
(isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
CommentsInCurrentLine = false;
OpenPOWER on IntegriCloud