checkpatch.pl 74 KB

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