]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
checkpatch: add option to not force /* */ for SPDX
authorPetr Vorel <pvorel@suse.cz>
Tue, 21 Apr 2026 21:14:07 +0000 (23:14 +0200)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 29 May 2026 04:24:39 +0000 (21:24 -0700)
Add option --spdx-cxx-comments to not force C comments (/* */) for SPDX,
but allow also C++ comments (//).

As documented in aa19a176df95d6, this is required for some old toolchains
still have older assembler tools which cannot handle C++ style comments.
This avoids forcing this for projects which vendored checkpatch.pl (e.g.
LTP or u-boot).

Link: https://lore.kernel.org/20260421211408.383972-2-pvorel@suse.cz
Signed-off-by: Petr Vorel <pvorel@suse.cz>
Reviewed-by: Simon Glass <sjg@chromium.org>
Acked-by: Joe Perches <joe@perches.com>
Cc: Dwaipayan Ray <dwaipayanray1@gmail.com>
Cc: Lukas Bulwahn <lukas.bulwahn@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Documentation/dev-tools/checkpatch.rst
scripts/checkpatch.pl

index 010dfcd615afcbabc18e6add3abeee2f004e67d5..6139a08c34cd8d793b7080cf0bd452d54297683d 100644 (file)
@@ -184,6 +184,13 @@ Available options:
    Override checking of perl version.  Runtime errors may be encountered after
    enabling this flag if the perl version does not meet the minimum specified.
 
+ - --spdx-cxx-comments
+
+   Don't force C comments ``/* */`` for SPDX license (required by old
+   toolchains), allow also C++ comments ``//``.
+
+   NOTE: it should *not* be used for Linux mainline.
+
  - --codespell
 
    Use the codespell dictionary for checking spelling errors.
index 8b44b3a6a4dd3a4a1becc7decc49feec8f97413a..0d18771f1b01390087329fafc56a2e21bffcccbd 100755 (executable)
@@ -62,6 +62,7 @@ my $def_configuration_dirs_help = '.:$HOME:.scripts';
 my $env_config_dir = 'CHECKPATCH_CONFIG_DIR';
 my $max_line_length = 100;
 my $ignore_perl_version = 0;
+my $spdx_cxx_comments = 0;
 my $minimum_perl_version = 5.10.0;
 my $min_conf_desc_length = 4;
 my $spelling_file = "$D/spelling.txt";
@@ -138,6 +139,10 @@ Options:
                              file.  It's your fault if there's no backup or git
   --ignore-perl-version      override checking of perl version.  expect
                              runtime errors.
+  --spdx-cxx-comments        don't force C comments (/* */) for SPDX license
+                             (required by old toolchains), allow also C++
+                             comments (//).
+                             NOTE: it should *not* be used for Linux mainline.
   --codespell                Use the codespell dictionary for spelling/typos
                              (default:$codespellfile)
   --codespellfile            Use this codespell dictionary
@@ -347,6 +352,7 @@ GetOptions(
        'fix!'          => \$fix,
        'fix-inplace!'  => \$fix_inplace,
        'ignore-perl-version!' => \$ignore_perl_version,
+       'spdx-cxx-comments!' => \$spdx_cxx_comments,
        'debug=s'       => \%debug,
        'test-only=s'   => \$tst_only,
        'codespell!'    => \$codespell,
@@ -3815,26 +3821,33 @@ sub process {
                                $checklicenseline = 2;
                        } elsif ($rawline =~ /^\+/) {
                                my $comment = "";
-                               if ($realfile =~ /\.(h|s|S)$/) {
-                                       $comment = '/*';
-                               } elsif ($realfile =~ /\.(c|rs|dts|dtsi)$/) {
+                               if ($realfile =~ /\.(c|rs|dts|dtsi)$/) {
                                        $comment = '//';
                                } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
                                        $comment = '#';
                                } elsif ($realfile =~ /\.rst$/) {
                                        $comment = '..';
                                }
+                               my $pattern = qr{\Q$comment\E};
+                               if ($realfile =~ /\.(h|s|S)$/) {
+                                       $comment = '/*';
+                                       $pattern = qr{/\*};
+                                       if ($spdx_cxx_comments) {
+                                               $comment = '// or /*';
+                                               $pattern = qr{//|/\*};
+                                       }
+                               }
 
 # check SPDX comment style for .[chsS] files
                                if ($realfile =~ /\.[chsS]$/ &&
                                    $rawline =~ /SPDX-License-Identifier:/ &&
-                                   $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
+                                   $rawline !~ m@^\+\s*$pattern\s*@) {
                                        WARN("SPDX_LICENSE_TAG",
                                             "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
                                }
 
                                if ($comment !~ /^$/ &&
-                                   $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
+                                   $rawline !~ m@^\+$pattern SPDX-License-Identifier: @) {
                                        WARN("SPDX_LICENSE_TAG",
                                             "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
                                } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {