Skip to content

Commit 25a5d78

Browse files
committed
Cxx: add "initialized" role to "member" kind
struct opt file_ops { .read = file_read, .write = file_write, }; The parser extracts "read" and "write" with "initialized" role of "member" kind. Signed-off-by: Masatake YAMATO <[email protected]>
1 parent 0e49c79 commit 25a5d78

File tree

11 files changed

+285
-154
lines changed

11 files changed

+285
-154
lines changed

Tmain/extras-field-for-pseudo-tags.d/stdout-expected.txt

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
!_TAG_ROLE_DESCRIPTION!C!header local /local header/
2727
!_TAG_ROLE_DESCRIPTION!C!header system /system header/
2828
!_TAG_ROLE_DESCRIPTION!C!macro undef /undefined/
29+
!_TAG_ROLE_DESCRIPTION!C!member initialized /initialized with form '.member = ...'/
2930
main input.c /^int main (void) { return 0; }$/
3031
# option: --format=2
3132
!_TAG_EXTRA_DESCRIPTION pseudo /Include pseudo tags/;" extras:pseudo
@@ -55,4 +56,5 @@ main input.c /^int main (void) { return 0; }$/
5556
!_TAG_ROLE_DESCRIPTION!C!header local /local header/;" extras:pseudo
5657
!_TAG_ROLE_DESCRIPTION!C!header system /system header/;" extras:pseudo
5758
!_TAG_ROLE_DESCRIPTION!C!macro undef /undefined/;" extras:pseudo
59+
!_TAG_ROLE_DESCRIPTION!C!member initialized /initialized with form '.member = ...'/;" extras:pseudo
5860
main input.c /^int main (void) { return 0; }$/

Tmain/json-output-format.d/stdout-expected.txt

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
{"_type": "ptag", "name": "TAG_ROLE_DESCRIPTION", "parserName": "C!header", "path": "local", "pattern": "local header"}
9090
{"_type": "ptag", "name": "TAG_ROLE_DESCRIPTION", "parserName": "C!header", "path": "system", "pattern": "system header"}
9191
{"_type": "ptag", "name": "TAG_ROLE_DESCRIPTION", "parserName": "C!macro", "path": "undef", "pattern": "undefined"}
92+
{"_type": "ptag", "name": "TAG_ROLE_DESCRIPTION", "parserName": "C!member", "path": "initialized", "pattern": "initialized with form '.member = ...'"}
9293
{"_type": "ptag", "name": "TAG_ROLE_DESCRIPTION", "parserName": "Go!package", "path": "imported", "pattern": "imported package"}
9394
{"_type": "ptag", "name": "TAG_ROLE_DESCRIPTION", "parserName": "Go!unknown", "path": "receiverType", "pattern": "receiver type"}
9495
{"_type": "ptag", "name": "TAG_ROLE_DESCRIPTION", "parserName": "Python!module", "path": "imported", "pattern": "imported modules"}

Tmain/list-kinds-full.d/stdout-expected.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ f function yes no 0 C function definitions
88
g enum yes no 0 C enumeration names
99
h header yes yes 2 C included header files
1010
l local no no 0 C local variables
11-
m member yes no 0 C struct, and union members
11+
m member yes no 1 C struct, and union members
1212
p prototype no no 0 C function prototypes
1313
s struct yes no 0 C structure names
1414
t typedef yes no 0 C typedefs
@@ -31,7 +31,7 @@ f function yes no 0 C function definitions
3131
g enum yes no 0 C enumeration names
3232
h header yes yes 2 C included header files
3333
l local no no 0 C local variables
34-
m member yes no 0 C class, struct, and union members
34+
m member yes no 1 C class, struct, and union members
3535
n namespace yes no 0 NONE namespaces
3636
p prototype no no 0 C function prototypes
3737
s struct yes no 0 C structure names

Tmain/list-roles.d/stdout-expected.txt

+181-150
Large diffs are not rendered by default.

