[clang] [clang-format] Handle lambdas in QualifierAlignment (PR #72456)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 15 16:16:57 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
Fixed #<!-- -->62780.
---
Full diff: https://github.com/llvm/llvm-project/pull/72456.diff
3 Files Affected:
- (modified) clang/lib/Format/QualifierAlignmentFixer.cpp (+9-3)
- (modified) clang/lib/Format/QualifierAlignmentFixer.h (+4)
- (modified) clang/unittests/Format/QualifierFixerTest.cpp (+6)
``````````diff
diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp
index e2fab1c1e3c2a37..84941746f0df71b 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -535,14 +535,21 @@ LeftRightQualifierAlignmentFixer::analyze(
SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
FormatTokenLexer &Tokens) {
tooling::Replacements Fixes;
- const AdditionalKeywords &Keywords = Tokens.getKeywords();
- const SourceManager &SourceMgr = Env.getSourceManager();
AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
+ fixQualifierAlignment(AnnotatedLines, Tokens, Fixes);
+ return {Fixes, 0};
+}
+void LeftRightQualifierAlignmentFixer::fixQualifierAlignment(
+ SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, FormatTokenLexer &Tokens,
+ tooling::Replacements &Fixes) {
+ const AdditionalKeywords &Keywords = Tokens.getKeywords();
+ const SourceManager &SourceMgr = Env.getSourceManager();
tok::TokenKind QualifierToken = getTokenFromQualifier(Qualifier);
assert(QualifierToken != tok::identifier && "Unrecognised Qualifier");
for (AnnotatedLine *Line : AnnotatedLines) {
+ fixQualifierAlignment(Line->Children, Tokens, Fixes);
if (!Line->Affected || Line->InPPDirective)
continue;
FormatToken *First = Line->First;
@@ -565,7 +572,6 @@ LeftRightQualifierAlignmentFixer::analyze(
}
}
}
- return {Fixes, 0};
}
void prepareLeftRightOrderingForQualifierAlignmentFixer(
diff --git a/clang/lib/Format/QualifierAlignmentFixer.h b/clang/lib/Format/QualifierAlignmentFixer.h
index a72d135179f1ece..e922d8005595103 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.h
+++ b/clang/lib/Format/QualifierAlignmentFixer.h
@@ -52,6 +52,10 @@ class LeftRightQualifierAlignmentFixer : public TokenAnalyzer {
static tok::TokenKind getTokenFromQualifier(const std::string &Qualifier);
+ void fixQualifierAlignment(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
+ FormatTokenLexer &Tokens,
+ tooling::Replacements &Fixes);
+
const FormatToken *analyzeRight(const SourceManager &SourceMgr,
const AdditionalKeywords &Keywords,
tooling::Replacements &Fixes,
diff --git a/clang/unittests/Format/QualifierFixerTest.cpp b/clang/unittests/Format/QualifierFixerTest.cpp
index a56323a88f4a04d..324366ca7f5e511 100644
--- a/clang/unittests/Format/QualifierFixerTest.cpp
+++ b/clang/unittests/Format/QualifierFixerTest.cpp
@@ -357,6 +357,9 @@ TEST_F(QualifierFixerTest, RightQualifier) {
verifyFormat("void f(std::integral auto const &x);",
"void f(const std::integral auto &x);", Style);
+ verifyFormat("auto lambda = [] { int const i = 0; };",
+ "auto lambda = [] { const int i = 0; };", Style);
+
verifyFormat("Foo<Foo<int> const> P;\n#if 0\n#else\n#endif",
"Foo<const Foo<int>> P;\n#if 0\n#else\n#endif", Style);
@@ -663,6 +666,9 @@ TEST_F(QualifierFixerTest, LeftQualifier) {
verifyFormat("void f(const std::integral auto &x);",
"void f(std::integral auto const &x);", Style);
+ verifyFormat("auto lambda = [] { const int i = 0; };",
+ "auto lambda = [] { int const i = 0; };", Style);
+
verifyFormat("Foo<const Foo<int>> P;\n#if 0\n#else\n#endif",
"Foo<Foo<int> const> P;\n#if 0\n#else\n#endif", Style);
``````````
</details>
https://github.com/llvm/llvm-project/pull/72456
More information about the cfe-commits
mailing list