1
0
mirror of https://xff.cz/git/u-boot/ synced 2025-11-01 10:56:02 +01:00

dtoc: Support scanning of structs in header files

Drivers can have private / platform data contained in structs and these
struct definitions are generally kept in header files. In order to
generate build-time devices, dtoc needs to generate code that declares
the data contained in those structs. This generated code must include the
relevant header file, to avoid a build error.

We need a way for dtoc to scan header files for struct definitions. Then,
when it wants to generate code that uses a struct, it can make sure it
includes the correct header file, first.

Add a parser for struct information, similar to drivers. Keep a dict of
the structs that were found.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass
2021-02-03 06:00:55 -07:00
parent 1a8b4b9d94
commit acf5cb88b4
2 changed files with 128 additions and 3 deletions

View File

@@ -318,3 +318,48 @@ UCLASS_DRIVER(i2c) = {
scan._parse_uclass_driver('file.c', buff)
self.assertIn("file.c: Cannot parse uclass ID in driver 'i2c'",
str(exc.exception))
def test_struct_scan(self):
"""Test collection of struct info"""
buff = '''
/* some comment */
struct some_struct1 {
struct i2c_msg *msgs;
uint nmsgs;
};
'''
scan = src_scan.Scanner(None, False, None)
scan._basedir = os.path.join(OUR_PATH, '..', '..')
scan._parse_structs('arch/arm/include/asm/file.h', buff)
self.assertIn('some_struct1', scan._structs)
struc = scan._structs['some_struct1']
self.assertEqual('some_struct1', struc.name)
self.assertEqual('asm/file.h', struc.fname)
buff = '''
/* another comment */
struct another_struct {
int speed_hz;
int max_transaction_bytes;
};
'''
scan._parse_structs('include/file2.h', buff)
self.assertIn('another_struct', scan._structs)
struc = scan._structs['another_struct']
self.assertEqual('another_struct', struc.name)
self.assertEqual('file2.h', struc.fname)
self.assertEqual(2, len(scan._structs))
self.assertEqual("Struct(name='another_struct', fname='file2.h')",
str(struc))
def test_struct_scan_errors(self):
"""Test scanning a header file with an invalid unicode file"""
output = tools.GetOutputFilename('output.h')
tools.WriteFile(output, b'struct this is a test \x81 of bad unicode')
scan = src_scan.Scanner(None, False, None)
with test_util.capture_sys_output() as (stdout, _):
scan.scan_header(output)
self.assertIn('due to unicode error', stdout.getvalue())