aboutsummaryrefslogtreecommitdiff
path: root/clang
diff options
context:
space:
mode:
Diffstat (limited to 'clang')
-rw-r--r--clang/lib/Format/QualifierAlignmentFixer.cpp20
-rwxr-xr-xclang/unittests/Format/QualifierFixerTest.cpp35
2 files changed, 53 insertions, 2 deletions
diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp
index aff5562..7480202 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -224,6 +224,12 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
rotateTokens(SourceMgr, Fixes, Tok, LastQual, /*Left=*/false);
Tok = LastQual;
} else if (Tok->startsSequence(QualifierType, tok::identifier,
+ TT_TemplateCloser)) {
+ FormatToken *Closer = Tok->Next->Next;
+ rotateTokens(SourceMgr, Fixes, Tok, Tok->Next, /*Left=*/false);
+ Tok = Closer;
+ return Tok;
+ } else if (Tok->startsSequence(QualifierType, tok::identifier,
TT_TemplateOpener)) {
// Read from the TemplateOpener to
// TemplateCloser as in const ArrayRef<int> a; const ArrayRef<int> &a;
@@ -307,6 +313,11 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(
rotateTokens(SourceMgr, Fixes, Tok, Tok->Next, /*Left=*/true);
Tok = Tok->Next;
}
+ } else if (Tok->startsSequence(tok::identifier, QualifierType,
+ TT_TemplateCloser)) {
+ FormatToken *Closer = Tok->Next->Next;
+ rotateTokens(SourceMgr, Fixes, Tok, Tok->Next, /*Left=*/true);
+ Tok = Closer;
}
}
if (Tok->is(TT_TemplateOpener) && Tok->Next &&
@@ -329,8 +340,12 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(
Next = Next->getNextNonComment();
assert(Next->MatchingParen && "Missing template closer");
Next = Next->MatchingParen;
+
+ // If the template closer is closing the requires clause,
+ // then stop and go back to the TemplateOpener and do whatever is
+ // inside the <>.
if (Next->ClosesRequiresClause)
- return Next;
+ return Next->MatchingParen;
Next = Next->Next;
// Move to the end of any template class members e.g.
@@ -461,7 +476,8 @@ bool LeftRightQualifierAlignmentFixer::isPossibleMacro(const FormatToken *Tok) {
if (!Tok->is(tok::identifier))
return false;
if (Tok->TokenText.upper() == Tok->TokenText.str())
- return true;
+ // T,K,U,V likely could be template arguments
+ return (Tok->TokenText.size() != 1);
return false;
}
diff --git a/clang/unittests/Format/QualifierFixerTest.cpp b/clang/unittests/Format/QualifierFixerTest.cpp
index 0c81c83..167a30e 100755
--- a/clang/unittests/Format/QualifierFixerTest.cpp
+++ b/clang/unittests/Format/QualifierFixerTest.cpp
@@ -895,5 +895,40 @@ TEST_F(QualifierFixerTest, DisableRegions) {
Style);
}
+TEST_F(QualifierFixerTest, TemplatesRight) {
+ FormatStyle Style = getLLVMStyle();
+ Style.QualifierAlignment = FormatStyle::QAS_Custom;
+ Style.QualifierOrder = {"type", "const"};
+
+ verifyFormat("template <typename T>\n"
+ " requires Concept<T const>\n"
+ "void f();",
+ "template <typename T>\n"
+ " requires Concept<const T>\n"
+ "void f();",
+ Style);
+ verifyFormat("TemplateType<T const> t;", "TemplateType<const T> t;", Style);
+ verifyFormat("TemplateType<Container const> t;",
+ "TemplateType<const Container> t;", Style);
+}
+
+TEST_F(QualifierFixerTest, TemplatesLeft) {
+ FormatStyle Style = getLLVMStyle();
+ Style.QualifierAlignment = FormatStyle::QAS_Custom;
+ Style.QualifierOrder = {"const", "type"};
+
+ verifyFormat("template <const T> t;", "template <T const> t;", Style);
+ verifyFormat("template <typename T>\n"
+ " requires Concept<const T>\n"
+ "void f();",
+ "template <typename T>\n"
+ " requires Concept<T const>\n"
+ "void f();",
+ Style);
+ verifyFormat("TemplateType<const T> t;", "TemplateType<T const> t;", Style);
+ verifyFormat("TemplateType<const Container> t;",
+ "TemplateType<Container const> t;", Style);
+}
+
} // namespace format
} // namespace clang