checkpatch.pl 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977
  1. #!/usr/bin/perl -w
  2. # (c) 2001, Dave Jones. (the file handling bit)
  3. # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
  4. # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
  5. # (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
  6. # Licensed under the terms of the GNU GPL License version 2
  7. use strict;
  8. my $P = $0;
  9. $P =~ s@.*/@@g;
  10. my $V = '0.31';
  11. use Getopt::Long qw(:config no_auto_abbrev);
  12. my $quiet = 0;
  13. my $tree = 1;
  14. my $chk_signoff = 1;
  15. my $chk_patch = 1;
  16. my $tst_only;
  17. my $emacs = 0;
  18. my $terse = 0;
  19. my $file = 0;
  20. my $check = 0;
  21. my $summary = 1;
  22. my $mailback = 0;
  23. my $summary_file = 0;
  24. my $root;
  25. my %debug;
  26. my $help = 0;
  27. sub help {
  28. my ($exitcode) = @_;
  29. print << "EOM";
  30. Usage: $P [OPTION]... [FILE]...
  31. Version: $V
  32. Options:
  33. -q, --quiet quiet
  34. --no-tree run without a kernel tree
  35. --no-signoff do not check for 'Signed-off-by' line
  36. --patch treat FILE as patchfile (default)
  37. --emacs emacs compile window format
  38. --terse one line per report
  39. -f, --file treat FILE as regular source file
  40. --subjective, --strict enable more subjective tests
  41. --root=PATH PATH to the kernel tree root
  42. --no-summary suppress the per-file summary
  43. --mailback only produce a report in case of warnings/errors
  44. --summary-file include the filename in summary
  45. --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
  46. 'values', 'possible', 'type', and 'attr' (default
  47. is all off)
  48. --test-only=WORD report only warnings/errors containing WORD
  49. literally
  50. -h, --help, --version display this help and exit
  51. When FILE is - read standard input.
  52. EOM
  53. exit($exitcode);
  54. }
  55. GetOptions(
  56. 'q|quiet+' => \$quiet,
  57. 'tree!' => \$tree,
  58. 'signoff!' => \$chk_signoff,
  59. 'patch!' => \$chk_patch,
  60. 'emacs!' => \$emacs,
  61. 'terse!' => \$terse,
  62. 'f|file!' => \$file,
  63. 'subjective!' => \$check,
  64. 'strict!' => \$check,
  65. 'root=s' => \$root,
  66. 'summary!' => \$summary,
  67. 'mailback!' => \$mailback,
  68. 'summary-file!' => \$summary_file,
  69. 'debug=s' => \%debug,
  70. 'test-only=s' => \$tst_only,
  71. 'h|help' => \$help,
  72. 'version' => \$help
  73. ) or help(1);
  74. help(0) if ($help);
  75. my $exit = 0;
  76. if ($#ARGV < 0) {
  77. print "$P: no input files\n";
  78. exit(1);
  79. }
  80. my $dbg_values = 0;
  81. my $dbg_possible = 0;
  82. my $dbg_type = 0;
  83. my $dbg_attr = 0;
  84. for my $key (keys %debug) {
  85. ## no critic
  86. eval "\${dbg_$key} = '$debug{$key}';";
  87. die "$@" if ($@);
  88. }
  89. my $rpt_cleaners = 0;
  90. if ($terse) {
  91. $emacs = 1;
  92. $quiet++;
  93. }
  94. if ($tree) {
  95. if (defined $root) {
  96. if (!top_of_kernel_tree($root)) {
  97. die "$P: $root: --root does not point at a valid tree\n";
  98. }
  99. } else {
  100. if (top_of_kernel_tree('.')) {
  101. $root = '.';
  102. } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
  103. top_of_kernel_tree($1)) {
  104. $root = $1;
  105. }
  106. }
  107. if (!defined $root) {
  108. print "Must be run from the top-level dir. of a kernel tree\n";
  109. exit(2);
  110. }
  111. }
  112. my $emitted_corrupt = 0;
  113. our $Ident = qr{
  114. [A-Za-z_][A-Za-z\d_]*
  115. (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
  116. }x;
  117. our $Storage = qr{extern|static|asmlinkage};
  118. our $Sparse = qr{
  119. __user|
  120. __kernel|
  121. __force|
  122. __iomem|
  123. __must_check|
  124. __init_refok|
  125. __kprobes|
  126. __ref
  127. }x;
  128. # Notes to $Attribute:
  129. # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
  130. our $Attribute = qr{
  131. const|
  132. __percpu|
  133. __nocast|
  134. __safe|
  135. __bitwise__|
  136. __packed__|
  137. __packed2__|
  138. __naked|
  139. __maybe_unused|
  140. __always_unused|
  141. __noreturn|
  142. __used|
  143. __cold|
  144. __noclone|
  145. __deprecated|
  146. __read_mostly|
  147. __kprobes|
  148. __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
  149. ____cacheline_aligned|
  150. ____cacheline_aligned_in_smp|
  151. ____cacheline_internodealigned_in_smp|
  152. __weak
  153. }x;
  154. our $Modifier;
  155. our $Inline = qr{inline|__always_inline|noinline};
  156. our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
  157. our $Lval = qr{$Ident(?:$Member)*};
  158. our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
  159. our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
  160. our $Compare = qr{<=|>=|==|!=|<|>};
  161. our $Operators = qr{
  162. <=|>=|==|!=|
  163. =>|->|<<|>>|<|>|!|~|
  164. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
  165. }x;
  166. our $NonptrType;
  167. our $Type;
  168. our $Declare;
  169. our $UTF8 = qr {
  170. [\x09\x0A\x0D\x20-\x7E] # ASCII
  171. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  172. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  173. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  174. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  175. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  176. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  177. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  178. }x;
  179. our $typeTypedefs = qr{(?x:
  180. (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
  181. atomic_t
  182. )};
  183. our $logFunctions = qr{(?x:
  184. printk|
  185. [a-z]+_(emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)|
  186. WARN|
  187. panic|
  188. MODULE_[A-Z_]+
  189. )};
  190. our @typeList = (
  191. qr{void},
  192. qr{(?:unsigned\s+)?char},
  193. qr{(?:unsigned\s+)?short},
  194. qr{(?:unsigned\s+)?int},
  195. qr{(?:unsigned\s+)?long},
  196. qr{(?:unsigned\s+)?long\s+int},
  197. qr{(?:unsigned\s+)?long\s+long},
  198. qr{(?:unsigned\s+)?long\s+long\s+int},
  199. qr{unsigned},
  200. qr{float},
  201. qr{double},
  202. qr{bool},
  203. qr{struct\s+$Ident},
  204. qr{union\s+$Ident},
  205. qr{enum\s+$Ident},
  206. qr{${Ident}_t},
  207. qr{${Ident}_handler},
  208. qr{${Ident}_handler_fn},
  209. );
  210. our @modifierList = (
  211. qr{fastcall},
  212. );
  213. our $allowed_asm_includes = qr{(?x:
  214. irq|
  215. memory
  216. )};
  217. # memory.h: ARM has a custom one
  218. sub build_types {
  219. my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
  220. my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
  221. $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
  222. $NonptrType = qr{
  223. (?:$Modifier\s+|const\s+)*
  224. (?:
  225. (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
  226. (?:$typeTypedefs\b)|
  227. (?:${all}\b)
  228. )
  229. (?:\s+$Modifier|\s+const)*
  230. }x;
  231. $Type = qr{
  232. $NonptrType
  233. (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
  234. (?:\s+$Inline|\s+$Modifier)*
  235. }x;
  236. $Declare = qr{(?:$Storage\s+)?$Type};
  237. }
  238. build_types();
  239. $chk_signoff = 0 if ($file);
  240. my @dep_includes = ();
  241. my @dep_functions = ();
  242. my $removal = "Documentation/feature-removal-schedule.txt";
  243. if ($tree && -f "$root/$removal") {
  244. open(my $REMOVE, '<', "$root/$removal") ||
  245. die "$P: $removal: open failed - $!\n";
  246. while (<$REMOVE>) {
  247. if (/^Check:\s+(.*\S)/) {
  248. for my $entry (split(/[, ]+/, $1)) {
  249. if ($entry =~ m@include/(.*)@) {
  250. push(@dep_includes, $1);
  251. } elsif ($entry !~ m@/@) {
  252. push(@dep_functions, $entry);
  253. }
  254. }
  255. }
  256. }
  257. close($REMOVE);
  258. }
  259. my @rawlines = ();
  260. my @lines = ();
  261. my $vname;
  262. for my $filename (@ARGV) {
  263. my $FILE;
  264. if ($file) {
  265. open($FILE, '-|', "diff -u /dev/null $filename") ||
  266. die "$P: $filename: diff failed - $!\n";
  267. } elsif ($filename eq '-') {
  268. open($FILE, '<&STDIN');
  269. } else {
  270. open($FILE, '<', "$filename") ||
  271. die "$P: $filename: open failed - $!\n";
  272. }
  273. if ($filename eq '-') {
  274. $vname = 'Your patch';
  275. } else {
  276. $vname = $filename;
  277. }
  278. while (<$FILE>) {
  279. chomp;
  280. push(@rawlines, $_);
  281. }
  282. close($FILE);
  283. if (!process($filename)) {
  284. $exit = 1;
  285. }
  286. @rawlines = ();
  287. @lines = ();
  288. }
  289. exit($exit);
  290. sub top_of_kernel_tree {
  291. my ($root) = @_;
  292. my @tree_check = (
  293. "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
  294. "README", "Documentation", "arch", "include", "drivers",
  295. "fs", "init", "ipc", "kernel", "lib", "scripts",
  296. );
  297. foreach my $check (@tree_check) {
  298. if (! -e $root . '/' . $check) {
  299. return 0;
  300. }
  301. }
  302. return 1;
  303. }
  304. sub expand_tabs {
  305. my ($str) = @_;
  306. my $res = '';
  307. my $n = 0;
  308. for my $c (split(//, $str)) {
  309. if ($c eq "\t") {
  310. $res .= ' ';
  311. $n++;
  312. for (; ($n % 8) != 0; $n++) {
  313. $res .= ' ';
  314. }
  315. next;
  316. }
  317. $res .= $c;
  318. $n++;
  319. }
  320. return $res;
  321. }
  322. sub copy_spacing {
  323. (my $res = shift) =~ tr/\t/ /c;
  324. return $res;
  325. }
  326. sub line_stats {
  327. my ($line) = @_;
  328. # Drop the diff line leader and expand tabs
  329. $line =~ s/^.//;
  330. $line = expand_tabs($line);
  331. # Pick the indent from the front of the line.
  332. my ($white) = ($line =~ /^(\s*)/);
  333. return (length($line), length($white));
  334. }
  335. my $sanitise_quote = '';
  336. sub sanitise_line_reset {
  337. my ($in_comment) = @_;
  338. if ($in_comment) {
  339. $sanitise_quote = '*/';
  340. } else {
  341. $sanitise_quote = '';
  342. }
  343. }
  344. sub sanitise_line {
  345. my ($line) = @_;
  346. my $res = '';
  347. my $l = '';
  348. my $qlen = 0;
  349. my $off = 0;
  350. my $c;
  351. # Always copy over the diff marker.
  352. $res = substr($line, 0, 1);
  353. for ($off = 1; $off < length($line); $off++) {
  354. $c = substr($line, $off, 1);
  355. # Comments we are wacking completly including the begin
  356. # and end, all to $;.
  357. if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
  358. $sanitise_quote = '*/';
  359. substr($res, $off, 2, "$;$;");
  360. $off++;
  361. next;
  362. }
  363. if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
  364. $sanitise_quote = '';
  365. substr($res, $off, 2, "$;$;");
  366. $off++;
  367. next;
  368. }
  369. if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
  370. $sanitise_quote = '//';
  371. substr($res, $off, 2, $sanitise_quote);
  372. $off++;
  373. next;
  374. }
  375. # A \ in a string means ignore the next character.
  376. if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
  377. $c eq "\\") {
  378. substr($res, $off, 2, 'XX');
  379. $off++;
  380. next;
  381. }
  382. # Regular quotes.
  383. if ($c eq "'" || $c eq '"') {
  384. if ($sanitise_quote eq '') {
  385. $sanitise_quote = $c;
  386. substr($res, $off, 1, $c);
  387. next;
  388. } elsif ($sanitise_quote eq $c) {
  389. $sanitise_quote = '';
  390. }
  391. }
  392. #print "c<$c> SQ<$sanitise_quote>\n";
  393. if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
  394. substr($res, $off, 1, $;);
  395. } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
  396. substr($res, $off, 1, $;);
  397. } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
  398. substr($res, $off, 1, 'X');
  399. } else {
  400. substr($res, $off, 1, $c);
  401. }
  402. }
  403. if ($sanitise_quote eq '//') {
  404. $sanitise_quote = '';
  405. }
  406. # The pathname on a #include may be surrounded by '<' and '>'.
  407. if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
  408. my $clean = 'X' x length($1);
  409. $res =~ s@\<.*\>@<$clean>@;
  410. # The whole of a #error is a string.
  411. } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
  412. my $clean = 'X' x length($1);
  413. $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
  414. }
  415. return $res;
  416. }
  417. sub ctx_statement_block {
  418. my ($linenr, $remain, $off) = @_;
  419. my $line = $linenr - 1;
  420. my $blk = '';
  421. my $soff = $off;
  422. my $coff = $off - 1;
  423. my $coff_set = 0;
  424. my $loff = 0;
  425. my $type = '';
  426. my $level = 0;
  427. my @stack = ();
  428. my $p;
  429. my $c;
  430. my $len = 0;
  431. my $remainder;
  432. while (1) {
  433. @stack = (['', 0]) if ($#stack == -1);
  434. #warn "CSB: blk<$blk> remain<$remain>\n";
  435. # If we are about to drop off the end, pull in more
  436. # context.
  437. if ($off >= $len) {
  438. for (; $remain > 0; $line++) {
  439. last if (!defined $lines[$line]);
  440. next if ($lines[$line] =~ /^-/);
  441. $remain--;
  442. $loff = $len;
  443. $blk .= $lines[$line] . "\n";
  444. $len = length($blk);
  445. $line++;
  446. last;
  447. }
  448. # Bail if there is no further context.
  449. #warn "CSB: blk<$blk> off<$off> len<$len>\n";
  450. if ($off >= $len) {
  451. last;
  452. }
  453. }
  454. $p = $c;
  455. $c = substr($blk, $off, 1);
  456. $remainder = substr($blk, $off);
  457. #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
  458. # Handle nested #if/#else.
  459. if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
  460. push(@stack, [ $type, $level ]);
  461. } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
  462. ($type, $level) = @{$stack[$#stack - 1]};
  463. } elsif ($remainder =~ /^#\s*endif\b/) {
  464. ($type, $level) = @{pop(@stack)};
  465. }
  466. # Statement ends at the ';' or a close '}' at the
  467. # outermost level.
  468. if ($level == 0 && $c eq ';') {
  469. last;
  470. }
  471. # An else is really a conditional as long as its not else if
  472. if ($level == 0 && $coff_set == 0 &&
  473. (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
  474. $remainder =~ /^(else)(?:\s|{)/ &&
  475. $remainder !~ /^else\s+if\b/) {
  476. $coff = $off + length($1) - 1;
  477. $coff_set = 1;
  478. #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
  479. #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
  480. }
  481. if (($type eq '' || $type eq '(') && $c eq '(') {
  482. $level++;
  483. $type = '(';
  484. }
  485. if ($type eq '(' && $c eq ')') {
  486. $level--;
  487. $type = ($level != 0)? '(' : '';
  488. if ($level == 0 && $coff < $soff) {
  489. $coff = $off;
  490. $coff_set = 1;
  491. #warn "CSB: mark coff<$coff>\n";
  492. }
  493. }
  494. if (($type eq '' || $type eq '{') && $c eq '{') {
  495. $level++;
  496. $type = '{';
  497. }
  498. if ($type eq '{' && $c eq '}') {
  499. $level--;
  500. $type = ($level != 0)? '{' : '';
  501. if ($level == 0) {
  502. if (substr($blk, $off + 1, 1) eq ';') {
  503. $off++;
  504. }
  505. last;
  506. }
  507. }
  508. $off++;
  509. }
  510. # We are truly at the end, so shuffle to the next line.
  511. if ($off == $len) {
  512. $loff = $len + 1;
  513. $line++;
  514. $remain--;
  515. }
  516. my $statement = substr($blk, $soff, $off - $soff + 1);
  517. my $condition = substr($blk, $soff, $coff - $soff + 1);
  518. #warn "STATEMENT<$statement>\n";
  519. #warn "CONDITION<$condition>\n";
  520. #print "coff<$coff> soff<$off> loff<$loff>\n";
  521. return ($statement, $condition,
  522. $line, $remain + 1, $off - $loff + 1, $level);
  523. }
  524. sub statement_lines {
  525. my ($stmt) = @_;
  526. # Strip the diff line prefixes and rip blank lines at start and end.
  527. $stmt =~ s/(^|\n)./$1/g;
  528. $stmt =~ s/^\s*//;
  529. $stmt =~ s/\s*$//;
  530. my @stmt_lines = ($stmt =~ /\n/g);
  531. return $#stmt_lines + 2;
  532. }
  533. sub statement_rawlines {
  534. my ($stmt) = @_;
  535. my @stmt_lines = ($stmt =~ /\n/g);
  536. return $#stmt_lines + 2;
  537. }
  538. sub statement_block_size {
  539. my ($stmt) = @_;
  540. $stmt =~ s/(^|\n)./$1/g;
  541. $stmt =~ s/^\s*{//;
  542. $stmt =~ s/}\s*$//;
  543. $stmt =~ s/^\s*//;
  544. $stmt =~ s/\s*$//;
  545. my @stmt_lines = ($stmt =~ /\n/g);
  546. my @stmt_statements = ($stmt =~ /;/g);
  547. my $stmt_lines = $#stmt_lines + 2;
  548. my $stmt_statements = $#stmt_statements + 1;
  549. if ($stmt_lines > $stmt_statements) {
  550. return $stmt_lines;
  551. } else {
  552. return $stmt_statements;
  553. }
  554. }
  555. sub ctx_statement_full {
  556. my ($linenr, $remain, $off) = @_;
  557. my ($statement, $condition, $level);
  558. my (@chunks);
  559. # Grab the first conditional/block pair.
  560. ($statement, $condition, $linenr, $remain, $off, $level) =
  561. ctx_statement_block($linenr, $remain, $off);
  562. #print "F: c<$condition> s<$statement> remain<$remain>\n";
  563. push(@chunks, [ $condition, $statement ]);
  564. if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
  565. return ($level, $linenr, @chunks);
  566. }
  567. # Pull in the following conditional/block pairs and see if they
  568. # could continue the statement.
  569. for (;;) {
  570. ($statement, $condition, $linenr, $remain, $off, $level) =
  571. ctx_statement_block($linenr, $remain, $off);
  572. #print "C: c<$condition> s<$statement> remain<$remain>\n";
  573. last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
  574. #print "C: push\n";
  575. push(@chunks, [ $condition, $statement ]);
  576. }
  577. return ($level, $linenr, @chunks);
  578. }
  579. sub ctx_block_get {
  580. my ($linenr, $remain, $outer, $open, $close, $off) = @_;
  581. my $line;
  582. my $start = $linenr - 1;
  583. my $blk = '';
  584. my @o;
  585. my @c;
  586. my @res = ();
  587. my $level = 0;
  588. my @stack = ($level);
  589. for ($line = $start; $remain > 0; $line++) {
  590. next if ($rawlines[$line] =~ /^-/);
  591. $remain--;
  592. $blk .= $rawlines[$line];
  593. # Handle nested #if/#else.
  594. if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
  595. push(@stack, $level);
  596. } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
  597. $level = $stack[$#stack - 1];
  598. } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
  599. $level = pop(@stack);
  600. }
  601. foreach my $c (split(//, $lines[$line])) {
  602. ##print "C<$c>L<$level><$open$close>O<$off>\n";
  603. if ($off > 0) {
  604. $off--;
  605. next;
  606. }
  607. if ($c eq $close && $level > 0) {
  608. $level--;
  609. last if ($level == 0);
  610. } elsif ($c eq $open) {
  611. $level++;
  612. }
  613. }
  614. if (!$outer || $level <= 1) {
  615. push(@res, $rawlines[$line]);
  616. }
  617. last if ($level == 0);
  618. }
  619. return ($level, @res);
  620. }
  621. sub ctx_block_outer {
  622. my ($linenr, $remain) = @_;
  623. my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
  624. return @r;
  625. }
  626. sub ctx_block {
  627. my ($linenr, $remain) = @_;
  628. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  629. return @r;
  630. }
  631. sub ctx_statement {
  632. my ($linenr, $remain, $off) = @_;
  633. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  634. return @r;
  635. }
  636. sub ctx_block_level {
  637. my ($linenr, $remain) = @_;
  638. return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  639. }
  640. sub ctx_statement_level {
  641. my ($linenr, $remain, $off) = @_;
  642. return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  643. }
  644. sub ctx_locate_comment {
  645. my ($first_line, $end_line) = @_;
  646. # Catch a comment on the end of the line itself.
  647. my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
  648. return $current_comment if (defined $current_comment);
  649. # Look through the context and try and figure out if there is a
  650. # comment.
  651. my $in_comment = 0;
  652. $current_comment = '';
  653. for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
  654. my $line = $rawlines[$linenr - 1];
  655. #warn " $line\n";
  656. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  657. $in_comment = 1;
  658. }
  659. if ($line =~ m@/\*@) {
  660. $in_comment = 1;
  661. }
  662. if (!$in_comment && $current_comment ne '') {
  663. $current_comment = '';
  664. }
  665. $current_comment .= $line . "\n" if ($in_comment);
  666. if ($line =~ m@\*/@) {
  667. $in_comment = 0;
  668. }
  669. }
  670. chomp($current_comment);
  671. return($current_comment);
  672. }
  673. sub ctx_has_comment {
  674. my ($first_line, $end_line) = @_;
  675. my $cmt = ctx_locate_comment($first_line, $end_line);
  676. ##print "LINE: $rawlines[$end_line - 1 ]\n";
  677. ##print "CMMT: $cmt\n";
  678. return ($cmt ne '');
  679. }
  680. sub raw_line {
  681. my ($linenr, $cnt) = @_;
  682. my $offset = $linenr - 1;
  683. $cnt++;
  684. my $line;
  685. while ($cnt) {
  686. $line = $rawlines[$offset++];
  687. next if (defined($line) && $line =~ /^-/);
  688. $cnt--;
  689. }
  690. return $line;
  691. }
  692. sub cat_vet {
  693. my ($vet) = @_;
  694. my ($res, $coded);
  695. $res = '';
  696. while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
  697. $res .= $1;
  698. if ($2 ne '') {
  699. $coded = sprintf("^%c", unpack('C', $2) + 64);
  700. $res .= $coded;
  701. }
  702. }
  703. $res =~ s/$/\$/;
  704. return $res;
  705. }
  706. my $av_preprocessor = 0;
  707. my $av_pending;
  708. my @av_paren_type;
  709. my $av_pend_colon;
  710. sub annotate_reset {
  711. $av_preprocessor = 0;
  712. $av_pending = '_';
  713. @av_paren_type = ('E');
  714. $av_pend_colon = 'O';
  715. }
  716. sub annotate_values {
  717. my ($stream, $type) = @_;
  718. my $res;
  719. my $var = '_' x length($stream);
  720. my $cur = $stream;
  721. print "$stream\n" if ($dbg_values > 1);
  722. while (length($cur)) {
  723. @av_paren_type = ('E') if ($#av_paren_type < 0);
  724. print " <" . join('', @av_paren_type) .
  725. "> <$type> <$av_pending>" if ($dbg_values > 1);
  726. if ($cur =~ /^(\s+)/o) {
  727. print "WS($1)\n" if ($dbg_values > 1);
  728. if ($1 =~ /\n/ && $av_preprocessor) {
  729. $type = pop(@av_paren_type);
  730. $av_preprocessor = 0;
  731. }
  732. } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
  733. print "CAST($1)\n" if ($dbg_values > 1);
  734. push(@av_paren_type, $type);
  735. $type = 'C';
  736. } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
  737. print "DECLARE($1)\n" if ($dbg_values > 1);
  738. $type = 'T';
  739. } elsif ($cur =~ /^($Modifier)\s*/) {
  740. print "MODIFIER($1)\n" if ($dbg_values > 1);
  741. $type = 'T';
  742. } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
  743. print "DEFINE($1,$2)\n" if ($dbg_values > 1);
  744. $av_preprocessor = 1;
  745. push(@av_paren_type, $type);
  746. if ($2 ne '') {
  747. $av_pending = 'N';
  748. }
  749. $type = 'E';
  750. } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
  751. print "UNDEF($1)\n" if ($dbg_values > 1);
  752. $av_preprocessor = 1;
  753. push(@av_paren_type, $type);
  754. } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
  755. print "PRE_START($1)\n" if ($dbg_values > 1);
  756. $av_preprocessor = 1;
  757. push(@av_paren_type, $type);
  758. push(@av_paren_type, $type);
  759. $type = 'E';
  760. } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
  761. print "PRE_RESTART($1)\n" if ($dbg_values > 1);
  762. $av_preprocessor = 1;
  763. push(@av_paren_type, $av_paren_type[$#av_paren_type]);
  764. $type = 'E';
  765. } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
  766. print "PRE_END($1)\n" if ($dbg_values > 1);
  767. $av_preprocessor = 1;
  768. # Assume all arms of the conditional end as this
  769. # one does, and continue as if the #endif was not here.
  770. pop(@av_paren_type);
  771. push(@av_paren_type, $type);
  772. $type = 'E';
  773. } elsif ($cur =~ /^(\\\n)/o) {
  774. print "PRECONT($1)\n" if ($dbg_values > 1);
  775. } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
  776. print "ATTR($1)\n" if ($dbg_values > 1);
  777. $av_pending = $type;
  778. $type = 'N';
  779. } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
  780. print "SIZEOF($1)\n" if ($dbg_values > 1);
  781. if (defined $2) {
  782. $av_pending = 'V';
  783. }
  784. $type = 'N';
  785. } elsif ($cur =~ /^(if|while|for)\b/o) {
  786. print "COND($1)\n" if ($dbg_values > 1);
  787. $av_pending = 'E';
  788. $type = 'N';
  789. } elsif ($cur =~/^(case)/o) {
  790. print "CASE($1)\n" if ($dbg_values > 1);
  791. $av_pend_colon = 'C';
  792. $type = 'N';
  793. } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
  794. print "KEYWORD($1)\n" if ($dbg_values > 1);
  795. $type = 'N';
  796. } elsif ($cur =~ /^(\()/o) {
  797. print "PAREN('$1')\n" if ($dbg_values > 1);
  798. push(@av_paren_type, $av_pending);
  799. $av_pending = '_';
  800. $type = 'N';
  801. } elsif ($cur =~ /^(\))/o) {
  802. my $new_type = pop(@av_paren_type);
  803. if ($new_type ne '_') {
  804. $type = $new_type;
  805. print "PAREN('$1') -> $type\n"
  806. if ($dbg_values > 1);
  807. } else {
  808. print "PAREN('$1')\n" if ($dbg_values > 1);
  809. }
  810. } elsif ($cur =~ /^($Ident)\s*\(/o) {
  811. print "FUNC($1)\n" if ($dbg_values > 1);
  812. $type = 'V';
  813. $av_pending = 'V';
  814. } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
  815. if (defined $2 && $type eq 'C' || $type eq 'T') {
  816. $av_pend_colon = 'B';
  817. } elsif ($type eq 'E') {
  818. $av_pend_colon = 'L';
  819. }
  820. print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
  821. $type = 'V';
  822. } elsif ($cur =~ /^($Ident|$Constant)/o) {
  823. print "IDENT($1)\n" if ($dbg_values > 1);
  824. $type = 'V';
  825. } elsif ($cur =~ /^($Assignment)/o) {
  826. print "ASSIGN($1)\n" if ($dbg_values > 1);
  827. $type = 'N';
  828. } elsif ($cur =~/^(;|{|})/) {
  829. print "END($1)\n" if ($dbg_values > 1);
  830. $type = 'E';
  831. $av_pend_colon = 'O';
  832. } elsif ($cur =~/^(,)/) {
  833. print "COMMA($1)\n" if ($dbg_values > 1);
  834. $type = 'C';
  835. } elsif ($cur =~ /^(\?)/o) {
  836. print "QUESTION($1)\n" if ($dbg_values > 1);
  837. $type = 'N';
  838. } elsif ($cur =~ /^(:)/o) {
  839. print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
  840. substr($var, length($res), 1, $av_pend_colon);
  841. if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
  842. $type = 'E';
  843. } else {
  844. $type = 'N';
  845. }
  846. $av_pend_colon = 'O';
  847. } elsif ($cur =~ /^(\[)/o) {
  848. print "CLOSE($1)\n" if ($dbg_values > 1);
  849. $type = 'N';
  850. } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
  851. my $variant;
  852. print "OPV($1)\n" if ($dbg_values > 1);
  853. if ($type eq 'V') {
  854. $variant = 'B';
  855. } else {
  856. $variant = 'U';
  857. }
  858. substr($var, length($res), 1, $variant);
  859. $type = 'N';
  860. } elsif ($cur =~ /^($Operators)/o) {
  861. print "OP($1)\n" if ($dbg_values > 1);
  862. if ($1 ne '++' && $1 ne '--') {
  863. $type = 'N';
  864. }
  865. } elsif ($cur =~ /(^.)/o) {
  866. print "C($1)\n" if ($dbg_values > 1);
  867. }
  868. if (defined $1) {
  869. $cur = substr($cur, length($1));
  870. $res .= $type x length($1);
  871. }
  872. }
  873. return ($res, $var);
  874. }
  875. sub possible {
  876. my ($possible, $line) = @_;
  877. my $notPermitted = qr{(?:
  878. ^(?:
  879. $Modifier|
  880. $Storage|
  881. $Type|
  882. DEFINE_\S+
  883. )$|
  884. ^(?:
  885. goto|
  886. return|
  887. case|
  888. else|
  889. asm|__asm__|
  890. do
  891. )(?:\s|$)|
  892. ^(?:typedef|struct|enum)\b
  893. )}x;
  894. warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
  895. if ($possible !~ $notPermitted) {
  896. # Check for modifiers.
  897. $possible =~ s/\s*$Storage\s*//g;
  898. $possible =~ s/\s*$Sparse\s*//g;
  899. if ($possible =~ /^\s*$/) {
  900. } elsif ($possible =~ /\s/) {
  901. $possible =~ s/\s*$Type\s*//g;
  902. for my $modifier (split(' ', $possible)) {
  903. if ($modifier !~ $notPermitted) {
  904. warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
  905. push(@modifierList, $modifier);
  906. }
  907. }
  908. } else {
  909. warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
  910. push(@typeList, $possible);
  911. }
  912. build_types();
  913. } else {
  914. warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
  915. }
  916. }
  917. my $prefix = '';
  918. sub report {
  919. if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
  920. return 0;
  921. }
  922. my $line = $prefix . $_[0];
  923. $line = (split('\n', $line))[0] . "\n" if ($terse);
  924. push(our @report, $line);
  925. return 1;
  926. }
  927. sub report_dump {
  928. our @report;
  929. }
  930. sub ERROR {
  931. if (report("ERROR: $_[0]\n")) {
  932. our $clean = 0;
  933. our $cnt_error++;
  934. }
  935. }
  936. sub WARN {
  937. if (report("WARNING: $_[0]\n")) {
  938. our $clean = 0;
  939. our $cnt_warn++;
  940. }
  941. }
  942. sub CHK {
  943. if ($check && report("CHECK: $_[0]\n")) {
  944. our $clean = 0;
  945. our $cnt_chk++;
  946. }
  947. }
  948. sub check_absolute_file {
  949. my ($absolute, $herecurr) = @_;
  950. my $file = $absolute;
  951. ##print "absolute<$absolute>\n";
  952. # See if any suffix of this path is a path within the tree.
  953. while ($file =~ s@^[^/]*/@@) {
  954. if (-f "$root/$file") {
  955. ##print "file<$file>\n";
  956. last;
  957. }
  958. }
  959. if (! -f _) {
  960. return 0;
  961. }
  962. # It is, so see if the prefix is acceptable.
  963. my $prefix = $absolute;
  964. substr($prefix, -length($file)) = '';
  965. ##print "prefix<$prefix>\n";
  966. if ($prefix ne ".../") {
  967. WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
  968. }
  969. }
  970. sub process {
  971. my $filename = shift;
  972. my $linenr=0;
  973. my $prevline="";
  974. my $prevrawline="";
  975. my $stashline="";
  976. my $stashrawline="";
  977. my $length;
  978. my $indent;
  979. my $previndent=0;
  980. my $stashindent=0;
  981. our $clean = 1;
  982. my $signoff = 0;
  983. my $is_patch = 0;
  984. our @report = ();
  985. our $cnt_lines = 0;
  986. our $cnt_error = 0;
  987. our $cnt_warn = 0;
  988. our $cnt_chk = 0;
  989. # Trace the real file/line as we go.
  990. my $realfile = '';
  991. my $realline = 0;
  992. my $realcnt = 0;
  993. my $here = '';
  994. my $in_comment = 0;
  995. my $comment_edge = 0;
  996. my $first_line = 0;
  997. my $p1_prefix = '';
  998. my $prev_values = 'E';
  999. # suppression flags
  1000. my %suppress_ifbraces;
  1001. my %suppress_whiletrailers;
  1002. my %suppress_export;
  1003. # Pre-scan the patch sanitizing the lines.
  1004. # Pre-scan the patch looking for any __setup documentation.
  1005. #
  1006. my @setup_docs = ();
  1007. my $setup_docs = 0;
  1008. sanitise_line_reset();
  1009. my $line;
  1010. foreach my $rawline (@rawlines) {
  1011. $linenr++;
  1012. $line = $rawline;
  1013. if ($rawline=~/^\+\+\+\s+(\S+)/) {
  1014. $setup_docs = 0;
  1015. if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
  1016. $setup_docs = 1;
  1017. }
  1018. #next;
  1019. }
  1020. if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  1021. $realline=$1-1;
  1022. if (defined $2) {
  1023. $realcnt=$3+1;
  1024. } else {
  1025. $realcnt=1+1;
  1026. }
  1027. $in_comment = 0;
  1028. # Guestimate if this is a continuing comment. Run
  1029. # the context looking for a comment "edge". If this
  1030. # edge is a close comment then we must be in a comment
  1031. # at context start.
  1032. my $edge;
  1033. my $cnt = $realcnt;
  1034. for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
  1035. next if (defined $rawlines[$ln - 1] &&
  1036. $rawlines[$ln - 1] =~ /^-/);
  1037. $cnt--;
  1038. #print "RAW<$rawlines[$ln - 1]>\n";
  1039. last if (!defined $rawlines[$ln - 1]);
  1040. if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
  1041. $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
  1042. ($edge) = $1;
  1043. last;
  1044. }
  1045. }
  1046. if (defined $edge && $edge eq '*/') {
  1047. $in_comment = 1;
  1048. }
  1049. # Guestimate if this is a continuing comment. If this
  1050. # is the start of a diff block and this line starts
  1051. # ' *' then it is very likely a comment.
  1052. if (!defined $edge &&
  1053. $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
  1054. {
  1055. $in_comment = 1;
  1056. }
  1057. ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
  1058. sanitise_line_reset($in_comment);
  1059. } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
  1060. # Standardise the strings and chars within the input to
  1061. # simplify matching -- only bother with positive lines.
  1062. $line = sanitise_line($rawline);
  1063. }
  1064. push(@lines, $line);
  1065. if ($realcnt > 1) {
  1066. $realcnt-- if ($line =~ /^(?:\+| |$)/);
  1067. } else {
  1068. $realcnt = 0;
  1069. }
  1070. #print "==>$rawline\n";
  1071. #print "-->$line\n";
  1072. if ($setup_docs && $line =~ /^\+/) {
  1073. push(@setup_docs, $line);
  1074. }
  1075. }
  1076. $prefix = '';
  1077. $realcnt = 0;
  1078. $linenr = 0;
  1079. foreach my $line (@lines) {
  1080. $linenr++;
  1081. my $rawline = $rawlines[$linenr - 1];
  1082. #extract the line range in the file after the patch is applied
  1083. if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  1084. $is_patch = 1;
  1085. $first_line = $linenr + 1;
  1086. $realline=$1-1;
  1087. if (defined $2) {
  1088. $realcnt=$3+1;
  1089. } else {
  1090. $realcnt=1+1;
  1091. }
  1092. annotate_reset();
  1093. $prev_values = 'E';
  1094. %suppress_ifbraces = ();
  1095. %suppress_whiletrailers = ();
  1096. %suppress_export = ();
  1097. next;
  1098. # track the line number as we move through the hunk, note that
  1099. # new versions of GNU diff omit the leading space on completely
  1100. # blank context lines so we need to count that too.
  1101. } elsif ($line =~ /^( |\+|$)/) {
  1102. $realline++;
  1103. $realcnt-- if ($realcnt != 0);
  1104. # Measure the line length and indent.
  1105. ($length, $indent) = line_stats($rawline);
  1106. # Track the previous line.
  1107. ($prevline, $stashline) = ($stashline, $line);
  1108. ($previndent, $stashindent) = ($stashindent, $indent);
  1109. ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
  1110. #warn "line<$line>\n";
  1111. } elsif ($realcnt == 1) {
  1112. $realcnt--;
  1113. }
  1114. my $hunk_line = ($realcnt != 0);
  1115. #make up the handle for any error we report on this line
  1116. $prefix = "$filename:$realline: " if ($emacs && $file);
  1117. $prefix = "$filename:$linenr: " if ($emacs && !$file);
  1118. $here = "#$linenr: " if (!$file);
  1119. $here = "#$realline: " if ($file);
  1120. # extract the filename as it passes
  1121. if ($line =~ /^diff --git.*?(\S+)$/) {
  1122. $realfile = $1;
  1123. $realfile =~ s@^([^/]*)/@@;
  1124. } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
  1125. $realfile = $1;
  1126. $realfile =~ s@^([^/]*)/@@;
  1127. $p1_prefix = $1;
  1128. if (!$file && $tree && $p1_prefix ne '' &&
  1129. -e "$root/$p1_prefix") {
  1130. WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
  1131. }
  1132. if ($realfile =~ m@^include/asm/@) {
  1133. ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
  1134. }
  1135. next;
  1136. }
  1137. $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
  1138. my $hereline = "$here\n$rawline\n";
  1139. my $herecurr = "$here\n$rawline\n";
  1140. my $hereprev = "$here\n$prevrawline\n$rawline\n";
  1141. $cnt_lines++ if ($realcnt != 0);
  1142. # Check for incorrect file permissions
  1143. if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
  1144. my $permhere = $here . "FILE: $realfile\n";
  1145. if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
  1146. ERROR("do not set execute permissions for source files\n" . $permhere);
  1147. }
  1148. }
  1149. #check the patch for a signoff:
  1150. if ($line =~ /^\s*signed-off-by:/i) {
  1151. # This is a signoff, if ugly, so do not double report.
  1152. $signoff++;
  1153. if (!($line =~ /^\s*Signed-off-by:/)) {
  1154. WARN("Signed-off-by: is the preferred form\n" .
  1155. $herecurr);
  1156. }
  1157. if ($line =~ /^\s*signed-off-by:\S/i) {
  1158. WARN("space required after Signed-off-by:\n" .
  1159. $herecurr);
  1160. }
  1161. }
  1162. # Check for wrappage within a valid hunk of the file
  1163. if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
  1164. ERROR("patch seems to be corrupt (line wrapped?)\n" .
  1165. $herecurr) if (!$emitted_corrupt++);
  1166. }
  1167. # Check for absolute kernel paths.
  1168. if ($tree) {
  1169. while ($line =~ m{(?:^|\s)(/\S*)}g) {
  1170. my $file = $1;
  1171. if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
  1172. check_absolute_file($1, $herecurr)) {
  1173. #
  1174. } else {
  1175. check_absolute_file($file, $herecurr);
  1176. }
  1177. }
  1178. }
  1179. # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
  1180. if (($realfile =~ /^$/ || $line =~ /^\+/) &&
  1181. $rawline !~ m/^$UTF8*$/) {
  1182. my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
  1183. my $blank = copy_spacing($rawline);
  1184. my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
  1185. my $hereptr = "$hereline$ptr\n";
  1186. ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
  1187. }
  1188. # ignore non-hunk lines and lines being removed
  1189. next if (!$hunk_line || $line =~ /^-/);
  1190. #trailing whitespace
  1191. if ($line =~ /^\+.*\015/) {
  1192. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1193. ERROR("DOS line endings\n" . $herevet);
  1194. } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
  1195. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1196. ERROR("trailing whitespace\n" . $herevet);
  1197. $rpt_cleaners = 1;
  1198. }
  1199. # check for Kconfig help text having a real description
  1200. # Only applies when adding the entry originally, after that we do not have
  1201. # sufficient context to determine whether it is indeed long enough.
  1202. if ($realfile =~ /Kconfig/ &&
  1203. $line =~ /\+\s*(?:---)?help(?:---)?$/) {
  1204. my $length = 0;
  1205. my $cnt = $realcnt;
  1206. my $ln = $linenr + 1;
  1207. my $f;
  1208. my $is_end = 0;
  1209. while ($cnt > 0 && defined $lines[$ln - 1]) {
  1210. $f = $lines[$ln - 1];
  1211. $cnt-- if ($lines[$ln - 1] !~ /^-/);
  1212. $is_end = $lines[$ln - 1] =~ /^\+/;
  1213. $ln++;
  1214. next if ($f =~ /^-/);
  1215. $f =~ s/^.//;
  1216. $f =~ s/#.*//;
  1217. $f =~ s/^\s+//;
  1218. next if ($f =~ /^$/);
  1219. if ($f =~ /^\s*config\s/) {
  1220. $is_end = 1;
  1221. last;
  1222. }
  1223. $length++;
  1224. }
  1225. WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
  1226. #print "is_end<$is_end> length<$length>\n";
  1227. }
  1228. # check we are in a valid source file if not then ignore this hunk
  1229. next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
  1230. #80 column limit
  1231. if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
  1232. $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
  1233. !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
  1234. $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
  1235. $length > 80)
  1236. {
  1237. WARN("line over 80 characters\n" . $herecurr);
  1238. }
  1239. # check for spaces before a quoted newline
  1240. if ($rawline =~ /^.*\".*\s\\n/) {
  1241. WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
  1242. }
  1243. # check for adding lines without a newline.
  1244. if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
  1245. WARN("adding a line without newline at end of file\n" . $herecurr);
  1246. }
  1247. # Blackfin: use hi/lo macros
  1248. if ($realfile =~ m@arch/blackfin/.*\.S$@) {
  1249. if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
  1250. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1251. ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
  1252. }
  1253. if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
  1254. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1255. ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
  1256. }
  1257. }
  1258. # check we are in a valid source file C or perl if not then ignore this hunk
  1259. next if ($realfile !~ /\.(h|c|pl)$/);
  1260. # at the beginning of a line any tabs must come first and anything
  1261. # more than 8 must use tabs.
  1262. if ($rawline =~ /^\+\s* \t\s*\S/ ||
  1263. $rawline =~ /^\+\s* \s*/) {
  1264. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1265. ERROR("code indent should use tabs where possible\n" . $herevet);
  1266. $rpt_cleaners = 1;
  1267. }
  1268. # check for space before tabs.
  1269. if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
  1270. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1271. WARN("please, no space before tabs\n" . $herevet);
  1272. }
  1273. # check for spaces at the beginning of a line.
  1274. # Exceptions:
  1275. # 1) within comments
  1276. # 2) indented preprocessor commands
  1277. # 3) hanging labels
  1278. if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
  1279. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1280. WARN("please, no spaces at the start of a line\n" . $herevet);
  1281. }
  1282. # check we are in a valid C source file if not then ignore this hunk
  1283. next if ($realfile !~ /\.(h|c)$/);
  1284. # check for RCS/CVS revision markers
  1285. if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
  1286. WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
  1287. }
  1288. # Blackfin: don't use __builtin_bfin_[cs]sync
  1289. if ($line =~ /__builtin_bfin_csync/) {
  1290. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1291. ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
  1292. }
  1293. if ($line =~ /__builtin_bfin_ssync/) {
  1294. my $herevet = "$here\n" . cat_vet($line) . "\n";
  1295. ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
  1296. }
  1297. # Check for potential 'bare' types
  1298. my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
  1299. $realline_next);
  1300. if ($realcnt && $line =~ /.\s*\S/) {
  1301. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  1302. ctx_statement_block($linenr, $realcnt, 0);
  1303. $stat =~ s/\n./\n /g;
  1304. $cond =~ s/\n./\n /g;
  1305. # Find the real next line.
  1306. $realline_next = $line_nr_next;
  1307. if (defined $realline_next &&
  1308. (!defined $lines[$realline_next - 1] ||
  1309. substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
  1310. $realline_next++;
  1311. }
  1312. my $s = $stat;
  1313. $s =~ s/{.*$//s;
  1314. # Ignore goto labels.
  1315. if ($s =~ /$Ident:\*$/s) {
  1316. # Ignore functions being called
  1317. } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
  1318. } elsif ($s =~ /^.\s*else\b/s) {
  1319. # declarations always start with types
  1320. } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
  1321. my $type = $1;
  1322. $type =~ s/\s+/ /g;
  1323. possible($type, "A:" . $s);
  1324. # definitions in global scope can only start with types
  1325. } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
  1326. possible($1, "B:" . $s);
  1327. }
  1328. # any (foo ... *) is a pointer cast, and foo is a type
  1329. while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
  1330. possible($1, "C:" . $s);
  1331. }
  1332. # Check for any sort of function declaration.
  1333. # int foo(something bar, other baz);
  1334. # void (*store_gdt)(x86_descr_ptr *);
  1335. if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
  1336. my ($name_len) = length($1);
  1337. my $ctx = $s;
  1338. substr($ctx, 0, $name_len + 1, '');
  1339. $ctx =~ s/\)[^\)]*$//;
  1340. for my $arg (split(/\s*,\s*/, $ctx)) {
  1341. if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
  1342. possible($1, "D:" . $s);
  1343. }
  1344. }
  1345. }
  1346. }
  1347. #
  1348. # Checks which may be anchored in the context.
  1349. #
  1350. # Check for switch () and associated case and default
  1351. # statements should be at the same indent.
  1352. if ($line=~/\bswitch\s*\(.*\)/) {
  1353. my $err = '';
  1354. my $sep = '';
  1355. my @ctx = ctx_block_outer($linenr, $realcnt);
  1356. shift(@ctx);
  1357. for my $ctx (@ctx) {
  1358. my ($clen, $cindent) = line_stats($ctx);
  1359. if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  1360. $indent != $cindent) {
  1361. $err .= "$sep$ctx\n";
  1362. $sep = '';
  1363. } else {
  1364. $sep = "[...]\n";
  1365. }
  1366. }
  1367. if ($err ne '') {
  1368. ERROR("switch and case should be at the same indent\n$hereline$err");
  1369. }
  1370. }
  1371. # if/while/etc brace do not go on next line, unless defining a do while loop,
  1372. # or if that brace on the next line is for something else
  1373. if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
  1374. my $pre_ctx = "$1$2";
  1375. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
  1376. my $ctx_cnt = $realcnt - $#ctx - 1;
  1377. my $ctx = join("\n", @ctx);
  1378. my $ctx_ln = $linenr;
  1379. my $ctx_skip = $realcnt;
  1380. while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
  1381. defined $lines[$ctx_ln - 1] &&
  1382. $lines[$ctx_ln - 1] =~ /^-/)) {
  1383. ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
  1384. $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
  1385. $ctx_ln++;
  1386. }
  1387. #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
  1388. #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
  1389. if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
  1390. ERROR("that open brace { should be on the previous line\n" .
  1391. "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
  1392. }
  1393. if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
  1394. $ctx =~ /\)\s*\;\s*$/ &&
  1395. defined $lines[$ctx_ln - 1])
  1396. {
  1397. my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
  1398. if ($nindent > $indent) {
  1399. WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
  1400. "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
  1401. }
  1402. }
  1403. }
  1404. # Check relative indent for conditionals and blocks.
  1405. if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
  1406. my ($s, $c) = ($stat, $cond);
  1407. substr($s, 0, length($c), '');
  1408. # Make sure we remove the line prefixes as we have
  1409. # none on the first line, and are going to readd them
  1410. # where necessary.
  1411. $s =~ s/\n./\n/gs;
  1412. # Find out how long the conditional actually is.
  1413. my @newlines = ($c =~ /\n/gs);
  1414. my $cond_lines = 1 + $#newlines;
  1415. # We want to check the first line inside the block
  1416. # starting at the end of the conditional, so remove:
  1417. # 1) any blank line termination
  1418. # 2) any opening brace { on end of the line
  1419. # 3) any do (...) {
  1420. my $continuation = 0;
  1421. my $check = 0;
  1422. $s =~ s/^.*\bdo\b//;
  1423. $s =~ s/^\s*{//;
  1424. if ($s =~ s/^\s*\\//) {
  1425. $continuation = 1;
  1426. }
  1427. if ($s =~ s/^\s*?\n//) {
  1428. $check = 1;
  1429. $cond_lines++;
  1430. }
  1431. # Also ignore a loop construct at the end of a
  1432. # preprocessor statement.
  1433. if (($prevline =~ /^.\s*#\s*define\s/ ||
  1434. $prevline =~ /\\\s*$/) && $continuation == 0) {
  1435. $check = 0;
  1436. }
  1437. my $cond_ptr = -1;
  1438. $continuation = 0;
  1439. while ($cond_ptr != $cond_lines) {
  1440. $cond_ptr = $cond_lines;
  1441. # If we see an #else/#elif then the code
  1442. # is not linear.
  1443. if ($s =~ /^\s*\#\s*(?:else|elif)/) {
  1444. $check = 0;
  1445. }
  1446. # Ignore:
  1447. # 1) blank lines, they should be at 0,
  1448. # 2) preprocessor lines, and
  1449. # 3) labels.
  1450. if ($continuation ||
  1451. $s =~ /^\s*?\n/ ||
  1452. $s =~ /^\s*#\s*?/ ||
  1453. $s =~ /^\s*$Ident\s*:/) {
  1454. $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
  1455. if ($s =~ s/^.*?\n//) {
  1456. $cond_lines++;
  1457. }
  1458. }
  1459. }
  1460. my (undef, $sindent) = line_stats("+" . $s);
  1461. my $stat_real = raw_line($linenr, $cond_lines);
  1462. # Check if either of these lines are modified, else
  1463. # this is not this patch's fault.
  1464. if (!defined($stat_real) ||
  1465. $stat !~ /^\+/ && $stat_real !~ /^\+/) {
  1466. $check = 0;
  1467. }
  1468. if (defined($stat_real) && $cond_lines > 1) {
  1469. $stat_real = "[...]\n$stat_real";
  1470. }
  1471. #print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
  1472. if ($check && (($sindent % 8) != 0 ||
  1473. ($sindent <= $indent && $s ne ''))) {
  1474. WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
  1475. }
  1476. }
  1477. # Track the 'values' across context and added lines.
  1478. my $opline = $line; $opline =~ s/^./ /;
  1479. my ($curr_values, $curr_vars) =
  1480. annotate_values($opline . "\n", $prev_values);
  1481. $curr_values = $prev_values . $curr_values;
  1482. if ($dbg_values) {
  1483. my $outline = $opline; $outline =~ s/\t/ /g;
  1484. print "$linenr > .$outline\n";
  1485. print "$linenr > $curr_values\n";
  1486. print "$linenr > $curr_vars\n";
  1487. }
  1488. $prev_values = substr($curr_values, -1);
  1489. #ignore lines not being added
  1490. if ($line=~/^[^\+]/) {next;}
  1491. # TEST: allow direct testing of the type matcher.
  1492. if ($dbg_type) {
  1493. if ($line =~ /^.\s*$Declare\s*$/) {
  1494. ERROR("TEST: is type\n" . $herecurr);
  1495. } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
  1496. ERROR("TEST: is not type ($1 is)\n". $herecurr);
  1497. }
  1498. next;
  1499. }
  1500. # TEST: allow direct testing of the attribute matcher.
  1501. if ($dbg_attr) {
  1502. if ($line =~ /^.\s*$Modifier\s*$/) {
  1503. ERROR("TEST: is attr\n" . $herecurr);
  1504. } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
  1505. ERROR("TEST: is not attr ($1 is)\n". $herecurr);
  1506. }
  1507. next;
  1508. }
  1509. # check for initialisation to aggregates open brace on the next line
  1510. if ($line =~ /^.\s*{/ &&
  1511. $prevline =~ /(?:^|[^=])=\s*$/) {
  1512. ERROR("that open brace { should be on the previous line\n" . $hereprev);
  1513. }
  1514. #
  1515. # Checks which are anchored on the added line.
  1516. #
  1517. # check for malformed paths in #include statements (uses RAW line)
  1518. if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
  1519. my $path = $1;
  1520. if ($path =~ m{//}) {
  1521. ERROR("malformed #include filename\n" .
  1522. $herecurr);
  1523. }
  1524. }
  1525. # no C99 // comments
  1526. if ($line =~ m{//}) {
  1527. ERROR("do not use C99 // comments\n" . $herecurr);
  1528. }
  1529. # Remove C99 comments.
  1530. $line =~ s@//.*@@;
  1531. $opline =~ s@//.*@@;
  1532. # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
  1533. # the whole statement.
  1534. #print "APW <$lines[$realline_next - 1]>\n";
  1535. if (defined $realline_next &&
  1536. exists $lines[$realline_next - 1] &&
  1537. !defined $suppress_export{$realline_next} &&
  1538. ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
  1539. $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  1540. # Handle definitions which produce identifiers with
  1541. # a prefix:
  1542. # XXX(foo);
  1543. # EXPORT_SYMBOL(something_foo);
  1544. my $name = $1;
  1545. if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
  1546. $name =~ /^${Ident}_$2/) {
  1547. #print "FOO C name<$name>\n";
  1548. $suppress_export{$realline_next} = 1;
  1549. } elsif ($stat !~ /(?:
  1550. \n.}\s*$|
  1551. ^.DEFINE_$Ident\(\Q$name\E\)|
  1552. ^.DECLARE_$Ident\(\Q$name\E\)|
  1553. ^.LIST_HEAD\(\Q$name\E\)|
  1554. ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
  1555. \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
  1556. )/x) {
  1557. #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
  1558. $suppress_export{$realline_next} = 2;
  1559. } else {
  1560. $suppress_export{$realline_next} = 1;
  1561. }
  1562. }
  1563. if (!defined $suppress_export{$linenr} &&
  1564. $prevline =~ /^.\s*$/ &&
  1565. ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
  1566. $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  1567. #print "FOO B <$lines[$linenr - 1]>\n";
  1568. $suppress_export{$linenr} = 2;
  1569. }
  1570. if (defined $suppress_export{$linenr} &&
  1571. $suppress_export{$linenr} == 2) {
  1572. WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
  1573. }
  1574. # check for global initialisers.
  1575. if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
  1576. ERROR("do not initialise globals to 0 or NULL\n" .
  1577. $herecurr);
  1578. }
  1579. # check for static initialisers.
  1580. if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
  1581. ERROR("do not initialise statics to 0 or NULL\n" .
  1582. $herecurr);
  1583. }
  1584. # check for static const char * arrays.
  1585. if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
  1586. WARN("static const char * array should probably be static const char * const\n" .
  1587. $herecurr);
  1588. }
  1589. # check for static char foo[] = "bar" declarations.
  1590. if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
  1591. WARN("static char array declaration should probably be static const char\n" .
  1592. $herecurr);
  1593. }
  1594. # check for declarations of struct pci_device_id
  1595. if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
  1596. WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
  1597. }
  1598. # check for new typedefs, only function parameters and sparse annotations
  1599. # make sense.
  1600. if ($line =~ /\btypedef\s/ &&
  1601. $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
  1602. $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
  1603. $line !~ /\b$typeTypedefs\b/ &&
  1604. $line !~ /\b__bitwise(?:__|)\b/) {
  1605. WARN("do not add new typedefs\n" . $herecurr);
  1606. }
  1607. # * goes on variable not on type
  1608. # (char*[ const])
  1609. if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
  1610. my ($from, $to) = ($1, $1);
  1611. # Should start with a space.
  1612. $to =~ s/^(\S)/ $1/;
  1613. # Should not end with a space.
  1614. $to =~ s/\s+$//;
  1615. # '*'s should not have spaces between.
  1616. while ($to =~ s/\*\s+\*/\*\*/) {
  1617. }
  1618. #print "from<$from> to<$to>\n";
  1619. if ($from ne $to) {
  1620. ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
  1621. }
  1622. } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
  1623. my ($from, $to, $ident) = ($1, $1, $2);
  1624. # Should start with a space.
  1625. $to =~ s/^(\S)/ $1/;
  1626. # Should not end with a space.
  1627. $to =~ s/\s+$//;
  1628. # '*'s should not have spaces between.
  1629. while ($to =~ s/\*\s+\*/\*\*/) {
  1630. }
  1631. # Modifiers should have spaces.
  1632. $to =~ s/(\b$Modifier$)/$1 /;
  1633. #print "from<$from> to<$to> ident<$ident>\n";
  1634. if ($from ne $to && $ident !~ /^$Modifier$/) {
  1635. ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
  1636. }
  1637. }
  1638. # # no BUG() or BUG_ON()
  1639. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  1640. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  1641. # print "$herecurr";
  1642. # $clean = 0;
  1643. # }
  1644. if ($line =~ /\bLINUX_VERSION_CODE\b/) {
  1645. WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
  1646. }
  1647. # check for uses of printk_ratelimit
  1648. if ($line =~ /\bprintk_ratelimit\s*\(/) {
  1649. WARN("Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
  1650. }
  1651. # printk should use KERN_* levels. Note that follow on printk's on the
  1652. # same line do not need a level, so we use the current block context
  1653. # to try and find and validate the current printk. In summary the current
  1654. # printk includes all preceding printk's which have no newline on the end.
  1655. # we assume the first bad printk is the one to report.
  1656. if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
  1657. my $ok = 0;
  1658. for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
  1659. #print "CHECK<$lines[$ln - 1]\n";
  1660. # we have a preceding printk if it ends
  1661. # with "\n" ignore it, else it is to blame
  1662. if ($lines[$ln - 1] =~ m{\bprintk\(}) {
  1663. if ($rawlines[$ln - 1] !~ m{\\n"}) {
  1664. $ok = 1;
  1665. }
  1666. last;
  1667. }
  1668. }
  1669. if ($ok == 0) {
  1670. WARN("printk() should include KERN_ facility level\n" . $herecurr);
  1671. }
  1672. }
  1673. # function brace can't be on same line, except for #defines of do while,
  1674. # or if closed on same line
  1675. if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
  1676. !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
  1677. ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
  1678. }
  1679. # open braces for enum, union and struct go on the same line.
  1680. if ($line =~ /^.\s*{/ &&
  1681. $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
  1682. ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
  1683. }
  1684. # missing space after union, struct or enum definition
  1685. if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
  1686. WARN("missing space after $1 definition\n" . $herecurr);
  1687. }
  1688. # check for spacing round square brackets; allowed:
  1689. # 1. with a type on the left -- int [] a;
  1690. # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
  1691. # 3. inside a curly brace -- = { [0...10] = 5 }
  1692. while ($line =~ /(.*?\s)\[/g) {
  1693. my ($where, $prefix) = ($-[1], $1);
  1694. if ($prefix !~ /$Type\s+$/ &&
  1695. ($where != 0 || $prefix !~ /^.\s+$/) &&
  1696. $prefix !~ /{\s+$/) {
  1697. ERROR("space prohibited before open square bracket '['\n" . $herecurr);
  1698. }
  1699. }
  1700. # check for spaces between functions and their parentheses.
  1701. while ($line =~ /($Ident)\s+\(/g) {
  1702. my $name = $1;
  1703. my $ctx_before = substr($line, 0, $-[1]);
  1704. my $ctx = "$ctx_before$name";
  1705. # Ignore those directives where spaces _are_ permitted.
  1706. if ($name =~ /^(?:
  1707. if|for|while|switch|return|case|
  1708. volatile|__volatile__|
  1709. __attribute__|format|__extension__|
  1710. asm|__asm__)$/x)
  1711. {
  1712. # cpp #define statements have non-optional spaces, ie
  1713. # if there is a space between the name and the open
  1714. # parenthesis it is simply not a parameter group.
  1715. } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
  1716. # cpp #elif statement condition may start with a (
  1717. } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
  1718. # If this whole things ends with a type its most
  1719. # likely a typedef for a function.
  1720. } elsif ($ctx =~ /$Type$/) {
  1721. } else {
  1722. WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
  1723. }
  1724. }
  1725. # Check operator spacing.
  1726. if (!($line=~/\#\s*include/)) {
  1727. my $ops = qr{
  1728. <<=|>>=|<=|>=|==|!=|
  1729. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  1730. =>|->|<<|>>|<|>|=|!|~|
  1731. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
  1732. \?|:
  1733. }x;
  1734. my @elements = split(/($ops|;)/, $opline);
  1735. my $off = 0;
  1736. my $blank = copy_spacing($opline);
  1737. for (my $n = 0; $n < $#elements; $n += 2) {
  1738. $off += length($elements[$n]);
  1739. # Pick up the preceding and succeeding characters.
  1740. my $ca = substr($opline, 0, $off);
  1741. my $cc = '';
  1742. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  1743. $cc = substr($opline, $off + length($elements[$n + 1]));
  1744. }
  1745. my $cb = "$ca$;$cc";
  1746. my $a = '';
  1747. $a = 'V' if ($elements[$n] ne '');
  1748. $a = 'W' if ($elements[$n] =~ /\s$/);
  1749. $a = 'C' if ($elements[$n] =~ /$;$/);
  1750. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  1751. $a = 'O' if ($elements[$n] eq '');
  1752. $a = 'E' if ($ca =~ /^\s*$/);
  1753. my $op = $elements[$n + 1];
  1754. my $c = '';
  1755. if (defined $elements[$n + 2]) {
  1756. $c = 'V' if ($elements[$n + 2] ne '');
  1757. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  1758. $c = 'C' if ($elements[$n + 2] =~ /^$;/);
  1759. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  1760. $c = 'O' if ($elements[$n + 2] eq '');
  1761. $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
  1762. } else {
  1763. $c = 'E';
  1764. }
  1765. my $ctx = "${a}x${c}";
  1766. my $at = "(ctx:$ctx)";
  1767. my $ptr = substr($blank, 0, $off) . "^";
  1768. my $hereptr = "$hereline$ptr\n";
  1769. # Pull out the value of this operator.
  1770. my $op_type = substr($curr_values, $off + 1, 1);
  1771. # Get the full operator variant.
  1772. my $opv = $op . substr($curr_vars, $off, 1);
  1773. # Ignore operators passed as parameters.
  1774. if ($op_type ne 'V' &&
  1775. $ca =~ /\s$/ && $cc =~ /^\s*,/) {
  1776. # # Ignore comments
  1777. # } elsif ($op =~ /^$;+$/) {
  1778. # ; should have either the end of line or a space or \ after it
  1779. } elsif ($op eq ';') {
  1780. if ($ctx !~ /.x[WEBC]/ &&
  1781. $cc !~ /^\\/ && $cc !~ /^;/) {
  1782. ERROR("space required after that '$op' $at\n" . $hereptr);
  1783. }
  1784. # // is a comment
  1785. } elsif ($op eq '//') {
  1786. # No spaces for:
  1787. # ->
  1788. # : when part of a bitfield
  1789. } elsif ($op eq '->' || $opv eq ':B') {
  1790. if ($ctx =~ /Wx.|.xW/) {
  1791. ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
  1792. }
  1793. # , must have a space on the right.
  1794. } elsif ($op eq ',') {
  1795. if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
  1796. ERROR("space required after that '$op' $at\n" . $hereptr);
  1797. }
  1798. # '*' as part of a type definition -- reported already.
  1799. } elsif ($opv eq '*_') {
  1800. #warn "'*' is part of type\n";
  1801. # unary operators should have a space before and
  1802. # none after. May be left adjacent to another
  1803. # unary operator, or a cast
  1804. } elsif ($op eq '!' || $op eq '~' ||
  1805. $opv eq '*U' || $opv eq '-U' ||
  1806. $opv eq '&U' || $opv eq '&&U') {
  1807. if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  1808. ERROR("space required before that '$op' $at\n" . $hereptr);
  1809. }
  1810. if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
  1811. # A unary '*' may be const
  1812. } elsif ($ctx =~ /.xW/) {
  1813. ERROR("space prohibited after that '$op' $at\n" . $hereptr);
  1814. }
  1815. # unary ++ and unary -- are allowed no space on one side.
  1816. } elsif ($op eq '++' or $op eq '--') {
  1817. if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
  1818. ERROR("space required one side of that '$op' $at\n" . $hereptr);
  1819. }
  1820. if ($ctx =~ /Wx[BE]/ ||
  1821. ($ctx =~ /Wx./ && $cc =~ /^;/)) {
  1822. ERROR("space prohibited before that '$op' $at\n" . $hereptr);
  1823. }
  1824. if ($ctx =~ /ExW/) {
  1825. ERROR("space prohibited after that '$op' $at\n" . $hereptr);
  1826. }
  1827. # << and >> may either have or not have spaces both sides
  1828. } elsif ($op eq '<<' or $op eq '>>' or
  1829. $op eq '&' or $op eq '^' or $op eq '|' or
  1830. $op eq '+' or $op eq '-' or
  1831. $op eq '*' or $op eq '/' or
  1832. $op eq '%')
  1833. {
  1834. if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
  1835. ERROR("need consistent spacing around '$op' $at\n" .
  1836. $hereptr);
  1837. }
  1838. # A colon needs no spaces before when it is
  1839. # terminating a case value or a label.
  1840. } elsif ($opv eq ':C' || $opv eq ':L') {
  1841. if ($ctx =~ /Wx./) {
  1842. ERROR("space prohibited before that '$op' $at\n" . $hereptr);
  1843. }
  1844. # All the others need spaces both sides.
  1845. } elsif ($ctx !~ /[EWC]x[CWE]/) {
  1846. my $ok = 0;
  1847. # Ignore email addresses <foo@bar>
  1848. if (($op eq '<' &&
  1849. $cc =~ /^\S+\@\S+>/) ||
  1850. ($op eq '>' &&
  1851. $ca =~ /<\S+\@\S+$/))
  1852. {
  1853. $ok = 1;
  1854. }
  1855. # Ignore ?:
  1856. if (($opv eq ':O' && $ca =~ /\?$/) ||
  1857. ($op eq '?' && $cc =~ /^:/)) {
  1858. $ok = 1;
  1859. }
  1860. if ($ok == 0) {
  1861. ERROR("spaces required around that '$op' $at\n" . $hereptr);
  1862. }
  1863. }
  1864. $off += length($elements[$n + 1]);
  1865. }
  1866. }
  1867. # check for multiple assignments
  1868. if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
  1869. CHK("multiple assignments should be avoided\n" . $herecurr);
  1870. }
  1871. ## # check for multiple declarations, allowing for a function declaration
  1872. ## # continuation.
  1873. ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  1874. ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  1875. ##
  1876. ## # Remove any bracketed sections to ensure we do not
  1877. ## # falsly report the parameters of functions.
  1878. ## my $ln = $line;
  1879. ## while ($ln =~ s/\([^\(\)]*\)//g) {
  1880. ## }
  1881. ## if ($ln =~ /,/) {
  1882. ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
  1883. ## }
  1884. ## }
  1885. #need space before brace following if, while, etc
  1886. if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
  1887. $line =~ /do{/) {
  1888. ERROR("space required before the open brace '{'\n" . $herecurr);
  1889. }
  1890. # closing brace should have a space following it when it has anything
  1891. # on the line
  1892. if ($line =~ /}(?!(?:,|;|\)))\S/) {
  1893. ERROR("space required after that close brace '}'\n" . $herecurr);
  1894. }
  1895. # check spacing on square brackets
  1896. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  1897. ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
  1898. }
  1899. if ($line =~ /\s\]/) {
  1900. ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
  1901. }
  1902. # check spacing on parentheses
  1903. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  1904. $line !~ /for\s*\(\s+;/) {
  1905. ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
  1906. }
  1907. if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
  1908. $line !~ /for\s*\(.*;\s+\)/ &&
  1909. $line !~ /:\s+\)/) {
  1910. ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
  1911. }
  1912. #goto labels aren't indented, allow a single space however
  1913. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  1914. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  1915. WARN("labels should not be indented\n" . $herecurr);
  1916. }
  1917. # Return is not a function.
  1918. if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
  1919. my $spacing = $1;
  1920. my $value = $2;
  1921. # Flatten any parentheses
  1922. $value =~ s/\(/ \(/g;
  1923. $value =~ s/\)/\) /g;
  1924. while ($value =~ s/\[[^\{\}]*\]/1/ ||
  1925. $value !~ /(?:$Ident|-?$Constant)\s*
  1926. $Compare\s*
  1927. (?:$Ident|-?$Constant)/x &&
  1928. $value =~ s/\([^\(\)]*\)/1/) {
  1929. }
  1930. #print "value<$value>\n";
  1931. if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
  1932. ERROR("return is not a function, parentheses are not required\n" . $herecurr);
  1933. } elsif ($spacing !~ /\s+/) {
  1934. ERROR("space required before the open parenthesis '('\n" . $herecurr);
  1935. }
  1936. }
  1937. # Return of what appears to be an errno should normally be -'ve
  1938. if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
  1939. my $name = $1;
  1940. if ($name ne 'EOF' && $name ne 'ERROR') {
  1941. WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
  1942. }
  1943. }
  1944. # Need a space before open parenthesis after if, while etc
  1945. if ($line=~/\b(if|while|for|switch)\(/) {
  1946. ERROR("space required before the open parenthesis '('\n" . $herecurr);
  1947. }
  1948. # Check for illegal assignment in if conditional -- and check for trailing
  1949. # statements after the conditional.
  1950. if ($line =~ /do\s*(?!{)/) {
  1951. my ($stat_next) = ctx_statement_block($line_nr_next,
  1952. $remain_next, $off_next);
  1953. $stat_next =~ s/\n./\n /g;
  1954. ##print "stat<$stat> stat_next<$stat_next>\n";
  1955. if ($stat_next =~ /^\s*while\b/) {
  1956. # If the statement carries leading newlines,
  1957. # then count those as offsets.
  1958. my ($whitespace) =
  1959. ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
  1960. my $offset =
  1961. statement_rawlines($whitespace) - 1;
  1962. $suppress_whiletrailers{$line_nr_next +
  1963. $offset} = 1;
  1964. }
  1965. }
  1966. if (!defined $suppress_whiletrailers{$linenr} &&
  1967. $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
  1968. my ($s, $c) = ($stat, $cond);
  1969. if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
  1970. ERROR("do not use assignment in if condition\n" . $herecurr);
  1971. }
  1972. # Find out what is on the end of the line after the
  1973. # conditional.
  1974. substr($s, 0, length($c), '');
  1975. $s =~ s/\n.*//g;
  1976. $s =~ s/$;//g; # Remove any comments
  1977. if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
  1978. $c !~ /}\s*while\s*/)
  1979. {
  1980. # Find out how long the conditional actually is.
  1981. my @newlines = ($c =~ /\n/gs);
  1982. my $cond_lines = 1 + $#newlines;
  1983. my $stat_real = '';
  1984. $stat_real = raw_line($linenr, $cond_lines)
  1985. . "\n" if ($cond_lines);
  1986. if (defined($stat_real) && $cond_lines > 1) {
  1987. $stat_real = "[...]\n$stat_real";
  1988. }
  1989. ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
  1990. }
  1991. }
  1992. # Check for bitwise tests written as boolean
  1993. if ($line =~ /
  1994. (?:
  1995. (?:\[|\(|\&\&|\|\|)
  1996. \s*0[xX][0-9]+\s*
  1997. (?:\&\&|\|\|)
  1998. |
  1999. (?:\&\&|\|\|)
  2000. \s*0[xX][0-9]+\s*
  2001. (?:\&\&|\|\||\)|\])
  2002. )/x)
  2003. {
  2004. WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
  2005. }
  2006. # if and else should not have general statements after it
  2007. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
  2008. my $s = $1;
  2009. $s =~ s/$;//g; # Remove any comments
  2010. if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
  2011. ERROR("trailing statements should be on next line\n" . $herecurr);
  2012. }
  2013. }
  2014. # if should not continue a brace
  2015. if ($line =~ /}\s*if\b/) {
  2016. ERROR("trailing statements should be on next line\n" .
  2017. $herecurr);
  2018. }
  2019. # case and default should not have general statements after them
  2020. if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
  2021. $line !~ /\G(?:
  2022. (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
  2023. \s*return\s+
  2024. )/xg)
  2025. {
  2026. ERROR("trailing statements should be on next line\n" . $herecurr);
  2027. }
  2028. # Check for }<nl>else {, these must be at the same
  2029. # indent level to be relevant to each other.
  2030. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  2031. $previndent == $indent) {
  2032. ERROR("else should follow close brace '}'\n" . $hereprev);
  2033. }
  2034. if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
  2035. $previndent == $indent) {
  2036. my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
  2037. # Find out what is on the end of the line after the
  2038. # conditional.
  2039. substr($s, 0, length($c), '');
  2040. $s =~ s/\n.*//g;
  2041. if ($s =~ /^\s*;/) {
  2042. ERROR("while should follow close brace '}'\n" . $hereprev);
  2043. }
  2044. }
  2045. #studly caps, commented out until figure out how to distinguish between use of existing and adding new
  2046. # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
  2047. # print "No studly caps, use _\n";
  2048. # print "$herecurr";
  2049. # $clean = 0;
  2050. # }
  2051. #no spaces allowed after \ in define
  2052. if ($line=~/\#\s*define.*\\\s$/) {
  2053. WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
  2054. }
  2055. #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
  2056. if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
  2057. my $file = "$1.h";
  2058. my $checkfile = "include/linux/$file";
  2059. if (-f "$root/$checkfile" &&
  2060. $realfile ne $checkfile &&
  2061. $1 !~ /$allowed_asm_includes/)
  2062. {
  2063. if ($realfile =~ m{^arch/}) {
  2064. CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  2065. } else {
  2066. WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  2067. }
  2068. }
  2069. }
  2070. # multi-statement macros should be enclosed in a do while loop, grab the
  2071. # first statement and ensure its the whole macro if its not enclosed
  2072. # in a known good container
  2073. if ($realfile !~ m@/vmlinux.lds.h$@ &&
  2074. $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
  2075. my $ln = $linenr;
  2076. my $cnt = $realcnt;
  2077. my ($off, $dstat, $dcond, $rest);
  2078. my $ctx = '';
  2079. my $args = defined($1);
  2080. # Find the end of the macro and limit our statement
  2081. # search to that.
  2082. while ($cnt > 0 && defined $lines[$ln - 1] &&
  2083. $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
  2084. {
  2085. $ctx .= $rawlines[$ln - 1] . "\n";
  2086. $cnt-- if ($lines[$ln - 1] !~ /^-/);
  2087. $ln++;
  2088. }
  2089. $ctx .= $rawlines[$ln - 1];
  2090. ($dstat, $dcond, $ln, $cnt, $off) =
  2091. ctx_statement_block($linenr, $ln - $linenr + 1, 0);
  2092. #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
  2093. #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
  2094. # Extract the remainder of the define (if any) and
  2095. # rip off surrounding spaces, and trailing \'s.
  2096. $rest = '';
  2097. while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
  2098. #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
  2099. if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
  2100. $rest .= substr($lines[$ln - 1], $off) . "\n";
  2101. $cnt--;
  2102. }
  2103. $ln++;
  2104. $off = 0;
  2105. }
  2106. $rest =~ s/\\\n.//g;
  2107. $rest =~ s/^\s*//s;
  2108. $rest =~ s/\s*$//s;
  2109. # Clean up the original statement.
  2110. if ($args) {
  2111. substr($dstat, 0, length($dcond), '');
  2112. } else {
  2113. $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
  2114. }
  2115. $dstat =~ s/$;//g;
  2116. $dstat =~ s/\\\n.//g;
  2117. $dstat =~ s/^\s*//s;
  2118. $dstat =~ s/\s*$//s;
  2119. # Flatten any parentheses and braces
  2120. while ($dstat =~ s/\([^\(\)]*\)/1/ ||
  2121. $dstat =~ s/\{[^\{\}]*\}/1/ ||
  2122. $dstat =~ s/\[[^\{\}]*\]/1/)
  2123. {
  2124. }
  2125. my $exceptions = qr{
  2126. $Declare|
  2127. module_param_named|
  2128. MODULE_PARAM_DESC|
  2129. DECLARE_PER_CPU|
  2130. DEFINE_PER_CPU|
  2131. __typeof__\(|
  2132. union|
  2133. struct|
  2134. \.$Ident\s*=\s*|
  2135. ^\"|\"$
  2136. }x;
  2137. #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
  2138. if ($rest ne '' && $rest ne ',') {
  2139. if ($rest !~ /while\s*\(/ &&
  2140. $dstat !~ /$exceptions/)
  2141. {
  2142. ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
  2143. }
  2144. } elsif ($ctx !~ /;/) {
  2145. if ($dstat ne '' &&
  2146. $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
  2147. $dstat !~ /$exceptions/ &&
  2148. $dstat !~ /^\.$Ident\s*=/ &&
  2149. $dstat =~ /$Operators/)
  2150. {
  2151. ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
  2152. }
  2153. }
  2154. }
  2155. # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
  2156. # all assignments may have only one of the following with an assignment:
  2157. # .
  2158. # ALIGN(...)
  2159. # VMLINUX_SYMBOL(...)
  2160. if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
  2161. WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
  2162. }
  2163. # check for redundant bracing round if etc
  2164. if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
  2165. my ($level, $endln, @chunks) =
  2166. ctx_statement_full($linenr, $realcnt, 1);
  2167. #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
  2168. #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
  2169. if ($#chunks > 0 && $level == 0) {
  2170. my $allowed = 0;
  2171. my $seen = 0;
  2172. my $herectx = $here . "\n";
  2173. my $ln = $linenr - 1;
  2174. for my $chunk (@chunks) {
  2175. my ($cond, $block) = @{$chunk};
  2176. # If the condition carries leading newlines, then count those as offsets.
  2177. my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
  2178. my $offset = statement_rawlines($whitespace) - 1;
  2179. #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
  2180. # We have looked at and allowed this specific line.
  2181. $suppress_ifbraces{$ln + $offset} = 1;
  2182. $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
  2183. $ln += statement_rawlines($block) - 1;
  2184. substr($block, 0, length($cond), '');
  2185. $seen++ if ($block =~ /^\s*{/);
  2186. #print "cond<$cond> block<$block> allowed<$allowed>\n";
  2187. if (statement_lines($cond) > 1) {
  2188. #print "APW: ALLOWED: cond<$cond>\n";
  2189. $allowed = 1;
  2190. }
  2191. if ($block =~/\b(?:if|for|while)\b/) {
  2192. #print "APW: ALLOWED: block<$block>\n";
  2193. $allowed = 1;
  2194. }
  2195. if (statement_block_size($block) > 1) {
  2196. #print "APW: ALLOWED: lines block<$block>\n";
  2197. $allowed = 1;
  2198. }
  2199. }
  2200. if ($seen && !$allowed) {
  2201. WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
  2202. }
  2203. }
  2204. }
  2205. if (!defined $suppress_ifbraces{$linenr - 1} &&
  2206. $line =~ /\b(if|while|for|else)\b/) {
  2207. my $allowed = 0;
  2208. # Check the pre-context.
  2209. if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
  2210. #print "APW: ALLOWED: pre<$1>\n";
  2211. $allowed = 1;
  2212. }
  2213. my ($level, $endln, @chunks) =
  2214. ctx_statement_full($linenr, $realcnt, $-[0]);
  2215. # Check the condition.
  2216. my ($cond, $block) = @{$chunks[0]};
  2217. #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
  2218. if (defined $cond) {
  2219. substr($block, 0, length($cond), '');
  2220. }
  2221. if (statement_lines($cond) > 1) {
  2222. #print "APW: ALLOWED: cond<$cond>\n";
  2223. $allowed = 1;
  2224. }
  2225. if ($block =~/\b(?:if|for|while)\b/) {
  2226. #print "APW: ALLOWED: block<$block>\n";
  2227. $allowed = 1;
  2228. }
  2229. if (statement_block_size($block) > 1) {
  2230. #print "APW: ALLOWED: lines block<$block>\n";
  2231. $allowed = 1;
  2232. }
  2233. # Check the post-context.
  2234. if (defined $chunks[1]) {
  2235. my ($cond, $block) = @{$chunks[1]};
  2236. if (defined $cond) {
  2237. substr($block, 0, length($cond), '');
  2238. }
  2239. if ($block =~ /^\s*\{/) {
  2240. #print "APW: ALLOWED: chunk-1 block<$block>\n";
  2241. $allowed = 1;
  2242. }
  2243. }
  2244. if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
  2245. my $herectx = $here . "\n";;
  2246. my $cnt = statement_rawlines($block);
  2247. for (my $n = 0; $n < $cnt; $n++) {
  2248. $herectx .= raw_line($linenr, $n) . "\n";;
  2249. }
  2250. WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
  2251. }
  2252. }
  2253. # don't include deprecated include files (uses RAW line)
  2254. for my $inc (@dep_includes) {
  2255. if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
  2256. ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  2257. }
  2258. }
  2259. # don't use deprecated functions
  2260. for my $func (@dep_functions) {
  2261. if ($line =~ /\b$func\b/) {
  2262. ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  2263. }
  2264. }
  2265. # no volatiles please
  2266. my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
  2267. if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
  2268. WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
  2269. }
  2270. # warn about #if 0
  2271. if ($line =~ /^.\s*\#\s*if\s+0\b/) {
  2272. CHK("if this code is redundant consider removing it\n" .
  2273. $herecurr);
  2274. }
  2275. # check for needless kfree() checks
  2276. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  2277. my $expr = $1;
  2278. if ($line =~ /\bkfree\(\Q$expr\E\);/) {
  2279. WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
  2280. }
  2281. }
  2282. # check for needless usb_free_urb() checks
  2283. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  2284. my $expr = $1;
  2285. if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
  2286. WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
  2287. }
  2288. }
  2289. # prefer usleep_range over udelay
  2290. if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
  2291. # ignore udelay's < 10, however
  2292. if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
  2293. CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
  2294. }
  2295. }
  2296. # warn about unexpectedly long msleep's
  2297. if ($line =~ /\bmsleep\s*\((\d+)\);/) {
  2298. if ($1 < 20) {
  2299. WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
  2300. }
  2301. }
  2302. # warn about #ifdefs in C files
  2303. # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  2304. # print "#ifdef in C files should be avoided\n";
  2305. # print "$herecurr";
  2306. # $clean = 0;
  2307. # }
  2308. # warn about spacing in #ifdefs
  2309. if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
  2310. ERROR("exactly one space required after that #$1\n" . $herecurr);
  2311. }
  2312. # check for spinlock_t definitions without a comment.
  2313. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
  2314. $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
  2315. my $which = $1;
  2316. if (!ctx_has_comment($first_line, $linenr)) {
  2317. CHK("$1 definition without comment\n" . $herecurr);
  2318. }
  2319. }
  2320. # check for memory barriers without a comment.
  2321. if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  2322. if (!ctx_has_comment($first_line, $linenr)) {
  2323. CHK("memory barrier without comment\n" . $herecurr);
  2324. }
  2325. }
  2326. # check of hardware specific defines
  2327. if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
  2328. CHK("architecture specific defines should be avoided\n" . $herecurr);
  2329. }
  2330. # Check that the storage class is at the beginning of a declaration
  2331. if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
  2332. WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
  2333. }
  2334. # check the location of the inline attribute, that it is between
  2335. # storage class and type.
  2336. if ($line =~ /\b$Type\s+$Inline\b/ ||
  2337. $line =~ /\b$Inline\s+$Storage\b/) {
  2338. ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
  2339. }
  2340. # Check for __inline__ and __inline, prefer inline
  2341. if ($line =~ /\b(__inline__|__inline)\b/) {
  2342. WARN("plain inline is preferred over $1\n" . $herecurr);
  2343. }
  2344. # Check for __attribute__ packed, prefer __packed
  2345. if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
  2346. WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr);
  2347. }
  2348. # check for sizeof(&)
  2349. if ($line =~ /\bsizeof\s*\(\s*\&/) {
  2350. WARN("sizeof(& should be avoided\n" . $herecurr);
  2351. }
  2352. # check for line continuations in quoted strings with odd counts of "
  2353. if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
  2354. WARN("Avoid line continuations in quoted strings\n" . $herecurr);
  2355. }
  2356. # check for new externs in .c files.
  2357. if ($realfile =~ /\.c$/ && defined $stat &&
  2358. $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
  2359. {
  2360. my $function_name = $1;
  2361. my $paren_space = $2;
  2362. my $s = $stat;
  2363. if (defined $cond) {
  2364. substr($s, 0, length($cond), '');
  2365. }
  2366. if ($s =~ /^\s*;/ &&
  2367. $function_name ne 'uninitialized_var')
  2368. {
  2369. WARN("externs should be avoided in .c files\n" . $herecurr);
  2370. }
  2371. if ($paren_space =~ /\n/) {
  2372. WARN("arguments for function declarations should follow identifier\n" . $herecurr);
  2373. }
  2374. } elsif ($realfile =~ /\.c$/ && defined $stat &&
  2375. $stat =~ /^.\s*extern\s+/)
  2376. {
  2377. WARN("externs should be avoided in .c files\n" . $herecurr);
  2378. }
  2379. # checks for new __setup's
  2380. if ($rawline =~ /\b__setup\("([^"]*)"/) {
  2381. my $name = $1;
  2382. if (!grep(/$name/, @setup_docs)) {
  2383. CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
  2384. }
  2385. }
  2386. # check for pointless casting of kmalloc return
  2387. if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
  2388. WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
  2389. }
  2390. # check for multiple semicolons
  2391. if ($line =~ /;\s*;\s*$/) {
  2392. WARN("Statements terminations use 1 semicolon\n" . $herecurr);
  2393. }
  2394. # check for gcc specific __FUNCTION__
  2395. if ($line =~ /__FUNCTION__/) {
  2396. WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
  2397. }
  2398. # check for semaphores initialized locked
  2399. if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
  2400. WARN("consider using a completion\n" . $herecurr);
  2401. }
  2402. # recommend kstrto* over simple_strto*
  2403. if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
  2404. WARN("consider using kstrto* in preference to simple_$1\n" . $herecurr);
  2405. }
  2406. # check for __initcall(), use device_initcall() explicitly please
  2407. if ($line =~ /^.\s*__initcall\s*\(/) {
  2408. WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
  2409. }
  2410. # check for various ops structs, ensure they are const.
  2411. my $struct_ops = qr{acpi_dock_ops|
  2412. address_space_operations|
  2413. backlight_ops|
  2414. block_device_operations|
  2415. dentry_operations|
  2416. dev_pm_ops|
  2417. dma_map_ops|
  2418. extent_io_ops|
  2419. file_lock_operations|
  2420. file_operations|
  2421. hv_ops|
  2422. ide_dma_ops|
  2423. intel_dvo_dev_ops|
  2424. item_operations|
  2425. iwl_ops|
  2426. kgdb_arch|
  2427. kgdb_io|
  2428. kset_uevent_ops|
  2429. lock_manager_operations|
  2430. microcode_ops|
  2431. mtrr_ops|
  2432. neigh_ops|
  2433. nlmsvc_binding|
  2434. pci_raw_ops|
  2435. pipe_buf_operations|
  2436. platform_hibernation_ops|
  2437. platform_suspend_ops|
  2438. proto_ops|
  2439. rpc_pipe_ops|
  2440. seq_operations|
  2441. snd_ac97_build_ops|
  2442. soc_pcmcia_socket_ops|
  2443. stacktrace_ops|
  2444. sysfs_ops|
  2445. tty_operations|
  2446. usb_mon_operations|
  2447. wd_ops}x;
  2448. if ($line !~ /\bconst\b/ &&
  2449. $line =~ /\bstruct\s+($struct_ops)\b/) {
  2450. WARN("struct $1 should normally be const\n" .
  2451. $herecurr);
  2452. }
  2453. # use of NR_CPUS is usually wrong
  2454. # ignore definitions of NR_CPUS and usage to define arrays as likely right
  2455. if ($line =~ /\bNR_CPUS\b/ &&
  2456. $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
  2457. $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
  2458. $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
  2459. $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
  2460. $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
  2461. {
  2462. WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
  2463. }
  2464. # check for %L{u,d,i} in strings
  2465. my $string;
  2466. while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
  2467. $string = substr($rawline, $-[1], $+[1] - $-[1]);
  2468. $string =~ s/%%/__/g;
  2469. if ($string =~ /(?<!%)%L[udi]/) {
  2470. WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
  2471. last;
  2472. }
  2473. }
  2474. # whine mightly about in_atomic
  2475. if ($line =~ /\bin_atomic\s*\(/) {
  2476. if ($realfile =~ m@^drivers/@) {
  2477. ERROR("do not use in_atomic in drivers\n" . $herecurr);
  2478. } elsif ($realfile !~ m@^kernel/@) {
  2479. WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
  2480. }
  2481. }
  2482. # check for lockdep_set_novalidate_class
  2483. if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
  2484. $line =~ /__lockdep_no_validate__\s*\)/ ) {
  2485. if ($realfile !~ m@^kernel/lockdep@ &&
  2486. $realfile !~ m@^include/linux/lockdep@ &&
  2487. $realfile !~ m@^drivers/base/core@) {
  2488. ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
  2489. }
  2490. }
  2491. if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
  2492. $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
  2493. WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
  2494. }
  2495. # Check for memset with swapped arguments
  2496. if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
  2497. ERROR("memset size is 3rd argument, not the second.\n" . $herecurr);
  2498. }
  2499. }
  2500. # If we have no input at all, then there is nothing to report on
  2501. # so just keep quiet.
  2502. if ($#rawlines == -1) {
  2503. exit(0);
  2504. }
  2505. # In mailback mode only produce a report in the negative, for
  2506. # things that appear to be patches.
  2507. if ($mailback && ($clean == 1 || !$is_patch)) {
  2508. exit(0);
  2509. }
  2510. # This is not a patch, and we are are in 'no-patch' mode so
  2511. # just keep quiet.
  2512. if (!$chk_patch && !$is_patch) {
  2513. exit(0);
  2514. }
  2515. if (!$is_patch) {
  2516. ERROR("Does not appear to be a unified-diff format patch\n");
  2517. }
  2518. if ($is_patch && $chk_signoff && $signoff == 0) {
  2519. ERROR("Missing Signed-off-by: line(s)\n");
  2520. }
  2521. print report_dump();
  2522. if ($summary && !($clean == 1 && $quiet == 1)) {
  2523. print "$filename " if ($summary_file);
  2524. print "total: $cnt_error errors, $cnt_warn warnings, " .
  2525. (($check)? "$cnt_chk checks, " : "") .
  2526. "$cnt_lines lines checked\n";
  2527. print "\n" if ($quiet == 0);
  2528. }
  2529. if ($quiet == 0) {
  2530. # If there were whitespace errors which cleanpatch can fix
  2531. # then suggest that.
  2532. if ($rpt_cleaners) {
  2533. print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
  2534. print " scripts/cleanfile\n\n";
  2535. $rpt_cleaners = 0;
  2536. }
  2537. }
  2538. if ($clean == 1 && $quiet == 0) {
  2539. print "$vname has no obvious style problems and is ready for submission.\n"
  2540. }
  2541. if ($clean == 0 && $quiet == 0) {
  2542. print "$vname has style problems, please review. If any of these errors\n";
  2543. print "are false positives report them to the maintainer, see\n";
  2544. print "CHECKPATCH in MAINTAINERS.\n";
  2545. }
  2546. return $clean;
  2547. }