All of lore.kernel.org
 help / color / mirror / Atom feed
From: Charalampos Mitrodimas <charmitro@posteo.net>
To: albankurti <kurti@invicto.ai>
Cc: Joe Perches <joe@perches.com>,
	 Andrew Morton <akpm@linux-foundation.org>,
	 Miguel Ojeda <ojeda@kernel.org>,
	linux-kernel@vger.kernel.org,  rust-for-linux@vger.kernel.org
Subject: Re: [PATCH 1/1 RESEND] checkpatch: add warning for pr_* and dev_* macros without a trailing newline
Date: Mon, 03 Feb 2025 21:57:11 +0000	[thread overview]
Message-ID: <m2seou3d54.fsf@posteo.net> (raw)
In-Reply-To: <20250203201149.330117-1-kurti@invicto.ai> (albankurti's message of "Mon, 03 Feb 2025 20:11:51 +0000 (UTC)")

albankurti <kurti@invicto.ai> writes:

> Add a new check in scripts/checkpatch.pl to detect usage of pr_(level)
> and dev_(level) macros (for both C and Rust) when the string literal
> does not end with '\n'. Missing trailing newlines can lead to incomplete
> log lines that do not appear properly in dmesg or in console output.
> To show an example of this working after applying the patch we can run
> the script on the commit that likely motivated this need/issue:
>   ./scripts/checkpatch.pl --strict -g f431c5c581fa176f608ba3fdebb3c1051bad5774
>
> Suggested-by: Miguel Ojeda <ojeda@kernel.org>
> Link: https://github.com/Rust-for-Linux/linux/issues/1140
> Signed-off-by: albankurti <kurti@invicto.ai>
> ---
>  scripts/checkpatch.pl | 48 +++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 48 insertions(+)
>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 9eed3683ad76..86ff666f5017 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -3888,6 +3888,54 @@ sub process {
>  			}
>  		}
>  
> +# check for pr_* and dev_* logs without a newline for C and Rust files to avoid missing log messages
> +    my $macro_pattern_c = qr{
> +        \b
> +        (
> +            pr_(?:emerg|alert|crit|err|warn|notice|info|debug)
> +          | dev_(?:emerg|alert|crit|err|warn|notice|info|dbg)
> +        )
> +        \(\s*
> +        "((?:\\.|[^"])*)"
> +        \s*
> +        (?:,|\))
> +    }x;
> +
> +    my $macro_pattern_rust = qr{
> +        \b
> +        (
> +            pr_(?:emerg|alert|crit|err|warn|notice|info|debug)
> +          | dev_(?:emerg|alert|crit|err|warn|notice|info|dbg)
> +        )
> +        !\s*
> +        \(\s*
> +        "((?:\\.|[^"])*)"
> +        \s*
> +        (?:,|\))
> +    }x;
> +
> +    if ($realfile =~ /\.(?:c|h|rs)$/) {
> +      my $is_rust = ($realfile =~ /\.rs$/);
> +      my $macro_pattern = $is_rust ? $macro_pattern_rust : $macro_pattern_c;
> +
> +      if ($line =~ /^\+/) {
> +        my $cleanline = $line;
> +        $cleanline =~ s/^[+\s]+//;
> +        $cleanline =~ s/\r?$//;
> +
> +        if ($cleanline =~ /$macro_pattern/) {
> +          my $macro_call = $1;
> +          my $string_arg = $2;
> +
> +          if ($string_arg !~ /\\n$/ && $string_arg !~ /\n$/) {
> +            my $lang = $is_rust ? "Rust" : "C";
> +            WARN("${lang}_LOG_NO_NEWLINE",
> +                 "Usage of $macro_call without a trailing newline. Consider adding '\\n'.\n" . $herecurr);
> +          }

Having a comment breaks this,

  $ ./scripts/checkpatch.pl --strict -f newlines.rs
  [...]
  WARNING: Usage of pr_err without a trailing newline. Consider adding '\n'.
  #3: FILE: newlines.rs:3:
  +pr_err!("Has newline\n"); // A comment

I believe you also have to strip out comments?

> +        }
> +      }
> +    }
> +
>  # check for .L prefix local symbols in .S files
>  		if ($realfile =~ /\.S$/ &&
>  		    $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) {

      reply	other threads:[~2025-02-03 21:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-03 20:11 [PATCH 1/1 RESEND] checkpatch: add warning for pr_* and dev_* macros without a trailing newline albankurti
2025-02-03 21:57 ` Charalampos Mitrodimas [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2seou3d54.fsf@posteo.net \
    --to=charmitro@posteo.net \
    --cc=akpm@linux-foundation.org \
    --cc=joe@perches.com \
    --cc=kurti@invicto.ai \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.