2
0

checkpatch.pl 83 KB

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