checkpatch.pl 118 KB

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