Skip to content

Conversation

@Sandeep2265
Copy link

Added function checks if a type is a TypedefType and, if so, returns its original typedef name instead of printing the full type.This helps keep typedef names preserved in ExtractAPI declaration fragments instead of showing the underlying struct type.

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Nov 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 10, 2025

@llvm/pr-subscribers-clang

Author: None (Sandeep2265)

Changes

Added function checks if a type is a TypedefType and, if so, returns its original typedef name instead of printing the full type.This helps keep typedef names preserved in ExtractAPI declaration fragments instead of showing the underlying struct type.


Full diff: https://github.com/llvm/llvm-project/pull/167319.diff

1 Files Affected:

  • (modified) clang/lib/ExtractAPI/DeclarationFragments.cpp (+13-1)
diff --git a/clang/lib/ExtractAPI/DeclarationFragments.cpp b/clang/lib/ExtractAPI/DeclarationFragments.cpp
index e5eda46df8056..16a524702e963 100644
--- a/clang/lib/ExtractAPI/DeclarationFragments.cpp
+++ b/clang/lib/ExtractAPI/DeclarationFragments.cpp
@@ -63,6 +63,17 @@ void findTypeLocForBlockDecl(const clang::TypeSourceInfo *TSInfo,
 
 } // namespace
 
+static std::string OriginalNameTypedef(const clang::QualType &qt,
+                                       const clang::PrintingPolicy &policy) {
+  if (const auto *tt = llvm::dyn_cast<clang::TypedefType>(qt.getTypePtrOrNull())) {
+    const auto *td = tt->getDecl();
+    if (!td->getName().empty())
+      return td->getName().str();
+  }
+  return qt.getAsString(policy);
+}
+
+
 DeclarationFragments &
 DeclarationFragments::appendUnduplicatedTextCharacter(char Character) {
   if (!Fragments.empty()) {
@@ -452,7 +463,8 @@ DeclarationFragments DeclarationFragmentsBuilder::getFragmentsForType(
   // Default fragment builder for other kinds of types (BuiltinType etc.)
   SmallString<128> USR;
   clang::index::generateUSRForType(Base, Context, USR);
-  Fragments.append(Base.getAsString(),
+  std::string typestr = OriginalNameTypedef(Base, Context.getPrintingPolicy());
+  Fragments.append(typestr,
                    DeclarationFragments::FragmentKind::TypeIdentifier, USR);
 
   return Fragments;

@QuietMisdreavus
Copy link
Contributor

Hey there, thanks for opening this PR! Are you able to reduce the issue into a test we can run? You should be able to adapt one of the typedef tests to test one of the situations from the issue you linked.

@github-actions
Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions cpp -- clang/lib/ExtractAPI/DeclarationFragments.cpp --diff_from_common_commit

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/clang/lib/ExtractAPI/DeclarationFragments.cpp b/clang/lib/ExtractAPI/DeclarationFragments.cpp
index 16a524702..dbef6a3dc 100644
--- a/clang/lib/ExtractAPI/DeclarationFragments.cpp
+++ b/clang/lib/ExtractAPI/DeclarationFragments.cpp
@@ -65,7 +65,8 @@ void findTypeLocForBlockDecl(const clang::TypeSourceInfo *TSInfo,
 
 static std::string OriginalNameTypedef(const clang::QualType &qt,
                                        const clang::PrintingPolicy &policy) {
-  if (const auto *tt = llvm::dyn_cast<clang::TypedefType>(qt.getTypePtrOrNull())) {
+  if (const auto *tt =
+          llvm::dyn_cast<clang::TypedefType>(qt.getTypePtrOrNull())) {
     const auto *td = tt->getDecl();
     if (!td->getName().empty())
       return td->getName().str();
@@ -73,7 +74,6 @@ static std::string OriginalNameTypedef(const clang::QualType &qt,
   return qt.getAsString(policy);
 }
 
-
 DeclarationFragments &
 DeclarationFragments::appendUnduplicatedTextCharacter(char Character) {
   if (!Fragments.empty()) {
@@ -464,8 +464,8 @@ DeclarationFragments DeclarationFragmentsBuilder::getFragmentsForType(
   SmallString<128> USR;
   clang::index::generateUSRForType(Base, Context, USR);
   std::string typestr = OriginalNameTypedef(Base, Context.getPrintingPolicy());
-  Fragments.append(typestr,
-                   DeclarationFragments::FragmentKind::TypeIdentifier, USR);
+  Fragments.append(typestr, DeclarationFragments::FragmentKind::TypeIdentifier,
+                   USR);
 
   return Fragments;
 }

@shafik shafik requested review from AaronBallman and zwuis November 20, 2025 05:55
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The explanation does not add up. The subject is not a proper subject, it does not tell us what the PR does. We need a more complete rationale and tests to demonstrate what this change actually does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants