checkpatch.pl 69 KB

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