blob: 0a8f7408f14646aedd9a2422185abe8c37f10a5d [file] [log] [blame]
Simon Glassb91126d2017-05-29 15:31:28 -06001# -*- coding: utf-8 -*-
Tom Rini10e47792018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glass26132882012-01-14 15:12:45 +00003#
Simon Glasscf5f0b52020-06-14 10:54:04 -06004# Tests for U-Boot-specific checkpatch.pl features
5#
Simon Glass26132882012-01-14 15:12:45 +00006# Copyright (c) 2011 The Chromium OS Authors.
7#
Simon Glass26132882012-01-14 15:12:45 +00008
9import os
10import tempfile
11import unittest
12
Simon Glassa997ea52020-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 Glass26132882012-01-14 15:12:45 +000018
19
Simon Glassf5dd9062020-06-14 10:54:05 -060020class Line:
Simon Glassf1309302023-10-13 09:28:33 -070021 """Single changed line in one file in a patch
22
23 Args:
24 fname (str): Filename containing the added line
25 text (str): Text of the added line
26 """
Simon Glassf5dd9062020-06-14 10:54:05 -060027 def __init__(self, fname, text):
28 self.fname = fname
29 self.text = text
30
31
32class PatchMaker:
Simon Glassf1309302023-10-13 09:28:33 -070033 """Makes a patch for checking with checkpatch.pl
34
35 The idea here is to create a patch which adds one line in one file,
36 intended to provoke a checkpatch error or warning. The base patch is empty
37 (i.e. invalid), so you should call add_line() to add at least one line.
38 """
Simon Glassf5dd9062020-06-14 10:54:05 -060039 def __init__(self):
Simon Glassf1309302023-10-13 09:28:33 -070040 """Set up the PatchMaker object
41
42 Properties:
43 lines (list of Line): List of lines to add to the patch. Note that
44 each line has both a file and some text associated with it,
45 since for simplicity we just add a single line for each file
46 """
Simon Glassf5dd9062020-06-14 10:54:05 -060047 self.lines = []
48
49 def add_line(self, fname, text):
Simon Glassf1309302023-10-13 09:28:33 -070050 """Add to the list of filename/line pairs"""
Simon Glassf5dd9062020-06-14 10:54:05 -060051 self.lines.append(Line(fname, text))
52
53 def get_patch_text(self):
Simon Glassf1309302023-10-13 09:28:33 -070054 """Build the patch text
55
56 Takes a base patch and adds a diffstat and patch for each filename/line
57 pair in the list.
58
59 Returns:
60 str: Patch text ready for submission to checkpatch
61 """
Simon Glassf5dd9062020-06-14 10:54:05 -060062 base = '''From 125b77450f4c66b8fd9654319520bbe795c9ef31 Mon Sep 17 00:00:00 2001
63From: Simon Glass <sjg@chromium.org>
64Date: Sun, 14 Jun 2020 09:45:14 -0600
65Subject: [PATCH] Test commit
66
67This is a test commit.
68
69Signed-off-by: Simon Glass <sjg@chromium.org>
70---
71
72'''
73 lines = base.splitlines()
74
75 # Create the diffstat
76 change = 0
77 insert = 0
78 for line in self.lines:
79 lines.append(' %s | 1 +' % line.fname)
80 change += 1
81 insert += 1
82 lines.append(' %d files changed, %d insertions(+)' % (change, insert))
83 lines.append('')
84
85 # Create the patch info for each file
86 for line in self.lines:
87 lines.append('diff --git a/%s b/%s' % (line.fname, line.fname))
88 lines.append('index 7837d459f18..5ba7840f68e 100644')
89 lines.append('--- a/%s' % line.fname)
90 lines.append('+++ b/%s' % line.fname)
91 lines += ('''@@ -121,6 +121,7 @@ enum uclass_id {
92 UCLASS_W1, /* Dallas 1-Wire bus */
93 UCLASS_W1_EEPROM, /* one-wire EEPROMs */
94 UCLASS_WDT, /* Watchdog Timer driver */
95+%s
96
97 UCLASS_COUNT,
98 UCLASS_INVALID = -1,
99''' % line.text).splitlines()
100 lines.append('---')
101 lines.append('2.17.1')
102
103 return '\n'.join(lines)
104
105 def get_patch(self):
Simon Glassf1309302023-10-13 09:28:33 -0700106 """Get the patch text and write it into a temporary file
107
108 Returns:
109 str: Filename containing the patch
110 """
Simon Glassf5dd9062020-06-14 10:54:05 -0600111 inhandle, inname = tempfile.mkstemp()
112 infd = os.fdopen(inhandle, 'w')
113 infd.write(self.get_patch_text())
114 infd.close()
115 return inname
116
117 def run_checkpatch(self):
Simon Glassf1309302023-10-13 09:28:33 -0700118 """Run checkpatch on the patch file
119
120 Returns:
121 namedtuple containing:
122 ok: False=failure, True=ok
123 problems: List of problems, each a dict:
124 'type'; error or warning
125 'msg': text message
126 'file' : filename
127 'line': line number
128 errors: Number of errors
129 warnings: Number of warnings
130 checks: Number of checks
131 lines: Number of lines
132 stdout: Full output of checkpatch
133 """
Simon Glassd84e84a2022-01-29 14:14:06 -0700134 return checkpatch.check_patch(self.get_patch(), show_types=True)
Simon Glassf5dd9062020-06-14 10:54:05 -0600135
136
Simon Glass26132882012-01-14 15:12:45 +0000137class TestPatch(unittest.TestCase):
Simon Glasscf5f0b52020-06-14 10:54:04 -0600138 """Test the u_boot_line() function in checkpatch.pl"""
Simon Glass26132882012-01-14 15:12:45 +0000139
Simon Glass88a9ec22022-01-29 14:14:13 -0700140 def test_basic(self):
Simon Glass26132882012-01-14 15:12:45 +0000141 """Test basic filter operation"""
142 data='''
143
144From 656c9a8c31fa65859d924cd21da920d6ba537fad Mon Sep 17 00:00:00 2001
145From: Simon Glass <sjg@chromium.org>
146Date: Thu, 28 Apr 2011 09:58:51 -0700
147Subject: [PATCH (resend) 3/7] Tegra2: Add more clock support
148
149This adds functions to enable/disable clocks and reset to on-chip peripherals.
150
Simon Glassb91126d2017-05-29 15:31:28 -0600151cmd/pci.c:152:11: warning: format ‘%llx’ expects argument of type
152 ‘long long unsigned int’, but argument 3 has type
153 ‘u64 {aka long unsigned int}’ [-Wformat=]
154
Simon Glass26132882012-01-14 15:12:45 +0000155BUG=chromium-os:13875
156TEST=build U-Boot for Seaboard, boot
157
158Change-Id: I80fe1d0c0b7dd10aa58ce5bb1d9290b6664d5413
159
160Review URL: http://codereview.chromium.org/6900006
161
162Signed-off-by: Simon Glass <sjg@chromium.org>
163---
164 arch/arm/cpu/armv7/tegra2/Makefile | 2 +-
165 arch/arm/cpu/armv7/tegra2/ap20.c | 57 ++----
166 arch/arm/cpu/armv7/tegra2/clock.c | 163 +++++++++++++++++
167'''
Douglas Anderson52b5ee82019-09-27 09:23:56 -0700168 expected='''Message-Id: <19991231235959.0.I80fe1d0c0b7dd10aa58ce5bb1d9290b6664d5413@changeid>
169
Simon Glass26132882012-01-14 15:12:45 +0000170
171From 656c9a8c31fa65859d924cd21da920d6ba537fad Mon Sep 17 00:00:00 2001
172From: Simon Glass <sjg@chromium.org>
173Date: Thu, 28 Apr 2011 09:58:51 -0700
174Subject: [PATCH (resend) 3/7] Tegra2: Add more clock support
175
176This adds functions to enable/disable clocks and reset to on-chip peripherals.
177
Simon Glassb91126d2017-05-29 15:31:28 -0600178cmd/pci.c:152:11: warning: format ‘%llx’ expects argument of type
179 ‘long long unsigned int’, but argument 3 has type
180 ‘u64 {aka long unsigned int}’ [-Wformat=]
181
Simon Glass26132882012-01-14 15:12:45 +0000182Signed-off-by: Simon Glass <sjg@chromium.org>
183---
Simon Glassb0cd3412014-08-28 09:43:35 -0600184
Simon Glass26132882012-01-14 15:12:45 +0000185 arch/arm/cpu/armv7/tegra2/Makefile | 2 +-
186 arch/arm/cpu/armv7/tegra2/ap20.c | 57 ++----
187 arch/arm/cpu/armv7/tegra2/clock.c | 163 +++++++++++++++++
188'''
189 out = ''
190 inhandle, inname = tempfile.mkstemp()
Simon Glassf544a2d2019-10-31 07:42:51 -0600191 infd = os.fdopen(inhandle, 'w', encoding='utf-8')
Simon Glass26132882012-01-14 15:12:45 +0000192 infd.write(data)
193 infd.close()
194
195 exphandle, expname = tempfile.mkstemp()
Simon Glassf544a2d2019-10-31 07:42:51 -0600196 expfd = os.fdopen(exphandle, 'w', encoding='utf-8')
Simon Glass26132882012-01-14 15:12:45 +0000197 expfd.write(expected)
198 expfd.close()
199
Simon Glass93f61c02020-10-29 21:46:19 -0600200 # Normally by the time we call fix_patch we've already collected
Douglas Anderson52b5ee82019-09-27 09:23:56 -0700201 # metadata. Here, we haven't, but at least fake up something.
Simon Glass93f61c02020-10-29 21:46:19 -0600202 # Set the "count" to -1 which tells fix_patch to use a bogus/fixed
Douglas Anderson52b5ee82019-09-27 09:23:56 -0700203 # time for generating the Message-Id.
204 com = commit.Commit('')
205 com.change_id = 'I80fe1d0c0b7dd10aa58ce5bb1d9290b6664d5413'
206 com.count = -1
207
Simon Glass93f61c02020-10-29 21:46:19 -0600208 patchstream.fix_patch(None, inname, series.Series(), com)
Douglas Anderson52b5ee82019-09-27 09:23:56 -0700209
Simon Glass26132882012-01-14 15:12:45 +0000210 rc = os.system('diff -u %s %s' % (inname, expname))
211 self.assertEqual(rc, 0)
212
213 os.remove(inname)
214 os.remove(expname)
215
Simon Glass88a9ec22022-01-29 14:14:13 -0700216 def get_data(self, data_type):
Simon Glassab2dabc2017-11-12 21:52:12 -0700217 data='''From 4924887af52713cabea78420eff03badea8f0035 Mon Sep 17 00:00:00 2001
Simon Glass26132882012-01-14 15:12:45 +0000218From: Simon Glass <sjg@chromium.org>
219Date: Thu, 7 Apr 2011 10:14:41 -0700
220Subject: [PATCH 1/4] Add microsecond boot time measurement
221
222This defines the basics of a new boot time measurement feature. This allows
223logging of very accurate time measurements as the boot proceeds, by using
224an available microsecond counter.
225
226%s
227---
228 README | 11 ++++++++
Simon Glassab2dabc2017-11-12 21:52:12 -0700229 MAINTAINERS | 3 ++
Simon Glass26132882012-01-14 15:12:45 +0000230 common/bootstage.c | 50 ++++++++++++++++++++++++++++++++++++
231 include/bootstage.h | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++
232 include/common.h | 8 ++++++
233 5 files changed, 141 insertions(+), 0 deletions(-)
234 create mode 100644 common/bootstage.c
235 create mode 100644 include/bootstage.h
236
237diff --git a/README b/README
238index 6f3748d..f9e4e65 100644
239--- a/README
240+++ b/README
241@@ -2026,6 +2026,17 @@ The following options need to be configured:
Doug Anderson37816752012-11-26 15:21:39 +0000242 example, some LED's) on your board. At the moment,
243 the following checkpoints are implemented:
Simon Glass26132882012-01-14 15:12:45 +0000244
245+- Time boot progress
246+ CONFIG_BOOTSTAGE
247+
248+ Define this option to enable microsecond boot stage timing
249+ on supported platforms. For this to work your platform
250+ needs to define a function timer_get_us() which returns the
251+ number of microseconds since reset. This would normally
252+ be done in your SOC or board timer.c file.
253+
254+ You can add calls to bootstage_mark() to set time markers.
255+
256 - Standalone program support:
Doug Anderson37816752012-11-26 15:21:39 +0000257 CONFIG_STANDALONE_LOAD_ADDR
Simon Glass26132882012-01-14 15:12:45 +0000258
Simon Glassab2dabc2017-11-12 21:52:12 -0700259diff --git a/MAINTAINERS b/MAINTAINERS
260index b167b028ec..beb7dc634f 100644
261--- a/MAINTAINERS
262+++ b/MAINTAINERS
263@@ -474,3 +474,8 @@ S: Maintained
264 T: git git://git.denx.de/u-boot.git
265 F: *
266 F: */
267+
268+BOOTSTAGE
269+M: Simon Glass <sjg@chromium.org>
270+L: u-boot@lists.denx.de
271+F: common/bootstage.c
Simon Glass26132882012-01-14 15:12:45 +0000272diff --git a/common/bootstage.c b/common/bootstage.c
273new file mode 100644
274index 0000000..2234c87
275--- /dev/null
276+++ b/common/bootstage.c
Simon Glassab2dabc2017-11-12 21:52:12 -0700277@@ -0,0 +1,37 @@
Chris Packhamdf0c6af2018-06-07 20:45:07 +1200278+%s
Simon Glass26132882012-01-14 15:12:45 +0000279+/*
280+ * Copyright (c) 2011, Google Inc. All rights reserved.
281+ *
Simon Glass26132882012-01-14 15:12:45 +0000282+ */
283+
Simon Glass26132882012-01-14 15:12:45 +0000284+/*
285+ * This module records the progress of boot and arbitrary commands, and
286+ * permits accurate timestamping of each. The records can optionally be
287+ * passed to kernel in the ATAGs
288+ */
289+
Tom Rinib80e21b2023-10-13 09:28:32 -0700290+#include <config.h>
Simon Glass26132882012-01-14 15:12:45 +0000291+
Simon Glass26132882012-01-14 15:12:45 +0000292+struct bootstage_record {
Simon Glassab2dabc2017-11-12 21:52:12 -0700293+ u32 time_us;
Simon Glass26132882012-01-14 15:12:45 +0000294+ const char *name;
295+};
296+
297+static struct bootstage_record record[BOOTSTAGE_COUNT];
298+
Simon Glassab2dabc2017-11-12 21:52:12 -0700299+u32 bootstage_mark(enum bootstage_id id, const char *name)
Simon Glass26132882012-01-14 15:12:45 +0000300+{
301+ struct bootstage_record *rec = &record[id];
302+
303+ /* Only record the first event for each */
304+%sif (!rec->name) {
Simon Glassab2dabc2017-11-12 21:52:12 -0700305+ rec->time_us = (u32)timer_get_us();
Simon Glass26132882012-01-14 15:12:45 +0000306+ rec->name = name;
307+ }
Simon Glass0495abf2013-03-26 13:09:39 +0000308+ if (!rec->name &&
309+ %ssomething_else) {
Simon Glassab2dabc2017-11-12 21:52:12 -0700310+ rec->time_us = (u32)timer_get_us();
Simon Glass0495abf2013-03-26 13:09:39 +0000311+ rec->name = name;
312+ }
Simon Glass26132882012-01-14 15:12:45 +0000313+%sreturn rec->time_us;
314+}
315--
3161.7.3.1
317'''
318 signoff = 'Signed-off-by: Simon Glass <sjg@chromium.org>\n'
Chris Packhamdf0c6af2018-06-07 20:45:07 +1200319 license = '// SPDX-License-Identifier: GPL-2.0+'
Simon Glass26132882012-01-14 15:12:45 +0000320 tab = ' '
Simon Glass0495abf2013-03-26 13:09:39 +0000321 indent = ' '
Simon Glass26132882012-01-14 15:12:45 +0000322 if data_type == 'good':
323 pass
324 elif data_type == 'no-signoff':
325 signoff = ''
Chris Packhamdf0c6af2018-06-07 20:45:07 +1200326 elif data_type == 'no-license':
327 license = ''
Simon Glass26132882012-01-14 15:12:45 +0000328 elif data_type == 'spaces':
329 tab = ' '
Simon Glass0495abf2013-03-26 13:09:39 +0000330 elif data_type == 'indent':
331 indent = tab
Simon Glass26132882012-01-14 15:12:45 +0000332 else:
Paul Burtonc3931342016-09-27 16:03:50 +0100333 print('not implemented')
Chris Packhamdf0c6af2018-06-07 20:45:07 +1200334 return data % (signoff, license, tab, indent, tab)
Simon Glass26132882012-01-14 15:12:45 +0000335
Simon Glass88a9ec22022-01-29 14:14:13 -0700336 def setup_data(self, data_type):
Simon Glass26132882012-01-14 15:12:45 +0000337 inhandle, inname = tempfile.mkstemp()
338 infd = os.fdopen(inhandle, 'w')
Simon Glass88a9ec22022-01-29 14:14:13 -0700339 data = self.get_data(data_type)
Simon Glass26132882012-01-14 15:12:45 +0000340 infd.write(data)
341 infd.close()
342 return inname
343
Simon Glass88a9ec22022-01-29 14:14:13 -0700344 def test_good(self):
Simon Glass26132882012-01-14 15:12:45 +0000345 """Test checkpatch operation"""
Simon Glass88a9ec22022-01-29 14:14:13 -0700346 inf = self.setup_data('good')
Simon Glassd84e84a2022-01-29 14:14:06 -0700347 result = checkpatch.check_patch(inf)
Simon Glass0495abf2013-03-26 13:09:39 +0000348 self.assertEqual(result.ok, True)
349 self.assertEqual(result.problems, [])
350 self.assertEqual(result.errors, 0)
351 self.assertEqual(result.warnings, 0)
352 self.assertEqual(result.checks, 0)
Simon Glassab2dabc2017-11-12 21:52:12 -0700353 self.assertEqual(result.lines, 62)
Simon Glass26132882012-01-14 15:12:45 +0000354 os.remove(inf)
355
Simon Glass88a9ec22022-01-29 14:14:13 -0700356 def test_no_signoff(self):
357 inf = self.setup_data('no-signoff')
Simon Glassd84e84a2022-01-29 14:14:06 -0700358 result = checkpatch.check_patch(inf)
Simon Glass0495abf2013-03-26 13:09:39 +0000359 self.assertEqual(result.ok, False)
360 self.assertEqual(len(result.problems), 1)
361 self.assertEqual(result.errors, 1)
362 self.assertEqual(result.warnings, 0)
363 self.assertEqual(result.checks, 0)
Simon Glassab2dabc2017-11-12 21:52:12 -0700364 self.assertEqual(result.lines, 62)
Simon Glass26132882012-01-14 15:12:45 +0000365 os.remove(inf)
366
Simon Glass88a9ec22022-01-29 14:14:13 -0700367 def test_no_license(self):
368 inf = self.setup_data('no-license')
Simon Glassd84e84a2022-01-29 14:14:06 -0700369 result = checkpatch.check_patch(inf)
Chris Packhamdf0c6af2018-06-07 20:45:07 +1200370 self.assertEqual(result.ok, False)
371 self.assertEqual(len(result.problems), 1)
372 self.assertEqual(result.errors, 0)
373 self.assertEqual(result.warnings, 1)
374 self.assertEqual(result.checks, 0)
375 self.assertEqual(result.lines, 62)
376 os.remove(inf)
377
Simon Glass88a9ec22022-01-29 14:14:13 -0700378 def test_spaces(self):
379 inf = self.setup_data('spaces')
Simon Glassd84e84a2022-01-29 14:14:06 -0700380 result = checkpatch.check_patch(inf)
Simon Glass0495abf2013-03-26 13:09:39 +0000381 self.assertEqual(result.ok, False)
Simon Glassab2dabc2017-11-12 21:52:12 -0700382 self.assertEqual(len(result.problems), 3)
Simon Glass0495abf2013-03-26 13:09:39 +0000383 self.assertEqual(result.errors, 0)
Simon Glassab2dabc2017-11-12 21:52:12 -0700384 self.assertEqual(result.warnings, 3)
Simon Glass0495abf2013-03-26 13:09:39 +0000385 self.assertEqual(result.checks, 0)
Simon Glassab2dabc2017-11-12 21:52:12 -0700386 self.assertEqual(result.lines, 62)
Simon Glass0495abf2013-03-26 13:09:39 +0000387 os.remove(inf)
388
Simon Glass88a9ec22022-01-29 14:14:13 -0700389 def test_indent(self):
390 inf = self.setup_data('indent')
Simon Glassd84e84a2022-01-29 14:14:06 -0700391 result = checkpatch.check_patch(inf)
Simon Glass0495abf2013-03-26 13:09:39 +0000392 self.assertEqual(result.ok, False)
393 self.assertEqual(len(result.problems), 1)
394 self.assertEqual(result.errors, 0)
395 self.assertEqual(result.warnings, 0)
396 self.assertEqual(result.checks, 1)
Simon Glassab2dabc2017-11-12 21:52:12 -0700397 self.assertEqual(result.lines, 62)
Simon Glass26132882012-01-14 15:12:45 +0000398 os.remove(inf)
399
Simon Glass88a9ec22022-01-29 14:14:13 -0700400 def check_single_message(self, pm, msg, pmtype = 'warning'):
Simon Glasscc00e492020-06-14 10:54:07 -0600401 """Helper function to run checkpatch and check the result
402
403 Args:
404 pm: PatchMaker object to use
Sean Anderson50fe43f2021-03-11 00:15:45 -0500405 msg: Expected message (e.g. 'LIVETREE')
Simon Glasscc00e492020-06-14 10:54:07 -0600406 pmtype: Type of problem ('error', 'warning')
407 """
408 result = pm.run_checkpatch()
409 if pmtype == 'warning':
410 self.assertEqual(result.warnings, 1)
411 elif pmtype == 'error':
412 self.assertEqual(result.errors, 1)
413 if len(result.problems) != 1:
414 print(result.problems)
415 self.assertEqual(len(result.problems), 1)
416 self.assertIn(msg, result.problems[0]['cptype'])
417
Simon Glass88a9ec22022-01-29 14:14:13 -0700418 def test_uclass(self):
Simon Glassf5dd9062020-06-14 10:54:05 -0600419 """Test for possible new uclass"""
420 pm = PatchMaker()
421 pm.add_line('include/dm/uclass-id.h', 'UCLASS_WIBBLE,')
Simon Glass88a9ec22022-01-29 14:14:13 -0700422 self.check_single_message(pm, 'NEW_UCLASS')
Simon Glasscc00e492020-06-14 10:54:07 -0600423
Simon Glass88a9ec22022-01-29 14:14:13 -0700424 def test_livetree(self):
Simon Glass50137da2020-07-19 10:16:00 -0600425 """Test for using the livetree API"""
Simon Glasscc00e492020-06-14 10:54:07 -0600426 pm = PatchMaker()
427 pm.add_line('common/main.c', 'fdtdec_do_something()')
Simon Glass88a9ec22022-01-29 14:14:13 -0700428 self.check_single_message(pm, 'LIVETREE')
Simon Glasscc00e492020-06-14 10:54:07 -0600429
Simon Glass88a9ec22022-01-29 14:14:13 -0700430 def test_new_command(self):
Simon Glass50137da2020-07-19 10:16:00 -0600431 """Test for adding a new command"""
Simon Glasscc00e492020-06-14 10:54:07 -0600432 pm = PatchMaker()
433 pm.add_line('common/main.c', 'do_wibble(struct cmd_tbl *cmd_tbl)')
Simon Glass88a9ec22022-01-29 14:14:13 -0700434 self.check_single_message(pm, 'CMD_TEST')
Simon Glasscc00e492020-06-14 10:54:07 -0600435
Simon Glass88a9ec22022-01-29 14:14:13 -0700436 def test_prefer_if(self):
Simon Glass50137da2020-07-19 10:16:00 -0600437 """Test for using #ifdef"""
Simon Glasscc00e492020-06-14 10:54:07 -0600438 pm = PatchMaker()
439 pm.add_line('common/main.c', '#ifdef CONFIG_YELLOW')
Simon Glass2ec1ed22020-06-14 10:54:08 -0600440 pm.add_line('common/init.h', '#ifdef CONFIG_YELLOW')
441 pm.add_line('fred.dtsi', '#ifdef CONFIG_YELLOW')
Simon Glass88a9ec22022-01-29 14:14:13 -0700442 self.check_single_message(pm, "PREFER_IF")
Simon Glasscc00e492020-06-14 10:54:07 -0600443
Simon Glass88a9ec22022-01-29 14:14:13 -0700444 def test_command_use_defconfig(self):
Simon Glass50137da2020-07-19 10:16:00 -0600445 """Test for enabling/disabling commands using preprocesor"""
Simon Glasscc00e492020-06-14 10:54:07 -0600446 pm = PatchMaker()
447 pm.add_line('common/main.c', '#undef CONFIG_CMD_WHICH')
Tom Rini14f8ef62022-12-04 10:14:14 -0500448 self.check_single_message(pm, 'DEFINE_CONFIG_SYM', 'error')
Simon Glassf5dd9062020-06-14 10:54:05 -0600449
Simon Glass88a9ec22022-01-29 14:14:13 -0700450 def test_barred_include_in_hdr(self):
Simon Glassf38051e2020-07-19 10:16:01 -0600451 """Test for using a barred include in a header file"""
452 pm = PatchMaker()
Simon Glassf38051e2020-07-19 10:16:01 -0600453 pm.add_line('include/myfile.h', '#include <dm.h>')
Simon Glass88a9ec22022-01-29 14:14:13 -0700454 self.check_single_message(pm, 'BARRED_INCLUDE_IN_HDR', 'error')
Simon Glassf38051e2020-07-19 10:16:01 -0600455
Tom Rinib80e21b2023-10-13 09:28:32 -0700456 def test_barred_include_common_h(self):
457 """Test for adding common.h to a file"""
458 pm = PatchMaker()
459 pm.add_line('include/myfile.h', '#include <common.h>')
460 self.check_single_message(pm, 'BARRED_INCLUDE_COMMON_H', 'error')
461
Simon Glass88a9ec22022-01-29 14:14:13 -0700462 def test_config_is_enabled_config(self):
Alper Nebi Yasak7c1a3022020-10-05 09:57:30 +0300463 """Test for accidental CONFIG_IS_ENABLED(CONFIG_*) calls"""
464 pm = PatchMaker()
465 pm.add_line('common/main.c', 'if (CONFIG_IS_ENABLED(CONFIG_CLK))')
Simon Glass88a9ec22022-01-29 14:14:13 -0700466 self.check_single_message(pm, 'CONFIG_IS_ENABLED_CONFIG', 'error')
Alper Nebi Yasak7c1a3022020-10-05 09:57:30 +0300467
Simon Glass2d848112020-12-03 16:55:24 -0700468 def check_struct(self, auto, suffix, warning):
469 """Check one of the warnings for struct naming
470
471 Args:
472 auto: Auto variable name, e.g. 'per_child_auto'
473 suffix: Suffix to expect on member, e.g. '_priv'
474 warning: Warning name, e.g. 'PRIV_AUTO'
475 """
476 pm = PatchMaker()
477 pm.add_line('common/main.c', '.%s = sizeof(struct(fred)),' % auto)
478 pm.add_line('common/main.c', '.%s = sizeof(struct(mary%s)),' %
479 (auto, suffix))
Simon Glass88a9ec22022-01-29 14:14:13 -0700480 self.check_single_message(
Simon Glass2d848112020-12-03 16:55:24 -0700481 pm, warning, "struct 'fred' should have a %s suffix" % suffix)
482
Simon Glass88a9ec22022-01-29 14:14:13 -0700483 def test_dm_driver_auto(self):
Simon Glass2d848112020-12-03 16:55:24 -0700484 """Check for the correct suffix on 'struct driver' auto members"""
485 self.check_struct('priv_auto', '_priv', 'PRIV_AUTO')
486 self.check_struct('plat_auto', '_plat', 'PLAT_AUTO')
487 self.check_struct('per_child_auto', '_priv', 'CHILD_PRIV_AUTO')
488 self.check_struct('per_child_plat_auto', '_plat', 'CHILD_PLAT_AUTO')
489
Simon Glass88a9ec22022-01-29 14:14:13 -0700490 def test_dm_uclass_auto(self):
Simon Glass2d848112020-12-03 16:55:24 -0700491 """Check for the correct suffix on 'struct uclass' auto members"""
492 # Some of these are omitted since they match those from struct driver
493 self.check_struct('per_device_auto', '_priv', 'DEVICE_PRIV_AUTO')
494 self.check_struct('per_device_plat_auto', '_plat', 'DEVICE_PLAT_AUTO')
495
Sean Anderson50fe43f2021-03-11 00:15:45 -0500496 def check_strl(self, func):
497 """Check one of the checks for strn(cpy|cat)"""
498 pm = PatchMaker()
499 pm.add_line('common/main.c', "strn%s(foo, bar, sizeof(foo));" % func)
Simon Glass88a9ec22022-01-29 14:14:13 -0700500 self.check_single_message(pm, "STRL",
Sean Anderson50fe43f2021-03-11 00:15:45 -0500501 "strl%s is preferred over strn%s because it always produces a nul-terminated string\n"
502 % (func, func))
503
Simon Glass88a9ec22022-01-29 14:14:13 -0700504 def test_strl(self):
Sean Anderson50fe43f2021-03-11 00:15:45 -0500505 """Check for uses of strn(cat|cpy)"""
506 self.check_strl("cat");
507 self.check_strl("cpy");
Simon Glass26132882012-01-14 15:12:45 +0000508
Simon Glassb4af4582023-02-13 08:56:38 -0700509 def test_schema(self):
510 """Check for uses of strn(cat|cpy)"""
511 pm = PatchMaker()
512 pm.add_line('arch/sandbox/dts/sandbox.dtsi', '\tu-boot,dm-pre-proper;')
513 self.check_single_message(pm, 'PRE_SCHEMA', 'error')
514
Simon Glass26132882012-01-14 15:12:45 +0000515if __name__ == "__main__":
516 unittest.main()
517 gitutil.RunTests()