checkpatch.pl 104 KB

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