checkpatch.pl 74 KB

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