test_src_scan.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright 2020 Google LLC
  3. #
  4. """Tests for the src_scan module
  5. This includes unit tests for scanning of the source code
  6. """
  7. import copy
  8. import os
  9. import shutil
  10. import tempfile
  11. import unittest
  12. from unittest import mock
  13. from dtoc import src_scan
  14. from patman import test_util
  15. from patman import tools
  16. OUR_PATH = os.path.dirname(os.path.realpath(__file__))
  17. EXPECT_WARN = {'rockchip_rk3288_grf':
  18. {'WARNING: the driver rockchip_rk3288_grf was not found in the driver list'}}
  19. class FakeNode:
  20. """Fake Node object for testing"""
  21. def __init__(self):
  22. self.name = None
  23. self.props = {}
  24. class FakeProp:
  25. """Fake Prop object for testing"""
  26. def __init__(self):
  27. self.name = None
  28. self.value = None
  29. # This is a test so is allowed to access private things in the module it is
  30. # testing
  31. # pylint: disable=W0212
  32. class TestSrcScan(unittest.TestCase):
  33. """Tests for src_scan"""
  34. @classmethod
  35. def setUpClass(cls):
  36. tools.PrepareOutputDir(None)
  37. @classmethod
  38. def tearDownClass(cls):
  39. tools.FinaliseOutputDir()
  40. def test_simple(self):
  41. """Simple test of scanning drivers"""
  42. scan = src_scan.Scanner(None, None)
  43. scan.scan_drivers()
  44. self.assertIn('sandbox_gpio', scan._drivers)
  45. self.assertIn('sandbox_gpio_alias', scan._driver_aliases)
  46. self.assertEqual('sandbox_gpio',
  47. scan._driver_aliases['sandbox_gpio_alias'])
  48. self.assertNotIn('sandbox_gpio_alias2', scan._driver_aliases)
  49. def test_additional(self):
  50. """Test with additional drivers to scan"""
  51. scan = src_scan.Scanner(
  52. None, [None, '', 'tools/dtoc/test/dtoc_test_scan_drivers.cxx'])
  53. scan.scan_drivers()
  54. self.assertIn('sandbox_gpio_alias2', scan._driver_aliases)
  55. self.assertEqual('sandbox_gpio',
  56. scan._driver_aliases['sandbox_gpio_alias2'])
  57. def test_unicode_error(self):
  58. """Test running dtoc with an invalid unicode file
  59. To be able to perform this test without adding a weird text file which
  60. would produce issues when using checkpatch.pl or patman, generate the
  61. file at runtime and then process it.
  62. """
  63. driver_fn = '/tmp/' + next(tempfile._get_candidate_names())
  64. with open(driver_fn, 'wb+') as fout:
  65. fout.write(b'\x81')
  66. scan = src_scan.Scanner(None, [driver_fn])
  67. with test_util.capture_sys_output() as (stdout, _):
  68. scan.scan_drivers()
  69. self.assertRegex(stdout.getvalue(),
  70. r"Skipping file '.*' due to unicode error\s*")
  71. def test_driver(self):
  72. """Test the Driver class"""
  73. i2c = 'I2C_UCLASS'
  74. compat = {'rockchip,rk3288-grf': 'ROCKCHIP_SYSCON_GRF',
  75. 'rockchip,rk3288-srf': None}
  76. drv1 = src_scan.Driver('fred', 'fred.c')
  77. drv2 = src_scan.Driver('mary', 'mary.c')
  78. drv3 = src_scan.Driver('fred', 'fred.c')
  79. drv1.uclass_id = i2c
  80. drv1.compat = compat
  81. drv2.uclass_id = i2c
  82. drv2.compat = compat
  83. drv3.uclass_id = i2c
  84. drv3.compat = compat
  85. self.assertEqual(
  86. "Driver(name='fred', used=False, uclass_id='I2C_UCLASS', "
  87. "compat={'rockchip,rk3288-grf': 'ROCKCHIP_SYSCON_GRF', "
  88. "'rockchip,rk3288-srf': None}, priv=)", str(drv1))
  89. self.assertEqual(drv1, drv3)
  90. self.assertNotEqual(drv1, drv2)
  91. self.assertNotEqual(drv2, drv3)
  92. def test_scan_dirs(self):
  93. """Test scanning of source directories"""
  94. def add_file(fname):
  95. pathname = os.path.join(indir, fname)
  96. dirname = os.path.dirname(pathname)
  97. os.makedirs(dirname, exist_ok=True)
  98. tools.WriteFile(pathname, '', binary=False)
  99. fname_list.append(pathname)
  100. try:
  101. indir = tempfile.mkdtemp(prefix='dtoc.')
  102. fname_list = []
  103. add_file('fname.c')
  104. add_file('.git/ignoreme.c')
  105. add_file('dir/fname2.c')
  106. add_file('build-sandbox/ignoreme2.c')
  107. # Mock out scan_driver and check that it is called with the
  108. # expected files
  109. with mock.patch.object(src_scan.Scanner, "scan_driver") as mocked:
  110. scan = src_scan.Scanner(indir, None)
  111. scan.scan_drivers()
  112. self.assertEqual(2, len(mocked.mock_calls))
  113. self.assertEqual(mock.call(fname_list[0]),
  114. mocked.mock_calls[0])
  115. # .git file should be ignored
  116. self.assertEqual(mock.call(fname_list[2]),
  117. mocked.mock_calls[1])
  118. finally:
  119. shutil.rmtree(indir)
  120. def test_scan(self):
  121. """Test scanning of a driver"""
  122. fname = os.path.join(OUR_PATH, '..', '..', 'drivers/i2c/tegra_i2c.c')
  123. buff = tools.ReadFile(fname, False)
  124. scan = src_scan.Scanner(None, None)
  125. scan._parse_driver(fname, buff)
  126. self.assertIn('i2c_tegra', scan._drivers)
  127. drv = scan._drivers['i2c_tegra']
  128. self.assertEqual('i2c_tegra', drv.name)
  129. self.assertEqual('UCLASS_I2C', drv.uclass_id)
  130. self.assertEqual(
  131. {'nvidia,tegra114-i2c': 'TYPE_114',
  132. 'nvidia,tegra20-i2c': 'TYPE_STD',
  133. 'nvidia,tegra20-i2c-dvc': 'TYPE_DVC'}, drv.compat)
  134. self.assertEqual('i2c_bus', drv.priv)
  135. self.assertEqual(1, len(scan._drivers))
  136. self.assertEqual({}, scan._warnings)
  137. def test_normalized_name(self):
  138. """Test operation of get_normalized_compat_name()"""
  139. prop = FakeProp()
  140. prop.name = 'compatible'
  141. prop.value = 'rockchip,rk3288-grf'
  142. node = FakeNode()
  143. node.props = {'compatible': prop}
  144. # get_normalized_compat_name() uses this to check for root node
  145. node.parent = FakeNode()
  146. scan = src_scan.Scanner(None, None)
  147. with test_util.capture_sys_output() as (stdout, _):
  148. name, aliases = scan.get_normalized_compat_name(node)
  149. self.assertEqual('rockchip_rk3288_grf', name)
  150. self.assertEqual([], aliases)
  151. self.assertEqual(1, len(scan._missing_drivers))
  152. self.assertEqual({'rockchip_rk3288_grf'}, scan._missing_drivers)
  153. self.assertEqual('', stdout.getvalue().strip())
  154. self.assertEqual(EXPECT_WARN, scan._warnings)
  155. i2c = 'I2C_UCLASS'
  156. compat = {'rockchip,rk3288-grf': 'ROCKCHIP_SYSCON_GRF',
  157. 'rockchip,rk3288-srf': None}
  158. drv = src_scan.Driver('fred', 'fred.c')
  159. drv.uclass_id = i2c
  160. drv.compat = compat
  161. scan._drivers['rockchip_rk3288_grf'] = drv
  162. scan._driver_aliases['rockchip_rk3288_srf'] = 'rockchip_rk3288_grf'
  163. with test_util.capture_sys_output() as (stdout, _):
  164. name, aliases = scan.get_normalized_compat_name(node)
  165. self.assertEqual('', stdout.getvalue().strip())
  166. self.assertEqual('rockchip_rk3288_grf', name)
  167. self.assertEqual([], aliases)
  168. self.assertEqual(EXPECT_WARN, scan._warnings)
  169. prop.value = 'rockchip,rk3288-srf'
  170. with test_util.capture_sys_output() as (stdout, _):
  171. name, aliases = scan.get_normalized_compat_name(node)
  172. self.assertEqual('', stdout.getvalue().strip())
  173. self.assertEqual('rockchip_rk3288_grf', name)
  174. self.assertEqual(['rockchip_rk3288_srf'], aliases)
  175. self.assertEqual(EXPECT_WARN, scan._warnings)
  176. def test_scan_errors(self):
  177. """Test detection of scanning errors"""
  178. buff = '''
  179. static const struct udevice_id tegra_i2c_ids2[] = {
  180. { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
  181. { }
  182. };
  183. U_BOOT_DRIVER(i2c_tegra) = {
  184. .name = "i2c_tegra",
  185. .id = UCLASS_I2C,
  186. .of_match = tegra_i2c_ids,
  187. };
  188. '''
  189. scan = src_scan.Scanner(None, None)
  190. with self.assertRaises(ValueError) as exc:
  191. scan._parse_driver('file.c', buff)
  192. self.assertIn(
  193. "file.c: Unknown compatible var 'tegra_i2c_ids' (found: tegra_i2c_ids2)",
  194. str(exc.exception))
  195. def test_of_match(self):
  196. """Test detection of of_match_ptr() member"""
  197. buff = '''
  198. static const struct udevice_id tegra_i2c_ids[] = {
  199. { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
  200. { }
  201. };
  202. U_BOOT_DRIVER(i2c_tegra) = {
  203. .name = "i2c_tegra",
  204. .id = UCLASS_I2C,
  205. .of_match = of_match_ptr(tegra_i2c_ids),
  206. };
  207. '''
  208. scan = src_scan.Scanner(None, None)
  209. scan._parse_driver('file.c', buff)
  210. self.assertIn('i2c_tegra', scan._drivers)
  211. drv = scan._drivers['i2c_tegra']
  212. self.assertEqual('i2c_tegra', drv.name)
  213. self.assertEqual('', drv.phase)
  214. self.assertEqual([], drv.headers)
  215. def test_priv(self):
  216. """Test collection of struct info from drivers"""
  217. buff = '''
  218. static const struct udevice_id test_ids[] = {
  219. { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
  220. { }
  221. };
  222. U_BOOT_DRIVER(testing) = {
  223. .name = "testing",
  224. .id = UCLASS_I2C,
  225. .of_match = test_ids,
  226. .priv_auto = sizeof(struct some_priv),
  227. .plat_auto = sizeof(struct some_plat),
  228. .per_child_auto = sizeof(struct some_cpriv),
  229. .per_child_plat_auto = sizeof(struct some_cplat),
  230. DM_PHASE(tpl)
  231. DM_HEADER(<i2c.h>)
  232. DM_HEADER(<asm/clk.h>)
  233. };
  234. '''
  235. scan = src_scan.Scanner(None, None)
  236. scan._parse_driver('file.c', buff)
  237. self.assertIn('testing', scan._drivers)
  238. drv = scan._drivers['testing']
  239. self.assertEqual('testing', drv.name)
  240. self.assertEqual('UCLASS_I2C', drv.uclass_id)
  241. self.assertEqual(
  242. {'nvidia,tegra114-i2c': 'TYPE_114'}, drv.compat)
  243. self.assertEqual('some_priv', drv.priv)
  244. self.assertEqual('some_plat', drv.plat)
  245. self.assertEqual('some_cpriv', drv.child_priv)
  246. self.assertEqual('some_cplat', drv.child_plat)
  247. self.assertEqual('tpl', drv.phase)
  248. self.assertEqual(['<i2c.h>', '<asm/clk.h>'], drv.headers)
  249. self.assertEqual(1, len(scan._drivers))
  250. def test_uclass_scan(self):
  251. """Test collection of uclass-driver info"""
  252. buff = '''
  253. UCLASS_DRIVER(i2c) = {
  254. .id = UCLASS_I2C,
  255. .name = "i2c",
  256. .flags = DM_UC_FLAG_SEQ_ALIAS,
  257. .priv_auto = sizeof(struct some_priv),
  258. .per_device_auto = sizeof(struct per_dev_priv),
  259. .per_device_plat_auto = sizeof(struct per_dev_plat),
  260. .per_child_auto = sizeof(struct per_child_priv),
  261. .per_child_plat_auto = sizeof(struct per_child_plat),
  262. .child_post_bind = i2c_child_post_bind,
  263. };
  264. '''
  265. scan = src_scan.Scanner(None, None)
  266. scan._parse_uclass_driver('file.c', buff)
  267. self.assertIn('UCLASS_I2C', scan._uclass)
  268. drv = scan._uclass['UCLASS_I2C']
  269. self.assertEqual('i2c', drv.name)
  270. self.assertEqual('UCLASS_I2C', drv.uclass_id)
  271. self.assertEqual('some_priv', drv.priv)
  272. self.assertEqual('per_dev_priv', drv.per_dev_priv)
  273. self.assertEqual('per_dev_plat', drv.per_dev_plat)
  274. self.assertEqual('per_child_priv', drv.per_child_priv)
  275. self.assertEqual('per_child_plat', drv.per_child_plat)
  276. self.assertEqual(1, len(scan._uclass))
  277. drv2 = copy.deepcopy(drv)
  278. self.assertEqual(drv, drv2)
  279. drv2.priv = 'other_priv'
  280. self.assertNotEqual(drv, drv2)
  281. # The hashes only depend on the uclass ID, so should be equal
  282. self.assertEqual(drv.__hash__(), drv2.__hash__())
  283. self.assertEqual("UclassDriver(name='i2c', uclass_id='UCLASS_I2C')",
  284. str(drv))
  285. def test_uclass_scan_errors(self):
  286. """Test detection of uclass scanning errors"""
  287. buff = '''
  288. UCLASS_DRIVER(i2c) = {
  289. .name = "i2c",
  290. };
  291. '''
  292. scan = src_scan.Scanner(None, None)
  293. with self.assertRaises(ValueError) as exc:
  294. scan._parse_uclass_driver('file.c', buff)
  295. self.assertIn("file.c: Cannot parse uclass ID in driver 'i2c'",
  296. str(exc.exception))
  297. def test_struct_scan(self):
  298. """Test collection of struct info"""
  299. buff = '''
  300. /* some comment */
  301. struct some_struct1 {
  302. struct i2c_msg *msgs;
  303. uint nmsgs;
  304. };
  305. '''
  306. scan = src_scan.Scanner(None, None)
  307. scan._basedir = os.path.join(OUR_PATH, '..', '..')
  308. scan._parse_structs('arch/arm/include/asm/file.h', buff)
  309. self.assertIn('some_struct1', scan._structs)
  310. struc = scan._structs['some_struct1']
  311. self.assertEqual('some_struct1', struc.name)
  312. self.assertEqual('asm/file.h', struc.fname)
  313. buff = '''
  314. /* another comment */
  315. struct another_struct {
  316. int speed_hz;
  317. int max_transaction_bytes;
  318. };
  319. '''
  320. scan._parse_structs('include/file2.h', buff)
  321. self.assertIn('another_struct', scan._structs)
  322. struc = scan._structs['another_struct']
  323. self.assertEqual('another_struct', struc.name)
  324. self.assertEqual('file2.h', struc.fname)
  325. self.assertEqual(2, len(scan._structs))
  326. self.assertEqual("Struct(name='another_struct', fname='file2.h')",
  327. str(struc))
  328. def test_struct_scan_errors(self):
  329. """Test scanning a header file with an invalid unicode file"""
  330. output = tools.GetOutputFilename('output.h')
  331. tools.WriteFile(output, b'struct this is a test \x81 of bad unicode')
  332. scan = src_scan.Scanner(None, None)
  333. with test_util.capture_sys_output() as (stdout, _):
  334. scan.scan_header(output)
  335. self.assertIn('due to unicode error', stdout.getvalue())
  336. def setup_dup_drivers(self, name, phase=''):
  337. """Set up for a duplcate test
  338. Returns:
  339. tuple:
  340. Scanner to use
  341. Driver record for first driver
  342. Text of second driver declaration
  343. Node for driver 1
  344. """
  345. driver1 = '''
  346. static const struct udevice_id test_ids[] = {
  347. { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
  348. { }
  349. };
  350. U_BOOT_DRIVER(%s) = {
  351. .name = "testing",
  352. .id = UCLASS_I2C,
  353. .of_match = test_ids,
  354. %s
  355. };
  356. ''' % (name, 'DM_PHASE(%s)' % phase if phase else '')
  357. driver2 = '''
  358. static const struct udevice_id test_ids[] = {
  359. { .compatible = "nvidia,tegra114-dvc" },
  360. { }
  361. };
  362. U_BOOT_DRIVER(%s) = {
  363. .name = "testing",
  364. .id = UCLASS_RAM,
  365. .of_match = test_ids,
  366. };
  367. ''' % name
  368. scan = src_scan.Scanner(None, None, phase)
  369. scan._parse_driver('file1.c', driver1)
  370. self.assertIn(name, scan._drivers)
  371. drv1 = scan._drivers[name]
  372. prop = FakeProp()
  373. prop.name = 'compatible'
  374. prop.value = 'nvidia,tegra114-i2c'
  375. node = FakeNode()
  376. node.name = 'testing'
  377. node.props = {'compatible': prop}
  378. # get_normalized_compat_name() uses this to check for root node
  379. node.parent = FakeNode()
  380. return scan, drv1, driver2, node
  381. def test_dup_drivers(self):
  382. """Test handling of duplicate drivers"""
  383. name = 'nvidia_tegra114_i2c'
  384. scan, drv1, driver2, node = self.setup_dup_drivers(name)
  385. self.assertEqual('', drv1.phase)
  386. # The driver should not have a duplicate yet
  387. self.assertEqual([], drv1.dups)
  388. scan._parse_driver('file2.c', driver2)
  389. # The first driver should now be a duplicate of the second
  390. drv2 = scan._drivers[name]
  391. self.assertEqual('', drv2.phase)
  392. self.assertEqual(1, len(drv2.dups))
  393. self.assertEqual([drv1], drv2.dups)
  394. # There is no way to distinguish them, so we should expect a warning
  395. self.assertTrue(drv2.warn_dups)
  396. # We should see a warning
  397. with test_util.capture_sys_output() as (stdout, _):
  398. scan.mark_used([node])
  399. self.assertEqual(
  400. "Warning: Duplicate driver name 'nvidia_tegra114_i2c' (orig=file2.c, dups=file1.c)",
  401. stdout.getvalue().strip())
  402. def test_dup_drivers_phase(self):
  403. """Test handling of duplicate drivers but with different phases"""
  404. name = 'nvidia_tegra114_i2c'
  405. scan, drv1, driver2, node = self.setup_dup_drivers(name, 'spl')
  406. scan._parse_driver('file2.c', driver2)
  407. self.assertEqual('spl', drv1.phase)
  408. # The second driver should now be a duplicate of the second
  409. self.assertEqual(1, len(drv1.dups))
  410. drv2 = drv1.dups[0]
  411. # The phase is different, so we should not warn of dups
  412. self.assertFalse(drv1.warn_dups)
  413. # We should not see a warning
  414. with test_util.capture_sys_output() as (stdout, _):
  415. scan.mark_used([node])
  416. self.assertEqual('', stdout.getvalue().strip())
  417. def test_sequence(self):
  418. """Test assignment of sequence numnbers"""
  419. scan = src_scan.Scanner(None, None, '')
  420. node = FakeNode()
  421. uc = src_scan.UclassDriver('UCLASS_I2C')
  422. node.uclass = uc
  423. node.driver = True
  424. node.seq = -1
  425. node.path = 'mypath'
  426. uc.alias_num_to_node[2] = node
  427. # This should assign 3 (after the 2 that exists)
  428. seq = scan.assign_seq(node)
  429. self.assertEqual(3, seq)
  430. self.assertEqual({'mypath': 3}, uc.alias_path_to_num)
  431. self.assertEqual({2: node, 3: node}, uc.alias_num_to_node)
  432. def test_scan_warnings(self):
  433. """Test detection of scanning warnings"""
  434. buff = '''
  435. static const struct udevice_id tegra_i2c_ids[] = {
  436. { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
  437. { }
  438. };
  439. U_BOOT_DRIVER(i2c_tegra) = {
  440. .name = "i2c_tegra",
  441. .id = UCLASS_I2C,
  442. .of_match = tegra_i2c_ids + 1,
  443. };
  444. '''
  445. # The '+ 1' above should generate a warning
  446. prop = FakeProp()
  447. prop.name = 'compatible'
  448. prop.value = 'rockchip,rk3288-grf'
  449. node = FakeNode()
  450. node.props = {'compatible': prop}
  451. # get_normalized_compat_name() uses this to check for root node
  452. node.parent = FakeNode()
  453. scan = src_scan.Scanner(None, None)
  454. scan._parse_driver('file.c', buff)
  455. self.assertEqual(
  456. {'i2c_tegra':
  457. {"file.c: Warning: unexpected suffix ' + 1' on .of_match line for compat 'tegra_i2c_ids'"}},
  458. scan._warnings)
  459. tprop = FakeProp()
  460. tprop.name = 'compatible'
  461. tprop.value = 'nvidia,tegra114-i2c'
  462. tnode = FakeNode()
  463. tnode.props = {'compatible': tprop}
  464. # get_normalized_compat_name() uses this to check for root node
  465. tnode.parent = FakeNode()
  466. with test_util.capture_sys_output() as (stdout, _):
  467. scan.get_normalized_compat_name(node)
  468. scan.get_normalized_compat_name(tnode)
  469. self.assertEqual('', stdout.getvalue().strip())
  470. self.assertEqual(2, len(scan._missing_drivers))
  471. self.assertEqual({'rockchip_rk3288_grf', 'nvidia_tegra114_i2c'},
  472. scan._missing_drivers)
  473. with test_util.capture_sys_output() as (stdout, _):
  474. scan.show_warnings()
  475. self.assertIn('rockchip_rk3288_grf', stdout.getvalue())
  476. # This should show just the rockchip warning, since the tegra driver
  477. # is not in self._missing_drivers
  478. scan._missing_drivers.remove('nvidia_tegra114_i2c')
  479. with test_util.capture_sys_output() as (stdout, _):
  480. scan.show_warnings()
  481. self.assertIn('rockchip_rk3288_grf', stdout.getvalue())
  482. self.assertNotIn('tegra_i2c_ids', stdout.getvalue())
  483. # Do a similar thing with used drivers. By marking the tegra driver as
  484. # used, the warning related to that driver will be shown
  485. drv = scan._drivers['i2c_tegra']
  486. drv.used = True
  487. with test_util.capture_sys_output() as (stdout, _):
  488. scan.show_warnings()
  489. self.assertIn('rockchip_rk3288_grf', stdout.getvalue())
  490. self.assertIn('tegra_i2c_ids', stdout.getvalue())
  491. # Add a warning to make sure multiple warnings are shown
  492. scan._warnings['i2c_tegra'].update(
  493. scan._warnings['nvidia_tegra114_i2c'])
  494. del scan._warnings['nvidia_tegra114_i2c']
  495. with test_util.capture_sys_output() as (stdout, _):
  496. scan.show_warnings()
  497. self.assertEqual('''i2c_tegra: WARNING: the driver nvidia_tegra114_i2c was not found in the driver list
  498. : file.c: Warning: unexpected suffix ' + 1' on .of_match line for compat 'tegra_i2c_ids'
  499. rockchip_rk3288_grf: WARNING: the driver rockchip_rk3288_grf was not found in the driver list
  500. ''',
  501. stdout.getvalue())
  502. self.assertIn('tegra_i2c_ids', stdout.getvalue())
  503. def scan_uclass_warning(self):
  504. """Test a missing .uclass in the driver"""
  505. buff = '''
  506. static const struct udevice_id tegra_i2c_ids[] = {
  507. { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
  508. { }
  509. };
  510. U_BOOT_DRIVER(i2c_tegra) = {
  511. .name = "i2c_tegra",
  512. .of_match = tegra_i2c_ids,
  513. };
  514. '''
  515. scan = src_scan.Scanner(None, None)
  516. scan._parse_driver('file.c', buff)
  517. self.assertEqual(
  518. {'i2c_tegra': {'Missing .uclass in file.c'}},
  519. scan._warnings)
  520. def scan_compat_warning(self):
  521. """Test a missing .compatible in the driver"""
  522. buff = '''
  523. static const struct udevice_id tegra_i2c_ids[] = {
  524. { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
  525. { }
  526. };
  527. U_BOOT_DRIVER(i2c_tegra) = {
  528. .name = "i2c_tegra",
  529. .id = UCLASS_I2C,
  530. };
  531. '''
  532. scan = src_scan.Scanner(None, None)
  533. scan._parse_driver('file.c', buff)
  534. self.assertEqual(
  535. {'i2c_tegra': {'Missing .compatible in file.c'}},
  536. scan._warnings)