Skip to content

Commit

Permalink
[clang-doc] Always emit the TagType for RecordInfo
Browse files Browse the repository at this point in the history
Always emit the TagType for RecordInfo in YAML output. Previously this omitted the type for "struct", considering it the default. But records in C++ don't really have a default type so always emitting this is more clear.

Emit IsTypeDef in YAML. Previously this existed only in the Representation but was never written. Additionally, adds IsTypeDef to the record merge operation which was clearing it (all RecordInfo structures are merged with am empty RecordInfo during the reduce phase).

Reviewed By: paulkirth

Differential Revision: https://reviews.llvm.org/D131739
  • Loading branch information
brettw authored and ilovepi committed Aug 12, 2022
1 parent 75c7e79 commit 6826682
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions clang-tools-extra/clang-doc/Representation.cpp
Expand Up @@ -222,6 +222,7 @@ void RecordInfo::merge(RecordInfo &&Other) {
assert(mergeable(Other));
if (!TagType)
TagType = Other.TagType;
IsTypeDef = IsTypeDef || Other.IsTypeDef;
if (Members.empty())
Members = std::move(Other.Members);
if (Bases.empty())
Expand Down
13 changes: 9 additions & 4 deletions clang-tools-extra/clang-doc/Representation.h
Expand Up @@ -348,10 +348,15 @@ struct RecordInfo : public SymbolInfo {

void merge(RecordInfo &&I);

TagTypeKind TagType = TagTypeKind::TTK_Struct; // Type of this record
// (struct, class, union,
// interface).
bool IsTypeDef = false; // Indicates if record was declared using typedef
// Type of this record (struct, class, union, interface).
TagTypeKind TagType = TagTypeKind::TTK_Struct;

// Indicates if the record was declared using a typedef. Things like anonymous
// structs in a typedef:
// typedef struct { ... } foo_t;
// are converted into records with the typedef as the Name + this flag set.
bool IsTypeDef = false;

llvm::SmallVector<MemberTypeInfo, 4>
Members; // List of info about record members.
llvm::SmallVector<Reference, 4> Parents; // List of base/parent records
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/clang-doc/YAMLGenerator.cpp
Expand Up @@ -127,7 +127,8 @@ static void SymbolInfoMapping(IO &IO, SymbolInfo &I) {

static void RecordInfoMapping(IO &IO, RecordInfo &I) {
SymbolInfoMapping(IO, I);
IO.mapOptional("TagType", I.TagType, clang::TagTypeKind::TTK_Struct);
IO.mapOptional("TagType", I.TagType);
IO.mapOptional("IsTypeDef", I.IsTypeDef, false);
IO.mapOptional("Members", I.Members);
IO.mapOptional("Bases", I.Bases);
IO.mapOptional("Parents", I.Parents, llvm::SmallVector<Reference, 4>());
Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/unittests/clang-doc/MergeTest.cpp
Expand Up @@ -78,6 +78,7 @@ TEST(MergeTest, mergeNamespaceInfos) {
TEST(MergeTest, mergeRecordInfos) {
RecordInfo One;
One.Name = "r";
One.IsTypeDef = true;
One.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);

One.DefLoc = Location(10, llvm::SmallString<16>{"test.cpp"});
Expand Down Expand Up @@ -119,6 +120,7 @@ TEST(MergeTest, mergeRecordInfos) {

auto Expected = std::make_unique<RecordInfo>();
Expected->Name = "r";
Expected->IsTypeDef = true;
Expected->Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);

Expected->DefLoc = Location(10, llvm::SmallString<16>{"test.cpp"});
Expand Down
3 changes: 3 additions & 0 deletions clang-tools-extra/unittests/clang-doc/YAMLGeneratorTest.cpp
Expand Up @@ -76,6 +76,7 @@ TEST(YAMLGeneratorTest, emitRecordYAML) {
RecordInfo I;
I.Name = "r";
I.Path = "path/to/A";
I.IsTypeDef = true;
I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);

I.DefLoc = Location(10, llvm::SmallString<16>{"test.cpp"});
Expand Down Expand Up @@ -136,6 +137,7 @@ Path: 'path/to/A'
- LineNumber: 12
Filename: 'test.cpp'
TagType: Class
IsTypeDef: true
Members:
- Type:
Name: 'int'
Expand All @@ -154,6 +156,7 @@ TagType: Class
- USR: '0000000000000000000000000000000000000000'
Name: 'F'
Path: 'path/to/F'
TagType: Struct
Members:
- Type:
Name: 'int'
Expand Down

0 comments on commit 6826682

Please sign in to comment.