checkpatch.pl 115 KB

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