Skip to content

Commit 2b932bc

Browse files
authored
[clang-doc] Use LangOpts when printing types (#120308)
The implementation in the clang-doc serializer failed to take in the LangOpts from the declaration. As a result, we'd do things like print `_Bool` instead of `bool`, even in C++ code. Fixes #62970
1 parent 72e58e0 commit 2b932bc

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

clang-tools-extra/clang-doc/Serialize.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ static RecordDecl *getRecordDeclForType(const QualType &T) {
236236
return nullptr;
237237
}
238238

239-
TypeInfo getTypeInfoForType(const QualType &T) {
239+
TypeInfo getTypeInfoForType(const QualType &T, const PrintingPolicy &Policy) {
240240
const TagDecl *TD = getTagDeclForType(T);
241241
if (!TD)
242-
return TypeInfo(Reference(SymbolID(), T.getAsString()));
242+
return TypeInfo(Reference(SymbolID(), T.getAsString(Policy)));
243243

244244
InfoType IT;
245245
if (dyn_cast<EnumDecl>(TD)) {
@@ -250,7 +250,7 @@ TypeInfo getTypeInfoForType(const QualType &T) {
250250
IT = InfoType::IT_default;
251251
}
252252
return TypeInfo(Reference(getUSRForDecl(TD), TD->getNameAsString(), IT,
253-
T.getAsString(), getInfoRelativePath(TD)));
253+
T.getAsString(Policy), getInfoRelativePath(TD)));
254254
}
255255

256256
static bool isPublic(const clang::AccessSpecifier AS,
@@ -379,10 +379,11 @@ static void parseFields(RecordInfo &I, const RecordDecl *D, bool PublicOnly,
379379
if (!shouldSerializeInfo(PublicOnly, /*IsInAnonymousNamespace=*/false, F))
380380
continue;
381381

382+
auto &LO = F->getLangOpts();
382383
// Use getAccessUnsafe so that we just get the default AS_none if it's not
383384
// valid, as opposed to an assert.
384385
MemberTypeInfo &NewMember = I.Members.emplace_back(
385-
getTypeInfoForType(F->getTypeSourceInfo()->getType()),
386+
getTypeInfoForType(F->getTypeSourceInfo()->getType(), LO),
386387
F->getNameAsString(),
387388
getFinalAccessSpecifier(Access, F->getAccessUnsafe()));
388389
populateMemberTypeInfo(NewMember, F);
@@ -412,9 +413,10 @@ static void parseEnumerators(EnumInfo &I, const EnumDecl *D) {
412413
}
413414

414415
static void parseParameters(FunctionInfo &I, const FunctionDecl *D) {
416+
auto &LO = D->getLangOpts();
415417
for (const ParmVarDecl *P : D->parameters()) {
416418
FieldTypeInfo &FieldInfo = I.Params.emplace_back(
417-
getTypeInfoForType(P->getOriginalType()), P->getNameAsString());
419+
getTypeInfoForType(P->getOriginalType(), LO), P->getNameAsString());
418420
FieldInfo.DefaultValue = getSourceCode(D, P->getDefaultArgRange());
419421
}
420422
}
@@ -541,7 +543,8 @@ static void populateFunctionInfo(FunctionInfo &I, const FunctionDecl *D,
541543
bool &IsInAnonymousNamespace) {
542544
populateSymbolInfo(I, D, FC, LineNumber, Filename, IsFileInRootDir,
543545
IsInAnonymousNamespace);
544-
I.ReturnType = getTypeInfoForType(D->getReturnType());
546+
auto &LO = D->getLangOpts();
547+
I.ReturnType = getTypeInfoForType(D->getReturnType(), LO);
545548
parseParameters(I, D);
546549

547550
PopulateTemplateParameters(I.Template, D);
@@ -783,7 +786,8 @@ emitInfo(const TypedefDecl *D, const FullComment *FC, int LineNumber,
783786
return {};
784787

785788
Info.DefLoc.emplace(LineNumber, File, IsFileInRootDir);
786-
Info.Underlying = getTypeInfoForType(D->getUnderlyingType());
789+
auto &LO = D->getLangOpts();
790+
Info.Underlying = getTypeInfoForType(D->getUnderlyingType(), LO);
787791
if (Info.Underlying.Type.Name.empty()) {
788792
// Typedef for an unnamed type. This is like "typedef struct { } Foo;"
789793
// The record serializer explicitly checks for this syntax and constructs
@@ -809,7 +813,8 @@ emitInfo(const TypeAliasDecl *D, const FullComment *FC, int LineNumber,
809813
return {};
810814

811815
Info.DefLoc.emplace(LineNumber, File, IsFileInRootDir);
812-
Info.Underlying = getTypeInfoForType(D->getUnderlyingType());
816+
auto &LO = D->getLangOpts();
817+
Info.Underlying = getTypeInfoForType(D->getUnderlyingType(), LO);
813818
Info.IsUsing = true;
814819

815820
// Info is wrapped in its parent scope so is returned in the second position.

clang-tools-extra/test/clang-doc/builtin_types.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ extern bool b();
2323
// YAML-NEXT: Filename: '{{.*}}'
2424
// YAML-NEXT: ReturnType:
2525
// YAML-NEXT: Type:
26-
// YAML-NEXT: Name: '_Bool'
27-
// YAML-NEXT: QualName: '_Bool'
26+
// YAML-NEXT: Name: 'bool'
27+
// YAML-NEXT: QualName: 'bool'
2828

2929
// MD: ### b
30-
// MD: *_Bool b()*
30+
// MD: *bool b()*
3131

3232
char c();
3333

clang-tools-extra/test/clang-doc/templates.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ void function<bool, 0>(bool x) {}
8080
// YAML-NEXT: Filename: '{{.*}}'
8181
// YAML-NEXT: Params:
8282
// YAML-NEXT: - Type:
83-
// YAML-NEXT: Name: '_Bool'
84-
// YAML-NEXT: QualName: '_Bool'
83+
// YAML-NEXT: Name: 'bool'
84+
// YAML-NEXT: QualName: 'bool'
8585
// YAML-NEXT: Name: 'x'
8686
// YAML-NEXT: ReturnType:
8787
// YAML-NEXT: Type:
@@ -95,7 +95,7 @@ void function<bool, 0>(bool x) {}
9595
// YAML-NEXT: - Contents: '0'
9696

9797
// MD: ### function
98-
// MD: *void function(_Bool x)*
98+
// MD: *void function(bool x)*
9999
// MD: *Defined at {{.*}}templates.cpp#[[# @LINE - 26]]*
100100

101101
/// A Tuple type
@@ -136,21 +136,21 @@ tuple<int,int,bool> func_with_tuple_param(tuple<int,int,bool> t){ return t;}
136136
// YAML-NEXT: - Type:
137137
// YAML-NEXT: Type: Record
138138
// YAML-NEXT: Name: 'tuple'
139-
// YAML-NEXT: QualName: 'tuple<int, int, _Bool>'
139+
// YAML-NEXT: QualName: 'tuple<int, int, bool>'
140140
// YAML-NEXT: USR: '{{([0-9A-F]{40})}}'
141141
// YAML-NEXT: Path: 'GlobalNamespace'
142142
// YAML-NEXT: Name: 't'
143143
// YAML-NEXT: ReturnType:
144144
// YAML-NEXT: Type:
145145
// YAML-NEXT: Type: Record
146146
// YAML-NEXT: Name: 'tuple'
147-
// YAML-NEXT: QualName: 'tuple<int, int, _Bool>'
147+
// YAML-NEXT: QualName: 'tuple<int, int, bool>'
148148
// YAML-NEXT: USR: '{{([0-9A-F]{40})}}'
149149
// YAML-NEXT: Path: 'GlobalNamespace'
150150
// YAML-NEXT: ...
151151

152152
// MD: ### func_with_tuple_param
153-
// MD: *tuple<int, int, _Bool> func_with_tuple_param(tuple<int, int, _Bool> t)*
153+
// MD: *tuple<int, int, bool> func_with_tuple_param(tuple<int, int, bool> t)*
154154
// MD: *Defined at {{.*}}templates.cpp#[[# @LINE - 44]]*
155155
// MD: A function with a tuple parameter
156156
// MD: **t** The input to func_with_tuple_param

clang-tools-extra/unittests/clang-doc/SerializeTest.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,8 @@ TEST(SerializeTests, emitTypedefs) {
631631
TEST(SerializeTests, emitFunctionTemplate) {
632632
EmittedInfoList Infos;
633633
// A template and a specialization.
634-
ExtractInfosFromCode("template<typename T = int> void GetFoo(T);\n"
635-
"template<> void GetFoo<bool>(bool);",
634+
ExtractInfosFromCode("template<typename T = int> bool GetFoo(T);\n"
635+
"template<> bool GetFoo<bool>(bool);",
636636
2,
637637
/*Public=*/false, Infos);
638638

@@ -666,6 +666,8 @@ TEST(SerializeTests, emitFunctionTemplate) {
666666
ASSERT_EQ(1u, Func2.Template->Specialization->Params.size());
667667
EXPECT_EQ("bool", Func2.Template->Specialization->Params[0].Contents);
668668
EXPECT_EQ(Func1.USR, Func2.Template->Specialization->SpecializationOf);
669+
670+
EXPECT_EQ("bool", Func2.ReturnType.Type.Name);
669671
}
670672

671673
TEST(SerializeTests, emitClassTemplate) {

0 commit comments

Comments
 (0)