Skip to content

Commit

Permalink
feat: Parse struct constructor initializer list using clang CLI (#19)
Browse files Browse the repository at this point in the history
At this time, only parse the struct constructors, but not the assign
constructors, and move constructors.
  • Loading branch information
littleGnAl authored Oct 10, 2023
1 parent 99ed901 commit f572126
Show file tree
Hide file tree
Showing 12 changed files with 1,443 additions and 16 deletions.
58 changes: 55 additions & 3 deletions cxx-parser/__integration_test__/cxx_parser.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import path from 'path';

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

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

describe('cxx_parser', () => {
let tmpDir: string = '';
Expand All @@ -27,8 +28,8 @@ describe('cxx_parser', () => {
file1Path,
`
struct AAA {
int a;
}
int a;
};
`
);

Expand Down Expand Up @@ -85,4 +86,55 @@ struct AAA {
expect(JSON.parse(json)).toEqual(JSON.parse(expectedJson));
});
});

describe('CXXParser', () => {
it('parse Struct with constructor initializer list', () => {
let file1Name = 'file1.h';
let file1Path = path.join(tmpDir, file1Name);

fs.writeFileSync(
file1Path,
`
#pragma once
namespace ns1 {
struct AAA {
int aaa_;
AAA(): aaa_(0) {}
AAA(int aaa): aaa_(aaa) {}
};
}
`
);

let args = {
includeHeaderDirs: [],
definesMacros: [],
parseFiles: { include: [file1Name] },
customHeaders: [],
};

let parseResult = CXXParser(
new TerraContext(tmpDir, tmpDir),
args,
undefined
)!;

let s = (parseResult.nodes[0] as CXXFile).nodes[0] as Struct;
expect(s.constructors.length).toBe(2);

expect(s.constructors[0].initializerList.length).toBe(1);
expect(s.constructors[0].initializerList[0].kind).toEqual('Value');
expect(s.constructors[0].initializerList[0].name).toEqual('aaa_');
expect(s.constructors[0].initializerList[0].type).toEqual('int');
expect(s.constructors[0].initializerList[0].values).toEqual(['0']);

expect(s.constructors[1].initializerList.length).toBe(1);
expect(s.constructors[1].initializerList[0].kind).toEqual('Parameter');
expect(s.constructors[1].initializerList[0].name).toEqual('aaa_');
expect(s.constructors[1].initializerList[0].type).toEqual('int');
expect(s.constructors[1].initializerList[0].values).toEqual(['aaa']);
});
});
});
Loading

0 comments on commit f572126

Please sign in to comment.