checkpatch.pl 89 KB

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