checkpatch.pl 91 KB

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