checkpatch.pl 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188
  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 process {
  1156. my $filename = shift;
  1157. my $linenr=0;
  1158. my $prevline="";
  1159. my $prevrawline="";
  1160. my $stashline="";
  1161. my $stashrawline="";
  1162. my $length;
  1163. my $indent;
  1164. my $previndent=0;
  1165. my $stashindent=0;
  1166. our $clean = 1;
  1167. my $signoff = 0;
  1168. my $is_patch = 0;
  1169. my $in_header_lines = $file ? 0 : 1;
  1170. my $in_commit_log = 0; #Scanning lines before patch
  1171. my $reported_maintainer_file = 0;
  1172. my $reported_mixing_imported_file = 0;
  1173. my $in_imported_file = 0;
  1174. my $in_no_imported_file = 0;
  1175. my $non_utf8_charset = 0;
  1176. our @report = ();
  1177. our $cnt_lines = 0;
  1178. our $cnt_error = 0;
  1179. our $cnt_warn = 0;
  1180. our $cnt_chk = 0;
  1181. # Trace the real file/line as we go.
  1182. my $realfile = '';
  1183. my $realline = 0;
  1184. my $realcnt = 0;
  1185. my $here = '';
  1186. my $in_comment = 0;
  1187. my $comment_edge = 0;
  1188. my $first_line = 0;
  1189. my $p1_prefix = '';
  1190. my $prev_values = 'E';
  1191. # suppression flags
  1192. my %suppress_ifbraces;
  1193. my %suppress_whiletrailers;
  1194. my %suppress_export;
  1195. my $acpi_testexpected;
  1196. my $acpi_nontestexpected;
  1197. # Pre-scan the patch sanitizing the lines.
  1198. sanitise_line_reset();
  1199. my $line;
  1200. foreach my $rawline (@rawlines) {
  1201. $linenr++;
  1202. $line = $rawline;
  1203. if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  1204. $realline=$1-1;
  1205. if (defined $2) {
  1206. $realcnt=$3+1;
  1207. } else {
  1208. $realcnt=1+1;
  1209. }
  1210. $in_comment = 0;
  1211. # Guestimate if this is a continuing comment. Run
  1212. # the context looking for a comment "edge". If this
  1213. # edge is a close comment then we must be in a comment
  1214. # at context start.
  1215. my $edge;
  1216. my $cnt = $realcnt;
  1217. for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
  1218. next if (defined $rawlines[$ln - 1] &&
  1219. $rawlines[$ln - 1] =~ /^-/);
  1220. $cnt--;
  1221. #print "RAW<$rawlines[$ln - 1]>\n";
  1222. last if (!defined $rawlines[$ln - 1]);
  1223. if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
  1224. $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
  1225. ($edge) = $1;
  1226. last;
  1227. }
  1228. }
  1229. if (defined $edge && $edge eq '*/') {
  1230. $in_comment = 1;
  1231. }
  1232. # Guestimate if this is a continuing comment. If this
  1233. # is the start of a diff block and this line starts
  1234. # ' *' then it is very likely a comment.
  1235. if (!defined $edge &&
  1236. $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
  1237. {
  1238. $in_comment = 1;
  1239. }
  1240. ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
  1241. sanitise_line_reset($in_comment);
  1242. } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
  1243. # Standardise the strings and chars within the input to
  1244. # simplify matching -- only bother with positive lines.
  1245. $line = sanitise_line($rawline);
  1246. }
  1247. push(@lines, $line);
  1248. if ($realcnt > 1) {
  1249. $realcnt-- if ($line =~ /^(?:\+| |$)/);
  1250. } else {
  1251. $realcnt = 0;
  1252. }
  1253. #print "==>$rawline\n";
  1254. #print "-->$line\n";
  1255. }
  1256. $prefix = '';
  1257. $realcnt = 0;
  1258. $linenr = 0;
  1259. foreach my $line (@lines) {
  1260. $linenr++;
  1261. my $rawline = $rawlines[$linenr - 1];
  1262. #extract the line range in the file after the patch is applied
  1263. if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  1264. $is_patch = 1;
  1265. $first_line = $linenr + 1;
  1266. $realline=$1-1;
  1267. if (defined $2) {
  1268. $realcnt=$3+1;
  1269. } else {
  1270. $realcnt=1+1;
  1271. }
  1272. annotate_reset();
  1273. $prev_values = 'E';
  1274. %suppress_ifbraces = ();
  1275. %suppress_whiletrailers = ();
  1276. %suppress_export = ();
  1277. next;
  1278. # track the line number as we move through the hunk, note that
  1279. # new versions of GNU diff omit the leading space on completely
  1280. # blank context lines so we need to count that too.
  1281. } elsif ($line =~ /^( |\+|$)/) {
  1282. $realline++;
  1283. $realcnt-- if ($realcnt != 0);
  1284. # Measure the line length and indent.
  1285. ($length, $indent) = line_stats($rawline);
  1286. # Track the previous line.
  1287. ($prevline, $stashline) = ($stashline, $line);
  1288. ($previndent, $stashindent) = ($stashindent, $indent);
  1289. ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
  1290. #warn "line<$line>\n";
  1291. } elsif ($realcnt == 1) {
  1292. $realcnt--;
  1293. }
  1294. my $hunk_line = ($realcnt != 0);
  1295. #make up the handle for any error we report on this line
  1296. $prefix = "$filename:$realline: " if ($emacs && $file);
  1297. $prefix = "$filename:$linenr: " if ($emacs && !$file);
  1298. $here = "#$linenr: " if (!$file);
  1299. $here = "#$realline: " if ($file);
  1300. # extract the filename as it passes
  1301. if ($line =~ /^diff --git.*?(\S+)$/) {
  1302. $realfile = $1;
  1303. $realfile =~ s@^([^/]*)/@@ if (!$file);
  1304. checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected);
  1305. } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
  1306. $realfile = $1;
  1307. $realfile =~ s@^([^/]*)/@@ if (!$file);
  1308. checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected);
  1309. $p1_prefix = $1;
  1310. if (!$file && $tree && $p1_prefix ne '' &&
  1311. -e "$root/$p1_prefix") {
  1312. WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
  1313. }
  1314. next;
  1315. }
  1316. $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
  1317. my $hereline = "$here\n$rawline\n";
  1318. my $herecurr = "$here\n$rawline\n";
  1319. my $hereprev = "$here\n$prevrawline\n$rawline\n";
  1320. $cnt_lines++ if ($realcnt != 0);
  1321. # Check for incorrect file permissions
  1322. if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
  1323. my $permhere = $here . "FILE: $realfile\n";
  1324. if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
  1325. ERROR("do not set execute permissions for source files\n" . $permhere);
  1326. }
  1327. }
  1328. # Only allow Python 3 interpreter
  1329. if ($realline == 1 &&
  1330. $line =~ /^\+#!\ *\/usr\/bin\/(?:env )?python$/) {
  1331. ERROR("please use python3 interpreter\n" . $herecurr);
  1332. }
  1333. # Accept git diff extended headers as valid patches
  1334. if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\.\-]+\s*$/) {
  1335. $is_patch = 1;
  1336. }
  1337. if ($line =~ /^(Author|From): .* via .*<qemu-\w+\@nongnu\.org>/) {
  1338. ERROR("Author email address is mangled by the mailing list\n" . $herecurr);
  1339. }
  1340. #check the patch for a signoff:
  1341. if ($line =~ /^\s*signed-off-by:/i) {
  1342. # This is a signoff, if ugly, so do not double report.
  1343. $signoff++;
  1344. $in_commit_log = 0;
  1345. if (!($line =~ /^\s*Signed-off-by:/)) {
  1346. ERROR("The correct form is \"Signed-off-by\"\n" .
  1347. $herecurr);
  1348. }
  1349. if ($line =~ /^\s*signed-off-by:\S/i) {
  1350. ERROR("space required after Signed-off-by:\n" .
  1351. $herecurr);
  1352. }
  1353. }
  1354. # Check if MAINTAINERS is being updated. If so, there's probably no need to
  1355. # emit the "does MAINTAINERS need updating?" message on file add/move/delete
  1356. if ($line =~ /^\s*MAINTAINERS\s*\|/) {
  1357. $reported_maintainer_file = 1;
  1358. }
  1359. # Check for added, moved or deleted files
  1360. if (!$reported_maintainer_file && !$in_commit_log &&
  1361. ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
  1362. $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
  1363. ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
  1364. (defined($1) || defined($2)))) &&
  1365. !(($realfile ne '') &&
  1366. defined($acpi_testexpected) &&
  1367. ($realfile eq $acpi_testexpected))) {
  1368. $reported_maintainer_file = 1;
  1369. WARN("added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
  1370. }
  1371. # Check for wrappage within a valid hunk of the file
  1372. if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
  1373. ERROR("patch seems to be corrupt (line wrapped?)\n" .
  1374. $herecurr) if (!$emitted_corrupt++);
  1375. }
  1376. # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
  1377. if (($realfile =~ /^$/ || $line =~ /^\+/) &&
  1378. $rawline !~ m/^$UTF8*$/) {
  1379. my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
  1380. my $blank = copy_spacing($rawline);
  1381. my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
  1382. my $hereptr = "$hereline$ptr\n";
  1383. ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
  1384. }
  1385. if ($rawline =~ m/$UTF8_MOJIBAKE/) {
  1386. ERROR("Doubly-encoded UTF-8\n" . $herecurr);
  1387. }
  1388. # Check if it's the start of a commit log
  1389. # (not a header line and we haven't seen the patch filename)
  1390. if ($in_header_lines && $realfile =~ /^$/ &&
  1391. !($rawline =~ /^\s+\S/ ||
  1392. $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
  1393. $in_header_lines = 0;
  1394. $in_commit_log = 1;
  1395. }
  1396. # Check if there is UTF-8 in a commit log when a mail header has explicitly
  1397. # declined it, i.e defined some charset where it is missing.
  1398. if ($in_header_lines &&
  1399. $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
  1400. $1 !~ /utf-8/i) {
  1401. $non_utf8_charset = 1;
  1402. }
  1403. if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
  1404. $rawline =~ /$NON_ASCII_UTF8/) {
  1405. WARN("8-bit UTF-8 used in possible commit log\n" . $herecurr);
  1406. }
  1407. # Check for various typo / spelling mistakes
  1408. if (defined($misspellings) &&
  1409. ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
  1410. while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
  1411. my $typo = $1;
  1412. my $blank = copy_spacing($rawline);
  1413. my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
  1414. my $hereptr = "$hereline$ptr\n";
  1415. my $typo_fix = $spelling_fix{lc($typo)};
  1416. $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
  1417. $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
  1418. WARN("'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr);
  1419. }
  1420. }
  1421. # ignore non-hunk lines and lines being removed
  1422. next if (!$hunk_line || $line =~ /^-/);
  1423. # Check that updating imported files from Linux are not mixed with other changes
  1424. if ($realfile =~ /^(linux-headers|include\/standard-headers)\//) {
  1425. if (!$in_imported_file) {
  1426. WARN("added, moved or deleted file(s) " .
  1427. "imported from Linux, are you using " .
  1428. "scripts/update-linux-headers.sh?\n" .
  1429. $herecurr);
  1430. }
  1431. $in_imported_file = 1;
  1432. } else {
  1433. $in_no_imported_file = 1;
  1434. }
  1435. if (!$reported_mixing_imported_file &&
  1436. $in_imported_file && $in_no_imported_file) {
  1437. ERROR("headers imported from Linux should be self-" .
  1438. "contained in a patch with no other changes\n" .
  1439. $herecurr);
  1440. $reported_mixing_imported_file = 1;
  1441. }
  1442. # ignore files that are being periodically imported from Linux
  1443. next if ($realfile =~ /^(linux-headers|include\/standard-headers)\//);
  1444. #trailing whitespace
  1445. if ($line =~ /^\+.*\015/) {
  1446. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1447. ERROR("DOS line endings\n" . $herevet);
  1448. } elsif ($realfile =~ /^docs\/.+\.txt/ ||
  1449. $realfile =~ /^docs\/.+\.md/) {
  1450. if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) {
  1451. # TODO: properly check we're in a code block
  1452. # (surrounding text is 4-column aligned)
  1453. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1454. ERROR("code blocks in documentation should have " .
  1455. "empty lines with exactly 4 columns of " .
  1456. "whitespace\n" . $herevet);
  1457. }
  1458. } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
  1459. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1460. ERROR("trailing whitespace\n" . $herevet);
  1461. $rpt_cleaners = 1;
  1462. }
  1463. # checks for trace-events files
  1464. if ($realfile =~ /trace-events$/ && $line =~ /^\+/) {
  1465. if ($rawline =~ /%[-+ 0]*#/) {
  1466. ERROR("Don't use '#' flag of printf format ('%#') in " .
  1467. "trace-events, use '0x' prefix instead\n" . $herecurr);
  1468. } else {
  1469. my $hex =
  1470. qr/%[-+ *.0-9]*([hljztL]|ll|hh)?(x|X|"\s*PRI[xX][^"]*"?)/;
  1471. # don't consider groups split by [.:/ ], like 2A.20:12ab
  1472. my $tmpline = $rawline;
  1473. $tmpline =~ s/($hex[.:\/ ])+$hex//g;
  1474. if ($tmpline =~ /(?<!0x)$hex/) {
  1475. ERROR("Hex numbers must be prefixed with '0x'\n" .
  1476. $herecurr);
  1477. }
  1478. }
  1479. }
  1480. # check we are in a valid source file if not then ignore this hunk
  1481. next if ($realfile !~ /$SrcFile/);
  1482. #90 column limit; exempt URLs, if no other words on line
  1483. if ($line =~ /^\+/ &&
  1484. !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
  1485. !($rawline =~ /^[^[:alnum:]]*https?:\S*$/) &&
  1486. $length > 80)
  1487. {
  1488. if ($length > 90) {
  1489. ERROR("line over 90 characters\n" . $herecurr);
  1490. } else {
  1491. WARN("line over 80 characters\n" . $herecurr);
  1492. }
  1493. }
  1494. # check for spaces before a quoted newline
  1495. if ($rawline =~ /^.*\".*\s\\n/) {
  1496. ERROR("unnecessary whitespace before a quoted newline\n" . $herecurr);
  1497. }
  1498. # check for adding lines without a newline.
  1499. if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
  1500. ERROR("adding a line without newline at end of file\n" . $herecurr);
  1501. }
  1502. # check for RCS/CVS revision markers
  1503. if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) {
  1504. ERROR("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
  1505. }
  1506. # tabs are only allowed in assembly source code, and in
  1507. # some scripts we imported from other projects.
  1508. next if ($realfile =~ /\.(s|S)$/);
  1509. next if ($realfile =~ /(checkpatch|get_maintainer)\.pl$/);
  1510. next if ($realfile =~ /^target\/hexagon\/imported\/*/);
  1511. if ($rawline =~ /^\+.*\t/) {
  1512. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1513. ERROR("code indent should never use tabs\n" . $herevet);
  1514. $rpt_cleaners = 1;
  1515. }
  1516. # check we are in a valid C source file if not then ignore this hunk
  1517. next if ($realfile !~ /\.((h|c)(\.inc)?|cpp)$/);
  1518. # Block comment styles
  1519. # Block comments use /* on a line of its own
  1520. my $commentline = $rawline;
  1521. while ($commentline =~ s@^(\+.*)/\*.*\*/@$1@o) { # remove inline /*...*/
  1522. }
  1523. if ($commentline =~ m@^\+.*/\*\*?+[ \t]*[^ \t]@) { # /* or /** non-blank
  1524. WARN("Block comments use a leading /* on a separate line\n" . $herecurr);
  1525. }
  1526. # Block comments use * on subsequent lines
  1527. if ($prevline =~ /$;[ \t]*$/ && #ends in comment
  1528. $prevrawline =~ /^\+.*?\/\*/ && #starting /*
  1529. $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
  1530. $rawline =~ /^\+/ && #line is new
  1531. $rawline !~ /^\+[ \t]*\*/) { #no leading *
  1532. WARN("Block comments use * on subsequent lines\n" . $hereprev);
  1533. }
  1534. # Block comments use */ on trailing lines
  1535. if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
  1536. $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
  1537. $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
  1538. $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
  1539. WARN("Block comments use a trailing */ on a separate line\n" . $herecurr);
  1540. }
  1541. # Block comment * alignment
  1542. if ($prevline =~ /$;[ \t]*$/ && #ends in comment
  1543. $line =~ /^\+[ \t]*$;/ && #leading comment
  1544. $rawline =~ /^\+[ \t]*\*/ && #leading *
  1545. (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
  1546. $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
  1547. $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
  1548. my $oldindent;
  1549. $prevrawline =~ m@^\+([ \t]*/?)\*@;
  1550. if (defined($1)) {
  1551. $oldindent = expand_tabs($1);
  1552. } else {
  1553. $prevrawline =~ m@^\+(.*/?)\*@;
  1554. $oldindent = expand_tabs($1);
  1555. }
  1556. $rawline =~ m@^\+([ \t]*)\*@;
  1557. my $newindent = $1;
  1558. $newindent = expand_tabs($newindent);
  1559. if (length($oldindent) ne length($newindent)) {
  1560. WARN("Block comments should align the * on each line\n" . $hereprev);
  1561. }
  1562. }
  1563. # Check for potential 'bare' types
  1564. my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
  1565. $realline_next);
  1566. if ($realcnt && $line =~ /.\s*\S/) {
  1567. ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
  1568. ctx_statement_block($linenr, $realcnt, 0);
  1569. $stat =~ s/\n./\n /g;
  1570. $cond =~ s/\n./\n /g;
  1571. # Find the real next line.
  1572. $realline_next = $line_nr_next;
  1573. if (defined $realline_next &&
  1574. (!defined $lines[$realline_next - 1] ||
  1575. substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
  1576. $realline_next++;
  1577. }
  1578. my $s = $stat;
  1579. $s =~ s/{.*$//s;
  1580. # Ignore goto labels.
  1581. if ($s =~ /$Ident:\*$/s) {
  1582. # Ignore functions being called
  1583. } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
  1584. } elsif ($s =~ /^.\s*else\b/s) {
  1585. # declarations always start with types
  1586. } 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) {
  1587. my $type = $1;
  1588. $type =~ s/\s+/ /g;
  1589. possible($type, "A:" . $s);
  1590. # definitions in global scope can only start with types
  1591. } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
  1592. possible($1, "B:" . $s);
  1593. }
  1594. # any (foo ... *) is a pointer cast, and foo is a type
  1595. while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
  1596. possible($1, "C:" . $s);
  1597. }
  1598. # Check for any sort of function declaration.
  1599. # int foo(something bar, other baz);
  1600. # void (*store_gdt)(x86_descr_ptr *);
  1601. if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
  1602. my ($name_len) = length($1);
  1603. my $ctx = $s;
  1604. substr($ctx, 0, $name_len + 1, '');
  1605. $ctx =~ s/\)[^\)]*$//;
  1606. for my $arg (split(/\s*,\s*/, $ctx)) {
  1607. if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
  1608. possible($1, "D:" . $s);
  1609. }
  1610. }
  1611. }
  1612. }
  1613. #
  1614. # Checks which may be anchored in the context.
  1615. #
  1616. # Check for switch () and associated case and default
  1617. # statements should be at the same indent.
  1618. if ($line=~/\bswitch\s*\(.*\)/) {
  1619. my $err = '';
  1620. my $sep = '';
  1621. my @ctx = ctx_block_outer($linenr, $realcnt);
  1622. shift(@ctx);
  1623. for my $ctx (@ctx) {
  1624. my ($clen, $cindent) = line_stats($ctx);
  1625. if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  1626. $indent != $cindent) {
  1627. $err .= "$sep$ctx\n";
  1628. $sep = '';
  1629. } else {
  1630. $sep = "[...]\n";
  1631. }
  1632. }
  1633. if ($err ne '') {
  1634. ERROR("switch and case should be at the same indent\n$hereline$err");
  1635. }
  1636. }
  1637. # if/while/etc brace do not go on next line, unless defining a do while loop,
  1638. # or if that brace on the next line is for something else
  1639. if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
  1640. my $pre_ctx = "$1$2";
  1641. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
  1642. my $ctx_cnt = $realcnt - $#ctx - 1;
  1643. my $ctx = join("\n", @ctx);
  1644. my $ctx_ln = $linenr;
  1645. my $ctx_skip = $realcnt;
  1646. while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
  1647. defined $lines[$ctx_ln - 1] &&
  1648. $lines[$ctx_ln - 1] =~ /^-/)) {
  1649. ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
  1650. $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
  1651. $ctx_ln++;
  1652. }
  1653. #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
  1654. #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
  1655. # The length of the "previous line" is checked against 80 because it
  1656. # includes the + at the beginning of the line (if the actual line has
  1657. # 79 or 80 characters, it is no longer possible to add a space and an
  1658. # opening brace there)
  1659. if ($#ctx == 0 && $ctx !~ /{\s*/ &&
  1660. defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
  1661. defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
  1662. ERROR("that open brace { should be on the previous line\n" .
  1663. "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
  1664. }
  1665. if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
  1666. $ctx =~ /\)\s*\;\s*$/ &&
  1667. defined $lines[$ctx_ln - 1])
  1668. {
  1669. my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
  1670. if ($nindent > $indent) {
  1671. ERROR("trailing semicolon indicates no statements, indent implies otherwise\n" .
  1672. "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
  1673. }
  1674. }
  1675. }
  1676. # 'do ... while (0/false)' only makes sense in macros, without trailing ';'
  1677. if ($line =~ /while\s*\((0|false)\);/) {
  1678. ERROR("suspicious ; after while (0)\n" . $herecurr);
  1679. }
  1680. # Check superfluous trailing ';'
  1681. if ($line =~ /;;$/) {
  1682. ERROR("superfluous trailing semicolon\n" . $herecurr);
  1683. }
  1684. # Check relative indent for conditionals and blocks.
  1685. if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
  1686. my ($s, $c) = ($stat, $cond);
  1687. substr($s, 0, length($c), '');
  1688. # Make sure we remove the line prefixes as we have
  1689. # none on the first line, and are going to re-add them
  1690. # where necessary.
  1691. $s =~ s/\n./\n/gs;
  1692. # Find out how long the conditional actually is.
  1693. my @newlines = ($c =~ /\n/gs);
  1694. my $cond_lines = 1 + $#newlines;
  1695. # We want to check the first line inside the block
  1696. # starting at the end of the conditional, so remove:
  1697. # 1) any blank line termination
  1698. # 2) any opening brace { on end of the line
  1699. # 3) any do (...) {
  1700. my $continuation = 0;
  1701. my $check = 0;
  1702. $s =~ s/^.*\bdo\b//;
  1703. $s =~ s/^\s*\{//;
  1704. if ($s =~ s/^\s*\\//) {
  1705. $continuation = 1;
  1706. }
  1707. if ($s =~ s/^\s*?\n//) {
  1708. $check = 1;
  1709. $cond_lines++;
  1710. }
  1711. # Also ignore a loop construct at the end of a
  1712. # preprocessor statement.
  1713. if (($prevline =~ /^.\s*#\s*define\s/ ||
  1714. $prevline =~ /\\\s*$/) && $continuation == 0) {
  1715. $check = 0;
  1716. }
  1717. my $cond_ptr = -1;
  1718. $continuation = 0;
  1719. while ($cond_ptr != $cond_lines) {
  1720. $cond_ptr = $cond_lines;
  1721. # If we see an #else/#elif then the code
  1722. # is not linear.
  1723. if ($s =~ /^\s*\#\s*(?:else|elif)/) {
  1724. $check = 0;
  1725. }
  1726. # Ignore:
  1727. # 1) blank lines, they should be at 0,
  1728. # 2) preprocessor lines, and
  1729. # 3) labels.
  1730. if ($continuation ||
  1731. $s =~ /^\s*?\n/ ||
  1732. $s =~ /^\s*#\s*?/ ||
  1733. $s =~ /^\s*$Ident\s*:/) {
  1734. $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
  1735. if ($s =~ s/^.*?\n//) {
  1736. $cond_lines++;
  1737. }
  1738. }
  1739. }
  1740. my (undef, $sindent) = line_stats("+" . $s);
  1741. my $stat_real = raw_line($linenr, $cond_lines);
  1742. # Check if either of these lines are modified, else
  1743. # this is not this patch's fault.
  1744. if (!defined($stat_real) ||
  1745. $stat !~ /^\+/ && $stat_real !~ /^\+/) {
  1746. $check = 0;
  1747. }
  1748. if (defined($stat_real) && $cond_lines > 1) {
  1749. $stat_real = "[...]\n$stat_real";
  1750. }
  1751. #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";
  1752. if ($check && (($sindent % 4) != 0 ||
  1753. ($sindent <= $indent && $s ne ''))) {
  1754. ERROR("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
  1755. }
  1756. }
  1757. # Track the 'values' across context and added lines.
  1758. my $opline = $line; $opline =~ s/^./ /;
  1759. my ($curr_values, $curr_vars) =
  1760. annotate_values($opline . "\n", $prev_values);
  1761. $curr_values = $prev_values . $curr_values;
  1762. if ($dbg_values) {
  1763. my $outline = $opline; $outline =~ s/\t/ /g;
  1764. print "$linenr > .$outline\n";
  1765. print "$linenr > $curr_values\n";
  1766. print "$linenr > $curr_vars\n";
  1767. }
  1768. $prev_values = substr($curr_values, -1);
  1769. #ignore lines not being added
  1770. if ($line=~/^[^\+]/) {next;}
  1771. # TEST: allow direct testing of the type matcher.
  1772. if ($dbg_type) {
  1773. if ($line =~ /^.\s*$Declare\s*$/) {
  1774. ERROR("TEST: is type\n" . $herecurr);
  1775. } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
  1776. ERROR("TEST: is not type ($1 is)\n". $herecurr);
  1777. }
  1778. next;
  1779. }
  1780. # TEST: allow direct testing of the attribute matcher.
  1781. if ($dbg_attr) {
  1782. if ($line =~ /^.\s*$Modifier\s*$/) {
  1783. ERROR("TEST: is attr\n" . $herecurr);
  1784. } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
  1785. ERROR("TEST: is not attr ($1 is)\n". $herecurr);
  1786. }
  1787. next;
  1788. }
  1789. # check for initialisation to aggregates open brace on the next line
  1790. if ($line =~ /^.\s*\{/ &&
  1791. $prevline =~ /(?:^|[^=])=\s*$/) {
  1792. ERROR("that open brace { should be on the previous line\n" . $hereprev);
  1793. }
  1794. #
  1795. # Checks which are anchored on the added line.
  1796. #
  1797. # check for malformed paths in #include statements (uses RAW line)
  1798. if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
  1799. my $path = $1;
  1800. if ($path =~ m{//}) {
  1801. ERROR("malformed #include filename\n" .
  1802. $herecurr);
  1803. }
  1804. }
  1805. # no C99 // comments
  1806. if ($line =~ m{//} &&
  1807. $rawline !~ m{// SPDX-License-Identifier: }) {
  1808. ERROR("do not use C99 // comments\n" . $herecurr);
  1809. }
  1810. # Remove C99 comments.
  1811. $line =~ s@//.*@@;
  1812. $opline =~ s@//.*@@;
  1813. # check for global initialisers.
  1814. if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
  1815. ERROR("do not initialise globals to 0 or NULL\n" .
  1816. $herecurr);
  1817. }
  1818. # check for static initialisers.
  1819. if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
  1820. ERROR("do not initialise statics to 0 or NULL\n" .
  1821. $herecurr);
  1822. }
  1823. # * goes on variable not on type
  1824. # (char*[ const])
  1825. if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
  1826. my ($from, $to) = ($1, $1);
  1827. # Should start with a space.
  1828. $to =~ s/^(\S)/ $1/;
  1829. # Should not end with a space.
  1830. $to =~ s/\s+$//;
  1831. # '*'s should not have spaces between.
  1832. while ($to =~ s/\*\s+\*/\*\*/) {
  1833. }
  1834. #print "from<$from> to<$to>\n";
  1835. if ($from ne $to) {
  1836. ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
  1837. }
  1838. } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
  1839. my ($from, $to, $ident) = ($1, $1, $2);
  1840. # Should start with a space.
  1841. $to =~ s/^(\S)/ $1/;
  1842. # Should not end with a space.
  1843. $to =~ s/\s+$//;
  1844. # '*'s should not have spaces between.
  1845. while ($to =~ s/\*\s+\*/\*\*/) {
  1846. }
  1847. # Modifiers should have spaces.
  1848. $to =~ s/(\b$Modifier$)/$1 /;
  1849. #print "from<$from> to<$to> ident<$ident>\n";
  1850. if ($from ne $to && $ident !~ /^$Modifier$/) {
  1851. ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
  1852. }
  1853. }
  1854. # function brace can't be on same line, except for #defines of do while,
  1855. # or if closed on same line
  1856. if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
  1857. !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
  1858. ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
  1859. }
  1860. # open braces for enum, union and struct go on the same line.
  1861. if ($line =~ /^.\s*\{/ &&
  1862. $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
  1863. ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
  1864. }
  1865. # missing space after union, struct or enum definition
  1866. if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
  1867. ERROR("missing space after $1 definition\n" . $herecurr);
  1868. }
  1869. # check for spacing round square brackets; allowed:
  1870. # 1. with a type on the left -- int [] a;
  1871. # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
  1872. # 3. inside a curly brace -- = { [0...10] = 5 }
  1873. # 4. after a comma -- [1] = 5, [2] = 6
  1874. # 5. in a macro definition -- #define abc(x) [x] = y
  1875. while ($line =~ /(.*?\s)\[/g) {
  1876. my ($where, $prefix) = ($-[1], $1);
  1877. if ($prefix !~ /$Type\s+$/ &&
  1878. ($where != 0 || $prefix !~ /^.\s+$/) &&
  1879. $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
  1880. $prefix !~ /[,{:]\s+$/) {
  1881. ERROR("space prohibited before open square bracket '['\n" . $herecurr);
  1882. }
  1883. }
  1884. # check for spaces between functions and their parentheses.
  1885. while ($line =~ /($Ident)\s+\(/g) {
  1886. my $name = $1;
  1887. my $ctx_before = substr($line, 0, $-[1]);
  1888. my $ctx = "$ctx_before$name";
  1889. # Ignore those directives where spaces _are_ permitted.
  1890. if ($name =~ /^(?:
  1891. if|for|while|switch|return|case|
  1892. volatile|__volatile__|coroutine_fn|
  1893. __attribute__|format|__extension__|
  1894. asm|__asm__)$/x)
  1895. {
  1896. # Ignore 'catch (...)' in C++
  1897. } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
  1898. # cpp #define statements have non-optional spaces, ie
  1899. # if there is a space between the name and the open
  1900. # parenthesis it is simply not a parameter group.
  1901. } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
  1902. # cpp #elif statement condition may start with a (
  1903. } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
  1904. # If this whole things ends with a type its most
  1905. # likely a typedef for a function.
  1906. } elsif ($ctx =~ /$Type$/) {
  1907. } else {
  1908. ERROR("space prohibited between function name and open parenthesis '('\n" . $herecurr);
  1909. }
  1910. }
  1911. # Check operator spacing.
  1912. if (!($line=~/\#\s*(include|import)/)) {
  1913. my $ops = qr{
  1914. <<=|>>=|<=|>=|==|!=|
  1915. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  1916. =>|->|<<|>>|<|>|=|!|~|
  1917. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
  1918. \?|::|:
  1919. }x;
  1920. my @elements = split(/($ops|;)/, $opline);
  1921. my $off = 0;
  1922. my $blank = copy_spacing($opline);
  1923. for (my $n = 0; $n < $#elements; $n += 2) {
  1924. $off += length($elements[$n]);
  1925. # Pick up the preceding and succeeding characters.
  1926. my $ca = substr($opline, 0, $off);
  1927. my $cc = '';
  1928. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  1929. $cc = substr($opline, $off + length($elements[$n + 1]));
  1930. }
  1931. my $cb = "$ca$;$cc";
  1932. my $a = '';
  1933. $a = 'V' if ($elements[$n] ne '');
  1934. $a = 'W' if ($elements[$n] =~ /\s$/);
  1935. $a = 'C' if ($elements[$n] =~ /$;$/);
  1936. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  1937. $a = 'O' if ($elements[$n] eq '');
  1938. $a = 'E' if ($ca =~ /^\s*$/);
  1939. my $op = $elements[$n + 1];
  1940. my $c = '';
  1941. if (defined $elements[$n + 2]) {
  1942. $c = 'V' if ($elements[$n + 2] ne '');
  1943. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  1944. $c = 'C' if ($elements[$n + 2] =~ /^$;/);
  1945. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  1946. $c = 'O' if ($elements[$n + 2] eq '');
  1947. $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
  1948. } else {
  1949. $c = 'E';
  1950. }
  1951. my $ctx = "${a}x${c}";
  1952. my $at = "(ctx:$ctx)";
  1953. my $ptr = substr($blank, 0, $off) . "^";
  1954. my $hereptr = "$hereline$ptr\n";
  1955. # Pull out the value of this operator.
  1956. my $op_type = substr($curr_values, $off + 1, 1);
  1957. # Get the full operator variant.
  1958. my $opv = $op . substr($curr_vars, $off, 1);
  1959. # Ignore operators passed as parameters.
  1960. if ($op_type ne 'V' &&
  1961. $ca =~ /\s$/ && $cc =~ /^\s*,/) {
  1962. # # Ignore comments
  1963. # } elsif ($op =~ /^$;+$/) {
  1964. # ; should have either the end of line or a space or \ after it
  1965. } elsif ($op eq ';') {
  1966. if ($ctx !~ /.x[WEBC]/ &&
  1967. $cc !~ /^\\/ && $cc !~ /^;/) {
  1968. ERROR("space required after that '$op' $at\n" . $hereptr);
  1969. }
  1970. # // is a comment
  1971. } elsif ($op eq '//') {
  1972. # Ignore : used in class declaration in C++
  1973. } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
  1974. $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
  1975. # No spaces for:
  1976. # ->
  1977. # : when part of a bitfield
  1978. } elsif ($op eq '->' || $opv eq ':B') {
  1979. if ($ctx =~ /Wx.|.xW/) {
  1980. ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
  1981. }
  1982. # , must have a space on the right.
  1983. # not required when having a single },{ on one line
  1984. } elsif ($op eq ',') {
  1985. if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
  1986. ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
  1987. ERROR("space required after that '$op' $at\n" . $hereptr);
  1988. }
  1989. # '*' as part of a type definition -- reported already.
  1990. } elsif ($opv eq '*_') {
  1991. #warn "'*' is part of type\n";
  1992. # unary operators should have a space before and
  1993. # none after. May be left adjacent to another
  1994. # unary operator, or a cast
  1995. } elsif ($op eq '!' || $op eq '~' ||
  1996. $opv eq '*U' || $opv eq '-U' ||
  1997. $opv eq '&U' || $opv eq '&&U') {
  1998. if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
  1999. # '~' used as a name of Destructor
  2000. } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  2001. ERROR("space required before that '$op' $at\n" . $hereptr);
  2002. }
  2003. if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
  2004. # A unary '*' may be const
  2005. } elsif ($ctx =~ /.xW/) {
  2006. ERROR("space prohibited after that '$op' $at\n" . $hereptr);
  2007. }
  2008. # unary ++ and unary -- are allowed no space on one side.
  2009. } elsif ($op eq '++' or $op eq '--') {
  2010. if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
  2011. ERROR("space required one side of that '$op' $at\n" . $hereptr);
  2012. }
  2013. if ($ctx =~ /Wx[BE]/ ||
  2014. ($ctx =~ /Wx./ && $cc =~ /^;/)) {
  2015. ERROR("space prohibited before that '$op' $at\n" . $hereptr);
  2016. }
  2017. if ($ctx =~ /ExW/) {
  2018. ERROR("space prohibited after that '$op' $at\n" . $hereptr);
  2019. }
  2020. # A colon needs no spaces before when it is
  2021. # terminating a case value or a label.
  2022. } elsif ($opv eq ':C' || $opv eq ':L') {
  2023. if ($ctx =~ /Wx./) {
  2024. ERROR("space prohibited before that '$op' $at\n" . $hereptr);
  2025. }
  2026. # All the others need spaces both sides.
  2027. } elsif ($ctx !~ /[EWC]x[CWE]/) {
  2028. my $ok = 0;
  2029. if ($realfile =~ /\.cpp|\.h$/) {
  2030. # Ignore template arguments <...> in C++
  2031. if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
  2032. $ok = 1;
  2033. }
  2034. # Ignore :: in C++
  2035. if ($op eq '::') {
  2036. $ok = 1;
  2037. }
  2038. }
  2039. # Ignore email addresses <foo@bar>
  2040. if (($op eq '<' &&
  2041. $cc =~ /^\S+\@\S+>/) ||
  2042. ($op eq '>' &&
  2043. $ca =~ /<\S+\@\S+$/))
  2044. {
  2045. $ok = 1;
  2046. }
  2047. # Ignore ?:
  2048. if (($opv eq ':O' && $ca =~ /\?$/) ||
  2049. ($op eq '?' && $cc =~ /^:/)) {
  2050. $ok = 1;
  2051. }
  2052. if ($ok == 0) {
  2053. ERROR("spaces required around that '$op' $at\n" . $hereptr);
  2054. }
  2055. }
  2056. $off += length($elements[$n + 1]);
  2057. }
  2058. }
  2059. #need space before brace following if, while, etc
  2060. if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
  2061. $line =~ /do\{/) {
  2062. ERROR("space required before the open brace '{'\n" . $herecurr);
  2063. }
  2064. # closing brace should have a space following it when it has anything
  2065. # on the line
  2066. if ($line =~ /}(?!(?:,|;|\)))\S/) {
  2067. ERROR("space required after that close brace '}'\n" . $herecurr);
  2068. }
  2069. # check spacing on square brackets
  2070. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  2071. ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
  2072. }
  2073. if ($line =~ /\s\]/) {
  2074. ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
  2075. }
  2076. # check spacing on parentheses
  2077. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  2078. $line !~ /for\s*\(\s+;/) {
  2079. ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
  2080. }
  2081. if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
  2082. $line !~ /for\s*\(.*;\s+\)/ &&
  2083. $line !~ /:\s+\)/) {
  2084. ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
  2085. }
  2086. # Return is not a function.
  2087. if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
  2088. my $spacing = $1;
  2089. my $value = $2;
  2090. # Flatten any parentheses
  2091. $value =~ s/\(/ \(/g;
  2092. $value =~ s/\)/\) /g;
  2093. while ($value =~ s/\[[^\{\}]*\]/1/ ||
  2094. $value !~ /(?:$Ident|-?$Constant)\s*
  2095. $Compare\s*
  2096. (?:$Ident|-?$Constant)/x &&
  2097. $value =~ s/\([^\(\)]*\)/1/) {
  2098. }
  2099. #print "value<$value>\n";
  2100. if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/ &&
  2101. $line =~ /;$/) {
  2102. ERROR("return is not a function, parentheses are not required\n" . $herecurr);
  2103. } elsif ($spacing !~ /\s+/) {
  2104. ERROR("space required before the open parenthesis '('\n" . $herecurr);
  2105. }
  2106. }
  2107. # Return of what appears to be an errno should normally be -'ve
  2108. if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
  2109. my $name = $1;
  2110. if ($name ne 'EOF' && $name ne 'ERROR') {
  2111. ERROR("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
  2112. }
  2113. }
  2114. if ($line =~ /^.\s*(Q(?:S?LIST|SIMPLEQ|TAILQ)_HEAD)\s*\(\s*[^,]/ &&
  2115. $line !~ /^.typedef/) {
  2116. ERROR("named $1 should be typedefed separately\n" . $herecurr);
  2117. }
  2118. # Need a space before open parenthesis after if, while etc
  2119. if ($line=~/\b(if|while|for|switch)\(/) {
  2120. ERROR("space required before the open parenthesis '('\n" . $herecurr);
  2121. }
  2122. # Check for illegal assignment in if conditional -- and check for trailing
  2123. # statements after the conditional.
  2124. if ($line =~ /do\s*(?!{)/) {
  2125. my ($stat_next) = ctx_statement_block($line_nr_next,
  2126. $remain_next, $off_next);
  2127. $stat_next =~ s/\n./\n /g;
  2128. ##print "stat<$stat> stat_next<$stat_next>\n";
  2129. if ($stat_next =~ /^\s*while\b/) {
  2130. # If the statement carries leading newlines,
  2131. # then count those as offsets.
  2132. my ($whitespace) =
  2133. ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
  2134. my $offset =
  2135. statement_rawlines($whitespace) - 1;
  2136. $suppress_whiletrailers{$line_nr_next +
  2137. $offset} = 1;
  2138. }
  2139. }
  2140. if (!defined $suppress_whiletrailers{$linenr} &&
  2141. $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
  2142. my ($s, $c) = ($stat, $cond);
  2143. if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
  2144. ERROR("do not use assignment in if condition\n" . $herecurr);
  2145. }
  2146. # Find out what is on the end of the line after the
  2147. # conditional.
  2148. substr($s, 0, length($c), '');
  2149. $s =~ s/\n.*//g;
  2150. $s =~ s/$;//g; # Remove any comments
  2151. if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
  2152. $c !~ /}\s*while\s*/)
  2153. {
  2154. # Find out how long the conditional actually is.
  2155. my @newlines = ($c =~ /\n/gs);
  2156. my $cond_lines = 1 + $#newlines;
  2157. my $stat_real = '';
  2158. $stat_real = raw_line($linenr, $cond_lines)
  2159. . "\n" if ($cond_lines);
  2160. if (defined($stat_real) && $cond_lines > 1) {
  2161. $stat_real = "[...]\n$stat_real";
  2162. }
  2163. ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
  2164. }
  2165. }
  2166. # Check for bitwise tests written as boolean
  2167. if ($line =~ /
  2168. (?:
  2169. (?:\[|\(|\&\&|\|\|)
  2170. \s*0[xX][0-9]+\s*
  2171. (?:\&\&|\|\|)
  2172. |
  2173. (?:\&\&|\|\|)
  2174. \s*0[xX][0-9]+\s*
  2175. (?:\&\&|\|\||\)|\])
  2176. )/x)
  2177. {
  2178. ERROR("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
  2179. }
  2180. # if and else should not have general statements after it
  2181. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
  2182. my $s = $1;
  2183. $s =~ s/$;//g; # Remove any comments
  2184. if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
  2185. ERROR("trailing statements should be on next line\n" . $herecurr);
  2186. }
  2187. }
  2188. # if should not continue a brace
  2189. if ($line =~ /}\s*if\b/) {
  2190. ERROR("trailing statements should be on next line\n" .
  2191. $herecurr);
  2192. }
  2193. # case and default should not have general statements after them
  2194. if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
  2195. $line !~ /\G(?:
  2196. (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
  2197. \s*return\s+
  2198. )/xg)
  2199. {
  2200. ERROR("trailing statements should be on next line\n" . $herecurr);
  2201. }
  2202. # Check for }<nl>else {, these must be at the same
  2203. # indent level to be relevant to each other.
  2204. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  2205. $previndent == $indent) {
  2206. ERROR("else should follow close brace '}'\n" . $hereprev);
  2207. }
  2208. if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
  2209. $previndent == $indent) {
  2210. my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
  2211. # Find out what is on the end of the line after the
  2212. # conditional.
  2213. substr($s, 0, length($c), '');
  2214. $s =~ s/\n.*//g;
  2215. if ($s =~ /^\s*;/) {
  2216. ERROR("while should follow close brace '}'\n" . $hereprev);
  2217. }
  2218. }
  2219. #studly caps, commented out until figure out how to distinguish between use of existing and adding new
  2220. # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
  2221. # print "No studly caps, use _\n";
  2222. # print "$herecurr";
  2223. # $clean = 0;
  2224. # }
  2225. #no spaces allowed after \ in define
  2226. if ($line=~/\#\s*define.*\\\s$/) {
  2227. ERROR("Whitespace after \\ makes next lines useless\n" . $herecurr);
  2228. }
  2229. # multi-statement macros should be enclosed in a do while loop, grab the
  2230. # first statement and ensure its the whole macro if its not enclosed
  2231. # in a known good container
  2232. if ($realfile !~ m@/vmlinux.lds.h$@ &&
  2233. $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
  2234. my $ln = $linenr;
  2235. my $cnt = $realcnt;
  2236. my ($off, $dstat, $dcond, $rest);
  2237. my $ctx = '';
  2238. my $args = defined($1);
  2239. # Find the end of the macro and limit our statement
  2240. # search to that.
  2241. while ($cnt > 0 && defined $lines[$ln - 1] &&
  2242. $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
  2243. {
  2244. $ctx .= $rawlines[$ln - 1] . "\n";
  2245. $cnt-- if ($lines[$ln - 1] !~ /^-/);
  2246. $ln++;
  2247. }
  2248. $ctx .= $rawlines[$ln - 1];
  2249. ($dstat, $dcond, $ln, $cnt, $off) =
  2250. ctx_statement_block($linenr, $ln - $linenr + 1, 0);
  2251. #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
  2252. #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
  2253. # Extract the remainder of the define (if any) and
  2254. # rip off surrounding spaces, and trailing \'s.
  2255. $rest = '';
  2256. while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
  2257. #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
  2258. if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
  2259. $rest .= substr($lines[$ln - 1], $off) . "\n";
  2260. $cnt--;
  2261. }
  2262. $ln++;
  2263. $off = 0;
  2264. }
  2265. $rest =~ s/\\\n.//g;
  2266. $rest =~ s/^\s*//s;
  2267. $rest =~ s/\s*$//s;
  2268. # Clean up the original statement.
  2269. if ($args) {
  2270. substr($dstat, 0, length($dcond), '');
  2271. } else {
  2272. $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
  2273. }
  2274. $dstat =~ s/$;//g;
  2275. $dstat =~ s/\\\n.//g;
  2276. $dstat =~ s/^\s*//s;
  2277. $dstat =~ s/\s*$//s;
  2278. # Flatten any parentheses and braces
  2279. while ($dstat =~ s/\([^\(\)]*\)/1/ ||
  2280. $dstat =~ s/\{[^\{\}]*\}/1/ ||
  2281. $dstat =~ s/\[[^\{\}]*\]/1/)
  2282. {
  2283. }
  2284. my $exceptions = qr{
  2285. $Declare|
  2286. module_param_named|
  2287. MODULE_PARAM_DESC|
  2288. DECLARE_PER_CPU|
  2289. DEFINE_PER_CPU|
  2290. __typeof__\(|
  2291. union|
  2292. struct|
  2293. \.$Ident\s*=\s*|
  2294. ^\"|\"$
  2295. }x;
  2296. #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
  2297. if ($rest ne '' && $rest ne ',') {
  2298. if ($rest !~ /while\s*\(/ &&
  2299. $dstat !~ /$exceptions/)
  2300. {
  2301. ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
  2302. }
  2303. } elsif ($ctx !~ /;/) {
  2304. if ($dstat ne '' &&
  2305. $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
  2306. $dstat !~ /$exceptions/ &&
  2307. $dstat !~ /^\.$Ident\s*=/ &&
  2308. $dstat =~ /$Operators/)
  2309. {
  2310. ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
  2311. }
  2312. }
  2313. }
  2314. # check for missing bracing around if etc
  2315. if ($line =~ /(^.*)\b(?:if|while|for)\b/ &&
  2316. $line !~ /\#\s*if/) {
  2317. my $allowed = 0;
  2318. # Check the pre-context.
  2319. if ($line =~ /(\}.*?)$/) {
  2320. my $pre = $1;
  2321. if ($line !~ /else/) {
  2322. print "APW: ALLOWED: pre<$pre> line<$line>\n"
  2323. if $dbg_adv_apw;
  2324. $allowed = 1;
  2325. }
  2326. }
  2327. my ($level, $endln, @chunks) =
  2328. ctx_statement_full($linenr, $realcnt, 1);
  2329. if ($dbg_adv_apw) {
  2330. print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
  2331. print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
  2332. if $#chunks >= 1;
  2333. }
  2334. if ($#chunks >= 0 && $level == 0) {
  2335. my $seen = 0;
  2336. my $herectx = $here . "\n";
  2337. my $ln = $linenr - 1;
  2338. for my $chunk (@chunks) {
  2339. my ($cond, $block) = @{$chunk};
  2340. # If the condition carries leading newlines, then count those as offsets.
  2341. my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
  2342. my $offset = statement_rawlines($whitespace) - 1;
  2343. #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
  2344. # We have looked at and allowed this specific line.
  2345. $suppress_ifbraces{$ln + $offset} = 1;
  2346. $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
  2347. $ln += statement_rawlines($block) - 1;
  2348. substr($block, 0, length($cond), '');
  2349. my $spaced_block = $block;
  2350. $spaced_block =~ s/\n\+/ /g;
  2351. $seen++ if ($spaced_block =~ /^\s*\{/);
  2352. print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
  2353. if $dbg_adv_apw;
  2354. if (statement_lines($cond) > 1) {
  2355. print "APW: ALLOWED: cond<$cond>\n"
  2356. if $dbg_adv_apw;
  2357. $allowed = 1;
  2358. }
  2359. if ($block =~/\b(?:if|for|while)\b/) {
  2360. print "APW: ALLOWED: block<$block>\n"
  2361. if $dbg_adv_apw;
  2362. $allowed = 1;
  2363. }
  2364. if (statement_block_size($block) > 1) {
  2365. print "APW: ALLOWED: lines block<$block>\n"
  2366. if $dbg_adv_apw;
  2367. $allowed = 1;
  2368. }
  2369. }
  2370. if ($seen != ($#chunks + 1) && !$allowed) {
  2371. ERROR("braces {} are necessary for all arms of this statement\n" . $herectx);
  2372. }
  2373. }
  2374. }
  2375. if (!defined $suppress_ifbraces{$linenr - 1} &&
  2376. $line =~ /\b(if|while|for|else)\b/ &&
  2377. $line !~ /\#\s*if/ &&
  2378. $line !~ /\#\s*else/) {
  2379. my $allowed = 0;
  2380. # Check the pre-context.
  2381. if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
  2382. my $pre = $1;
  2383. if ($line !~ /else/) {
  2384. print "APW: ALLOWED: pre<$pre> line<$line>\n"
  2385. if $dbg_adv_apw;
  2386. $allowed = 1;
  2387. }
  2388. }
  2389. my ($level, $endln, @chunks) =
  2390. ctx_statement_full($linenr, $realcnt, $-[0]);
  2391. # Check the condition.
  2392. my ($cond, $block) = @{$chunks[0]};
  2393. print "CHECKING<$linenr> cond<$cond> block<$block>\n"
  2394. if $dbg_adv_checking;
  2395. if (defined $cond) {
  2396. substr($block, 0, length($cond), '');
  2397. }
  2398. if (statement_lines($cond) > 1) {
  2399. print "APW: ALLOWED: cond<$cond>\n"
  2400. if $dbg_adv_apw;
  2401. $allowed = 1;
  2402. }
  2403. if ($block =~/\b(?:if|for|while)\b/) {
  2404. print "APW: ALLOWED: block<$block>\n"
  2405. if $dbg_adv_apw;
  2406. $allowed = 1;
  2407. }
  2408. if (statement_block_size($block) > 1) {
  2409. print "APW: ALLOWED: lines block<$block>\n"
  2410. if $dbg_adv_apw;
  2411. $allowed = 1;
  2412. }
  2413. # Check the post-context.
  2414. if (defined $chunks[1]) {
  2415. my ($cond, $block) = @{$chunks[1]};
  2416. if (defined $cond) {
  2417. substr($block, 0, length($cond), '');
  2418. }
  2419. if ($block =~ /^\s*\{/) {
  2420. print "APW: ALLOWED: chunk-1 block<$block>\n"
  2421. if $dbg_adv_apw;
  2422. $allowed = 1;
  2423. }
  2424. }
  2425. print "DCS: level=$level block<$block> allowed=$allowed\n"
  2426. if $dbg_adv_dcs;
  2427. if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
  2428. my $herectx = $here . "\n";;
  2429. my $cnt = statement_rawlines($block);
  2430. for (my $n = 0; $n < $cnt; $n++) {
  2431. $herectx .= raw_line($linenr, $n) . "\n";;
  2432. }
  2433. ERROR("braces {} are necessary even for single statement blocks\n" . $herectx);
  2434. }
  2435. }
  2436. # no volatiles please
  2437. my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
  2438. if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/ &&
  2439. $line !~ /sig_atomic_t/ &&
  2440. !ctx_has_comment($first_line, $linenr)) {
  2441. my $msg = "Use of volatile is usually wrong, please add a comment\n" . $herecurr;
  2442. ERROR($msg);
  2443. }
  2444. # warn about #if 0
  2445. if ($line =~ /^.\s*\#\s*if\s+0\b/) {
  2446. ERROR("if this code is redundant consider removing it\n" .
  2447. $herecurr);
  2448. }
  2449. # check for needless g_free() checks
  2450. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  2451. my $expr = $1;
  2452. if ($line =~ /\bg_free\(\Q$expr\E\);/) {
  2453. ERROR("g_free(NULL) is safe this check is probably not required\n" . $hereprev);
  2454. }
  2455. }
  2456. # warn about #ifdefs in C files
  2457. # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  2458. # print "#ifdef in C files should be avoided\n";
  2459. # print "$herecurr";
  2460. # $clean = 0;
  2461. # }
  2462. # warn about spacing in #ifdefs
  2463. if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
  2464. ERROR("exactly one space required after that #$1\n" . $herecurr);
  2465. }
  2466. # check for memory barriers without a comment.
  2467. if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  2468. if (!ctx_has_comment($first_line, $linenr)) {
  2469. ERROR("memory barrier without comment\n" . $herecurr);
  2470. }
  2471. }
  2472. # check of hardware specific defines
  2473. # we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases
  2474. # where they might be necessary.
  2475. if ($line =~ m@^.\s*\#\s*if.*\b__@) {
  2476. WARN("architecture specific defines should be avoided\n" . $herecurr);
  2477. }
  2478. # Check that the storage class is at the beginning of a declaration
  2479. if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
  2480. ERROR("storage class should be at the beginning of the declaration\n" . $herecurr)
  2481. }
  2482. # check the location of the inline attribute, that it is between
  2483. # storage class and type.
  2484. if ($line =~ /\b$Type\s+$Inline\b/ ||
  2485. $line =~ /\b$Inline\s+$Storage\b/) {
  2486. ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
  2487. }
  2488. # check for sizeof(&)
  2489. if ($line =~ /\bsizeof\s*\(\s*\&/) {
  2490. ERROR("sizeof(& should be avoided\n" . $herecurr);
  2491. }
  2492. # check for new externs in .c files.
  2493. if ($realfile =~ /\.c$/ && defined $stat &&
  2494. $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
  2495. {
  2496. my $function_name = $1;
  2497. my $paren_space = $2;
  2498. my $s = $stat;
  2499. if (defined $cond) {
  2500. substr($s, 0, length($cond), '');
  2501. }
  2502. if ($s =~ /^\s*;/ &&
  2503. $function_name ne 'uninitialized_var')
  2504. {
  2505. ERROR("externs should be avoided in .c files\n" . $herecurr);
  2506. }
  2507. if ($paren_space =~ /\n/) {
  2508. ERROR("arguments for function declarations should follow identifier\n" . $herecurr);
  2509. }
  2510. } elsif ($realfile =~ /\.c$/ && defined $stat &&
  2511. $stat =~ /^.\s*extern\s+/)
  2512. {
  2513. ERROR("externs should be avoided in .c files\n" . $herecurr);
  2514. }
  2515. # check for pointless casting of g_malloc return
  2516. if ($line =~ /\*\s*\)\s*g_(try|)(m|re)alloc(0?)(_n)?\b/) {
  2517. if ($2 eq 'm') {
  2518. ERROR("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr);
  2519. } else {
  2520. ERROR("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr);
  2521. }
  2522. }
  2523. # check for gcc specific __FUNCTION__
  2524. if ($line =~ /__FUNCTION__/) {
  2525. ERROR("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
  2526. }
  2527. # recommend g_path_get_* over g_strdup(basename/dirname(...))
  2528. if ($line =~ /\bg_strdup\s*\(\s*(basename|dirname)\s*\(/) {
  2529. WARN("consider using g_path_get_$1() in preference to g_strdup($1())\n" . $herecurr);
  2530. }
  2531. # enforce g_memdup2() over g_memdup()
  2532. if ($line =~ /\bg_memdup\s*\(/) {
  2533. ERROR("use g_memdup2() instead of unsafe g_memdup()\n" . $herecurr);
  2534. }
  2535. # recommend qemu_strto* over strto* for numeric conversions
  2536. if ($line =~ /\b(strto[^kd].*?)\s*\(/) {
  2537. ERROR("consider using qemu_$1 in preference to $1\n" . $herecurr);
  2538. }
  2539. # recommend sigaction over signal for portability, when establishing a handler
  2540. if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) {
  2541. ERROR("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr);
  2542. }
  2543. # recommend qemu_bh_new_guarded instead of qemu_bh_new
  2544. if ($realfile =~ /.*\/hw\/.*/ && $line =~ /\bqemu_bh_new\s*\(/) {
  2545. ERROR("use qemu_bh_new_guarded() instead of qemu_bh_new() to avoid reentrancy problems\n" . $herecurr);
  2546. }
  2547. # recommend aio_bh_new_guarded instead of aio_bh_new
  2548. if ($realfile =~ /.*\/hw\/.*/ && $line =~ /\baio_bh_new\s*\(/) {
  2549. ERROR("use aio_bh_new_guarded() instead of aio_bh_new() to avoid reentrancy problems\n" . $herecurr);
  2550. }
  2551. # check for module_init(), use category-specific init macros explicitly please
  2552. if ($line =~ /^module_init\s*\(/) {
  2553. ERROR("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr);
  2554. }
  2555. # check for various ops structs, ensure they are const.
  2556. my $struct_ops = qr{AIOCBInfo|
  2557. BdrvActionOps|
  2558. BlockDevOps|
  2559. BlockJobDriver|
  2560. DisplayChangeListenerOps|
  2561. GraphicHwOps|
  2562. IDEDMAOps|
  2563. KVMCapabilityInfo|
  2564. MemoryRegionIOMMUOps|
  2565. MemoryRegionOps|
  2566. MemoryRegionPortio|
  2567. QEMUFileOps|
  2568. SCSIBusInfo|
  2569. SCSIReqOps|
  2570. Spice[A-Z][a-zA-Z0-9]*Interface|
  2571. TypeInfo|
  2572. USBDesc[A-Z][a-zA-Z0-9]*|
  2573. VhostOps|
  2574. VMStateDescription|
  2575. VMStateInfo}x;
  2576. if ($line !~ /\bconst\b/ &&
  2577. $line =~ /\b($struct_ops)\b.*=/) {
  2578. ERROR("initializer for struct $1 should normally be const\n" .
  2579. $herecurr);
  2580. }
  2581. # format strings checks
  2582. my $string;
  2583. while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
  2584. $string = substr($rawline, $-[1], $+[1] - $-[1]);
  2585. $string =~ s/%%/__/g;
  2586. # check for %L{u,d,i} in strings
  2587. if ($string =~ /(?<!%)%L[udi]/) {
  2588. ERROR("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
  2589. }
  2590. # check for %# or %0# in printf-style format strings
  2591. if ($string =~ /(?<!%)%0?#/) {
  2592. ERROR("Don't use '#' flag of printf format " .
  2593. "('%#') in format strings, use '0x' " .
  2594. "prefix instead\n" . $herecurr);
  2595. }
  2596. }
  2597. # QEMU specific tests
  2598. if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
  2599. ERROR("use QEMU instead of Qemu or QEmu\n" . $herecurr);
  2600. }
  2601. # Qemu error function tests
  2602. # Find newlines in error messages
  2603. my $qemu_error_funcs = qr{error_setg|
  2604. error_setg_errno|
  2605. error_setg_win32|
  2606. error_setg_file_open|
  2607. error_set|
  2608. error_prepend|
  2609. warn_reportf_err|
  2610. error_reportf_err|
  2611. error_vreport|
  2612. warn_vreport|
  2613. info_vreport|
  2614. error_report|
  2615. warn_report|
  2616. info_report|
  2617. g_test_message}x;
  2618. if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) {
  2619. ERROR("Error messages should not contain newlines\n" . $herecurr);
  2620. }
  2621. # Continue checking for error messages that contains newlines. This
  2622. # check handles cases where string literals are spread over multiple lines.
  2623. # Example:
  2624. # error_report("Error msg line #1"
  2625. # "Error msg line #2\n");
  2626. my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
  2627. my $continued_str_literal = qr{\+\s*\".*\"};
  2628. if ($rawline =~ /$quoted_newline_regex/) {
  2629. # Backtrack to first line that does not contain only a quoted literal
  2630. # and assume that it is the start of the statement.
  2631. my $i = $linenr - 2;
  2632. while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
  2633. $i--;
  2634. }
  2635. if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) {
  2636. ERROR("Error messages should not contain newlines\n" . $herecurr);
  2637. }
  2638. }
  2639. # check for non-portable libc calls that have portable alternatives in QEMU
  2640. if ($line =~ /\bffs\(/) {
  2641. ERROR("use ctz32() instead of ffs()\n" . $herecurr);
  2642. }
  2643. if ($line =~ /\bffsl\(/) {
  2644. ERROR("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr);
  2645. }
  2646. if ($line =~ /\bffsll\(/) {
  2647. ERROR("use ctz64() instead of ffsll()\n" . $herecurr);
  2648. }
  2649. if ($line =~ /\bbzero\(/) {
  2650. ERROR("use memset() instead of bzero()\n" . $herecurr);
  2651. }
  2652. if ($line =~ /\bgetpagesize\(\)/) {
  2653. ERROR("use qemu_real_host_page_size() instead of getpagesize()\n" . $herecurr);
  2654. }
  2655. if ($line =~ /\bsysconf\(_SC_PAGESIZE\)/) {
  2656. ERROR("use qemu_real_host_page_size() instead of sysconf(_SC_PAGESIZE)\n" . $herecurr);
  2657. }
  2658. if ($line =~ /\b(g_)?assert\(0\)/) {
  2659. ERROR("use g_assert_not_reached() instead of assert(0)\n" . $herecurr);
  2660. }
  2661. if ($line =~ /\b(g_)?assert\(false\)/) {
  2662. ERROR("use g_assert_not_reached() instead of assert(false)\n" .
  2663. $herecurr);
  2664. }
  2665. if ($line =~ /\bstrerrorname_np\(/) {
  2666. ERROR("use strerror() instead of strerrorname_np()\n" . $herecurr);
  2667. }
  2668. my $non_exit_glib_asserts = qr{g_assert_cmpstr|
  2669. g_assert_cmpint|
  2670. g_assert_cmpuint|
  2671. g_assert_cmphex|
  2672. g_assert_cmpfloat|
  2673. g_assert_true|
  2674. g_assert_false|
  2675. g_assert_nonnull|
  2676. g_assert_null|
  2677. g_assert_no_error|
  2678. g_assert_error|
  2679. g_test_assert_expected_messages|
  2680. g_test_trap_assert_passed|
  2681. g_test_trap_assert_stdout|
  2682. g_test_trap_assert_stdout_unmatched|
  2683. g_test_trap_assert_stderr|
  2684. g_test_trap_assert_stderr_unmatched}x;
  2685. if ($realfile !~ /^tests\// &&
  2686. $line =~ /\b(?:$non_exit_glib_asserts)\(/) {
  2687. ERROR("Use g_assert or g_assert_not_reached\n". $herecurr);
  2688. }
  2689. }
  2690. if ($is_patch && $chk_signoff && $signoff == 0) {
  2691. ERROR("Missing Signed-off-by: line(s)\n");
  2692. }
  2693. # If we have no input at all, then there is nothing to report on
  2694. # so just keep quiet.
  2695. if ($#rawlines == -1) {
  2696. return 1;
  2697. }
  2698. # In mailback mode only produce a report in the negative, for
  2699. # things that appear to be patches.
  2700. if ($mailback && ($clean == 1 || !$is_patch)) {
  2701. return 1;
  2702. }
  2703. # This is not a patch, and we are are in 'no-patch' mode so
  2704. # just keep quiet.
  2705. if (!$chk_patch && !$is_patch) {
  2706. return 1;
  2707. }
  2708. if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
  2709. ERROR("Does not appear to be a unified-diff format patch\n");
  2710. }
  2711. print report_dump();
  2712. if ($summary && !($clean == 1 && $quiet == 1)) {
  2713. print "$filename " if ($summary_file);
  2714. print "total: $cnt_error errors, $cnt_warn warnings, " .
  2715. "$cnt_lines lines checked\n";
  2716. print "\n" if ($quiet == 0);
  2717. }
  2718. if ($quiet == 0) {
  2719. # If there were whitespace errors which cleanpatch can fix
  2720. # then suggest that.
  2721. # if ($rpt_cleaners) {
  2722. # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
  2723. # print " scripts/cleanfile\n\n";
  2724. # }
  2725. }
  2726. if ($clean == 1 && $quiet == 0) {
  2727. print "$vname has no obvious style problems and is ready for submission.\n"
  2728. }
  2729. if ($clean == 0 && $quiet == 0) {
  2730. print "$vname has style problems, please review. If any of these errors\n";
  2731. print "are false positives report them to the maintainer, see\n";
  2732. print "CHECKPATCH in MAINTAINERS.\n";
  2733. }
  2734. return ($no_warnings ? $clean : $cnt_error == 0);
  2735. }