checkpatch.pl 61 KB

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