2
0

checkpatch.pl 79 KB

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