Skip to content

Commit 9dcc3bf

Browse files
evelez7daniel-grumberg
authored andcommitted
[clang][ExtractAPI] Fix bool spelling coming from the macro definition.
getFragmentsForType resulted in a bool typeIdentifier fragment to be spelled "_Bool". This fixes the spelling to be "bool" and checks it in C and C++. Reviewed By: dang Differential Revision: https://reviews.llvm.org/D158474
1 parent 2d8697d commit 9dcc3bf

File tree

4 files changed

+415
-0
lines changed

4 files changed

+415
-0
lines changed

clang/include/clang/ExtractAPI/DeclarationFragments.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ class DeclarationFragments {
171171
return *this;
172172
}
173173

174+
DeclarationFragments &replace(std::string NewSpelling, unsigned Position) {
175+
Fragments.at(Position).Spelling = NewSpelling;
176+
return *this;
177+
}
178+
174179
/// Append a text Fragment of a space character.
175180
///
176181
/// \returns a reference to the DeclarationFragments object itself after

clang/lib/ExtractAPI/DeclarationFragments.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ DeclarationFragments DeclarationFragmentsBuilder::getFragmentsForType(
433433
DeclarationFragments QualsFragments = getFragmentsForQualifiers(SQT.Quals),
434434
TypeFragments =
435435
getFragmentsForType(SQT.Ty, Context, After);
436+
if (QT.getAsString() == "_Bool")
437+
TypeFragments.replace("bool", 0);
438+
436439
if (QualsFragments.getFragments().empty())
437440
return TypeFragments;
438441

clang/test/ExtractAPI/bool.c

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
// RUN: sed -e "s@INPUT_DIR@%{/t:regex_replacement}@g" \
4+
// RUN: %t/reference.output.json.in >> %t/reference.output.json
5+
// RUN: %clang -extract-api -target arm64-apple-macosx \
6+
// RUN: %t/input.h -o %t/output.json
7+
8+
// Generator version is not consistent across test runs, normalize it.
9+
// RUN: sed -e "s@\"generator\": \".*\"@\"generator\": \"?\"@g" \
10+
// RUN: %t/output.json >> %t/output-normalized.json
11+
// RUN: diff %t/reference.output.json %t/output-normalized.json
12+
13+
//--- input.h
14+
#include <stdbool.h>
15+
bool Foo;
16+
17+
bool IsFoo(bool Bar);
18+
/// expected-no-diagnostics
19+
20+
//--- reference.output.json.in
21+
{
22+
"metadata": {
23+
"formatVersion": {
24+
"major": 0,
25+
"minor": 5,
26+
"patch": 3
27+
},
28+
"generator": "?"
29+
},
30+
"module": {
31+
"name": "",
32+
"platform": {
33+
"architecture": "arm64",
34+
"operatingSystem": {
35+
"minimumVersion": {
36+
"major": 11,
37+
"minor": 0,
38+
"patch": 0
39+
},
40+
"name": "macosx"
41+
},
42+
"vendor": "apple"
43+
}
44+
},
45+
"relationships": [],
46+
"symbols": [
47+
{
48+
"accessLevel": "public",
49+
"declarationFragments": [
50+
{
51+
"kind": "typeIdentifier",
52+
"preciseIdentifier": "c:b",
53+
"spelling": "bool"
54+
},
55+
{
56+
"kind": "text",
57+
"spelling": " "
58+
},
59+
{
60+
"kind": "identifier",
61+
"spelling": "Foo"
62+
},
63+
{
64+
"kind": "text",
65+
"spelling": ";"
66+
}
67+
],
68+
"identifier": {
69+
"interfaceLanguage": "c",
70+
"precise": "c:@Foo"
71+
},
72+
"kind": {
73+
"displayName": "Global Variable",
74+
"identifier": "c.var"
75+
},
76+
"location": {
77+
"position": {
78+
"character": 6,
79+
"line": 2
80+
},
81+
"uri": "file://INPUT_DIR/input.h"
82+
},
83+
"names": {
84+
"navigator": [
85+
{
86+
"kind": "identifier",
87+
"spelling": "Foo"
88+
}
89+
],
90+
"subHeading": [
91+
{
92+
"kind": "identifier",
93+
"spelling": "Foo"
94+
}
95+
],
96+
"title": "Foo"
97+
},
98+
"pathComponents": [
99+
"Foo"
100+
]
101+
},
102+
{
103+
"accessLevel": "public",
104+
"declarationFragments": [
105+
{
106+
"kind": "typeIdentifier",
107+
"preciseIdentifier": "c:b",
108+
"spelling": "bool"
109+
},
110+
{
111+
"kind": "text",
112+
"spelling": " "
113+
},
114+
{
115+
"kind": "identifier",
116+
"spelling": "IsFoo"
117+
},
118+
{
119+
"kind": "text",
120+
"spelling": "("
121+
},
122+
{
123+
"kind": "typeIdentifier",
124+
"preciseIdentifier": "c:b",
125+
"spelling": "bool"
126+
},
127+
{
128+
"kind": "text",
129+
"spelling": " "
130+
},
131+
{
132+
"kind": "internalParam",
133+
"spelling": "Bar"
134+
},
135+
{
136+
"kind": "text",
137+
"spelling": ");"
138+
}
139+
],
140+
"functionSignature": {
141+
"parameters": [
142+
{
143+
"declarationFragments": [
144+
{
145+
"kind": "typeIdentifier",
146+
"preciseIdentifier": "c:b",
147+
"spelling": "bool"
148+
},
149+
{
150+
"kind": "text",
151+
"spelling": " "
152+
},
153+
{
154+
"kind": "internalParam",
155+
"spelling": "Bar"
156+
}
157+
],
158+
"name": "Bar"
159+
}
160+
],
161+
"returns": [
162+
{
163+
"kind": "typeIdentifier",
164+
"preciseIdentifier": "c:b",
165+
"spelling": "bool"
166+
}
167+
]
168+
},
169+
"identifier": {
170+
"interfaceLanguage": "c",
171+
"precise": "c:@F@IsFoo"
172+
},
173+
"kind": {
174+
"displayName": "Function",
175+
"identifier": "c.func"
176+
},
177+
"location": {
178+
"position": {
179+
"character": 6,
180+
"line": 4
181+
},
182+
"uri": "file://INPUT_DIR/input.h"
183+
},
184+
"names": {
185+
"navigator": [
186+
{
187+
"kind": "identifier",
188+
"spelling": "IsFoo"
189+
}
190+
],
191+
"subHeading": [
192+
{
193+
"kind": "identifier",
194+
"spelling": "IsFoo"
195+
}
196+
],
197+
"title": "IsFoo"
198+
},
199+
"pathComponents": [
200+
"IsFoo"
201+
]
202+
}
203+
]
204+
}

0 commit comments

Comments
 (0)