checkpatch.pl 71 KB

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