checkpatch.pl 58 KB

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