blob: 4c2ab6e590ed4050188c8c127979e9f5b4f1d4d9 [file] [log] [blame]
Simon Glass2eb5fc12017-05-29 15:31:28 -06001# -*- coding: utf-8 -*-
Tom Rini83d290c2018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glass0d24de92012-01-14 15:12:45 +00003#
Simon Glass40d97342020-06-14 10:54:04 -06004# Tests for U-Boot-specific checkpatch.pl features
5#
Simon Glass0d24de92012-01-14 15:12:45 +00006# Copyright (c) 2011 The Chromium OS Authors.
7#
Simon Glass0d24de92012-01-14 15:12:45 +00008
9import os
10import tempfile
11import unittest
12
Simon Glassbf776672020-04-17 18:09:04 -060013from patman import checkpatch
14from patman import gitutil
15from patman import patchstream
16from patman import series
17from patman import commit
Simon Glass0d24de92012-01-14 15:12:45 +000018
19
Simon Glass4148c202020-06-14 10:54:05 -060020class Line:
21 def __init__(self, fname, text):
22 self.fname = fname
23 self.text = text
24
25
26class PatchMaker:
27 def __init__(self):
28 self.lines = []
29
30 def add_line(self, fname, text):
31 self.lines.append(Line(fname, text))
32
33 def get_patch_text(self):
34 base = '''From 125b77450f4c66b8fd9654319520bbe795c9ef31 Mon Sep 17 00:00:00 2001
35From: Simon Glass <sjg@chromium.org>
36Date: Sun, 14 Jun 2020 09:45:14 -0600
37Subject: [PATCH] Test commit
38
39This is a test commit.
40
41Signed-off-by: Simon Glass <sjg@chromium.org>
42---
43
44'''
45 lines = base.splitlines()
46
47 # Create the diffstat
48 change = 0
49 insert = 0
50 for line in self.lines:
51 lines.append(' %s | 1 +' % line.fname)
52 change += 1
53 insert += 1
54 lines.append(' %d files changed, %d insertions(+)' % (change, insert))
55 lines.append('')
56
57 # Create the patch info for each file
58 for line in self.lines:
59 lines.append('diff --git a/%s b/%s' % (line.fname, line.fname))
60 lines.append('index 7837d459f18..5ba7840f68e 100644')
61 lines.append('--- a/%s' % line.fname)
62 lines.append('+++ b/%s' % line.fname)
63 lines += ('''@@ -121,6 +121,7 @@ enum uclass_id {
64 UCLASS_W1, /* Dallas 1-Wire bus */
65 UCLASS_W1_EEPROM, /* one-wire EEPROMs */
66 UCLASS_WDT, /* Watchdog Timer driver */
67+%s
68
69 UCLASS_COUNT,
70 UCLASS_INVALID = -1,
71''' % line.text).splitlines()
72 lines.append('---')
73 lines.append('2.17.1')
74
75 return '\n'.join(lines)
76
77 def get_patch(self):
78 inhandle, inname = tempfile.mkstemp()
79 infd = os.fdopen(inhandle, 'w')
80 infd.write(self.get_patch_text())
81 infd.close()
82 return inname
83
84 def run_checkpatch(self):
Simon Glassae5e9262022-01-29 14:14:06 -070085 return checkpatch.check_patch(self.get_patch(), show_types=True)
Simon Glass4148c202020-06-14 10:54:05 -060086
87
Simon Glass0d24de92012-01-14 15:12:45 +000088class TestPatch(unittest.TestCase):
Simon Glass40d97342020-06-14 10:54:04 -060089 """Test the u_boot_line() function in checkpatch.pl"""
Simon Glass0d24de92012-01-14 15:12:45 +000090
Simon Glassce312772022-01-29 14:14:13 -070091 def test_basic(self):
Simon Glass0d24de92012-01-14 15:12:45 +000092 """Test basic filter operation"""
93 data='''
94
95From 656c9a8c31fa65859d924cd21da920d6ba537fad Mon Sep 17 00:00:00 2001
96From: Simon Glass <sjg@chromium.org>
97Date: Thu, 28 Apr 2011 09:58:51 -0700
98Subject: [PATCH (resend) 3/7] Tegra2: Add more clock support
99
100This adds functions to enable/disable clocks and reset to on-chip peripherals.
101
Simon Glass2eb5fc12017-05-29 15:31:28 -0600102cmd/pci.c:152:11: warning: format ‘%llx’ expects argument of type
103 ‘long long unsigned int’, but argument 3 has type
104 ‘u64 {aka long unsigned int}’ [-Wformat=]
105
Simon Glass0d24de92012-01-14 15:12:45 +0000106BUG=chromium-os:13875
107TEST=build U-Boot for Seaboard, boot
108
109Change-Id: I80fe1d0c0b7dd10aa58ce5bb1d9290b6664d5413
110
111Review URL: http://codereview.chromium.org/6900006
112
113Signed-off-by: Simon Glass <sjg@chromium.org>
114---
115 arch/arm/cpu/armv7/tegra2/Makefile | 2 +-
116 arch/arm/cpu/armv7/tegra2/ap20.c | 57 ++----
117 arch/arm/cpu/armv7/tegra2/clock.c | 163 +++++++++++++++++
118'''
Douglas Anderson833e4192019-09-27 09:23:56 -0700119 expected='''Message-Id: <19991231235959.0.I80fe1d0c0b7dd10aa58ce5bb1d9290b6664d5413@changeid>
120
Simon Glass0d24de92012-01-14 15:12:45 +0000121
122From 656c9a8c31fa65859d924cd21da920d6ba537fad Mon Sep 17 00:00:00 2001
123From: Simon Glass <sjg@chromium.org>
124Date: Thu, 28 Apr 2011 09:58:51 -0700
125Subject: [PATCH (resend) 3/7] Tegra2: Add more clock support
126
127This adds functions to enable/disable clocks and reset to on-chip peripherals.
128
Simon Glass2eb5fc12017-05-29 15:31:28 -0600129cmd/pci.c:152:11: warning: format ‘%llx’ expects argument of type
130 ‘long long unsigned int’, but argument 3 has type
131 ‘u64 {aka long unsigned int}’ [-Wformat=]
132
Simon Glass0d24de92012-01-14 15:12:45 +0000133Signed-off-by: Simon Glass <sjg@chromium.org>
134---
Simon Glasse752edc2014-08-28 09:43:35 -0600135
Simon Glass0d24de92012-01-14 15:12:45 +0000136 arch/arm/cpu/armv7/tegra2/Makefile | 2 +-
137 arch/arm/cpu/armv7/tegra2/ap20.c | 57 ++----
138 arch/arm/cpu/armv7/tegra2/clock.c | 163 +++++++++++++++++
139'''
140 out = ''
141 inhandle, inname = tempfile.mkstemp()
Simon Glass272cd852019-10-31 07:42:51 -0600142 infd = os.fdopen(inhandle, 'w', encoding='utf-8')
Simon Glass0d24de92012-01-14 15:12:45 +0000143 infd.write(data)
144 infd.close()
145
146 exphandle, expname = tempfile.mkstemp()
Simon Glass272cd852019-10-31 07:42:51 -0600147 expfd = os.fdopen(exphandle, 'w', encoding='utf-8')
Simon Glass0d24de92012-01-14 15:12:45 +0000148 expfd.write(expected)
149 expfd.close()
150
Simon Glassd93720e2020-10-29 21:46:19 -0600151 # Normally by the time we call fix_patch we've already collected
Douglas Anderson833e4192019-09-27 09:23:56 -0700152 # metadata. Here, we haven't, but at least fake up something.
Simon Glassd93720e2020-10-29 21:46:19 -0600153 # Set the "count" to -1 which tells fix_patch to use a bogus/fixed
Douglas Anderson833e4192019-09-27 09:23:56 -0700154 # time for generating the Message-Id.
155 com = commit.Commit('')
156 com.change_id = 'I80fe1d0c0b7dd10aa58ce5bb1d9290b6664d5413'
157 com.count = -1
158
Simon Glassd93720e2020-10-29 21:46:19 -0600159 patchstream.fix_patch(None, inname, series.Series(), com)
Douglas Anderson833e4192019-09-27 09:23:56 -0700160
Simon Glass0d24de92012-01-14 15:12:45 +0000161 rc = os.system('diff -u %s %s' % (inname, expname))
162 self.assertEqual(rc, 0)
163
164 os.remove(inname)
165 os.remove(expname)
166
Simon Glassce312772022-01-29 14:14:13 -0700167 def get_data(self, data_type):
Simon Glass6c328f22017-11-12 21:52:12 -0700168 data='''From 4924887af52713cabea78420eff03badea8f0035 Mon Sep 17 00:00:00 2001
Simon Glass0d24de92012-01-14 15:12:45 +0000169From: Simon Glass <sjg@chromium.org>
170Date: Thu, 7 Apr 2011 10:14:41 -0700
171Subject: [PATCH 1/4] Add microsecond boot time measurement
172
173This defines the basics of a new boot time measurement feature. This allows
174logging of very accurate time measurements as the boot proceeds, by using
175an available microsecond counter.
176
177%s
178---
179 README | 11 ++++++++
Simon Glass6c328f22017-11-12 21:52:12 -0700180 MAINTAINERS | 3 ++
Simon Glass0d24de92012-01-14 15:12:45 +0000181 common/bootstage.c | 50 ++++++++++++++++++++++++++++++++++++
182 include/bootstage.h | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++
183 include/common.h | 8 ++++++
184 5 files changed, 141 insertions(+), 0 deletions(-)
185 create mode 100644 common/bootstage.c
186 create mode 100644 include/bootstage.h
187
188diff --git a/README b/README
189index 6f3748d..f9e4e65 100644
190--- a/README
191+++ b/README
192@@ -2026,6 +2026,17 @@ The following options need to be configured:
Doug Anderson05d52822012-11-26 15:21:39 +0000193 example, some LED's) on your board. At the moment,
194 the following checkpoints are implemented:
Simon Glass0d24de92012-01-14 15:12:45 +0000195
196+- Time boot progress
197+ CONFIG_BOOTSTAGE
198+
199+ Define this option to enable microsecond boot stage timing
200+ on supported platforms. For this to work your platform
201+ needs to define a function timer_get_us() which returns the
202+ number of microseconds since reset. This would normally
203+ be done in your SOC or board timer.c file.
204+
205+ You can add calls to bootstage_mark() to set time markers.
206+
207 - Standalone program support:
Doug Anderson05d52822012-11-26 15:21:39 +0000208 CONFIG_STANDALONE_LOAD_ADDR
Simon Glass0d24de92012-01-14 15:12:45 +0000209
Simon Glass6c328f22017-11-12 21:52:12 -0700210diff --git a/MAINTAINERS b/MAINTAINERS
211index b167b028ec..beb7dc634f 100644
212--- a/MAINTAINERS
213+++ b/MAINTAINERS
214@@ -474,3 +474,8 @@ S: Maintained
215 T: git git://git.denx.de/u-boot.git
216 F: *
217 F: */
218+
219+BOOTSTAGE
220+M: Simon Glass <sjg@chromium.org>
221+L: u-boot@lists.denx.de
222+F: common/bootstage.c
Simon Glass0d24de92012-01-14 15:12:45 +0000223diff --git a/common/bootstage.c b/common/bootstage.c
224new file mode 100644
225index 0000000..2234c87
226--- /dev/null
227+++ b/common/bootstage.c
Simon Glass6c328f22017-11-12 21:52:12 -0700228@@ -0,0 +1,37 @@
Chris Packhamfe6ef1e2018-06-07 20:45:07 +1200229+%s
Simon Glass0d24de92012-01-14 15:12:45 +0000230+/*
231+ * Copyright (c) 2011, Google Inc. All rights reserved.
232+ *
Simon Glass0d24de92012-01-14 15:12:45 +0000233+ */
234+
Simon Glass0d24de92012-01-14 15:12:45 +0000235+/*
236+ * This module records the progress of boot and arbitrary commands, and
237+ * permits accurate timestamping of each. The records can optionally be
238+ * passed to kernel in the ATAGs
239+ */
240+
241+#include <common.h>
242+
Simon Glass0d24de92012-01-14 15:12:45 +0000243+struct bootstage_record {
Simon Glass6c328f22017-11-12 21:52:12 -0700244+ u32 time_us;
Simon Glass0d24de92012-01-14 15:12:45 +0000245+ const char *name;
246+};
247+
248+static struct bootstage_record record[BOOTSTAGE_COUNT];
249+
Simon Glass6c328f22017-11-12 21:52:12 -0700250+u32 bootstage_mark(enum bootstage_id id, const char *name)
Simon Glass0d24de92012-01-14 15:12:45 +0000251+{
252+ struct bootstage_record *rec = &record[id];
253+
254+ /* Only record the first event for each */
255+%sif (!rec->name) {
Simon Glass6c328f22017-11-12 21:52:12 -0700256+ rec->time_us = (u32)timer_get_us();
Simon Glass0d24de92012-01-14 15:12:45 +0000257+ rec->name = name;
258+ }
Simon Glassd29fe6e2013-03-26 13:09:39 +0000259+ if (!rec->name &&
260+ %ssomething_else) {
Simon Glass6c328f22017-11-12 21:52:12 -0700261+ rec->time_us = (u32)timer_get_us();
Simon Glassd29fe6e2013-03-26 13:09:39 +0000262+ rec->name = name;
263+ }
Simon Glass0d24de92012-01-14 15:12:45 +0000264+%sreturn rec->time_us;
265+}
266--
2671.7.3.1
268'''
269 signoff = 'Signed-off-by: Simon Glass <sjg@chromium.org>\n'
Chris Packhamfe6ef1e2018-06-07 20:45:07 +1200270 license = '// SPDX-License-Identifier: GPL-2.0+'
Simon Glass0d24de92012-01-14 15:12:45 +0000271 tab = ' '
Simon Glassd29fe6e2013-03-26 13:09:39 +0000272 indent = ' '
Simon Glass0d24de92012-01-14 15:12:45 +0000273 if data_type == 'good':
274 pass
275 elif data_type == 'no-signoff':
276 signoff = ''
Chris Packhamfe6ef1e2018-06-07 20:45:07 +1200277 elif data_type == 'no-license':
278 license = ''
Simon Glass0d24de92012-01-14 15:12:45 +0000279 elif data_type == 'spaces':
280 tab = ' '
Simon Glassd29fe6e2013-03-26 13:09:39 +0000281 elif data_type == 'indent':
282 indent = tab
Simon Glass0d24de92012-01-14 15:12:45 +0000283 else:
Paul Burtona920a172016-09-27 16:03:50 +0100284 print('not implemented')
Chris Packhamfe6ef1e2018-06-07 20:45:07 +1200285 return data % (signoff, license, tab, indent, tab)
Simon Glass0d24de92012-01-14 15:12:45 +0000286
Simon Glassce312772022-01-29 14:14:13 -0700287 def setup_data(self, data_type):
Simon Glass0d24de92012-01-14 15:12:45 +0000288 inhandle, inname = tempfile.mkstemp()
289 infd = os.fdopen(inhandle, 'w')
Simon Glassce312772022-01-29 14:14:13 -0700290 data = self.get_data(data_type)
Simon Glass0d24de92012-01-14 15:12:45 +0000291 infd.write(data)
292 infd.close()
293 return inname
294
Simon Glassce312772022-01-29 14:14:13 -0700295 def test_good(self):
Simon Glass0d24de92012-01-14 15:12:45 +0000296 """Test checkpatch operation"""
Simon Glassce312772022-01-29 14:14:13 -0700297 inf = self.setup_data('good')
Simon Glassae5e9262022-01-29 14:14:06 -0700298 result = checkpatch.check_patch(inf)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000299 self.assertEqual(result.ok, True)
300 self.assertEqual(result.problems, [])
301 self.assertEqual(result.errors, 0)
302 self.assertEqual(result.warnings, 0)
303 self.assertEqual(result.checks, 0)
Simon Glass6c328f22017-11-12 21:52:12 -0700304 self.assertEqual(result.lines, 62)
Simon Glass0d24de92012-01-14 15:12:45 +0000305 os.remove(inf)
306
Simon Glassce312772022-01-29 14:14:13 -0700307 def test_no_signoff(self):
308 inf = self.setup_data('no-signoff')
Simon Glassae5e9262022-01-29 14:14:06 -0700309 result = checkpatch.check_patch(inf)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000310 self.assertEqual(result.ok, False)
311 self.assertEqual(len(result.problems), 1)
312 self.assertEqual(result.errors, 1)
313 self.assertEqual(result.warnings, 0)
314 self.assertEqual(result.checks, 0)
Simon Glass6c328f22017-11-12 21:52:12 -0700315 self.assertEqual(result.lines, 62)
Simon Glass0d24de92012-01-14 15:12:45 +0000316 os.remove(inf)
317
Simon Glassce312772022-01-29 14:14:13 -0700318 def test_no_license(self):
319 inf = self.setup_data('no-license')
Simon Glassae5e9262022-01-29 14:14:06 -0700320 result = checkpatch.check_patch(inf)
Chris Packhamfe6ef1e2018-06-07 20:45:07 +1200321 self.assertEqual(result.ok, False)
322 self.assertEqual(len(result.problems), 1)
323 self.assertEqual(result.errors, 0)
324 self.assertEqual(result.warnings, 1)
325 self.assertEqual(result.checks, 0)
326 self.assertEqual(result.lines, 62)
327 os.remove(inf)
328
Simon Glassce312772022-01-29 14:14:13 -0700329 def test_spaces(self):
330 inf = self.setup_data('spaces')
Simon Glassae5e9262022-01-29 14:14:06 -0700331 result = checkpatch.check_patch(inf)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000332 self.assertEqual(result.ok, False)
Simon Glass6c328f22017-11-12 21:52:12 -0700333 self.assertEqual(len(result.problems), 3)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000334 self.assertEqual(result.errors, 0)
Simon Glass6c328f22017-11-12 21:52:12 -0700335 self.assertEqual(result.warnings, 3)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000336 self.assertEqual(result.checks, 0)
Simon Glass6c328f22017-11-12 21:52:12 -0700337 self.assertEqual(result.lines, 62)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000338 os.remove(inf)
339
Simon Glassce312772022-01-29 14:14:13 -0700340 def test_indent(self):
341 inf = self.setup_data('indent')
Simon Glassae5e9262022-01-29 14:14:06 -0700342 result = checkpatch.check_patch(inf)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000343 self.assertEqual(result.ok, False)
344 self.assertEqual(len(result.problems), 1)
345 self.assertEqual(result.errors, 0)
346 self.assertEqual(result.warnings, 0)
347 self.assertEqual(result.checks, 1)
Simon Glass6c328f22017-11-12 21:52:12 -0700348 self.assertEqual(result.lines, 62)
Simon Glass0d24de92012-01-14 15:12:45 +0000349 os.remove(inf)
350
Simon Glassce312772022-01-29 14:14:13 -0700351 def check_single_message(self, pm, msg, pmtype = 'warning'):
Simon Glass5c430762020-06-14 10:54:07 -0600352 """Helper function to run checkpatch and check the result
353
354 Args:
355 pm: PatchMaker object to use
Sean Andersond9c30502021-03-11 00:15:45 -0500356 msg: Expected message (e.g. 'LIVETREE')
Simon Glass5c430762020-06-14 10:54:07 -0600357 pmtype: Type of problem ('error', 'warning')
358 """
359 result = pm.run_checkpatch()
360 if pmtype == 'warning':
361 self.assertEqual(result.warnings, 1)
362 elif pmtype == 'error':
363 self.assertEqual(result.errors, 1)
364 if len(result.problems) != 1:
365 print(result.problems)
366 self.assertEqual(len(result.problems), 1)
367 self.assertIn(msg, result.problems[0]['cptype'])
368
Simon Glassce312772022-01-29 14:14:13 -0700369 def test_uclass(self):
Simon Glass4148c202020-06-14 10:54:05 -0600370 """Test for possible new uclass"""
371 pm = PatchMaker()
372 pm.add_line('include/dm/uclass-id.h', 'UCLASS_WIBBLE,')
Simon Glassce312772022-01-29 14:14:13 -0700373 self.check_single_message(pm, 'NEW_UCLASS')
Simon Glass5c430762020-06-14 10:54:07 -0600374
Simon Glassce312772022-01-29 14:14:13 -0700375 def test_livetree(self):
Simon Glass4620d462020-07-19 10:16:00 -0600376 """Test for using the livetree API"""
Simon Glass5c430762020-06-14 10:54:07 -0600377 pm = PatchMaker()
378 pm.add_line('common/main.c', 'fdtdec_do_something()')
Simon Glassce312772022-01-29 14:14:13 -0700379 self.check_single_message(pm, 'LIVETREE')
Simon Glass5c430762020-06-14 10:54:07 -0600380
Simon Glassce312772022-01-29 14:14:13 -0700381 def test_new_command(self):
Simon Glass4620d462020-07-19 10:16:00 -0600382 """Test for adding a new command"""
Simon Glass5c430762020-06-14 10:54:07 -0600383 pm = PatchMaker()
384 pm.add_line('common/main.c', 'do_wibble(struct cmd_tbl *cmd_tbl)')
Simon Glassce312772022-01-29 14:14:13 -0700385 self.check_single_message(pm, 'CMD_TEST')
Simon Glass5c430762020-06-14 10:54:07 -0600386
Simon Glassce312772022-01-29 14:14:13 -0700387 def test_prefer_if(self):
Simon Glass4620d462020-07-19 10:16:00 -0600388 """Test for using #ifdef"""
Simon Glass5c430762020-06-14 10:54:07 -0600389 pm = PatchMaker()
390 pm.add_line('common/main.c', '#ifdef CONFIG_YELLOW')
Simon Glass8af45b12020-06-14 10:54:08 -0600391 pm.add_line('common/init.h', '#ifdef CONFIG_YELLOW')
392 pm.add_line('fred.dtsi', '#ifdef CONFIG_YELLOW')
Simon Glassce312772022-01-29 14:14:13 -0700393 self.check_single_message(pm, "PREFER_IF")
Simon Glass5c430762020-06-14 10:54:07 -0600394
Simon Glassce312772022-01-29 14:14:13 -0700395 def test_command_use_defconfig(self):
Simon Glass4620d462020-07-19 10:16:00 -0600396 """Test for enabling/disabling commands using preprocesor"""
Simon Glass5c430762020-06-14 10:54:07 -0600397 pm = PatchMaker()
398 pm.add_line('common/main.c', '#undef CONFIG_CMD_WHICH')
Tom Rini2a06da02022-12-04 10:14:14 -0500399 self.check_single_message(pm, 'DEFINE_CONFIG_SYM', 'error')
Simon Glass4148c202020-06-14 10:54:05 -0600400
Simon Glassce312772022-01-29 14:14:13 -0700401 def test_barred_include_in_hdr(self):
Simon Glass23552ba2020-07-19 10:16:01 -0600402 """Test for using a barred include in a header file"""
403 pm = PatchMaker()
404 #pm.add_line('include/myfile.h', '#include <common.h>')
405 pm.add_line('include/myfile.h', '#include <dm.h>')
Simon Glassce312772022-01-29 14:14:13 -0700406 self.check_single_message(pm, 'BARRED_INCLUDE_IN_HDR', 'error')
Simon Glass23552ba2020-07-19 10:16:01 -0600407
Simon Glassce312772022-01-29 14:14:13 -0700408 def test_config_is_enabled_config(self):
Alper Nebi Yasakb9cca2c2020-10-05 09:57:30 +0300409 """Test for accidental CONFIG_IS_ENABLED(CONFIG_*) calls"""
410 pm = PatchMaker()
411 pm.add_line('common/main.c', 'if (CONFIG_IS_ENABLED(CONFIG_CLK))')
Simon Glassce312772022-01-29 14:14:13 -0700412 self.check_single_message(pm, 'CONFIG_IS_ENABLED_CONFIG', 'error')
Alper Nebi Yasakb9cca2c2020-10-05 09:57:30 +0300413
Simon Glassb7bbd552020-12-03 16:55:24 -0700414 def check_struct(self, auto, suffix, warning):
415 """Check one of the warnings for struct naming
416
417 Args:
418 auto: Auto variable name, e.g. 'per_child_auto'
419 suffix: Suffix to expect on member, e.g. '_priv'
420 warning: Warning name, e.g. 'PRIV_AUTO'
421 """
422 pm = PatchMaker()
423 pm.add_line('common/main.c', '.%s = sizeof(struct(fred)),' % auto)
424 pm.add_line('common/main.c', '.%s = sizeof(struct(mary%s)),' %
425 (auto, suffix))
Simon Glassce312772022-01-29 14:14:13 -0700426 self.check_single_message(
Simon Glassb7bbd552020-12-03 16:55:24 -0700427 pm, warning, "struct 'fred' should have a %s suffix" % suffix)
428
Simon Glassce312772022-01-29 14:14:13 -0700429 def test_dm_driver_auto(self):
Simon Glassb7bbd552020-12-03 16:55:24 -0700430 """Check for the correct suffix on 'struct driver' auto members"""
431 self.check_struct('priv_auto', '_priv', 'PRIV_AUTO')
432 self.check_struct('plat_auto', '_plat', 'PLAT_AUTO')
433 self.check_struct('per_child_auto', '_priv', 'CHILD_PRIV_AUTO')
434 self.check_struct('per_child_plat_auto', '_plat', 'CHILD_PLAT_AUTO')
435
Simon Glassce312772022-01-29 14:14:13 -0700436 def test_dm_uclass_auto(self):
Simon Glassb7bbd552020-12-03 16:55:24 -0700437 """Check for the correct suffix on 'struct uclass' auto members"""
438 # Some of these are omitted since they match those from struct driver
439 self.check_struct('per_device_auto', '_priv', 'DEVICE_PRIV_AUTO')
440 self.check_struct('per_device_plat_auto', '_plat', 'DEVICE_PLAT_AUTO')
441
Sean Andersond9c30502021-03-11 00:15:45 -0500442 def check_strl(self, func):
443 """Check one of the checks for strn(cpy|cat)"""
444 pm = PatchMaker()
445 pm.add_line('common/main.c', "strn%s(foo, bar, sizeof(foo));" % func)
Simon Glassce312772022-01-29 14:14:13 -0700446 self.check_single_message(pm, "STRL",
Sean Andersond9c30502021-03-11 00:15:45 -0500447 "strl%s is preferred over strn%s because it always produces a nul-terminated string\n"
448 % (func, func))
449
Simon Glassce312772022-01-29 14:14:13 -0700450 def test_strl(self):
Sean Andersond9c30502021-03-11 00:15:45 -0500451 """Check for uses of strn(cat|cpy)"""
452 self.check_strl("cat");
453 self.check_strl("cpy");
Simon Glass0d24de92012-01-14 15:12:45 +0000454
455if __name__ == "__main__":
456 unittest.main()
457 gitutil.RunTests()