checkpatch.pl 104 KB

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