checkpatch.pl 101 KB

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