-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Issue #61157 #167319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Issue #61157 #167319
Conversation
…ue with typedefs for structs.
|
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 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. |
|
@llvm/pr-subscribers-clang Author: None (Sandeep2265) ChangesAdded 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:
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;
|
|
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. |
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
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;
}
|
There was a problem hiding this 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.
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.