checkpatch.pl 111 KB

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