checkpatch.pl 71 KB

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