checkpatch.pl 93 KB

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