checkpatch.pl 101 KB

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