Skip to content

Commit 7e061a7

Browse files
committed
[clang-format] Don't swap (const override) with QAS_Right
Fixes #154846
1 parent 3c0d4aa commit 7e061a7

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

clang/lib/Format/QualifierAlignmentFixer.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,16 @@ static bool isQualifier(const FormatToken *const Tok) {
177177

178178
const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
179179
const SourceManager &SourceMgr, const AdditionalKeywords &Keywords,
180-
tooling::Replacements &Fixes, const FormatToken *const Tok,
180+
tooling::Replacements &Fixes, const FormatToken *Tok,
181181
const std::string &Qualifier, tok::TokenKind QualifierType) {
182182
// We only need to think about streams that begin with a qualifier.
183183
if (Tok->isNot(QualifierType))
184184
return Tok;
185+
186+
const auto *Next = Tok->getNextNonComment();
187+
185188
// Don't concern yourself if nothing follows the qualifier.
186-
if (!Tok->Next)
189+
if (!Next)
187190
return Tok;
188191

189192
// Skip qualifiers to the left to find what preceeds the qualifiers.
@@ -247,9 +250,15 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
247250
}();
248251

249252
// Find the last qualifier to the right.
250-
const FormatToken *LastQual = Tok;
251-
while (isQualifier(LastQual->getNextNonComment()))
252-
LastQual = LastQual->getNextNonComment();
253+
const auto *LastQual = Tok;
254+
for (; isQualifier(Next); Next = Next->getNextNonComment())
255+
LastQual = Next;
256+
257+
if (!LastQual || !Next ||
258+
(LastQual->isOneOf(tok::kw_const, tok::kw_volatile) &&
259+
Next->isOneOf(Keywords.kw_override, Keywords.kw_final))) {
260+
return Tok;
261+
}
253262

254263
// If this qualifier is to the right of a type or pointer do a partial sort
255264
// and return.

clang/unittests/Format/QualifierFixerTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ TEST_F(QualifierFixerTest, RightQualifier) {
215215
Style);
216216
verifyFormat("void foo() const override;", Style);
217217
verifyFormat("void foo() const override LLVM_READONLY;", Style);
218+
verifyFormat("MOCK_METHOD(ReturnType, myMethod, (int), (const override));",
219+
Style);
218220
verifyFormat("void foo() const final;", Style);
219221
verifyFormat("void foo() const final LLVM_READONLY;", Style);
220222
verifyFormat("void foo() const LLVM_READONLY;", Style);

0 commit comments

Comments
 (0)