Tmain/nested-subparsers.d/stdout-expected.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ f function yes no 0 C function definitions
4444
g enum yes no 0 C enumeration names
4545
h header yes yes 2 C included header files
4646
l local no no 0 C local variables
47-
m member yes no 0 C struct, and union members
47+
m member yes no 1 C struct, and union members
4848
p prototype no no 0 C function prototypes
4949
s struct yes no 0 C structure names
5050
t typedef yes no 0 C typedefs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--sort=no
2+
--extras=+r
3+
--fields=+rKZ
4+
--kinds-C=*
5+
--roles-C.{member}=*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
read input.c /^ .read = file_read_fn,$/;" member scope:variable:file_ops roles:initialized
2+
write input.c /^ .write = file_write_fn,$/;" member scope:variable:file_ops roles:initialized
3+
file_ops input.c /^static struct ops file_ops = {$/;" variable typeref:struct:ops file: roles:def
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
static struct ops file_ops = {
2+
.read = file_read_fn,
3+
.write = file_write_fn,
4+
};

parsers/cxx/cxx_parser_variable.c

+69
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,62 @@ CXXToken * cxxParserFindFirstPossiblyNestedAndQualifiedIdentifier(
6767
return cxxTokenChainNextTokenOfType(pId,CXXTokenTypeIdentifier);
6868
}
6969

70+
static void cxxParserExtractMembersInitialization(CXXTokenChain * pChain, int iScopeCorkIndex)
71+
{
72+
tagEntryInfo *pTag = getEntryInCorkQueue(iScopeCorkIndex);
73+
if(!pTag || pTag->kindIndex != CXXTagKindVARIABLE)
74+
return;
75+
76+
// Looking for the pattern:
77+
//
78+
// { .member = ...
79+
//
80+
// or
81+
//
82+
// ; .member = ...
83+
//
84+
for (CXXToken *t = cxxTokenChainFirst(pChain); t && t != pChain->pTail; t = t->pNext)
85+
{
86+
if(
87+
(cxxTokenTypeIs(t, CXXTokenTypeOpeningBracket)
88+
|| cxxTokenTypeIs(t, CXXTokenTypeComma)) &&
89+
(t->pNext
90+
&& cxxTokenTypeIs(t->pNext, CXXTokenTypeDotOperator)) &&
91+
(t->pNext->pNext
92+
&& cxxTokenTypeIs(t->pNext->pNext, CXXTokenTypeIdentifier)) &&
93+
(t->pNext->pNext->pNext
94+
&& cxxTokenTypeIs(t->pNext->pNext->pNext, CXXTokenTypeAssignment))
95+
)
96+
{
97+
CXXToken *pIdentifier = t->pNext->pNext;
98+
if(pIdentifier->iCorkIndex != CORK_NIL && pIdentifier->bCorkIndexForReftag)
99+
{
100+
// Tagged with "unknown" kind already. Reset it.
101+
cxxTagResetRefTag(pIdentifier->iCorkIndex, iScopeCorkIndex,
102+
CXXTagKindMEMBER, CXXTagMemberRoleINITIALIZED);
103+
}
104+
else if(pIdentifier->iCorkIndex == CORK_NIL)
105+
{
106+
tagEntryInfo oEntry;
107+
initRefTagEntry(&oEntry, vStringValue(t->pszWord),
108+
CXXTagKindMEMBER, CXXTagMemberRoleINITIALIZED);
109+
oEntry.lineNumber = t->iLineNumber;
110+
oEntry.filePosition = t->oFilePosition;
111+
oEntry.isFileScope = false;
112+
// TODO: Other scope field must be filled.
113+
oEntry.extensionFields.scopeIndex = iScopeCorkIndex;
114+
pIdentifier->iCorkIndex = makeTagEntry(&oEntry);
115+
registerEntry(pIdentifier->iCorkIndex);
116+
pIdentifier->bCorkIndexForReftag = 1;
117+
118+
}
119+
// Point t to the assignment.
120+
t = t->pNext->pNext->pNext;
121+
}
122+
}
123+
return;
124+
}
125+
70126
//
71127
// Attempt to extract variable declarations from the chain.
72128
// Returns true if at least one variable was extracted.
@@ -800,11 +856,18 @@ bool cxxParserExtractVariableDeclarations(CXXTokenChain * pChain,unsigned int uF
800856
return bGotVariable;
801857
}
802858

859+
// pointing {} of ... = {}
860+
CXXToken * pTokenBracketChain = NULL;
803861
if(!cxxTokenTypeIsOneOf(
804862
t,
805863
CXXTokenTypeComma | CXXTokenTypeSemicolon | CXXTokenTypeOpeningBracket
806864
))
807865
{
866+
if(iCorkIndex != CORK_NIL &&
867+
cxxTokenTypeIs(t, CXXTokenTypeAssignment) &&
868+
t->pNext && cxxTokenTypeIs(t->pNext, CXXTokenTypeBracketChain) &&
869+
t->pNext->pChain)
870+
pTokenBracketChain = t->pNext;
808871
// look for it, but also check for "<" signs: these usually indicate an uncondensed
809872
// template. We give up on them as they are too complicated in this context.
810873
// It's rather unlikely to have multiple declarations with templates after the first one
@@ -829,6 +892,9 @@ bool cxxParserExtractVariableDeclarations(CXXTokenChain * pChain,unsigned int uF
829892
{
830893
if (iCorkIndex != CORK_NIL)
831894
{
895+
if(pTokenBracketChain)
896+
cxxParserExtractMembersInitialization(pTokenBracketChain->pChain,
897+
iCorkIndex);
832898
cxxParserSetEndLineForTagInCorkQueue (iCorkIndex, t->iLineNumber);
833899
iCorkIndex = CORK_NIL;
834900
if(iCorkIndexFQ != CORK_NIL)
@@ -844,6 +910,9 @@ bool cxxParserExtractVariableDeclarations(CXXTokenChain * pChain,unsigned int uF
844910
// Comma. Might have other declarations here.
845911
if (iCorkIndex != CORK_NIL)
846912
{
913+
if(pTokenBracketChain)
914+
cxxParserExtractMembersInitialization(pTokenBracketChain->pChain,
915+
iCorkIndex);
847916
cxxParserSetEndLineForTagInCorkQueue (iCorkIndex, t->iLineNumber);
848917
iCorkIndex = CORK_NIL;
849918
if(iCorkIndexFQ != CORK_NIL)

parsers/cxx/cxx_tag.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ CXX_COMMON_UNKNOWN_ROLES(C);
2828
CXX_COMMON_UNKNOWN_ROLES(CXX);
2929
CXX_COMMON_UNKNOWN_ROLES(CUDA);
3030

31+
#define CXX_COMMON_MEMBER_ROLES(__langPrefix) \
32+
static roleDefinition __langPrefix##MemberRoles [] = { \
33+
{ true, "initialized", "initialized with form '.member = ...'" }, \
34+
}
35+
36+
CXX_COMMON_MEMBER_ROLES(C);
37+
CXX_COMMON_MEMBER_ROLES(CXX);
38+
CXX_COMMON_MEMBER_ROLES(CUDA);
39+
3140
#define CXX_COMMON_MACRO_ROLES(__langPrefix) \
3241
static roleDefinition __langPrefix##MacroRoles [] = { \
3342
RoleTemplateUndef, \
@@ -60,7 +69,9 @@ CXX_COMMON_HEADER_ROLES(CUDA);
6069
.referenceOnly = true, ATTACH_ROLES(_langPrefix##HeaderRoles), .syncWith = _syncWith \
6170
}, \
6271
{ false, 'l', "local", "local variables", .syncWith = _syncWith }, \
63-
{ true, 'm', "member", _szMemberDescription, .syncWith = _syncWith }, \
72+
{ true, 'm', "member", _szMemberDescription, .syncWith = _syncWith, \
73+
.referenceOnly = false, ATTACH_ROLES(_langPrefix##MemberRoles), .syncWith = _syncWith \
74+
},\
6475
{ false, 'p', "prototype", "function prototypes", .syncWith = _syncWith }, \
6576
{ true, 's', "struct", "structure names", .syncWith = _syncWith }, \
6677
{ true, 't', "typedef", "typedefs", .syncWith = _syncWith }, \

parsers/cxx/cxx_tag.h

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ enum CXXTagUnknownRole
4545
CXXTagUnknownRoleREFERENCED,
4646
};
4747

48+
enum CXXTagMemberRole
49+
{
50+
CXXTagMemberRoleINITIALIZED,
51+
};
52+
4853
// Tags specific to the CPP language.
4954
enum CXXTagCPPKind
5055
{

0 commit comments

Comments
 (0)