checkpatch.pl 66 KB

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