[clang] f3dcd3a - [clang-format] Correctly limit formatted ranges when specifying qualifier alignment
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Thu May 4 02:59:30 PDT 2023
Author: Colin Ogilvie
Date: 2023-05-04T02:59:05-07:00
New Revision: f3dcd3ad992c82be4f652fd2aac6b0ef414566a2
URL: https://github.com/llvm/llvm-project/commit/f3dcd3ad992c82be4f652fd2aac6b0ef414566a2
DIFF: https://github.com/llvm/llvm-project/commit/f3dcd3ad992c82be4f652fd2aac6b0ef414566a2.diff
LOG: [clang-format] Correctly limit formatted ranges when specifying qualifier alignment
The qualifier alignment fixer appeared to ignore any ranges specified for limiting formatting.
This change ensures that it only formats affected lines to avoid unexpected changes.
Fixes #54888.
Differential Revision: https://reviews.llvm.org/D149643
Added:
Modified:
clang/lib/Format/QualifierAlignmentFixer.cpp
clang/unittests/Format/QualifierFixerTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp
index 5142a83efdd45..ff54fb75b3dd9 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -587,7 +587,7 @@ LeftRightQualifierAlignmentFixer::analyze(
assert(QualifierToken != tok::identifier && "Unrecognised Qualifier");
for (AnnotatedLine *Line : AnnotatedLines) {
- if (Line->InPPDirective)
+ if (!Line->Affected || Line->InPPDirective)
continue;
FormatToken *First = Line->First;
assert(First);
diff --git a/clang/unittests/Format/QualifierFixerTest.cpp b/clang/unittests/Format/QualifierFixerTest.cpp
index 76800b3fc8e37..9760c80dcc619 100755
--- a/clang/unittests/Format/QualifierFixerTest.cpp
+++ b/clang/unittests/Format/QualifierFixerTest.cpp
@@ -1343,6 +1343,29 @@ TEST_F(QualifierFixerTest, TemplatesLeft) {
"TemplateType<Container const> t;", Style);
}
+TEST_F(QualifierFixerTest, Ranges) {
+ FormatStyle Style = getLLVMStyle();
+ Style.QualifierAlignment = FormatStyle::QAS_Custom;
+ Style.QualifierOrder = {"const", "volatile", "type"};
+
+ // Only the first line should be formatted; the second should remain as is.
+ verifyFormat("template <typename T> const Foo f();\n"
+ "template <typename T> Foo const f();",
+ "template <typename T> Foo const f();\n"
+ "template <typename T> Foo const f();",
+ Style, {tooling::Range(0, 36)});
+
+ // Only the middle line should be formatted; the first and last should remain
+ // as is.
+ verifyFormat("template <typename T> Foo const f();\n"
+ "template <typename T> const Foo f();\n"
+ "template <typename T> Foo const f();",
+ "template <typename T> Foo const f();\n"
+ "template <typename T> Foo const f();\n"
+ "template <typename T> Foo const f();",
+ Style, {tooling::Range(37, 36)});
+}
+
} // namespace
} // namespace test
} // namespace format
More information about the cfe-commits
mailing list