checkpatch.pl 112 KB

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