Drop newline at the end of a message string when the printing function adds a newline.
The complete semantic patch that detects this issue is as shown below (http://coccinelle.lip6.fr/). It works in two phases - the first phase counts how many uses of a function involve a newline and how many don't, and then the second phase removes newlines in the case of calls where a newline is used one fourth of the times or less.
This approach is only moderately reliable, and all patches have been checked to ensure that the newline is not needed.
This also converts some cases of string concatenation to single strings in modified code, as this improves greppability.
// <smpl> virtual after_start
@initialize:ocaml@ @@
let withnl = Hashtbl.create 101 let withoutnl = Hashtbl.create 101
let ignore = ["strcpy";"strlcpy";"strcat";"strlcat";"strcmp";"strncmp";"strcspn"; "strsep";"sprintf";"printf";"strncasecmp";"seq_printf";"strstr";"strspn"; "strlen";"strpbrk";"strtok_r";"memcmp";"memcpy"]
let dignore = ["tools";"samples"]
let inc tbl k = let cell = try Hashtbl.find tbl k with Not_found -> let cell = ref 0 in Hashtbl.add tbl k cell; cell in cell := 1 + !cell
let endnl c = let len = String.length c in try String.get c (len-3) = '\' && String.get c (len-2) = 'n' && String.get c (len-1) = '"' with _ -> false
let clean_string s extra = let pieces = Str.split (Str.regexp "" "") s in let nonempty s = not (s = "") && String.get s 0 = '"' && not (String.get s 1 = '"') in let rec loop = function [] -> [] | [x] -> [x] | x::y::rest -> if nonempty x && nonempty y then let xend = String.get x (String.length x - 2) = ' ' in let yend = String.get y 1 = ' ' in match (xend,yend) with (true,false) | (false,true) -> x :: (loop (y::rest)) | (true,true) -> x :: (loop (((String.sub y 0 (String.length y - 2))^""")::rest)) | (false,false) -> ((String.sub x 0 (String.length x - 1)) ^ " "") :: (loop (y::rest)) else x :: (loop (y::rest)) in (String.concat "" (loop pieces))^extra
@r depends on !after_start@ constant char[] c; expression list[n] es; identifier f; position p; @@
f@p(es,c,...)
@script:ocaml@ f << r.f; n << r.n; p << r.p; c << r.c; @@
let pieces = Str.split (Str.regexp "/") (List.hd p).file in if not (List.mem f ignore) && List.for_all (fun x -> not (List.mem x pieces)) dignore then (if endnl c then inc withnl (f,n) else inc withoutnl (f,n))
@finalize:ocaml depends on !after_start@ w1 << merge.withnl; w2 << merge.withoutnl; @@
let names = ref [] in let incn tbl k v = let cell = try Hashtbl.find tbl k with Not_found -> begin let cell = ref 0 in Hashtbl.add tbl k cell; cell end in (if not (List.mem k !names) then names := k :: !names); cell := !v + !cell in List.iter (function w -> Hashtbl.iter (incn withnl) w) w1; List.iter (function w -> Hashtbl.iter (incn withoutnl) w) w2;
List.iter (function name -> let wth = try !(Hashtbl.find withnl name) with _ -> 0 in let wo = try !(Hashtbl.find withoutnl name) with _ -> 0 in if wth > 0 && wth <= wo / 3 then Hashtbl.remove withnl name else (Printf.eprintf "dropping %s %d %d\n" (fst name) wth wo; Hashtbl.remove withoutnl name; Hashtbl.remove withnl name)) !names;
let it = new iteration() in it#add_virtual_rule After_start; it#register()
@s1 depends on after_start@ constant char[] c; expression list[n] es; identifier f; position p; @@
f(es,c@p,...)
@script:ocaml s2@ f << s1.f; n << s1.n; c << s1.c; newc; @@
try let _ = Hashtbl.find withnl (f,n) in if endnl c then Coccilib.include_match false else newc := make_expr(clean_string (String.sub c 0 (String.length c - 1)) "\n"") with Not_found -> try let _ = Hashtbl.find withoutnl (f,n) in if endnl c then newc := make_expr(clean_string (String.sub c 0 (String.length c - 3)) """) else Coccilib.include_match false with Not_found -> Coccilib.include_match false
@@ constant char[] s1.c; position s1.p; expression s2.newc; @@
- c@p + newc // </smpl>
---
arch/arm/mach-davinci/board-da850-evm.c | 4 ++-- drivers/block/DAC960.c | 4 ++-- drivers/gpu/drm/amd/powerplay/hwmgr/smu7_hwmgr.c | 12 ++++++++---- drivers/gpu/drm/amd/powerplay/smumgr/fiji_smumgr.c | 2 +- drivers/gpu/drm/amd/powerplay/smumgr/iceland_smumgr.c | 2 +- drivers/gpu/drm/amd/powerplay/smumgr/polaris10_smumgr.c | 2 +- drivers/gpu/drm/amd/powerplay/smumgr/tonga_smumgr.c | 2 +- drivers/media/usb/pvrusb2/pvrusb2-hdw.c | 3 ++- drivers/s390/block/dasd_diag.c | 3 +-- drivers/scsi/hpsa.c | 2 +- fs/dlm/plock.c | 3 +-- fs/ext2/super.c | 2 +- fs/hpfs/dnode.c | 3 ++- net/dccp/ackvec.c | 2 +- net/openvswitch/conntrack.c | 4 ++-- tools/perf/tests/dso-data.c | 9 +++++---- 16 files changed, 32 insertions(+), 27 deletions(-)
PP_ASSERT_WITH_CODE prints a newline at the end of the message string, so the message string does not need to include a newline explicitly. Done using Coccinelle.
Signed-off-by: Julia Lawall Julia.Lawall@lip6.fr
---
I couldn't figure out how to configure the kernel to get any of this code to compile.
drivers/gpu/drm/amd/powerplay/hwmgr/smu7_hwmgr.c | 12 ++++++++---- drivers/gpu/drm/amd/powerplay/smumgr/fiji_smumgr.c | 2 +- drivers/gpu/drm/amd/powerplay/smumgr/iceland_smumgr.c | 2 +- drivers/gpu/drm/amd/powerplay/smumgr/polaris10_smumgr.c | 2 +- drivers/gpu/drm/amd/powerplay/smumgr/tonga_smumgr.c | 2 +- 5 files changed, 12 insertions(+), 8 deletions(-)
diff --git a/drivers/gpu/drm/amd/powerplay/hwmgr/smu7_hwmgr.c b/drivers/gpu/drm/amd/powerplay/hwmgr/smu7_hwmgr.c index 40adc85..8d7fd06 100644 --- a/drivers/gpu/drm/amd/powerplay/hwmgr/smu7_hwmgr.c +++ b/drivers/gpu/drm/amd/powerplay/hwmgr/smu7_hwmgr.c @@ -2266,14 +2266,18 @@ static int smu7_set_private_data_based_on_pptable_v0(struct pp_hwmgr *hwmgr) struct phm_clock_voltage_dependency_table *allowed_mclk_vddci_table = hwmgr->dyn_state.vddci_dependency_on_mclk;
PP_ASSERT_WITH_CODE(allowed_sclk_vddc_table != NULL, - "VDDC dependency on SCLK table is missing. This table is mandatory\n", return -EINVAL); + "VDDC dependency on SCLK table is missing. This table is mandatory", + return -EINVAL); PP_ASSERT_WITH_CODE(allowed_sclk_vddc_table->count >= 1, - "VDDC dependency on SCLK table has to have is missing. This table is mandatory\n", return -EINVAL); + "VDDC dependency on SCLK table has to have is missing. This table is mandatory", + return -EINVAL);
PP_ASSERT_WITH_CODE(allowed_mclk_vddc_table != NULL, - "VDDC dependency on MCLK table is missing. This table is mandatory\n", return -EINVAL); + "VDDC dependency on MCLK table is missing. This table is mandatory", + return -EINVAL); PP_ASSERT_WITH_CODE(allowed_mclk_vddc_table->count >= 1, - "VDD dependency on MCLK table has to have is missing. This table is mandatory\n", return -EINVAL); + "VDD dependency on MCLK table has to have is missing. This table is mandatory", + return -EINVAL);
data->min_vddc_in_pptable = (uint16_t)allowed_sclk_vddc_table->entries[0].v; data->max_vddc_in_pptable = (uint16_t)allowed_sclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].v; diff --git a/drivers/gpu/drm/amd/powerplay/smumgr/fiji_smumgr.c b/drivers/gpu/drm/amd/powerplay/smumgr/fiji_smumgr.c index 085d81c..427daa6 100644 --- a/drivers/gpu/drm/amd/powerplay/smumgr/fiji_smumgr.c +++ b/drivers/gpu/drm/amd/powerplay/smumgr/fiji_smumgr.c @@ -1799,7 +1799,7 @@ static int fiji_populate_clock_stretcher_data_table(struct pp_hwmgr *hwmgr) phm_cap_unset(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_ClockStretcher); PP_ASSERT_WITH_CODE(false, - "Stretch Amount in PPTable not supported\n", + "Stretch Amount in PPTable not supported", return -EINVAL); }
diff --git a/drivers/gpu/drm/amd/powerplay/smumgr/iceland_smumgr.c b/drivers/gpu/drm/amd/powerplay/smumgr/iceland_smumgr.c index 1253126..6400065 100644 --- a/drivers/gpu/drm/amd/powerplay/smumgr/iceland_smumgr.c +++ b/drivers/gpu/drm/amd/powerplay/smumgr/iceland_smumgr.c @@ -546,7 +546,7 @@ static int iceland_get_std_voltage_value_sidd(struct pp_hwmgr *hwmgr,
/* SCLK/VDDC Dependency Table has to exist. */ PP_ASSERT_WITH_CODE(NULL != hwmgr->dyn_state.vddc_dependency_on_sclk, - "The SCLK/VDDC Dependency Table does not exist.\n", + "The SCLK/VDDC Dependency Table does not exist.", return -EINVAL);
if (NULL == hwmgr->dyn_state.cac_leakage_table) { diff --git a/drivers/gpu/drm/amd/powerplay/smumgr/polaris10_smumgr.c b/drivers/gpu/drm/amd/powerplay/smumgr/polaris10_smumgr.c index cdb4765..fd874f7 100644 --- a/drivers/gpu/drm/amd/powerplay/smumgr/polaris10_smumgr.c +++ b/drivers/gpu/drm/amd/powerplay/smumgr/polaris10_smumgr.c @@ -1652,7 +1652,7 @@ static int polaris10_populate_clock_stretcher_data_table(struct pp_hwmgr *hwmgr) phm_cap_unset(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_ClockStretcher); PP_ASSERT_WITH_CODE(false, - "Stretch Amount in PPTable not supported\n", + "Stretch Amount in PPTable not supported", return -EINVAL); }
diff --git a/drivers/gpu/drm/amd/powerplay/smumgr/tonga_smumgr.c b/drivers/gpu/drm/amd/powerplay/smumgr/tonga_smumgr.c index 79e5c05..5eb719e 100644 --- a/drivers/gpu/drm/amd/powerplay/smumgr/tonga_smumgr.c +++ b/drivers/gpu/drm/amd/powerplay/smumgr/tonga_smumgr.c @@ -1699,7 +1699,7 @@ static int tonga_populate_clock_stretcher_data_table(struct pp_hwmgr *hwmgr) phm_cap_unset(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_ClockStretcher); PP_ASSERT_WITH_CODE(false, - "Stretch Amount in PPTable not supported\n", + "Stretch Amount in PPTable not supported", return -EINVAL); }
On 2017-12-27 03:51 PM, Julia Lawall wrote:
Just enabling CONFIG_DRM_AMDGPU should be enough AFAICT.
----- Original Message ----- | Drop newline at the end of a message string when the printing function adds | a newline.
Hi Julia,
NACK.
As much as it's a pain when searching the source code for output strings, this patch set goes against the accepted Linux coding style document. See:
https://www.kernel.org/doc/html/v4.10/process/coding-style.html#breaking-lon...
Regards,
Bob Peterson
----- Original Message ----- | ----- Original Message ----- | | Drop newline at the end of a message string when the printing function adds | | a newline. | | Hi Julia, | | NACK. | | As much as it's a pain when searching the source code for output strings, | this patch set goes against the accepted Linux coding style document. See: | | https://www.kernel.org/doc/html/v4.10/process/coding-style.html#breaking-lon... | | Regards, | | Bob Peterson | | Hm. I guess I stand corrected. The document reads:
"However, never break user-visible strings such as printk messages, because that breaks the ability to grep for them."
Still, the GFS2 and DLM code has a plethora of broken-up printk messages, and I don't like the thought of re-combining them all.
Regards,
Bob Peterson
On Tue, 2 Jan 2018, Bob Peterson wrote:
Actually, the point of the patch was to remove the unnecessary \n at the end of the string, because log_print will add another one. If you prefer to keep the string broken up, I can resend the patch in that form, but without the unnecessary \n.
julia
On Tue, 2018-01-02 at 15:00 +0100, Julia Lawall wrote:
Please combine any user-visible strings into a single line for which the unneeded newline is dropped since these strings are modified anyway by your patch.
Thanks,
Bart.
dri-devel@lists.freedesktop.org