[clang] [clang-format] Allow custom pointer/ref alignment in return types (PR #169160)

Björn Schäpers via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 22 06:46:09 PST 2025


================
@@ -6540,12 +6540,44 @@ void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) const {
   llvm::errs() << "----\n";
 }
 
+static bool isReturnType(const FormatToken &Tok) {
+  // Look forward to see if there's a function declaration paren
+  const FormatToken *Next = Tok.Next;
+  while (Next) {
+    if (Next->isOneOf(TT_FunctionDeclarationLParen, TT_FunctionTypeLParen))
+      return true;
+    // Stop at certain tokens that indicate we're not in a return type
+    if (Next->isOneOf(tok::semi, tok::l_brace, tok::comma, tok::equal,
+                      TT_LambdaLSquare)) {
+      break;
+    }
+
+    Next = Next->Next;
+  }
+  return false;
+}
+
 FormatStyle::PointerAlignmentStyle
 TokenAnnotator::getTokenReferenceAlignment(const FormatToken &Reference) const {
   assert(Reference.isOneOf(tok::amp, tok::ampamp));
-  switch (Style.ReferenceAlignment) {
+
+  if (Style.ReferenceAlignment.ReturnType != FormatStyle::SAS_Default &&
+      isReturnType(Reference)) {
+    switch (Style.ReferenceAlignment.ReturnType) {
+    case FormatStyle::SAS_Left:
+      return FormatStyle::PAS_Left;
+    case FormatStyle::SAS_Right:
+      return FormatStyle::PAS_Right;
+    case FormatStyle::SAS_Middle:
+      return FormatStyle::PAS_Middle;
+    case FormatStyle::SAS_Default:
+      assert(0);
----------------
HazardyKnusperkeks wrote:

```suggestion
      assert(false);
```

https://github.com/llvm/llvm-project/pull/169160


More information about the cfe-commits mailing list