checkpatch.pl 83 KB

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