checkpatch.pl 68 KB

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