Skip to content

Commit 1241b5b

Browse files
authored
[clang-format][NFC] Refactor getting first/last non-comment of line (#74570)
1 parent f1c08ee commit 1241b5b

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

clang/lib/Format/TokenAnnotator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ class AnnotatedLine {
156156
return First->is(tok::comment) ? First->getNextNonComment() : First;
157157
}
158158

159+
FormatToken *getLastNonComment() const {
160+
assert(Last);
161+
return Last->is(tok::comment) ? Last->getPreviousNonComment() : Last;
162+
}
163+
159164
FormatToken *First;
160165
FormatToken *Last;
161166

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,10 @@ class LineJoiner {
346346
return false;
347347

348348
// Check if the found line starts a record.
349-
const FormatToken *LastNonComment = Line->Last;
349+
const auto *LastNonComment = Line->getLastNonComment();
350+
// There must be another token (usually `{`), because we chose a
351+
// non-PPDirective and non-comment line that has a smaller level.
350352
assert(LastNonComment);
351-
if (LastNonComment->is(tok::comment)) {
352-
LastNonComment = LastNonComment->getPreviousNonComment();
353-
// There must be another token (usually `{`), because we chose a
354-
// non-PPDirective and non-comment line that has a smaller level.
355-
assert(LastNonComment);
356-
}
357353
return isRecordLBrace(*LastNonComment);
358354
}
359355
}
@@ -363,12 +359,9 @@ class LineJoiner {
363359

364360
bool MergeShortFunctions = ShouldMergeShortFunctions();
365361

366-
const FormatToken *FirstNonComment = TheLine->First;
367-
if (FirstNonComment->is(tok::comment)) {
368-
FirstNonComment = FirstNonComment->getNextNonComment();
369-
if (!FirstNonComment)
370-
return 0;
371-
}
362+
const auto *FirstNonComment = TheLine->getFirstNonComment();
363+
if (!FirstNonComment)
364+
return 0;
372365
// FIXME: There are probably cases where we should use FirstNonComment
373366
// instead of TheLine->First.
374367

0 commit comments

Comments
 (0)