Skip to content

Commit

Permalink
fix: Fix nested nodes's full name not correct
Browse files Browse the repository at this point in the history
  • Loading branch information
littleGnAl committed Jul 3, 2024
1 parent b6d0190 commit fd6e450
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
78 changes: 77 additions & 1 deletion cxx-parser/__tests__/unit_test/cxx_parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import path from 'path';

import { TerraContext } from '@agoraio-extensions/terra-core';

import { dumpCXXAstJson, generateChecksum } from '../../src/cxx_parser';
import {
dumpCXXAstJson,
genParseResultFromJson,
generateChecksum,
} from '../../src/cxx_parser';
import { CXXFile, Struct } from '../../src/cxx_terra_node';

jest.mock('child_process');

Expand Down Expand Up @@ -355,4 +360,75 @@ describe('cxx_parser', () => {

expect(res).toEqual(expectedChecksum);
});

describe('genParseResultFromJson', () => {
it('can fill parent node', () => {
let json = `
[
{
"__TYPE": "CXXFile",
"file_path": "IAgoraRtmClient.h",
"nodes": [
{
"__TYPE": "Clazz",
"attributes": [],
"base_clazzs": [],
"comment": "",
"conditional_compilation_directives_infos": [],
"constructors": [],
"file_path": "IAgoraRtmClient.h",
"member_variables": [],
"methods": [],
"name": "IRtmEventHandler",
"namespaces": ["agora", "rtm"],
"parent_name": "",
"source": ""
},
{
"__TYPE": "Struct",
"attributes": [],
"base_clazzs": [],
"comment": "",
"conditional_compilation_directives_infos": [],
"constructors": [],
"file_path": "IAgoraRtmClient.h",
"member_variables": [],
"methods": [],
"name": "PresenceEvent",
"namespaces": ["agora", "rtm"],
"parent_name": "IRtmEventHandler",
"source": ""
},
{
"__TYPE": "Struct",
"attributes": [],
"base_clazzs": [],
"comment": "",
"conditional_compilation_directives_infos": [],
"constructors": [],
"file_path": "IAgoraRtmClient.h",
"member_variables": [],
"methods": [],
"name": "IntervalInfo",
"namespaces": ["agora", "rtm"],
"parent_name": "PresenceEvent",
"source": ""
}
]
}
]
`;

let parseResult = genParseResultFromJson(json);
let nestedStruct1 = (parseResult.nodes[0] as CXXFile).nodes[1] as Struct;
expect(nestedStruct1!.fullName).toEqual(
'agora::rtm::IRtmEventHandler::PresenceEvent'
);

let nestedStruct2 = (parseResult.nodes[0] as CXXFile).nodes[2] as Struct;
expect(nestedStruct2!.fullName).toEqual(
'agora::rtm::IRtmEventHandler::PresenceEvent::IntervalInfo'
);
});
});
});
14 changes: 11 additions & 3 deletions cxx-parser/src/cxx_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import crypto from 'crypto';
import * as fs from 'fs';
import path from 'path';

import './cxx_parser_ext';

import { ParseResult, TerraContext } from '@agoraio-extensions/terra-core';

import { ClangASTStructConstructorParser } from './constructor_initializer_parser';
Expand Down Expand Up @@ -112,9 +114,10 @@ export function genParseResultFromJson(astJsonContent: string): ParseResult {
}
return value;
});
fillParentNode(cxxFiles);

const parseResult = new ParseResult();
parseResult.nodes = cxxFiles;
fillParentNode(parseResult, cxxFiles);
return parseResult;
}

Expand Down Expand Up @@ -154,10 +157,15 @@ export function CXXParser(
return newParseResult;
}

function fillParentNode(cxxFiles: CXXFile[]) {
function fillParentNode(parseResult: ParseResult, cxxFiles: CXXFile[]) {
cxxFiles.forEach((file) => {
file.nodes.forEach((node) => {
node.parent = file;
if (node.parent_name) {
node.parent = parseResult.resolveNodeByName(node.parent_name) ?? file;
} else {
node.parent = file;
}

if (node.__TYPE === CXXTYPE.Clazz) {
node.asClazz().constructors.forEach((constructor) => {
constructor.parent = node;
Expand Down
6 changes: 6 additions & 0 deletions cxx-parser/src/cxx_terra_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@ export abstract class CXXTerraNode implements TerraNode {

public get fullName(): string {
let fn = this.realName;
let parentFullName = this.parent?.fullName ?? '';
if (parentFullName != '') {
return `${parentFullName}::${fn}`;
}

if (this.parent_name) {
fn = `${this.parent_name}::${fn}`;
}
if (this.namespaces?.length > 0) {
fn = `${this.namespace}::${fn}`;
}

return fn;
}

Expand Down

0 comments on commit fd6e450

Please sign in to comment.