checkpatch.pl 76 KB

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