https://github.com/Michael137 created 
https://github.com/llvm/llvm-project/pull/183304

In https://github.com/llvm/llvm-project/pull/168533 we're adding a new 
`AnonymousTagMode` and will be handled in `printAnonymousTagDecl`.

This patch extracts the logic that handles 
`AnonymousTagNameStyle::SourceLocation` into a helper function to make 
`printAnonymousTagDecl` easier to follow.

Drive-by changes:
* While copying the code into the helper I changed it to use early-return style.

>From 40312551153b3fa4fdac0777dde18bcdb8c23c1f Mon Sep 17 00:00:00 2001
From: Michael Buch <[email protected]>
Date: Mon, 16 Feb 2026 16:23:08 +0000
Subject: [PATCH] [clang][TypePrinter][NFC] Add helper to print tag locations

In https://github.com/llvm/llvm-project/pull/168533 we're adding a new 
`AnonymousTagMode` and will be handled in `printAnonymousTagDecl`.

This patch extracts the logic that handles 
`AnonymousTagNameStyle::SourceLocation` into a helper function to make 
`printAnonymousTagDecl` easier to follow.

Drive-by changes:
* While copying the code into the helper I changed it to use
  early-return style.
---
 clang/include/clang/AST/Decl.h |  3 +++
 clang/lib/AST/Decl.cpp         | 48 ++++++++++++++++++----------------
 2 files changed, 29 insertions(+), 22 deletions(-)

diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 5c46c912186c4..c3cd74a5b34db 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -3774,6 +3774,9 @@ class TagDecl : public TypeDecl,
   void printAnonymousTagDecl(llvm::raw_ostream &OS,
                              const PrintingPolicy &Policy) const;
 
+  void printAnonymousTagDeclLocation(llvm::raw_ostream &OS,
+                                     const PrintingPolicy &Policy) const;
+
 public:
   friend class ASTDeclReader;
   friend class ASTDeclWriter;
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 4148e0ce16b7d..37b00eeca539c 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -4961,6 +4961,30 @@ void TagDecl::setQualifierInfo(NestedNameSpecifierLoc 
QualifierLoc) {
   }
 }
 
+void TagDecl::printAnonymousTagDeclLocation(
+    llvm::raw_ostream &OS, const PrintingPolicy &Policy) const {
+  PresumedLoc PLoc =
+      getASTContext().getSourceManager().getPresumedLoc(getLocation());
+  if (!PLoc.isValid())
+    return;
+
+  OS << " at ";
+  StringRef File = PLoc.getFilename();
+  llvm::SmallString<1024> WrittenFile(File);
+  if (auto *Callbacks = Policy.Callbacks)
+    WrittenFile = Callbacks->remapPath(File);
+  // Fix inconsistent path separator created by
+  // clang::DirectoryLookup::LookupFile when the file path is relative
+  // path.
+  llvm::sys::path::Style Style =
+      llvm::sys::path::is_absolute(WrittenFile)
+          ? llvm::sys::path::Style::native
+          : (Policy.MSVCFormatting ? llvm::sys::path::Style::windows_backslash
+                                   : llvm::sys::path::Style::posix);
+  llvm::sys::path::native(WrittenFile, Style);
+  OS << WrittenFile << ':' << PLoc.getLine() << ':' << PLoc.getColumn();
+}
+
 void TagDecl::printAnonymousTagDecl(llvm::raw_ostream &OS,
                                     const PrintingPolicy &Policy) const {
   if (TypedefNameDecl *Typedef = getTypedefNameForAnonDecl()) {
@@ -4996,28 +5020,8 @@ void TagDecl::printAnonymousTagDecl(llvm::raw_ostream 
&OS,
     OS << ' ' << getKindName();
 
   if (Policy.AnonymousTagNameStyle ==
-      llvm::to_underlying(PrintingPolicy::AnonymousTagMode::SourceLocation)) {
-    PresumedLoc PLoc =
-        getASTContext().getSourceManager().getPresumedLoc(getLocation());
-    if (PLoc.isValid()) {
-      OS << " at ";
-      StringRef File = PLoc.getFilename();
-      llvm::SmallString<1024> WrittenFile(File);
-      if (auto *Callbacks = Policy.Callbacks)
-        WrittenFile = Callbacks->remapPath(File);
-      // Fix inconsistent path separator created by
-      // clang::DirectoryLookup::LookupFile when the file path is relative
-      // path.
-      llvm::sys::path::Style Style =
-          llvm::sys::path::is_absolute(WrittenFile)
-              ? llvm::sys::path::Style::native
-              : (Policy.MSVCFormatting
-                     ? llvm::sys::path::Style::windows_backslash
-                     : llvm::sys::path::Style::posix);
-      llvm::sys::path::native(WrittenFile, Style);
-      OS << WrittenFile << ':' << PLoc.getLine() << ':' << PLoc.getColumn();
-    }
-  }
+      llvm::to_underlying(PrintingPolicy::AnonymousTagMode::SourceLocation))
+    printAnonymousTagDeclLocation(OS, Policy);
 
   OS << (Policy.MSVCFormatting ? '\'' : ')');
 }

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to