checkpatch.pl 65 KB

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