checkpatch.pl 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178
  1. #!/usr/bin/perl -w
  2. # (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
  3. # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
  4. # (c) 2007, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite, etc)
  5. # Licensed under the terms of the GNU GPL License version 2
  6. use strict;
  7. my $P = $0;
  8. $P =~ s@.*/@@g;
  9. my $V = '0.20';
  10. use Getopt::Long qw(:config no_auto_abbrev);
  11. my $quiet = 0;
  12. my $tree = 1;
  13. my $chk_signoff = 1;
  14. my $chk_patch = 1;
  15. my $tst_type = 0;
  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. GetOptions(
  27. 'q|quiet+' => \$quiet,
  28. 'tree!' => \$tree,
  29. 'signoff!' => \$chk_signoff,
  30. 'patch!' => \$chk_patch,
  31. 'emacs!' => \$emacs,
  32. 'terse!' => \$terse,
  33. 'file!' => \$file,
  34. 'subjective!' => \$check,
  35. 'strict!' => \$check,
  36. 'root=s' => \$root,
  37. 'summary!' => \$summary,
  38. 'mailback!' => \$mailback,
  39. 'summary-file!' => \$summary_file,
  40. 'debug=s' => \%debug,
  41. 'test-type!' => \$tst_type,
  42. 'test-only=s' => \$tst_only,
  43. ) or exit;
  44. my $exit = 0;
  45. if ($#ARGV < 0) {
  46. print "usage: $P [options] patchfile\n";
  47. print "version: $V\n";
  48. print "options: -q => quiet\n";
  49. print " --no-tree => run without a kernel tree\n";
  50. print " --terse => one line per report\n";
  51. print " --emacs => emacs compile window format\n";
  52. print " --file => check a source file\n";
  53. print " --strict => enable more subjective tests\n";
  54. print " --root => path to the kernel tree root\n";
  55. print " --no-summary => suppress the per-file summary\n";
  56. print " --summary-file => include the filename in summary\n";
  57. exit(1);
  58. }
  59. my $dbg_values = 0;
  60. my $dbg_possible = 0;
  61. for my $key (keys %debug) {
  62. eval "\${dbg_$key} = '$debug{$key}';"
  63. }
  64. if ($terse) {
  65. $emacs = 1;
  66. $quiet++;
  67. }
  68. if ($tree) {
  69. if (defined $root) {
  70. if (!top_of_kernel_tree($root)) {
  71. die "$P: $root: --root does not point at a valid tree\n";
  72. }
  73. } else {
  74. if (top_of_kernel_tree('.')) {
  75. $root = '.';
  76. } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
  77. top_of_kernel_tree($1)) {
  78. $root = $1;
  79. }
  80. }
  81. if (!defined $root) {
  82. print "Must be run from the top-level dir. of a kernel tree\n";
  83. exit(2);
  84. }
  85. }
  86. my $emitted_corrupt = 0;
  87. our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
  88. our $Storage = qr{extern|static|asmlinkage};
  89. our $Sparse = qr{
  90. __user|
  91. __kernel|
  92. __force|
  93. __iomem|
  94. __must_check|
  95. __init_refok|
  96. __kprobes
  97. }x;
  98. our $Attribute = qr{
  99. const|
  100. __read_mostly|
  101. __kprobes|
  102. __(?:mem|cpu|dev|)(?:initdata|init)
  103. }x;
  104. our $Modifier;
  105. our $Inline = qr{inline|__always_inline|noinline};
  106. our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
  107. our $Lval = qr{$Ident(?:$Member)*};
  108. our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
  109. our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
  110. our $Operators = qr{
  111. <=|>=|==|!=|
  112. =>|->|<<|>>|<|>|!|~|
  113. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
  114. }x;
  115. our $NonptrType;
  116. our $Type;
  117. our $Declare;
  118. our $UTF8 = qr {
  119. [\x09\x0A\x0D\x20-\x7E] # ASCII
  120. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  121. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  122. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  123. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  124. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  125. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  126. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  127. }x;
  128. our @typeList = (
  129. qr{void},
  130. qr{(?:unsigned\s+)?char},
  131. qr{(?:unsigned\s+)?short},
  132. qr{(?:unsigned\s+)?int},
  133. qr{(?:unsigned\s+)?long},
  134. qr{(?:unsigned\s+)?long\s+int},
  135. qr{(?:unsigned\s+)?long\s+long},
  136. qr{(?:unsigned\s+)?long\s+long\s+int},
  137. qr{unsigned},
  138. qr{float},
  139. qr{double},
  140. qr{bool},
  141. qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
  142. qr{struct\s+$Ident},
  143. qr{union\s+$Ident},
  144. qr{enum\s+$Ident},
  145. qr{${Ident}_t},
  146. qr{${Ident}_handler},
  147. qr{${Ident}_handler_fn},
  148. );
  149. our @modifierList = (
  150. qr{fastcall},
  151. );
  152. sub build_types {
  153. my $mods = "(?: \n" . join("|\n ", @modifierList) . "\n)";
  154. my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
  155. $NonptrType = qr{
  156. (?:const\s+)?
  157. (?:$mods\s+)?
  158. (?:
  159. (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
  160. (?:${all}\b)
  161. )
  162. (?:\s+$Sparse|\s+const)*
  163. }x;
  164. $Type = qr{
  165. $NonptrType
  166. (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
  167. (?:\s+$Inline|\s+$Sparse|\s+$Attribute|\s+$mods)*
  168. }x;
  169. $Declare = qr{(?:$Storage\s+)?$Type};
  170. $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
  171. }
  172. build_types();
  173. $chk_signoff = 0 if ($file);
  174. my @dep_includes = ();
  175. my @dep_functions = ();
  176. my $removal = "Documentation/feature-removal-schedule.txt";
  177. if ($tree && -f "$root/$removal") {
  178. open(REMOVE, "<$root/$removal") ||
  179. die "$P: $removal: open failed - $!\n";
  180. while (<REMOVE>) {
  181. if (/^Check:\s+(.*\S)/) {
  182. for my $entry (split(/[, ]+/, $1)) {
  183. if ($entry =~ m@include/(.*)@) {
  184. push(@dep_includes, $1);
  185. } elsif ($entry !~ m@/@) {
  186. push(@dep_functions, $entry);
  187. }
  188. }
  189. }
  190. }
  191. }
  192. my @rawlines = ();
  193. my @lines = ();
  194. my $vname;
  195. for my $filename (@ARGV) {
  196. if ($file) {
  197. open(FILE, "diff -u /dev/null $filename|") ||
  198. die "$P: $filename: diff failed - $!\n";
  199. } else {
  200. open(FILE, "<$filename") ||
  201. die "$P: $filename: open failed - $!\n";
  202. }
  203. if ($filename eq '-') {
  204. $vname = 'Your patch';
  205. } else {
  206. $vname = $filename;
  207. }
  208. while (<FILE>) {
  209. chomp;
  210. push(@rawlines, $_);
  211. }
  212. close(FILE);
  213. if (!process($filename)) {
  214. $exit = 1;
  215. }
  216. @rawlines = ();
  217. @lines = ();
  218. }
  219. exit($exit);
  220. sub top_of_kernel_tree {
  221. my ($root) = @_;
  222. my @tree_check = (
  223. "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
  224. "README", "Documentation", "arch", "include", "drivers",
  225. "fs", "init", "ipc", "kernel", "lib", "scripts",
  226. );
  227. foreach my $check (@tree_check) {
  228. if (! -e $root . '/' . $check) {
  229. return 0;
  230. }
  231. }
  232. return 1;
  233. }
  234. sub expand_tabs {
  235. my ($str) = @_;
  236. my $res = '';
  237. my $n = 0;
  238. for my $c (split(//, $str)) {
  239. if ($c eq "\t") {
  240. $res .= ' ';
  241. $n++;
  242. for (; ($n % 8) != 0; $n++) {
  243. $res .= ' ';
  244. }
  245. next;
  246. }
  247. $res .= $c;
  248. $n++;
  249. }
  250. return $res;
  251. }
  252. sub copy_spacing {
  253. (my $res = shift) =~ tr/\t/ /c;
  254. return $res;
  255. }
  256. sub line_stats {
  257. my ($line) = @_;
  258. # Drop the diff line leader and expand tabs
  259. $line =~ s/^.//;
  260. $line = expand_tabs($line);
  261. # Pick the indent from the front of the line.
  262. my ($white) = ($line =~ /^(\s*)/);
  263. return (length($line), length($white));
  264. }
  265. my $sanitise_quote = '';
  266. sub sanitise_line_reset {
  267. my ($in_comment) = @_;
  268. if ($in_comment) {
  269. $sanitise_quote = '*/';
  270. } else {
  271. $sanitise_quote = '';
  272. }
  273. }
  274. sub sanitise_line {
  275. my ($line) = @_;
  276. my $res = '';
  277. my $l = '';
  278. my $qlen = 0;
  279. my $off = 0;
  280. my $c;
  281. # Always copy over the diff marker.
  282. $res = substr($line, 0, 1);
  283. for ($off = 1; $off < length($line); $off++) {
  284. $c = substr($line, $off, 1);
  285. # Comments we are wacking completly including the begin
  286. # and end, all to $;.
  287. if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
  288. $sanitise_quote = '*/';
  289. substr($res, $off, 2, "$;$;");
  290. $off++;
  291. next;
  292. }
  293. if (substr($line, $off, 2) eq '*/') {
  294. $sanitise_quote = '';
  295. substr($res, $off, 2, "$;$;");
  296. $off++;
  297. next;
  298. }
  299. # A \ in a string means ignore the next character.
  300. if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
  301. $c eq "\\") {
  302. substr($res, $off, 2, 'XX');
  303. $off++;
  304. next;
  305. }
  306. # Regular quotes.
  307. if ($c eq "'" || $c eq '"') {
  308. if ($sanitise_quote eq '') {
  309. $sanitise_quote = $c;
  310. substr($res, $off, 1, $c);
  311. next;
  312. } elsif ($sanitise_quote eq $c) {
  313. $sanitise_quote = '';
  314. }
  315. }
  316. #print "SQ:$sanitise_quote\n";
  317. if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
  318. substr($res, $off, 1, $;);
  319. } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
  320. substr($res, $off, 1, 'X');
  321. } else {
  322. substr($res, $off, 1, $c);
  323. }
  324. }
  325. # The pathname on a #include may be surrounded by '<' and '>'.
  326. if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
  327. my $clean = 'X' x length($1);
  328. $res =~ s@\<.*\>@<$clean>@;
  329. # The whole of a #error is a string.
  330. } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
  331. my $clean = 'X' x length($1);
  332. $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
  333. }
  334. return $res;
  335. }
  336. sub ctx_statement_block {
  337. my ($linenr, $remain, $off) = @_;
  338. my $line = $linenr - 1;
  339. my $blk = '';
  340. my $soff = $off;
  341. my $coff = $off - 1;
  342. my $coff_set = 0;
  343. my $loff = 0;
  344. my $type = '';
  345. my $level = 0;
  346. my $p;
  347. my $c;
  348. my $len = 0;
  349. my $remainder;
  350. while (1) {
  351. #warn "CSB: blk<$blk> remain<$remain>\n";
  352. # If we are about to drop off the end, pull in more
  353. # context.
  354. if ($off >= $len) {
  355. for (; $remain > 0; $line++) {
  356. next if ($lines[$line] =~ /^-/);
  357. $remain--;
  358. $loff = $len;
  359. $blk .= $lines[$line] . "\n";
  360. $len = length($blk);
  361. $line++;
  362. last;
  363. }
  364. # Bail if there is no further context.
  365. #warn "CSB: blk<$blk> off<$off> len<$len>\n";
  366. if ($off >= $len) {
  367. last;
  368. }
  369. }
  370. $p = $c;
  371. $c = substr($blk, $off, 1);
  372. $remainder = substr($blk, $off);
  373. #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
  374. # Statement ends at the ';' or a close '}' at the
  375. # outermost level.
  376. if ($level == 0 && $c eq ';') {
  377. last;
  378. }
  379. # An else is really a conditional as long as its not else if
  380. if ($level == 0 && $coff_set == 0 &&
  381. (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
  382. $remainder =~ /^(else)(?:\s|{)/ &&
  383. $remainder !~ /^else\s+if\b/) {
  384. $coff = $off + length($1) - 1;
  385. $coff_set = 1;
  386. #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
  387. #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
  388. }
  389. if (($type eq '' || $type eq '(') && $c eq '(') {
  390. $level++;
  391. $type = '(';
  392. }
  393. if ($type eq '(' && $c eq ')') {
  394. $level--;
  395. $type = ($level != 0)? '(' : '';
  396. if ($level == 0 && $coff < $soff) {
  397. $coff = $off;
  398. $coff_set = 1;
  399. #warn "CSB: mark coff<$coff>\n";
  400. }
  401. }
  402. if (($type eq '' || $type eq '{') && $c eq '{') {
  403. $level++;
  404. $type = '{';
  405. }
  406. if ($type eq '{' && $c eq '}') {
  407. $level--;
  408. $type = ($level != 0)? '{' : '';
  409. if ($level == 0) {
  410. last;
  411. }
  412. }
  413. $off++;
  414. }
  415. if ($off == $len) {
  416. $line++;
  417. $remain--;
  418. }
  419. my $statement = substr($blk, $soff, $off - $soff + 1);
  420. my $condition = substr($blk, $soff, $coff - $soff + 1);
  421. #warn "STATEMENT<$statement>\n";
  422. #warn "CONDITION<$condition>\n";
  423. #print "coff<$coff> soff<$off> loff<$loff>\n";
  424. return ($statement, $condition,
  425. $line, $remain + 1, $off - $loff + 1, $level);
  426. }
  427. sub statement_lines {
  428. my ($stmt) = @_;
  429. # Strip the diff line prefixes and rip blank lines at start and end.
  430. $stmt =~ s/(^|\n)./$1/g;
  431. $stmt =~ s/^\s*//;
  432. $stmt =~ s/\s*$//;
  433. my @stmt_lines = ($stmt =~ /\n/g);
  434. return $#stmt_lines + 2;
  435. }
  436. sub statement_rawlines {
  437. my ($stmt) = @_;
  438. my @stmt_lines = ($stmt =~ /\n/g);
  439. return $#stmt_lines + 2;
  440. }
  441. sub statement_block_size {
  442. my ($stmt) = @_;
  443. $stmt =~ s/(^|\n)./$1/g;
  444. $stmt =~ s/^\s*{//;
  445. $stmt =~ s/}\s*$//;
  446. $stmt =~ s/^\s*//;
  447. $stmt =~ s/\s*$//;
  448. my @stmt_lines = ($stmt =~ /\n/g);
  449. my @stmt_statements = ($stmt =~ /;/g);
  450. my $stmt_lines = $#stmt_lines + 2;
  451. my $stmt_statements = $#stmt_statements + 1;
  452. if ($stmt_lines > $stmt_statements) {
  453. return $stmt_lines;
  454. } else {
  455. return $stmt_statements;
  456. }
  457. }
  458. sub ctx_statement_full {
  459. my ($linenr, $remain, $off) = @_;
  460. my ($statement, $condition, $level);
  461. my (@chunks);
  462. # Grab the first conditional/block pair.
  463. ($statement, $condition, $linenr, $remain, $off, $level) =
  464. ctx_statement_block($linenr, $remain, $off);
  465. #print "F: c<$condition> s<$statement> remain<$remain>\n";
  466. push(@chunks, [ $condition, $statement ]);
  467. if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
  468. return ($level, $linenr, @chunks);
  469. }
  470. # Pull in the following conditional/block pairs and see if they
  471. # could continue the statement.
  472. for (;;) {
  473. ($statement, $condition, $linenr, $remain, $off, $level) =
  474. ctx_statement_block($linenr, $remain, $off);
  475. #print "C: c<$condition> s<$statement> remain<$remain>\n";
  476. last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
  477. #print "C: push\n";
  478. push(@chunks, [ $condition, $statement ]);
  479. }
  480. return ($level, $linenr, @chunks);
  481. }
  482. sub ctx_block_get {
  483. my ($linenr, $remain, $outer, $open, $close, $off) = @_;
  484. my $line;
  485. my $start = $linenr - 1;
  486. my $blk = '';
  487. my @o;
  488. my @c;
  489. my @res = ();
  490. my $level = 0;
  491. for ($line = $start; $remain > 0; $line++) {
  492. next if ($rawlines[$line] =~ /^-/);
  493. $remain--;
  494. $blk .= $rawlines[$line];
  495. foreach my $c (split(//, $rawlines[$line])) {
  496. ##print "C<$c>L<$level><$open$close>O<$off>\n";
  497. if ($off > 0) {
  498. $off--;
  499. next;
  500. }
  501. if ($c eq $close && $level > 0) {
  502. $level--;
  503. last if ($level == 0);
  504. } elsif ($c eq $open) {
  505. $level++;
  506. }
  507. }
  508. if (!$outer || $level <= 1) {
  509. push(@res, $rawlines[$line]);
  510. }
  511. last if ($level == 0);
  512. }
  513. return ($level, @res);
  514. }
  515. sub ctx_block_outer {
  516. my ($linenr, $remain) = @_;
  517. my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
  518. return @r;
  519. }
  520. sub ctx_block {
  521. my ($linenr, $remain) = @_;
  522. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  523. return @r;
  524. }
  525. sub ctx_statement {
  526. my ($linenr, $remain, $off) = @_;
  527. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  528. return @r;
  529. }
  530. sub ctx_block_level {
  531. my ($linenr, $remain) = @_;
  532. return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  533. }
  534. sub ctx_statement_level {
  535. my ($linenr, $remain, $off) = @_;
  536. return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  537. }
  538. sub ctx_locate_comment {
  539. my ($first_line, $end_line) = @_;
  540. # Catch a comment on the end of the line itself.
  541. my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
  542. return $current_comment if (defined $current_comment);
  543. # Look through the context and try and figure out if there is a
  544. # comment.
  545. my $in_comment = 0;
  546. $current_comment = '';
  547. for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
  548. my $line = $rawlines[$linenr - 1];
  549. #warn " $line\n";
  550. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  551. $in_comment = 1;
  552. }
  553. if ($line =~ m@/\*@) {
  554. $in_comment = 1;
  555. }
  556. if (!$in_comment && $current_comment ne '') {
  557. $current_comment = '';
  558. }
  559. $current_comment .= $line . "\n" if ($in_comment);
  560. if ($line =~ m@\*/@) {
  561. $in_comment = 0;
  562. }
  563. }
  564. chomp($current_comment);
  565. return($current_comment);
  566. }
  567. sub ctx_has_comment {
  568. my ($first_line, $end_line) = @_;
  569. my $cmt = ctx_locate_comment($first_line, $end_line);
  570. ##print "LINE: $rawlines[$end_line - 1 ]\n";
  571. ##print "CMMT: $cmt\n";
  572. return ($cmt ne '');
  573. }
  574. sub cat_vet {
  575. my ($vet) = @_;
  576. my ($res, $coded);
  577. $res = '';
  578. while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
  579. $res .= $1;
  580. if ($2 ne '') {
  581. $coded = sprintf("^%c", unpack('C', $2) + 64);
  582. $res .= $coded;
  583. }
  584. }
  585. $res =~ s/$/\$/;
  586. return $res;
  587. }
  588. my $av_preprocessor = 0;
  589. my $av_pending;
  590. my @av_paren_type;
  591. sub annotate_reset {
  592. $av_preprocessor = 0;
  593. $av_pending = '_';
  594. @av_paren_type = ('E');
  595. }
  596. sub annotate_values {
  597. my ($stream, $type) = @_;
  598. my $res;
  599. my $cur = $stream;
  600. print "$stream\n" if ($dbg_values > 1);
  601. while (length($cur)) {
  602. @av_paren_type = ('E') if ($#av_paren_type < 0);
  603. print " <" . join('', @av_paren_type) .
  604. "> <$type> <$av_pending>" if ($dbg_values > 1);
  605. if ($cur =~ /^(\s+)/o) {
  606. print "WS($1)\n" if ($dbg_values > 1);
  607. if ($1 =~ /\n/ && $av_preprocessor) {
  608. $type = pop(@av_paren_type);
  609. $av_preprocessor = 0;
  610. }
  611. } elsif ($cur =~ /^($Type)/) {
  612. print "DECLARE($1)\n" if ($dbg_values > 1);
  613. $type = 'T';
  614. } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
  615. print "DEFINE($1,$2)\n" if ($dbg_values > 1);
  616. $av_preprocessor = 1;
  617. push(@av_paren_type, $type);
  618. if ($2 ne '') {
  619. $av_pending = 'N';
  620. }
  621. $type = 'E';
  622. } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
  623. print "UNDEF($1)\n" if ($dbg_values > 1);
  624. $av_preprocessor = 1;
  625. push(@av_paren_type, $type);
  626. } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
  627. print "PRE_START($1)\n" if ($dbg_values > 1);
  628. $av_preprocessor = 1;
  629. push(@av_paren_type, $type);
  630. push(@av_paren_type, $type);
  631. $type = 'E';
  632. } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
  633. print "PRE_RESTART($1)\n" if ($dbg_values > 1);
  634. $av_preprocessor = 1;
  635. push(@av_paren_type, $av_paren_type[$#av_paren_type]);
  636. $type = 'E';
  637. } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
  638. print "PRE_END($1)\n" if ($dbg_values > 1);
  639. $av_preprocessor = 1;
  640. # Assume all arms of the conditional end as this
  641. # one does, and continue as if the #endif was not here.
  642. pop(@av_paren_type);
  643. push(@av_paren_type, $type);
  644. $type = 'E';
  645. } elsif ($cur =~ /^(\\\n)/o) {
  646. print "PRECONT($1)\n" if ($dbg_values > 1);
  647. } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
  648. print "ATTR($1)\n" if ($dbg_values > 1);
  649. $av_pending = $type;
  650. $type = 'N';
  651. } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
  652. print "SIZEOF($1)\n" if ($dbg_values > 1);
  653. if (defined $2) {
  654. $av_pending = 'V';
  655. }
  656. $type = 'N';
  657. } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
  658. print "COND($1)\n" if ($dbg_values > 1);
  659. $av_pending = 'N';
  660. $type = 'N';
  661. } elsif ($cur =~/^(return|case|else)/o) {
  662. print "KEYWORD($1)\n" if ($dbg_values > 1);
  663. $type = 'N';
  664. } elsif ($cur =~ /^(\()/o) {
  665. print "PAREN('$1')\n" if ($dbg_values > 1);
  666. push(@av_paren_type, $av_pending);
  667. $av_pending = '_';
  668. $type = 'N';
  669. } elsif ($cur =~ /^(\))/o) {
  670. my $new_type = pop(@av_paren_type);
  671. if ($new_type ne '_') {
  672. $type = $new_type;
  673. print "PAREN('$1') -> $type\n"
  674. if ($dbg_values > 1);
  675. } else {
  676. print "PAREN('$1')\n" if ($dbg_values > 1);
  677. }
  678. } elsif ($cur =~ /^($Ident)\(/o) {
  679. print "FUNC($1)\n" if ($dbg_values > 1);
  680. $av_pending = 'V';
  681. } elsif ($cur =~ /^($Ident|$Constant)/o) {
  682. print "IDENT($1)\n" if ($dbg_values > 1);
  683. $type = 'V';
  684. } elsif ($cur =~ /^($Assignment)/o) {
  685. print "ASSIGN($1)\n" if ($dbg_values > 1);
  686. $type = 'N';
  687. } elsif ($cur =~/^(;|{|})/) {
  688. print "END($1)\n" if ($dbg_values > 1);
  689. $type = 'E';
  690. } elsif ($cur =~ /^(;|\?|:|\[)/o) {
  691. print "CLOSE($1)\n" if ($dbg_values > 1);
  692. $type = 'N';
  693. } elsif ($cur =~ /^($Operators)/o) {
  694. print "OP($1)\n" if ($dbg_values > 1);
  695. if ($1 ne '++' && $1 ne '--') {
  696. $type = 'N';
  697. }
  698. } elsif ($cur =~ /(^.)/o) {
  699. print "C($1)\n" if ($dbg_values > 1);
  700. }
  701. if (defined $1) {
  702. $cur = substr($cur, length($1));
  703. $res .= $type x length($1);
  704. }
  705. }
  706. return $res;
  707. }
  708. sub possible {
  709. my ($possible, $line) = @_;
  710. print "CHECK<$possible> ($line)\n" if ($dbg_possible > 1);
  711. if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
  712. $possible ne 'goto' && $possible ne 'return' &&
  713. $possible ne 'case' && $possible ne 'else' &&
  714. $possible ne 'asm' &&
  715. $possible !~ /^(typedef|struct|enum)\b/) {
  716. # Check for modifiers.
  717. $possible =~ s/\s*$Storage\s*//g;
  718. $possible =~ s/\s*$Sparse\s*//g;
  719. if ($possible =~ /^\s*$/) {
  720. } elsif ($possible =~ /\s/) {
  721. $possible =~ s/\s*$Type\s*//g;
  722. warn "MODIFIER: $possible ($line)\n" if ($dbg_possible);
  723. push(@modifierList, $possible);
  724. } else {
  725. warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
  726. push(@typeList, $possible);
  727. }
  728. build_types();
  729. }
  730. }
  731. my $prefix = '';
  732. sub report {
  733. if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
  734. return 0;
  735. }
  736. my $line = $prefix . $_[0];
  737. $line = (split('\n', $line))[0] . "\n" if ($terse);
  738. push(our @report, $line);
  739. return 1;
  740. }
  741. sub report_dump {
  742. our @report;
  743. }
  744. sub ERROR {
  745. if (report("ERROR: $_[0]\n")) {
  746. our $clean = 0;
  747. our $cnt_error++;
  748. }
  749. }
  750. sub WARN {
  751. if (report("WARNING: $_[0]\n")) {
  752. our $clean = 0;
  753. our $cnt_warn++;
  754. }
  755. }
  756. sub CHK {
  757. if ($check && report("CHECK: $_[0]\n")) {
  758. our $clean = 0;
  759. our $cnt_chk++;
  760. }
  761. }
  762. sub process {
  763. my $filename = shift;
  764. my $linenr=0;
  765. my $prevline="";
  766. my $prevrawline="";
  767. my $stashline="";
  768. my $stashrawline="";
  769. my $length;
  770. my $indent;
  771. my $previndent=0;
  772. my $stashindent=0;
  773. our $clean = 1;
  774. my $signoff = 0;
  775. my $is_patch = 0;
  776. our @report = ();
  777. our $cnt_lines = 0;
  778. our $cnt_error = 0;
  779. our $cnt_warn = 0;
  780. our $cnt_chk = 0;
  781. # Trace the real file/line as we go.
  782. my $realfile = '';
  783. my $realline = 0;
  784. my $realcnt = 0;
  785. my $here = '';
  786. my $in_comment = 0;
  787. my $comment_edge = 0;
  788. my $first_line = 0;
  789. my $prev_values = 'E';
  790. # suppression flags
  791. my %suppress_ifbraces;
  792. # Pre-scan the patch sanitizing the lines.
  793. # Pre-scan the patch looking for any __setup documentation.
  794. #
  795. my @setup_docs = ();
  796. my $setup_docs = 0;
  797. sanitise_line_reset();
  798. my $line;
  799. foreach my $rawline (@rawlines) {
  800. $linenr++;
  801. $line = $rawline;
  802. if ($rawline=~/^\+\+\+\s+(\S+)/) {
  803. $setup_docs = 0;
  804. if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
  805. $setup_docs = 1;
  806. }
  807. #next;
  808. }
  809. if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  810. $realline=$1-1;
  811. if (defined $2) {
  812. $realcnt=$3+1;
  813. } else {
  814. $realcnt=1+1;
  815. }
  816. $in_comment = 0;
  817. # Guestimate if this is a continuing comment. Run
  818. # the context looking for a comment "edge". If this
  819. # edge is a close comment then we must be in a comment
  820. # at context start.
  821. my $edge;
  822. for (my $ln = $linenr + 1; $ln < ($linenr + $realcnt); $ln++) {
  823. next if ($line =~ /^-/);
  824. ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
  825. last if (defined $edge);
  826. }
  827. if (defined $edge && $edge eq '*/') {
  828. $in_comment = 1;
  829. }
  830. # Guestimate if this is a continuing comment. If this
  831. # is the start of a diff block and this line starts
  832. # ' *' then it is very likely a comment.
  833. if (!defined $edge &&
  834. $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
  835. {
  836. $in_comment = 1;
  837. }
  838. ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
  839. sanitise_line_reset($in_comment);
  840. } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
  841. # Standardise the strings and chars within the input to
  842. # simplify matching -- only bother with positive lines.
  843. $line = sanitise_line($rawline);
  844. }
  845. push(@lines, $line);
  846. if ($realcnt > 1) {
  847. $realcnt-- if ($line =~ /^(?:\+| |$)/);
  848. } else {
  849. $realcnt = 0;
  850. }
  851. #print "==>$rawline\n";
  852. #print "-->$line\n";
  853. if ($setup_docs && $line =~ /^\+/) {
  854. push(@setup_docs, $line);
  855. }
  856. }
  857. $prefix = '';
  858. $realcnt = 0;
  859. $linenr = 0;
  860. foreach my $line (@lines) {
  861. $linenr++;
  862. my $rawline = $rawlines[$linenr - 1];
  863. #extract the line range in the file after the patch is applied
  864. if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  865. $is_patch = 1;
  866. $first_line = $linenr + 1;
  867. $realline=$1-1;
  868. if (defined $2) {
  869. $realcnt=$3+1;
  870. } else {
  871. $realcnt=1+1;
  872. }
  873. annotate_reset();
  874. $prev_values = 'E';
  875. %suppress_ifbraces = ();
  876. next;
  877. # track the line number as we move through the hunk, note that
  878. # new versions of GNU diff omit the leading space on completely
  879. # blank context lines so we need to count that too.
  880. } elsif ($line =~ /^( |\+|$)/) {
  881. $realline++;
  882. $realcnt-- if ($realcnt != 0);
  883. # Measure the line length and indent.
  884. ($length, $indent) = line_stats($rawline);
  885. # Track the previous line.
  886. ($prevline, $stashline) = ($stashline, $line);
  887. ($previndent, $stashindent) = ($stashindent, $indent);
  888. ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
  889. #warn "line<$line>\n";
  890. } elsif ($realcnt == 1) {
  891. $realcnt--;
  892. }
  893. #make up the handle for any error we report on this line
  894. $prefix = "$filename:$realline: " if ($emacs && $file);
  895. $prefix = "$filename:$linenr: " if ($emacs && !$file);
  896. $here = "#$linenr: " if (!$file);
  897. $here = "#$realline: " if ($file);
  898. # extract the filename as it passes
  899. if ($line=~/^\+\+\+\s+(\S+)/) {
  900. $realfile = $1;
  901. $realfile =~ s@^[^/]*/@@;
  902. if ($realfile =~ m@include/asm/@) {
  903. ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
  904. }
  905. next;
  906. }
  907. $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
  908. my $hereline = "$here\n$rawline\n";
  909. my $herecurr = "$here\n$rawline\n";
  910. my $hereprev = "$here\n$prevrawline\n$rawline\n";
  911. $cnt_lines++ if ($realcnt != 0);
  912. #check the patch for a signoff:
  913. if ($line =~ /^\s*signed-off-by:/i) {
  914. # This is a signoff, if ugly, so do not double report.
  915. $signoff++;
  916. if (!($line =~ /^\s*Signed-off-by:/)) {
  917. WARN("Signed-off-by: is the preferred form\n" .
  918. $herecurr);
  919. }
  920. if ($line =~ /^\s*signed-off-by:\S/i) {
  921. WARN("space required after Signed-off-by:\n" .
  922. $herecurr);
  923. }
  924. }
  925. # Check for wrappage within a valid hunk of the file
  926. if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
  927. ERROR("patch seems to be corrupt (line wrapped?)\n" .
  928. $herecurr) if (!$emitted_corrupt++);
  929. }
  930. # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
  931. if (($realfile =~ /^$/ || $line =~ /^\+/) &&
  932. $rawline !~ m/^$UTF8*$/) {
  933. my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
  934. my $blank = copy_spacing($rawline);
  935. my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
  936. my $hereptr = "$hereline$ptr\n";
  937. ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
  938. }
  939. #ignore lines being removed
  940. if ($line=~/^-/) {next;}
  941. # check we are in a valid source file if not then ignore this hunk
  942. next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
  943. #trailing whitespace
  944. if ($line =~ /^\+.*\015/) {
  945. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  946. ERROR("DOS line endings\n" . $herevet);
  947. } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
  948. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  949. ERROR("trailing whitespace\n" . $herevet);
  950. }
  951. #80 column limit
  952. if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
  953. $rawline !~ /^.\s*\*\s*\@$Ident\s/ && $length > 80)
  954. {
  955. WARN("line over 80 characters\n" . $herecurr);
  956. }
  957. # check for adding lines without a newline.
  958. if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
  959. WARN("adding a line without newline at end of file\n" . $herecurr);
  960. }
  961. # check we are in a valid source file *.[hc] if not then ignore this hunk
  962. next if ($realfile !~ /\.[hc]$/);
  963. # at the beginning of a line any tabs must come first and anything
  964. # more than 8 must use tabs.
  965. if ($rawline =~ /^\+\s* \t\s*\S/ ||
  966. $rawline =~ /^\+\s* \s*/) {
  967. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  968. ERROR("code indent should use tabs where possible\n" . $herevet);
  969. }
  970. # check for RCS/CVS revision markers
  971. if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
  972. WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
  973. }
  974. # Check for potential 'bare' types
  975. my ($stat, $cond);
  976. if ($realcnt && $line =~ /.\s*\S/) {
  977. ($stat, $cond) = ctx_statement_block($linenr,
  978. $realcnt, 0);
  979. $stat =~ s/\n./\n /g;
  980. $cond =~ s/\n./\n /g;
  981. my $s = $stat;
  982. $s =~ s/{.*$//s;
  983. # Ignore goto labels.
  984. if ($s =~ /$Ident:\*$/s) {
  985. # Ignore functions being called
  986. } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
  987. # declarations always start with types
  988. } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))\s*(?:;|=|,|\()/s) {
  989. my $type = $1;
  990. $type =~ s/\s+/ /g;
  991. possible($type, "A:" . $s);
  992. # definitions in global scope can only start with types
  993. } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) {
  994. possible($1, "B:" . $s);
  995. }
  996. # any (foo ... *) is a pointer cast, and foo is a type
  997. while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) {
  998. possible($1, "C:" . $s);
  999. }
  1000. # Check for any sort of function declaration.
  1001. # int foo(something bar, other baz);
  1002. # void (*store_gdt)(x86_descr_ptr *);
  1003. if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
  1004. my ($name_len) = length($1);
  1005. my $ctx = $s;
  1006. substr($ctx, 0, $name_len + 1, '');
  1007. $ctx =~ s/\)[^\)]*$//;
  1008. for my $arg (split(/\s*,\s*/, $ctx)) {
  1009. if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
  1010. possible($1, "D:" . $s);
  1011. }
  1012. }
  1013. }
  1014. }
  1015. #
  1016. # Checks which may be anchored in the context.
  1017. #
  1018. # Check for switch () and associated case and default
  1019. # statements should be at the same indent.
  1020. if ($line=~/\bswitch\s*\(.*\)/) {
  1021. my $err = '';
  1022. my $sep = '';
  1023. my @ctx = ctx_block_outer($linenr, $realcnt);
  1024. shift(@ctx);
  1025. for my $ctx (@ctx) {
  1026. my ($clen, $cindent) = line_stats($ctx);
  1027. if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  1028. $indent != $cindent) {
  1029. $err .= "$sep$ctx\n";
  1030. $sep = '';
  1031. } else {
  1032. $sep = "[...]\n";
  1033. }
  1034. }
  1035. if ($err ne '') {
  1036. ERROR("switch and case should be at the same indent\n$hereline$err");
  1037. }
  1038. }
  1039. # if/while/etc brace do not go on next line, unless defining a do while loop,
  1040. # or if that brace on the next line is for something else
  1041. if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
  1042. my $pre_ctx = "$1$2";
  1043. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
  1044. my $ctx_ln = $linenr + $#ctx + 1;
  1045. my $ctx_cnt = $realcnt - $#ctx - 1;
  1046. my $ctx = join("\n", @ctx);
  1047. ##warn "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
  1048. # Skip over any removed lines in the context following statement.
  1049. while (defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^-/) {
  1050. $ctx_ln++;
  1051. }
  1052. ##warn "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
  1053. if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
  1054. ERROR("that open brace { should be on the previous line\n" .
  1055. "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
  1056. }
  1057. if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
  1058. $ctx =~ /\)\s*\;\s*$/ &&
  1059. defined $lines[$ctx_ln - 1])
  1060. {
  1061. my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
  1062. if ($nindent > $indent) {
  1063. WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
  1064. "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
  1065. }
  1066. }
  1067. }
  1068. # Track the 'values' across context and added lines.
  1069. my $opline = $line; $opline =~ s/^./ /;
  1070. my $curr_values = annotate_values($opline . "\n", $prev_values);
  1071. $curr_values = $prev_values . $curr_values;
  1072. if ($dbg_values) {
  1073. my $outline = $opline; $outline =~ s/\t/ /g;
  1074. print "$linenr > .$outline\n";
  1075. print "$linenr > $curr_values\n";
  1076. }
  1077. $prev_values = substr($curr_values, -1);
  1078. #ignore lines not being added
  1079. if ($line=~/^[^\+]/) {next;}
  1080. # TEST: allow direct testing of the type matcher.
  1081. if ($tst_type && $line =~ /^.$Declare$/) {
  1082. ERROR("TEST: is type $Declare\n" . $herecurr);
  1083. next;
  1084. }
  1085. # check for initialisation to aggregates open brace on the next line
  1086. if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
  1087. $line =~ /^.\s*{/) {
  1088. ERROR("that open brace { should be on the previous line\n" . $hereprev);
  1089. }
  1090. #
  1091. # Checks which are anchored on the added line.
  1092. #
  1093. # check for malformed paths in #include statements (uses RAW line)
  1094. if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
  1095. my $path = $1;
  1096. if ($path =~ m{//}) {
  1097. ERROR("malformed #include filename\n" .
  1098. $herecurr);
  1099. }
  1100. }
  1101. # no C99 // comments
  1102. if ($line =~ m{//}) {
  1103. ERROR("do not use C99 // comments\n" . $herecurr);
  1104. }
  1105. # Remove C99 comments.
  1106. $line =~ s@//.*@@;
  1107. $opline =~ s@//.*@@;
  1108. #EXPORT_SYMBOL should immediately follow its function closing }.
  1109. if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
  1110. ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  1111. my $name = $1;
  1112. if (($prevline !~ /^}/) &&
  1113. ($prevline !~ /^\+}/) &&
  1114. ($prevline !~ /^ }/) &&
  1115. ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
  1116. ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
  1117. ($prevline !~ /^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(/) &&
  1118. ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
  1119. WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
  1120. }
  1121. }
  1122. # check for external initialisers.
  1123. if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
  1124. ERROR("do not initialise externals to 0 or NULL\n" .
  1125. $herecurr);
  1126. }
  1127. # check for static initialisers.
  1128. if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
  1129. ERROR("do not initialise statics to 0 or NULL\n" .
  1130. $herecurr);
  1131. }
  1132. # check for new typedefs, only function parameters and sparse annotations
  1133. # make sense.
  1134. if ($line =~ /\btypedef\s/ &&
  1135. $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
  1136. $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
  1137. $line !~ /\b__bitwise(?:__|)\b/) {
  1138. WARN("do not add new typedefs\n" . $herecurr);
  1139. }
  1140. # * goes on variable not on type
  1141. if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
  1142. ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
  1143. $herecurr);
  1144. } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
  1145. ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
  1146. $herecurr);
  1147. } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
  1148. ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
  1149. $herecurr);
  1150. } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
  1151. ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
  1152. $herecurr);
  1153. }
  1154. # # no BUG() or BUG_ON()
  1155. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  1156. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  1157. # print "$herecurr";
  1158. # $clean = 0;
  1159. # }
  1160. if ($line =~ /\bLINUX_VERSION_CODE\b/) {
  1161. WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
  1162. }
  1163. # printk should use KERN_* levels. Note that follow on printk's on the
  1164. # same line do not need a level, so we use the current block context
  1165. # to try and find and validate the current printk. In summary the current
  1166. # printk includes all preceeding printk's which have no newline on the end.
  1167. # we assume the first bad printk is the one to report.
  1168. if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
  1169. my $ok = 0;
  1170. for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
  1171. #print "CHECK<$lines[$ln - 1]\n";
  1172. # we have a preceeding printk if it ends
  1173. # with "\n" ignore it, else it is to blame
  1174. if ($lines[$ln - 1] =~ m{\bprintk\(}) {
  1175. if ($rawlines[$ln - 1] !~ m{\\n"}) {
  1176. $ok = 1;
  1177. }
  1178. last;
  1179. }
  1180. }
  1181. if ($ok == 0) {
  1182. WARN("printk() should include KERN_ facility level\n" . $herecurr);
  1183. }
  1184. }
  1185. # function brace can't be on same line, except for #defines of do while,
  1186. # or if closed on same line
  1187. if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
  1188. !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
  1189. ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
  1190. }
  1191. # open braces for enum, union and struct go on the same line.
  1192. if ($line =~ /^.\s*{/ &&
  1193. $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
  1194. ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
  1195. }
  1196. # check for spaces between functions and their parentheses.
  1197. while ($line =~ /($Ident)\s+\(/g) {
  1198. my $name = $1;
  1199. my $ctx_before = substr($line, 0, $-[1]);
  1200. my $ctx = "$ctx_before$name";
  1201. # Ignore those directives where spaces _are_ permitted.
  1202. if ($name =~ /^(?:
  1203. if|for|while|switch|return|case|
  1204. volatile|__volatile__|
  1205. __attribute__|format|__extension__|
  1206. asm|__asm__)$/x)
  1207. {
  1208. # cpp #define statements have non-optional spaces, ie
  1209. # if there is a space between the name and the open
  1210. # parenthesis it is simply not a parameter group.
  1211. } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
  1212. # cpp #elif statement condition may start with a (
  1213. } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
  1214. # If this whole things ends with a type its most
  1215. # likely a typedef for a function.
  1216. } elsif ($ctx =~ /$Type$/) {
  1217. } else {
  1218. WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
  1219. }
  1220. }
  1221. # Check operator spacing.
  1222. if (!($line=~/\#\s*include/)) {
  1223. my $ops = qr{
  1224. <<=|>>=|<=|>=|==|!=|
  1225. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  1226. =>|->|<<|>>|<|>|=|!|~|
  1227. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
  1228. }x;
  1229. my @elements = split(/($ops|;)/, $opline);
  1230. my $off = 0;
  1231. my $blank = copy_spacing($opline);
  1232. for (my $n = 0; $n < $#elements; $n += 2) {
  1233. $off += length($elements[$n]);
  1234. # Pick up the preceeding and succeeding characters.
  1235. my $ca = substr($opline, 0, $off);
  1236. my $cc = '';
  1237. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  1238. $cc = substr($opline, $off + length($elements[$n + 1]));
  1239. }
  1240. my $cb = "$ca$;$cc";
  1241. my $a = '';
  1242. $a = 'V' if ($elements[$n] ne '');
  1243. $a = 'W' if ($elements[$n] =~ /\s$/);
  1244. $a = 'C' if ($elements[$n] =~ /$;$/);
  1245. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  1246. $a = 'O' if ($elements[$n] eq '');
  1247. $a = 'E' if ($ca =~ /^\s*$/);
  1248. my $op = $elements[$n + 1];
  1249. my $c = '';
  1250. if (defined $elements[$n + 2]) {
  1251. $c = 'V' if ($elements[$n + 2] ne '');
  1252. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  1253. $c = 'C' if ($elements[$n + 2] =~ /^$;/);
  1254. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  1255. $c = 'O' if ($elements[$n + 2] eq '');
  1256. $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
  1257. } else {
  1258. $c = 'E';
  1259. }
  1260. my $ctx = "${a}x${c}";
  1261. my $at = "(ctx:$ctx)";
  1262. my $ptr = substr($blank, 0, $off) . "^";
  1263. my $hereptr = "$hereline$ptr\n";
  1264. # Classify operators into binary, unary, or
  1265. # definitions (* only) where they have more
  1266. # than one mode.
  1267. my $op_type = substr($curr_values, $off + 1, 1);
  1268. my $op_left = substr($curr_values, $off, 1);
  1269. my $is_unary;
  1270. if ($op_type eq 'T') {
  1271. $is_unary = 2;
  1272. } elsif ($op_left eq 'V') {
  1273. $is_unary = 0;
  1274. } else {
  1275. $is_unary = 1;
  1276. }
  1277. #if ($op eq '-' || $op eq '&' || $op eq '*') {
  1278. # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
  1279. #}
  1280. # Ignore operators passed as parameters.
  1281. if ($op_type ne 'V' &&
  1282. $ca =~ /\s$/ && $cc =~ /^\s*,/) {
  1283. # # Ignore comments
  1284. # } elsif ($op =~ /^$;+$/) {
  1285. # ; should have either the end of line or a space or \ after it
  1286. } elsif ($op eq ';') {
  1287. if ($ctx !~ /.x[WEBC]/ &&
  1288. $cc !~ /^\\/ && $cc !~ /^;/) {
  1289. ERROR("space required after that '$op' $at\n" . $hereptr);
  1290. }
  1291. # // is a comment
  1292. } elsif ($op eq '//') {
  1293. # -> should have no spaces
  1294. } elsif ($op eq '->') {
  1295. if ($ctx =~ /Wx.|.xW/) {
  1296. ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
  1297. }
  1298. # , must have a space on the right.
  1299. } elsif ($op eq ',') {
  1300. if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
  1301. ERROR("space required after that '$op' $at\n" . $hereptr);
  1302. }
  1303. # '*' as part of a type definition -- reported already.
  1304. } elsif ($op eq '*' && $is_unary == 2) {
  1305. #warn "'*' is part of type\n";
  1306. # unary operators should have a space before and
  1307. # none after. May be left adjacent to another
  1308. # unary operator, or a cast
  1309. } elsif ($op eq '!' || $op eq '~' ||
  1310. ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
  1311. if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  1312. ERROR("space required before that '$op' $at\n" . $hereptr);
  1313. }
  1314. if ($op eq '*' && $cc =~/\s*const\b/) {
  1315. # A unary '*' may be const
  1316. } elsif ($ctx =~ /.xW/) {
  1317. ERROR("space prohibited after that '$op' $at\n" . $hereptr);
  1318. }
  1319. # unary ++ and unary -- are allowed no space on one side.
  1320. } elsif ($op eq '++' or $op eq '--') {
  1321. if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
  1322. ERROR("space required one side of that '$op' $at\n" . $hereptr);
  1323. }
  1324. if ($ctx =~ /Wx[BE]/ ||
  1325. ($ctx =~ /Wx./ && $cc =~ /^;/)) {
  1326. ERROR("space prohibited before that '$op' $at\n" . $hereptr);
  1327. }
  1328. if ($ctx =~ /ExW/) {
  1329. ERROR("space prohibited after that '$op' $at\n" . $hereptr);
  1330. }
  1331. # << and >> may either have or not have spaces both sides
  1332. } elsif ($op eq '<<' or $op eq '>>' or
  1333. $op eq '&' or $op eq '^' or $op eq '|' or
  1334. $op eq '+' or $op eq '-' or
  1335. $op eq '*' or $op eq '/' or
  1336. $op eq '%')
  1337. {
  1338. if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
  1339. ERROR("need consistent spacing around '$op' $at\n" .
  1340. $hereptr);
  1341. }
  1342. # All the others need spaces both sides.
  1343. } elsif ($ctx !~ /[EWC]x[CWE]/) {
  1344. # Ignore email addresses <foo@bar>
  1345. if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
  1346. !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
  1347. ERROR("spaces required around that '$op' $at\n" . $hereptr);
  1348. }
  1349. }
  1350. $off += length($elements[$n + 1]);
  1351. }
  1352. }
  1353. # check for multiple assignments
  1354. if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
  1355. CHK("multiple assignments should be avoided\n" . $herecurr);
  1356. }
  1357. ## # check for multiple declarations, allowing for a function declaration
  1358. ## # continuation.
  1359. ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  1360. ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  1361. ##
  1362. ## # Remove any bracketed sections to ensure we do not
  1363. ## # falsly report the parameters of functions.
  1364. ## my $ln = $line;
  1365. ## while ($ln =~ s/\([^\(\)]*\)//g) {
  1366. ## }
  1367. ## if ($ln =~ /,/) {
  1368. ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
  1369. ## }
  1370. ## }
  1371. #need space before brace following if, while, etc
  1372. if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
  1373. $line =~ /do{/) {
  1374. ERROR("space required before the open brace '{'\n" . $herecurr);
  1375. }
  1376. # closing brace should have a space following it when it has anything
  1377. # on the line
  1378. if ($line =~ /}(?!(?:,|;|\)))\S/) {
  1379. ERROR("space required after that close brace '}'\n" . $herecurr);
  1380. }
  1381. # check spacing on square brackets
  1382. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  1383. ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
  1384. }
  1385. if ($line =~ /\s\]/) {
  1386. ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
  1387. }
  1388. # check spacing on parentheses
  1389. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  1390. $line !~ /for\s*\(\s+;/) {
  1391. ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
  1392. }
  1393. if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
  1394. $line !~ /for\s*\(.*;\s+\)/ &&
  1395. $line !~ /:\s+\)/) {
  1396. ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
  1397. }
  1398. #goto labels aren't indented, allow a single space however
  1399. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  1400. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  1401. WARN("labels should not be indented\n" . $herecurr);
  1402. }
  1403. # Return is not a function.
  1404. if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
  1405. my $spacing = $1;
  1406. my $value = $2;
  1407. # Flatten any parentheses and braces
  1408. $value =~ s/\)\(/\) \(/g;
  1409. while ($value =~ s/\([^\(\)]*\)/1/) {
  1410. }
  1411. if ($value =~ /^(?:$Ident|-?$Constant)$/) {
  1412. ERROR("return is not a function, parentheses are not required\n" . $herecurr);
  1413. } elsif ($spacing !~ /\s+/) {
  1414. ERROR("space required before the open parenthesis '('\n" . $herecurr);
  1415. }
  1416. }
  1417. # Need a space before open parenthesis after if, while etc
  1418. if ($line=~/\b(if|while|for|switch)\(/) {
  1419. ERROR("space required before the open parenthesis '('\n" . $herecurr);
  1420. }
  1421. # Check for illegal assignment in if conditional.
  1422. if ($line =~ /\bif\s*\(/) {
  1423. my ($s, $c) = ($stat, $cond);
  1424. if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
  1425. ERROR("do not use assignment in if condition\n" . $herecurr);
  1426. }
  1427. # Find out what is on the end of the line after the
  1428. # conditional.
  1429. substr($s, 0, length($c), '');
  1430. $s =~ s/\n.*//g;
  1431. $s =~ s/$;//g; # Remove any comments
  1432. if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/ &&
  1433. $c !~ /^.\s*\#\s*if/)
  1434. {
  1435. ERROR("trailing statements should be on next line\n" . $herecurr);
  1436. }
  1437. }
  1438. # Check for bitwise tests written as boolean
  1439. if ($line =~ /
  1440. (?:
  1441. (?:\[|\(|\&\&|\|\|)
  1442. \s*0[xX][0-9]+\s*
  1443. (?:\&\&|\|\|)
  1444. |
  1445. (?:\&\&|\|\|)
  1446. \s*0[xX][0-9]+\s*
  1447. (?:\&\&|\|\||\)|\])
  1448. )/x)
  1449. {
  1450. WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
  1451. }
  1452. # if and else should not have general statements after it
  1453. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
  1454. my $s = $1;
  1455. $s =~ s/$;//g; # Remove any comments
  1456. if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
  1457. ERROR("trailing statements should be on next line\n" . $herecurr);
  1458. }
  1459. }
  1460. # Check for }<nl>else {, these must be at the same
  1461. # indent level to be relevant to each other.
  1462. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  1463. $previndent == $indent) {
  1464. ERROR("else should follow close brace '}'\n" . $hereprev);
  1465. }
  1466. if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
  1467. $previndent == $indent) {
  1468. my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
  1469. # Find out what is on the end of the line after the
  1470. # conditional.
  1471. substr($s, 0, length($c), '');
  1472. $s =~ s/\n.*//g;
  1473. if ($s =~ /^\s*;/) {
  1474. ERROR("while should follow close brace '}'\n" . $hereprev);
  1475. }
  1476. }
  1477. #studly caps, commented out until figure out how to distinguish between use of existing and adding new
  1478. # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
  1479. # print "No studly caps, use _\n";
  1480. # print "$herecurr";
  1481. # $clean = 0;
  1482. # }
  1483. #no spaces allowed after \ in define
  1484. if ($line=~/\#\s*define.*\\\s$/) {
  1485. WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
  1486. }
  1487. #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
  1488. if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
  1489. my $checkfile = "include/linux/$1.h";
  1490. if (-f "$root/$checkfile" && $realfile ne $checkfile &&
  1491. $1 ne 'irq')
  1492. {
  1493. WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
  1494. $herecurr);
  1495. }
  1496. }
  1497. # multi-statement macros should be enclosed in a do while loop, grab the
  1498. # first statement and ensure its the whole macro if its not enclosed
  1499. # in a known good container
  1500. if ($line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
  1501. my $ln = $linenr;
  1502. my $cnt = $realcnt;
  1503. my ($off, $dstat, $dcond, $rest);
  1504. my $ctx = '';
  1505. my $args = defined($1);
  1506. # Find the end of the macro and limit our statement
  1507. # search to that.
  1508. while ($cnt > 0 && defined $lines[$ln - 1] &&
  1509. $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
  1510. {
  1511. $ctx .= $rawlines[$ln - 1] . "\n";
  1512. $ln++;
  1513. $cnt--;
  1514. }
  1515. $ctx .= $rawlines[$ln - 1];
  1516. ($dstat, $dcond, $ln, $cnt, $off) =
  1517. ctx_statement_block($linenr, $ln - $linenr + 1, 0);
  1518. #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
  1519. #print "LINE<$lines[$ln]> len<" . length($lines[$ln]) . "\n";
  1520. # Extract the remainder of the define (if any) and
  1521. # rip off surrounding spaces, and trailing \'s.
  1522. $rest = '';
  1523. if (defined $lines[$ln - 1] &&
  1524. $off > length($lines[$ln - 1]))
  1525. {
  1526. $ln++;
  1527. $cnt--;
  1528. $off = 0;
  1529. }
  1530. while ($cnt > 0) {
  1531. $rest .= substr($lines[$ln - 1], $off) . "\n";
  1532. $ln++;
  1533. $cnt--;
  1534. $off = 0;
  1535. }
  1536. $rest =~ s/\\\n.//g;
  1537. $rest =~ s/^\s*//s;
  1538. $rest =~ s/\s*$//s;
  1539. # Clean up the original statement.
  1540. if ($args) {
  1541. substr($dstat, 0, length($dcond), '');
  1542. } else {
  1543. $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
  1544. }
  1545. $dstat =~ s/\\\n.//g;
  1546. $dstat =~ s/^\s*//s;
  1547. $dstat =~ s/\s*$//s;
  1548. # Flatten any parentheses and braces
  1549. while ($dstat =~ s/\([^\(\)]*\)/1/) {
  1550. }
  1551. while ($dstat =~ s/\{[^\{\}]*\}/1/) {
  1552. }
  1553. my $exceptions = qr{
  1554. $Declare|
  1555. module_param_named|
  1556. MODULE_PARAM_DESC|
  1557. DECLARE_PER_CPU|
  1558. DEFINE_PER_CPU|
  1559. __typeof__\(
  1560. }x;
  1561. if ($rest ne '') {
  1562. if ($rest !~ /while\s*\(/ &&
  1563. $dstat !~ /$exceptions/)
  1564. {
  1565. ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
  1566. }
  1567. } elsif ($ctx !~ /;/) {
  1568. if ($dstat ne '' &&
  1569. $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
  1570. $dstat !~ /$exceptions/ &&
  1571. $dstat =~ /$Operators/)
  1572. {
  1573. ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
  1574. }
  1575. }
  1576. }
  1577. # check for redundant bracing round if etc
  1578. if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
  1579. my ($level, $endln, @chunks) =
  1580. ctx_statement_full($linenr, $realcnt, 1);
  1581. #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
  1582. #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
  1583. if ($#chunks > 0 && $level == 0) {
  1584. my $allowed = 0;
  1585. my $seen = 0;
  1586. my $herectx = $here . "\n";
  1587. my $ln = $linenr - 1;
  1588. for my $chunk (@chunks) {
  1589. my ($cond, $block) = @{$chunk};
  1590. # If the condition carries leading newlines, then count those as offsets.
  1591. my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
  1592. my $offset = statement_rawlines($whitespace) - 1;
  1593. #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
  1594. # We have looked at and allowed this specific line.
  1595. $suppress_ifbraces{$ln + $offset} = 1;
  1596. $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
  1597. $ln += statement_rawlines($block) - 1;
  1598. substr($block, 0, length($cond), '');
  1599. $seen++ if ($block =~ /^\s*{/);
  1600. #print "cond<$cond> block<$block> allowed<$allowed>\n";
  1601. if (statement_lines($cond) > 1) {
  1602. #print "APW: ALLOWED: cond<$cond>\n";
  1603. $allowed = 1;
  1604. }
  1605. if ($block =~/\b(?:if|for|while)\b/) {
  1606. #print "APW: ALLOWED: block<$block>\n";
  1607. $allowed = 1;
  1608. }
  1609. if (statement_block_size($block) > 1) {
  1610. #print "APW: ALLOWED: lines block<$block>\n";
  1611. $allowed = 1;
  1612. }
  1613. }
  1614. if ($seen && !$allowed) {
  1615. WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
  1616. }
  1617. }
  1618. }
  1619. if (!defined $suppress_ifbraces{$linenr - 1} &&
  1620. $line =~ /\b(if|while|for|else)\b/) {
  1621. my $allowed = 0;
  1622. # Check the pre-context.
  1623. if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
  1624. #print "APW: ALLOWED: pre<$1>\n";
  1625. $allowed = 1;
  1626. }
  1627. my ($level, $endln, @chunks) =
  1628. ctx_statement_full($linenr, $realcnt, $-[0]);
  1629. # Check the condition.
  1630. my ($cond, $block) = @{$chunks[0]};
  1631. #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
  1632. if (defined $cond) {
  1633. substr($block, 0, length($cond), '');
  1634. }
  1635. if (statement_lines($cond) > 1) {
  1636. #print "APW: ALLOWED: cond<$cond>\n";
  1637. $allowed = 1;
  1638. }
  1639. if ($block =~/\b(?:if|for|while)\b/) {
  1640. #print "APW: ALLOWED: block<$block>\n";
  1641. $allowed = 1;
  1642. }
  1643. if (statement_block_size($block) > 1) {
  1644. #print "APW: ALLOWED: lines block<$block>\n";
  1645. $allowed = 1;
  1646. }
  1647. # Check the post-context.
  1648. if (defined $chunks[1]) {
  1649. my ($cond, $block) = @{$chunks[1]};
  1650. if (defined $cond) {
  1651. substr($block, 0, length($cond), '');
  1652. }
  1653. if ($block =~ /^\s*\{/) {
  1654. #print "APW: ALLOWED: chunk-1 block<$block>\n";
  1655. $allowed = 1;
  1656. }
  1657. }
  1658. if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
  1659. my $herectx = $here . "\n";;
  1660. my $end = $linenr + statement_rawlines($block) - 1;
  1661. for (my $ln = $linenr - 1; $ln < $end; $ln++) {
  1662. $herectx .= $rawlines[$ln] . "\n";;
  1663. }
  1664. WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
  1665. }
  1666. }
  1667. # don't include deprecated include files (uses RAW line)
  1668. for my $inc (@dep_includes) {
  1669. if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
  1670. ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  1671. }
  1672. }
  1673. # don't use deprecated functions
  1674. for my $func (@dep_functions) {
  1675. if ($line =~ /\b$func\b/) {
  1676. ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  1677. }
  1678. }
  1679. # no volatiles please
  1680. my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
  1681. if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
  1682. WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
  1683. }
  1684. # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
  1685. if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
  1686. ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
  1687. }
  1688. # warn about #if 0
  1689. if ($line =~ /^.\s*\#\s*if\s+0\b/) {
  1690. CHK("if this code is redundant consider removing it\n" .
  1691. $herecurr);
  1692. }
  1693. # check for needless kfree() checks
  1694. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  1695. my $expr = $1;
  1696. if ($line =~ /\bkfree\(\Q$expr\E\);/) {
  1697. WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
  1698. }
  1699. }
  1700. # warn about #ifdefs in C files
  1701. # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  1702. # print "#ifdef in C files should be avoided\n";
  1703. # print "$herecurr";
  1704. # $clean = 0;
  1705. # }
  1706. # warn about spacing in #ifdefs
  1707. if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
  1708. ERROR("exactly one space required after that #$1\n" . $herecurr);
  1709. }
  1710. # check for spinlock_t definitions without a comment.
  1711. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
  1712. $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
  1713. my $which = $1;
  1714. if (!ctx_has_comment($first_line, $linenr)) {
  1715. CHK("$1 definition without comment\n" . $herecurr);
  1716. }
  1717. }
  1718. # check for memory barriers without a comment.
  1719. if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  1720. if (!ctx_has_comment($first_line, $linenr)) {
  1721. CHK("memory barrier without comment\n" . $herecurr);
  1722. }
  1723. }
  1724. # check of hardware specific defines
  1725. if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
  1726. CHK("architecture specific defines should be avoided\n" . $herecurr);
  1727. }
  1728. # check the location of the inline attribute, that it is between
  1729. # storage class and type.
  1730. if ($line =~ /\b$Type\s+$Inline\b/ ||
  1731. $line =~ /\b$Inline\s+$Storage\b/) {
  1732. ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
  1733. }
  1734. # Check for __inline__ and __inline, prefer inline
  1735. if ($line =~ /\b(__inline__|__inline)\b/) {
  1736. WARN("plain inline is preferred over $1\n" . $herecurr);
  1737. }
  1738. # check for new externs in .c files.
  1739. if ($realfile =~ /\.c$/ && defined $stat &&
  1740. $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
  1741. {
  1742. my $function_name = $1;
  1743. my $paren_space = $2;
  1744. my $s = $stat;
  1745. if (defined $cond) {
  1746. substr($s, 0, length($cond), '');
  1747. }
  1748. if ($s =~ /^\s*;/ &&
  1749. $function_name ne 'uninitialized_var')
  1750. {
  1751. WARN("externs should be avoided in .c files\n" . $herecurr);
  1752. }
  1753. if ($paren_space =~ /\n/) {
  1754. WARN("arguments for function declarations should follow identifier\n" . $herecurr);
  1755. }
  1756. } elsif ($realfile =~ /\.c$/ && defined $stat &&
  1757. $stat =~ /^.\s*extern\s+/)
  1758. {
  1759. WARN("externs should be avoided in .c files\n" . $herecurr);
  1760. }
  1761. # checks for new __setup's
  1762. if ($rawline =~ /\b__setup\("([^"]*)"/) {
  1763. my $name = $1;
  1764. if (!grep(/$name/, @setup_docs)) {
  1765. CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
  1766. }
  1767. }
  1768. # check for pointless casting of kmalloc return
  1769. if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
  1770. WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
  1771. }
  1772. # check for gcc specific __FUNCTION__
  1773. if ($line =~ /__FUNCTION__/) {
  1774. WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
  1775. }
  1776. # check for semaphores used as mutexes
  1777. if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
  1778. WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
  1779. }
  1780. # check for semaphores used as mutexes
  1781. if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
  1782. WARN("consider using a completion\n" . $herecurr);
  1783. }
  1784. # recommend strict_strto* over simple_strto*
  1785. if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
  1786. WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
  1787. }
  1788. # use of NR_CPUS is usually wrong
  1789. # ignore definitions of NR_CPUS and usage to define arrays as likely right
  1790. if ($line =~ /\bNR_CPUS\b/ &&
  1791. $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
  1792. $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
  1793. $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
  1794. $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
  1795. $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
  1796. {
  1797. WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
  1798. }
  1799. # check for %L{u,d,i} in strings
  1800. my $string;
  1801. while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
  1802. $string = substr($rawline, $-[1], $+[1] - $-[1]);
  1803. if ($string =~ /(?<!%)%L[udi]/) {
  1804. WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
  1805. last;
  1806. }
  1807. }
  1808. }
  1809. # If we have no input at all, then there is nothing to report on
  1810. # so just keep quiet.
  1811. if ($#rawlines == -1) {
  1812. exit(0);
  1813. }
  1814. # In mailback mode only produce a report in the negative, for
  1815. # things that appear to be patches.
  1816. if ($mailback && ($clean == 1 || !$is_patch)) {
  1817. exit(0);
  1818. }
  1819. # This is not a patch, and we are are in 'no-patch' mode so
  1820. # just keep quiet.
  1821. if (!$chk_patch && !$is_patch) {
  1822. exit(0);
  1823. }
  1824. if (!$is_patch) {
  1825. ERROR("Does not appear to be a unified-diff format patch\n");
  1826. }
  1827. if ($is_patch && $chk_signoff && $signoff == 0) {
  1828. ERROR("Missing Signed-off-by: line(s)\n");
  1829. }
  1830. print report_dump();
  1831. if ($summary && !($clean == 1 && $quiet == 1)) {
  1832. print "$filename " if ($summary_file);
  1833. print "total: $cnt_error errors, $cnt_warn warnings, " .
  1834. (($check)? "$cnt_chk checks, " : "") .
  1835. "$cnt_lines lines checked\n";
  1836. print "\n" if ($quiet == 0);
  1837. }
  1838. if ($clean == 1 && $quiet == 0) {
  1839. print "$vname has no obvious style problems and is ready for submission.\n"
  1840. }
  1841. if ($clean == 0 && $quiet == 0) {
  1842. print "$vname has style problems, please review. If any of these errors\n";
  1843. print "are false positives report them to the maintainer, see\n";
  1844. print "CHECKPATCH in MAINTAINERS.\n";
  1845. }
  1846. return $clean;
  1847. }