checkpatch.pl 79 KB

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