Skip to content

Commit

Permalink
[clang-format][NFC] Refactor getting first/last non-comment of line (l…
Browse files Browse the repository at this point in the history
  • Loading branch information
owenca committed Dec 6, 2023
1 parent f1c08ee commit 1241b5b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
5 changes: 5 additions & 0 deletions clang/lib/Format/TokenAnnotator.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ class AnnotatedLine {
return First->is(tok::comment) ? First->getNextNonComment() : First;
}

FormatToken *getLastNonComment() const {
assert(Last);
return Last->is(tok::comment) ? Last->getPreviousNonComment() : Last;
}

FormatToken *First;
FormatToken *Last;

Expand Down
19 changes: 6 additions & 13 deletions clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,10 @@ class LineJoiner {
return false;

// Check if the found line starts a record.
const FormatToken *LastNonComment = Line->Last;
const auto *LastNonComment = Line->getLastNonComment();
// There must be another token (usually `{`), because we chose a
// non-PPDirective and non-comment line that has a smaller level.
assert(LastNonComment);
if (LastNonComment->is(tok::comment)) {
LastNonComment = LastNonComment->getPreviousNonComment();
// There must be another token (usually `{`), because we chose a
// non-PPDirective and non-comment line that has a smaller level.
assert(LastNonComment);
}
return isRecordLBrace(*LastNonComment);
}
}
Expand All @@ -363,12 +359,9 @@ class LineJoiner {

bool MergeShortFunctions = ShouldMergeShortFunctions();

const FormatToken *FirstNonComment = TheLine->First;
if (FirstNonComment->is(tok::comment)) {
FirstNonComment = FirstNonComment->getNextNonComment();
if (!FirstNonComment)
return 0;
}
const auto *FirstNonComment = TheLine->getFirstNonComment();
if (!FirstNonComment)
return 0;
// FIXME: There are probably cases where we should use FirstNonComment
// instead of TheLine->First.

Expand Down

0 comments on commit 1241b5b

Please sign in to comment.