checkpatch.pl 88 KB

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