checkpatch.pl 80 KB

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