checkpatch.pl 91 KB

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