Skip to content

Commit 7eb4bfe

Browse files
snprajwaldyung
authored andcommitted
[ExtractAPI] Format typedef params correctly (llvm#171516)
Typically, pointer types are formatted in a way where the identifier comes right after the type definition without a space separating them, e.g. `int *foo`, where the type is `int *` and the identifier is `foo`. However, if a type alias to a pointer type is used, the emitted declaration fragments are incorrect due to the missing space between the type and identifier, like in the below example: ``` typedef int *T; // The declaration fragment contains `Tbar` instead of `T bar` void foo(T bar); ``` This patch checks if pointer types are aliased, and inserts the space correctly if so. rdar://132022003 (cherry picked from commit 794218b)
1 parent 51e5074 commit 7eb4bfe

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

clang/lib/ExtractAPI/DeclarationFragments.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,10 @@ DeclarationFragmentsBuilder::getFragmentsForParam(const ParmVarDecl *Param) {
636636
DeclarationFragments::FragmentKind::InternalParam);
637637
} else {
638638
Fragments.append(std::move(TypeFragments));
639-
if (!T->isAnyPointerType() && !T->isBlockPointerType())
639+
// If the type is a type alias, append the space
640+
// even if the underlying type is a pointer type.
641+
if (T->isTypedefNameType() ||
642+
(!T->isAnyPointerType() && !T->isBlockPointerType()))
640643
Fragments.appendSpace();
641644
Fragments
642645
.append(Param->getName(),

clang/test/ExtractAPI/typedef.c

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: rm -rf %t
2-
// RUN: %clang_cc1 -extract-api --pretty-sgf --emit-sgf-symbol-labels-for-testing \
2+
// RUN: %clang_cc1 -extract-api --pretty-sgf --emit-sgf-symbol-labels-for-testing -fblocks \
33
// RUN: -triple arm64-apple-macosx -x objective-c-header %s -o %t/output.symbols.json -verify
44

55
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix MYINT
@@ -90,4 +90,83 @@ void foo(BarPtr value);
9090
void baz(BarPtr *value);
9191
// CHECK-NOT: struct Bar *
9292

93+
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix BLOCKPTR
94+
typedef int (^CustomType)(const unsigned int *, unsigned long);
95+
void bar(CustomType block);
96+
97+
// BLOCKPTR-LABEL: "!testLabel": "c:@F@bar",
98+
// BLOCKPTR: "declarationFragments": [
99+
// BLOCKPTR-NEXT: {
100+
// BLOCKPTR-NEXT: "kind": "typeIdentifier",
101+
// BLOCKPTR-NEXT: "preciseIdentifier": "c:v",
102+
// BLOCKPTR-NEXT: "spelling": "void"
103+
// BLOCKPTR-NEXT: },
104+
// BLOCKPTR-NEXT: {
105+
// BLOCKPTR-NEXT: "kind": "text",
106+
// BLOCKPTR-NEXT: "spelling": " "
107+
// BLOCKPTR-NEXT: },
108+
// BLOCKPTR-NEXT: {
109+
// BLOCKPTR-NEXT: "kind": "identifier",
110+
// BLOCKPTR-NEXT: "spelling": "bar"
111+
// BLOCKPTR-NEXT: },
112+
// BLOCKPTR-NEXT: {
113+
// BLOCKPTR-NEXT: "kind": "text",
114+
// BLOCKPTR-NEXT: "spelling": "("
115+
// BLOCKPTR-NEXT: },
116+
// BLOCKPTR-NEXT: {
117+
// BLOCKPTR-NEXT: "kind": "typeIdentifier",
118+
// BLOCKPTR-NEXT: "preciseIdentifier": "c:typedef.c@T@CustomType",
119+
// BLOCKPTR-NEXT: "spelling": "CustomType"
120+
// BLOCKPTR-NEXT: },
121+
// BLOCKPTR-NEXT: {
122+
// BLOCKPTR-NEXT: "kind": "text",
123+
// BLOCKPTR-NEXT: "spelling": " "
124+
// BLOCKPTR-NEXT: },
125+
// BLOCKPTR-NEXT: {
126+
// BLOCKPTR-NEXT: "kind": "internalParam",
127+
// BLOCKPTR-NEXT: "spelling": "block"
128+
// BLOCKPTR-NEXT: },
129+
// BLOCKPTR-NEXT: {
130+
// BLOCKPTR-NEXT: "kind": "text",
131+
// BLOCKPTR-NEXT: "spelling": ");"
132+
// BLOCKPTR-NEXT: }
133+
// BLOCKPTR-NEXT: ],
134+
// BLOCKPTR-NEXT: "functionSignature": {
135+
// BLOCKPTR-NEXT: "parameters": [
136+
// BLOCKPTR-NEXT: {
137+
// BLOCKPTR-NEXT: "declarationFragments": [
138+
// BLOCKPTR-NEXT: {
139+
// BLOCKPTR-NEXT: "kind": "typeIdentifier",
140+
// BLOCKPTR-NEXT: "preciseIdentifier": "c:typedef.c@T@CustomType",
141+
// BLOCKPTR-NEXT: "spelling": "CustomType"
142+
// BLOCKPTR-NEXT: },
143+
// BLOCKPTR-NEXT: {
144+
// BLOCKPTR-NEXT: "kind": "text",
145+
// BLOCKPTR-NEXT: "spelling": " "
146+
// BLOCKPTR-NEXT: },
147+
// BLOCKPTR-NEXT: {
148+
// BLOCKPTR-NEXT: "kind": "internalParam",
149+
// BLOCKPTR-NEXT: "spelling": "block"
150+
// BLOCKPTR-NEXT: }
151+
// BLOCKPTR-NEXT: ],
152+
// BLOCKPTR-NEXT: "name": "block"
153+
// BLOCKPTR-NEXT: }
154+
// BLOCKPTR-NEXT: ],
155+
// BLOCKPTR-NEXT: "returns": [
156+
// BLOCKPTR-NEXT: {
157+
// BLOCKPTR-NEXT: "kind": "typeIdentifier",
158+
// BLOCKPTR-NEXT: "preciseIdentifier": "c:v",
159+
// BLOCKPTR-NEXT: "spelling": "void"
160+
// BLOCKPTR-NEXT: }
161+
// BLOCKPTR-NEXT: ]
162+
// BLOCKPTR-NEXT: },
163+
// BLOCKPTR: "identifier": {
164+
// BLOCKPTR-NEXT: "interfaceLanguage": "objective-c",
165+
// BLOCKPTR-NEXT: "precise": "c:@F@bar"
166+
// BLOCKPTR-NEXT: },
167+
// BLOCKPTR: "kind": {
168+
// BLOCKPTR-NEXT: "displayName": "Function",
169+
// BLOCKPTR-NEXT: "identifier": "objective-c.func"
170+
// BLOCKPTR-NEXT: },
171+
93172
// expected-no-diagnostics

0 commit comments

Comments
 (0)