[clang] [clang][ExtractAPI] emit correct spelling for type aliases (PR #134007)
Erick Velez via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 1 21:55:04 PDT 2025
https://github.com/evelez7 updated https://github.com/llvm/llvm-project/pull/134007
>From 28879073b9d59dffa13f9523b4c9ec677b3a7b9c Mon Sep 17 00:00:00 2001
From: Erick Velez <erickvelez7 at gmail.com>
Date: Tue, 1 Apr 2025 16:20:44 -0700
Subject: [PATCH 1/2] [clang][ExtractAPI] emit correct spelling for type
aliases
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.
---
clang/lib/ExtractAPI/DeclarationFragments.cpp | 26 ++++++---
clang/test/ExtractAPI/type-alias.cpp | 56 +++++++++++++++++++
2 files changed, 75 insertions(+), 7 deletions(-)
create mode 100644 clang/test/ExtractAPI/type-alias.cpp
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
>From 61e7db6a233258e538c7ef6cb9fb9077d8bec4f4 Mon Sep 17 00:00:00 2001
From: Erick Velez <erickvelez7 at gmail.com>
Date: Tue, 1 Apr 2025 21:54:51 -0700
Subject: [PATCH 2/2] add newline to test
---
clang/test/ExtractAPI/type-alias.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/ExtractAPI/type-alias.cpp b/clang/test/ExtractAPI/type-alias.cpp
index 246e8fbb92156..ff0dca8bb3bf5 100644
--- a/clang/test/ExtractAPI/type-alias.cpp
+++ b/clang/test/ExtractAPI/type-alias.cpp
@@ -53,4 +53,4 @@ using MyAlias = int;
//MYALIAS-NEXT: "MyAlias"
//MYALIAS-NEXT: ]
-// expected-no-diagnostics
\ No newline at end of file
+// expected-no-diagnostics
More information about the cfe-commits
mailing list