checkpatch.pl 113 KB

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