checkpatch.pl 99 KB

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