checkpatch.pl 116 KB

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