checkpatch.pl 90 KB

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