checkpatch.pl 93 KB

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