PRESUBMIT.py 238 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171
  1. # Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """Top-level presubmit script for Chromium.
  5. See https://www.chromium.org/developers/how-tos/depottools/presubmit-scripts/
  6. for more details about the presubmit API built into depot_tools.
  7. """
  8. from typing import Callable
  9. from typing import Optional
  10. from typing import Sequence
  11. from dataclasses import dataclass
  12. PRESUBMIT_VERSION = '2.0.0'
  13. # This line is 'magic' in that git-cl looks for it to decide whether to
  14. # use Python3 instead of Python2 when running the code in this file.
  15. USE_PYTHON3 = True
  16. _EXCLUDED_PATHS = (
  17. # Generated file
  18. (r"chrome[\\/]android[\\/]webapk[\\/]shell_apk[\\/]src[\\/]org[\\/]chromium"
  19. r"[\\/]webapk[\\/]lib[\\/]runtime_library[\\/]IWebApkApi.java"),
  20. # File needs to write to stdout to emulate a tool it's replacing.
  21. r"chrome[\\/]updater[\\/]mac[\\/]keystone[\\/]ksadmin.mm",
  22. # Generated file.
  23. (r"^components[\\/]variations[\\/]proto[\\/]devtools[\\/]"
  24. r"client_variations.js"),
  25. # These are video files, not typescript.
  26. r"^media[\\/]test[\\/]data[\\/].*.ts",
  27. r"^native_client_sdksrc[\\/]build_tools[\\/]make_rules.py",
  28. r"^native_client_sdk[\\/]src[\\/]build_tools[\\/]make_simple.py",
  29. r"^native_client_sdk[\\/]src[\\/]tools[\\/].*.mk",
  30. r"^net[\\/]tools[\\/]spdyshark[\\/].*",
  31. r"^skia[\\/].*",
  32. r"^third_party[\\/]blink[\\/].*",
  33. r"^third_party[\\/]breakpad[\\/].*",
  34. # sqlite is an imported third party dependency.
  35. r"^third_party[\\/]sqlite[\\/].*",
  36. r"^v8[\\/].*",
  37. r".*MakeFile$",
  38. r".+_autogen\.h$",
  39. r".+_pb2\.py$",
  40. r".+[\\/]pnacl_shim\.c$",
  41. r"^gpu[\\/]config[\\/].*_list_json\.cc$",
  42. r"tools[\\/]md_browser[\\/].*\.css$",
  43. # Test pages for Maps telemetry tests.
  44. r"tools[\\/]perf[\\/]page_sets[\\/]maps_perf_test.*",
  45. # Test pages for WebRTC telemetry tests.
  46. r"tools[\\/]perf[\\/]page_sets[\\/]webrtc_cases.*",
  47. )
  48. _EXCLUDED_SET_NO_PARENT_PATHS = (
  49. # It's for historical reasons that blink isn't a top level directory, where
  50. # it would be allowed to have "set noparent" to avoid top level owners
  51. # accidentally +1ing changes.
  52. 'third_party/blink/OWNERS',
  53. )
  54. # Fragment of a regular expression that matches C++ and Objective-C++
  55. # implementation files.
  56. _IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
  57. # Fragment of a regular expression that matches C++ and Objective-C++
  58. # header files.
  59. _HEADER_EXTENSIONS = r'\.(h|hpp|hxx)$'
  60. # Paths with sources that don't use //base.
  61. _NON_BASE_DEPENDENT_PATHS = (
  62. r"^chrome[\\/]browser[\\/]browser_switcher[\\/]bho[\\/]",
  63. r"^tools[\\/]win[\\/]",
  64. )
  65. # Regular expression that matches code only used for test binaries
  66. # (best effort).
  67. _TEST_CODE_EXCLUDED_PATHS = (
  68. r'.*[\\/](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS,
  69. r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS,
  70. # Test suite files, like:
  71. # foo_browsertest.cc
  72. # bar_unittest_mac.cc (suffix)
  73. # baz_unittests.cc (plural)
  74. r'.+_(api|browser|eg|int|perf|pixel|unit|ui)?test(s)?(_[a-z]+)?%s' %
  75. _IMPLEMENTATION_EXTENSIONS,
  76. r'.+_(fuzz|fuzzer)(_[a-z]+)?%s' % _IMPLEMENTATION_EXTENSIONS,
  77. r'.+sync_service_impl_harness%s' % _IMPLEMENTATION_EXTENSIONS,
  78. r'.*[\\/](test|tool(s)?)[\\/].*',
  79. # content_shell is used for running content_browsertests.
  80. r'content[\\/]shell[\\/].*',
  81. # Web test harness.
  82. r'content[\\/]web_test[\\/].*',
  83. # Non-production example code.
  84. r'mojo[\\/]examples[\\/].*',
  85. # Launcher for running iOS tests on the simulator.
  86. r'testing[\\/]iossim[\\/]iossim\.mm$',
  87. # EarlGrey app side code for tests.
  88. r'ios[\\/].*_app_interface\.mm$',
  89. # Views Examples code
  90. r'ui[\\/]views[\\/]examples[\\/].*',
  91. # Chromium Codelab
  92. r'codelabs[\\/]*'
  93. )
  94. _THIRD_PARTY_EXCEPT_BLINK = 'third_party/(?!blink/)'
  95. _TEST_ONLY_WARNING = (
  96. 'You might be calling functions intended only for testing from\n'
  97. 'production code. If you are doing this from inside another method\n'
  98. 'named as *ForTesting(), then consider exposing things to have tests\n'
  99. 'make that same call directly.\n'
  100. 'If that is not possible, you may put a comment on the same line with\n'
  101. ' // IN-TEST \n'
  102. 'to tell the PRESUBMIT script that the code is inside a *ForTesting()\n'
  103. 'method and can be ignored. Do not do this inside production code.\n'
  104. 'The android-binary-size trybot will block if the method exists in the\n'
  105. 'release apk.')
  106. @dataclass
  107. class BanRule:
  108. # String pattern. If the pattern begins with a slash, the pattern will be
  109. # treated as a regular expression instead.
  110. pattern: str
  111. # Explanation as a sequence of strings. Each string in the sequence will be
  112. # printed on its own line.
  113. explanation: Sequence[str]
  114. # Whether or not to treat this ban as a fatal error. If unspecified,
  115. # defaults to true.
  116. treat_as_error: Optional[bool] = None
  117. # Paths that should be excluded from the ban check. Each string is a regular
  118. # expression that will be matched against the path of the file being checked
  119. # relative to the root of the source tree.
  120. excluded_paths: Optional[Sequence[str]] = None
  121. _BANNED_JAVA_IMPORTS : Sequence[BanRule] = (
  122. BanRule(
  123. 'import java.net.URI;',
  124. (
  125. 'Use org.chromium.url.GURL instead of java.net.URI, where possible.',
  126. ),
  127. excluded_paths=(
  128. (r'net/android/javatests/src/org/chromium/net/'
  129. 'AndroidProxySelectorTest\.java'),
  130. r'components/cronet/',
  131. r'third_party/robolectric/local/',
  132. ),
  133. ),
  134. BanRule(
  135. 'import android.annotation.TargetApi;',
  136. (
  137. 'Do not use TargetApi, use @androidx.annotation.RequiresApi instead. '
  138. 'RequiresApi ensures that any calls are guarded by the appropriate '
  139. 'SDK_INT check. See https://crbug.com/1116486.',
  140. ),
  141. ),
  142. BanRule(
  143. 'import android.support.test.rule.UiThreadTestRule;',
  144. (
  145. 'Do not use UiThreadTestRule, just use '
  146. '@org.chromium.base.test.UiThreadTest on test methods that should run '
  147. 'on the UI thread. See https://crbug.com/1111893.',
  148. ),
  149. ),
  150. BanRule(
  151. 'import android.support.test.annotation.UiThreadTest;',
  152. ('Do not use android.support.test.annotation.UiThreadTest, use '
  153. 'org.chromium.base.test.UiThreadTest instead. See '
  154. 'https://crbug.com/1111893.',
  155. ),
  156. ),
  157. BanRule(
  158. 'import android.support.test.rule.ActivityTestRule;',
  159. (
  160. 'Do not use ActivityTestRule, use '
  161. 'org.chromium.base.test.BaseActivityTestRule instead.',
  162. ),
  163. excluded_paths=(
  164. 'components/cronet/',
  165. ),
  166. ),
  167. )
  168. _BANNED_JAVA_FUNCTIONS : Sequence[BanRule] = (
  169. BanRule(
  170. 'StrictMode.allowThreadDiskReads()',
  171. (
  172. 'Prefer using StrictModeContext.allowDiskReads() to using StrictMode '
  173. 'directly.',
  174. ),
  175. False,
  176. ),
  177. BanRule(
  178. 'StrictMode.allowThreadDiskWrites()',
  179. (
  180. 'Prefer using StrictModeContext.allowDiskWrites() to using StrictMode '
  181. 'directly.',
  182. ),
  183. False,
  184. ),
  185. BanRule(
  186. '.waitForIdleSync()',
  187. (
  188. 'Do not use waitForIdleSync as it masks underlying issues. There is '
  189. 'almost always something else you should wait on instead.',
  190. ),
  191. False,
  192. ),
  193. )
  194. _BANNED_OBJC_FUNCTIONS : Sequence[BanRule] = (
  195. BanRule(
  196. 'addTrackingRect:',
  197. (
  198. 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
  199. 'prohibited. Please use CrTrackingArea instead.',
  200. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  201. ),
  202. False,
  203. ),
  204. BanRule(
  205. r'/NSTrackingArea\W',
  206. (
  207. 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
  208. 'instead.',
  209. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  210. ),
  211. False,
  212. ),
  213. BanRule(
  214. 'convertPointFromBase:',
  215. (
  216. 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
  217. 'Please use |convertPoint:(point) fromView:nil| instead.',
  218. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  219. ),
  220. True,
  221. ),
  222. BanRule(
  223. 'convertPointToBase:',
  224. (
  225. 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
  226. 'Please use |convertPoint:(point) toView:nil| instead.',
  227. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  228. ),
  229. True,
  230. ),
  231. BanRule(
  232. 'convertRectFromBase:',
  233. (
  234. 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
  235. 'Please use |convertRect:(point) fromView:nil| instead.',
  236. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  237. ),
  238. True,
  239. ),
  240. BanRule(
  241. 'convertRectToBase:',
  242. (
  243. 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
  244. 'Please use |convertRect:(point) toView:nil| instead.',
  245. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  246. ),
  247. True,
  248. ),
  249. BanRule(
  250. 'convertSizeFromBase:',
  251. (
  252. 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
  253. 'Please use |convertSize:(point) fromView:nil| instead.',
  254. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  255. ),
  256. True,
  257. ),
  258. BanRule(
  259. 'convertSizeToBase:',
  260. (
  261. 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
  262. 'Please use |convertSize:(point) toView:nil| instead.',
  263. 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
  264. ),
  265. True,
  266. ),
  267. BanRule(
  268. r"/\s+UTF8String\s*]",
  269. (
  270. 'The use of -[NSString UTF8String] is dangerous as it can return null',
  271. 'even if |canBeConvertedToEncoding:NSUTF8StringEncoding| returns YES.',
  272. 'Please use |SysNSStringToUTF8| instead.',
  273. ),
  274. True,
  275. ),
  276. BanRule(
  277. r'__unsafe_unretained',
  278. (
  279. 'The use of __unsafe_unretained is almost certainly wrong, unless',
  280. 'when interacting with NSFastEnumeration or NSInvocation.',
  281. 'Please use __weak in files build with ARC, nothing otherwise.',
  282. ),
  283. False,
  284. ),
  285. BanRule(
  286. 'freeWhenDone:NO',
  287. (
  288. 'The use of "freeWhenDone:NO" with the NoCopy creation of ',
  289. 'Foundation types is prohibited.',
  290. ),
  291. True,
  292. ),
  293. )
  294. _BANNED_IOS_OBJC_FUNCTIONS = (
  295. BanRule(
  296. r'/\bTEST[(]',
  297. (
  298. 'TEST() macro should not be used in Objective-C++ code as it does not ',
  299. 'drain the autorelease pool at the end of the test. Use TEST_F() ',
  300. 'macro instead with a fixture inheriting from PlatformTest (or a ',
  301. 'typedef).'
  302. ),
  303. True,
  304. ),
  305. BanRule(
  306. r'/\btesting::Test\b',
  307. (
  308. 'testing::Test should not be used in Objective-C++ code as it does ',
  309. 'not drain the autorelease pool at the end of the test. Use ',
  310. 'PlatformTest instead.'
  311. ),
  312. True,
  313. ),
  314. BanRule(
  315. ' systemImageNamed:',
  316. (
  317. '+[UIImage systemImageNamed:] should not be used to create symbols.',
  318. 'Instead use a wrapper defined in:',
  319. 'ios/chrome/browser/ui/icons/chrome_symbol.h'
  320. ),
  321. True,
  322. excluded_paths=(
  323. 'ios/chrome/browser/ui/icons/chrome_symbol.mm',
  324. ),
  325. ),
  326. )
  327. _BANNED_IOS_EGTEST_FUNCTIONS : Sequence[BanRule] = (
  328. BanRule(
  329. r'/\bEXPECT_OCMOCK_VERIFY\b',
  330. (
  331. 'EXPECT_OCMOCK_VERIFY should not be used in EarlGrey tests because ',
  332. 'it is meant for GTests. Use [mock verify] instead.'
  333. ),
  334. True,
  335. ),
  336. )
  337. _BANNED_CPP_FUNCTIONS : Sequence[BanRule] = (
  338. BanRule(
  339. r'/\busing namespace ',
  340. (
  341. 'Using directives ("using namespace x") are banned by the Google Style',
  342. 'Guide ( http://google.github.io/styleguide/cppguide.html#Namespaces ).',
  343. 'Explicitly qualify symbols or use using declarations ("using x::foo").',
  344. ),
  345. True,
  346. [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
  347. ),
  348. # Make sure that gtest's FRIEND_TEST() macro is not used; the
  349. # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
  350. # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
  351. BanRule(
  352. 'FRIEND_TEST(',
  353. (
  354. 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
  355. 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
  356. ),
  357. False,
  358. (),
  359. ),
  360. BanRule(
  361. 'setMatrixClip',
  362. (
  363. 'Overriding setMatrixClip() is prohibited; ',
  364. 'the base function is deprecated. ',
  365. ),
  366. True,
  367. (),
  368. ),
  369. BanRule(
  370. 'SkRefPtr',
  371. (
  372. 'The use of SkRefPtr is prohibited. ',
  373. 'Please use sk_sp<> instead.'
  374. ),
  375. True,
  376. (),
  377. ),
  378. BanRule(
  379. 'SkAutoRef',
  380. (
  381. 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
  382. 'Please use sk_sp<> instead.'
  383. ),
  384. True,
  385. (),
  386. ),
  387. BanRule(
  388. 'SkAutoTUnref',
  389. (
  390. 'The use of SkAutoTUnref is dangerous because it implicitly ',
  391. 'converts to a raw pointer. Please use sk_sp<> instead.'
  392. ),
  393. True,
  394. (),
  395. ),
  396. BanRule(
  397. 'SkAutoUnref',
  398. (
  399. 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
  400. 'because it implicitly converts to a raw pointer. ',
  401. 'Please use sk_sp<> instead.'
  402. ),
  403. True,
  404. (),
  405. ),
  406. BanRule(
  407. r'/HANDLE_EINTR\(.*close',
  408. (
  409. 'HANDLE_EINTR(close) is invalid. If close fails with EINTR, the file',
  410. 'descriptor will be closed, and it is incorrect to retry the close.',
  411. 'Either call close directly and ignore its return value, or wrap close',
  412. 'in IGNORE_EINTR to use its return value. See http://crbug.com/269623'
  413. ),
  414. True,
  415. (),
  416. ),
  417. BanRule(
  418. r'/IGNORE_EINTR\((?!.*close)',
  419. (
  420. 'IGNORE_EINTR is only valid when wrapping close. To wrap other system',
  421. 'calls, use HANDLE_EINTR. See http://crbug.com/269623',
  422. ),
  423. True,
  424. (
  425. # Files that #define IGNORE_EINTR.
  426. r'^base[\\/]posix[\\/]eintr_wrapper\.h$',
  427. r'^ppapi[\\/]tests[\\/]test_broker\.cc$',
  428. ),
  429. ),
  430. BanRule(
  431. r'/v8::Extension\(',
  432. (
  433. 'Do not introduce new v8::Extensions into the code base, use',
  434. 'gin::Wrappable instead. See http://crbug.com/334679',
  435. ),
  436. True,
  437. (
  438. r'extensions[\\/]renderer[\\/]safe_builtins\.*',
  439. ),
  440. ),
  441. BanRule(
  442. '#pragma comment(lib,',
  443. (
  444. 'Specify libraries to link with in build files and not in the source.',
  445. ),
  446. True,
  447. (
  448. r'^base[\\/]third_party[\\/]symbolize[\\/].*',
  449. r'^third_party[\\/]abseil-cpp[\\/].*',
  450. ),
  451. ),
  452. BanRule(
  453. r'/base::SequenceChecker\b',
  454. (
  455. 'Consider using SEQUENCE_CHECKER macros instead of the class directly.',
  456. ),
  457. False,
  458. (),
  459. ),
  460. BanRule(
  461. r'/base::ThreadChecker\b',
  462. (
  463. 'Consider using THREAD_CHECKER macros instead of the class directly.',
  464. ),
  465. False,
  466. (),
  467. ),
  468. BanRule(
  469. r'/(Time(|Delta|Ticks)|ThreadTicks)::FromInternalValue|ToInternalValue',
  470. (
  471. 'base::TimeXXX::FromInternalValue() and ToInternalValue() are',
  472. 'deprecated (http://crbug.com/634507). Please avoid converting away',
  473. 'from the Time types in Chromium code, especially if any math is',
  474. 'being done on time values. For interfacing with platform/library',
  475. 'APIs, use FromMicroseconds() or InMicroseconds(), or one of the other',
  476. 'type converter methods instead. For faking TimeXXX values (for unit',
  477. 'testing only), use TimeXXX() + Microseconds(N). For',
  478. 'other use cases, please contact base/time/OWNERS.',
  479. ),
  480. False,
  481. (),
  482. ),
  483. BanRule(
  484. 'CallJavascriptFunctionUnsafe',
  485. (
  486. "Don't use CallJavascriptFunctionUnsafe() in new code. Instead, use",
  487. 'AllowJavascript(), OnJavascriptAllowed()/OnJavascriptDisallowed(),',
  488. 'and CallJavascriptFunction(). See https://goo.gl/qivavq.',
  489. ),
  490. False,
  491. (
  492. r'^content[\\/]browser[\\/]webui[\\/]web_ui_impl\.(cc|h)$',
  493. r'^content[\\/]public[\\/]browser[\\/]web_ui\.h$',
  494. r'^content[\\/]public[\\/]test[\\/]test_web_ui\.(cc|h)$',
  495. ),
  496. ),
  497. BanRule(
  498. 'leveldb::DB::Open',
  499. (
  500. 'Instead of leveldb::DB::Open() use leveldb_env::OpenDB() from',
  501. 'third_party/leveldatabase/env_chromium.h. It exposes databases to',
  502. "Chrome's tracing, making their memory usage visible.",
  503. ),
  504. True,
  505. (
  506. r'^third_party/leveldatabase/.*\.(cc|h)$',
  507. ),
  508. ),
  509. BanRule(
  510. 'leveldb::NewMemEnv',
  511. (
  512. 'Instead of leveldb::NewMemEnv() use leveldb_chrome::NewMemEnv() from',
  513. 'third_party/leveldatabase/leveldb_chrome.h. It exposes environments',
  514. "to Chrome's tracing, making their memory usage visible.",
  515. ),
  516. True,
  517. (
  518. r'^third_party/leveldatabase/.*\.(cc|h)$',
  519. ),
  520. ),
  521. BanRule(
  522. 'RunLoop::QuitCurrent',
  523. (
  524. 'Please migrate away from RunLoop::QuitCurrent*() methods. Use member',
  525. 'methods of a specific RunLoop instance instead.',
  526. ),
  527. False,
  528. (),
  529. ),
  530. BanRule(
  531. 'base::ScopedMockTimeMessageLoopTaskRunner',
  532. (
  533. 'ScopedMockTimeMessageLoopTaskRunner is deprecated. Prefer',
  534. 'TaskEnvironment::TimeSource::MOCK_TIME. There are still a',
  535. 'few cases that may require a ScopedMockTimeMessageLoopTaskRunner',
  536. '(i.e. mocking the main MessageLoopForUI in browser_tests), but check',
  537. 'with gab@ first if you think you need it)',
  538. ),
  539. False,
  540. (),
  541. ),
  542. BanRule(
  543. 'std::regex',
  544. (
  545. 'Using std::regex adds unnecessary binary size to Chrome. Please use',
  546. 're2::RE2 instead (crbug.com/755321)',
  547. ),
  548. True,
  549. # Abseil's benchmarks never linked into chrome.
  550. ['third_party/abseil-cpp/.*_benchmark.cc'],
  551. ),
  552. BanRule(
  553. r'/\bstd::stoi\b',
  554. (
  555. 'std::stoi uses exceptions to communicate results. ',
  556. 'Use base::StringToInt() instead.',
  557. ),
  558. True,
  559. [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
  560. ),
  561. BanRule(
  562. r'/\bstd::stol\b',
  563. (
  564. 'std::stol uses exceptions to communicate results. ',
  565. 'Use base::StringToInt() instead.',
  566. ),
  567. True,
  568. [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
  569. ),
  570. BanRule(
  571. r'/\bstd::stoul\b',
  572. (
  573. 'std::stoul uses exceptions to communicate results. ',
  574. 'Use base::StringToUint() instead.',
  575. ),
  576. True,
  577. [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
  578. ),
  579. BanRule(
  580. r'/\bstd::stoll\b',
  581. (
  582. 'std::stoll uses exceptions to communicate results. ',
  583. 'Use base::StringToInt64() instead.',
  584. ),
  585. True,
  586. [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
  587. ),
  588. BanRule(
  589. r'/\bstd::stoull\b',
  590. (
  591. 'std::stoull uses exceptions to communicate results. ',
  592. 'Use base::StringToUint64() instead.',
  593. ),
  594. True,
  595. [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
  596. ),
  597. BanRule(
  598. r'/\bstd::stof\b',
  599. (
  600. 'std::stof uses exceptions to communicate results. ',
  601. 'For locale-independent values, e.g. reading numbers from disk',
  602. 'profiles, use base::StringToDouble().',
  603. 'For user-visible values, parse using ICU.',
  604. ),
  605. True,
  606. [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
  607. ),
  608. BanRule(
  609. r'/\bstd::stod\b',
  610. (
  611. 'std::stod uses exceptions to communicate results. ',
  612. 'For locale-independent values, e.g. reading numbers from disk',
  613. 'profiles, use base::StringToDouble().',
  614. 'For user-visible values, parse using ICU.',
  615. ),
  616. True,
  617. [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
  618. ),
  619. BanRule(
  620. r'/\bstd::stold\b',
  621. (
  622. 'std::stold uses exceptions to communicate results. ',
  623. 'For locale-independent values, e.g. reading numbers from disk',
  624. 'profiles, use base::StringToDouble().',
  625. 'For user-visible values, parse using ICU.',
  626. ),
  627. True,
  628. [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
  629. ),
  630. BanRule(
  631. r'/\bstd::to_string\b',
  632. (
  633. 'std::to_string is locale dependent and slower than alternatives.',
  634. 'For locale-independent strings, e.g. writing numbers to disk',
  635. 'profiles, use base::NumberToString().',
  636. 'For user-visible strings, use base::FormatNumber() and',
  637. 'the related functions in base/i18n/number_formatting.h.',
  638. ),
  639. False, # Only a warning since it is already used.
  640. [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
  641. ),
  642. BanRule(
  643. r'/\bstd::shared_ptr\b',
  644. (
  645. 'std::shared_ptr should not be used. Use scoped_refptr instead.',
  646. ),
  647. True,
  648. [
  649. # Needed for interop with third-party library.
  650. '^third_party/blink/renderer/core/typed_arrays/array_buffer/' +
  651. 'array_buffer_contents\.(cc|h)',
  652. '^third_party/blink/renderer/bindings/core/v8/' +
  653. 'v8_wasm_response_extensions.cc',
  654. '^gin/array_buffer\.(cc|h)',
  655. '^chrome/services/sharing/nearby/',
  656. # gRPC provides some C++ libraries that use std::shared_ptr<>.
  657. '^chromeos/services/libassistant/grpc/',
  658. '^chromecast/cast_core/grpc',
  659. '^chromecast/cast_core/runtime/browser',
  660. # Fuchsia provides C++ libraries that use std::shared_ptr<>.
  661. '^base/fuchsia/filtered_service_directory\.(cc|h)',
  662. '^base/fuchsia/service_directory_test_base\.h',
  663. '.*fuchsia.*test\.(cc|h)',
  664. # Needed for clang plugin tests
  665. '^tools/clang/plugins/tests/',
  666. _THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders.
  667. ),
  668. BanRule(
  669. r'/\bstd::weak_ptr\b',
  670. (
  671. 'std::weak_ptr should not be used. Use base::WeakPtr instead.',
  672. ),
  673. True,
  674. [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders.
  675. ),
  676. BanRule(
  677. r'/\blong long\b',
  678. (
  679. 'long long is banned. Use stdint.h if you need a 64 bit number.',
  680. ),
  681. False, # Only a warning since it is already used.
  682. [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
  683. ),
  684. BanRule(
  685. r'\b(absl|std)::any\b',
  686. (
  687. 'absl::any / std::any are not safe to use in a component build.',
  688. ),
  689. True,
  690. # Not an error in third party folders, though it probably should be :)
  691. [_THIRD_PARTY_EXCEPT_BLINK],
  692. ),
  693. BanRule(
  694. r'/\bstd::bind\b',
  695. (
  696. 'std::bind is banned because of lifetime risks.',
  697. 'Use base::BindOnce or base::BindRepeating instead.',
  698. ),
  699. True,
  700. [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders.
  701. ),
  702. BanRule(
  703. r'/\bstd::optional\b',
  704. (
  705. 'std::optional is banned. Use absl::optional instead.',
  706. ),
  707. True,
  708. [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders.
  709. ),
  710. BanRule(
  711. r'/\b#include <chrono>\b',
  712. (
  713. '<chrono> overlaps with Time APIs in base. Keep using',
  714. 'base classes.',
  715. ),
  716. True,
  717. [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders.
  718. ),
  719. BanRule(
  720. r'/\b#include <exception>\b',
  721. (
  722. 'Exceptions are banned and disabled in Chromium.',
  723. ),
  724. True,
  725. [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders.
  726. ),
  727. BanRule(
  728. r'/\bstd::function\b',
  729. (
  730. 'std::function is banned. Instead use base::OnceCallback or ',
  731. 'base::RepeatingCallback, which directly support Chromium\'s weak ',
  732. 'pointers, ref counting and more.',
  733. ),
  734. False, # Only a warning since it is already used.
  735. [_THIRD_PARTY_EXCEPT_BLINK], # Do not warn in third_party folders.
  736. ),
  737. BanRule(
  738. r'/\b#include <random>\b',
  739. (
  740. 'Do not use any random number engines from <random>. Instead',
  741. 'use base::RandomBitGenerator.',
  742. ),
  743. True,
  744. [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders.
  745. ),
  746. BanRule(
  747. r'/\b#include <X11/',
  748. (
  749. 'Do not use Xlib. Use xproto (from //ui/gfx/x:xproto) instead.',
  750. ),
  751. True,
  752. [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders.
  753. ),
  754. BanRule(
  755. r'/\bstd::ratio\b',
  756. (
  757. 'std::ratio is banned by the Google Style Guide.',
  758. ),
  759. True,
  760. [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders.
  761. ),
  762. BanRule(
  763. ('base::ThreadRestrictions::ScopedAllowIO'),
  764. (
  765. 'ScopedAllowIO is deprecated, use ScopedAllowBlocking instead.',
  766. ),
  767. False,
  768. (),
  769. ),
  770. BanRule(
  771. r'/\bRunMessageLoop\b',
  772. (
  773. 'RunMessageLoop is deprecated, use RunLoop instead.',
  774. ),
  775. False,
  776. (),
  777. ),
  778. BanRule(
  779. 'RunThisRunLoop',
  780. (
  781. 'RunThisRunLoop is deprecated, use RunLoop directly instead.',
  782. ),
  783. False,
  784. (),
  785. ),
  786. BanRule(
  787. 'RunAllPendingInMessageLoop()',
  788. (
  789. "Prefer RunLoop over RunAllPendingInMessageLoop, please contact gab@",
  790. "if you're convinced you need this.",
  791. ),
  792. False,
  793. (),
  794. ),
  795. BanRule(
  796. 'RunAllPendingInMessageLoop(BrowserThread',
  797. (
  798. 'RunAllPendingInMessageLoop is deprecated. Use RunLoop for',
  799. 'BrowserThread::UI, BrowserTaskEnvironment::RunIOThreadUntilIdle',
  800. 'for BrowserThread::IO, and prefer RunLoop::QuitClosure to observe',
  801. 'async events instead of flushing threads.',
  802. ),
  803. False,
  804. (),
  805. ),
  806. BanRule(
  807. r'MessageLoopRunner',
  808. (
  809. 'MessageLoopRunner is deprecated, use RunLoop instead.',
  810. ),
  811. False,
  812. (),
  813. ),
  814. BanRule(
  815. 'GetDeferredQuitTaskForRunLoop',
  816. (
  817. "GetDeferredQuitTaskForRunLoop shouldn't be needed, please contact",
  818. "gab@ if you found a use case where this is the only solution.",
  819. ),
  820. False,
  821. (),
  822. ),
  823. BanRule(
  824. 'sqlite3_initialize(',
  825. (
  826. 'Instead of calling sqlite3_initialize(), depend on //sql, ',
  827. '#include "sql/initialize.h" and use sql::EnsureSqliteInitialized().',
  828. ),
  829. True,
  830. (
  831. r'^sql/initialization\.(cc|h)$',
  832. r'^third_party/sqlite/.*\.(c|cc|h)$',
  833. ),
  834. ),
  835. BanRule(
  836. 'std::random_shuffle',
  837. (
  838. 'std::random_shuffle is deprecated in C++14, and removed in C++17. Use',
  839. 'base::RandomShuffle instead.'
  840. ),
  841. True,
  842. (),
  843. ),
  844. BanRule(
  845. 'ios/web/public/test/http_server',
  846. (
  847. 'web::HTTPserver is deprecated use net::EmbeddedTestServer instead.',
  848. ),
  849. False,
  850. (),
  851. ),
  852. BanRule(
  853. 'GetAddressOf',
  854. (
  855. 'Improper use of Microsoft::WRL::ComPtr<T>::GetAddressOf() has been ',
  856. 'implicated in a few leaks. ReleaseAndGetAddressOf() is safe but ',
  857. 'operator& is generally recommended. So always use operator& instead. ',
  858. 'See http://crbug.com/914910 for more conversion guidance.'
  859. ),
  860. True,
  861. (),
  862. ),
  863. BanRule(
  864. 'SHFileOperation',
  865. (
  866. 'SHFileOperation was deprecated in Windows Vista, and there are less ',
  867. 'complex functions to achieve the same goals. Use IFileOperation for ',
  868. 'any esoteric actions instead.'
  869. ),
  870. True,
  871. (),
  872. ),
  873. BanRule(
  874. 'StringFromGUID2',
  875. (
  876. 'StringFromGUID2 introduces an unnecessary dependency on ole32.dll.',
  877. 'Use base::win::WStringFromGUID instead.'
  878. ),
  879. True,
  880. (
  881. r'/base/win/win_util_unittest.cc',
  882. ),
  883. ),
  884. BanRule(
  885. 'StringFromCLSID',
  886. (
  887. 'StringFromCLSID introduces an unnecessary dependency on ole32.dll.',
  888. 'Use base::win::WStringFromGUID instead.'
  889. ),
  890. True,
  891. (
  892. r'/base/win/win_util_unittest.cc',
  893. ),
  894. ),
  895. BanRule(
  896. 'kCFAllocatorNull',
  897. (
  898. 'The use of kCFAllocatorNull with the NoCopy creation of ',
  899. 'CoreFoundation types is prohibited.',
  900. ),
  901. True,
  902. (),
  903. ),
  904. BanRule(
  905. 'mojo::ConvertTo',
  906. (
  907. 'mojo::ConvertTo and TypeConverter are deprecated. Please consider',
  908. 'StructTraits / UnionTraits / EnumTraits / ArrayTraits / MapTraits /',
  909. 'StringTraits if you would like to convert between custom types and',
  910. 'the wire format of mojom types.'
  911. ),
  912. False,
  913. (
  914. r'^fuchsia_web/webengine/browser/url_request_rewrite_rules_manager\.cc$',
  915. r'^fuchsia_web/webengine/url_request_rewrite_type_converters\.cc$',
  916. r'^third_party/blink/.*\.(cc|h)$',
  917. r'^content/renderer/.*\.(cc|h)$',
  918. ),
  919. ),
  920. BanRule(
  921. 'GetInterfaceProvider',
  922. (
  923. 'InterfaceProvider is deprecated.',
  924. 'Please use ExecutionContext::GetBrowserInterfaceBroker and overrides',
  925. 'or Platform::GetBrowserInterfaceBroker.'
  926. ),
  927. False,
  928. (),
  929. ),
  930. BanRule(
  931. 'CComPtr',
  932. (
  933. 'New code should use Microsoft::WRL::ComPtr from wrl/client.h as a ',
  934. 'replacement for CComPtr from ATL. See http://crbug.com/5027 for more ',
  935. 'details.'
  936. ),
  937. False,
  938. (),
  939. ),
  940. BanRule(
  941. r'/\b(IFACE|STD)METHOD_?\(',
  942. (
  943. 'IFACEMETHOD() and STDMETHOD() make code harder to format and read.',
  944. 'Instead, always use IFACEMETHODIMP in the declaration.'
  945. ),
  946. False,
  947. [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders.
  948. ),
  949. BanRule(
  950. 'set_owned_by_client',
  951. (
  952. 'set_owned_by_client is deprecated.',
  953. 'views::View already owns the child views by default. This introduces ',
  954. 'a competing ownership model which makes the code difficult to reason ',
  955. 'about. See http://crbug.com/1044687 for more details.'
  956. ),
  957. False,
  958. (),
  959. ),
  960. BanRule(
  961. 'RemoveAllChildViewsWithoutDeleting',
  962. (
  963. 'RemoveAllChildViewsWithoutDeleting is deprecated.',
  964. 'This method is deemed dangerous as, unless raw pointers are re-added,',
  965. 'calls to this method introduce memory leaks.'
  966. ),
  967. False,
  968. (),
  969. ),
  970. BanRule(
  971. r'/\bTRACE_EVENT_ASYNC_',
  972. (
  973. 'Please use TRACE_EVENT_NESTABLE_ASYNC_.. macros instead',
  974. 'of TRACE_EVENT_ASYNC_.. (crbug.com/1038710).',
  975. ),
  976. False,
  977. (
  978. r'^base/trace_event/.*',
  979. r'^base/tracing/.*',
  980. ),
  981. ),
  982. BanRule(
  983. r'/\bbase::debug::DumpWithoutCrashingUnthrottled[(][)]',
  984. (
  985. 'base::debug::DumpWithoutCrashingUnthrottled() does not throttle',
  986. 'dumps and may spam crash reports. Consider if the throttled',
  987. 'variants suffice instead.',
  988. ),
  989. False,
  990. (),
  991. ),
  992. BanRule(
  993. 'RoInitialize',
  994. (
  995. 'Improper use of [base::win]::RoInitialize() has been implicated in a ',
  996. 'few COM initialization leaks. Use base::win::ScopedWinrtInitializer ',
  997. 'instead. See http://crbug.com/1197722 for more information.'
  998. ),
  999. True,
  1000. (
  1001. r'^base[\\/]win[\\/]scoped_winrt_initializer\.cc$',
  1002. ),
  1003. ),
  1004. BanRule(
  1005. r'base::Watchdog',
  1006. (
  1007. 'base::Watchdog is deprecated because it creates its own thread.',
  1008. 'Instead, manually start a timer on a SequencedTaskRunner.',
  1009. ),
  1010. False,
  1011. (),
  1012. ),
  1013. BanRule(
  1014. 'base::Passed',
  1015. (
  1016. 'Do not use base::Passed. It is a legacy helper for capturing ',
  1017. 'move-only types with base::BindRepeating, but invoking the ',
  1018. 'resulting RepeatingCallback moves the captured value out of ',
  1019. 'the callback storage, and subsequent invocations may pass the ',
  1020. 'value in a valid but undefined state. Prefer base::BindOnce().',
  1021. 'See http://crbug.com/1326449 for context.'
  1022. ),
  1023. False,
  1024. (),
  1025. ),
  1026. BanRule(
  1027. r'/\babsl::FunctionRef\b',
  1028. (
  1029. 'absl::FunctionRef is banned. Use base::FunctionRef instead.',
  1030. ),
  1031. True,
  1032. [
  1033. # base::Bind{Once,Repeating} references absl::FunctionRef to disallow
  1034. # interoperability.
  1035. r'^base[\\/]bind_internal\.h',
  1036. # base::FunctionRef is implemented on top of absl::FunctionRef.
  1037. r'^base[\\/]functional[\\/]function_ref.*\..+',
  1038. # Not an error in third_party folders.
  1039. _THIRD_PARTY_EXCEPT_BLINK,
  1040. ],
  1041. ),
  1042. )
  1043. _BANNED_MOJOM_PATTERNS : Sequence[BanRule] = (
  1044. BanRule(
  1045. 'handle<shared_buffer>',
  1046. (
  1047. 'Please use one of the more specific shared memory types instead:',
  1048. ' mojo_base.mojom.ReadOnlySharedMemoryRegion',
  1049. ' mojo_base.mojom.WritableSharedMemoryRegion',
  1050. ' mojo_base.mojom.UnsafeSharedMemoryRegion',
  1051. ),
  1052. True,
  1053. ),
  1054. )
  1055. _IPC_ENUM_TRAITS_DEPRECATED = (
  1056. 'You are using IPC_ENUM_TRAITS() in your code. It has been deprecated.\n'
  1057. 'See http://www.chromium.org/Home/chromium-security/education/'
  1058. 'security-tips-for-ipc')
  1059. _LONG_PATH_ERROR = (
  1060. 'Some files included in this CL have file names that are too long (> 200'
  1061. ' characters). If committed, these files will cause issues on Windows. See'
  1062. ' https://crbug.com/612667 for more details.'
  1063. )
  1064. _JAVA_MULTIPLE_DEFINITION_EXCLUDED_PATHS = [
  1065. r".*[\\/]AppHooksImpl\.java",
  1066. r".*[\\/]BuildHooksAndroidImpl\.java",
  1067. r".*[\\/]LicenseContentProvider\.java",
  1068. r".*[\\/]PlatformServiceBridgeImpl.java",
  1069. r".*chrome[\\\/]android[\\\/]feed[\\\/]dummy[\\\/].*\.java",
  1070. ]
  1071. # List of image extensions that are used as resources in chromium.
  1072. _IMAGE_EXTENSIONS = ['.svg', '.png', '.webp']
  1073. # These paths contain test data and other known invalid JSON files.
  1074. _KNOWN_TEST_DATA_AND_INVALID_JSON_FILE_PATTERNS = [
  1075. r'test[\\/]data[\\/]',
  1076. r'testing[\\/]buildbot[\\/]',
  1077. r'^components[\\/]policy[\\/]resources[\\/]policy_templates\.json$',
  1078. r'^third_party[\\/]protobuf[\\/]',
  1079. r'^third_party[\\/]blink[\\/]perf_tests[\\/]speedometer[\\/]resources[\\/]todomvc[\\/]learn.json',
  1080. r'^third_party[\\/]blink[\\/]renderer[\\/]devtools[\\/]protocol\.json$',
  1081. r'^third_party[\\/]blink[\\/]web_tests[\\/]external[\\/]wpt[\\/]',
  1082. r'^tools[\\/]perf[\\/]',
  1083. r'^tools[\\/]traceline[\\/]svgui[\\/]startup-release.json',
  1084. # vscode configuration files allow comments
  1085. r'^tools[\\/]vscode[\\/]',
  1086. ]
  1087. # These are not checked on the public chromium-presubmit trybot.
  1088. # Add files here that rely on .py files that exists only for target_os="android"
  1089. # checkouts.
  1090. _ANDROID_SPECIFIC_PYDEPS_FILES = [
  1091. 'chrome/android/features/create_stripped_java_factory.pydeps',
  1092. ]
  1093. _GENERIC_PYDEPS_FILES = [
  1094. 'android_webview/test/components/run_webview_component_smoketest.pydeps',
  1095. 'android_webview/tools/run_cts.pydeps',
  1096. 'base/android/jni_generator/jni_generator.pydeps',
  1097. 'base/android/jni_generator/jni_registration_generator.pydeps',
  1098. 'build/android/apk_operations.pydeps',
  1099. 'build/android/devil_chromium.pydeps',
  1100. 'build/android/gyp/aar.pydeps',
  1101. 'build/android/gyp/aidl.pydeps',
  1102. 'build/android/gyp/allot_native_libraries.pydeps',
  1103. 'build/android/gyp/apkbuilder.pydeps',
  1104. 'build/android/gyp/assert_static_initializers.pydeps',
  1105. 'build/android/gyp/bytecode_processor.pydeps',
  1106. 'build/android/gyp/bytecode_rewriter.pydeps',
  1107. 'build/android/gyp/check_flag_expectations.pydeps',
  1108. 'build/android/gyp/compile_java.pydeps',
  1109. 'build/android/gyp/compile_resources.pydeps',
  1110. 'build/android/gyp/copy_ex.pydeps',
  1111. 'build/android/gyp/create_apk_operations_script.pydeps',
  1112. 'build/android/gyp/create_app_bundle.pydeps',
  1113. 'build/android/gyp/create_app_bundle_apks.pydeps',
  1114. 'build/android/gyp/create_bundle_wrapper_script.pydeps',
  1115. 'build/android/gyp/create_java_binary_script.pydeps',
  1116. 'build/android/gyp/create_r_java.pydeps',
  1117. 'build/android/gyp/create_r_txt.pydeps',
  1118. 'build/android/gyp/create_size_info_files.pydeps',
  1119. 'build/android/gyp/create_test_apk_wrapper_script.pydeps',
  1120. 'build/android/gyp/create_ui_locale_resources.pydeps',
  1121. 'build/android/gyp/dex.pydeps',
  1122. 'build/android/gyp/dex_jdk_libs.pydeps',
  1123. 'build/android/gyp/dexsplitter.pydeps',
  1124. 'build/android/gyp/dist_aar.pydeps',
  1125. 'build/android/gyp/filter_zip.pydeps',
  1126. 'build/android/gyp/flatc_java.pydeps',
  1127. 'build/android/gyp/gcc_preprocess.pydeps',
  1128. 'build/android/gyp/generate_linker_version_script.pydeps',
  1129. 'build/android/gyp/ijar.pydeps',
  1130. 'build/android/gyp/jacoco_instr.pydeps',
  1131. 'build/android/gyp/java_cpp_enum.pydeps',
  1132. 'build/android/gyp/java_cpp_features.pydeps',
  1133. 'build/android/gyp/java_cpp_strings.pydeps',
  1134. 'build/android/gyp/java_google_api_keys.pydeps',
  1135. 'build/android/gyp/jinja_template.pydeps',
  1136. 'build/android/gyp/lint.pydeps',
  1137. 'build/android/gyp/merge_manifest.pydeps',
  1138. 'build/android/gyp/optimize_resources.pydeps',
  1139. 'build/android/gyp/prepare_resources.pydeps',
  1140. 'build/android/gyp/process_native_prebuilt.pydeps',
  1141. 'build/android/gyp/proguard.pydeps',
  1142. 'build/android/gyp/system_image_apks.pydeps',
  1143. 'build/android/gyp/trace_event_bytecode_rewriter.pydeps',
  1144. 'build/android/gyp/turbine.pydeps',
  1145. 'build/android/gyp/unused_resources.pydeps',
  1146. 'build/android/gyp/validate_static_library_dex_references.pydeps',
  1147. 'build/android/gyp/write_build_config.pydeps',
  1148. 'build/android/gyp/write_native_libraries_java.pydeps',
  1149. 'build/android/gyp/zip.pydeps',
  1150. 'build/android/incremental_install/generate_android_manifest.pydeps',
  1151. 'build/android/incremental_install/write_installer_json.pydeps',
  1152. 'build/android/pylib/results/presentation/test_results_presentation.pydeps',
  1153. 'build/android/resource_sizes.pydeps',
  1154. 'build/android/test_runner.pydeps',
  1155. 'build/android/test_wrapper/logdog_wrapper.pydeps',
  1156. 'build/lacros/lacros_resource_sizes.pydeps',
  1157. 'build/protoc_java.pydeps',
  1158. 'chrome/android/monochrome/scripts/monochrome_python_tests.pydeps',
  1159. 'chrome/test/chromedriver/log_replay/client_replay_unittest.pydeps',
  1160. 'chrome/test/chromedriver/test/run_py_tests.pydeps',
  1161. 'chromecast/resource_sizes/chromecast_resource_sizes.pydeps',
  1162. 'components/cronet/tools/generate_javadoc.pydeps',
  1163. 'components/cronet/tools/jar_src.pydeps',
  1164. 'components/module_installer/android/module_desc_java.pydeps',
  1165. 'content/public/android/generate_child_service.pydeps',
  1166. 'net/tools/testserver/testserver.pydeps',
  1167. 'testing/scripts/run_wpt_tests.pydeps',
  1168. 'testing/scripts/run_isolated_script_test.pydeps',
  1169. 'testing/merge_scripts/standard_isolated_script_merge.pydeps',
  1170. 'testing/merge_scripts/standard_gtest_merge.pydeps',
  1171. 'testing/merge_scripts/code_coverage/merge_results.pydeps',
  1172. 'testing/merge_scripts/code_coverage/merge_steps.pydeps',
  1173. 'third_party/android_platform/development/scripts/stack.pydeps',
  1174. 'third_party/blink/renderer/bindings/scripts/build_web_idl_database.pydeps',
  1175. 'third_party/blink/renderer/bindings/scripts/check_generated_file_list.pydeps',
  1176. 'third_party/blink/renderer/bindings/scripts/collect_idl_files.pydeps',
  1177. 'third_party/blink/renderer/bindings/scripts/generate_bindings.pydeps',
  1178. 'third_party/blink/renderer/bindings/scripts/validate_web_idl.pydeps',
  1179. 'third_party/blink/tools/blinkpy/web_tests/merge_results.pydeps',
  1180. 'third_party/blink/tools/merge_web_test_results.pydeps',
  1181. 'tools/binary_size/sizes.pydeps',
  1182. 'tools/binary_size/supersize.pydeps',
  1183. 'tools/perf/process_perf_results.pydeps',
  1184. ]
  1185. _ALL_PYDEPS_FILES = _ANDROID_SPECIFIC_PYDEPS_FILES + _GENERIC_PYDEPS_FILES
  1186. # Bypass the AUTHORS check for these accounts.
  1187. _KNOWN_ROBOTS = set(
  1188. ) | set('%s@appspot.gserviceaccount.com' % s for s in ('findit-for-me',)
  1189. ) | set('%s@developer.gserviceaccount.com' % s for s in ('3su6n15k.default',)
  1190. ) | set('%s@chops-service-accounts.iam.gserviceaccount.com' % s
  1191. for s in ('bling-autoroll-builder', 'v8-ci-autoroll-builder',
  1192. 'wpt-autoroller', 'chrome-weblayer-builder',
  1193. 'lacros-version-skew-roller', 'skylab-test-cros-roller',
  1194. 'infra-try-recipes-tester', 'lacros-tracking-roller',
  1195. 'lacros-sdk-version-roller')
  1196. ) | set('%s@skia-public.iam.gserviceaccount.com' % s
  1197. for s in ('chromium-autoroll', 'chromium-release-autoroll')
  1198. ) | set('%s@skia-corp.google.com.iam.gserviceaccount.com' % s
  1199. for s in ('chromium-internal-autoroll',)
  1200. ) | set('%s@owners-cleanup-prod.google.com.iam.gserviceaccount.com' % s
  1201. for s in ('swarming-tasks',))
  1202. _INVALID_GRD_FILE_LINE = [
  1203. (r'<file lang=.* path=.*', 'Path should come before lang in GRD files.')
  1204. ]
  1205. def _IsCPlusPlusFile(input_api, file_path):
  1206. """Returns True if this file contains C++-like code (and not Python,
  1207. Go, Java, MarkDown, ...)"""
  1208. ext = input_api.os_path.splitext(file_path)[1]
  1209. # This list is compatible with CppChecker.IsCppFile but we should
  1210. # consider adding ".c" to it. If we do that we can use this function
  1211. # at more places in the code.
  1212. return ext in (
  1213. '.h',
  1214. '.cc',
  1215. '.cpp',
  1216. '.m',
  1217. '.mm',
  1218. )
  1219. def _IsCPlusPlusHeaderFile(input_api, file_path):
  1220. return input_api.os_path.splitext(file_path)[1] == ".h"
  1221. def _IsJavaFile(input_api, file_path):
  1222. return input_api.os_path.splitext(file_path)[1] == ".java"
  1223. def _IsProtoFile(input_api, file_path):
  1224. return input_api.os_path.splitext(file_path)[1] == ".proto"
  1225. def _IsXmlOrGrdFile(input_api, file_path):
  1226. ext = input_api.os_path.splitext(file_path)[1]
  1227. return ext in ('.grd', '.xml')
  1228. def CheckNoUpstreamDepsOnClank(input_api, output_api):
  1229. """Prevent additions of dependencies from the upstream repo on //clank."""
  1230. # clank can depend on clank
  1231. if input_api.change.RepositoryRoot().endswith('clank'):
  1232. return []
  1233. build_file_patterns = [
  1234. r'(.+/)?BUILD\.gn',
  1235. r'.+\.gni',
  1236. ]
  1237. excluded_files = [r'build[/\\]config[/\\]android[/\\]config\.gni']
  1238. bad_pattern = input_api.re.compile(r'^[^#]*//clank')
  1239. error_message = 'Disallowed import on //clank in an upstream build file:'
  1240. def FilterFile(affected_file):
  1241. return input_api.FilterSourceFile(affected_file,
  1242. files_to_check=build_file_patterns,
  1243. files_to_skip=excluded_files)
  1244. problems = []
  1245. for f in input_api.AffectedSourceFiles(FilterFile):
  1246. local_path = f.LocalPath()
  1247. for line_number, line in f.ChangedContents():
  1248. if (bad_pattern.search(line)):
  1249. problems.append('%s:%d\n %s' %
  1250. (local_path, line_number, line.strip()))
  1251. if problems:
  1252. return [output_api.PresubmitPromptOrNotify(error_message, problems)]
  1253. else:
  1254. return []
  1255. def CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
  1256. """Attempts to prevent use of functions intended only for testing in
  1257. non-testing code. For now this is just a best-effort implementation
  1258. that ignores header files and may have some false positives. A
  1259. better implementation would probably need a proper C++ parser.
  1260. """
  1261. # We only scan .cc files and the like, as the declaration of
  1262. # for-testing functions in header files are hard to distinguish from
  1263. # calls to such functions without a proper C++ parser.
  1264. file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS]
  1265. base_function_pattern = r'[ :]test::[^\s]+|ForTest(s|ing)?|for_test(s|ing)?'
  1266. inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' %
  1267. base_function_pattern)
  1268. comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern)
  1269. allowlist_pattern = input_api.re.compile(r'// IN-TEST$')
  1270. exclusion_pattern = input_api.re.compile(
  1271. r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' %
  1272. (base_function_pattern, base_function_pattern))
  1273. # Avoid a false positive in this case, where the method name, the ::, and
  1274. # the closing { are all on different lines due to line wrapping.
  1275. # HelperClassForTesting::
  1276. # HelperClassForTesting(
  1277. # args)
  1278. # : member(0) {}
  1279. method_defn_pattern = input_api.re.compile(r'[A-Za-z0-9_]+::$')
  1280. def FilterFile(affected_file):
  1281. files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS +
  1282. input_api.DEFAULT_FILES_TO_SKIP)
  1283. return input_api.FilterSourceFile(
  1284. affected_file,
  1285. files_to_check=file_inclusion_pattern,
  1286. files_to_skip=files_to_skip)
  1287. problems = []
  1288. for f in input_api.AffectedSourceFiles(FilterFile):
  1289. local_path = f.LocalPath()
  1290. in_method_defn = False
  1291. for line_number, line in f.ChangedContents():
  1292. if (inclusion_pattern.search(line)
  1293. and not comment_pattern.search(line)
  1294. and not exclusion_pattern.search(line)
  1295. and not allowlist_pattern.search(line)
  1296. and not in_method_defn):
  1297. problems.append('%s:%d\n %s' %
  1298. (local_path, line_number, line.strip()))
  1299. in_method_defn = method_defn_pattern.search(line)
  1300. if problems:
  1301. return [
  1302. output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)
  1303. ]
  1304. else:
  1305. return []
  1306. def CheckNoProductionCodeUsingTestOnlyFunctionsJava(input_api, output_api):
  1307. """This is a simplified version of
  1308. CheckNoProductionCodeUsingTestOnlyFunctions for Java files.
  1309. """
  1310. javadoc_start_re = input_api.re.compile(r'^\s*/\*\*')
  1311. javadoc_end_re = input_api.re.compile(r'^\s*\*/')
  1312. name_pattern = r'ForTest(s|ing)?'
  1313. # Describes an occurrence of "ForTest*" inside a // comment.
  1314. comment_re = input_api.re.compile(r'//.*%s' % name_pattern)
  1315. # Describes @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
  1316. annotation_re = input_api.re.compile(r'@VisibleForTesting\(')
  1317. # Catch calls.
  1318. inclusion_re = input_api.re.compile(r'(%s)\s*\(' % name_pattern)
  1319. # Ignore definitions. (Comments are ignored separately.)
  1320. exclusion_re = input_api.re.compile(r'(%s)[^;]+\{' % name_pattern)
  1321. problems = []
  1322. sources = lambda x: input_api.FilterSourceFile(
  1323. x,
  1324. files_to_skip=(('(?i).*test', r'.*\/junit\/') + input_api.
  1325. DEFAULT_FILES_TO_SKIP),
  1326. files_to_check=[r'.*\.java$'])
  1327. for f in input_api.AffectedFiles(include_deletes=False,
  1328. file_filter=sources):
  1329. local_path = f.LocalPath()
  1330. is_inside_javadoc = False
  1331. for line_number, line in f.ChangedContents():
  1332. if is_inside_javadoc and javadoc_end_re.search(line):
  1333. is_inside_javadoc = False
  1334. if not is_inside_javadoc and javadoc_start_re.search(line):
  1335. is_inside_javadoc = True
  1336. if is_inside_javadoc:
  1337. continue
  1338. if (inclusion_re.search(line) and not comment_re.search(line)
  1339. and not annotation_re.search(line)
  1340. and not exclusion_re.search(line)):
  1341. problems.append('%s:%d\n %s' %
  1342. (local_path, line_number, line.strip()))
  1343. if problems:
  1344. return [
  1345. output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)
  1346. ]
  1347. else:
  1348. return []
  1349. def CheckNoIOStreamInHeaders(input_api, output_api):
  1350. """Checks to make sure no .h files include <iostream>."""
  1351. files = []
  1352. pattern = input_api.re.compile(r'^#include\s*<iostream>',
  1353. input_api.re.MULTILINE)
  1354. for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
  1355. if not f.LocalPath().endswith('.h'):
  1356. continue
  1357. contents = input_api.ReadFile(f)
  1358. if pattern.search(contents):
  1359. files.append(f)
  1360. if len(files):
  1361. return [
  1362. output_api.PresubmitError(
  1363. 'Do not #include <iostream> in header files, since it inserts static '
  1364. 'initialization into every file including the header. Instead, '
  1365. '#include <ostream>. See http://crbug.com/94794', files)
  1366. ]
  1367. return []
  1368. def CheckNoStrCatRedefines(input_api, output_api):
  1369. """Checks no windows headers with StrCat redefined are included directly."""
  1370. files = []
  1371. files_to_check = (r'.+%s' % _HEADER_EXTENSIONS,
  1372. r'.+%s' % _IMPLEMENTATION_EXTENSIONS)
  1373. files_to_skip = (input_api.DEFAULT_FILES_TO_SKIP +
  1374. _NON_BASE_DEPENDENT_PATHS)
  1375. sources_filter = lambda f: input_api.FilterSourceFile(
  1376. f, files_to_check=files_to_check, files_to_skip=files_to_skip)
  1377. pattern_deny = input_api.re.compile(
  1378. r'^#include\s*[<"](shlwapi|atlbase|propvarutil|sphelper).h[">]',
  1379. input_api.re.MULTILINE)
  1380. pattern_allow = input_api.re.compile(
  1381. r'^#include\s"base/win/windows_defines.inc"', input_api.re.MULTILINE)
  1382. for f in input_api.AffectedSourceFiles(sources_filter):
  1383. contents = input_api.ReadFile(f)
  1384. if pattern_deny.search(
  1385. contents) and not pattern_allow.search(contents):
  1386. files.append(f.LocalPath())
  1387. if len(files):
  1388. return [
  1389. output_api.PresubmitError(
  1390. 'Do not #include shlwapi.h, atlbase.h, propvarutil.h or sphelper.h '
  1391. 'directly since they pollute code with StrCat macro. Instead, '
  1392. 'include matching header from base/win. See http://crbug.com/856536',
  1393. files)
  1394. ]
  1395. return []
  1396. def CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
  1397. """Checks to make sure no source files use UNIT_TEST."""
  1398. problems = []
  1399. for f in input_api.AffectedFiles():
  1400. if (not f.LocalPath().endswith(('.cc', '.mm'))):
  1401. continue
  1402. for line_num, line in f.ChangedContents():
  1403. if 'UNIT_TEST ' in line or line.endswith('UNIT_TEST'):
  1404. problems.append(' %s:%d' % (f.LocalPath(), line_num))
  1405. if not problems:
  1406. return []
  1407. return [
  1408. output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
  1409. '\n'.join(problems))
  1410. ]
  1411. def CheckNoDISABLETypoInTests(input_api, output_api):
  1412. """Checks to prevent attempts to disable tests with DISABLE_ prefix.
  1413. This test warns if somebody tries to disable a test with the DISABLE_ prefix
  1414. instead of DISABLED_. To filter false positives, reports are only generated
  1415. if a corresponding MAYBE_ line exists.
  1416. """
  1417. problems = []
  1418. # The following two patterns are looked for in tandem - is a test labeled
  1419. # as MAYBE_ followed by a DISABLE_ (instead of the correct DISABLED)
  1420. maybe_pattern = input_api.re.compile(r'MAYBE_([a-zA-Z0-9_]+)')
  1421. disable_pattern = input_api.re.compile(r'DISABLE_([a-zA-Z0-9_]+)')
  1422. # This is for the case that a test is disabled on all platforms.
  1423. full_disable_pattern = input_api.re.compile(
  1424. r'^\s*TEST[^(]*\([a-zA-Z0-9_]+,\s*DISABLE_[a-zA-Z0-9_]+\)',
  1425. input_api.re.MULTILINE)
  1426. for f in input_api.AffectedFiles(False):
  1427. if not 'test' in f.LocalPath() or not f.LocalPath().endswith('.cc'):
  1428. continue
  1429. # Search for MABYE_, DISABLE_ pairs.
  1430. disable_lines = {} # Maps of test name to line number.
  1431. maybe_lines = {}
  1432. for line_num, line in f.ChangedContents():
  1433. disable_match = disable_pattern.search(line)
  1434. if disable_match:
  1435. disable_lines[disable_match.group(1)] = line_num
  1436. maybe_match = maybe_pattern.search(line)
  1437. if maybe_match:
  1438. maybe_lines[maybe_match.group(1)] = line_num
  1439. # Search for DISABLE_ occurrences within a TEST() macro.
  1440. disable_tests = set(disable_lines.keys())
  1441. maybe_tests = set(maybe_lines.keys())
  1442. for test in disable_tests.intersection(maybe_tests):
  1443. problems.append(' %s:%d' % (f.LocalPath(), disable_lines[test]))
  1444. contents = input_api.ReadFile(f)
  1445. full_disable_match = full_disable_pattern.search(contents)
  1446. if full_disable_match:
  1447. problems.append(' %s' % f.LocalPath())
  1448. if not problems:
  1449. return []
  1450. return [
  1451. output_api.PresubmitPromptWarning(
  1452. 'Attempt to disable a test with DISABLE_ instead of DISABLED_?\n' +
  1453. '\n'.join(problems))
  1454. ]
  1455. def CheckForgettingMAYBEInTests(input_api, output_api):
  1456. """Checks to make sure tests disabled conditionally are not missing a
  1457. corresponding MAYBE_ prefix.
  1458. """
  1459. # Expect at least a lowercase character in the test name. This helps rule out
  1460. # false positives with macros wrapping the actual tests name.
  1461. define_maybe_pattern = input_api.re.compile(
  1462. r'^\#define MAYBE_(?P<test_name>\w*[a-z]\w*)')
  1463. # The test_maybe_pattern needs to handle all of these forms. The standard:
  1464. # IN_PROC_TEST_F(SyncTest, MAYBE_Start) {
  1465. # With a wrapper macro around the test name:
  1466. # IN_PROC_TEST_F(SyncTest, E2E_ENABLED(MAYBE_Start)) {
  1467. # And the odd-ball NACL_BROWSER_TEST_f format:
  1468. # NACL_BROWSER_TEST_F(NaClBrowserTest, SimpleLoad, {
  1469. # The optional E2E_ENABLED-style is handled with (\w*\()?
  1470. # The NACL_BROWSER_TEST_F pattern is handled by allowing a trailing comma or
  1471. # trailing ')'.
  1472. test_maybe_pattern = (
  1473. r'^\s*\w*TEST[^(]*\(\s*\w+,\s*(\w*\()?MAYBE_{test_name}[\),]')
  1474. suite_maybe_pattern = r'^\s*\w*TEST[^(]*\(\s*MAYBE_{test_name}[\),]'
  1475. warnings = []
  1476. # Read the entire files. We can't just read the affected lines, forgetting to
  1477. # add MAYBE_ on a change would not show up otherwise.
  1478. for f in input_api.AffectedFiles(False):
  1479. if not 'test' in f.LocalPath() or not f.LocalPath().endswith('.cc'):
  1480. continue
  1481. contents = input_api.ReadFile(f)
  1482. lines = contents.splitlines(True)
  1483. current_position = 0
  1484. warning_test_names = set()
  1485. for line_num, line in enumerate(lines, start=1):
  1486. current_position += len(line)
  1487. maybe_match = define_maybe_pattern.search(line)
  1488. if maybe_match:
  1489. test_name = maybe_match.group('test_name')
  1490. # Do not warn twice for the same test.
  1491. if (test_name in warning_test_names):
  1492. continue
  1493. warning_test_names.add(test_name)
  1494. # Attempt to find the corresponding MAYBE_ test or suite, starting from
  1495. # the current position.
  1496. test_match = input_api.re.compile(
  1497. test_maybe_pattern.format(test_name=test_name),
  1498. input_api.re.MULTILINE).search(contents, current_position)
  1499. suite_match = input_api.re.compile(
  1500. suite_maybe_pattern.format(test_name=test_name),
  1501. input_api.re.MULTILINE).search(contents, current_position)
  1502. if not test_match and not suite_match:
  1503. warnings.append(
  1504. output_api.PresubmitPromptWarning(
  1505. '%s:%d found MAYBE_ defined without corresponding test %s'
  1506. % (f.LocalPath(), line_num, test_name)))
  1507. return warnings
  1508. def CheckDCHECK_IS_ONHasBraces(input_api, output_api):
  1509. """Checks to make sure DCHECK_IS_ON() does not skip the parentheses."""
  1510. errors = []
  1511. pattern = input_api.re.compile(r'\bDCHECK_IS_ON\b(?!\(\))',
  1512. input_api.re.MULTILINE)
  1513. for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
  1514. if (not f.LocalPath().endswith(('.cc', '.mm', '.h'))):
  1515. continue
  1516. for lnum, line in f.ChangedContents():
  1517. if input_api.re.search(pattern, line):
  1518. errors.append(
  1519. output_api.PresubmitError((
  1520. '%s:%d: Use of DCHECK_IS_ON() must be written as "#if '
  1521. + 'DCHECK_IS_ON()", not forgetting the parentheses.') %
  1522. (f.LocalPath(), lnum)))
  1523. return errors
  1524. # TODO(crbug/1138055): Reimplement CheckUmaHistogramChangesOnUpload check in a
  1525. # more reliable way. See
  1526. # https://chromium-review.googlesource.com/c/chromium/src/+/2500269
  1527. def CheckFlakyTestUsage(input_api, output_api):
  1528. """Check that FlakyTest annotation is our own instead of the android one"""
  1529. pattern = input_api.re.compile(r'import android.test.FlakyTest;')
  1530. files = []
  1531. for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
  1532. if f.LocalPath().endswith('Test.java'):
  1533. if pattern.search(input_api.ReadFile(f)):
  1534. files.append(f)
  1535. if len(files):
  1536. return [
  1537. output_api.PresubmitError(
  1538. 'Use org.chromium.base.test.util.FlakyTest instead of '
  1539. 'android.test.FlakyTest', files)
  1540. ]
  1541. return []
  1542. def CheckNoDEPSGIT(input_api, output_api):
  1543. """Make sure .DEPS.git is never modified manually."""
  1544. if any(f.LocalPath().endswith('.DEPS.git')
  1545. for f in input_api.AffectedFiles()):
  1546. return [
  1547. output_api.PresubmitError(
  1548. 'Never commit changes to .DEPS.git. This file is maintained by an\n'
  1549. 'automated system based on what\'s in DEPS and your changes will be\n'
  1550. 'overwritten.\n'
  1551. 'See https://sites.google.com/a/chromium.org/dev/developers/how-tos/'
  1552. 'get-the-code#Rolling_DEPS\n'
  1553. 'for more information')
  1554. ]
  1555. return []
  1556. def CheckValidHostsInDEPSOnUpload(input_api, output_api):
  1557. """Checks that DEPS file deps are from allowed_hosts."""
  1558. # Run only if DEPS file has been modified to annoy fewer bystanders.
  1559. if all(f.LocalPath() != 'DEPS' for f in input_api.AffectedFiles()):
  1560. return []
  1561. # Outsource work to gclient verify
  1562. try:
  1563. gclient_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
  1564. 'third_party', 'depot_tools',
  1565. 'gclient.py')
  1566. input_api.subprocess.check_output(
  1567. [input_api.python3_executable, gclient_path, 'verify'],
  1568. stderr=input_api.subprocess.STDOUT)
  1569. return []
  1570. except input_api.subprocess.CalledProcessError as error:
  1571. return [
  1572. output_api.PresubmitError(
  1573. 'DEPS file must have only git dependencies.',
  1574. long_text=error.output)
  1575. ]
  1576. def _GetMessageForMatchingType(input_api, affected_file, line_number, line,
  1577. ban_rule):
  1578. """Helper method for CheckNoBannedFunctions and CheckNoDeprecatedMojoTypes.
  1579. Returns an string composed of the name of the file, the line number where the
  1580. match has been found and the additional text passed as |message| in case the
  1581. target type name matches the text inside the line passed as parameter.
  1582. """
  1583. result = []
  1584. # Ignore comments about banned types.
  1585. if input_api.re.search(r"^ *//", line):
  1586. return result
  1587. # A // nocheck comment will bypass this error.
  1588. if line.endswith(" nocheck"):
  1589. return result
  1590. matched = False
  1591. if ban_rule.pattern[0:1] == '/':
  1592. regex = ban_rule.pattern[1:]
  1593. if input_api.re.search(regex, line):
  1594. matched = True
  1595. elif ban_rule.pattern in line:
  1596. matched = True
  1597. if matched:
  1598. result.append(' %s:%d:' % (affected_file.LocalPath(), line_number))
  1599. for line in ban_rule.explanation:
  1600. result.append(' %s' % line)
  1601. return result
  1602. def CheckNoBannedFunctions(input_api, output_api):
  1603. """Make sure that banned functions are not used."""
  1604. warnings = []
  1605. errors = []
  1606. def IsExcludedFile(affected_file, excluded_paths):
  1607. if not excluded_paths:
  1608. return False
  1609. local_path = affected_file.LocalPath()
  1610. for item in excluded_paths:
  1611. if input_api.re.match(item, local_path):
  1612. return True
  1613. return False
  1614. def IsIosObjcFile(affected_file):
  1615. local_path = affected_file.LocalPath()
  1616. if input_api.os_path.splitext(local_path)[-1] not in ('.mm', '.m',
  1617. '.h'):
  1618. return False
  1619. basename = input_api.os_path.basename(local_path)
  1620. if 'ios' in basename.split('_'):
  1621. return True
  1622. for sep in (input_api.os_path.sep, input_api.os_path.altsep):
  1623. if sep and 'ios' in local_path.split(sep):
  1624. return True
  1625. return False
  1626. def CheckForMatch(affected_file, line_num: int, line: str,
  1627. ban_rule: BanRule):
  1628. if IsExcludedFile(affected_file, ban_rule.excluded_paths):
  1629. return
  1630. problems = _GetMessageForMatchingType(input_api, f, line_num, line,
  1631. ban_rule)
  1632. if problems:
  1633. if ban_rule.treat_as_error is not None and ban_rule.treat_as_error:
  1634. errors.extend(problems)
  1635. else:
  1636. warnings.extend(problems)
  1637. file_filter = lambda f: f.LocalPath().endswith(('.java'))
  1638. for f in input_api.AffectedFiles(file_filter=file_filter):
  1639. for line_num, line in f.ChangedContents():
  1640. for ban_rule in _BANNED_JAVA_FUNCTIONS:
  1641. CheckForMatch(f, line_num, line, ban_rule)
  1642. file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
  1643. for f in input_api.AffectedFiles(file_filter=file_filter):
  1644. for line_num, line in f.ChangedContents():
  1645. for ban_rule in _BANNED_OBJC_FUNCTIONS:
  1646. CheckForMatch(f, line_num, line, ban_rule)
  1647. for f in input_api.AffectedFiles(file_filter=IsIosObjcFile):
  1648. for line_num, line in f.ChangedContents():
  1649. for ban_rule in _BANNED_IOS_OBJC_FUNCTIONS:
  1650. CheckForMatch(f, line_num, line, ban_rule)
  1651. egtest_filter = lambda f: f.LocalPath().endswith(('_egtest.mm'))
  1652. for f in input_api.AffectedFiles(file_filter=egtest_filter):
  1653. for line_num, line in f.ChangedContents():
  1654. for ban_rule in _BANNED_IOS_EGTEST_FUNCTIONS:
  1655. CheckForMatch(f, line_num, line, ban_rule)
  1656. file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
  1657. for f in input_api.AffectedFiles(file_filter=file_filter):
  1658. for line_num, line in f.ChangedContents():
  1659. for ban_rule in _BANNED_CPP_FUNCTIONS:
  1660. CheckForMatch(f, line_num, line, ban_rule)
  1661. file_filter = lambda f: f.LocalPath().endswith(('.mojom'))
  1662. for f in input_api.AffectedFiles(file_filter=file_filter):
  1663. for line_num, line in f.ChangedContents():
  1664. for ban_rule in _BANNED_MOJOM_PATTERNS:
  1665. CheckForMatch(f, line_num, line, ban_rule)
  1666. result = []
  1667. if (warnings):
  1668. result.append(
  1669. output_api.PresubmitPromptWarning('Banned functions were used.\n' +
  1670. '\n'.join(warnings)))
  1671. if (errors):
  1672. result.append(
  1673. output_api.PresubmitError('Banned functions were used.\n' +
  1674. '\n'.join(errors)))
  1675. return result
  1676. def _CheckAndroidNoBannedImports(input_api, output_api):
  1677. """Make sure that banned java imports are not used."""
  1678. errors = []
  1679. file_filter = lambda f: f.LocalPath().endswith(('.java'))
  1680. for f in input_api.AffectedFiles(file_filter=file_filter):
  1681. for line_num, line in f.ChangedContents():
  1682. for ban_rule in _BANNED_JAVA_IMPORTS:
  1683. # Consider merging this into the above function. There is no
  1684. # real difference anymore other than helping with a little
  1685. # bit of boilerplate text. Doing so means things like
  1686. # `treat_as_error` will also be uniformly handled.
  1687. problems = _GetMessageForMatchingType(input_api, f, line_num,
  1688. line, ban_rule)
  1689. if problems:
  1690. errors.extend(problems)
  1691. result = []
  1692. if (errors):
  1693. result.append(
  1694. output_api.PresubmitError('Banned imports were used.\n' +
  1695. '\n'.join(errors)))
  1696. return result
  1697. def CheckNoPragmaOnce(input_api, output_api):
  1698. """Make sure that banned functions are not used."""
  1699. files = []
  1700. pattern = input_api.re.compile(r'^#pragma\s+once', input_api.re.MULTILINE)
  1701. for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
  1702. if not f.LocalPath().endswith('.h'):
  1703. continue
  1704. if f.LocalPath().endswith('com_imported_mstscax.h'):
  1705. continue
  1706. contents = input_api.ReadFile(f)
  1707. if pattern.search(contents):
  1708. files.append(f)
  1709. if files:
  1710. return [
  1711. output_api.PresubmitError(
  1712. 'Do not use #pragma once in header files.\n'
  1713. 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
  1714. files)
  1715. ]
  1716. return []
  1717. def CheckNoTrinaryTrueFalse(input_api, output_api):
  1718. """Checks to make sure we don't introduce use of foo ? true : false."""
  1719. problems = []
  1720. pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
  1721. for f in input_api.AffectedFiles():
  1722. if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
  1723. continue
  1724. for line_num, line in f.ChangedContents():
  1725. if pattern.match(line):
  1726. problems.append(' %s:%d' % (f.LocalPath(), line_num))
  1727. if not problems:
  1728. return []
  1729. return [
  1730. output_api.PresubmitPromptWarning(
  1731. 'Please consider avoiding the "? true : false" pattern if possible.\n'
  1732. + '\n'.join(problems))
  1733. ]
  1734. def CheckUnwantedDependencies(input_api, output_api):
  1735. """Runs checkdeps on #include and import statements added in this
  1736. change. Breaking - rules is an error, breaking ! rules is a
  1737. warning.
  1738. """
  1739. # Return early if no relevant file types were modified.
  1740. for f in input_api.AffectedFiles():
  1741. path = f.LocalPath()
  1742. if (_IsCPlusPlusFile(input_api, path) or _IsProtoFile(input_api, path)
  1743. or _IsJavaFile(input_api, path)):
  1744. break
  1745. else:
  1746. return []
  1747. import sys
  1748. # We need to wait until we have an input_api object and use this
  1749. # roundabout construct to import checkdeps because this file is
  1750. # eval-ed and thus doesn't have __file__.
  1751. original_sys_path = sys.path
  1752. try:
  1753. sys.path = sys.path + [
  1754. input_api.os_path.join(input_api.PresubmitLocalPath(),
  1755. 'buildtools', 'checkdeps')
  1756. ]
  1757. import checkdeps
  1758. from rules import Rule
  1759. finally:
  1760. # Restore sys.path to what it was before.
  1761. sys.path = original_sys_path
  1762. added_includes = []
  1763. added_imports = []
  1764. added_java_imports = []
  1765. for f in input_api.AffectedFiles():
  1766. if _IsCPlusPlusFile(input_api, f.LocalPath()):
  1767. changed_lines = [line for _, line in f.ChangedContents()]
  1768. added_includes.append([f.AbsoluteLocalPath(), changed_lines])
  1769. elif _IsProtoFile(input_api, f.LocalPath()):
  1770. changed_lines = [line for _, line in f.ChangedContents()]
  1771. added_imports.append([f.AbsoluteLocalPath(), changed_lines])
  1772. elif _IsJavaFile(input_api, f.LocalPath()):
  1773. changed_lines = [line for _, line in f.ChangedContents()]
  1774. added_java_imports.append([f.AbsoluteLocalPath(), changed_lines])
  1775. deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
  1776. error_descriptions = []
  1777. warning_descriptions = []
  1778. error_subjects = set()
  1779. warning_subjects = set()
  1780. for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
  1781. added_includes):
  1782. path = input_api.os_path.relpath(path, input_api.PresubmitLocalPath())
  1783. description_with_path = '%s\n %s' % (path, rule_description)
  1784. if rule_type == Rule.DISALLOW:
  1785. error_descriptions.append(description_with_path)
  1786. error_subjects.add("#includes")
  1787. else:
  1788. warning_descriptions.append(description_with_path)
  1789. warning_subjects.add("#includes")
  1790. for path, rule_type, rule_description in deps_checker.CheckAddedProtoImports(
  1791. added_imports):
  1792. path = input_api.os_path.relpath(path, input_api.PresubmitLocalPath())
  1793. description_with_path = '%s\n %s' % (path, rule_description)
  1794. if rule_type == Rule.DISALLOW:
  1795. error_descriptions.append(description_with_path)
  1796. error_subjects.add("imports")
  1797. else:
  1798. warning_descriptions.append(description_with_path)
  1799. warning_subjects.add("imports")
  1800. for path, rule_type, rule_description in deps_checker.CheckAddedJavaImports(
  1801. added_java_imports, _JAVA_MULTIPLE_DEFINITION_EXCLUDED_PATHS):
  1802. path = input_api.os_path.relpath(path, input_api.PresubmitLocalPath())
  1803. description_with_path = '%s\n %s' % (path, rule_description)
  1804. if rule_type == Rule.DISALLOW:
  1805. error_descriptions.append(description_with_path)
  1806. error_subjects.add("imports")
  1807. else:
  1808. warning_descriptions.append(description_with_path)
  1809. warning_subjects.add("imports")
  1810. results = []
  1811. if error_descriptions:
  1812. results.append(
  1813. output_api.PresubmitError(
  1814. 'You added one or more %s that violate checkdeps rules.' %
  1815. " and ".join(error_subjects), error_descriptions))
  1816. if warning_descriptions:
  1817. results.append(
  1818. output_api.PresubmitPromptOrNotify(
  1819. 'You added one or more %s of files that are temporarily\n'
  1820. 'allowed but being removed. Can you avoid introducing the\n'
  1821. '%s? See relevant DEPS file(s) for details and contacts.' %
  1822. (" and ".join(warning_subjects), "/".join(warning_subjects)),
  1823. warning_descriptions))
  1824. return results
  1825. def CheckFilePermissions(input_api, output_api):
  1826. """Check that all files have their permissions properly set."""
  1827. if input_api.platform == 'win32':
  1828. return []
  1829. checkperms_tool = input_api.os_path.join(input_api.PresubmitLocalPath(),
  1830. 'tools', 'checkperms',
  1831. 'checkperms.py')
  1832. args = [
  1833. input_api.python3_executable, checkperms_tool, '--root',
  1834. input_api.change.RepositoryRoot()
  1835. ]
  1836. with input_api.CreateTemporaryFile() as file_list:
  1837. for f in input_api.AffectedFiles():
  1838. # checkperms.py file/directory arguments must be relative to the
  1839. # repository.
  1840. file_list.write((f.LocalPath() + '\n').encode('utf8'))
  1841. file_list.close()
  1842. args += ['--file-list', file_list.name]
  1843. try:
  1844. input_api.subprocess.check_output(args)
  1845. return []
  1846. except input_api.subprocess.CalledProcessError as error:
  1847. return [
  1848. output_api.PresubmitError('checkperms.py failed:',
  1849. long_text=error.output.decode(
  1850. 'utf-8', 'ignore'))
  1851. ]
  1852. def CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
  1853. """Makes sure we don't include ui/aura/window_property.h
  1854. in header files.
  1855. """
  1856. pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
  1857. errors = []
  1858. for f in input_api.AffectedFiles():
  1859. if not f.LocalPath().endswith('.h'):
  1860. continue
  1861. for line_num, line in f.ChangedContents():
  1862. if pattern.match(line):
  1863. errors.append(' %s:%d' % (f.LocalPath(), line_num))
  1864. results = []
  1865. if errors:
  1866. results.append(
  1867. output_api.PresubmitError(
  1868. 'Header files should not include ui/aura/window_property.h',
  1869. errors))
  1870. return results
  1871. def CheckNoInternalHeapIncludes(input_api, output_api):
  1872. """Makes sure we don't include any headers from
  1873. third_party/blink/renderer/platform/heap/impl or
  1874. third_party/blink/renderer/platform/heap/v8_wrapper from files outside of
  1875. third_party/blink/renderer/platform/heap
  1876. """
  1877. impl_pattern = input_api.re.compile(
  1878. r'^\s*#include\s*"third_party/blink/renderer/platform/heap/impl/.*"')
  1879. v8_wrapper_pattern = input_api.re.compile(
  1880. r'^\s*#include\s*"third_party/blink/renderer/platform/heap/v8_wrapper/.*"'
  1881. )
  1882. file_filter = lambda f: not input_api.re.match(
  1883. r"^third_party[\\/]blink[\\/]renderer[\\/]platform[\\/]heap[\\/].*",
  1884. f.LocalPath())
  1885. errors = []
  1886. for f in input_api.AffectedFiles(file_filter=file_filter):
  1887. for line_num, line in f.ChangedContents():
  1888. if impl_pattern.match(line) or v8_wrapper_pattern.match(line):
  1889. errors.append(' %s:%d' % (f.LocalPath(), line_num))
  1890. results = []
  1891. if errors:
  1892. results.append(
  1893. output_api.PresubmitError(
  1894. 'Do not include files from third_party/blink/renderer/platform/heap/impl'
  1895. ' or third_party/blink/renderer/platform/heap/v8_wrapper. Use the '
  1896. 'relevant counterparts from third_party/blink/renderer/platform/heap',
  1897. errors))
  1898. return results
  1899. def _CheckForVersionControlConflictsInFile(input_api, f):
  1900. pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
  1901. errors = []
  1902. for line_num, line in f.ChangedContents():
  1903. if f.LocalPath().endswith(('.md', '.rst', '.txt')):
  1904. # First-level headers in markdown look a lot like version control
  1905. # conflict markers. http://daringfireball.net/projects/markdown/basics
  1906. continue
  1907. if pattern.match(line):
  1908. errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
  1909. return errors
  1910. def CheckForVersionControlConflicts(input_api, output_api):
  1911. """Usually this is not intentional and will cause a compile failure."""
  1912. errors = []
  1913. for f in input_api.AffectedFiles():
  1914. errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
  1915. results = []
  1916. if errors:
  1917. results.append(
  1918. output_api.PresubmitError(
  1919. 'Version control conflict markers found, please resolve.',
  1920. errors))
  1921. return results
  1922. def CheckGoogleSupportAnswerUrlOnUpload(input_api, output_api):
  1923. pattern = input_api.re.compile('support\.google\.com\/chrome.*/answer')
  1924. errors = []
  1925. for f in input_api.AffectedFiles():
  1926. for line_num, line in f.ChangedContents():
  1927. if pattern.search(line):
  1928. errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
  1929. results = []
  1930. if errors:
  1931. results.append(
  1932. output_api.PresubmitPromptWarning(
  1933. 'Found Google support URL addressed by answer number. Please replace '
  1934. 'with a p= identifier instead. See crbug.com/679462\n',
  1935. errors))
  1936. return results
  1937. def CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
  1938. def FilterFile(affected_file):
  1939. """Filter function for use with input_api.AffectedSourceFiles,
  1940. below. This filters out everything except non-test files from
  1941. top-level directories that generally speaking should not hard-code
  1942. service URLs (e.g. src/android_webview/, src/content/ and others).
  1943. """
  1944. return input_api.FilterSourceFile(
  1945. affected_file,
  1946. files_to_check=[r'^(android_webview|base|content|net)[\\/].*'],
  1947. files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS +
  1948. input_api.DEFAULT_FILES_TO_SKIP))
  1949. base_pattern = ('"[^"]*(google|googleapis|googlezip|googledrive|appspot)'
  1950. '\.(com|net)[^"]*"')
  1951. comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
  1952. pattern = input_api.re.compile(base_pattern)
  1953. problems = [] # items are (filename, line_number, line)
  1954. for f in input_api.AffectedSourceFiles(FilterFile):
  1955. for line_num, line in f.ChangedContents():
  1956. if not comment_pattern.search(line) and pattern.search(line):
  1957. problems.append((f.LocalPath(), line_num, line))
  1958. if problems:
  1959. return [
  1960. output_api.PresubmitPromptOrNotify(
  1961. 'Most layers below src/chrome/ should not hardcode service URLs.\n'
  1962. 'Are you sure this is correct?', [
  1963. ' %s:%d: %s' % (problem[0], problem[1], problem[2])
  1964. for problem in problems
  1965. ])
  1966. ]
  1967. else:
  1968. return []
  1969. def CheckChromeOsSyncedPrefRegistration(input_api, output_api):
  1970. """Warns if Chrome OS C++ files register syncable prefs as browser prefs."""
  1971. def FileFilter(affected_file):
  1972. """Includes directories known to be Chrome OS only."""
  1973. return input_api.FilterSourceFile(
  1974. affected_file,
  1975. files_to_check=(
  1976. '^ash/',
  1977. '^chromeos/', # Top-level src/chromeos.
  1978. '.*/chromeos/', # Any path component.
  1979. '^components/arc',
  1980. '^components/exo'),
  1981. files_to_skip=(input_api.DEFAULT_FILES_TO_SKIP))
  1982. prefs = []
  1983. priority_prefs = []
  1984. for f in input_api.AffectedFiles(file_filter=FileFilter):
  1985. for line_num, line in f.ChangedContents():
  1986. if input_api.re.search('PrefRegistrySyncable::SYNCABLE_PREF',
  1987. line):
  1988. prefs.append(' %s:%d:' % (f.LocalPath(), line_num))
  1989. prefs.append(' %s' % line)
  1990. if input_api.re.search(
  1991. 'PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF', line):
  1992. priority_prefs.append(' %s:%d' % (f.LocalPath(), line_num))
  1993. priority_prefs.append(' %s' % line)
  1994. results = []
  1995. if (prefs):
  1996. results.append(
  1997. output_api.PresubmitPromptWarning(
  1998. 'Preferences were registered as SYNCABLE_PREF and will be controlled '
  1999. 'by browser sync settings. If these prefs should be controlled by OS '
  2000. 'sync settings use SYNCABLE_OS_PREF instead.\n' +
  2001. '\n'.join(prefs)))
  2002. if (priority_prefs):
  2003. results.append(
  2004. output_api.PresubmitPromptWarning(
  2005. 'Preferences were registered as SYNCABLE_PRIORITY_PREF and will be '
  2006. 'controlled by browser sync settings. If these prefs should be '
  2007. 'controlled by OS sync settings use SYNCABLE_OS_PRIORITY_PREF '
  2008. 'instead.\n' + '\n'.join(prefs)))
  2009. return results
  2010. # TODO: add unit tests.
  2011. def CheckNoAbbreviationInPngFileName(input_api, output_api):
  2012. """Makes sure there are no abbreviations in the name of PNG files.
  2013. The native_client_sdk directory is excluded because it has auto-generated PNG
  2014. files for documentation.
  2015. """
  2016. errors = []
  2017. files_to_check = [r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$']
  2018. files_to_skip = [r'^native_client_sdk[\\/]',
  2019. r'^services[\\/]test[\\/]',
  2020. r'^third_party[\\/]blink[\\/]web_tests[\\/]',
  2021. ]
  2022. file_filter = lambda f: input_api.FilterSourceFile(
  2023. f, files_to_check=files_to_check, files_to_skip=files_to_skip)
  2024. for f in input_api.AffectedFiles(include_deletes=False,
  2025. file_filter=file_filter):
  2026. errors.append(' %s' % f.LocalPath())
  2027. results = []
  2028. if errors:
  2029. results.append(
  2030. output_api.PresubmitError(
  2031. 'The name of PNG files should not have abbreviations. \n'
  2032. 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
  2033. 'Contact oshima@chromium.org if you have questions.', errors))
  2034. return results
  2035. def CheckNoProductIconsAddedToPublicRepo(input_api, output_api):
  2036. """Heuristically identifies product icons based on their file name and reminds
  2037. contributors not to add them to the Chromium repository.
  2038. """
  2039. errors = []
  2040. files_to_check = [r'.*google.*\.png$|.*google.*\.svg$|.*google.*\.icon$']
  2041. file_filter = lambda f: input_api.FilterSourceFile(
  2042. f, files_to_check=files_to_check)
  2043. for f in input_api.AffectedFiles(include_deletes=False,
  2044. file_filter=file_filter):
  2045. errors.append(' %s' % f.LocalPath())
  2046. results = []
  2047. if errors:
  2048. # Give warnings instead of errors on presubmit --all and presubmit
  2049. # --files.
  2050. message_type = (output_api.PresubmitNotifyResult if input_api.no_diffs
  2051. else output_api.PresubmitError)
  2052. results.append(
  2053. message_type(
  2054. 'Trademarked images should not be added to the public repo. '
  2055. 'See crbug.com/944754', errors))
  2056. return results
  2057. def _ExtractAddRulesFromParsedDeps(parsed_deps):
  2058. """Extract the rules that add dependencies from a parsed DEPS file.
  2059. Args:
  2060. parsed_deps: the locals dictionary from evaluating the DEPS file."""
  2061. add_rules = set()
  2062. add_rules.update([
  2063. rule[1:] for rule in parsed_deps.get('include_rules', [])
  2064. if rule.startswith('+') or rule.startswith('!')
  2065. ])
  2066. for _, rules in parsed_deps.get('specific_include_rules', {}).items():
  2067. add_rules.update([
  2068. rule[1:] for rule in rules
  2069. if rule.startswith('+') or rule.startswith('!')
  2070. ])
  2071. return add_rules
  2072. def _ParseDeps(contents):
  2073. """Simple helper for parsing DEPS files."""
  2074. # Stubs for handling special syntax in the root DEPS file.
  2075. class _VarImpl:
  2076. def __init__(self, local_scope):
  2077. self._local_scope = local_scope
  2078. def Lookup(self, var_name):
  2079. """Implements the Var syntax."""
  2080. try:
  2081. return self._local_scope['vars'][var_name]
  2082. except KeyError:
  2083. raise Exception('Var is not defined: %s' % var_name)
  2084. local_scope = {}
  2085. global_scope = {
  2086. 'Var': _VarImpl(local_scope).Lookup,
  2087. 'Str': str,
  2088. }
  2089. exec(contents, global_scope, local_scope)
  2090. return local_scope
  2091. def _CalculateAddedDeps(os_path, old_contents, new_contents):
  2092. """Helper method for CheckAddedDepsHaveTargetApprovals. Returns
  2093. a set of DEPS entries that we should look up.
  2094. For a directory (rather than a specific filename) we fake a path to
  2095. a specific filename by adding /DEPS. This is chosen as a file that
  2096. will seldom or never be subject to per-file include_rules.
  2097. """
  2098. # We ignore deps entries on auto-generated directories.
  2099. AUTO_GENERATED_DIRS = ['grit', 'jni']
  2100. old_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(old_contents))
  2101. new_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(new_contents))
  2102. added_deps = new_deps.difference(old_deps)
  2103. results = set()
  2104. for added_dep in added_deps:
  2105. if added_dep.split('/')[0] in AUTO_GENERATED_DIRS:
  2106. continue
  2107. # Assume that a rule that ends in .h is a rule for a specific file.
  2108. if added_dep.endswith('.h'):
  2109. results.add(added_dep)
  2110. else:
  2111. results.add(os_path.join(added_dep, 'DEPS'))
  2112. return results
  2113. def CheckAddedDepsHaveTargetApprovals(input_api, output_api):
  2114. """When a dependency prefixed with + is added to a DEPS file, we
  2115. want to make sure that the change is reviewed by an OWNER of the
  2116. target file or directory, to avoid layering violations from being
  2117. introduced. This check verifies that this happens.
  2118. """
  2119. # We rely on Gerrit's code-owners to check approvals.
  2120. # input_api.gerrit is always set for Chromium, but other projects
  2121. # might not use Gerrit.
  2122. if not input_api.gerrit or input_api.no_diffs:
  2123. return []
  2124. if 'PRESUBMIT_SKIP_NETWORK' in input_api.environ:
  2125. return []
  2126. try:
  2127. if (input_api.change.issue and
  2128. input_api.gerrit.IsOwnersOverrideApproved(
  2129. input_api.change.issue)):
  2130. # Skip OWNERS check when Owners-Override label is approved. This is
  2131. # intended for global owners, trusted bots, and on-call sheriffs.
  2132. # Review is still required for these changes.
  2133. return []
  2134. except Exception as e:
  2135. return [output_api.PresubmitPromptWarning(
  2136. 'Failed to retrieve owner override status - %s' % str(e))]
  2137. virtual_depended_on_files = set()
  2138. file_filter = lambda f: not input_api.re.match(
  2139. r"^third_party[\\/]blink[\\/].*", f.LocalPath())
  2140. for f in input_api.AffectedFiles(include_deletes=False,
  2141. file_filter=file_filter):
  2142. filename = input_api.os_path.basename(f.LocalPath())
  2143. if filename == 'DEPS':
  2144. virtual_depended_on_files.update(
  2145. _CalculateAddedDeps(input_api.os_path,
  2146. '\n'.join(f.OldContents()),
  2147. '\n'.join(f.NewContents())))
  2148. if not virtual_depended_on_files:
  2149. return []
  2150. if input_api.is_committing:
  2151. if input_api.tbr:
  2152. return [
  2153. output_api.PresubmitNotifyResult(
  2154. '--tbr was specified, skipping OWNERS check for DEPS additions'
  2155. )
  2156. ]
  2157. # TODO(dcheng): Make this generate an error on dry runs if the reviewer
  2158. # is not added, to prevent review serialization.
  2159. if input_api.dry_run:
  2160. return [
  2161. output_api.PresubmitNotifyResult(
  2162. 'This is a dry run, skipping OWNERS check for DEPS additions'
  2163. )
  2164. ]
  2165. if not input_api.change.issue:
  2166. return [
  2167. output_api.PresubmitError(
  2168. "DEPS approval by OWNERS check failed: this change has "
  2169. "no change number, so we can't check it for approvals.")
  2170. ]
  2171. output = output_api.PresubmitError
  2172. else:
  2173. output = output_api.PresubmitNotifyResult
  2174. owner_email, reviewers = (
  2175. input_api.canned_checks.GetCodereviewOwnerAndReviewers(
  2176. input_api, None, approval_needed=input_api.is_committing))
  2177. owner_email = owner_email or input_api.change.author_email
  2178. approval_status = input_api.owners_client.GetFilesApprovalStatus(
  2179. virtual_depended_on_files, reviewers.union([owner_email]), [])
  2180. missing_files = [
  2181. f for f in virtual_depended_on_files
  2182. if approval_status[f] != input_api.owners_client.APPROVED
  2183. ]
  2184. # We strip the /DEPS part that was added by
  2185. # _FilesToCheckForIncomingDeps to fake a path to a file in a
  2186. # directory.
  2187. def StripDeps(path):
  2188. start_deps = path.rfind('/DEPS')
  2189. if start_deps != -1:
  2190. return path[:start_deps]
  2191. else:
  2192. return path
  2193. unapproved_dependencies = [
  2194. "'+%s'," % StripDeps(path) for path in missing_files
  2195. ]
  2196. if unapproved_dependencies:
  2197. output_list = [
  2198. output(
  2199. 'You need LGTM from owners of depends-on paths in DEPS that were '
  2200. 'modified in this CL:\n %s' %
  2201. '\n '.join(sorted(unapproved_dependencies)))
  2202. ]
  2203. suggested_owners = input_api.owners_client.SuggestOwners(
  2204. missing_files, exclude=[owner_email])
  2205. output_list.append(
  2206. output('Suggested missing target path OWNERS:\n %s' %
  2207. '\n '.join(suggested_owners or [])))
  2208. return output_list
  2209. return []
  2210. # TODO: add unit tests.
  2211. def CheckSpamLogging(input_api, output_api):
  2212. file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS]
  2213. files_to_skip = (
  2214. _EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS +
  2215. input_api.DEFAULT_FILES_TO_SKIP + (
  2216. r"^base[\\/]logging\.h$",
  2217. r"^base[\\/]logging\.cc$",
  2218. r"^base[\\/]task[\\/]thread_pool[\\/]task_tracker\.cc$",
  2219. r"^chrome[\\/]app[\\/]chrome_main_delegate\.cc$",
  2220. r"^chrome[\\/]browser[\\/]chrome_browser_main\.cc$",
  2221. r"^chrome[\\/]browser[\\/]ui[\\/]startup[\\/]"
  2222. r"startup_browser_creator\.cc$",
  2223. r"^chrome[\\/]browser[\\/]browser_switcher[\\/]bho[\\/].*",
  2224. r"^chrome[\\/]browser[\\/]diagnostics[\\/]" +
  2225. r"diagnostics_writer\.cc$",
  2226. r"^chrome[\\/]chrome_cleaner[\\/].*",
  2227. r"^chrome[\\/]chrome_elf[\\/]dll_hash[\\/]" +
  2228. r"dll_hash_main\.cc$",
  2229. r"^chrome[\\/]installer[\\/]setup[\\/].*",
  2230. r"^chromecast[\\/]",
  2231. r"^components[\\/]browser_watcher[\\/]"
  2232. r"dump_stability_report_main_win\.cc$",
  2233. r"^components[\\/]media_control[\\/]renderer[\\/]"
  2234. r"media_playback_options\.cc$",
  2235. r"^components[\\/]viz[\\/]service[\\/]display[\\/]"
  2236. r"overlay_strategy_underlay_cast\.cc$",
  2237. r"^components[\\/]zucchini[\\/].*",
  2238. # TODO(peter): Remove exception. https://crbug.com/534537
  2239. r"^content[\\/]browser[\\/]notifications[\\/]"
  2240. r"notification_event_dispatcher_impl\.cc$",
  2241. r"^content[\\/]common[\\/]gpu[\\/]client[\\/]"
  2242. r"gl_helper_benchmark\.cc$",
  2243. r"^courgette[\\/]courgette_minimal_tool\.cc$",
  2244. r"^courgette[\\/]courgette_tool\.cc$",
  2245. r"^extensions[\\/]renderer[\\/]logging_native_handler\.cc$",
  2246. r"^fuchsia_web[\\/]common[\\/]init_logging\.cc$",
  2247. r"^fuchsia_web[\\/]runners[\\/]common[\\/]web_component\.cc$",
  2248. r"^fuchsia_web[\\/]shell[\\/].*_shell\.cc$",
  2249. r"^headless[\\/]app[\\/]headless_shell\.cc$",
  2250. r"^ipc[\\/]ipc_logging\.cc$",
  2251. r"^native_client_sdk[\\/]",
  2252. r"^remoting[\\/]base[\\/]logging\.h$",
  2253. r"^remoting[\\/]host[\\/].*",
  2254. r"^sandbox[\\/]linux[\\/].*",
  2255. r"^storage[\\/]browser[\\/]file_system[\\/]" +
  2256. r"dump_file_system\.cc$",
  2257. r"^tools[\\/]",
  2258. r"^ui[\\/]base[\\/]resource[\\/]data_pack\.cc$",
  2259. r"^ui[\\/]aura[\\/]bench[\\/]bench_main\.cc$",
  2260. r"^ui[\\/]ozone[\\/]platform[\\/]cast[\\/]",
  2261. r"^ui[\\/]base[\\/]x[\\/]xwmstartupcheck[\\/]"
  2262. r"xwmstartupcheck\.cc$"))
  2263. source_file_filter = lambda x: input_api.FilterSourceFile(
  2264. x, files_to_check=file_inclusion_pattern, files_to_skip=files_to_skip)
  2265. log_info = set([])
  2266. printf = set([])
  2267. for f in input_api.AffectedSourceFiles(source_file_filter):
  2268. for _, line in f.ChangedContents():
  2269. if input_api.re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", line):
  2270. log_info.add(f.LocalPath())
  2271. elif input_api.re.search(r"\bD?LOG_IF\s*\(\s*INFO\s*,", line):
  2272. log_info.add(f.LocalPath())
  2273. if input_api.re.search(r"\bprintf\(", line):
  2274. printf.add(f.LocalPath())
  2275. elif input_api.re.search(r"\bfprintf\((stdout|stderr)", line):
  2276. printf.add(f.LocalPath())
  2277. if log_info:
  2278. return [
  2279. output_api.PresubmitError(
  2280. 'These files spam the console log with LOG(INFO):',
  2281. items=log_info)
  2282. ]
  2283. if printf:
  2284. return [
  2285. output_api.PresubmitError(
  2286. 'These files spam the console log with printf/fprintf:',
  2287. items=printf)
  2288. ]
  2289. return []
  2290. def CheckForAnonymousVariables(input_api, output_api):
  2291. """These types are all expected to hold locks while in scope and
  2292. so should never be anonymous (which causes them to be immediately
  2293. destroyed)."""
  2294. they_who_must_be_named = [
  2295. 'base::AutoLock',
  2296. 'base::AutoReset',
  2297. 'base::AutoUnlock',
  2298. 'SkAutoAlphaRestore',
  2299. 'SkAutoBitmapShaderInstall',
  2300. 'SkAutoBlitterChoose',
  2301. 'SkAutoBounderCommit',
  2302. 'SkAutoCallProc',
  2303. 'SkAutoCanvasRestore',
  2304. 'SkAutoCommentBlock',
  2305. 'SkAutoDescriptor',
  2306. 'SkAutoDisableDirectionCheck',
  2307. 'SkAutoDisableOvalCheck',
  2308. 'SkAutoFree',
  2309. 'SkAutoGlyphCache',
  2310. 'SkAutoHDC',
  2311. 'SkAutoLockColors',
  2312. 'SkAutoLockPixels',
  2313. 'SkAutoMalloc',
  2314. 'SkAutoMaskFreeImage',
  2315. 'SkAutoMutexAcquire',
  2316. 'SkAutoPathBoundsUpdate',
  2317. 'SkAutoPDFRelease',
  2318. 'SkAutoRasterClipValidate',
  2319. 'SkAutoRef',
  2320. 'SkAutoTime',
  2321. 'SkAutoTrace',
  2322. 'SkAutoUnref',
  2323. ]
  2324. anonymous = r'(%s)\s*[({]' % '|'.join(they_who_must_be_named)
  2325. # bad: base::AutoLock(lock.get());
  2326. # not bad: base::AutoLock lock(lock.get());
  2327. bad_pattern = input_api.re.compile(anonymous)
  2328. # good: new base::AutoLock(lock.get())
  2329. good_pattern = input_api.re.compile(r'\bnew\s*' + anonymous)
  2330. errors = []
  2331. for f in input_api.AffectedFiles():
  2332. if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
  2333. continue
  2334. for linenum, line in f.ChangedContents():
  2335. if bad_pattern.search(line) and not good_pattern.search(line):
  2336. errors.append('%s:%d' % (f.LocalPath(), linenum))
  2337. if errors:
  2338. return [
  2339. output_api.PresubmitError(
  2340. 'These lines create anonymous variables that need to be named:',
  2341. items=errors)
  2342. ]
  2343. return []
  2344. def CheckUniquePtrOnUpload(input_api, output_api):
  2345. # Returns whether |template_str| is of the form <T, U...> for some types T
  2346. # and U. Assumes that |template_str| is already in the form <...>.
  2347. def HasMoreThanOneArg(template_str):
  2348. # Level of <...> nesting.
  2349. nesting = 0
  2350. for c in template_str:
  2351. if c == '<':
  2352. nesting += 1
  2353. elif c == '>':
  2354. nesting -= 1
  2355. elif c == ',' and nesting == 1:
  2356. return True
  2357. return False
  2358. file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS]
  2359. sources = lambda affected_file: input_api.FilterSourceFile(
  2360. affected_file,
  2361. files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api.
  2362. DEFAULT_FILES_TO_SKIP),
  2363. files_to_check=file_inclusion_pattern)
  2364. # Pattern to capture a single "<...>" block of template arguments. It can
  2365. # handle linearly nested blocks, such as "<std::vector<std::set<T>>>", but
  2366. # cannot handle branching structures, such as "<pair<set<T>,set<U>>". The
  2367. # latter would likely require counting that < and > match, which is not
  2368. # expressible in regular languages. Should the need arise, one can introduce
  2369. # limited counting (matching up to a total number of nesting depth), which
  2370. # should cover all practical cases for already a low nesting limit.
  2371. template_arg_pattern = (
  2372. r'<[^>]*' # Opening block of <.
  2373. r'>([^<]*>)?') # Closing block of >.
  2374. # Prefix expressing that whatever follows is not already inside a <...>
  2375. # block.
  2376. not_inside_template_arg_pattern = r'(^|[^<,\s]\s*)'
  2377. null_construct_pattern = input_api.re.compile(
  2378. not_inside_template_arg_pattern + r'\bstd::unique_ptr' +
  2379. template_arg_pattern + r'\(\)')
  2380. # Same as template_arg_pattern, but excluding type arrays, e.g., <T[]>.
  2381. template_arg_no_array_pattern = (
  2382. r'<[^>]*[^]]' # Opening block of <.
  2383. r'>([^(<]*[^]]>)?') # Closing block of >.
  2384. # Prefix saying that what follows is the start of an expression.
  2385. start_of_expr_pattern = r'(=|\breturn|^)\s*'
  2386. # Suffix saying that what follows are call parentheses with a non-empty list
  2387. # of arguments.
  2388. nonempty_arg_list_pattern = r'\(([^)]|$)'
  2389. # Put the template argument into a capture group for deeper examination later.
  2390. return_construct_pattern = input_api.re.compile(
  2391. start_of_expr_pattern + r'std::unique_ptr' + '(?P<template_arg>' +
  2392. template_arg_no_array_pattern + ')' + nonempty_arg_list_pattern)
  2393. problems_constructor = []
  2394. problems_nullptr = []
  2395. for f in input_api.AffectedSourceFiles(sources):
  2396. for line_number, line in f.ChangedContents():
  2397. # Disallow:
  2398. # return std::unique_ptr<T>(foo);
  2399. # bar = std::unique_ptr<T>(foo);
  2400. # But allow:
  2401. # return std::unique_ptr<T[]>(foo);
  2402. # bar = std::unique_ptr<T[]>(foo);
  2403. # And also allow cases when the second template argument is present. Those
  2404. # cases cannot be handled by std::make_unique:
  2405. # return std::unique_ptr<T, U>(foo);
  2406. # bar = std::unique_ptr<T, U>(foo);
  2407. local_path = f.LocalPath()
  2408. return_construct_result = return_construct_pattern.search(line)
  2409. if return_construct_result and not HasMoreThanOneArg(
  2410. return_construct_result.group('template_arg')):
  2411. problems_constructor.append(
  2412. '%s:%d\n %s' % (local_path, line_number, line.strip()))
  2413. # Disallow:
  2414. # std::unique_ptr<T>()
  2415. if null_construct_pattern.search(line):
  2416. problems_nullptr.append(
  2417. '%s:%d\n %s' % (local_path, line_number, line.strip()))
  2418. errors = []
  2419. if problems_nullptr:
  2420. errors.append(
  2421. output_api.PresubmitPromptWarning(
  2422. 'The following files use std::unique_ptr<T>(). Use nullptr instead.',
  2423. problems_nullptr))
  2424. if problems_constructor:
  2425. errors.append(
  2426. output_api.PresubmitError(
  2427. 'The following files use explicit std::unique_ptr constructor. '
  2428. 'Use std::make_unique<T>() instead, or use base::WrapUnique if '
  2429. 'std::make_unique is not an option.', problems_constructor))
  2430. return errors
  2431. def CheckUserActionUpdate(input_api, output_api):
  2432. """Checks if any new user action has been added."""
  2433. if any('actions.xml' == input_api.os_path.basename(f)
  2434. for f in input_api.LocalPaths()):
  2435. # If actions.xml is already included in the changelist, the PRESUBMIT
  2436. # for actions.xml will do a more complete presubmit check.
  2437. return []
  2438. file_inclusion_pattern = [r'.*\.(cc|mm)$']
  2439. files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS +
  2440. input_api.DEFAULT_FILES_TO_SKIP)
  2441. file_filter = lambda f: input_api.FilterSourceFile(
  2442. f, files_to_check=file_inclusion_pattern, files_to_skip=files_to_skip)
  2443. action_re = r'[^a-zA-Z]UserMetricsAction\("([^"]*)'
  2444. current_actions = None
  2445. for f in input_api.AffectedFiles(file_filter=file_filter):
  2446. for line_num, line in f.ChangedContents():
  2447. match = input_api.re.search(action_re, line)
  2448. if match:
  2449. # Loads contents in tools/metrics/actions/actions.xml to memory. It's
  2450. # loaded only once.
  2451. if not current_actions:
  2452. with open(
  2453. 'tools/metrics/actions/actions.xml') as actions_f:
  2454. current_actions = actions_f.read()
  2455. # Search for the matched user action name in |current_actions|.
  2456. for action_name in match.groups():
  2457. action = 'name="{0}"'.format(action_name)
  2458. if action not in current_actions:
  2459. return [
  2460. output_api.PresubmitPromptWarning(
  2461. 'File %s line %d: %s is missing in '
  2462. 'tools/metrics/actions/actions.xml. Please run '
  2463. 'tools/metrics/actions/extract_actions.py to update.'
  2464. % (f.LocalPath(), line_num, action_name))
  2465. ]
  2466. return []
  2467. def _ImportJSONCommentEater(input_api):
  2468. import sys
  2469. sys.path = sys.path + [
  2470. input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools',
  2471. 'json_comment_eater')
  2472. ]
  2473. import json_comment_eater
  2474. return json_comment_eater
  2475. def _GetJSONParseError(input_api, filename, eat_comments=True):
  2476. try:
  2477. contents = input_api.ReadFile(filename)
  2478. if eat_comments:
  2479. json_comment_eater = _ImportJSONCommentEater(input_api)
  2480. contents = json_comment_eater.Nom(contents)
  2481. input_api.json.loads(contents)
  2482. except ValueError as e:
  2483. return e
  2484. return None
  2485. def _GetIDLParseError(input_api, filename):
  2486. try:
  2487. contents = input_api.ReadFile(filename)
  2488. for i, char in enumerate(contents):
  2489. if not char.isascii():
  2490. return (
  2491. 'Non-ascii character "%s" (ord %d) found at offset %d.' %
  2492. (char, ord(char), i))
  2493. idl_schema = input_api.os_path.join(input_api.PresubmitLocalPath(),
  2494. 'tools', 'json_schema_compiler',
  2495. 'idl_schema.py')
  2496. process = input_api.subprocess.Popen(
  2497. [input_api.python3_executable, idl_schema],
  2498. stdin=input_api.subprocess.PIPE,
  2499. stdout=input_api.subprocess.PIPE,
  2500. stderr=input_api.subprocess.PIPE,
  2501. universal_newlines=True)
  2502. (_, error) = process.communicate(input=contents)
  2503. return error or None
  2504. except ValueError as e:
  2505. return e
  2506. def CheckParseErrors(input_api, output_api):
  2507. """Check that IDL and JSON files do not contain syntax errors."""
  2508. actions = {
  2509. '.idl': _GetIDLParseError,
  2510. '.json': _GetJSONParseError,
  2511. }
  2512. # Most JSON files are preprocessed and support comments, but these do not.
  2513. json_no_comments_patterns = [
  2514. r'^testing[\\/]',
  2515. ]
  2516. # Only run IDL checker on files in these directories.
  2517. idl_included_patterns = [
  2518. r'^chrome[\\/]common[\\/]extensions[\\/]api[\\/]',
  2519. r'^extensions[\\/]common[\\/]api[\\/]',
  2520. ]
  2521. def get_action(affected_file):
  2522. filename = affected_file.LocalPath()
  2523. return actions.get(input_api.os_path.splitext(filename)[1])
  2524. def FilterFile(affected_file):
  2525. action = get_action(affected_file)
  2526. if not action:
  2527. return False
  2528. path = affected_file.LocalPath()
  2529. if _MatchesFile(input_api,
  2530. _KNOWN_TEST_DATA_AND_INVALID_JSON_FILE_PATTERNS, path):
  2531. return False
  2532. if (action == _GetIDLParseError
  2533. and not _MatchesFile(input_api, idl_included_patterns, path)):
  2534. return False
  2535. return True
  2536. results = []
  2537. for affected_file in input_api.AffectedFiles(file_filter=FilterFile,
  2538. include_deletes=False):
  2539. action = get_action(affected_file)
  2540. kwargs = {}
  2541. if (action == _GetJSONParseError
  2542. and _MatchesFile(input_api, json_no_comments_patterns,
  2543. affected_file.LocalPath())):
  2544. kwargs['eat_comments'] = False
  2545. parse_error = action(input_api, affected_file.AbsoluteLocalPath(),
  2546. **kwargs)
  2547. if parse_error:
  2548. results.append(
  2549. output_api.PresubmitError(
  2550. '%s could not be parsed: %s' %
  2551. (affected_file.LocalPath(), parse_error)))
  2552. return results
  2553. def CheckJavaStyle(input_api, output_api):
  2554. """Runs checkstyle on changed java files and returns errors if any exist."""
  2555. # Return early if no java files were modified.
  2556. if not any(
  2557. _IsJavaFile(input_api, f.LocalPath())
  2558. for f in input_api.AffectedFiles()):
  2559. return []
  2560. import sys
  2561. original_sys_path = sys.path
  2562. try:
  2563. sys.path = sys.path + [
  2564. input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools',
  2565. 'android', 'checkstyle')
  2566. ]
  2567. import checkstyle
  2568. finally:
  2569. # Restore sys.path to what it was before.
  2570. sys.path = original_sys_path
  2571. return checkstyle.RunCheckstyle(
  2572. input_api,
  2573. output_api,
  2574. 'tools/android/checkstyle/chromium-style-5.0.xml',
  2575. files_to_skip=_EXCLUDED_PATHS + input_api.DEFAULT_FILES_TO_SKIP)
  2576. def CheckPythonDevilInit(input_api, output_api):
  2577. """Checks to make sure devil is initialized correctly in python scripts."""
  2578. script_common_initialize_pattern = input_api.re.compile(
  2579. r'script_common\.InitializeEnvironment\(')
  2580. devil_env_config_initialize = input_api.re.compile(
  2581. r'devil_env\.config\.Initialize\(')
  2582. errors = []
  2583. sources = lambda affected_file: input_api.FilterSourceFile(
  2584. affected_file,
  2585. files_to_skip=(_EXCLUDED_PATHS + input_api.DEFAULT_FILES_TO_SKIP + (
  2586. r'^build[\\/]android[\\/]devil_chromium\.py',
  2587. r'^third_party[\\/].*',
  2588. )),
  2589. files_to_check=[r'.*\.py$'])
  2590. for f in input_api.AffectedSourceFiles(sources):
  2591. for line_num, line in f.ChangedContents():
  2592. if (script_common_initialize_pattern.search(line)
  2593. or devil_env_config_initialize.search(line)):
  2594. errors.append("%s:%d" % (f.LocalPath(), line_num))
  2595. results = []
  2596. if errors:
  2597. results.append(
  2598. output_api.PresubmitError(
  2599. 'Devil initialization should always be done using '
  2600. 'devil_chromium.Initialize() in the chromium project, to use better '
  2601. 'defaults for dependencies (ex. up-to-date version of adb).',
  2602. errors))
  2603. return results
  2604. def _MatchesFile(input_api, patterns, path):
  2605. for pattern in patterns:
  2606. if input_api.re.search(pattern, path):
  2607. return True
  2608. return False
  2609. def _ChangeHasSecurityReviewer(input_api, owners_file):
  2610. """Returns True iff the CL has a reviewer from SECURITY_OWNERS.
  2611. Args:
  2612. input_api: The presubmit input API.
  2613. owners_file: OWNERS file with required reviewers. Typically, this is
  2614. something like ipc/SECURITY_OWNERS.
  2615. Note: if the presubmit is running for commit rather than for upload, this
  2616. only returns True if a security reviewer has also approved the CL.
  2617. """
  2618. # Owners-Override should bypass all additional OWNERS enforcement checks.
  2619. # A CR+1 vote will still be required to land this change.
  2620. if (input_api.change.issue and input_api.gerrit.IsOwnersOverrideApproved(
  2621. input_api.change.issue)):
  2622. return True
  2623. owner_email, reviewers = (
  2624. input_api.canned_checks.GetCodereviewOwnerAndReviewers(
  2625. input_api,
  2626. None,
  2627. approval_needed=input_api.is_committing and not input_api.dry_run))
  2628. security_owners = input_api.owners_client.ListOwners(owners_file)
  2629. return any(owner in reviewers for owner in security_owners)
  2630. @dataclass
  2631. class _SecurityProblemWithItems:
  2632. problem: str
  2633. items: Sequence[str]
  2634. @dataclass
  2635. class _MissingSecurityOwnersResult:
  2636. owners_file_problems: Sequence[_SecurityProblemWithItems]
  2637. has_security_sensitive_files: bool
  2638. missing_reviewer_problem: Optional[_SecurityProblemWithItems]
  2639. def _FindMissingSecurityOwners(input_api,
  2640. output_api,
  2641. file_patterns: Sequence[str],
  2642. excluded_patterns: Sequence[str],
  2643. required_owners_file: str,
  2644. custom_rule_function: Optional[Callable] = None
  2645. ) -> _MissingSecurityOwnersResult:
  2646. """Find OWNERS files missing per-file rules for security-sensitive files.
  2647. Args:
  2648. input_api: the PRESUBMIT input API object.
  2649. output_api: the PRESUBMIT output API object.
  2650. file_patterns: basename patterns that require a corresponding per-file
  2651. security restriction.
  2652. excluded_patterns: path patterns that should be exempted from
  2653. requiring a security restriction.
  2654. required_owners_file: path to the required OWNERS file, e.g.
  2655. ipc/SECURITY_OWNERS
  2656. cc_alias: If not None, email that will be CCed automatically if the
  2657. change contains security-sensitive files, as determined by
  2658. `file_patterns` and `excluded_patterns`.
  2659. custom_rule_function: If not None, will be called with `input_api` and
  2660. the current file under consideration. Returning True will add an
  2661. exact match per-file rule check for the current file.
  2662. """
  2663. # `to_check` is a mapping of an OWNERS file path to Patterns.
  2664. #
  2665. # Patterns is a dictionary mapping glob patterns (suitable for use in
  2666. # per-file rules) to a PatternEntry.
  2667. #
  2668. # PatternEntry is a dictionary with two keys:
  2669. # - 'files': the files that are matched by this pattern
  2670. # - 'rules': the per-file rules needed for this pattern
  2671. #
  2672. # For example, if we expect OWNERS file to contain rules for *.mojom and
  2673. # *_struct_traits*.*, Patterns might look like this:
  2674. # {
  2675. # '*.mojom': {
  2676. # 'files': ...,
  2677. # 'rules': [
  2678. # 'per-file *.mojom=set noparent',
  2679. # 'per-file *.mojom=file://ipc/SECURITY_OWNERS',
  2680. # ],
  2681. # },
  2682. # '*_struct_traits*.*': {
  2683. # 'files': ...,
  2684. # 'rules': [
  2685. # 'per-file *_struct_traits*.*=set noparent',
  2686. # 'per-file *_struct_traits*.*=file://ipc/SECURITY_OWNERS',
  2687. # ],
  2688. # },
  2689. # }
  2690. to_check = {}
  2691. files_to_review = []
  2692. def AddPatternToCheck(file, pattern):
  2693. owners_file = input_api.os_path.join(
  2694. input_api.os_path.dirname(file.LocalPath()), 'OWNERS')
  2695. if owners_file not in to_check:
  2696. to_check[owners_file] = {}
  2697. if pattern not in to_check[owners_file]:
  2698. to_check[owners_file][pattern] = {
  2699. 'files': [],
  2700. 'rules': [
  2701. f'per-file {pattern}=set noparent',
  2702. f'per-file {pattern}=file://{required_owners_file}',
  2703. ]
  2704. }
  2705. to_check[owners_file][pattern]['files'].append(file.LocalPath())
  2706. files_to_review.append(file.LocalPath())
  2707. # Only enforce security OWNERS rules for a directory if that directory has a
  2708. # file that matches `file_patterns`. For example, if a directory only
  2709. # contains *.mojom files and no *_messages*.h files, the check should only
  2710. # ensure that rules for *.mojom files are present.
  2711. for file in input_api.AffectedFiles(include_deletes=False):
  2712. file_basename = input_api.os_path.basename(file.LocalPath())
  2713. if custom_rule_function is not None and custom_rule_function(
  2714. input_api, file):
  2715. AddPatternToCheck(file, file_basename)
  2716. continue
  2717. if any(
  2718. input_api.fnmatch.fnmatch(file.LocalPath(), pattern)
  2719. for pattern in excluded_patterns):
  2720. continue
  2721. for pattern in file_patterns:
  2722. # Unlike `excluded_patterns`, `file_patterns` is checked only against the
  2723. # file's basename.
  2724. if input_api.fnmatch.fnmatch(file_basename, pattern):
  2725. AddPatternToCheck(file, pattern)
  2726. break
  2727. has_security_sensitive_files = bool(to_check)
  2728. # Check if any newly added lines in OWNERS files intersect with required
  2729. # per-file OWNERS lines. If so, ensure that a security reviewer is included.
  2730. # This is a hack, but is needed because the OWNERS check (by design) ignores
  2731. # new OWNERS entries; otherwise, a non-owner could add someone as a new
  2732. # OWNER and have that newly-added OWNER self-approve their own addition.
  2733. newly_covered_files = []
  2734. for file in input_api.AffectedFiles(include_deletes=False):
  2735. if not file.LocalPath() in to_check:
  2736. continue
  2737. for _, line in file.ChangedContents():
  2738. for _, entry in to_check[file.LocalPath()].items():
  2739. if line in entry['rules']:
  2740. newly_covered_files.extend(entry['files'])
  2741. missing_reviewer_problems = None
  2742. if newly_covered_files and not _ChangeHasSecurityReviewer(
  2743. input_api, required_owners_file):
  2744. missing_reviewer_problems = _SecurityProblemWithItems(
  2745. f'Review from an owner in {required_owners_file} is required for '
  2746. 'the following newly-added files:',
  2747. [f'{file}' for file in sorted(set(newly_covered_files))])
  2748. # Go through the OWNERS files to check, filtering out rules that are already
  2749. # present in that OWNERS file.
  2750. for owners_file, patterns in to_check.items():
  2751. try:
  2752. lines = set(
  2753. input_api.ReadFile(
  2754. input_api.os_path.join(input_api.change.RepositoryRoot(),
  2755. owners_file)).splitlines())
  2756. for entry in patterns.values():
  2757. entry['rules'] = [
  2758. rule for rule in entry['rules'] if rule not in lines
  2759. ]
  2760. except IOError:
  2761. # No OWNERS file, so all the rules are definitely missing.
  2762. continue
  2763. # All the remaining lines weren't found in OWNERS files, so emit an error.
  2764. owners_file_problems = []
  2765. for owners_file, patterns in to_check.items():
  2766. missing_lines = []
  2767. files = []
  2768. for _, entry in patterns.items():
  2769. files.extend(entry['files'])
  2770. missing_lines.extend(entry['rules'])
  2771. if missing_lines:
  2772. joined_missing_lines = '\n'.join(line for line in missing_lines)
  2773. owners_file_problems.append(
  2774. _SecurityProblemWithItems(
  2775. 'Found missing OWNERS lines for security-sensitive files. '
  2776. f'Please add the following lines to {owners_file}:\n'
  2777. f'{joined_missing_lines}\n\nTo ensure security review for:',
  2778. files))
  2779. return _MissingSecurityOwnersResult(owners_file_problems,
  2780. has_security_sensitive_files,
  2781. missing_reviewer_problems)
  2782. def _CheckChangeForIpcSecurityOwners(input_api, output_api):
  2783. # Whether or not a file affects IPC is (mostly) determined by a simple list
  2784. # of filename patterns.
  2785. file_patterns = [
  2786. # Legacy IPC:
  2787. '*_messages.cc',
  2788. '*_messages*.h',
  2789. '*_param_traits*.*',
  2790. # Mojo IPC:
  2791. '*.mojom',
  2792. '*_mojom_traits*.*',
  2793. '*_type_converter*.*',
  2794. # Android native IPC:
  2795. '*.aidl',
  2796. ]
  2797. excluded_patterns = [
  2798. # These third_party directories do not contain IPCs, but contain files
  2799. # matching the above patterns, which trigger false positives.
  2800. 'third_party/crashpad/*',
  2801. 'third_party/blink/renderer/platform/bindings/*',
  2802. 'third_party/protobuf/benchmarks/python/*',
  2803. 'third_party/win_build_output/*',
  2804. # Enum-only mojoms used for web metrics, so no security review needed.
  2805. 'third_party/blink/public/mojom/use_counter/metrics/*',
  2806. # These files are just used to communicate between class loaders running
  2807. # in the same process.
  2808. 'weblayer/browser/java/org/chromium/weblayer_private/interfaces/*',
  2809. 'weblayer/browser/java/org/chromium/weblayer_private/test_interfaces/*',
  2810. ]
  2811. def IsMojoServiceManifestFile(input_api, file):
  2812. manifest_pattern = input_api.re.compile('manifests?\.(cc|h)$')
  2813. test_manifest_pattern = input_api.re.compile('test_manifests?\.(cc|h)')
  2814. if not manifest_pattern.search(file.LocalPath()):
  2815. return False
  2816. if test_manifest_pattern.search(file.LocalPath()):
  2817. return False
  2818. # All actual service manifest files should contain at least one
  2819. # qualified reference to service_manager::Manifest.
  2820. return any('service_manager::Manifest' in line
  2821. for line in file.NewContents())
  2822. return _FindMissingSecurityOwners(
  2823. input_api,
  2824. output_api,
  2825. file_patterns,
  2826. excluded_patterns,
  2827. 'ipc/SECURITY_OWNERS',
  2828. custom_rule_function=IsMojoServiceManifestFile)
  2829. def _CheckChangeForFuchsiaSecurityOwners(input_api, output_api):
  2830. file_patterns = [
  2831. # Component specifications.
  2832. '*.cml', # Component Framework v2.
  2833. '*.cmx', # Component Framework v1.
  2834. # Fuchsia IDL protocol specifications.
  2835. '*.fidl',
  2836. ]
  2837. # Don't check for owners files for changes in these directories.
  2838. excluded_patterns = [
  2839. 'third_party/crashpad/*',
  2840. ]
  2841. return _FindMissingSecurityOwners(input_api, output_api, file_patterns,
  2842. excluded_patterns,
  2843. 'build/fuchsia/SECURITY_OWNERS')
  2844. def CheckSecurityOwners(input_api, output_api):
  2845. """Checks that various security-sensitive files have an IPC OWNERS rule."""
  2846. ipc_results = _CheckChangeForIpcSecurityOwners(input_api, output_api)
  2847. fuchsia_results = _CheckChangeForFuchsiaSecurityOwners(
  2848. input_api, output_api)
  2849. if ipc_results.has_security_sensitive_files:
  2850. output_api.AppendCC('ipc-security-reviews@chromium.org')
  2851. results = []
  2852. missing_reviewer_problems = []
  2853. if ipc_results.missing_reviewer_problem:
  2854. missing_reviewer_problems.append(ipc_results.missing_reviewer_problem)
  2855. if fuchsia_results.missing_reviewer_problem:
  2856. missing_reviewer_problems.append(
  2857. fuchsia_results.missing_reviewer_problem)
  2858. # Missing reviewers are an error unless there's no issue number
  2859. # associated with this branch; in that case, the presubmit is being run
  2860. # with --all or --files.
  2861. #
  2862. # Note that upload should never be an error; otherwise, it would be
  2863. # impossible to upload changes at all.
  2864. if input_api.is_committing and input_api.change.issue:
  2865. make_presubmit_message = output_api.PresubmitError
  2866. else:
  2867. make_presubmit_message = output_api.PresubmitNotifyResult
  2868. for problem in missing_reviewer_problems:
  2869. results.append(
  2870. make_presubmit_message(problem.problem, items=problem.items))
  2871. owners_file_problems = []
  2872. owners_file_problems.extend(ipc_results.owners_file_problems)
  2873. owners_file_problems.extend(fuchsia_results.owners_file_problems)
  2874. for problem in owners_file_problems:
  2875. # Missing per-file rules are always an error. While swarming and caching
  2876. # means that uploading a patchset with updated OWNERS files and sending
  2877. # it to the CQ again should not have a large incremental cost, it is
  2878. # still frustrating to discover the error only after the change has
  2879. # already been uploaded.
  2880. results.append(
  2881. output_api.PresubmitError(problem.problem, items=problem.items))
  2882. return results
  2883. def _GetFilesUsingSecurityCriticalFunctions(input_api):
  2884. """Checks affected files for changes to security-critical calls. This
  2885. function checks the full change diff, to catch both additions/changes
  2886. and removals.
  2887. Returns a dict keyed by file name, and the value is a set of detected
  2888. functions.
  2889. """
  2890. # Map of function pretty name (displayed in an error) to the pattern to
  2891. # match it with.
  2892. _PATTERNS_TO_CHECK = {
  2893. 'content::GetServiceSandboxType<>()': 'GetServiceSandboxType\\<'
  2894. }
  2895. _PATTERNS_TO_CHECK = {
  2896. k: input_api.re.compile(v)
  2897. for k, v in _PATTERNS_TO_CHECK.items()
  2898. }
  2899. # We don't want to trigger on strings within this file.
  2900. def presubmit_file_filter(f):
  2901. return 'PRESUBMIT.py' != input_api.os_path.split(f.LocalPath())[1]
  2902. # Scan all affected files for changes touching _FUNCTIONS_TO_CHECK.
  2903. files_to_functions = {}
  2904. for f in input_api.AffectedFiles(file_filter=presubmit_file_filter):
  2905. diff = f.GenerateScmDiff()
  2906. for line in diff.split('\n'):
  2907. # Not using just RightHandSideLines() because removing a
  2908. # call to a security-critical function can be just as important
  2909. # as adding or changing the arguments.
  2910. if line.startswith('-') or (line.startswith('+')
  2911. and not line.startswith('++')):
  2912. for name, pattern in _PATTERNS_TO_CHECK.items():
  2913. if pattern.search(line):
  2914. path = f.LocalPath()
  2915. if not path in files_to_functions:
  2916. files_to_functions[path] = set()
  2917. files_to_functions[path].add(name)
  2918. return files_to_functions
  2919. def CheckSecurityChanges(input_api, output_api):
  2920. """Checks that changes involving security-critical functions are reviewed
  2921. by the security team.
  2922. """
  2923. files_to_functions = _GetFilesUsingSecurityCriticalFunctions(input_api)
  2924. if not len(files_to_functions):
  2925. return []
  2926. owners_file = 'ipc/SECURITY_OWNERS'
  2927. if _ChangeHasSecurityReviewer(input_api, owners_file):
  2928. return []
  2929. msg = 'The following files change calls to security-sensitive functions\n' \
  2930. 'that need to be reviewed by {}.\n'.format(owners_file)
  2931. for path, names in files_to_functions.items():
  2932. msg += ' {}\n'.format(path)
  2933. for name in names:
  2934. msg += ' {}\n'.format(name)
  2935. msg += '\n'
  2936. if input_api.is_committing:
  2937. output = output_api.PresubmitError
  2938. else:
  2939. output = output_api.PresubmitNotifyResult
  2940. return [output(msg)]
  2941. def CheckSetNoParent(input_api, output_api):
  2942. """Checks that set noparent is only used together with an OWNERS file in
  2943. //build/OWNERS.setnoparent (see also
  2944. //docs/code_reviews.md#owners-files-details)
  2945. """
  2946. # Return early if no OWNERS files were modified.
  2947. if not any(f.LocalPath().endswith('OWNERS')
  2948. for f in input_api.AffectedFiles(include_deletes=False)):
  2949. return []
  2950. errors = []
  2951. allowed_owners_files_file = 'build/OWNERS.setnoparent'
  2952. allowed_owners_files = set()
  2953. with open(allowed_owners_files_file, 'r') as f:
  2954. for line in f:
  2955. line = line.strip()
  2956. if not line or line.startswith('#'):
  2957. continue
  2958. allowed_owners_files.add(line)
  2959. per_file_pattern = input_api.re.compile('per-file (.+)=(.+)')
  2960. for f in input_api.AffectedFiles(include_deletes=False):
  2961. if not f.LocalPath().endswith('OWNERS'):
  2962. continue
  2963. found_owners_files = set()
  2964. found_set_noparent_lines = dict()
  2965. # Parse the OWNERS file.
  2966. for lineno, line in enumerate(f.NewContents(), 1):
  2967. line = line.strip()
  2968. if line.startswith('set noparent'):
  2969. found_set_noparent_lines[''] = lineno
  2970. if line.startswith('file://'):
  2971. if line in allowed_owners_files:
  2972. found_owners_files.add('')
  2973. if line.startswith('per-file'):
  2974. match = per_file_pattern.match(line)
  2975. if match:
  2976. glob = match.group(1).strip()
  2977. directive = match.group(2).strip()
  2978. if directive == 'set noparent':
  2979. found_set_noparent_lines[glob] = lineno
  2980. if directive.startswith('file://'):
  2981. if directive in allowed_owners_files:
  2982. found_owners_files.add(glob)
  2983. # Check that every set noparent line has a corresponding file:// line
  2984. # listed in build/OWNERS.setnoparent. An exception is made for top level
  2985. # directories since src/OWNERS shouldn't review them.
  2986. linux_path = f.LocalPath().replace(input_api.os_path.sep, '/')
  2987. if (linux_path.count('/') != 1
  2988. and (not linux_path in _EXCLUDED_SET_NO_PARENT_PATHS)):
  2989. for set_noparent_line in found_set_noparent_lines:
  2990. if set_noparent_line in found_owners_files:
  2991. continue
  2992. errors.append(' %s:%d' %
  2993. (linux_path,
  2994. found_set_noparent_lines[set_noparent_line]))
  2995. results = []
  2996. if errors:
  2997. if input_api.is_committing:
  2998. output = output_api.PresubmitError
  2999. else:
  3000. output = output_api.PresubmitPromptWarning
  3001. results.append(
  3002. output(
  3003. 'Found the following "set noparent" restrictions in OWNERS files that '
  3004. 'do not include owners from build/OWNERS.setnoparent:',
  3005. long_text='\n\n'.join(errors)))
  3006. return results
  3007. def CheckUselessForwardDeclarations(input_api, output_api):
  3008. """Checks that added or removed lines in non third party affected
  3009. header files do not lead to new useless class or struct forward
  3010. declaration.
  3011. """
  3012. results = []
  3013. class_pattern = input_api.re.compile(r'^class\s+(\w+);$',
  3014. input_api.re.MULTILINE)
  3015. struct_pattern = input_api.re.compile(r'^struct\s+(\w+);$',
  3016. input_api.re.MULTILINE)
  3017. for f in input_api.AffectedFiles(include_deletes=False):
  3018. if (f.LocalPath().startswith('third_party')
  3019. and not f.LocalPath().startswith('third_party/blink')
  3020. and not f.LocalPath().startswith('third_party\\blink')):
  3021. continue
  3022. if not f.LocalPath().endswith('.h'):
  3023. continue
  3024. contents = input_api.ReadFile(f)
  3025. fwd_decls = input_api.re.findall(class_pattern, contents)
  3026. fwd_decls.extend(input_api.re.findall(struct_pattern, contents))
  3027. useless_fwd_decls = []
  3028. for decl in fwd_decls:
  3029. count = sum(1 for _ in input_api.re.finditer(
  3030. r'\b%s\b' % input_api.re.escape(decl), contents))
  3031. if count == 1:
  3032. useless_fwd_decls.append(decl)
  3033. if not useless_fwd_decls:
  3034. continue
  3035. for line in f.GenerateScmDiff().splitlines():
  3036. if (line.startswith('-') and not line.startswith('--')
  3037. or line.startswith('+') and not line.startswith('++')):
  3038. for decl in useless_fwd_decls:
  3039. if input_api.re.search(r'\b%s\b' % decl, line[1:]):
  3040. results.append(
  3041. output_api.PresubmitPromptWarning(
  3042. '%s: %s forward declaration is no longer needed'
  3043. % (f.LocalPath(), decl)))
  3044. useless_fwd_decls.remove(decl)
  3045. return results
  3046. def _CheckAndroidDebuggableBuild(input_api, output_api):
  3047. """Checks that code uses BuildInfo.isDebugAndroid() instead of
  3048. Build.TYPE.equals('') or ''.equals(Build.TYPE) to check if
  3049. this is a debuggable build of Android.
  3050. """
  3051. build_type_check_pattern = input_api.re.compile(
  3052. r'\bBuild\.TYPE\.equals\(|\.equals\(\s*\bBuild\.TYPE\)')
  3053. errors = []
  3054. sources = lambda affected_file: input_api.FilterSourceFile(
  3055. affected_file,
  3056. files_to_skip=(
  3057. _EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api.
  3058. DEFAULT_FILES_TO_SKIP + (
  3059. r"^android_webview[\\/]support_library[\\/]"
  3060. "boundary_interfaces[\\/]",
  3061. r"^chrome[\\/]android[\\/]webapk[\\/].*",
  3062. r'^third_party[\\/].*',
  3063. r"tools[\\/]android[\\/]customtabs_benchmark[\\/].*",
  3064. r"webview[\\/]chromium[\\/]License.*",
  3065. )),
  3066. files_to_check=[r'.*\.java$'])
  3067. for f in input_api.AffectedSourceFiles(sources):
  3068. for line_num, line in f.ChangedContents():
  3069. if build_type_check_pattern.search(line):
  3070. errors.append("%s:%d" % (f.LocalPath(), line_num))
  3071. results = []
  3072. if errors:
  3073. results.append(
  3074. output_api.PresubmitPromptWarning(
  3075. 'Build.TYPE.equals or .equals(Build.TYPE) usage is detected.'
  3076. ' Please use BuildInfo.isDebugAndroid() instead.', errors))
  3077. return results
  3078. # TODO: add unit tests
  3079. def _CheckAndroidToastUsage(input_api, output_api):
  3080. """Checks that code uses org.chromium.ui.widget.Toast instead of
  3081. android.widget.Toast (Chromium Toast doesn't force hardware
  3082. acceleration on low-end devices, saving memory).
  3083. """
  3084. toast_import_pattern = input_api.re.compile(
  3085. r'^import android\.widget\.Toast;$')
  3086. errors = []
  3087. sources = lambda affected_file: input_api.FilterSourceFile(
  3088. affected_file,
  3089. files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api.
  3090. DEFAULT_FILES_TO_SKIP + (r'^chromecast[\\/].*',
  3091. r'^remoting[\\/].*')),
  3092. files_to_check=[r'.*\.java$'])
  3093. for f in input_api.AffectedSourceFiles(sources):
  3094. for line_num, line in f.ChangedContents():
  3095. if toast_import_pattern.search(line):
  3096. errors.append("%s:%d" % (f.LocalPath(), line_num))
  3097. results = []
  3098. if errors:
  3099. results.append(
  3100. output_api.PresubmitError(
  3101. 'android.widget.Toast usage is detected. Android toasts use hardware'
  3102. ' acceleration, and can be\ncostly on low-end devices. Please use'
  3103. ' org.chromium.ui.widget.Toast instead.\n'
  3104. 'Contact dskiba@chromium.org if you have any questions.',
  3105. errors))
  3106. return results
  3107. def _CheckAndroidCrLogUsage(input_api, output_api):
  3108. """Checks that new logs using org.chromium.base.Log:
  3109. - Are using 'TAG' as variable name for the tags (warn)
  3110. - Are using a tag that is shorter than 20 characters (error)
  3111. """
  3112. # Do not check format of logs in the given files
  3113. cr_log_check_excluded_paths = [
  3114. # //chrome/android/webapk cannot depend on //base
  3115. r"^chrome[\\/]android[\\/]webapk[\\/].*",
  3116. # WebView license viewer code cannot depend on //base; used in stub APK.
  3117. r"^android_webview[\\/]glue[\\/]java[\\/]src[\\/]com[\\/]android[\\/]"
  3118. r"webview[\\/]chromium[\\/]License.*",
  3119. # The customtabs_benchmark is a small app that does not depend on Chromium
  3120. # java pieces.
  3121. r"tools[\\/]android[\\/]customtabs_benchmark[\\/].*",
  3122. ]
  3123. cr_log_import_pattern = input_api.re.compile(
  3124. r'^import org\.chromium\.base\.Log;$', input_api.re.MULTILINE)
  3125. class_in_base_pattern = input_api.re.compile(
  3126. r'^package org\.chromium\.base;$', input_api.re.MULTILINE)
  3127. has_some_log_import_pattern = input_api.re.compile(r'^import .*\.Log;$',
  3128. input_api.re.MULTILINE)
  3129. # Extract the tag from lines like `Log.d(TAG, "*");` or `Log.d("TAG", "*");`
  3130. log_call_pattern = input_api.re.compile(r'\bLog\.\w\((?P<tag>\"?\w+)')
  3131. log_decl_pattern = input_api.re.compile(
  3132. r'static final String TAG = "(?P<name>(.*))"')
  3133. rough_log_decl_pattern = input_api.re.compile(r'\bString TAG\s*=')
  3134. REF_MSG = ('See docs/android_logging.md for more info.')
  3135. sources = lambda x: input_api.FilterSourceFile(
  3136. x,
  3137. files_to_check=[r'.*\.java$'],
  3138. files_to_skip=cr_log_check_excluded_paths)
  3139. tag_decl_errors = []
  3140. tag_length_errors = []
  3141. tag_errors = []
  3142. tag_with_dot_errors = []
  3143. util_log_errors = []
  3144. for f in input_api.AffectedSourceFiles(sources):
  3145. file_content = input_api.ReadFile(f)
  3146. has_modified_logs = False
  3147. # Per line checks
  3148. if (cr_log_import_pattern.search(file_content)
  3149. or (class_in_base_pattern.search(file_content)
  3150. and not has_some_log_import_pattern.search(file_content))):
  3151. # Checks to run for files using cr log
  3152. for line_num, line in f.ChangedContents():
  3153. if rough_log_decl_pattern.search(line):
  3154. has_modified_logs = True
  3155. # Check if the new line is doing some logging
  3156. match = log_call_pattern.search(line)
  3157. if match:
  3158. has_modified_logs = True
  3159. # Make sure it uses "TAG"
  3160. if not match.group('tag') == 'TAG':
  3161. tag_errors.append("%s:%d" % (f.LocalPath(), line_num))
  3162. else:
  3163. # Report non cr Log function calls in changed lines
  3164. for line_num, line in f.ChangedContents():
  3165. if log_call_pattern.search(line):
  3166. util_log_errors.append("%s:%d" % (f.LocalPath(), line_num))
  3167. # Per file checks
  3168. if has_modified_logs:
  3169. # Make sure the tag is using the "cr" prefix and is not too long
  3170. match = log_decl_pattern.search(file_content)
  3171. tag_name = match.group('name') if match else None
  3172. if not tag_name:
  3173. tag_decl_errors.append(f.LocalPath())
  3174. elif len(tag_name) > 20:
  3175. tag_length_errors.append(f.LocalPath())
  3176. elif '.' in tag_name:
  3177. tag_with_dot_errors.append(f.LocalPath())
  3178. results = []
  3179. if tag_decl_errors:
  3180. results.append(
  3181. output_api.PresubmitPromptWarning(
  3182. 'Please define your tags using the suggested format: .\n'
  3183. '"private static final String TAG = "<package tag>".\n'
  3184. 'They will be prepended with "cr_" automatically.\n' + REF_MSG,
  3185. tag_decl_errors))
  3186. if tag_length_errors:
  3187. results.append(
  3188. output_api.PresubmitError(
  3189. 'The tag length is restricted by the system to be at most '
  3190. '20 characters.\n' + REF_MSG, tag_length_errors))
  3191. if tag_errors:
  3192. results.append(
  3193. output_api.PresubmitPromptWarning(
  3194. 'Please use a variable named "TAG" for your log tags.\n' +
  3195. REF_MSG, tag_errors))
  3196. if util_log_errors:
  3197. results.append(
  3198. output_api.PresubmitPromptWarning(
  3199. 'Please use org.chromium.base.Log for new logs.\n' + REF_MSG,
  3200. util_log_errors))
  3201. if tag_with_dot_errors:
  3202. results.append(
  3203. output_api.PresubmitPromptWarning(
  3204. 'Dot in log tags cause them to be elided in crash reports.\n' +
  3205. REF_MSG, tag_with_dot_errors))
  3206. return results
  3207. def _CheckAndroidTestJUnitFrameworkImport(input_api, output_api):
  3208. """Checks that junit.framework.* is no longer used."""
  3209. deprecated_junit_framework_pattern = input_api.re.compile(
  3210. r'^import junit\.framework\..*;', input_api.re.MULTILINE)
  3211. sources = lambda x: input_api.FilterSourceFile(
  3212. x, files_to_check=[r'.*\.java$'], files_to_skip=None)
  3213. errors = []
  3214. for f in input_api.AffectedFiles(file_filter=sources):
  3215. for line_num, line in f.ChangedContents():
  3216. if deprecated_junit_framework_pattern.search(line):
  3217. errors.append("%s:%d" % (f.LocalPath(), line_num))
  3218. results = []
  3219. if errors:
  3220. results.append(
  3221. output_api.PresubmitError(
  3222. 'APIs from junit.framework.* are deprecated, please use JUnit4 framework'
  3223. '(org.junit.*) from //third_party/junit. Contact yolandyan@chromium.org'
  3224. ' if you have any question.', errors))
  3225. return results
  3226. def _CheckAndroidTestJUnitInheritance(input_api, output_api):
  3227. """Checks that if new Java test classes have inheritance.
  3228. Either the new test class is JUnit3 test or it is a JUnit4 test class
  3229. with a base class, either case is undesirable.
  3230. """
  3231. class_declaration_pattern = input_api.re.compile(r'^public class \w*Test ')
  3232. sources = lambda x: input_api.FilterSourceFile(
  3233. x, files_to_check=[r'.*Test\.java$'], files_to_skip=None)
  3234. errors = []
  3235. for f in input_api.AffectedFiles(file_filter=sources):
  3236. if not f.OldContents():
  3237. class_declaration_start_flag = False
  3238. for line_num, line in f.ChangedContents():
  3239. if class_declaration_pattern.search(line):
  3240. class_declaration_start_flag = True
  3241. if class_declaration_start_flag and ' extends ' in line:
  3242. errors.append('%s:%d' % (f.LocalPath(), line_num))
  3243. if '{' in line:
  3244. class_declaration_start_flag = False
  3245. results = []
  3246. if errors:
  3247. results.append(
  3248. output_api.PresubmitPromptWarning(
  3249. 'The newly created files include Test classes that inherits from base'
  3250. ' class. Please do not use inheritance in JUnit4 tests or add new'
  3251. ' JUnit3 tests. Contact yolandyan@chromium.org if you have any'
  3252. ' questions.', errors))
  3253. return results
  3254. def _CheckAndroidTestAnnotationUsage(input_api, output_api):
  3255. """Checks that android.test.suitebuilder.annotation.* is no longer used."""
  3256. deprecated_annotation_import_pattern = input_api.re.compile(
  3257. r'^import android\.test\.suitebuilder\.annotation\..*;',
  3258. input_api.re.MULTILINE)
  3259. sources = lambda x: input_api.FilterSourceFile(
  3260. x, files_to_check=[r'.*\.java$'], files_to_skip=None)
  3261. errors = []
  3262. for f in input_api.AffectedFiles(file_filter=sources):
  3263. for line_num, line in f.ChangedContents():
  3264. if deprecated_annotation_import_pattern.search(line):
  3265. errors.append("%s:%d" % (f.LocalPath(), line_num))
  3266. results = []
  3267. if errors:
  3268. results.append(
  3269. output_api.PresubmitError(
  3270. 'Annotations in android.test.suitebuilder.annotation have been'
  3271. ' deprecated since API level 24. Please use android.support.test.filters'
  3272. ' from //third_party/android_support_test_runner:runner_java instead.'
  3273. ' Contact yolandyan@chromium.org if you have any questions.',
  3274. errors))
  3275. return results
  3276. def _CheckAndroidNewMdpiAssetLocation(input_api, output_api):
  3277. """Checks if MDPI assets are placed in a correct directory."""
  3278. file_filter = lambda f: (f.LocalPath().endswith(
  3279. '.png') and ('/res/drawable/'.replace('/', input_api.os_path.sep) in f.
  3280. LocalPath() or '/res/drawable-ldrtl/'.replace(
  3281. '/', input_api.os_path.sep) in f.LocalPath()))
  3282. errors = []
  3283. for f in input_api.AffectedFiles(include_deletes=False,
  3284. file_filter=file_filter):
  3285. errors.append(' %s' % f.LocalPath())
  3286. results = []
  3287. if errors:
  3288. results.append(
  3289. output_api.PresubmitError(
  3290. 'MDPI assets should be placed in /res/drawable-mdpi/ or '
  3291. '/res/drawable-ldrtl-mdpi/\ninstead of /res/drawable/ and'
  3292. '/res/drawable-ldrtl/.\n'
  3293. 'Contact newt@chromium.org if you have questions.', errors))
  3294. return results
  3295. def _CheckAndroidWebkitImports(input_api, output_api):
  3296. """Checks that code uses org.chromium.base.Callback instead of
  3297. android.webview.ValueCallback except in the WebView glue layer
  3298. and WebLayer.
  3299. """
  3300. valuecallback_import_pattern = input_api.re.compile(
  3301. r'^import android\.webkit\.ValueCallback;$')
  3302. errors = []
  3303. sources = lambda affected_file: input_api.FilterSourceFile(
  3304. affected_file,
  3305. files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api.
  3306. DEFAULT_FILES_TO_SKIP + (
  3307. r'^android_webview[\\/]glue[\\/].*',
  3308. r'^weblayer[\\/].*',
  3309. )),
  3310. files_to_check=[r'.*\.java$'])
  3311. for f in input_api.AffectedSourceFiles(sources):
  3312. for line_num, line in f.ChangedContents():
  3313. if valuecallback_import_pattern.search(line):
  3314. errors.append("%s:%d" % (f.LocalPath(), line_num))
  3315. results = []
  3316. if errors:
  3317. results.append(
  3318. output_api.PresubmitError(
  3319. 'android.webkit.ValueCallback usage is detected outside of the glue'
  3320. ' layer. To stay compatible with the support library, android.webkit.*'
  3321. ' classes should only be used inside the glue layer and'
  3322. ' org.chromium.base.Callback should be used instead.', errors))
  3323. return results
  3324. def _CheckAndroidXmlStyle(input_api, output_api, is_check_on_upload):
  3325. """Checks Android XML styles """
  3326. # Return early if no relevant files were modified.
  3327. if not any(
  3328. _IsXmlOrGrdFile(input_api, f.LocalPath())
  3329. for f in input_api.AffectedFiles(include_deletes=False)):
  3330. return []
  3331. import sys
  3332. original_sys_path = sys.path
  3333. try:
  3334. sys.path = sys.path + [
  3335. input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools',
  3336. 'android', 'checkxmlstyle')
  3337. ]
  3338. import checkxmlstyle
  3339. finally:
  3340. # Restore sys.path to what it was before.
  3341. sys.path = original_sys_path
  3342. if is_check_on_upload:
  3343. return checkxmlstyle.CheckStyleOnUpload(input_api, output_api)
  3344. else:
  3345. return checkxmlstyle.CheckStyleOnCommit(input_api, output_api)
  3346. def _CheckAndroidInfoBarDeprecation(input_api, output_api):
  3347. """Checks Android Infobar Deprecation """
  3348. import sys
  3349. original_sys_path = sys.path
  3350. try:
  3351. sys.path = sys.path + [
  3352. input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools',
  3353. 'android', 'infobar_deprecation')
  3354. ]
  3355. import infobar_deprecation
  3356. finally:
  3357. # Restore sys.path to what it was before.
  3358. sys.path = original_sys_path
  3359. return infobar_deprecation.CheckDeprecationOnUpload(input_api, output_api)
  3360. class _PydepsCheckerResult:
  3361. def __init__(self, cmd, pydeps_path, process, old_contents):
  3362. self._cmd = cmd
  3363. self._pydeps_path = pydeps_path
  3364. self._process = process
  3365. self._old_contents = old_contents
  3366. def GetError(self):
  3367. """Returns an error message, or None."""
  3368. import difflib
  3369. if self._process.wait() != 0:
  3370. # STDERR should already be printed.
  3371. return 'Command failed: ' + self._cmd
  3372. new_contents = self._process.stdout.read().splitlines()[2:]
  3373. if self._old_contents != new_contents:
  3374. diff = '\n'.join(
  3375. difflib.context_diff(self._old_contents, new_contents))
  3376. return ('File is stale: {}\n'
  3377. 'Diff (apply to fix):\n'
  3378. '{}\n'
  3379. 'To regenerate, run:\n\n'
  3380. ' {}').format(self._pydeps_path, diff, self._cmd)
  3381. return None
  3382. class PydepsChecker:
  3383. def __init__(self, input_api, pydeps_files):
  3384. self._file_cache = {}
  3385. self._input_api = input_api
  3386. self._pydeps_files = pydeps_files
  3387. def _LoadFile(self, path):
  3388. """Returns the list of paths within a .pydeps file relative to //."""
  3389. if path not in self._file_cache:
  3390. with open(path, encoding='utf-8') as f:
  3391. self._file_cache[path] = f.read()
  3392. return self._file_cache[path]
  3393. def _ComputeNormalizedPydepsEntries(self, pydeps_path):
  3394. """Returns an iterable of paths within the .pydep, relativized to //."""
  3395. pydeps_data = self._LoadFile(pydeps_path)
  3396. uses_gn_paths = '--gn-paths' in pydeps_data
  3397. entries = (l for l in pydeps_data.splitlines()
  3398. if not l.startswith('#'))
  3399. if uses_gn_paths:
  3400. # Paths look like: //foo/bar/baz
  3401. return (e[2:] for e in entries)
  3402. else:
  3403. # Paths look like: path/relative/to/file.pydeps
  3404. os_path = self._input_api.os_path
  3405. pydeps_dir = os_path.dirname(pydeps_path)
  3406. return (os_path.normpath(os_path.join(pydeps_dir, e))
  3407. for e in entries)
  3408. def _CreateFilesToPydepsMap(self):
  3409. """Returns a map of local_path -> list_of_pydeps."""
  3410. ret = {}
  3411. for pydep_local_path in self._pydeps_files:
  3412. for path in self._ComputeNormalizedPydepsEntries(pydep_local_path):
  3413. ret.setdefault(path, []).append(pydep_local_path)
  3414. return ret
  3415. def ComputeAffectedPydeps(self):
  3416. """Returns an iterable of .pydeps files that might need regenerating."""
  3417. affected_pydeps = set()
  3418. file_to_pydeps_map = None
  3419. for f in self._input_api.AffectedFiles(include_deletes=True):
  3420. local_path = f.LocalPath()
  3421. # Changes to DEPS can lead to .pydeps changes if any .py files are in
  3422. # subrepositories. We can't figure out which files change, so re-check
  3423. # all files.
  3424. # Changes to print_python_deps.py affect all .pydeps.
  3425. if local_path in ('DEPS', 'PRESUBMIT.py'
  3426. ) or local_path.endswith('print_python_deps.py'):
  3427. return self._pydeps_files
  3428. elif local_path.endswith('.pydeps'):
  3429. if local_path in self._pydeps_files:
  3430. affected_pydeps.add(local_path)
  3431. elif local_path.endswith('.py'):
  3432. if file_to_pydeps_map is None:
  3433. file_to_pydeps_map = self._CreateFilesToPydepsMap()
  3434. affected_pydeps.update(file_to_pydeps_map.get(local_path, ()))
  3435. return affected_pydeps
  3436. def DetermineIfStaleAsync(self, pydeps_path):
  3437. """Runs print_python_deps.py to see if the files is stale."""
  3438. import os
  3439. old_pydeps_data = self._LoadFile(pydeps_path).splitlines()
  3440. if old_pydeps_data:
  3441. cmd = old_pydeps_data[1][1:].strip()
  3442. if '--output' not in cmd:
  3443. cmd += ' --output ' + pydeps_path
  3444. old_contents = old_pydeps_data[2:]
  3445. else:
  3446. # A default cmd that should work in most cases (as long as pydeps filename
  3447. # matches the script name) so that PRESUBMIT.py does not crash if pydeps
  3448. # file is empty/new.
  3449. cmd = 'build/print_python_deps.py {} --root={} --output={}'.format(
  3450. pydeps_path[:-4], os.path.dirname(pydeps_path), pydeps_path)
  3451. old_contents = []
  3452. env = dict(os.environ)
  3453. env['PYTHONDONTWRITEBYTECODE'] = '1'
  3454. process = self._input_api.subprocess.Popen(
  3455. cmd + ' --output ""',
  3456. shell=True,
  3457. env=env,
  3458. stdout=self._input_api.subprocess.PIPE,
  3459. encoding='utf-8')
  3460. return _PydepsCheckerResult(cmd, pydeps_path, process, old_contents)
  3461. def _ParseGclientArgs():
  3462. args = {}
  3463. with open('build/config/gclient_args.gni', 'r') as f:
  3464. for line in f:
  3465. line = line.strip()
  3466. if not line or line.startswith('#'):
  3467. continue
  3468. attribute, value = line.split('=')
  3469. args[attribute.strip()] = value.strip()
  3470. return args
  3471. def CheckPydepsNeedsUpdating(input_api, output_api, checker_for_tests=None):
  3472. """Checks if a .pydeps file needs to be regenerated."""
  3473. # This check is for Python dependency lists (.pydeps files), and involves
  3474. # paths not only in the PRESUBMIT.py, but also in the .pydeps files. It
  3475. # doesn't work on Windows and Mac, so skip it on other platforms.
  3476. if not input_api.platform.startswith('linux'):
  3477. return []
  3478. results = []
  3479. # First, check for new / deleted .pydeps.
  3480. for f in input_api.AffectedFiles(include_deletes=True):
  3481. # Check whether we are running the presubmit check for a file in src.
  3482. # f.LocalPath is relative to repo (src, or internal repo).
  3483. # os_path.exists is relative to src repo.
  3484. # Therefore if os_path.exists is true, it means f.LocalPath is relative
  3485. # to src and we can conclude that the pydeps is in src.
  3486. if f.LocalPath().endswith('.pydeps'):
  3487. if input_api.os_path.exists(f.LocalPath()):
  3488. if f.Action() == 'D' and f.LocalPath() in _ALL_PYDEPS_FILES:
  3489. results.append(
  3490. output_api.PresubmitError(
  3491. 'Please update _ALL_PYDEPS_FILES within //PRESUBMIT.py to '
  3492. 'remove %s' % f.LocalPath()))
  3493. elif f.Action() != 'D' and f.LocalPath(
  3494. ) not in _ALL_PYDEPS_FILES:
  3495. results.append(
  3496. output_api.PresubmitError(
  3497. 'Please update _ALL_PYDEPS_FILES within //PRESUBMIT.py to '
  3498. 'include %s' % f.LocalPath()))
  3499. if results:
  3500. return results
  3501. is_android = _ParseGclientArgs().get('checkout_android', 'false') == 'true'
  3502. checker = checker_for_tests or PydepsChecker(input_api, _ALL_PYDEPS_FILES)
  3503. affected_pydeps = set(checker.ComputeAffectedPydeps())
  3504. affected_android_pydeps = affected_pydeps.intersection(
  3505. set(_ANDROID_SPECIFIC_PYDEPS_FILES))
  3506. if affected_android_pydeps and not is_android:
  3507. results.append(
  3508. output_api.PresubmitPromptOrNotify(
  3509. 'You have changed python files that may affect pydeps for android\n'
  3510. 'specific scripts. However, the relevant presubmit check cannot be\n'
  3511. 'run because you are not using an Android checkout. To validate that\n'
  3512. 'the .pydeps are correct, re-run presubmit in an Android checkout, or\n'
  3513. 'use the android-internal-presubmit optional trybot.\n'
  3514. 'Possibly stale pydeps files:\n{}'.format(
  3515. '\n'.join(affected_android_pydeps))))
  3516. all_pydeps = _ALL_PYDEPS_FILES if is_android else _GENERIC_PYDEPS_FILES
  3517. pydeps_to_check = affected_pydeps.intersection(all_pydeps)
  3518. # Process these concurrently, as each one takes 1-2 seconds.
  3519. pydep_results = [checker.DetermineIfStaleAsync(p) for p in pydeps_to_check]
  3520. for result in pydep_results:
  3521. error_msg = result.GetError()
  3522. if error_msg:
  3523. results.append(output_api.PresubmitError(error_msg))
  3524. return results
  3525. def CheckSingletonInHeaders(input_api, output_api):
  3526. """Checks to make sure no header files have |Singleton<|."""
  3527. def FileFilter(affected_file):
  3528. # It's ok for base/memory/singleton.h to have |Singleton<|.
  3529. files_to_skip = (_EXCLUDED_PATHS + input_api.DEFAULT_FILES_TO_SKIP +
  3530. (r"^base[\\/]memory[\\/]singleton\.h$",
  3531. r"^net[\\/]quic[\\/]platform[\\/]impl[\\/]"
  3532. r"quic_singleton_impl\.h$"))
  3533. return input_api.FilterSourceFile(affected_file,
  3534. files_to_skip=files_to_skip)
  3535. pattern = input_api.re.compile(r'(?<!class\sbase::)Singleton\s*<')
  3536. files = []
  3537. for f in input_api.AffectedSourceFiles(FileFilter):
  3538. if (f.LocalPath().endswith('.h') or f.LocalPath().endswith('.hxx')
  3539. or f.LocalPath().endswith('.hpp')
  3540. or f.LocalPath().endswith('.inl')):
  3541. contents = input_api.ReadFile(f)
  3542. for line in contents.splitlines(False):
  3543. if (not line.lstrip().startswith('//')
  3544. and # Strip C++ comment.
  3545. pattern.search(line)):
  3546. files.append(f)
  3547. break
  3548. if files:
  3549. return [
  3550. output_api.PresubmitError(
  3551. 'Found base::Singleton<T> in the following header files.\n' +
  3552. 'Please move them to an appropriate source file so that the ' +
  3553. 'template gets instantiated in a single compilation unit.',
  3554. files)
  3555. ]
  3556. return []
  3557. _DEPRECATED_CSS = [
  3558. # Values
  3559. ( "-webkit-box", "flex" ),
  3560. ( "-webkit-inline-box", "inline-flex" ),
  3561. ( "-webkit-flex", "flex" ),
  3562. ( "-webkit-inline-flex", "inline-flex" ),
  3563. ( "-webkit-min-content", "min-content" ),
  3564. ( "-webkit-max-content", "max-content" ),
  3565. # Properties
  3566. ( "-webkit-background-clip", "background-clip" ),
  3567. ( "-webkit-background-origin", "background-origin" ),
  3568. ( "-webkit-background-size", "background-size" ),
  3569. ( "-webkit-box-shadow", "box-shadow" ),
  3570. ( "-webkit-user-select", "user-select" ),
  3571. # Functions
  3572. ( "-webkit-gradient", "gradient" ),
  3573. ( "-webkit-repeating-gradient", "repeating-gradient" ),
  3574. ( "-webkit-linear-gradient", "linear-gradient" ),
  3575. ( "-webkit-repeating-linear-gradient", "repeating-linear-gradient" ),
  3576. ( "-webkit-radial-gradient", "radial-gradient" ),
  3577. ( "-webkit-repeating-radial-gradient", "repeating-radial-gradient" ),
  3578. ]
  3579. # TODO: add unit tests
  3580. def CheckNoDeprecatedCss(input_api, output_api):
  3581. """ Make sure that we don't use deprecated CSS
  3582. properties, functions or values. Our external
  3583. documentation and iOS CSS for dom distiller
  3584. (reader mode) are ignored by the hooks as it
  3585. needs to be consumed by WebKit. """
  3586. results = []
  3587. file_inclusion_pattern = [r".+\.css$"]
  3588. files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS +
  3589. input_api.DEFAULT_FILES_TO_SKIP +
  3590. (r"^chrome/common/extensions/docs", r"^chrome/docs",
  3591. r"^native_client_sdk"))
  3592. file_filter = lambda f: input_api.FilterSourceFile(
  3593. f, files_to_check=file_inclusion_pattern, files_to_skip=files_to_skip)
  3594. for fpath in input_api.AffectedFiles(file_filter=file_filter):
  3595. for line_num, line in fpath.ChangedContents():
  3596. for (deprecated_value, value) in _DEPRECATED_CSS:
  3597. if deprecated_value in line:
  3598. results.append(
  3599. output_api.PresubmitError(
  3600. "%s:%d: Use of deprecated CSS %s, use %s instead" %
  3601. (fpath.LocalPath(), line_num, deprecated_value,
  3602. value)))
  3603. return results
  3604. def CheckForRelativeIncludes(input_api, output_api):
  3605. bad_files = {}
  3606. for f in input_api.AffectedFiles(include_deletes=False):
  3607. if (f.LocalPath().startswith('third_party')
  3608. and not f.LocalPath().startswith('third_party/blink')
  3609. and not f.LocalPath().startswith('third_party\\blink')):
  3610. continue
  3611. if not _IsCPlusPlusFile(input_api, f.LocalPath()):
  3612. continue
  3613. relative_includes = [
  3614. line for _, line in f.ChangedContents()
  3615. if "#include" in line and "../" in line
  3616. ]
  3617. if not relative_includes:
  3618. continue
  3619. bad_files[f.LocalPath()] = relative_includes
  3620. if not bad_files:
  3621. return []
  3622. error_descriptions = []
  3623. for file_path, bad_lines in bad_files.items():
  3624. error_description = file_path
  3625. for line in bad_lines:
  3626. error_description += '\n ' + line
  3627. error_descriptions.append(error_description)
  3628. results = []
  3629. results.append(
  3630. output_api.PresubmitError(
  3631. 'You added one or more relative #include paths (including "../").\n'
  3632. 'These shouldn\'t be used because they can be used to include headers\n'
  3633. 'from code that\'s not correctly specified as a dependency in the\n'
  3634. 'relevant BUILD.gn file(s).', error_descriptions))
  3635. return results
  3636. def CheckForCcIncludes(input_api, output_api):
  3637. """Check that nobody tries to include a cc file. It's a relatively
  3638. common error which results in duplicate symbols in object
  3639. files. This may not always break the build until someone later gets
  3640. very confusing linking errors."""
  3641. results = []
  3642. for f in input_api.AffectedFiles(include_deletes=False):
  3643. # We let third_party code do whatever it wants
  3644. if (f.LocalPath().startswith('third_party')
  3645. and not f.LocalPath().startswith('third_party/blink')
  3646. and not f.LocalPath().startswith('third_party\\blink')):
  3647. continue
  3648. if not _IsCPlusPlusFile(input_api, f.LocalPath()):
  3649. continue
  3650. for _, line in f.ChangedContents():
  3651. if line.startswith('#include "'):
  3652. included_file = line.split('"')[1]
  3653. if _IsCPlusPlusFile(input_api, included_file):
  3654. # The most common naming for external files with C++ code,
  3655. # apart from standard headers, is to call them foo.inc, but
  3656. # Chromium sometimes uses foo-inc.cc so allow that as well.
  3657. if not included_file.endswith(('.h', '-inc.cc')):
  3658. results.append(
  3659. output_api.PresubmitError(
  3660. 'Only header files or .inc files should be included in other\n'
  3661. 'C++ files. Compiling the contents of a cc file more than once\n'
  3662. 'will cause duplicate information in the build which may later\n'
  3663. 'result in strange link_errors.\n' +
  3664. f.LocalPath() + ':\n ' + line))
  3665. return results
  3666. def _CheckWatchlistDefinitionsEntrySyntax(key, value, ast):
  3667. if not isinstance(key, ast.Str):
  3668. return 'Key at line %d must be a string literal' % key.lineno
  3669. if not isinstance(value, ast.Dict):
  3670. return 'Value at line %d must be a dict' % value.lineno
  3671. if len(value.keys) != 1:
  3672. return 'Dict at line %d must have single entry' % value.lineno
  3673. if not isinstance(value.keys[0], ast.Str) or value.keys[0].s != 'filepath':
  3674. return (
  3675. 'Entry at line %d must have a string literal \'filepath\' as key' %
  3676. value.lineno)
  3677. return None
  3678. def _CheckWatchlistsEntrySyntax(key, value, ast, email_regex):
  3679. if not isinstance(key, ast.Str):
  3680. return 'Key at line %d must be a string literal' % key.lineno
  3681. if not isinstance(value, ast.List):
  3682. return 'Value at line %d must be a list' % value.lineno
  3683. for element in value.elts:
  3684. if not isinstance(element, ast.Str):
  3685. return 'Watchlist elements on line %d is not a string' % key.lineno
  3686. if not email_regex.match(element.s):
  3687. return ('Watchlist element on line %d doesn\'t look like a valid '
  3688. + 'email: %s') % (key.lineno, element.s)
  3689. return None
  3690. def _CheckWATCHLISTSEntries(wd_dict, w_dict, input_api):
  3691. mismatch_template = (
  3692. 'Mismatch between WATCHLIST_DEFINITIONS entry (%s) and WATCHLISTS '
  3693. 'entry (%s)')
  3694. email_regex = input_api.re.compile(
  3695. r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+$")
  3696. ast = input_api.ast
  3697. i = 0
  3698. last_key = ''
  3699. while True:
  3700. if i >= len(wd_dict.keys):
  3701. if i >= len(w_dict.keys):
  3702. return None
  3703. return mismatch_template % ('missing',
  3704. 'line %d' % w_dict.keys[i].lineno)
  3705. elif i >= len(w_dict.keys):
  3706. return (mismatch_template %
  3707. ('line %d' % wd_dict.keys[i].lineno, 'missing'))
  3708. wd_key = wd_dict.keys[i]
  3709. w_key = w_dict.keys[i]
  3710. result = _CheckWatchlistDefinitionsEntrySyntax(wd_key,
  3711. wd_dict.values[i], ast)
  3712. if result is not None:
  3713. return 'Bad entry in WATCHLIST_DEFINITIONS dict: %s' % result
  3714. result = _CheckWatchlistsEntrySyntax(w_key, w_dict.values[i], ast,
  3715. email_regex)
  3716. if result is not None:
  3717. return 'Bad entry in WATCHLISTS dict: %s' % result
  3718. if wd_key.s != w_key.s:
  3719. return mismatch_template % ('%s at line %d' %
  3720. (wd_key.s, wd_key.lineno),
  3721. '%s at line %d' %
  3722. (w_key.s, w_key.lineno))
  3723. if wd_key.s < last_key:
  3724. return (
  3725. 'WATCHLISTS dict is not sorted lexicographically at line %d and %d'
  3726. % (wd_key.lineno, w_key.lineno))
  3727. last_key = wd_key.s
  3728. i = i + 1
  3729. def _CheckWATCHLISTSSyntax(expression, input_api):
  3730. ast = input_api.ast
  3731. if not isinstance(expression, ast.Expression):
  3732. return 'WATCHLISTS file must contain a valid expression'
  3733. dictionary = expression.body
  3734. if not isinstance(dictionary, ast.Dict) or len(dictionary.keys) != 2:
  3735. return 'WATCHLISTS file must have single dict with exactly two entries'
  3736. first_key = dictionary.keys[0]
  3737. first_value = dictionary.values[0]
  3738. second_key = dictionary.keys[1]
  3739. second_value = dictionary.values[1]
  3740. if (not isinstance(first_key, ast.Str)
  3741. or first_key.s != 'WATCHLIST_DEFINITIONS'
  3742. or not isinstance(first_value, ast.Dict)):
  3743. return ('The first entry of the dict in WATCHLISTS file must be '
  3744. 'WATCHLIST_DEFINITIONS dict')
  3745. if (not isinstance(second_key, ast.Str) or second_key.s != 'WATCHLISTS'
  3746. or not isinstance(second_value, ast.Dict)):
  3747. return ('The second entry of the dict in WATCHLISTS file must be '
  3748. 'WATCHLISTS dict')
  3749. return _CheckWATCHLISTSEntries(first_value, second_value, input_api)
  3750. def CheckWATCHLISTS(input_api, output_api):
  3751. for f in input_api.AffectedFiles(include_deletes=False):
  3752. if f.LocalPath() == 'WATCHLISTS':
  3753. contents = input_api.ReadFile(f, 'r')
  3754. try:
  3755. # First, make sure that it can be evaluated.
  3756. input_api.ast.literal_eval(contents)
  3757. # Get an AST tree for it and scan the tree for detailed style checking.
  3758. expression = input_api.ast.parse(contents,
  3759. filename='WATCHLISTS',
  3760. mode='eval')
  3761. except ValueError as e:
  3762. return [
  3763. output_api.PresubmitError('Cannot parse WATCHLISTS file',
  3764. long_text=repr(e))
  3765. ]
  3766. except SyntaxError as e:
  3767. return [
  3768. output_api.PresubmitError('Cannot parse WATCHLISTS file',
  3769. long_text=repr(e))
  3770. ]
  3771. except TypeError as e:
  3772. return [
  3773. output_api.PresubmitError('Cannot parse WATCHLISTS file',
  3774. long_text=repr(e))
  3775. ]
  3776. result = _CheckWATCHLISTSSyntax(expression, input_api)
  3777. if result is not None:
  3778. return [output_api.PresubmitError(result)]
  3779. break
  3780. return []
  3781. def CheckGnGlobForward(input_api, output_api):
  3782. """Checks that forward_variables_from(invoker, "*") follows best practices.
  3783. As documented at //build/docs/writing_gn_templates.md
  3784. """
  3785. def gn_files(f):
  3786. return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gni', ))
  3787. problems = []
  3788. for f in input_api.AffectedSourceFiles(gn_files):
  3789. for line_num, line in f.ChangedContents():
  3790. if 'forward_variables_from(invoker, "*")' in line:
  3791. problems.append(
  3792. 'Bare forward_variables_from(invoker, "*") in %s:%d' %
  3793. (f.LocalPath(), line_num))
  3794. if problems:
  3795. return [
  3796. output_api.PresubmitPromptWarning(
  3797. 'forward_variables_from("*") without exclusions',
  3798. items=sorted(problems),
  3799. long_text=(
  3800. 'The variables "visibility" and "test_only" should be '
  3801. 'explicitly listed in forward_variables_from(). For more '
  3802. 'details, see:\n'
  3803. 'https://chromium.googlesource.com/chromium/src/+/HEAD/'
  3804. 'build/docs/writing_gn_templates.md'
  3805. '#Using-forward_variables_from'))
  3806. ]
  3807. return []
  3808. def CheckNewHeaderWithoutGnChangeOnUpload(input_api, output_api):
  3809. """Checks that newly added header files have corresponding GN changes.
  3810. Note that this is only a heuristic. To be precise, run script:
  3811. build/check_gn_headers.py.
  3812. """
  3813. def headers(f):
  3814. return input_api.FilterSourceFile(
  3815. f, files_to_check=(r'.+%s' % _HEADER_EXTENSIONS, ))
  3816. new_headers = []
  3817. for f in input_api.AffectedSourceFiles(headers):
  3818. if f.Action() != 'A':
  3819. continue
  3820. new_headers.append(f.LocalPath())
  3821. def gn_files(f):
  3822. return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gn', ))
  3823. all_gn_changed_contents = ''
  3824. for f in input_api.AffectedSourceFiles(gn_files):
  3825. for _, line in f.ChangedContents():
  3826. all_gn_changed_contents += line
  3827. problems = []
  3828. for header in new_headers:
  3829. basename = input_api.os_path.basename(header)
  3830. if basename not in all_gn_changed_contents:
  3831. problems.append(header)
  3832. if problems:
  3833. return [
  3834. output_api.PresubmitPromptWarning(
  3835. 'Missing GN changes for new header files',
  3836. items=sorted(problems),
  3837. long_text=
  3838. 'Please double check whether newly added header files need '
  3839. 'corresponding changes in gn or gni files.\nThis checking is only a '
  3840. 'heuristic. Run build/check_gn_headers.py to be precise.\n'
  3841. 'Read https://crbug.com/661774 for more info.')
  3842. ]
  3843. return []
  3844. def CheckCorrectProductNameInMessages(input_api, output_api):
  3845. """Check that Chromium-branded strings don't include "Chrome" or vice versa.
  3846. This assumes we won't intentionally reference one product from the other
  3847. product.
  3848. """
  3849. all_problems = []
  3850. test_cases = [{
  3851. "filename_postfix": "google_chrome_strings.grd",
  3852. "correct_name": "Chrome",
  3853. "incorrect_name": "Chromium",
  3854. }, {
  3855. "filename_postfix": "chromium_strings.grd",
  3856. "correct_name": "Chromium",
  3857. "incorrect_name": "Chrome",
  3858. }]
  3859. for test_case in test_cases:
  3860. problems = []
  3861. filename_filter = lambda x: x.LocalPath().endswith(test_case[
  3862. "filename_postfix"])
  3863. # Check each new line. Can yield false positives in multiline comments, but
  3864. # easier than trying to parse the XML because messages can have nested
  3865. # children, and associating message elements with affected lines is hard.
  3866. for f in input_api.AffectedSourceFiles(filename_filter):
  3867. for line_num, line in f.ChangedContents():
  3868. if "<message" in line or "<!--" in line or "-->" in line:
  3869. continue
  3870. if test_case["incorrect_name"] in line:
  3871. problems.append("Incorrect product name in %s:%d" %
  3872. (f.LocalPath(), line_num))
  3873. if problems:
  3874. message = (
  3875. "Strings in %s-branded string files should reference \"%s\", not \"%s\""
  3876. % (test_case["correct_name"], test_case["correct_name"],
  3877. test_case["incorrect_name"]))
  3878. all_problems.append(
  3879. output_api.PresubmitPromptWarning(message, items=problems))
  3880. return all_problems
  3881. def CheckForTooLargeFiles(input_api, output_api):
  3882. """Avoid large files, especially binary files, in the repository since
  3883. git doesn't scale well for those. They will be in everyone's repo
  3884. clones forever, forever making Chromium slower to clone and work
  3885. with."""
  3886. # Uploading files to cloud storage is not trivial so we don't want
  3887. # to set the limit too low, but the upper limit for "normal" large
  3888. # files seems to be 1-2 MB, with a handful around 5-8 MB, so
  3889. # anything over 20 MB is exceptional.
  3890. TOO_LARGE_FILE_SIZE_LIMIT = 20 * 1024 * 1024 # 10 MB
  3891. too_large_files = []
  3892. for f in input_api.AffectedFiles():
  3893. # Check both added and modified files (but not deleted files).
  3894. if f.Action() in ('A', 'M'):
  3895. size = input_api.os_path.getsize(f.AbsoluteLocalPath())
  3896. if size > TOO_LARGE_FILE_SIZE_LIMIT:
  3897. too_large_files.append("%s: %d bytes" % (f.LocalPath(), size))
  3898. if too_large_files:
  3899. message = (
  3900. 'Do not commit large files to git since git scales badly for those.\n'
  3901. +
  3902. 'Instead put the large files in cloud storage and use DEPS to\n' +
  3903. 'fetch them.\n' + '\n'.join(too_large_files))
  3904. return [
  3905. output_api.PresubmitError('Too large files found in commit',
  3906. long_text=message + '\n')
  3907. ]
  3908. else:
  3909. return []
  3910. def CheckFuzzTargetsOnUpload(input_api, output_api):
  3911. """Checks specific for fuzz target sources."""
  3912. EXPORTED_SYMBOLS = [
  3913. 'LLVMFuzzerInitialize',
  3914. 'LLVMFuzzerCustomMutator',
  3915. 'LLVMFuzzerCustomCrossOver',
  3916. 'LLVMFuzzerMutate',
  3917. ]
  3918. REQUIRED_HEADER = '#include "testing/libfuzzer/libfuzzer_exports.h"'
  3919. def FilterFile(affected_file):
  3920. """Ignore libFuzzer source code."""
  3921. files_to_check = r'.*fuzz.*\.(h|hpp|hcc|cc|cpp|cxx)$'
  3922. files_to_skip = r"^third_party[\\/]libFuzzer"
  3923. return input_api.FilterSourceFile(affected_file,
  3924. files_to_check=[files_to_check],
  3925. files_to_skip=[files_to_skip])
  3926. files_with_missing_header = []
  3927. for f in input_api.AffectedSourceFiles(FilterFile):
  3928. contents = input_api.ReadFile(f, 'r')
  3929. if REQUIRED_HEADER in contents:
  3930. continue
  3931. if any(symbol in contents for symbol in EXPORTED_SYMBOLS):
  3932. files_with_missing_header.append(f.LocalPath())
  3933. if not files_with_missing_header:
  3934. return []
  3935. long_text = (
  3936. 'If you define any of the libFuzzer optional functions (%s), it is '
  3937. 'recommended to add \'%s\' directive. Otherwise, the fuzz target may '
  3938. 'work incorrectly on Mac (crbug.com/687076).\nNote that '
  3939. 'LLVMFuzzerInitialize should not be used, unless your fuzz target needs '
  3940. 'to access command line arguments passed to the fuzzer. Instead, prefer '
  3941. 'static initialization and shared resources as documented in '
  3942. 'https://chromium.googlesource.com/chromium/src/+/main/testing/'
  3943. 'libfuzzer/efficient_fuzzing.md#simplifying-initialization_cleanup.\n'
  3944. % (', '.join(EXPORTED_SYMBOLS), REQUIRED_HEADER))
  3945. return [
  3946. output_api.PresubmitPromptWarning(message="Missing '%s' in:" %
  3947. REQUIRED_HEADER,
  3948. items=files_with_missing_header,
  3949. long_text=long_text)
  3950. ]
  3951. def _CheckNewImagesWarning(input_api, output_api):
  3952. """
  3953. Warns authors who add images into the repo to make sure their images are
  3954. optimized before committing.
  3955. """
  3956. images_added = False
  3957. image_paths = []
  3958. errors = []
  3959. filter_lambda = lambda x: input_api.FilterSourceFile(
  3960. x,
  3961. files_to_skip=(('(?i).*test', r'.*\/junit\/') + input_api.
  3962. DEFAULT_FILES_TO_SKIP),
  3963. files_to_check=[r'.*\/(drawable|mipmap)'])
  3964. for f in input_api.AffectedFiles(include_deletes=False,
  3965. file_filter=filter_lambda):
  3966. local_path = f.LocalPath().lower()
  3967. if any(
  3968. local_path.endswith(extension)
  3969. for extension in _IMAGE_EXTENSIONS):
  3970. images_added = True
  3971. image_paths.append(f)
  3972. if images_added:
  3973. errors.append(
  3974. output_api.PresubmitPromptWarning(
  3975. 'It looks like you are trying to commit some images. If these are '
  3976. 'non-test-only images, please make sure to read and apply the tips in '
  3977. 'https://chromium.googlesource.com/chromium/src/+/HEAD/docs/speed/'
  3978. 'binary_size/optimization_advice.md#optimizing-images\nThis check is '
  3979. 'FYI only and will not block your CL on the CQ.', image_paths))
  3980. return errors
  3981. def ChecksAndroidSpecificOnUpload(input_api, output_api):
  3982. """Groups upload checks that target android code."""
  3983. results = []
  3984. results.extend(_CheckAndroidCrLogUsage(input_api, output_api))
  3985. results.extend(_CheckAndroidDebuggableBuild(input_api, output_api))
  3986. results.extend(_CheckAndroidNewMdpiAssetLocation(input_api, output_api))
  3987. results.extend(_CheckAndroidToastUsage(input_api, output_api))
  3988. results.extend(_CheckAndroidTestJUnitInheritance(input_api, output_api))
  3989. results.extend(_CheckAndroidTestJUnitFrameworkImport(
  3990. input_api, output_api))
  3991. results.extend(_CheckAndroidTestAnnotationUsage(input_api, output_api))
  3992. results.extend(_CheckAndroidWebkitImports(input_api, output_api))
  3993. results.extend(_CheckAndroidXmlStyle(input_api, output_api, True))
  3994. results.extend(_CheckNewImagesWarning(input_api, output_api))
  3995. results.extend(_CheckAndroidNoBannedImports(input_api, output_api))
  3996. results.extend(_CheckAndroidInfoBarDeprecation(input_api, output_api))
  3997. return results
  3998. def ChecksAndroidSpecificOnCommit(input_api, output_api):
  3999. """Groups commit checks that target android code."""
  4000. results = []
  4001. results.extend(_CheckAndroidXmlStyle(input_api, output_api, False))
  4002. return results
  4003. # TODO(chrishall): could we additionally match on any path owned by
  4004. # ui/accessibility/OWNERS ?
  4005. _ACCESSIBILITY_PATHS = (
  4006. r"^chrome[\\/]browser.*[\\/]accessibility[\\/]",
  4007. r"^chrome[\\/]browser[\\/]extensions[\\/]api[\\/]automation.*[\\/]",
  4008. r"^chrome[\\/]renderer[\\/]extensions[\\/]accessibility_.*",
  4009. r"^chrome[\\/]tests[\\/]data[\\/]accessibility[\\/]",
  4010. r"^content[\\/]browser[\\/]accessibility[\\/]",
  4011. r"^content[\\/]renderer[\\/]accessibility[\\/]",
  4012. r"^content[\\/]tests[\\/]data[\\/]accessibility[\\/]",
  4013. r"^extensions[\\/]renderer[\\/]api[\\/]automation[\\/]",
  4014. r"^ui[\\/]accessibility[\\/]",
  4015. r"^ui[\\/]views[\\/]accessibility[\\/]",
  4016. )
  4017. def CheckAccessibilityRelnotesField(input_api, output_api):
  4018. """Checks that commits to accessibility code contain an AX-Relnotes field in
  4019. their commit message."""
  4020. def FileFilter(affected_file):
  4021. paths = _ACCESSIBILITY_PATHS
  4022. return input_api.FilterSourceFile(affected_file, files_to_check=paths)
  4023. # Only consider changes affecting accessibility paths.
  4024. if not any(input_api.AffectedFiles(file_filter=FileFilter)):
  4025. return []
  4026. # AX-Relnotes can appear in either the description or the footer.
  4027. # When searching the description, require 'AX-Relnotes:' to appear at the
  4028. # beginning of a line.
  4029. ax_regex = input_api.re.compile('ax-relnotes[:=]')
  4030. description_has_relnotes = any(
  4031. ax_regex.match(line)
  4032. for line in input_api.change.DescriptionText().lower().splitlines())
  4033. footer_relnotes = input_api.change.GitFootersFromDescription().get(
  4034. 'AX-Relnotes', [])
  4035. if description_has_relnotes or footer_relnotes:
  4036. return []
  4037. # TODO(chrishall): link to Relnotes documentation in message.
  4038. message = (
  4039. "Missing 'AX-Relnotes:' field required for accessibility changes"
  4040. "\n please add 'AX-Relnotes: [release notes].' to describe any "
  4041. "user-facing changes"
  4042. "\n otherwise add 'AX-Relnotes: n/a.' if this change has no "
  4043. "user-facing effects"
  4044. "\n if this is confusing or annoying then please contact members "
  4045. "of ui/accessibility/OWNERS.")
  4046. return [output_api.PresubmitNotifyResult(message)]
  4047. _ACCESSIBILITY_EVENTS_TEST_PATH = (
  4048. r"^content[\\/]test[\\/]data[\\/]accessibility[\\/]event[\\/].*\.html",
  4049. )
  4050. _ACCESSIBILITY_TREE_TEST_PATH = (
  4051. r"^content[\\/]test[\\/]data[\\/]accessibility[\\/]accname[\\/].*\.html",
  4052. r"^content[\\/]test[\\/]data[\\/]accessibility[\\/]aria[\\/].*\.html",
  4053. r"^content[\\/]test[\\/]data[\\/]accessibility[\\/]css[\\/].*\.html",
  4054. r"^content[\\/]test[\\/]data[\\/]accessibility[\\/]html[\\/].*\.html",
  4055. )
  4056. _ACCESSIBILITY_ANDROID_EVENTS_TEST_PATH = (
  4057. r"^.*[\\/]WebContentsAccessibilityEventsTest\.java",
  4058. )
  4059. _ACCESSIBILITY_ANDROID_TREE_TEST_PATH = (
  4060. r"^.*[\\/]WebContentsAccessibilityTreeTest\.java",
  4061. )
  4062. def CheckAccessibilityEventsTestsAreIncludedForAndroid(input_api, output_api):
  4063. """Checks that commits that include a newly added, renamed/moved, or deleted
  4064. test in the DumpAccessibilityEventsTest suite also includes a corresponding
  4065. change to the Android test."""
  4066. def FilePathFilter(affected_file):
  4067. paths = _ACCESSIBILITY_EVENTS_TEST_PATH
  4068. return input_api.FilterSourceFile(affected_file, files_to_check=paths)
  4069. def AndroidFilePathFilter(affected_file):
  4070. paths = _ACCESSIBILITY_ANDROID_EVENTS_TEST_PATH
  4071. return input_api.FilterSourceFile(affected_file, files_to_check=paths)
  4072. # Only consider changes in the events test data path with html type.
  4073. if not any(
  4074. input_api.AffectedFiles(include_deletes=True,
  4075. file_filter=FilePathFilter)):
  4076. return []
  4077. # If the commit contains any change to the Android test file, ignore.
  4078. if any(
  4079. input_api.AffectedFiles(include_deletes=True,
  4080. file_filter=AndroidFilePathFilter)):
  4081. return []
  4082. # Only consider changes that are adding/renaming or deleting a file
  4083. message = []
  4084. for f in input_api.AffectedFiles(include_deletes=True,
  4085. file_filter=FilePathFilter):
  4086. if f.Action() == 'A' or f.Action() == 'D':
  4087. message = (
  4088. "It appears that you are adding, renaming or deleting"
  4089. "\na dump_accessibility_events* test, but have not included"
  4090. "\na corresponding change for Android."
  4091. "\nPlease include (or remove) the test from:"
  4092. "\n content/public/android/javatests/src/org/chromium/"
  4093. "content/browser/accessibility/"
  4094. "WebContentsAccessibilityEventsTest.java"
  4095. "\nIf this message is confusing or annoying, please contact"
  4096. "\nmembers of ui/accessibility/OWNERS.")
  4097. # If no message was set, return empty.
  4098. if not len(message):
  4099. return []
  4100. return [output_api.PresubmitPromptWarning(message)]
  4101. def CheckAccessibilityTreeTestsAreIncludedForAndroid(input_api, output_api):
  4102. """Checks that commits that include a newly added, renamed/moved, or deleted
  4103. test in the DumpAccessibilityTreeTest suite also includes a corresponding
  4104. change to the Android test."""
  4105. def FilePathFilter(affected_file):
  4106. paths = _ACCESSIBILITY_TREE_TEST_PATH
  4107. return input_api.FilterSourceFile(affected_file, files_to_check=paths)
  4108. def AndroidFilePathFilter(affected_file):
  4109. paths = _ACCESSIBILITY_ANDROID_TREE_TEST_PATH
  4110. return input_api.FilterSourceFile(affected_file, files_to_check=paths)
  4111. # Only consider changes in the various tree test data paths with html type.
  4112. if not any(
  4113. input_api.AffectedFiles(include_deletes=True,
  4114. file_filter=FilePathFilter)):
  4115. return []
  4116. # If the commit contains any change to the Android test file, ignore.
  4117. if any(
  4118. input_api.AffectedFiles(include_deletes=True,
  4119. file_filter=AndroidFilePathFilter)):
  4120. return []
  4121. # Only consider changes that are adding/renaming or deleting a file
  4122. message = []
  4123. for f in input_api.AffectedFiles(include_deletes=True,
  4124. file_filter=FilePathFilter):
  4125. if f.Action() == 'A' or f.Action() == 'D':
  4126. message = (
  4127. "It appears that you are adding, renaming or deleting"
  4128. "\na dump_accessibility_tree* test, but have not included"
  4129. "\na corresponding change for Android."
  4130. "\nPlease include (or remove) the test from:"
  4131. "\n content/public/android/javatests/src/org/chromium/"
  4132. "content/browser/accessibility/"
  4133. "WebContentsAccessibilityTreeTest.java"
  4134. "\nIf this message is confusing or annoying, please contact"
  4135. "\nmembers of ui/accessibility/OWNERS.")
  4136. # If no message was set, return empty.
  4137. if not len(message):
  4138. return []
  4139. return [output_api.PresubmitPromptWarning(message)]
  4140. # string pattern, sequence of strings to show when pattern matches,
  4141. # error flag. True if match is a presubmit error, otherwise it's a warning.
  4142. _NON_INCLUSIVE_TERMS = (
  4143. (
  4144. # Note that \b pattern in python re is pretty particular. In this
  4145. # regexp, 'class WhiteList ...' will match, but 'class FooWhiteList
  4146. # ...' will not. This may require some tweaking to catch these cases
  4147. # without triggering a lot of false positives. Leaving it naive and
  4148. # less matchy for now.
  4149. r'/\b(?i)((black|white)list|master|slave)\b', # nocheck
  4150. (
  4151. 'Please don\'t use blacklist, whitelist, ' # nocheck
  4152. 'or slave in your', # nocheck
  4153. 'code and make every effort to use other terms. Using "// nocheck"',
  4154. '"# nocheck" or "<!-- nocheck -->"',
  4155. 'at the end of the offending line will bypass this PRESUBMIT error',
  4156. 'but avoid using this whenever possible. Reach out to',
  4157. 'community@chromium.org if you have questions'),
  4158. True),)
  4159. def ChecksCommon(input_api, output_api):
  4160. """Checks common to both upload and commit."""
  4161. results = []
  4162. results.extend(
  4163. input_api.canned_checks.PanProjectChecks(
  4164. input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
  4165. author = input_api.change.author_email
  4166. if author and author not in _KNOWN_ROBOTS:
  4167. results.extend(
  4168. input_api.canned_checks.CheckAuthorizedAuthor(
  4169. input_api, output_api))
  4170. results.extend(
  4171. input_api.canned_checks.CheckChangeHasNoTabs(
  4172. input_api,
  4173. output_api,
  4174. source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
  4175. results.extend(
  4176. input_api.RunTests(
  4177. input_api.canned_checks.CheckVPythonSpec(input_api, output_api)))
  4178. dirmd = 'dirmd.bat' if input_api.is_windows else 'dirmd'
  4179. dirmd_bin = input_api.os_path.join(input_api.PresubmitLocalPath(),
  4180. 'third_party', 'depot_tools', dirmd)
  4181. results.extend(
  4182. input_api.RunTests(
  4183. input_api.canned_checks.CheckDirMetadataFormat(
  4184. input_api, output_api, dirmd_bin)))
  4185. results.extend(
  4186. input_api.canned_checks.CheckOwnersDirMetadataExclusive(
  4187. input_api, output_api))
  4188. results.extend(
  4189. input_api.canned_checks.CheckNoNewMetadataInOwners(
  4190. input_api, output_api))
  4191. results.extend(
  4192. input_api.canned_checks.CheckInclusiveLanguage(
  4193. input_api,
  4194. output_api,
  4195. excluded_directories_relative_path=[
  4196. 'infra', 'inclusive_language_presubmit_exempt_dirs.txt'
  4197. ],
  4198. non_inclusive_terms=_NON_INCLUSIVE_TERMS))
  4199. presubmit_py_filter = lambda f: input_api.FilterSourceFile(
  4200. f, files_to_check=[r'PRESUBMIT\.py$'])
  4201. for f in input_api.AffectedFiles(include_deletes=False,
  4202. file_filter=presubmit_py_filter):
  4203. full_path = input_api.os_path.dirname(f.AbsoluteLocalPath())
  4204. test_file = input_api.os_path.join(full_path, 'PRESUBMIT_test.py')
  4205. # The PRESUBMIT.py file (and the directory containing it) might have
  4206. # been affected by being moved or removed, so only try to run the tests
  4207. # if they still exist.
  4208. if not input_api.os_path.exists(test_file):
  4209. continue
  4210. use_python3 = False
  4211. with open(f.LocalPath()) as fp:
  4212. use_python3 = any(
  4213. line.startswith('USE_PYTHON3 = True')
  4214. for line in fp.readlines())
  4215. results.extend(
  4216. input_api.canned_checks.RunUnitTestsInDirectory(
  4217. input_api,
  4218. output_api,
  4219. full_path,
  4220. files_to_check=[r'^PRESUBMIT_test\.py$'],
  4221. run_on_python2=not use_python3,
  4222. run_on_python3=use_python3,
  4223. skip_shebang_check=True))
  4224. return results
  4225. def CheckPatchFiles(input_api, output_api):
  4226. problems = [
  4227. f.LocalPath() for f in input_api.AffectedFiles()
  4228. if f.LocalPath().endswith(('.orig', '.rej'))
  4229. ]
  4230. # Cargo.toml.orig files are part of third-party crates downloaded from
  4231. # crates.io and should be included.
  4232. problems = [f for f in problems if not f.endswith('Cargo.toml.orig')]
  4233. if problems:
  4234. return [
  4235. output_api.PresubmitError("Don't commit .rej and .orig files.",
  4236. problems)
  4237. ]
  4238. else:
  4239. return []
  4240. def CheckBuildConfigMacrosWithoutInclude(input_api, output_api):
  4241. # Excludes OS_CHROMEOS, which is not defined in build_config.h.
  4242. macro_re = input_api.re.compile(
  4243. r'^\s*#(el)?if.*\bdefined\(((COMPILER_|ARCH_CPU_|WCHAR_T_IS_)[^)]*)')
  4244. include_re = input_api.re.compile(r'^#include\s+"build/build_config.h"',
  4245. input_api.re.MULTILINE)
  4246. extension_re = input_api.re.compile(r'\.[a-z]+$')
  4247. errors = []
  4248. config_h_file = input_api.os_path.join('build', 'build_config.h')
  4249. for f in input_api.AffectedFiles(include_deletes=False):
  4250. # The build-config macros are allowed to be used in build_config.h
  4251. # without including itself.
  4252. if f.LocalPath() == config_h_file:
  4253. continue
  4254. if not f.LocalPath().endswith(
  4255. ('.h', '.c', '.cc', '.cpp', '.m', '.mm')):
  4256. continue
  4257. found_line_number = None
  4258. found_macro = None
  4259. all_lines = input_api.ReadFile(f, 'r').splitlines()
  4260. for line_num, line in enumerate(all_lines):
  4261. match = macro_re.search(line)
  4262. if match:
  4263. found_line_number = line_num
  4264. found_macro = match.group(2)
  4265. break
  4266. if not found_line_number:
  4267. continue
  4268. found_include_line = -1
  4269. for line_num, line in enumerate(all_lines):
  4270. if include_re.search(line):
  4271. found_include_line = line_num
  4272. break
  4273. if found_include_line >= 0 and found_include_line < found_line_number:
  4274. continue
  4275. if not f.LocalPath().endswith('.h'):
  4276. primary_header_path = extension_re.sub('.h', f.AbsoluteLocalPath())
  4277. try:
  4278. content = input_api.ReadFile(primary_header_path, 'r')
  4279. if include_re.search(content):
  4280. continue
  4281. except IOError:
  4282. pass
  4283. errors.append('%s:%d %s macro is used without first including build/'
  4284. 'build_config.h.' %
  4285. (f.LocalPath(), found_line_number, found_macro))
  4286. if errors:
  4287. return [output_api.PresubmitPromptWarning('\n'.join(errors))]
  4288. return []
  4289. def CheckForSuperfluousStlIncludesInHeaders(input_api, output_api):
  4290. stl_include_re = input_api.re.compile(r'^#include\s+<('
  4291. r'algorithm|'
  4292. r'array|'
  4293. r'limits|'
  4294. r'list|'
  4295. r'map|'
  4296. r'memory|'
  4297. r'queue|'
  4298. r'set|'
  4299. r'string|'
  4300. r'unordered_map|'
  4301. r'unordered_set|'
  4302. r'utility|'
  4303. r'vector)>')
  4304. std_namespace_re = input_api.re.compile(r'std::')
  4305. errors = []
  4306. for f in input_api.AffectedFiles():
  4307. if not _IsCPlusPlusHeaderFile(input_api, f.LocalPath()):
  4308. continue
  4309. uses_std_namespace = False
  4310. has_stl_include = False
  4311. for line in f.NewContents():
  4312. if has_stl_include and uses_std_namespace:
  4313. break
  4314. if not has_stl_include and stl_include_re.search(line):
  4315. has_stl_include = True
  4316. continue
  4317. if not uses_std_namespace and (std_namespace_re.search(line)
  4318. or 'no-std-usage-because-pch-file' in line):
  4319. uses_std_namespace = True
  4320. continue
  4321. if has_stl_include and not uses_std_namespace:
  4322. errors.append(
  4323. '%s: Includes STL header(s) but does not reference std::' %
  4324. f.LocalPath())
  4325. if errors:
  4326. return [output_api.PresubmitPromptWarning('\n'.join(errors))]
  4327. return []
  4328. def _CheckForDeprecatedOSMacrosInFile(input_api, f):
  4329. """Check for sensible looking, totally invalid OS macros."""
  4330. preprocessor_statement = input_api.re.compile(r'^\s*#')
  4331. os_macro = input_api.re.compile(r'defined\(OS_([^)]+)\)')
  4332. results = []
  4333. for lnum, line in f.ChangedContents():
  4334. if preprocessor_statement.search(line):
  4335. for match in os_macro.finditer(line):
  4336. results.append(
  4337. ' %s:%d: %s' %
  4338. (f.LocalPath(), lnum, 'defined(OS_' + match.group(1) +
  4339. ') -> BUILDFLAG(IS_' + match.group(1) + ')'))
  4340. return results
  4341. def CheckForDeprecatedOSMacros(input_api, output_api):
  4342. """Check all affected files for invalid OS macros."""
  4343. bad_macros = []
  4344. # The OS_ macros are allowed to be used in build/build_config.h.
  4345. config_h_file = input_api.os_path.join('build', 'build_config.h')
  4346. for f in input_api.AffectedSourceFiles(None):
  4347. if not f.LocalPath().endswith(('.py', '.js', '.html', '.css', '.md')) \
  4348. and f.LocalPath() != config_h_file:
  4349. bad_macros.extend(_CheckForDeprecatedOSMacrosInFile(input_api, f))
  4350. if not bad_macros:
  4351. return []
  4352. return [
  4353. output_api.PresubmitError(
  4354. 'OS macros have been deprecated. Please use BUILDFLAGs instead (still '
  4355. 'defined in build_config.h):', bad_macros)
  4356. ]
  4357. def _CheckForInvalidIfDefinedMacrosInFile(input_api, f):
  4358. """Check all affected files for invalid "if defined" macros."""
  4359. ALWAYS_DEFINED_MACROS = (
  4360. "TARGET_CPU_PPC",
  4361. "TARGET_CPU_PPC64",
  4362. "TARGET_CPU_68K",
  4363. "TARGET_CPU_X86",
  4364. "TARGET_CPU_ARM",
  4365. "TARGET_CPU_MIPS",
  4366. "TARGET_CPU_SPARC",
  4367. "TARGET_CPU_ALPHA",
  4368. "TARGET_IPHONE_SIMULATOR",
  4369. "TARGET_OS_EMBEDDED",
  4370. "TARGET_OS_IPHONE",
  4371. "TARGET_OS_MAC",
  4372. "TARGET_OS_UNIX",
  4373. "TARGET_OS_WIN32",
  4374. )
  4375. ifdef_macro = input_api.re.compile(
  4376. r'^\s*#.*(?:ifdef\s|defined\()([^\s\)]+)')
  4377. results = []
  4378. for lnum, line in f.ChangedContents():
  4379. for match in ifdef_macro.finditer(line):
  4380. if match.group(1) in ALWAYS_DEFINED_MACROS:
  4381. always_defined = ' %s is always defined. ' % match.group(1)
  4382. did_you_mean = 'Did you mean \'#if %s\'?' % match.group(1)
  4383. results.append(
  4384. ' %s:%d %s\n\t%s' %
  4385. (f.LocalPath(), lnum, always_defined, did_you_mean))
  4386. return results
  4387. def CheckForInvalidIfDefinedMacros(input_api, output_api):
  4388. """Check all affected files for invalid "if defined" macros."""
  4389. bad_macros = []
  4390. skipped_paths = ['third_party/sqlite/', 'third_party/abseil-cpp/']
  4391. for f in input_api.AffectedFiles():
  4392. if any([f.LocalPath().startswith(path) for path in skipped_paths]):
  4393. continue
  4394. if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')):
  4395. bad_macros.extend(
  4396. _CheckForInvalidIfDefinedMacrosInFile(input_api, f))
  4397. if not bad_macros:
  4398. return []
  4399. return [
  4400. output_api.PresubmitError(
  4401. 'Found ifdef check on always-defined macro[s]. Please fix your code\n'
  4402. 'or check the list of ALWAYS_DEFINED_MACROS in src/PRESUBMIT.py.',
  4403. bad_macros)
  4404. ]
  4405. def CheckForIPCRules(input_api, output_api):
  4406. """Check for same IPC rules described in
  4407. http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc
  4408. """
  4409. base_pattern = r'IPC_ENUM_TRAITS\('
  4410. inclusion_pattern = input_api.re.compile(r'(%s)' % base_pattern)
  4411. comment_pattern = input_api.re.compile(r'//.*(%s)' % base_pattern)
  4412. problems = []
  4413. for f in input_api.AffectedSourceFiles(None):
  4414. local_path = f.LocalPath()
  4415. if not local_path.endswith('.h'):
  4416. continue
  4417. for line_number, line in f.ChangedContents():
  4418. if inclusion_pattern.search(
  4419. line) and not comment_pattern.search(line):
  4420. problems.append('%s:%d\n %s' %
  4421. (local_path, line_number, line.strip()))
  4422. if problems:
  4423. return [
  4424. output_api.PresubmitPromptWarning(_IPC_ENUM_TRAITS_DEPRECATED,
  4425. problems)
  4426. ]
  4427. else:
  4428. return []
  4429. def CheckForLongPathnames(input_api, output_api):
  4430. """Check to make sure no files being submitted have long paths.
  4431. This causes issues on Windows.
  4432. """
  4433. problems = []
  4434. for f in input_api.AffectedTestableFiles():
  4435. local_path = f.LocalPath()
  4436. # Windows has a path limit of 260 characters. Limit path length to 200 so
  4437. # that we have some extra for the prefix on dev machines and the bots.
  4438. if len(local_path) > 200:
  4439. problems.append(local_path)
  4440. if problems:
  4441. return [output_api.PresubmitError(_LONG_PATH_ERROR, problems)]
  4442. else:
  4443. return []
  4444. def CheckForIncludeGuards(input_api, output_api):
  4445. """Check that header files have proper guards against multiple inclusion.
  4446. If a file should not have such guards (and it probably should) then it
  4447. should include the string "no-include-guard-because-multiply-included" or
  4448. "no-include-guard-because-pch-file".
  4449. """
  4450. def is_chromium_header_file(f):
  4451. # We only check header files under the control of the Chromium
  4452. # project. That is, those outside third_party apart from
  4453. # third_party/blink.
  4454. # We also exclude *_message_generator.h headers as they use
  4455. # include guards in a special, non-typical way.
  4456. file_with_path = input_api.os_path.normpath(f.LocalPath())
  4457. return (file_with_path.endswith('.h')
  4458. and not file_with_path.endswith('_message_generator.h')
  4459. and not file_with_path.endswith('com_imported_mstscax.h')
  4460. and (not file_with_path.startswith('third_party')
  4461. or file_with_path.startswith(
  4462. input_api.os_path.join('third_party', 'blink'))))
  4463. def replace_special_with_underscore(string):
  4464. return input_api.re.sub(r'[+\\/.-]', '_', string)
  4465. errors = []
  4466. for f in input_api.AffectedSourceFiles(is_chromium_header_file):
  4467. guard_name = None
  4468. guard_line_number = None
  4469. seen_guard_end = False
  4470. file_with_path = input_api.os_path.normpath(f.LocalPath())
  4471. base_file_name = input_api.os_path.splitext(
  4472. input_api.os_path.basename(file_with_path))[0]
  4473. upper_base_file_name = base_file_name.upper()
  4474. expected_guard = replace_special_with_underscore(
  4475. file_with_path.upper() + '_')
  4476. # For "path/elem/file_name.h" we should really only accept
  4477. # PATH_ELEM_FILE_NAME_H_ per coding style. Unfortunately there
  4478. # are too many (1000+) files with slight deviations from the
  4479. # coding style. The most important part is that the include guard
  4480. # is there, and that it's unique, not the name so this check is
  4481. # forgiving for existing files.
  4482. #
  4483. # As code becomes more uniform, this could be made stricter.
  4484. guard_name_pattern_list = [
  4485. # Anything with the right suffix (maybe with an extra _).
  4486. r'\w+_H__?',
  4487. # To cover include guards with old Blink style.
  4488. r'\w+_h',
  4489. # Anything including the uppercase name of the file.
  4490. r'\w*' + input_api.re.escape(
  4491. replace_special_with_underscore(upper_base_file_name)) +
  4492. r'\w*',
  4493. ]
  4494. guard_name_pattern = '|'.join(guard_name_pattern_list)
  4495. guard_pattern = input_api.re.compile(r'#ifndef\s+(' +
  4496. guard_name_pattern + ')')
  4497. for line_number, line in enumerate(f.NewContents()):
  4498. if ('no-include-guard-because-multiply-included' in line
  4499. or 'no-include-guard-because-pch-file' in line):
  4500. guard_name = 'DUMMY' # To not trigger check outside the loop.
  4501. break
  4502. if guard_name is None:
  4503. match = guard_pattern.match(line)
  4504. if match:
  4505. guard_name = match.group(1)
  4506. guard_line_number = line_number
  4507. # We allow existing files to use include guards whose names
  4508. # don't match the chromium style guide, but new files should
  4509. # get it right.
  4510. if guard_name != expected_guard:
  4511. if not f.OldContents():
  4512. errors.append(
  4513. output_api.PresubmitPromptWarning(
  4514. 'Header using the wrong include guard name %s'
  4515. % guard_name, [
  4516. '%s:%d' %
  4517. (f.LocalPath(), line_number + 1)
  4518. ], 'Expected: %r\nFound: %r' %
  4519. (expected_guard, guard_name)))
  4520. else:
  4521. # The line after #ifndef should have a #define of the same name.
  4522. if line_number == guard_line_number + 1:
  4523. expected_line = '#define %s' % guard_name
  4524. if line != expected_line:
  4525. errors.append(
  4526. output_api.PresubmitPromptWarning(
  4527. 'Missing "%s" for include guard' %
  4528. expected_line,
  4529. ['%s:%d' % (f.LocalPath(), line_number + 1)],
  4530. 'Expected: %r\nGot: %r' %
  4531. (expected_line, line)))
  4532. if not seen_guard_end and line == '#endif // %s' % guard_name:
  4533. seen_guard_end = True
  4534. elif seen_guard_end:
  4535. if line.strip() != '':
  4536. errors.append(
  4537. output_api.PresubmitPromptWarning(
  4538. 'Include guard %s not covering the whole file'
  4539. % (guard_name), [f.LocalPath()]))
  4540. break # Nothing else to check and enough to warn once.
  4541. if guard_name is None:
  4542. errors.append(
  4543. output_api.PresubmitPromptWarning(
  4544. 'Missing include guard in %s\n'
  4545. 'Recommended name: %s\n'
  4546. 'This check can be disabled by having the string\n'
  4547. '"no-include-guard-because-multiply-included" or\n'
  4548. '"no-include-guard-because-pch-file" in the header.'
  4549. % (f.LocalPath(), expected_guard)))
  4550. return errors
  4551. def CheckForWindowsLineEndings(input_api, output_api):
  4552. """Check source code and known ascii text files for Windows style line
  4553. endings.
  4554. """
  4555. known_text_files = r'.*\.(txt|html|htm|py|gyp|gypi|gn|isolate|icon)$'
  4556. file_inclusion_pattern = (known_text_files,
  4557. r'.+%s' % _IMPLEMENTATION_EXTENSIONS,
  4558. r'.+%s' % _HEADER_EXTENSIONS)
  4559. problems = []
  4560. source_file_filter = lambda f: input_api.FilterSourceFile(
  4561. f, files_to_check=file_inclusion_pattern, files_to_skip=None)
  4562. for f in input_api.AffectedSourceFiles(source_file_filter):
  4563. # Ignore test files that contain crlf intentionally.
  4564. if f.LocalPath().endswith('crlf.txt'):
  4565. continue
  4566. include_file = False
  4567. for line in input_api.ReadFile(f, 'r').splitlines(True):
  4568. if line.endswith('\r\n'):
  4569. include_file = True
  4570. if include_file:
  4571. problems.append(f.LocalPath())
  4572. if problems:
  4573. return [
  4574. output_api.PresubmitPromptWarning(
  4575. 'Are you sure that you want '
  4576. 'these files to contain Windows style line endings?\n' +
  4577. '\n'.join(problems))
  4578. ]
  4579. return []
  4580. def CheckIconFilesForLicenseHeaders(input_api, output_api):
  4581. """Check that .icon files (which are fragments of C++) have license headers.
  4582. """
  4583. icon_files = (r'.*\.icon$', )
  4584. icons = lambda x: input_api.FilterSourceFile(x, files_to_check=icon_files)
  4585. return input_api.canned_checks.CheckLicense(input_api,
  4586. output_api,
  4587. source_file_filter=icons)
  4588. def CheckForUseOfChromeAppsDeprecations(input_api, output_api):
  4589. """Check source code for use of Chrome App technologies being
  4590. deprecated.
  4591. """
  4592. def _CheckForDeprecatedTech(input_api,
  4593. output_api,
  4594. detection_list,
  4595. files_to_check=None,
  4596. files_to_skip=None):
  4597. if (files_to_check or files_to_skip):
  4598. source_file_filter = lambda f: input_api.FilterSourceFile(
  4599. f, files_to_check=files_to_check, files_to_skip=files_to_skip)
  4600. else:
  4601. source_file_filter = None
  4602. problems = []
  4603. for f in input_api.AffectedSourceFiles(source_file_filter):
  4604. if f.Action() == 'D':
  4605. continue
  4606. for _, line in f.ChangedContents():
  4607. if any(detect in line for detect in detection_list):
  4608. problems.append(f.LocalPath())
  4609. return problems
  4610. # to avoid this presubmit script triggering warnings
  4611. files_to_skip = ['PRESUBMIT.py', 'PRESUBMIT_test.py']
  4612. problems = []
  4613. # NMF: any files with extensions .nmf or NMF
  4614. _NMF_FILES = r'\.(nmf|NMF)$'
  4615. problems += _CheckForDeprecatedTech(
  4616. input_api,
  4617. output_api,
  4618. detection_list=[''], # any change to the file will trigger warning
  4619. files_to_check=[r'.+%s' % _NMF_FILES])
  4620. # MANIFEST: any manifest.json that in its diff includes "app":
  4621. _MANIFEST_FILES = r'(manifest\.json)$'
  4622. problems += _CheckForDeprecatedTech(
  4623. input_api,
  4624. output_api,
  4625. detection_list=['"app":'],
  4626. files_to_check=[r'.*%s' % _MANIFEST_FILES])
  4627. # NaCl / PNaCl: any file that in its diff contains the strings in the list
  4628. problems += _CheckForDeprecatedTech(
  4629. input_api,
  4630. output_api,
  4631. detection_list=['config=nacl', 'enable-nacl', 'cpu=pnacl', 'nacl_io'],
  4632. files_to_skip=files_to_skip + [r"^native_client_sdk[\\/]"])
  4633. # PPAPI: any C/C++ file that in its diff includes a ppapi library
  4634. problems += _CheckForDeprecatedTech(
  4635. input_api,
  4636. output_api,
  4637. detection_list=['#include "ppapi', '#include <ppapi'],
  4638. files_to_check=(r'.+%s' % _HEADER_EXTENSIONS,
  4639. r'.+%s' % _IMPLEMENTATION_EXTENSIONS),
  4640. files_to_skip=[r"^ppapi[\\/]"])
  4641. if problems:
  4642. return [
  4643. output_api.PresubmitPromptWarning(
  4644. 'You are adding/modifying code'
  4645. 'related to technologies which will soon be deprecated (Chrome Apps, NaCl,'
  4646. ' PNaCl, PPAPI). See this blog post for more details:\n'
  4647. 'https://blog.chromium.org/2020/08/changes-to-chrome-app-support-timeline.html\n'
  4648. 'and this documentation for options to replace these technologies:\n'
  4649. 'https://developer.chrome.com/docs/apps/migration/\n' +
  4650. '\n'.join(problems))
  4651. ]
  4652. return []
  4653. def CheckSyslogUseWarningOnUpload(input_api, output_api, src_file_filter=None):
  4654. """Checks that all source files use SYSLOG properly."""
  4655. syslog_files = []
  4656. for f in input_api.AffectedSourceFiles(src_file_filter):
  4657. for line_number, line in f.ChangedContents():
  4658. if 'SYSLOG' in line:
  4659. syslog_files.append(f.LocalPath() + ':' + str(line_number))
  4660. if syslog_files:
  4661. return [
  4662. output_api.PresubmitPromptWarning(
  4663. 'Please make sure there are no privacy sensitive bits of data in SYSLOG'
  4664. ' calls.\nFiles to check:\n',
  4665. items=syslog_files)
  4666. ]
  4667. return []
  4668. def CheckChangeOnUpload(input_api, output_api):
  4669. if input_api.version < [2, 0, 0]:
  4670. return [
  4671. output_api.PresubmitError(
  4672. "Your depot_tools is out of date. "
  4673. "This PRESUBMIT.py requires at least presubmit_support version 2.0.0, "
  4674. "but your version is %d.%d.%d" % tuple(input_api.version))
  4675. ]
  4676. results = []
  4677. results.extend(
  4678. input_api.canned_checks.CheckPatchFormatted(input_api, output_api))
  4679. return results
  4680. def CheckChangeOnCommit(input_api, output_api):
  4681. if input_api.version < [2, 0, 0]:
  4682. return [
  4683. output_api.PresubmitError(
  4684. "Your depot_tools is out of date. "
  4685. "This PRESUBMIT.py requires at least presubmit_support version 2.0.0, "
  4686. "but your version is %d.%d.%d" % tuple(input_api.version))
  4687. ]
  4688. results = []
  4689. # Make sure the tree is 'open'.
  4690. results.extend(
  4691. input_api.canned_checks.CheckTreeIsOpen(
  4692. input_api,
  4693. output_api,
  4694. json_url='http://chromium-status.appspot.com/current?format=json'))
  4695. results.extend(
  4696. input_api.canned_checks.CheckPatchFormatted(input_api, output_api))
  4697. results.extend(
  4698. input_api.canned_checks.CheckChangeHasBugField(input_api, output_api))
  4699. results.extend(
  4700. input_api.canned_checks.CheckChangeHasNoUnwantedTags(
  4701. input_api, output_api))
  4702. return results
  4703. def CheckStrings(input_api, output_api):
  4704. """Check string ICU syntax validity and if translation screenshots exist."""
  4705. # Skip translation screenshots check if a SkipTranslationScreenshotsCheck
  4706. # footer is set to true.
  4707. git_footers = input_api.change.GitFootersFromDescription()
  4708. skip_screenshot_check_footer = [
  4709. footer.lower() for footer in git_footers.get(
  4710. u'Skip-Translation-Screenshots-Check', [])
  4711. ]
  4712. run_screenshot_check = u'true' not in skip_screenshot_check_footer
  4713. import os
  4714. import re
  4715. import sys
  4716. from io import StringIO
  4717. new_or_added_paths = set(f.LocalPath() for f in input_api.AffectedFiles()
  4718. if (f.Action() == 'A' or f.Action() == 'M'))
  4719. removed_paths = set(f.LocalPath()
  4720. for f in input_api.AffectedFiles(include_deletes=True)
  4721. if f.Action() == 'D')
  4722. affected_grds = [
  4723. f for f in input_api.AffectedFiles()
  4724. if f.LocalPath().endswith(('.grd', '.grdp'))
  4725. ]
  4726. affected_grds = [
  4727. f for f in affected_grds if not 'testdata' in f.LocalPath()
  4728. ]
  4729. if not affected_grds:
  4730. return []
  4731. affected_png_paths = [
  4732. f.AbsoluteLocalPath() for f in input_api.AffectedFiles()
  4733. if (f.LocalPath().endswith('.png'))
  4734. ]
  4735. # Check for screenshots. Developers can upload screenshots using
  4736. # tools/translation/upload_screenshots.py which finds and uploads
  4737. # images associated with .grd files (e.g. test_grd/IDS_STRING.png for the
  4738. # message named IDS_STRING in test.grd) and produces a .sha1 file (e.g.
  4739. # test_grd/IDS_STRING.png.sha1) for each png when the upload is successful.
  4740. #
  4741. # The logic here is as follows:
  4742. #
  4743. # - If the CL has a .png file under the screenshots directory for a grd
  4744. # file, warn the developer. Actual images should never be checked into the
  4745. # Chrome repo.
  4746. #
  4747. # - If the CL contains modified or new messages in grd files and doesn't
  4748. # contain the corresponding .sha1 files, warn the developer to add images
  4749. # and upload them via tools/translation/upload_screenshots.py.
  4750. #
  4751. # - If the CL contains modified or new messages in grd files and the
  4752. # corresponding .sha1 files, everything looks good.
  4753. #
  4754. # - If the CL contains removed messages in grd files but the corresponding
  4755. # .sha1 files aren't removed, warn the developer to remove them.
  4756. unnecessary_screenshots = []
  4757. missing_sha1 = []
  4758. unnecessary_sha1_files = []
  4759. # This checks verifies that the ICU syntax of messages this CL touched is
  4760. # valid, and reports any found syntax errors.
  4761. # Without this presubmit check, ICU syntax errors in Chromium strings can land
  4762. # without developers being aware of them. Later on, such ICU syntax errors
  4763. # break message extraction for translation, hence would block Chromium
  4764. # translations until they are fixed.
  4765. icu_syntax_errors = []
  4766. def _CheckScreenshotAdded(screenshots_dir, message_id):
  4767. sha1_path = input_api.os_path.join(screenshots_dir,
  4768. message_id + '.png.sha1')
  4769. if sha1_path not in new_or_added_paths:
  4770. missing_sha1.append(sha1_path)
  4771. def _CheckScreenshotRemoved(screenshots_dir, message_id):
  4772. sha1_path = input_api.os_path.join(screenshots_dir,
  4773. message_id + '.png.sha1')
  4774. if input_api.os_path.exists(
  4775. sha1_path) and sha1_path not in removed_paths:
  4776. unnecessary_sha1_files.append(sha1_path)
  4777. def _ValidateIcuSyntax(text, level, signatures):
  4778. """Validates ICU syntax of a text string.
  4779. Check if text looks similar to ICU and checks for ICU syntax correctness
  4780. in this case. Reports various issues with ICU syntax and values of
  4781. variants. Supports checking of nested messages. Accumulate information of
  4782. each ICU messages found in the text for further checking.
  4783. Args:
  4784. text: a string to check.
  4785. level: a number of current nesting level.
  4786. signatures: an accumulator, a list of tuple of (level, variable,
  4787. kind, variants).
  4788. Returns:
  4789. None if a string is not ICU or no issue detected.
  4790. A tuple of (message, start index, end index) if an issue detected.
  4791. """
  4792. valid_types = {
  4793. 'plural': (frozenset(
  4794. ['=0', '=1', 'zero', 'one', 'two', 'few', 'many',
  4795. 'other']), frozenset(['=1', 'other'])),
  4796. 'selectordinal': (frozenset(
  4797. ['=0', '=1', 'zero', 'one', 'two', 'few', 'many',
  4798. 'other']), frozenset(['one', 'other'])),
  4799. 'select': (frozenset(), frozenset(['other'])),
  4800. }
  4801. # Check if the message looks like an attempt to use ICU
  4802. # plural. If yes - check if its syntax strictly matches ICU format.
  4803. like = re.match(r'^[^{]*\{[^{]*\b(plural|selectordinal|select)\b',
  4804. text)
  4805. if not like:
  4806. signatures.append((level, None, None, None))
  4807. return
  4808. # Check for valid prefix and suffix
  4809. m = re.match(
  4810. r'^([^{]*\{)([a-zA-Z0-9_]+),\s*'
  4811. r'(plural|selectordinal|select),\s*'
  4812. r'(?:offset:\d+)?\s*(.*)', text, re.DOTALL)
  4813. if not m:
  4814. return (('This message looks like an ICU plural, '
  4815. 'but does not follow ICU syntax.'), like.start(),
  4816. like.end())
  4817. starting, variable, kind, variant_pairs = m.groups()
  4818. variants, depth, last_pos = _ParseIcuVariants(variant_pairs,
  4819. m.start(4))
  4820. if depth:
  4821. return ('Invalid ICU format. Unbalanced opening bracket', last_pos,
  4822. len(text))
  4823. first = text[0]
  4824. ending = text[last_pos:]
  4825. if not starting:
  4826. return ('Invalid ICU format. No initial opening bracket',
  4827. last_pos - 1, last_pos)
  4828. if not ending or '}' not in ending:
  4829. return ('Invalid ICU format. No final closing bracket',
  4830. last_pos - 1, last_pos)
  4831. elif first != '{':
  4832. return ((
  4833. 'Invalid ICU format. Extra characters at the start of a complex '
  4834. 'message (go/icu-message-migration): "%s"') % starting, 0,
  4835. len(starting))
  4836. elif ending != '}':
  4837. return ((
  4838. 'Invalid ICU format. Extra characters at the end of a complex '
  4839. 'message (go/icu-message-migration): "%s"') % ending,
  4840. last_pos - 1, len(text) - 1)
  4841. if kind not in valid_types:
  4842. return (('Unknown ICU message type %s. '
  4843. 'Valid types are: plural, select, selectordinal') % kind,
  4844. 0, 0)
  4845. known, required = valid_types[kind]
  4846. defined_variants = set()
  4847. for variant, variant_range, value, value_range in variants:
  4848. start, end = variant_range
  4849. if variant in defined_variants:
  4850. return ('Variant "%s" is defined more than once' % variant,
  4851. start, end)
  4852. elif known and variant not in known:
  4853. return ('Variant "%s" is not valid for %s message' %
  4854. (variant, kind), start, end)
  4855. defined_variants.add(variant)
  4856. # Check for nested structure
  4857. res = _ValidateIcuSyntax(value[1:-1], level + 1, signatures)
  4858. if res:
  4859. return (res[0], res[1] + value_range[0] + 1,
  4860. res[2] + value_range[0] + 1)
  4861. missing = required - defined_variants
  4862. if missing:
  4863. return ('Required variants missing: %s' % ', '.join(missing), 0,
  4864. len(text))
  4865. signatures.append((level, variable, kind, defined_variants))
  4866. def _ParseIcuVariants(text, offset=0):
  4867. """Parse variants part of ICU complex message.
  4868. Builds a tuple of variant names and values, as well as
  4869. their offsets in the input string.
  4870. Args:
  4871. text: a string to parse
  4872. offset: additional offset to add to positions in the text to get correct
  4873. position in the complete ICU string.
  4874. Returns:
  4875. List of tuples, each tuple consist of four fields: variant name,
  4876. variant name span (tuple of two integers), variant value, value
  4877. span (tuple of two integers).
  4878. """
  4879. depth, start, end = 0, -1, -1
  4880. variants = []
  4881. key = None
  4882. for idx, char in enumerate(text):
  4883. if char == '{':
  4884. if not depth:
  4885. start = idx
  4886. chunk = text[end + 1:start]
  4887. key = chunk.strip()
  4888. pos = offset + end + 1 + chunk.find(key)
  4889. span = (pos, pos + len(key))
  4890. depth += 1
  4891. elif char == '}':
  4892. if not depth:
  4893. return variants, depth, offset + idx
  4894. depth -= 1
  4895. if not depth:
  4896. end = idx
  4897. variants.append((key, span, text[start:end + 1],
  4898. (offset + start, offset + end + 1)))
  4899. return variants, depth, offset + end + 1
  4900. try:
  4901. old_sys_path = sys.path
  4902. sys.path = sys.path + [
  4903. input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools',
  4904. 'translation')
  4905. ]
  4906. from helper import grd_helper
  4907. finally:
  4908. sys.path = old_sys_path
  4909. for f in affected_grds:
  4910. file_path = f.LocalPath()
  4911. old_id_to_msg_map = {}
  4912. new_id_to_msg_map = {}
  4913. # Note that this code doesn't check if the file has been deleted. This is
  4914. # OK because it only uses the old and new file contents and doesn't load
  4915. # the file via its path.
  4916. # It's also possible that a file's content refers to a renamed or deleted
  4917. # file via a <part> tag, such as <part file="now-deleted-file.grdp">. This
  4918. # is OK as well, because grd_helper ignores <part> tags when loading .grd or
  4919. # .grdp files.
  4920. if file_path.endswith('.grdp'):
  4921. if f.OldContents():
  4922. old_id_to_msg_map = grd_helper.GetGrdpMessagesFromString(
  4923. '\n'.join(f.OldContents()))
  4924. if f.NewContents():
  4925. new_id_to_msg_map = grd_helper.GetGrdpMessagesFromString(
  4926. '\n'.join(f.NewContents()))
  4927. else:
  4928. file_dir = input_api.os_path.dirname(file_path) or '.'
  4929. if f.OldContents():
  4930. old_id_to_msg_map = grd_helper.GetGrdMessages(
  4931. StringIO('\n'.join(f.OldContents())), file_dir)
  4932. if f.NewContents():
  4933. new_id_to_msg_map = grd_helper.GetGrdMessages(
  4934. StringIO('\n'.join(f.NewContents())), file_dir)
  4935. grd_name, ext = input_api.os_path.splitext(
  4936. input_api.os_path.basename(file_path))
  4937. screenshots_dir = input_api.os_path.join(
  4938. input_api.os_path.dirname(file_path),
  4939. grd_name + ext.replace('.', '_'))
  4940. # Compute added, removed and modified message IDs.
  4941. old_ids = set(old_id_to_msg_map)
  4942. new_ids = set(new_id_to_msg_map)
  4943. added_ids = new_ids - old_ids
  4944. removed_ids = old_ids - new_ids
  4945. modified_ids = set([])
  4946. for key in old_ids.intersection(new_ids):
  4947. if (old_id_to_msg_map[key].ContentsAsXml('', True) !=
  4948. new_id_to_msg_map[key].ContentsAsXml('', True)):
  4949. # The message content itself changed. Require an updated screenshot.
  4950. modified_ids.add(key)
  4951. elif old_id_to_msg_map[key].attrs['meaning'] != \
  4952. new_id_to_msg_map[key].attrs['meaning']:
  4953. # The message meaning changed. Ensure there is a screenshot for it.
  4954. sha1_path = input_api.os_path.join(screenshots_dir,
  4955. key + '.png.sha1')
  4956. if sha1_path not in new_or_added_paths and not \
  4957. input_api.os_path.exists(sha1_path):
  4958. # There is neither a previous screenshot nor is a new one added now.
  4959. # Require a screenshot.
  4960. modified_ids.add(key)
  4961. if run_screenshot_check:
  4962. # Check the screenshot directory for .png files. Warn if there is any.
  4963. for png_path in affected_png_paths:
  4964. if png_path.startswith(screenshots_dir):
  4965. unnecessary_screenshots.append(png_path)
  4966. for added_id in added_ids:
  4967. _CheckScreenshotAdded(screenshots_dir, added_id)
  4968. for modified_id in modified_ids:
  4969. _CheckScreenshotAdded(screenshots_dir, modified_id)
  4970. for removed_id in removed_ids:
  4971. _CheckScreenshotRemoved(screenshots_dir, removed_id)
  4972. # Check new and changed strings for ICU syntax errors.
  4973. for key in added_ids.union(modified_ids):
  4974. msg = new_id_to_msg_map[key].ContentsAsXml('', True)
  4975. err = _ValidateIcuSyntax(msg, 0, [])
  4976. if err is not None:
  4977. icu_syntax_errors.append(str(key) + ': ' + str(err[0]))
  4978. results = []
  4979. if run_screenshot_check:
  4980. if unnecessary_screenshots:
  4981. results.append(
  4982. output_api.PresubmitError(
  4983. 'Do not include actual screenshots in the changelist. Run '
  4984. 'tools/translate/upload_screenshots.py to upload them instead:',
  4985. sorted(unnecessary_screenshots)))
  4986. if missing_sha1:
  4987. results.append(
  4988. output_api.PresubmitError(
  4989. 'You are adding or modifying UI strings.\n'
  4990. 'To ensure the best translations, take screenshots of the relevant UI '
  4991. '(https://g.co/chrome/translation) and add these files to your '
  4992. 'changelist:', sorted(missing_sha1)))
  4993. if unnecessary_sha1_files:
  4994. results.append(
  4995. output_api.PresubmitError(
  4996. 'You removed strings associated with these files. Remove:',
  4997. sorted(unnecessary_sha1_files)))
  4998. else:
  4999. results.append(
  5000. output_api.PresubmitPromptOrNotify('Skipping translation '
  5001. 'screenshots check.'))
  5002. if icu_syntax_errors:
  5003. results.append(
  5004. output_api.PresubmitPromptWarning(
  5005. 'ICU syntax errors were found in the following strings (problems or '
  5006. 'feedback? Contact rainhard@chromium.org):',
  5007. items=icu_syntax_errors))
  5008. return results
  5009. def CheckTranslationExpectations(input_api, output_api,
  5010. repo_root=None,
  5011. translation_expectations_path=None,
  5012. grd_files=None):
  5013. import sys
  5014. affected_grds = [
  5015. f for f in input_api.AffectedFiles()
  5016. if (f.LocalPath().endswith('.grd') or f.LocalPath().endswith('.grdp'))
  5017. ]
  5018. if not affected_grds:
  5019. return []
  5020. try:
  5021. old_sys_path = sys.path
  5022. sys.path = sys.path + [
  5023. input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools',
  5024. 'translation')
  5025. ]
  5026. from helper import git_helper
  5027. from helper import translation_helper
  5028. finally:
  5029. sys.path = old_sys_path
  5030. # Check that translation expectations can be parsed and we can get a list of
  5031. # translatable grd files. |repo_root| and |translation_expectations_path| are
  5032. # only passed by tests.
  5033. if not repo_root:
  5034. repo_root = input_api.PresubmitLocalPath()
  5035. if not translation_expectations_path:
  5036. translation_expectations_path = input_api.os_path.join(
  5037. repo_root, 'tools', 'gritsettings', 'translation_expectations.pyl')
  5038. if not grd_files:
  5039. grd_files = git_helper.list_grds_in_repository(repo_root)
  5040. # Ignore bogus grd files used only for testing
  5041. # ui/webui/resources/tools/generate_grd.py.
  5042. ignore_path = input_api.os_path.join('ui', 'webui', 'resources', 'tools',
  5043. 'tests')
  5044. grd_files = [p for p in grd_files if ignore_path not in p]
  5045. try:
  5046. translation_helper.get_translatable_grds(
  5047. repo_root, grd_files, translation_expectations_path)
  5048. except Exception as e:
  5049. return [
  5050. output_api.PresubmitNotifyResult(
  5051. 'Failed to get a list of translatable grd files. This happens when:\n'
  5052. ' - One of the modified grd or grdp files cannot be parsed or\n'
  5053. ' - %s is not updated.\n'
  5054. 'Stack:\n%s' % (translation_expectations_path, str(e)))
  5055. ]
  5056. return []
  5057. def CheckStableMojomChanges(input_api, output_api):
  5058. """Changes to [Stable] mojom types must preserve backward-compatibility."""
  5059. changed_mojoms = input_api.AffectedFiles(
  5060. include_deletes=True,
  5061. file_filter=lambda f: f.LocalPath().endswith(('.mojom')))
  5062. if not changed_mojoms or input_api.no_diffs:
  5063. return []
  5064. delta = []
  5065. for mojom in changed_mojoms:
  5066. delta.append({
  5067. 'filename': mojom.LocalPath(),
  5068. 'old': '\n'.join(mojom.OldContents()) or None,
  5069. 'new': '\n'.join(mojom.NewContents()) or None,
  5070. })
  5071. process = input_api.subprocess.Popen([
  5072. input_api.python3_executable,
  5073. input_api.os_path.join(
  5074. input_api.PresubmitLocalPath(), 'mojo', 'public', 'tools', 'mojom',
  5075. 'check_stable_mojom_compatibility.py'), '--src-root',
  5076. input_api.PresubmitLocalPath()
  5077. ],
  5078. stdin=input_api.subprocess.PIPE,
  5079. stdout=input_api.subprocess.PIPE,
  5080. stderr=input_api.subprocess.PIPE,
  5081. universal_newlines=True)
  5082. (x, error) = process.communicate(input=input_api.json.dumps(delta))
  5083. if process.returncode:
  5084. return [
  5085. output_api.PresubmitError(
  5086. 'One or more [Stable] mojom definitions appears to have been changed '
  5087. 'in a way that is not backward-compatible.',
  5088. long_text=error)
  5089. ]
  5090. return []
  5091. def CheckDeprecationOfPreferences(input_api, output_api):
  5092. """Removing a preference should come with a deprecation."""
  5093. def FilterFile(affected_file):
  5094. """Accept only .cc files and the like."""
  5095. file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS]
  5096. files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS +
  5097. input_api.DEFAULT_FILES_TO_SKIP)
  5098. return input_api.FilterSourceFile(
  5099. affected_file,
  5100. files_to_check=file_inclusion_pattern,
  5101. files_to_skip=files_to_skip)
  5102. def ModifiedLines(affected_file):
  5103. """Returns a list of tuples (line number, line text) of added and removed
  5104. lines.
  5105. Deleted lines share the same line number as the previous line.
  5106. This relies on the scm diff output describing each changed code section
  5107. with a line of the form
  5108. ^@@ <old line num>,<old size> <new line num>,<new size> @@$
  5109. """
  5110. line_num = 0
  5111. modified_lines = []
  5112. for line in affected_file.GenerateScmDiff().splitlines():
  5113. # Extract <new line num> of the patch fragment (see format above).
  5114. m = input_api.re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@',
  5115. line)
  5116. if m:
  5117. line_num = int(m.groups(1)[0])
  5118. continue
  5119. if ((line.startswith('+') and not line.startswith('++'))
  5120. or (line.startswith('-') and not line.startswith('--'))):
  5121. modified_lines.append((line_num, line))
  5122. if not line.startswith('-'):
  5123. line_num += 1
  5124. return modified_lines
  5125. def FindLineWith(lines, needle):
  5126. """Returns the line number (i.e. index + 1) in `lines` containing `needle`.
  5127. If 0 or >1 lines contain `needle`, -1 is returned.
  5128. """
  5129. matching_line_numbers = [
  5130. # + 1 for 1-based counting of line numbers.
  5131. i + 1 for i, line in enumerate(lines) if needle in line
  5132. ]
  5133. return matching_line_numbers[0] if len(
  5134. matching_line_numbers) == 1 else -1
  5135. def ModifiedPrefMigration(affected_file):
  5136. """Returns whether the MigrateObsolete.*Pref functions were modified."""
  5137. # Determine first and last lines of MigrateObsolete.*Pref functions.
  5138. new_contents = affected_file.NewContents()
  5139. range_1 = (FindLineWith(new_contents,
  5140. 'BEGIN_MIGRATE_OBSOLETE_LOCAL_STATE_PREFS'),
  5141. FindLineWith(new_contents,
  5142. 'END_MIGRATE_OBSOLETE_LOCAL_STATE_PREFS'))
  5143. range_2 = (FindLineWith(new_contents,
  5144. 'BEGIN_MIGRATE_OBSOLETE_PROFILE_PREFS'),
  5145. FindLineWith(new_contents,
  5146. 'END_MIGRATE_OBSOLETE_PROFILE_PREFS'))
  5147. if (-1 in range_1 + range_2):
  5148. raise Exception(
  5149. 'Broken .*MIGRATE_OBSOLETE_.*_PREFS markers in browser_prefs.cc.'
  5150. )
  5151. # Check whether any of the modified lines are part of the
  5152. # MigrateObsolete.*Pref functions.
  5153. for line_nr, line in ModifiedLines(affected_file):
  5154. if (range_1[0] <= line_nr <= range_1[1]
  5155. or range_2[0] <= line_nr <= range_2[1]):
  5156. return True
  5157. return False
  5158. register_pref_pattern = input_api.re.compile(r'Register.+Pref')
  5159. browser_prefs_file_pattern = input_api.re.compile(
  5160. r'chrome/browser/prefs/browser_prefs.cc')
  5161. changes = input_api.AffectedFiles(include_deletes=True,
  5162. file_filter=FilterFile)
  5163. potential_problems = []
  5164. for f in changes:
  5165. for line in f.GenerateScmDiff().splitlines():
  5166. # Check deleted lines for pref registrations.
  5167. if (line.startswith('-') and not line.startswith('--')
  5168. and register_pref_pattern.search(line)):
  5169. potential_problems.append('%s: %s' % (f.LocalPath(), line))
  5170. if browser_prefs_file_pattern.search(f.LocalPath()):
  5171. # If the developer modified the MigrateObsolete.*Prefs() functions, we
  5172. # assume that they knew that they have to deprecate preferences and don't
  5173. # warn.
  5174. try:
  5175. if ModifiedPrefMigration(f):
  5176. return []
  5177. except Exception as e:
  5178. return [output_api.PresubmitError(str(e))]
  5179. if potential_problems:
  5180. return [
  5181. output_api.PresubmitPromptWarning(
  5182. 'Discovered possible removal of preference registrations.\n\n'
  5183. 'Please make sure to properly deprecate preferences by clearing their\n'
  5184. 'value for a couple of milestones before finally removing the code.\n'
  5185. 'Otherwise data may stay in the preferences files forever. See\n'
  5186. 'Migrate*Prefs() in chrome/browser/prefs/browser_prefs.cc and\n'
  5187. 'chrome/browser/prefs/README.md for examples.\n'
  5188. 'This may be a false positive warning (e.g. if you move preference\n'
  5189. 'registrations to a different place).\n', potential_problems)
  5190. ]
  5191. return []
  5192. def CheckConsistentGrdChanges(input_api, output_api):
  5193. """Changes to GRD files must be consistent for tools to read them."""
  5194. changed_grds = input_api.AffectedFiles(
  5195. include_deletes=False,
  5196. file_filter=lambda f: f.LocalPath().endswith(('.grd')))
  5197. errors = []
  5198. invalid_file_regexes = [(input_api.re.compile(matcher), msg)
  5199. for matcher, msg in _INVALID_GRD_FILE_LINE]
  5200. for grd in changed_grds:
  5201. for i, line in enumerate(grd.NewContents()):
  5202. for matcher, msg in invalid_file_regexes:
  5203. if matcher.search(line):
  5204. errors.append(
  5205. output_api.PresubmitError(
  5206. 'Problem on {grd}:{i} - {msg}'.format(
  5207. grd=grd.LocalPath(), i=i + 1, msg=msg)))
  5208. return errors
  5209. def CheckMPArchApiUsage(input_api, output_api):
  5210. """CC the MPArch watchlist if the CL uses an API that is ambiguous in the
  5211. presence of MPArch features such as bfcache, prerendering, and fenced frames.
  5212. """
  5213. # Only consider top-level directories that (1) can use content APIs or
  5214. # problematic blink APIs, (2) apply to desktop or android chrome, and (3)
  5215. # are known to have a significant number of uses of the APIs of concern.
  5216. files_to_check = (
  5217. r'^(chrome|components|content|extensions|third_party[\\/]blink[\\/]renderer)[\\/].+%s' %
  5218. _IMPLEMENTATION_EXTENSIONS,
  5219. r'^(chrome|components|content|extensions|third_party[\\/]blink[\\/]renderer)[\\/].+%s' %
  5220. _HEADER_EXTENSIONS,
  5221. )
  5222. files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS +
  5223. input_api.DEFAULT_FILES_TO_SKIP)
  5224. source_file_filter = lambda f: input_api.FilterSourceFile(
  5225. f, files_to_check=files_to_check, files_to_skip=files_to_skip)
  5226. # Here we list the classes/methods we're monitoring. For the "fyi" cases,
  5227. # we add the CL to the watchlist, but we don't omit a warning or have it be
  5228. # included in the triage rotation.
  5229. # Note that since these are are just regular expressions and we don't have
  5230. # the compiler's AST, we could have spurious matches (e.g. an unrelated class
  5231. # could have a method named IsInMainFrame).
  5232. fyi_concerning_class_pattern = input_api.re.compile(
  5233. r'WebContentsObserver|WebContentsUserData')
  5234. # A subset of WebContentsObserver overrides where there's particular risk for
  5235. # confusing tab and page level operations and data (e.g. incorrectly
  5236. # resetting page state in DidFinishNavigation).
  5237. fyi_concerning_wco_methods = [
  5238. 'DidStartNavigation',
  5239. 'ReadyToCommitNavigation',
  5240. 'DidFinishNavigation',
  5241. 'RenderViewReady',
  5242. 'RenderViewDeleted',
  5243. 'RenderViewHostChanged',
  5244. 'DOMContentLoaded',
  5245. 'DidFinishLoad',
  5246. ]
  5247. concerning_nav_handle_methods = [
  5248. 'IsInMainFrame',
  5249. ]
  5250. concerning_web_contents_methods = [
  5251. 'FromRenderFrameHost',
  5252. 'FromRenderViewHost',
  5253. ]
  5254. fyi_concerning_web_contents_methods = [
  5255. 'GetRenderViewHost',
  5256. ]
  5257. concerning_rfh_methods = [
  5258. 'GetParent',
  5259. 'GetMainFrame',
  5260. ]
  5261. fyi_concerning_rfh_methods = [
  5262. 'GetFrameTreeNodeId',
  5263. ]
  5264. concerning_rfhi_methods = [
  5265. 'is_main_frame',
  5266. ]
  5267. concerning_ftn_methods = [
  5268. 'IsMainFrame',
  5269. ]
  5270. concerning_blink_frame_methods = [
  5271. 'IsCrossOriginToNearestMainFrame',
  5272. ]
  5273. concerning_method_pattern = input_api.re.compile(r'(' + r'|'.join(
  5274. item for sublist in [
  5275. concerning_nav_handle_methods,
  5276. concerning_web_contents_methods, concerning_rfh_methods,
  5277. concerning_rfhi_methods, concerning_ftn_methods,
  5278. concerning_blink_frame_methods,
  5279. ] for item in sublist) + r')\(')
  5280. fyi_concerning_method_pattern = input_api.re.compile(r'(' + r'|'.join(
  5281. item for sublist in [
  5282. fyi_concerning_wco_methods, fyi_concerning_web_contents_methods,
  5283. fyi_concerning_rfh_methods,
  5284. ] for item in sublist) + r')\(')
  5285. used_apis = set()
  5286. used_fyi_methods = False
  5287. for f in input_api.AffectedFiles(include_deletes=False,
  5288. file_filter=source_file_filter):
  5289. for line_num, line in f.ChangedContents():
  5290. fyi_class_match = fyi_concerning_class_pattern.search(line)
  5291. if fyi_class_match:
  5292. used_fyi_methods = True
  5293. fyi_method_match = fyi_concerning_method_pattern.search(line)
  5294. if fyi_method_match:
  5295. used_fyi_methods = True
  5296. method_match = concerning_method_pattern.search(line)
  5297. if method_match:
  5298. used_apis.add(method_match[1])
  5299. if not used_apis:
  5300. if used_fyi_methods:
  5301. output_api.AppendCC('mparch-reviews+watchfyi@chromium.org')
  5302. return []
  5303. output_api.AppendCC('mparch-reviews+watch@chromium.org')
  5304. message = ('This change uses API(s) that are ambiguous in the presence of '
  5305. 'MPArch features such as bfcache, prerendering, and fenced '
  5306. 'frames.')
  5307. explanation = (
  5308. 'Please double check whether new code assumes that a WebContents only '
  5309. 'contains a single page at a time. Notably, checking whether a frame '
  5310. 'is the \"main frame\" is not specific enough to determine whether it '
  5311. 'corresponds to the document reflected in the omnibox. A WebContents '
  5312. 'may have additional main frames for prerendered pages, bfcached '
  5313. 'pages, fenced frames, etc. '
  5314. 'See this doc [1] and the comments on the individual APIs '
  5315. 'for guidance and this doc [2] for context. The MPArch review '
  5316. 'watchlist has been CC\'d on this change to help identify any issues.\n'
  5317. '[1] https://docs.google.com/document/d/13l16rWTal3o5wce4i0RwdpMP5ESELLKr439Faj2BBRo/edit?usp=sharing\n'
  5318. '[2] https://docs.google.com/document/d/1NginQ8k0w3znuwTiJ5qjYmBKgZDekvEPC22q0I4swxQ/edit?usp=sharing'
  5319. )
  5320. return [
  5321. output_api.PresubmitNotifyResult(message,
  5322. items=list(used_apis),
  5323. long_text=explanation)
  5324. ]
  5325. def CheckAssertAshOnlyCode(input_api, output_api):
  5326. """Errors if a BUILD.gn file in an ash/ directory doesn't include
  5327. assert(is_chromeos_ash).
  5328. """
  5329. def FileFilter(affected_file):
  5330. """Includes directories known to be Ash only."""
  5331. return input_api.FilterSourceFile(
  5332. affected_file,
  5333. files_to_check=(
  5334. r'^ash/.*BUILD\.gn', # Top-level src/ash/.
  5335. r'.*/ash/.*BUILD\.gn'), # Any path component.
  5336. files_to_skip=(input_api.DEFAULT_FILES_TO_SKIP))
  5337. errors = []
  5338. pattern = input_api.re.compile(r'assert\(is_chromeos_ash')
  5339. for f in input_api.AffectedFiles(include_deletes=False,
  5340. file_filter=FileFilter):
  5341. if (not pattern.search(input_api.ReadFile(f))):
  5342. errors.append(
  5343. output_api.PresubmitError(
  5344. 'Please add assert(is_chromeos_ash) to %s. If that\'s not '
  5345. 'possible, please create and issue and add a comment such '
  5346. 'as:\n # TODO(https://crbug.com/XXX): add '
  5347. 'assert(is_chromeos_ash) when ...' % f.LocalPath()))
  5348. return errors
  5349. def _IsRendererOnlyCppFile(input_api, affected_file):
  5350. path = affected_file.LocalPath()
  5351. if not _IsCPlusPlusFile(input_api, path):
  5352. return False
  5353. # Any code under a "renderer" subdirectory is assumed to be Renderer-only.
  5354. if "/renderer/" in path:
  5355. return True
  5356. # Blink's public/web API is only used/included by Renderer-only code. Note
  5357. # that public/platform API may be used in non-Renderer processes (e.g. there
  5358. # are some includes in code used by Utility, PDF, or Plugin processes).
  5359. if "/blink/public/web/" in path:
  5360. return True
  5361. # We assume that everything else may be used outside of Renderer processes.
  5362. return False
  5363. # TODO(https://crbug.com/1273182): Remove these checks, once they are replaced
  5364. # by the Chromium Clang Plugin (which will be preferable because it will
  5365. # 1) report errors earlier - at compile-time and 2) cover more rules).
  5366. def CheckRawPtrUsage(input_api, output_api):
  5367. """Rough checks that raw_ptr<T> usage guidelines are followed."""
  5368. errors = []
  5369. # The regex below matches "raw_ptr<" following a word boundary, but not in a
  5370. # C++ comment.
  5371. raw_ptr_matcher = input_api.re.compile(r'^((?!//).)*\braw_ptr<')
  5372. file_filter = lambda f: _IsRendererOnlyCppFile(input_api, f)
  5373. for f, line_num, line in input_api.RightHandSideLines(file_filter):
  5374. if raw_ptr_matcher.search(line):
  5375. errors.append(
  5376. output_api.PresubmitError(
  5377. 'Problem on {path}:{line} - '\
  5378. 'raw_ptr<T> should not be used in Renderer-only code '\
  5379. '(as documented in the "Pointers to unprotected memory" '\
  5380. 'section in //base/memory/raw_ptr.md)'.format(
  5381. path=f.LocalPath(), line=line_num)))
  5382. return errors
  5383. def CheckPythonShebang(input_api, output_api):
  5384. """Checks that python scripts use #!/usr/bin/env instead of hardcoding a
  5385. system-wide python.
  5386. """
  5387. errors = []
  5388. sources = lambda affected_file: input_api.FilterSourceFile(
  5389. affected_file,
  5390. files_to_skip=((_THIRD_PARTY_EXCEPT_BLINK,
  5391. r'third_party/blink/web_tests/external/') + input_api.
  5392. DEFAULT_FILES_TO_SKIP),
  5393. files_to_check=[r'.*\.py$'])
  5394. for f in input_api.AffectedSourceFiles(sources):
  5395. for line_num, line in f.ChangedContents():
  5396. if line_num == 1 and line.startswith('#!/usr/bin/python'):
  5397. errors.append(f.LocalPath())
  5398. break
  5399. result = []
  5400. for file in errors:
  5401. result.append(
  5402. output_api.PresubmitError(
  5403. "Please use '#!/usr/bin/env python/2/3' as the shebang of %s" %
  5404. file))
  5405. return result
  5406. def CheckBatchAnnotation(input_api, output_api):
  5407. """Checks that tests have either @Batch or @DoNotBatch annotation. If this
  5408. is not an instrumentation test, disregard."""
  5409. batch_annotation = input_api.re.compile(r'^\s*@Batch')
  5410. do_not_batch_annotation = input_api.re.compile(r'^\s*@DoNotBatch')
  5411. robolectric_test = input_api.re.compile(r'[rR]obolectric')
  5412. test_class_declaration = input_api.re.compile(r'^\s*public\sclass.*Test')
  5413. uiautomator_test = input_api.re.compile(r'[uU]i[aA]utomator')
  5414. missing_annotation_errors = []
  5415. extra_annotation_errors = []
  5416. def _FilterFile(affected_file):
  5417. return input_api.FilterSourceFile(
  5418. affected_file,
  5419. files_to_skip=input_api.DEFAULT_FILES_TO_SKIP,
  5420. files_to_check=[r'.*Test\.java$'])
  5421. for f in input_api.AffectedSourceFiles(_FilterFile):
  5422. batch_matched = None
  5423. do_not_batch_matched = None
  5424. is_instrumentation_test = True
  5425. for line in f.NewContents():
  5426. if robolectric_test.search(line) or uiautomator_test.search(line):
  5427. # Skip Robolectric and UiAutomator tests.
  5428. is_instrumentation_test = False
  5429. break
  5430. if not batch_matched:
  5431. batch_matched = batch_annotation.search(line)
  5432. if not do_not_batch_matched:
  5433. do_not_batch_matched = do_not_batch_annotation.search(line)
  5434. test_class_declaration_matched = test_class_declaration.search(
  5435. line)
  5436. if test_class_declaration_matched:
  5437. break
  5438. if (is_instrumentation_test and
  5439. not batch_matched and
  5440. not do_not_batch_matched):
  5441. missing_annotation_errors.append(str(f.LocalPath()))
  5442. if (not is_instrumentation_test and
  5443. (batch_matched or
  5444. do_not_batch_matched)):
  5445. extra_annotation_errors.append(str(f.LocalPath()))
  5446. results = []
  5447. if missing_annotation_errors:
  5448. results.append(
  5449. output_api.PresubmitPromptWarning(
  5450. """
  5451. Instrumentation tests should use either @Batch or @DoNotBatch. If tests are not
  5452. safe to run in batch, please use @DoNotBatch with reasons.
  5453. """, missing_annotation_errors))
  5454. if extra_annotation_errors:
  5455. results.append(
  5456. output_api.PresubmitPromptWarning(
  5457. """
  5458. Robolectric tests do not need a @Batch or @DoNotBatch annotations.
  5459. """, extra_annotation_errors))
  5460. return results