checkpatch.pl 115 KB

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