checkpatch.pl 72 KB

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