checkpatch.pl 73 KB

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