blob: db7860f551d0ac001b1a2e9c657791451224bfbe [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)
Maxim Cournoyer12f99fd2023-10-12 23:06:24 -0400212 os.remove(inname)
213
214 # Test whether the keep_change_id settings works.
215 inhandle, inname = tempfile.mkstemp()
216 infd = os.fdopen(inhandle, 'w', encoding='utf-8')
217 infd.write(data)
218 infd.close()
219
220 patchstream.fix_patch(None, inname, series.Series(), com,
221 keep_change_id=True)
222
223 with open(inname, 'r') as f:
224 content = f.read()
225 self.assertIn(
226 'Change-Id: I80fe1d0c0b7dd10aa58ce5bb1d9290b6664d5413',
227 content)
Simon Glass26132882012-01-14 15:12:45 +0000228
229 os.remove(inname)
230 os.remove(expname)
231
Simon Glass88a9ec22022-01-29 14:14:13 -0700232 def get_data(self, data_type):
Simon Glassab2dabc2017-11-12 21:52:12 -0700233 data='''From 4924887af52713cabea78420eff03badea8f0035 Mon Sep 17 00:00:00 2001
Simon Glass26132882012-01-14 15:12:45 +0000234From: Simon Glass <sjg@chromium.org>
235Date: Thu, 7 Apr 2011 10:14:41 -0700
236Subject: [PATCH 1/4] Add microsecond boot time measurement
237
238This defines the basics of a new boot time measurement feature. This allows
239logging of very accurate time measurements as the boot proceeds, by using
240an available microsecond counter.
241
242%s
243---
244 README | 11 ++++++++
Simon Glassab2dabc2017-11-12 21:52:12 -0700245 MAINTAINERS | 3 ++
Simon Glass26132882012-01-14 15:12:45 +0000246 common/bootstage.c | 50 ++++++++++++++++++++++++++++++++++++
247 include/bootstage.h | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++
248 include/common.h | 8 ++++++
249 5 files changed, 141 insertions(+), 0 deletions(-)
250 create mode 100644 common/bootstage.c
251 create mode 100644 include/bootstage.h
252
253diff --git a/README b/README
254index 6f3748d..f9e4e65 100644
255--- a/README
256+++ b/README
257@@ -2026,6 +2026,17 @@ The following options need to be configured:
Doug Anderson37816752012-11-26 15:21:39 +0000258 example, some LED's) on your board. At the moment,
259 the following checkpoints are implemented:
Simon Glass26132882012-01-14 15:12:45 +0000260
261+- Time boot progress
262+ CONFIG_BOOTSTAGE
263+
264+ Define this option to enable microsecond boot stage timing
265+ on supported platforms. For this to work your platform
266+ needs to define a function timer_get_us() which returns the
267+ number of microseconds since reset. This would normally
268+ be done in your SOC or board timer.c file.
269+
270+ You can add calls to bootstage_mark() to set time markers.
271+
272 - Standalone program support:
Doug Anderson37816752012-11-26 15:21:39 +0000273 CONFIG_STANDALONE_LOAD_ADDR
Simon Glass26132882012-01-14 15:12:45 +0000274
Simon Glassab2dabc2017-11-12 21:52:12 -0700275diff --git a/MAINTAINERS b/MAINTAINERS
276index b167b028ec..beb7dc634f 100644
277--- a/MAINTAINERS
278+++ b/MAINTAINERS
279@@ -474,3 +474,8 @@ S: Maintained
280 T: git git://git.denx.de/u-boot.git
281 F: *
282 F: */
283+
284+BOOTSTAGE
285+M: Simon Glass <sjg@chromium.org>
286+L: u-boot@lists.denx.de
287+F: common/bootstage.c
Simon Glass26132882012-01-14 15:12:45 +0000288diff --git a/common/bootstage.c b/common/bootstage.c
289new file mode 100644
290index 0000000..2234c87
291--- /dev/null
292+++ b/common/bootstage.c
Simon Glassab2dabc2017-11-12 21:52:12 -0700293@@ -0,0 +1,37 @@
Chris Packhamdf0c6af2018-06-07 20:45:07 +1200294+%s
Simon Glass26132882012-01-14 15:12:45 +0000295+/*
296+ * Copyright (c) 2011, Google Inc. All rights reserved.
297+ *
Simon Glass26132882012-01-14 15:12:45 +0000298+ */
299+
Simon Glass26132882012-01-14 15:12:45 +0000300+/*
301+ * This module records the progress of boot and arbitrary commands, and
302+ * permits accurate timestamping of each. The records can optionally be
303+ * passed to kernel in the ATAGs
304+ */
305+
Tom Rinib80e21b2023-10-13 09:28:32 -0700306+#include <config.h>
Simon Glass26132882012-01-14 15:12:45 +0000307+
Simon Glass26132882012-01-14 15:12:45 +0000308+struct bootstage_record {
Simon Glassab2dabc2017-11-12 21:52:12 -0700309+ u32 time_us;
Simon Glass26132882012-01-14 15:12:45 +0000310+ const char *name;
311+};
312+
313+static struct bootstage_record record[BOOTSTAGE_COUNT];
314+
Simon Glassab2dabc2017-11-12 21:52:12 -0700315+u32 bootstage_mark(enum bootstage_id id, const char *name)
Simon Glass26132882012-01-14 15:12:45 +0000316+{
317+ struct bootstage_record *rec = &record[id];
318+
319+ /* Only record the first event for each */
320+%sif (!rec->name) {
Simon Glassab2dabc2017-11-12 21:52:12 -0700321+ rec->time_us = (u32)timer_get_us();
Simon Glass26132882012-01-14 15:12:45 +0000322+ rec->name = name;
323+ }
Simon Glass0495abf2013-03-26 13:09:39 +0000324+ if (!rec->name &&
325+ %ssomething_else) {
Simon Glassab2dabc2017-11-12 21:52:12 -0700326+ rec->time_us = (u32)timer_get_us();
Simon Glass0495abf2013-03-26 13:09:39 +0000327+ rec->name = name;
328+ }
Simon Glass26132882012-01-14 15:12:45 +0000329+%sreturn rec->time_us;
330+}
331--
3321.7.3.1
333'''
334 signoff = 'Signed-off-by: Simon Glass <sjg@chromium.org>\n'
Chris Packhamdf0c6af2018-06-07 20:45:07 +1200335 license = '// SPDX-License-Identifier: GPL-2.0+'
Simon Glass26132882012-01-14 15:12:45 +0000336 tab = ' '
Simon Glass0495abf2013-03-26 13:09:39 +0000337 indent = ' '
Simon Glass26132882012-01-14 15:12:45 +0000338 if data_type == 'good':
339 pass
340 elif data_type == 'no-signoff':
341 signoff = ''
Chris Packhamdf0c6af2018-06-07 20:45:07 +1200342 elif data_type == 'no-license':
343 license = ''
Simon Glass26132882012-01-14 15:12:45 +0000344 elif data_type == 'spaces':
345 tab = ' '
Simon Glass0495abf2013-03-26 13:09:39 +0000346 elif data_type == 'indent':
347 indent = tab
Simon Glass26132882012-01-14 15:12:45 +0000348 else:
Paul Burtonc3931342016-09-27 16:03:50 +0100349 print('not implemented')
Chris Packhamdf0c6af2018-06-07 20:45:07 +1200350 return data % (signoff, license, tab, indent, tab)
Simon Glass26132882012-01-14 15:12:45 +0000351
Simon Glass88a9ec22022-01-29 14:14:13 -0700352 def setup_data(self, data_type):
Simon Glass26132882012-01-14 15:12:45 +0000353 inhandle, inname = tempfile.mkstemp()
354 infd = os.fdopen(inhandle, 'w')
Simon Glass88a9ec22022-01-29 14:14:13 -0700355 data = self.get_data(data_type)
Simon Glass26132882012-01-14 15:12:45 +0000356 infd.write(data)
357 infd.close()
358 return inname
359
Simon Glass88a9ec22022-01-29 14:14:13 -0700360 def test_good(self):
Simon Glass26132882012-01-14 15:12:45 +0000361 """Test checkpatch operation"""
Simon Glass88a9ec22022-01-29 14:14:13 -0700362 inf = self.setup_data('good')
Simon Glassd84e84a2022-01-29 14:14:06 -0700363 result = checkpatch.check_patch(inf)
Simon Glass0495abf2013-03-26 13:09:39 +0000364 self.assertEqual(result.ok, True)
365 self.assertEqual(result.problems, [])
366 self.assertEqual(result.errors, 0)
367 self.assertEqual(result.warnings, 0)
368 self.assertEqual(result.checks, 0)
Simon Glassab2dabc2017-11-12 21:52:12 -0700369 self.assertEqual(result.lines, 62)
Simon Glass26132882012-01-14 15:12:45 +0000370 os.remove(inf)
371
Simon Glass88a9ec22022-01-29 14:14:13 -0700372 def test_no_signoff(self):
373 inf = self.setup_data('no-signoff')
Simon Glassd84e84a2022-01-29 14:14:06 -0700374 result = checkpatch.check_patch(inf)
Simon Glass0495abf2013-03-26 13:09:39 +0000375 self.assertEqual(result.ok, False)
376 self.assertEqual(len(result.problems), 1)
377 self.assertEqual(result.errors, 1)
378 self.assertEqual(result.warnings, 0)
379 self.assertEqual(result.checks, 0)
Simon Glassab2dabc2017-11-12 21:52:12 -0700380 self.assertEqual(result.lines, 62)
Simon Glass26132882012-01-14 15:12:45 +0000381 os.remove(inf)
382
Simon Glass88a9ec22022-01-29 14:14:13 -0700383 def test_no_license(self):
384 inf = self.setup_data('no-license')
Simon Glassd84e84a2022-01-29 14:14:06 -0700385 result = checkpatch.check_patch(inf)
Chris Packhamdf0c6af2018-06-07 20:45:07 +1200386 self.assertEqual(result.ok, False)
387 self.assertEqual(len(result.problems), 1)
388 self.assertEqual(result.errors, 0)
389 self.assertEqual(result.warnings, 1)
390 self.assertEqual(result.checks, 0)
391 self.assertEqual(result.lines, 62)
392 os.remove(inf)
393
Simon Glass88a9ec22022-01-29 14:14:13 -0700394 def test_spaces(self):
395 inf = self.setup_data('spaces')
Simon Glassd84e84a2022-01-29 14:14:06 -0700396 result = checkpatch.check_patch(inf)
Simon Glass0495abf2013-03-26 13:09:39 +0000397 self.assertEqual(result.ok, False)
Simon Glassab2dabc2017-11-12 21:52:12 -0700398 self.assertEqual(len(result.problems), 3)
Simon Glass0495abf2013-03-26 13:09:39 +0000399 self.assertEqual(result.errors, 0)
Simon Glassab2dabc2017-11-12 21:52:12 -0700400 self.assertEqual(result.warnings, 3)
Simon Glass0495abf2013-03-26 13:09:39 +0000401 self.assertEqual(result.checks, 0)
Simon Glassab2dabc2017-11-12 21:52:12 -0700402 self.assertEqual(result.lines, 62)
Simon Glass0495abf2013-03-26 13:09:39 +0000403 os.remove(inf)
404
Simon Glass88a9ec22022-01-29 14:14:13 -0700405 def test_indent(self):
406 inf = self.setup_data('indent')
Simon Glassd84e84a2022-01-29 14:14:06 -0700407 result = checkpatch.check_patch(inf)
Simon Glass0495abf2013-03-26 13:09:39 +0000408 self.assertEqual(result.ok, False)
409 self.assertEqual(len(result.problems), 1)
410 self.assertEqual(result.errors, 0)
411 self.assertEqual(result.warnings, 0)
412 self.assertEqual(result.checks, 1)
Simon Glassab2dabc2017-11-12 21:52:12 -0700413 self.assertEqual(result.lines, 62)
Simon Glass26132882012-01-14 15:12:45 +0000414 os.remove(inf)
415
Simon Glass88a9ec22022-01-29 14:14:13 -0700416 def check_single_message(self, pm, msg, pmtype = 'warning'):
Simon Glasscc00e492020-06-14 10:54:07 -0600417 """Helper function to run checkpatch and check the result
418
419 Args:
420 pm: PatchMaker object to use
Sean Anderson50fe43f2021-03-11 00:15:45 -0500421 msg: Expected message (e.g. 'LIVETREE')
Simon Glasscc00e492020-06-14 10:54:07 -0600422 pmtype: Type of problem ('error', 'warning')
423 """
424 result = pm.run_checkpatch()
425 if pmtype == 'warning':
426 self.assertEqual(result.warnings, 1)
427 elif pmtype == 'error':
428 self.assertEqual(result.errors, 1)
429 if len(result.problems) != 1:
430 print(result.problems)
431 self.assertEqual(len(result.problems), 1)
432 self.assertIn(msg, result.problems[0]['cptype'])
433
Simon Glass88a9ec22022-01-29 14:14:13 -0700434 def test_uclass(self):
Simon Glassf5dd9062020-06-14 10:54:05 -0600435 """Test for possible new uclass"""
436 pm = PatchMaker()
437 pm.add_line('include/dm/uclass-id.h', 'UCLASS_WIBBLE,')
Simon Glass88a9ec22022-01-29 14:14:13 -0700438 self.check_single_message(pm, 'NEW_UCLASS')
Simon Glasscc00e492020-06-14 10:54:07 -0600439
Simon Glass88a9ec22022-01-29 14:14:13 -0700440 def test_livetree(self):
Simon Glass50137da2020-07-19 10:16:00 -0600441 """Test for using the livetree API"""
Simon Glasscc00e492020-06-14 10:54:07 -0600442 pm = PatchMaker()
443 pm.add_line('common/main.c', 'fdtdec_do_something()')
Simon Glass88a9ec22022-01-29 14:14:13 -0700444 self.check_single_message(pm, 'LIVETREE')
Simon Glasscc00e492020-06-14 10:54:07 -0600445
Simon Glass88a9ec22022-01-29 14:14:13 -0700446 def test_new_command(self):
Simon Glass50137da2020-07-19 10:16:00 -0600447 """Test for adding a new command"""
Simon Glasscc00e492020-06-14 10:54:07 -0600448 pm = PatchMaker()
449 pm.add_line('common/main.c', 'do_wibble(struct cmd_tbl *cmd_tbl)')
Simon Glass88a9ec22022-01-29 14:14:13 -0700450 self.check_single_message(pm, 'CMD_TEST')
Simon Glasscc00e492020-06-14 10:54:07 -0600451
Simon Glass88a9ec22022-01-29 14:14:13 -0700452 def test_prefer_if(self):
Simon Glass50137da2020-07-19 10:16:00 -0600453 """Test for using #ifdef"""
Simon Glasscc00e492020-06-14 10:54:07 -0600454 pm = PatchMaker()
455 pm.add_line('common/main.c', '#ifdef CONFIG_YELLOW')
Simon Glass2ec1ed22020-06-14 10:54:08 -0600456 pm.add_line('common/init.h', '#ifdef CONFIG_YELLOW')
457 pm.add_line('fred.dtsi', '#ifdef CONFIG_YELLOW')
Simon Glass88a9ec22022-01-29 14:14:13 -0700458 self.check_single_message(pm, "PREFER_IF")
Simon Glasscc00e492020-06-14 10:54:07 -0600459
Simon Glass88a9ec22022-01-29 14:14:13 -0700460 def test_command_use_defconfig(self):
Simon Glass50137da2020-07-19 10:16:00 -0600461 """Test for enabling/disabling commands using preprocesor"""
Simon Glasscc00e492020-06-14 10:54:07 -0600462 pm = PatchMaker()
463 pm.add_line('common/main.c', '#undef CONFIG_CMD_WHICH')
Tom Rini14f8ef62022-12-04 10:14:14 -0500464 self.check_single_message(pm, 'DEFINE_CONFIG_SYM', 'error')
Simon Glassf5dd9062020-06-14 10:54:05 -0600465
Simon Glass88a9ec22022-01-29 14:14:13 -0700466 def test_barred_include_in_hdr(self):
Simon Glassf38051e2020-07-19 10:16:01 -0600467 """Test for using a barred include in a header file"""
468 pm = PatchMaker()
Simon Glassf38051e2020-07-19 10:16:01 -0600469 pm.add_line('include/myfile.h', '#include <dm.h>')
Simon Glass88a9ec22022-01-29 14:14:13 -0700470 self.check_single_message(pm, 'BARRED_INCLUDE_IN_HDR', 'error')
Simon Glassf38051e2020-07-19 10:16:01 -0600471
Tom Rinib80e21b2023-10-13 09:28:32 -0700472 def test_barred_include_common_h(self):
473 """Test for adding common.h to a file"""
474 pm = PatchMaker()
475 pm.add_line('include/myfile.h', '#include <common.h>')
476 self.check_single_message(pm, 'BARRED_INCLUDE_COMMON_H', 'error')
477
Simon Glass88a9ec22022-01-29 14:14:13 -0700478 def test_config_is_enabled_config(self):
Alper Nebi Yasak7c1a3022020-10-05 09:57:30 +0300479 """Test for accidental CONFIG_IS_ENABLED(CONFIG_*) calls"""
480 pm = PatchMaker()
481 pm.add_line('common/main.c', 'if (CONFIG_IS_ENABLED(CONFIG_CLK))')
Simon Glass88a9ec22022-01-29 14:14:13 -0700482 self.check_single_message(pm, 'CONFIG_IS_ENABLED_CONFIG', 'error')
Alper Nebi Yasak7c1a3022020-10-05 09:57:30 +0300483
Simon Glass2d848112020-12-03 16:55:24 -0700484 def check_struct(self, auto, suffix, warning):
485 """Check one of the warnings for struct naming
486
487 Args:
488 auto: Auto variable name, e.g. 'per_child_auto'
489 suffix: Suffix to expect on member, e.g. '_priv'
490 warning: Warning name, e.g. 'PRIV_AUTO'
491 """
492 pm = PatchMaker()
493 pm.add_line('common/main.c', '.%s = sizeof(struct(fred)),' % auto)
494 pm.add_line('common/main.c', '.%s = sizeof(struct(mary%s)),' %
495 (auto, suffix))
Simon Glass88a9ec22022-01-29 14:14:13 -0700496 self.check_single_message(
Simon Glass2d848112020-12-03 16:55:24 -0700497 pm, warning, "struct 'fred' should have a %s suffix" % suffix)
498
Simon Glass88a9ec22022-01-29 14:14:13 -0700499 def test_dm_driver_auto(self):
Simon Glass2d848112020-12-03 16:55:24 -0700500 """Check for the correct suffix on 'struct driver' auto members"""
501 self.check_struct('priv_auto', '_priv', 'PRIV_AUTO')
502 self.check_struct('plat_auto', '_plat', 'PLAT_AUTO')
503 self.check_struct('per_child_auto', '_priv', 'CHILD_PRIV_AUTO')
504 self.check_struct('per_child_plat_auto', '_plat', 'CHILD_PLAT_AUTO')
505
Simon Glass88a9ec22022-01-29 14:14:13 -0700506 def test_dm_uclass_auto(self):
Simon Glass2d848112020-12-03 16:55:24 -0700507 """Check for the correct suffix on 'struct uclass' auto members"""
508 # Some of these are omitted since they match those from struct driver
509 self.check_struct('per_device_auto', '_priv', 'DEVICE_PRIV_AUTO')
510 self.check_struct('per_device_plat_auto', '_plat', 'DEVICE_PLAT_AUTO')
511
Sean Anderson50fe43f2021-03-11 00:15:45 -0500512 def check_strl(self, func):
513 """Check one of the checks for strn(cpy|cat)"""
514 pm = PatchMaker()
515 pm.add_line('common/main.c', "strn%s(foo, bar, sizeof(foo));" % func)
Simon Glass88a9ec22022-01-29 14:14:13 -0700516 self.check_single_message(pm, "STRL",
Sean Anderson50fe43f2021-03-11 00:15:45 -0500517 "strl%s is preferred over strn%s because it always produces a nul-terminated string\n"
518 % (func, func))
519
Simon Glass88a9ec22022-01-29 14:14:13 -0700520 def test_strl(self):
Sean Anderson50fe43f2021-03-11 00:15:45 -0500521 """Check for uses of strn(cat|cpy)"""
522 self.check_strl("cat");
523 self.check_strl("cpy");
Simon Glass26132882012-01-14 15:12:45 +0000524
Simon Glassb4af4582023-02-13 08:56:38 -0700525 def test_schema(self):
526 """Check for uses of strn(cat|cpy)"""
527 pm = PatchMaker()
528 pm.add_line('arch/sandbox/dts/sandbox.dtsi', '\tu-boot,dm-pre-proper;')
529 self.check_single_message(pm, 'PRE_SCHEMA', 'error')
530
Simon Glass26132882012-01-14 15:12:45 +0000531if __name__ == "__main__":
532 unittest.main()
533 gitutil.RunTests()