checkpatch.pl 83 KB

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