checkpatch.pl 80 KB

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