2
0

checkpatch.pl 79 KB

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