checkpatch.pl 84 KB

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