checkpatch.pl 70 KB

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