[clang] [clang][ExtractAPI] emit correct spelling for type aliases (PR #134007)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 1 16:34:23 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Erick Velez (evelez7)
<details>
<summary>Changes</summary>
Previously, C++11 type aliases were serialized using "typedef" regardless of the source spelling.
This checks if the TypedefNameDecl is actually a TypeAliasDecl and corrects the spelling.
---
Full diff: https://github.com/llvm/llvm-project/pull/134007.diff
2 Files Affected:
- (modified) clang/lib/ExtractAPI/DeclarationFragments.cpp (+19-7)
- (added) clang/test/ExtractAPI/type-alias.cpp (+56)
``````````diff
diff --git a/clang/lib/ExtractAPI/DeclarationFragments.cpp b/clang/lib/ExtractAPI/DeclarationFragments.cpp
index d7eebcbc3c2f9..80174e30ffd1a 100644
--- a/clang/lib/ExtractAPI/DeclarationFragments.cpp
+++ b/clang/lib/ExtractAPI/DeclarationFragments.cpp
@@ -1584,13 +1584,25 @@ DeclarationFragments DeclarationFragmentsBuilder::getFragmentsForObjCProtocol(
DeclarationFragments DeclarationFragmentsBuilder::getFragmentsForTypedef(
const TypedefNameDecl *Decl) {
DeclarationFragments Fragments, After;
- Fragments.append("typedef", DeclarationFragments::FragmentKind::Keyword)
- .appendSpace()
- .append(getFragmentsForType(Decl->getUnderlyingType(),
- Decl->getASTContext(), After))
- .append(std::move(After))
- .appendSpace()
- .append(Decl->getName(), DeclarationFragments::FragmentKind::Identifier);
+ if (!isa<TypeAliasDecl>(Decl))
+ Fragments.append("typedef", DeclarationFragments::FragmentKind::Keyword)
+ .appendSpace()
+ .append(getFragmentsForType(Decl->getUnderlyingType(),
+ Decl->getASTContext(), After))
+ .append(std::move(After))
+ .appendSpace()
+ .append(Decl->getName(),
+ DeclarationFragments::FragmentKind::Identifier);
+ else
+ Fragments.append("using", DeclarationFragments::FragmentKind::Keyword)
+ .appendSpace()
+ .append(Decl->getName(), DeclarationFragments::FragmentKind::Identifier)
+ .appendSpace()
+ .append("=", DeclarationFragments::FragmentKind::Text)
+ .appendSpace()
+ .append(getFragmentsForType(Decl->getUnderlyingType(),
+ Decl->getASTContext(), After))
+ .append(std::move(After));
return Fragments.appendSemicolon();
}
diff --git a/clang/test/ExtractAPI/type-alias.cpp b/clang/test/ExtractAPI/type-alias.cpp
new file mode 100644
index 0000000000000..246e8fbb92156
--- /dev/null
+++ b/clang/test/ExtractAPI/type-alias.cpp
@@ -0,0 +1,56 @@
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -extract-api --pretty-sgf --emit-sgf-symbol-labels-for-testing \
+// RUN: --product-name=TypeAlias -triple arm64-apple-macosx -x c++-header %s -o %t/type-alias.symbols.json -verify
+
+// RUN: FileCheck %s --input-file %t/type-alias.symbols.json --check-prefix MYALIAS
+using MyAlias = int;
+//MYALIAS-LABEL "!testLabel": "c:@MYALIAS"
+//MYALIAS: "accessLevel": "public",
+//MYALIAS: "declarationFragments": [
+//MYALIAS-NEXT: {
+//MYALIAS-NEXT: "kind": "keyword",
+//MYALIAS-NEXT: "spelling": "using"
+//MYALIAS-NEXT: },
+//MYALIAS-NEXT: {
+//MYALIAS-NEXT: "kind": "text",
+//MYALIAS-NEXT: "spelling": " "
+//MYALIAS-NEXT: },
+//MYALIAS-NEXT: {
+//MYALIAS-NEXT: "kind": "identifier",
+//MYALIAS-NEXT: "spelling": "MyAlias"
+//MYALIAS-NEXT: },
+//MYALIAS-NEXT: {
+//MYALIAS-NEXT: "kind": "text",
+//MYALIAS-NEXT: "spelling": " = "
+//MYALIAS-NEXT: },
+//MYALIAS-NEXT: {
+//MYALIAS-NEXT: "kind": "typeIdentifier",
+//MYALIAS-NEXT: "preciseIdentifier": "c:I",
+//MYALIAS-NEXT: "spelling": "int"
+//MYALIAS-NEXT: },
+//MYALIAS-NEXT: {
+//MYALIAS-NEXT: "kind": "text",
+//MYALIAS-NEXT: "spelling": ";"
+//MYALIAS-NEXT: }
+//MYALIAS: "kind": {
+//MYALIAS-NEXT: "displayName": "Type Alias",
+//MYALIAS-NEXT: "identifier": "c++.typealias"
+//MYALIAS: names": {
+//MYALIAS-NEXT: "navigator": [
+//MYALIAS-NEXT: {
+//MYALIAS-NEXT: "kind": "identifier",
+//MYALIAS-NEXT: "spelling": "MyAlias"
+//MYALIAS-NEXT: }
+//MYALIAS-NEXT: ],
+//MYALIAS-NEXT: "subHeading": [
+//MYALIAS-NEXT: {
+//MYALIAS-NEXT: "kind": "identifier",
+//MYALIAS-NEXT: "spelling": "MyAlias"
+//MYALIAS-NEXT: }
+//MYALIAS-NEXT: ],
+//MYALIAS-NEXT: "title": "MyAlias"
+//MYALIAS: "pathComponents": [
+//MYALIAS-NEXT: "MyAlias"
+//MYALIAS-NEXT: ]
+
+// expected-no-diagnostics
\ No newline at end of file
``````````
</details>
https://github.com/llvm/llvm-project/pull/134007
More information about the cfe-commits
mailing list