Skip to content

Commit 949177d

Browse files
[clang][ExtractAPI] Fix up casting from CXXClassRecord (#110983)
`RecordRecord::classOfKind` and `TagRecord::classofKind` didn't correctly capture `RK_CXXClass` and derived variants, e.g. `RK_ClassTemplate`. This materialized by anonymous C++ tag types not being correctly detected when they need to be merged with another record.
1 parent 5059059 commit 949177d

File tree

3 files changed

+82
-26
lines changed

3 files changed

+82
-26
lines changed

clang/include/clang/ExtractAPI/API.h

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/ADT/SmallVector.h"
2727
#include "llvm/Support/Allocator.h"
2828
#include "llvm/Support/Casting.h"
29+
#include "llvm/Support/Compiler.h"
2930
#include "llvm/TargetParser/Triple.h"
3031
#include <cstddef>
3132
#include <iterator>
@@ -615,7 +616,24 @@ struct TagRecord : APIRecord, RecordContext {
615616
return classofKind(Record->getKind());
616617
}
617618
static bool classofKind(RecordKind K) {
618-
return K == RK_Struct || K == RK_Union || K == RK_Enum;
619+
switch (K) {
620+
case RK_Enum:
621+
LLVM_FALLTHROUGH;
622+
case RK_Struct:
623+
LLVM_FALLTHROUGH;
624+
case RK_Union:
625+
LLVM_FALLTHROUGH;
626+
case RK_CXXClass:
627+
LLVM_FALLTHROUGH;
628+
case RK_ClassTemplate:
629+
LLVM_FALLTHROUGH;
630+
case RK_ClassTemplateSpecialization:
631+
LLVM_FALLTHROUGH;
632+
case RK_ClassTemplatePartialSpecialization:
633+
return true;
634+
default:
635+
return false;
636+
}
619637
}
620638

621639
bool IsEmbeddedInVarDeclarator;
@@ -684,7 +702,22 @@ struct RecordRecord : TagRecord {
684702
return classofKind(Record->getKind());
685703
}
686704
static bool classofKind(RecordKind K) {
687-
return K == RK_Struct || K == RK_Union;
705+
switch (K) {
706+
case RK_Struct:
707+
LLVM_FALLTHROUGH;
708+
case RK_Union:
709+
LLVM_FALLTHROUGH;
710+
case RK_CXXClass:
711+
LLVM_FALLTHROUGH;
712+
case RK_ClassTemplate:
713+
LLVM_FALLTHROUGH;
714+
case RK_ClassTemplateSpecialization:
715+
LLVM_FALLTHROUGH;
716+
case RK_ClassTemplatePartialSpecialization:
717+
return true;
718+
default:
719+
return false;
720+
}
688721
}
689722

690723
bool isAnonymousWithNoTypedef() { return Name.empty(); }

clang/test/ExtractAPI/anonymous_record_no_typedef.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
// RUN: rm -rf %t
22
// RUN: %clang_cc1 -extract-api --pretty-sgf --emit-sgf-symbol-labels-for-testing \
33
// RUN: -triple arm64-apple-macosx -isystem %S -fretain-comments-from-system-headers \
4-
// RUN: -x c-header %s -o %t/output.symbols.json -verify
4+
// RUN: -x c-header %s -o %t/output-c.symbols.json -verify
5+
//
6+
// RUN: %clang_cc1 -extract-api --pretty-sgf --emit-sgf-symbol-labels-for-testing \
7+
// RUN: -triple arm64-apple-macosx -isystem %S -fretain-comments-from-system-headers \
8+
// RUN: -x c++-header %s -o %t/output-cxx.symbols.json -verify
59

6-
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix GLOBAL
7-
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix PREFIX
8-
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix CONTENT
10+
// RUN: FileCheck %s --input-file %t/output-c.symbols.json --check-prefix GLOBAL
11+
// RUN: FileCheck %s --input-file %t/output-c.symbols.json --check-prefix PREFIX
12+
// RUN: FileCheck %s --input-file %t/output-c.symbols.json --check-prefix CONTENT
13+
// RUN: FileCheck %s --input-file %t/output-cxx.symbols.json --check-prefix GLOBAL
14+
// RUN: FileCheck %s --input-file %t/output-cxx.symbols.json --check-prefix PREFIX
15+
// RUN: FileCheck %s --input-file %t/output-cxx.symbols.json --check-prefix CONTENT
916
/// A global variable with an anonymous struct type.
1017
struct { char *prefix; char *content; } global;
1118
// GLOBAL-LABEL: "!testLabel": "c:@global"
@@ -30,7 +37,7 @@ struct { char *prefix; char *content; } global;
3037
// GLOBAL: "text": "A global variable with an anonymous struct type."
3138
// GLOBAL: "kind": {
3239
// GLOBAL-NEXT: "displayName": "Global Variable",
33-
// GLOBAL-NEXT: "identifier": "c.var"
40+
// GLOBAL-NEXT: "identifier": "c{{(\+\+)?}}.var"
3441
// GLOBAL: "title": "global"
3542
// GLOBAL: "pathComponents": [
3643
// GLOBAL-NEXT: "global"
@@ -54,9 +61,12 @@ struct { char *prefix; char *content; } global;
5461

5562
/// A Vehicle
5663
struct Vehicle {
57-
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix TYPE
58-
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix BICYCLE
59-
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix CAR
64+
// RUN: FileCheck %s --input-file %t/output-c.symbols.json --check-prefix TYPE
65+
// RUN: FileCheck %s --input-file %t/output-c.symbols.json --check-prefix BICYCLE
66+
// RUN: FileCheck %s --input-file %t/output-c.symbols.json --check-prefix CAR
67+
// RUN: FileCheck %s --input-file %t/output-cxx.symbols.json --check-prefix TYPE
68+
// RUN: FileCheck %s --input-file %t/output-cxx.symbols.json --check-prefix BICYCLE
69+
// RUN: FileCheck %s --input-file %t/output-cxx.symbols.json --check-prefix CAR
6070
/// The type of vehicle.
6171
enum {
6272
Bicycle,
@@ -96,9 +106,12 @@ struct Vehicle {
96106
// CAR-NEXT: "Car"
97107
// CAR-NEXT: ]
98108

99-
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix INFORMATION
100-
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix WHEELS
101-
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix NAME
109+
// RUN: FileCheck %s --input-file %t/output-c.symbols.json --check-prefix INFORMATION
110+
// RUN: FileCheck %s --input-file %t/output-c.symbols.json --check-prefix WHEELS
111+
// RUN: FileCheck %s --input-file %t/output-c.symbols.json --check-prefix NAME
112+
// RUN: FileCheck %s --input-file %t/output-cxx.symbols.json --check-prefix INFORMATION
113+
// RUN: FileCheck %s --input-file %t/output-cxx.symbols.json --check-prefix WHEELS
114+
// RUN: FileCheck %s --input-file %t/output-cxx.symbols.json --check-prefix NAME
102115
/// The information about the vehicle.
103116
union {
104117
int wheels;
@@ -145,8 +158,10 @@ struct Vehicle {
145158
// NAME-NEXT: ]
146159
};
147160

148-
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix GLOBALCASE
149-
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix GLOBALOTHERCASE
161+
// RUN: FileCheck %s --input-file %t/output-c.symbols.json --check-prefix GLOBALCASE
162+
// RUN: FileCheck %s --input-file %t/output-c.symbols.json --check-prefix GLOBALOTHERCASE
163+
// RUN: FileCheck %s --input-file %t/output-cxx.symbols.json --check-prefix GLOBALCASE
164+
// RUN: FileCheck %s --input-file %t/output-cxx.symbols.json --check-prefix GLOBALOTHERCASE
150165
enum {
151166
GlobalCase,
152167
GlobalOtherCase
@@ -163,7 +178,8 @@ enum {
163178
// GLOBALOTHERCASE-NEXT: "GlobalOtherCase"
164179
// GLOBALOTHERCASE-NEXT: ]
165180

166-
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix VEC
181+
// RUN: FileCheck %s --input-file %t/output-c.symbols.json --check-prefix VEC
182+
// RUN: FileCheck %s --input-file %t/output-cxx.symbols.json --check-prefix VEC
167183
union Vector {
168184
struct {
169185
float X;

clang/test/ExtractAPI/typedef_anonymous_record.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// RUN: rm -rf %t
22
// RUN: %clang_cc1 -extract-api --pretty-sgf --emit-sgf-symbol-labels-for-testing \
3-
// RUN: --product-name=TypedefChain -triple arm64-apple-macosx -x c-header %s -o %t/typedefchain.symbols.json -verify
3+
// RUN: --product-name=TypedefChain -triple arm64-apple-macosx -x c-header %s -o %t/typedefchain-c.symbols.json -verify
4+
// RUN: %clang_cc1 -extract-api --pretty-sgf --emit-sgf-symbol-labels-for-testing \
5+
// RUN: --product-name=TypedefChain -triple arm64-apple-macosx -x c++-header %s -o %t/typedefchain-cxx.symbols.json -verify
46

5-
// RUN: FileCheck %s --input-file %t/typedefchain.symbols.json --check-prefix MYSTRUCT
7+
// RUN: FileCheck %s --input-file %t/typedefchain-c.symbols.json --check-prefix MYSTRUCT
8+
// RUN: FileCheck %s --input-file %t/typedefchain-cxx.symbols.json --check-prefix MYSTRUCT
69
typedef struct { } MyStruct;
710
// MYSTRUCT-LABEL: "!testLabel": "c:@SA@MyStruct"
811
// MYSTRUCT: "accessLevel": "public",
@@ -34,7 +37,7 @@ typedef struct { } MyStruct;
3437
// MYSTRUCT-NEXT: ]
3538
// MYSTRUCT: "kind": {
3639
// MYSTRUCT-NEXT: "displayName": "Structure",
37-
// MYSTRUCT-NEXT: "identifier": "c.struct"
40+
// MYSTRUCT-NEXT: "identifier": "c{{(\+\+)?}}.struct"
3841
// MYSTRUCT: "names": {
3942
// MYSTRUCT-NEXT: "navigator": [
4043
// MYSTRUCT-NEXT: {
@@ -54,7 +57,8 @@ typedef struct { } MyStruct;
5457
// MYSTRUCT-NEXT: "MyStruct"
5558
// MYSTRUCT-NEXT: ]
5659

57-
// RUN: FileCheck %s --input-file %t/typedefchain.symbols.json --check-prefix MYSTRUCTSTRUCT
60+
// RUN: FileCheck %s --input-file %t/typedefchain-c.symbols.json --check-prefix MYSTRUCTSTRUCT
61+
// RUN: FileCheck %s --input-file %t/typedefchain-cxx.symbols.json --check-prefix MYSTRUCTSTRUCT
5862
typedef MyStruct MyStructStruct;
5963
// MYSTRUCTSTRUCT-LABEL: "!testLabel": "c:typedef_anonymous_record.c@T@MyStructStruct"
6064
// MYSTRUCTSTRUCT: "accessLevel": "public",
@@ -87,10 +91,12 @@ typedef MyStruct MyStructStruct;
8791
// MYSTRUCTSTRUCT-NEXT:],
8892
// MYSTRUCTSTRUCT: "kind": {
8993
// MYSTRUCTSTRUCT-NEXT: "displayName": "Type Alias",
90-
// MYSTRUCTSTRUCT-NEXT: "identifier": "c.typealias"
94+
// MYSTRUCTSTRUCT-NEXT: "identifier": "c{{(\+\+)?}}.typealias"
9195

92-
// RUN: FileCheck %s --input-file %t/typedefchain.symbols.json --check-prefix MYENUM
93-
// RUN: FileCheck %s --input-file %t/typedefchain.symbols.json --check-prefix CASE
96+
// RUN: FileCheck %s --input-file %t/typedefchain-c.symbols.json --check-prefix MYENUM
97+
// RUN: FileCheck %s --input-file %t/typedefchain-c.symbols.json --check-prefix CASE
98+
// RUN: FileCheck %s --input-file %t/typedefchain-cxx.symbols.json --check-prefix MYENUM
99+
// RUN: FileCheck %s --input-file %t/typedefchain-cxx.symbols.json --check-prefix CASE
94100
typedef enum { Case } MyEnum;
95101
// MYENUM: "source": "c:@EA@MyEnum@Case",
96102
// MYENUM-NEXT: "target": "c:@EA@MyEnum",
@@ -124,7 +130,7 @@ typedef enum { Case } MyEnum;
124130
// MYENUM-NEXT:],
125131
// MYENUM: "kind": {
126132
// MYENUM-NEXT: "displayName": "Enumeration",
127-
// MYENUM-NEXT: "identifier": "c.enum"
133+
// MYENUM-NEXT: "identifier": "c{{(\+\+)?}}.enum"
128134
// MYENUM: "names": {
129135
// MYENUM-NEXT: "navigator": [
130136
// MYENUM-NEXT: {
@@ -147,7 +153,8 @@ typedef enum { Case } MyEnum;
147153
// CASE-NEXT: "Case"
148154
// CASE-NEXT: ]
149155

150-
// RUN: FileCheck %s --input-file %t/typedefchain.symbols.json --check-prefix MYENUMENUM
156+
// RUN: FileCheck %s --input-file %t/typedefchain-c.symbols.json --check-prefix MYENUMENUM
157+
// RUN: FileCheck %s --input-file %t/typedefchain-cxx.symbols.json --check-prefix MYENUMENUM
151158
typedef MyEnum MyEnumEnum;
152159
// MYENUMENUM-LABEL: "!testLabel": "c:typedef_anonymous_record.c@T@MyEnumEnum"
153160
// MYENUMENUM: "declarationFragments": [
@@ -179,7 +186,7 @@ typedef MyEnum MyEnumEnum;
179186
// MYENUMENUM-NEXT: ],
180187
// MYENUMENUM: "kind": {
181188
// MYENUMENUM-NEXT: "displayName": "Type Alias",
182-
// MYENUMENUM-NEXT: "identifier": "c.typealias"
189+
// MYENUMENUM-NEXT: "identifier": "c{{(\+\+)?}}.typealias"
183190
// MYENUMENUM-NEXT: },
184191
// MYENUMENUM: "title": "MyEnumEnum"
185192

0 commit comments

Comments
 (0)