checkpatch.pl 55 KB

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