Author: Michael Buch Date: 2026-02-25T15:55:58Z New Revision: 5eb307e299c76e2520174acf45ea3a3d320a9509
URL: https://github.com/llvm/llvm-project/commit/5eb307e299c76e2520174acf45ea3a3d320a9509 DIFF: https://github.com/llvm/llvm-project/commit/5eb307e299c76e2520174acf45ea3a3d320a9509.diff LOG: [clang][TypePrinter][NFC] Extract logic that handles AnonymousTagNameStyle::SourceLocation into helper function (#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. Added: Modified: clang/include/clang/AST/Decl.h clang/lib/AST/Decl.cpp Removed: ################################################################################ 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
