checkpatch.pl 101 KB

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