ASTReader.cpp 469 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236
  1. //===- ASTReader.cpp - AST File Reader ------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the ASTReader class, which reads AST files.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Serialization/ASTReader.h"
  13. #include "ASTCommon.h"
  14. #include "ASTReaderInternals.h"
  15. #include "clang/AST/ASTConsumer.h"
  16. #include "clang/AST/ASTContext.h"
  17. #include "clang/AST/ASTMutationListener.h"
  18. #include "clang/AST/ASTUnresolvedSet.h"
  19. #include "clang/AST/Decl.h"
  20. #include "clang/AST/DeclBase.h"
  21. #include "clang/AST/DeclCXX.h"
  22. #include "clang/AST/DeclFriend.h"
  23. #include "clang/AST/DeclGroup.h"
  24. #include "clang/AST/DeclObjC.h"
  25. #include "clang/AST/DeclTemplate.h"
  26. #include "clang/AST/DeclarationName.h"
  27. #include "clang/AST/Expr.h"
  28. #include "clang/AST/ExprCXX.h"
  29. #include "clang/AST/ExternalASTSource.h"
  30. #include "clang/AST/NestedNameSpecifier.h"
  31. #include "clang/AST/ODRHash.h"
  32. #include "clang/AST/RawCommentList.h"
  33. #include "clang/AST/TemplateBase.h"
  34. #include "clang/AST/TemplateName.h"
  35. #include "clang/AST/Type.h"
  36. #include "clang/AST/TypeLoc.h"
  37. #include "clang/AST/TypeLocVisitor.h"
  38. #include "clang/AST/UnresolvedSet.h"
  39. #include "clang/Basic/CommentOptions.h"
  40. #include "clang/Basic/Diagnostic.h"
  41. #include "clang/Basic/DiagnosticOptions.h"
  42. #include "clang/Basic/ExceptionSpecificationType.h"
  43. #include "clang/Basic/FileManager.h"
  44. #include "clang/Basic/FileSystemOptions.h"
  45. #include "clang/Basic/IdentifierTable.h"
  46. #include "clang/Basic/LLVM.h"
  47. #include "clang/Basic/LangOptions.h"
  48. #include "clang/Basic/Module.h"
  49. #include "clang/Basic/ObjCRuntime.h"
  50. #include "clang/Basic/OperatorKinds.h"
  51. #include "clang/Basic/PragmaKinds.h"
  52. #include "clang/Basic/Sanitizers.h"
  53. #include "clang/Basic/SourceLocation.h"
  54. #include "clang/Basic/SourceManager.h"
  55. #include "clang/Basic/SourceManagerInternals.h"
  56. #include "clang/Basic/Specifiers.h"
  57. #include "clang/Basic/TargetInfo.h"
  58. #include "clang/Basic/TargetOptions.h"
  59. #include "clang/Basic/TokenKinds.h"
  60. #include "clang/Basic/Version.h"
  61. #include "clang/Lex/HeaderSearch.h"
  62. #include "clang/Lex/HeaderSearchOptions.h"
  63. #include "clang/Lex/MacroInfo.h"
  64. #include "clang/Lex/ModuleMap.h"
  65. #include "clang/Lex/PreprocessingRecord.h"
  66. #include "clang/Lex/Preprocessor.h"
  67. #include "clang/Lex/PreprocessorOptions.h"
  68. #include "clang/Lex/Token.h"
  69. #include "clang/Sema/ObjCMethodList.h"
  70. #include "clang/Sema/Scope.h"
  71. #include "clang/Sema/Sema.h"
  72. #include "clang/Sema/Weak.h"
  73. #include "clang/Serialization/ASTBitCodes.h"
  74. #include "clang/Serialization/ASTDeserializationListener.h"
  75. #include "clang/Serialization/ContinuousRangeMap.h"
  76. #include "clang/Serialization/GlobalModuleIndex.h"
  77. #include "clang/Serialization/InMemoryModuleCache.h"
  78. #include "clang/Serialization/Module.h"
  79. #include "clang/Serialization/ModuleFileExtension.h"
  80. #include "clang/Serialization/ModuleManager.h"
  81. #include "clang/Serialization/PCHContainerOperations.h"
  82. #include "clang/Serialization/SerializationDiagnostic.h"
  83. #include "llvm/ADT/APFloat.h"
  84. #include "llvm/ADT/APInt.h"
  85. #include "llvm/ADT/APSInt.h"
  86. #include "llvm/ADT/ArrayRef.h"
  87. #include "llvm/ADT/DenseMap.h"
  88. #include "llvm/ADT/FoldingSet.h"
  89. #include "llvm/ADT/Hashing.h"
  90. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  91. #include "llvm/ADT/None.h"
  92. #include "llvm/ADT/Optional.h"
  93. #include "llvm/ADT/STLExtras.h"
  94. #include "llvm/ADT/ScopeExit.h"
  95. #include "llvm/ADT/SmallPtrSet.h"
  96. #include "llvm/ADT/SmallString.h"
  97. #include "llvm/ADT/SmallVector.h"
  98. #include "llvm/ADT/StringExtras.h"
  99. #include "llvm/ADT/StringMap.h"
  100. #include "llvm/ADT/StringRef.h"
  101. #include "llvm/ADT/Triple.h"
  102. #include "llvm/ADT/iterator_range.h"
  103. #include "llvm/Bitstream/BitstreamReader.h"
  104. #include "llvm/Support/Casting.h"
  105. #include "llvm/Support/Compiler.h"
  106. #include "llvm/Support/Compression.h"
  107. #include "llvm/Support/DJB.h"
  108. #include "llvm/Support/Endian.h"
  109. #include "llvm/Support/Error.h"
  110. #include "llvm/Support/ErrorHandling.h"
  111. #include "llvm/Support/FileSystem.h"
  112. #include "llvm/Support/MemoryBuffer.h"
  113. #include "llvm/Support/Path.h"
  114. #include "llvm/Support/SaveAndRestore.h"
  115. #include "llvm/Support/Timer.h"
  116. #include "llvm/Support/VersionTuple.h"
  117. #include "llvm/Support/raw_ostream.h"
  118. #include <algorithm>
  119. #include <cassert>
  120. #include <cstddef>
  121. #include <cstdint>
  122. #include <cstdio>
  123. #include <ctime>
  124. #include <iterator>
  125. #include <limits>
  126. #include <map>
  127. #include <memory>
  128. #include <string>
  129. #include <system_error>
  130. #include <tuple>
  131. #include <utility>
  132. #include <vector>
  133. using namespace clang;
  134. using namespace clang::serialization;
  135. using namespace clang::serialization::reader;
  136. using llvm::BitstreamCursor;
  137. //===----------------------------------------------------------------------===//
  138. // ChainedASTReaderListener implementation
  139. //===----------------------------------------------------------------------===//
  140. bool
  141. ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
  142. return First->ReadFullVersionInformation(FullVersion) ||
  143. Second->ReadFullVersionInformation(FullVersion);
  144. }
  145. void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
  146. First->ReadModuleName(ModuleName);
  147. Second->ReadModuleName(ModuleName);
  148. }
  149. void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
  150. First->ReadModuleMapFile(ModuleMapPath);
  151. Second->ReadModuleMapFile(ModuleMapPath);
  152. }
  153. bool
  154. ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
  155. bool Complain,
  156. bool AllowCompatibleDifferences) {
  157. return First->ReadLanguageOptions(LangOpts, Complain,
  158. AllowCompatibleDifferences) ||
  159. Second->ReadLanguageOptions(LangOpts, Complain,
  160. AllowCompatibleDifferences);
  161. }
  162. bool ChainedASTReaderListener::ReadTargetOptions(
  163. const TargetOptions &TargetOpts, bool Complain,
  164. bool AllowCompatibleDifferences) {
  165. return First->ReadTargetOptions(TargetOpts, Complain,
  166. AllowCompatibleDifferences) ||
  167. Second->ReadTargetOptions(TargetOpts, Complain,
  168. AllowCompatibleDifferences);
  169. }
  170. bool ChainedASTReaderListener::ReadDiagnosticOptions(
  171. IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
  172. return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
  173. Second->ReadDiagnosticOptions(DiagOpts, Complain);
  174. }
  175. bool
  176. ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
  177. bool Complain) {
  178. return First->ReadFileSystemOptions(FSOpts, Complain) ||
  179. Second->ReadFileSystemOptions(FSOpts, Complain);
  180. }
  181. bool ChainedASTReaderListener::ReadHeaderSearchOptions(
  182. const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
  183. bool Complain) {
  184. return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
  185. Complain) ||
  186. Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
  187. Complain);
  188. }
  189. bool ChainedASTReaderListener::ReadPreprocessorOptions(
  190. const PreprocessorOptions &PPOpts, bool Complain,
  191. std::string &SuggestedPredefines) {
  192. return First->ReadPreprocessorOptions(PPOpts, Complain,
  193. SuggestedPredefines) ||
  194. Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
  195. }
  196. void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
  197. unsigned Value) {
  198. First->ReadCounter(M, Value);
  199. Second->ReadCounter(M, Value);
  200. }
  201. bool ChainedASTReaderListener::needsInputFileVisitation() {
  202. return First->needsInputFileVisitation() ||
  203. Second->needsInputFileVisitation();
  204. }
  205. bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
  206. return First->needsSystemInputFileVisitation() ||
  207. Second->needsSystemInputFileVisitation();
  208. }
  209. void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
  210. ModuleKind Kind) {
  211. First->visitModuleFile(Filename, Kind);
  212. Second->visitModuleFile(Filename, Kind);
  213. }
  214. bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
  215. bool isSystem,
  216. bool isOverridden,
  217. bool isExplicitModule) {
  218. bool Continue = false;
  219. if (First->needsInputFileVisitation() &&
  220. (!isSystem || First->needsSystemInputFileVisitation()))
  221. Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
  222. isExplicitModule);
  223. if (Second->needsInputFileVisitation() &&
  224. (!isSystem || Second->needsSystemInputFileVisitation()))
  225. Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
  226. isExplicitModule);
  227. return Continue;
  228. }
  229. void ChainedASTReaderListener::readModuleFileExtension(
  230. const ModuleFileExtensionMetadata &Metadata) {
  231. First->readModuleFileExtension(Metadata);
  232. Second->readModuleFileExtension(Metadata);
  233. }
  234. //===----------------------------------------------------------------------===//
  235. // PCH validator implementation
  236. //===----------------------------------------------------------------------===//
  237. ASTReaderListener::~ASTReaderListener() = default;
  238. /// Compare the given set of language options against an existing set of
  239. /// language options.
  240. ///
  241. /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
  242. /// \param AllowCompatibleDifferences If true, differences between compatible
  243. /// language options will be permitted.
  244. ///
  245. /// \returns true if the languagae options mis-match, false otherwise.
  246. static bool checkLanguageOptions(const LangOptions &LangOpts,
  247. const LangOptions &ExistingLangOpts,
  248. DiagnosticsEngine *Diags,
  249. bool AllowCompatibleDifferences = true) {
  250. #define LANGOPT(Name, Bits, Default, Description) \
  251. if (ExistingLangOpts.Name != LangOpts.Name) { \
  252. if (Diags) \
  253. Diags->Report(diag::err_pch_langopt_mismatch) \
  254. << Description << LangOpts.Name << ExistingLangOpts.Name; \
  255. return true; \
  256. }
  257. #define VALUE_LANGOPT(Name, Bits, Default, Description) \
  258. if (ExistingLangOpts.Name != LangOpts.Name) { \
  259. if (Diags) \
  260. Diags->Report(diag::err_pch_langopt_value_mismatch) \
  261. << Description; \
  262. return true; \
  263. }
  264. #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
  265. if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
  266. if (Diags) \
  267. Diags->Report(diag::err_pch_langopt_value_mismatch) \
  268. << Description; \
  269. return true; \
  270. }
  271. #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
  272. if (!AllowCompatibleDifferences) \
  273. LANGOPT(Name, Bits, Default, Description)
  274. #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
  275. if (!AllowCompatibleDifferences) \
  276. ENUM_LANGOPT(Name, Bits, Default, Description)
  277. #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
  278. if (!AllowCompatibleDifferences) \
  279. VALUE_LANGOPT(Name, Bits, Default, Description)
  280. #define BENIGN_LANGOPT(Name, Bits, Default, Description)
  281. #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
  282. #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
  283. #include "clang/Basic/LangOptions.def"
  284. if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
  285. if (Diags)
  286. Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
  287. return true;
  288. }
  289. if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
  290. if (Diags)
  291. Diags->Report(diag::err_pch_langopt_value_mismatch)
  292. << "target Objective-C runtime";
  293. return true;
  294. }
  295. if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
  296. LangOpts.CommentOpts.BlockCommandNames) {
  297. if (Diags)
  298. Diags->Report(diag::err_pch_langopt_value_mismatch)
  299. << "block command names";
  300. return true;
  301. }
  302. // Sanitizer feature mismatches are treated as compatible differences. If
  303. // compatible differences aren't allowed, we still only want to check for
  304. // mismatches of non-modular sanitizers (the only ones which can affect AST
  305. // generation).
  306. if (!AllowCompatibleDifferences) {
  307. SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
  308. SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
  309. SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
  310. ExistingSanitizers.clear(ModularSanitizers);
  311. ImportedSanitizers.clear(ModularSanitizers);
  312. if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
  313. const std::string Flag = "-fsanitize=";
  314. if (Diags) {
  315. #define SANITIZER(NAME, ID) \
  316. { \
  317. bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
  318. bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
  319. if (InExistingModule != InImportedModule) \
  320. Diags->Report(diag::err_pch_targetopt_feature_mismatch) \
  321. << InExistingModule << (Flag + NAME); \
  322. }
  323. #include "clang/Basic/Sanitizers.def"
  324. }
  325. return true;
  326. }
  327. }
  328. return false;
  329. }
  330. /// Compare the given set of target options against an existing set of
  331. /// target options.
  332. ///
  333. /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
  334. ///
  335. /// \returns true if the target options mis-match, false otherwise.
  336. static bool checkTargetOptions(const TargetOptions &TargetOpts,
  337. const TargetOptions &ExistingTargetOpts,
  338. DiagnosticsEngine *Diags,
  339. bool AllowCompatibleDifferences = true) {
  340. #define CHECK_TARGET_OPT(Field, Name) \
  341. if (TargetOpts.Field != ExistingTargetOpts.Field) { \
  342. if (Diags) \
  343. Diags->Report(diag::err_pch_targetopt_mismatch) \
  344. << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
  345. return true; \
  346. }
  347. // The triple and ABI must match exactly.
  348. CHECK_TARGET_OPT(Triple, "target");
  349. CHECK_TARGET_OPT(ABI, "target ABI");
  350. // We can tolerate different CPUs in many cases, notably when one CPU
  351. // supports a strict superset of another. When allowing compatible
  352. // differences skip this check.
  353. if (!AllowCompatibleDifferences)
  354. CHECK_TARGET_OPT(CPU, "target CPU");
  355. #undef CHECK_TARGET_OPT
  356. // Compare feature sets.
  357. SmallVector<StringRef, 4> ExistingFeatures(
  358. ExistingTargetOpts.FeaturesAsWritten.begin(),
  359. ExistingTargetOpts.FeaturesAsWritten.end());
  360. SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
  361. TargetOpts.FeaturesAsWritten.end());
  362. llvm::sort(ExistingFeatures);
  363. llvm::sort(ReadFeatures);
  364. // We compute the set difference in both directions explicitly so that we can
  365. // diagnose the differences differently.
  366. SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
  367. std::set_difference(
  368. ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
  369. ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
  370. std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
  371. ExistingFeatures.begin(), ExistingFeatures.end(),
  372. std::back_inserter(UnmatchedReadFeatures));
  373. // If we are allowing compatible differences and the read feature set is
  374. // a strict subset of the existing feature set, there is nothing to diagnose.
  375. if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
  376. return false;
  377. if (Diags) {
  378. for (StringRef Feature : UnmatchedReadFeatures)
  379. Diags->Report(diag::err_pch_targetopt_feature_mismatch)
  380. << /* is-existing-feature */ false << Feature;
  381. for (StringRef Feature : UnmatchedExistingFeatures)
  382. Diags->Report(diag::err_pch_targetopt_feature_mismatch)
  383. << /* is-existing-feature */ true << Feature;
  384. }
  385. return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
  386. }
  387. bool
  388. PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
  389. bool Complain,
  390. bool AllowCompatibleDifferences) {
  391. const LangOptions &ExistingLangOpts = PP.getLangOpts();
  392. return checkLanguageOptions(LangOpts, ExistingLangOpts,
  393. Complain ? &Reader.Diags : nullptr,
  394. AllowCompatibleDifferences);
  395. }
  396. bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
  397. bool Complain,
  398. bool AllowCompatibleDifferences) {
  399. const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
  400. return checkTargetOptions(TargetOpts, ExistingTargetOpts,
  401. Complain ? &Reader.Diags : nullptr,
  402. AllowCompatibleDifferences);
  403. }
  404. namespace {
  405. using MacroDefinitionsMap =
  406. llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
  407. using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
  408. } // namespace
  409. static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
  410. DiagnosticsEngine &Diags,
  411. bool Complain) {
  412. using Level = DiagnosticsEngine::Level;
  413. // Check current mappings for new -Werror mappings, and the stored mappings
  414. // for cases that were explicitly mapped to *not* be errors that are now
  415. // errors because of options like -Werror.
  416. DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
  417. for (DiagnosticsEngine *MappingSource : MappingSources) {
  418. for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
  419. diag::kind DiagID = DiagIDMappingPair.first;
  420. Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
  421. if (CurLevel < DiagnosticsEngine::Error)
  422. continue; // not significant
  423. Level StoredLevel =
  424. StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
  425. if (StoredLevel < DiagnosticsEngine::Error) {
  426. if (Complain)
  427. Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
  428. Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
  429. return true;
  430. }
  431. }
  432. }
  433. return false;
  434. }
  435. static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
  436. diag::Severity Ext = Diags.getExtensionHandlingBehavior();
  437. if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
  438. return true;
  439. return Ext >= diag::Severity::Error;
  440. }
  441. static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
  442. DiagnosticsEngine &Diags,
  443. bool IsSystem, bool Complain) {
  444. // Top-level options
  445. if (IsSystem) {
  446. if (Diags.getSuppressSystemWarnings())
  447. return false;
  448. // If -Wsystem-headers was not enabled before, be conservative
  449. if (StoredDiags.getSuppressSystemWarnings()) {
  450. if (Complain)
  451. Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
  452. return true;
  453. }
  454. }
  455. if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
  456. if (Complain)
  457. Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
  458. return true;
  459. }
  460. if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
  461. !StoredDiags.getEnableAllWarnings()) {
  462. if (Complain)
  463. Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
  464. return true;
  465. }
  466. if (isExtHandlingFromDiagsError(Diags) &&
  467. !isExtHandlingFromDiagsError(StoredDiags)) {
  468. if (Complain)
  469. Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
  470. return true;
  471. }
  472. return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
  473. }
  474. /// Return the top import module if it is implicit, nullptr otherwise.
  475. static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
  476. Preprocessor &PP) {
  477. // If the original import came from a file explicitly generated by the user,
  478. // don't check the diagnostic mappings.
  479. // FIXME: currently this is approximated by checking whether this is not a
  480. // module import of an implicitly-loaded module file.
  481. // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
  482. // the transitive closure of its imports, since unrelated modules cannot be
  483. // imported until after this module finishes validation.
  484. ModuleFile *TopImport = &*ModuleMgr.rbegin();
  485. while (!TopImport->ImportedBy.empty())
  486. TopImport = TopImport->ImportedBy[0];
  487. if (TopImport->Kind != MK_ImplicitModule)
  488. return nullptr;
  489. StringRef ModuleName = TopImport->ModuleName;
  490. assert(!ModuleName.empty() && "diagnostic options read before module name");
  491. Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
  492. assert(M && "missing module");
  493. return M;
  494. }
  495. bool PCHValidator::ReadDiagnosticOptions(
  496. IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
  497. DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
  498. IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
  499. IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
  500. new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
  501. // This should never fail, because we would have processed these options
  502. // before writing them to an ASTFile.
  503. ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
  504. ModuleManager &ModuleMgr = Reader.getModuleManager();
  505. assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
  506. Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
  507. if (!TopM)
  508. return false;
  509. // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
  510. // contains the union of their flags.
  511. return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
  512. Complain);
  513. }
  514. /// Collect the macro definitions provided by the given preprocessor
  515. /// options.
  516. static void
  517. collectMacroDefinitions(const PreprocessorOptions &PPOpts,
  518. MacroDefinitionsMap &Macros,
  519. SmallVectorImpl<StringRef> *MacroNames = nullptr) {
  520. for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
  521. StringRef Macro = PPOpts.Macros[I].first;
  522. bool IsUndef = PPOpts.Macros[I].second;
  523. std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
  524. StringRef MacroName = MacroPair.first;
  525. StringRef MacroBody = MacroPair.second;
  526. // For an #undef'd macro, we only care about the name.
  527. if (IsUndef) {
  528. if (MacroNames && !Macros.count(MacroName))
  529. MacroNames->push_back(MacroName);
  530. Macros[MacroName] = std::make_pair("", true);
  531. continue;
  532. }
  533. // For a #define'd macro, figure out the actual definition.
  534. if (MacroName.size() == Macro.size())
  535. MacroBody = "1";
  536. else {
  537. // Note: GCC drops anything following an end-of-line character.
  538. StringRef::size_type End = MacroBody.find_first_of("\n\r");
  539. MacroBody = MacroBody.substr(0, End);
  540. }
  541. if (MacroNames && !Macros.count(MacroName))
  542. MacroNames->push_back(MacroName);
  543. Macros[MacroName] = std::make_pair(MacroBody, false);
  544. }
  545. }
  546. /// Check the preprocessor options deserialized from the control block
  547. /// against the preprocessor options in an existing preprocessor.
  548. ///
  549. /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
  550. /// \param Validate If true, validate preprocessor options. If false, allow
  551. /// macros defined by \p ExistingPPOpts to override those defined by
  552. /// \p PPOpts in SuggestedPredefines.
  553. static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
  554. const PreprocessorOptions &ExistingPPOpts,
  555. DiagnosticsEngine *Diags,
  556. FileManager &FileMgr,
  557. std::string &SuggestedPredefines,
  558. const LangOptions &LangOpts,
  559. bool Validate = true) {
  560. // Check macro definitions.
  561. MacroDefinitionsMap ASTFileMacros;
  562. collectMacroDefinitions(PPOpts, ASTFileMacros);
  563. MacroDefinitionsMap ExistingMacros;
  564. SmallVector<StringRef, 4> ExistingMacroNames;
  565. collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
  566. for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
  567. // Dig out the macro definition in the existing preprocessor options.
  568. StringRef MacroName = ExistingMacroNames[I];
  569. std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
  570. // Check whether we know anything about this macro name or not.
  571. llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
  572. ASTFileMacros.find(MacroName);
  573. if (!Validate || Known == ASTFileMacros.end()) {
  574. // FIXME: Check whether this identifier was referenced anywhere in the
  575. // AST file. If so, we should reject the AST file. Unfortunately, this
  576. // information isn't in the control block. What shall we do about it?
  577. if (Existing.second) {
  578. SuggestedPredefines += "#undef ";
  579. SuggestedPredefines += MacroName.str();
  580. SuggestedPredefines += '\n';
  581. } else {
  582. SuggestedPredefines += "#define ";
  583. SuggestedPredefines += MacroName.str();
  584. SuggestedPredefines += ' ';
  585. SuggestedPredefines += Existing.first.str();
  586. SuggestedPredefines += '\n';
  587. }
  588. continue;
  589. }
  590. // If the macro was defined in one but undef'd in the other, we have a
  591. // conflict.
  592. if (Existing.second != Known->second.second) {
  593. if (Diags) {
  594. Diags->Report(diag::err_pch_macro_def_undef)
  595. << MacroName << Known->second.second;
  596. }
  597. return true;
  598. }
  599. // If the macro was #undef'd in both, or if the macro bodies are identical,
  600. // it's fine.
  601. if (Existing.second || Existing.first == Known->second.first)
  602. continue;
  603. // The macro bodies differ; complain.
  604. if (Diags) {
  605. Diags->Report(diag::err_pch_macro_def_conflict)
  606. << MacroName << Known->second.first << Existing.first;
  607. }
  608. return true;
  609. }
  610. // Check whether we're using predefines.
  611. if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
  612. if (Diags) {
  613. Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
  614. }
  615. return true;
  616. }
  617. // Detailed record is important since it is used for the module cache hash.
  618. if (LangOpts.Modules &&
  619. PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
  620. if (Diags) {
  621. Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
  622. }
  623. return true;
  624. }
  625. // Compute the #include and #include_macros lines we need.
  626. for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
  627. StringRef File = ExistingPPOpts.Includes[I];
  628. if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
  629. !ExistingPPOpts.PCHThroughHeader.empty()) {
  630. // In case the through header is an include, we must add all the includes
  631. // to the predefines so the start point can be determined.
  632. SuggestedPredefines += "#include \"";
  633. SuggestedPredefines += File;
  634. SuggestedPredefines += "\"\n";
  635. continue;
  636. }
  637. if (File == ExistingPPOpts.ImplicitPCHInclude)
  638. continue;
  639. if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
  640. != PPOpts.Includes.end())
  641. continue;
  642. SuggestedPredefines += "#include \"";
  643. SuggestedPredefines += File;
  644. SuggestedPredefines += "\"\n";
  645. }
  646. for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
  647. StringRef File = ExistingPPOpts.MacroIncludes[I];
  648. if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
  649. File)
  650. != PPOpts.MacroIncludes.end())
  651. continue;
  652. SuggestedPredefines += "#__include_macros \"";
  653. SuggestedPredefines += File;
  654. SuggestedPredefines += "\"\n##\n";
  655. }
  656. return false;
  657. }
  658. bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
  659. bool Complain,
  660. std::string &SuggestedPredefines) {
  661. const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
  662. return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
  663. Complain? &Reader.Diags : nullptr,
  664. PP.getFileManager(),
  665. SuggestedPredefines,
  666. PP.getLangOpts());
  667. }
  668. bool SimpleASTReaderListener::ReadPreprocessorOptions(
  669. const PreprocessorOptions &PPOpts,
  670. bool Complain,
  671. std::string &SuggestedPredefines) {
  672. return checkPreprocessorOptions(PPOpts,
  673. PP.getPreprocessorOpts(),
  674. nullptr,
  675. PP.getFileManager(),
  676. SuggestedPredefines,
  677. PP.getLangOpts(),
  678. false);
  679. }
  680. /// Check the header search options deserialized from the control block
  681. /// against the header search options in an existing preprocessor.
  682. ///
  683. /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
  684. static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
  685. StringRef SpecificModuleCachePath,
  686. StringRef ExistingModuleCachePath,
  687. DiagnosticsEngine *Diags,
  688. const LangOptions &LangOpts) {
  689. if (LangOpts.Modules) {
  690. if (SpecificModuleCachePath != ExistingModuleCachePath) {
  691. if (Diags)
  692. Diags->Report(diag::err_pch_modulecache_mismatch)
  693. << SpecificModuleCachePath << ExistingModuleCachePath;
  694. return true;
  695. }
  696. }
  697. return false;
  698. }
  699. bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
  700. StringRef SpecificModuleCachePath,
  701. bool Complain) {
  702. return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
  703. PP.getHeaderSearchInfo().getModuleCachePath(),
  704. Complain ? &Reader.Diags : nullptr,
  705. PP.getLangOpts());
  706. }
  707. void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
  708. PP.setCounterValue(Value);
  709. }
  710. //===----------------------------------------------------------------------===//
  711. // AST reader implementation
  712. //===----------------------------------------------------------------------===//
  713. void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
  714. bool TakeOwnership) {
  715. DeserializationListener = Listener;
  716. OwnsDeserializationListener = TakeOwnership;
  717. }
  718. unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
  719. return serialization::ComputeHash(Sel);
  720. }
  721. std::pair<unsigned, unsigned>
  722. ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
  723. using namespace llvm::support;
  724. unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
  725. unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
  726. return std::make_pair(KeyLen, DataLen);
  727. }
  728. ASTSelectorLookupTrait::internal_key_type
  729. ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
  730. using namespace llvm::support;
  731. SelectorTable &SelTable = Reader.getContext().Selectors;
  732. unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
  733. IdentifierInfo *FirstII = Reader.getLocalIdentifier(
  734. F, endian::readNext<uint32_t, little, unaligned>(d));
  735. if (N == 0)
  736. return SelTable.getNullarySelector(FirstII);
  737. else if (N == 1)
  738. return SelTable.getUnarySelector(FirstII);
  739. SmallVector<IdentifierInfo *, 16> Args;
  740. Args.push_back(FirstII);
  741. for (unsigned I = 1; I != N; ++I)
  742. Args.push_back(Reader.getLocalIdentifier(
  743. F, endian::readNext<uint32_t, little, unaligned>(d)));
  744. return SelTable.getSelector(N, Args.data());
  745. }
  746. ASTSelectorLookupTrait::data_type
  747. ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
  748. unsigned DataLen) {
  749. using namespace llvm::support;
  750. data_type Result;
  751. Result.ID = Reader.getGlobalSelectorID(
  752. F, endian::readNext<uint32_t, little, unaligned>(d));
  753. unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
  754. unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
  755. Result.InstanceBits = FullInstanceBits & 0x3;
  756. Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
  757. Result.FactoryBits = FullFactoryBits & 0x3;
  758. Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
  759. unsigned NumInstanceMethods = FullInstanceBits >> 3;
  760. unsigned NumFactoryMethods = FullFactoryBits >> 3;
  761. // Load instance methods
  762. for (unsigned I = 0; I != NumInstanceMethods; ++I) {
  763. if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
  764. F, endian::readNext<uint32_t, little, unaligned>(d)))
  765. Result.Instance.push_back(Method);
  766. }
  767. // Load factory methods
  768. for (unsigned I = 0; I != NumFactoryMethods; ++I) {
  769. if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
  770. F, endian::readNext<uint32_t, little, unaligned>(d)))
  771. Result.Factory.push_back(Method);
  772. }
  773. return Result;
  774. }
  775. unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
  776. return llvm::djbHash(a);
  777. }
  778. std::pair<unsigned, unsigned>
  779. ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
  780. using namespace llvm::support;
  781. unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
  782. unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
  783. return std::make_pair(KeyLen, DataLen);
  784. }
  785. ASTIdentifierLookupTraitBase::internal_key_type
  786. ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
  787. assert(n >= 2 && d[n-1] == '\0');
  788. return StringRef((const char*) d, n-1);
  789. }
  790. /// Whether the given identifier is "interesting".
  791. static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
  792. bool IsModule) {
  793. return II.hadMacroDefinition() ||
  794. II.isPoisoned() ||
  795. (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
  796. II.hasRevertedTokenIDToIdentifier() ||
  797. (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
  798. II.getFETokenInfo());
  799. }
  800. static bool readBit(unsigned &Bits) {
  801. bool Value = Bits & 0x1;
  802. Bits >>= 1;
  803. return Value;
  804. }
  805. IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
  806. using namespace llvm::support;
  807. unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
  808. return Reader.getGlobalIdentifierID(F, RawID >> 1);
  809. }
  810. static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
  811. if (!II.isFromAST()) {
  812. II.setIsFromAST();
  813. bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
  814. if (isInterestingIdentifier(Reader, II, IsModule))
  815. II.setChangedSinceDeserialization();
  816. }
  817. }
  818. IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
  819. const unsigned char* d,
  820. unsigned DataLen) {
  821. using namespace llvm::support;
  822. unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
  823. bool IsInteresting = RawID & 0x01;
  824. // Wipe out the "is interesting" bit.
  825. RawID = RawID >> 1;
  826. // Build the IdentifierInfo and link the identifier ID with it.
  827. IdentifierInfo *II = KnownII;
  828. if (!II) {
  829. II = &Reader.getIdentifierTable().getOwn(k);
  830. KnownII = II;
  831. }
  832. markIdentifierFromAST(Reader, *II);
  833. Reader.markIdentifierUpToDate(II);
  834. IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
  835. if (!IsInteresting) {
  836. // For uninteresting identifiers, there's nothing else to do. Just notify
  837. // the reader that we've finished loading this identifier.
  838. Reader.SetIdentifierInfo(ID, II);
  839. return II;
  840. }
  841. unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
  842. unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
  843. bool CPlusPlusOperatorKeyword = readBit(Bits);
  844. bool HasRevertedTokenIDToIdentifier = readBit(Bits);
  845. bool HasRevertedBuiltin = readBit(Bits);
  846. bool Poisoned = readBit(Bits);
  847. bool ExtensionToken = readBit(Bits);
  848. bool HadMacroDefinition = readBit(Bits);
  849. assert(Bits == 0 && "Extra bits in the identifier?");
  850. DataLen -= 8;
  851. // Set or check the various bits in the IdentifierInfo structure.
  852. // Token IDs are read-only.
  853. if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
  854. II->revertTokenIDToIdentifier();
  855. if (!F.isModule())
  856. II->setObjCOrBuiltinID(ObjCOrBuiltinID);
  857. else if (HasRevertedBuiltin && II->getBuiltinID()) {
  858. II->revertBuiltin();
  859. assert((II->hasRevertedBuiltin() ||
  860. II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
  861. "Incorrect ObjC keyword or builtin ID");
  862. }
  863. assert(II->isExtensionToken() == ExtensionToken &&
  864. "Incorrect extension token flag");
  865. (void)ExtensionToken;
  866. if (Poisoned)
  867. II->setIsPoisoned(true);
  868. assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
  869. "Incorrect C++ operator keyword flag");
  870. (void)CPlusPlusOperatorKeyword;
  871. // If this identifier is a macro, deserialize the macro
  872. // definition.
  873. if (HadMacroDefinition) {
  874. uint32_t MacroDirectivesOffset =
  875. endian::readNext<uint32_t, little, unaligned>(d);
  876. DataLen -= 4;
  877. Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
  878. }
  879. Reader.SetIdentifierInfo(ID, II);
  880. // Read all of the declarations visible at global scope with this
  881. // name.
  882. if (DataLen > 0) {
  883. SmallVector<uint32_t, 4> DeclIDs;
  884. for (; DataLen > 0; DataLen -= 4)
  885. DeclIDs.push_back(Reader.getGlobalDeclID(
  886. F, endian::readNext<uint32_t, little, unaligned>(d)));
  887. Reader.SetGloballyVisibleDecls(II, DeclIDs);
  888. }
  889. return II;
  890. }
  891. DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
  892. : Kind(Name.getNameKind()) {
  893. switch (Kind) {
  894. case DeclarationName::Identifier:
  895. Data = (uint64_t)Name.getAsIdentifierInfo();
  896. break;
  897. case DeclarationName::ObjCZeroArgSelector:
  898. case DeclarationName::ObjCOneArgSelector:
  899. case DeclarationName::ObjCMultiArgSelector:
  900. Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
  901. break;
  902. case DeclarationName::CXXOperatorName:
  903. Data = Name.getCXXOverloadedOperator();
  904. break;
  905. case DeclarationName::CXXLiteralOperatorName:
  906. Data = (uint64_t)Name.getCXXLiteralIdentifier();
  907. break;
  908. case DeclarationName::CXXDeductionGuideName:
  909. Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
  910. ->getDeclName().getAsIdentifierInfo();
  911. break;
  912. case DeclarationName::CXXConstructorName:
  913. case DeclarationName::CXXDestructorName:
  914. case DeclarationName::CXXConversionFunctionName:
  915. case DeclarationName::CXXUsingDirective:
  916. Data = 0;
  917. break;
  918. }
  919. }
  920. unsigned DeclarationNameKey::getHash() const {
  921. llvm::FoldingSetNodeID ID;
  922. ID.AddInteger(Kind);
  923. switch (Kind) {
  924. case DeclarationName::Identifier:
  925. case DeclarationName::CXXLiteralOperatorName:
  926. case DeclarationName::CXXDeductionGuideName:
  927. ID.AddString(((IdentifierInfo*)Data)->getName());
  928. break;
  929. case DeclarationName::ObjCZeroArgSelector:
  930. case DeclarationName::ObjCOneArgSelector:
  931. case DeclarationName::ObjCMultiArgSelector:
  932. ID.AddInteger(serialization::ComputeHash(Selector(Data)));
  933. break;
  934. case DeclarationName::CXXOperatorName:
  935. ID.AddInteger((OverloadedOperatorKind)Data);
  936. break;
  937. case DeclarationName::CXXConstructorName:
  938. case DeclarationName::CXXDestructorName:
  939. case DeclarationName::CXXConversionFunctionName:
  940. case DeclarationName::CXXUsingDirective:
  941. break;
  942. }
  943. return ID.ComputeHash();
  944. }
  945. ModuleFile *
  946. ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
  947. using namespace llvm::support;
  948. uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
  949. return Reader.getLocalModuleFile(F, ModuleFileID);
  950. }
  951. std::pair<unsigned, unsigned>
  952. ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
  953. using namespace llvm::support;
  954. unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
  955. unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
  956. return std::make_pair(KeyLen, DataLen);
  957. }
  958. ASTDeclContextNameLookupTrait::internal_key_type
  959. ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
  960. using namespace llvm::support;
  961. auto Kind = (DeclarationName::NameKind)*d++;
  962. uint64_t Data;
  963. switch (Kind) {
  964. case DeclarationName::Identifier:
  965. case DeclarationName::CXXLiteralOperatorName:
  966. case DeclarationName::CXXDeductionGuideName:
  967. Data = (uint64_t)Reader.getLocalIdentifier(
  968. F, endian::readNext<uint32_t, little, unaligned>(d));
  969. break;
  970. case DeclarationName::ObjCZeroArgSelector:
  971. case DeclarationName::ObjCOneArgSelector:
  972. case DeclarationName::ObjCMultiArgSelector:
  973. Data =
  974. (uint64_t)Reader.getLocalSelector(
  975. F, endian::readNext<uint32_t, little, unaligned>(
  976. d)).getAsOpaquePtr();
  977. break;
  978. case DeclarationName::CXXOperatorName:
  979. Data = *d++; // OverloadedOperatorKind
  980. break;
  981. case DeclarationName::CXXConstructorName:
  982. case DeclarationName::CXXDestructorName:
  983. case DeclarationName::CXXConversionFunctionName:
  984. case DeclarationName::CXXUsingDirective:
  985. Data = 0;
  986. break;
  987. }
  988. return DeclarationNameKey(Kind, Data);
  989. }
  990. void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
  991. const unsigned char *d,
  992. unsigned DataLen,
  993. data_type_builder &Val) {
  994. using namespace llvm::support;
  995. for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
  996. uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
  997. Val.insert(Reader.getGlobalDeclID(F, LocalID));
  998. }
  999. }
  1000. bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
  1001. BitstreamCursor &Cursor,
  1002. uint64_t Offset,
  1003. DeclContext *DC) {
  1004. assert(Offset != 0);
  1005. SavedStreamPosition SavedPosition(Cursor);
  1006. if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
  1007. Error(std::move(Err));
  1008. return true;
  1009. }
  1010. RecordData Record;
  1011. StringRef Blob;
  1012. Expected<unsigned> MaybeCode = Cursor.ReadCode();
  1013. if (!MaybeCode) {
  1014. Error(MaybeCode.takeError());
  1015. return true;
  1016. }
  1017. unsigned Code = MaybeCode.get();
  1018. Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
  1019. if (!MaybeRecCode) {
  1020. Error(MaybeRecCode.takeError());
  1021. return true;
  1022. }
  1023. unsigned RecCode = MaybeRecCode.get();
  1024. if (RecCode != DECL_CONTEXT_LEXICAL) {
  1025. Error("Expected lexical block");
  1026. return true;
  1027. }
  1028. assert(!isa<TranslationUnitDecl>(DC) &&
  1029. "expected a TU_UPDATE_LEXICAL record for TU");
  1030. // If we are handling a C++ class template instantiation, we can see multiple
  1031. // lexical updates for the same record. It's important that we select only one
  1032. // of them, so that field numbering works properly. Just pick the first one we
  1033. // see.
  1034. auto &Lex = LexicalDecls[DC];
  1035. if (!Lex.first) {
  1036. Lex = std::make_pair(
  1037. &M, llvm::makeArrayRef(
  1038. reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
  1039. Blob.data()),
  1040. Blob.size() / 4));
  1041. }
  1042. DC->setHasExternalLexicalStorage(true);
  1043. return false;
  1044. }
  1045. bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
  1046. BitstreamCursor &Cursor,
  1047. uint64_t Offset,
  1048. DeclID ID) {
  1049. assert(Offset != 0);
  1050. SavedStreamPosition SavedPosition(Cursor);
  1051. if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
  1052. Error(std::move(Err));
  1053. return true;
  1054. }
  1055. RecordData Record;
  1056. StringRef Blob;
  1057. Expected<unsigned> MaybeCode = Cursor.ReadCode();
  1058. if (!MaybeCode) {
  1059. Error(MaybeCode.takeError());
  1060. return true;
  1061. }
  1062. unsigned Code = MaybeCode.get();
  1063. Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
  1064. if (!MaybeRecCode) {
  1065. Error(MaybeRecCode.takeError());
  1066. return true;
  1067. }
  1068. unsigned RecCode = MaybeRecCode.get();
  1069. if (RecCode != DECL_CONTEXT_VISIBLE) {
  1070. Error("Expected visible lookup table block");
  1071. return true;
  1072. }
  1073. // We can't safely determine the primary context yet, so delay attaching the
  1074. // lookup table until we're done with recursive deserialization.
  1075. auto *Data = (const unsigned char*)Blob.data();
  1076. PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
  1077. return false;
  1078. }
  1079. void ASTReader::Error(StringRef Msg) const {
  1080. Error(diag::err_fe_pch_malformed, Msg);
  1081. if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
  1082. !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
  1083. Diag(diag::note_module_cache_path)
  1084. << PP.getHeaderSearchInfo().getModuleCachePath();
  1085. }
  1086. }
  1087. void ASTReader::Error(unsigned DiagID,
  1088. StringRef Arg1, StringRef Arg2) const {
  1089. if (Diags.isDiagnosticInFlight())
  1090. Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
  1091. else
  1092. Diag(DiagID) << Arg1 << Arg2;
  1093. }
  1094. void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
  1095. unsigned Select) const {
  1096. if (!Diags.isDiagnosticInFlight())
  1097. Diag(DiagID) << Arg1 << Arg2 << Select;
  1098. }
  1099. void ASTReader::Error(llvm::Error &&Err) const {
  1100. Error(toString(std::move(Err)));
  1101. }
  1102. //===----------------------------------------------------------------------===//
  1103. // Source Manager Deserialization
  1104. //===----------------------------------------------------------------------===//
  1105. /// Read the line table in the source manager block.
  1106. /// \returns true if there was an error.
  1107. bool ASTReader::ParseLineTable(ModuleFile &F,
  1108. const RecordData &Record) {
  1109. unsigned Idx = 0;
  1110. LineTableInfo &LineTable = SourceMgr.getLineTable();
  1111. // Parse the file names
  1112. std::map<int, int> FileIDs;
  1113. FileIDs[-1] = -1; // For unspecified filenames.
  1114. for (unsigned I = 0; Record[Idx]; ++I) {
  1115. // Extract the file name
  1116. auto Filename = ReadPath(F, Record, Idx);
  1117. FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
  1118. }
  1119. ++Idx;
  1120. // Parse the line entries
  1121. std::vector<LineEntry> Entries;
  1122. while (Idx < Record.size()) {
  1123. int FID = Record[Idx++];
  1124. assert(FID >= 0 && "Serialized line entries for non-local file.");
  1125. // Remap FileID from 1-based old view.
  1126. FID += F.SLocEntryBaseID - 1;
  1127. // Extract the line entries
  1128. unsigned NumEntries = Record[Idx++];
  1129. assert(NumEntries && "no line entries for file ID");
  1130. Entries.clear();
  1131. Entries.reserve(NumEntries);
  1132. for (unsigned I = 0; I != NumEntries; ++I) {
  1133. unsigned FileOffset = Record[Idx++];
  1134. unsigned LineNo = Record[Idx++];
  1135. int FilenameID = FileIDs[Record[Idx++]];
  1136. SrcMgr::CharacteristicKind FileKind
  1137. = (SrcMgr::CharacteristicKind)Record[Idx++];
  1138. unsigned IncludeOffset = Record[Idx++];
  1139. Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
  1140. FileKind, IncludeOffset));
  1141. }
  1142. LineTable.AddEntry(FileID::get(FID), Entries);
  1143. }
  1144. return false;
  1145. }
  1146. /// Read a source manager block
  1147. bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
  1148. using namespace SrcMgr;
  1149. BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
  1150. // Set the source-location entry cursor to the current position in
  1151. // the stream. This cursor will be used to read the contents of the
  1152. // source manager block initially, and then lazily read
  1153. // source-location entries as needed.
  1154. SLocEntryCursor = F.Stream;
  1155. // The stream itself is going to skip over the source manager block.
  1156. if (llvm::Error Err = F.Stream.SkipBlock()) {
  1157. Error(std::move(Err));
  1158. return true;
  1159. }
  1160. // Enter the source manager block.
  1161. if (llvm::Error Err =
  1162. SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
  1163. Error(std::move(Err));
  1164. return true;
  1165. }
  1166. RecordData Record;
  1167. while (true) {
  1168. Expected<llvm::BitstreamEntry> MaybeE =
  1169. SLocEntryCursor.advanceSkippingSubblocks();
  1170. if (!MaybeE) {
  1171. Error(MaybeE.takeError());
  1172. return true;
  1173. }
  1174. llvm::BitstreamEntry E = MaybeE.get();
  1175. switch (E.Kind) {
  1176. case llvm::BitstreamEntry::SubBlock: // Handled for us already.
  1177. case llvm::BitstreamEntry::Error:
  1178. Error("malformed block record in AST file");
  1179. return true;
  1180. case llvm::BitstreamEntry::EndBlock:
  1181. return false;
  1182. case llvm::BitstreamEntry::Record:
  1183. // The interesting case.
  1184. break;
  1185. }
  1186. // Read a record.
  1187. Record.clear();
  1188. StringRef Blob;
  1189. Expected<unsigned> MaybeRecord =
  1190. SLocEntryCursor.readRecord(E.ID, Record, &Blob);
  1191. if (!MaybeRecord) {
  1192. Error(MaybeRecord.takeError());
  1193. return true;
  1194. }
  1195. switch (MaybeRecord.get()) {
  1196. default: // Default behavior: ignore.
  1197. break;
  1198. case SM_SLOC_FILE_ENTRY:
  1199. case SM_SLOC_BUFFER_ENTRY:
  1200. case SM_SLOC_EXPANSION_ENTRY:
  1201. // Once we hit one of the source location entries, we're done.
  1202. return false;
  1203. }
  1204. }
  1205. }
  1206. /// If a header file is not found at the path that we expect it to be
  1207. /// and the PCH file was moved from its original location, try to resolve the
  1208. /// file by assuming that header+PCH were moved together and the header is in
  1209. /// the same place relative to the PCH.
  1210. static std::string
  1211. resolveFileRelativeToOriginalDir(const std::string &Filename,
  1212. const std::string &OriginalDir,
  1213. const std::string &CurrDir) {
  1214. assert(OriginalDir != CurrDir &&
  1215. "No point trying to resolve the file if the PCH dir didn't change");
  1216. using namespace llvm::sys;
  1217. SmallString<128> filePath(Filename);
  1218. fs::make_absolute(filePath);
  1219. assert(path::is_absolute(OriginalDir));
  1220. SmallString<128> currPCHPath(CurrDir);
  1221. path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
  1222. fileDirE = path::end(path::parent_path(filePath));
  1223. path::const_iterator origDirI = path::begin(OriginalDir),
  1224. origDirE = path::end(OriginalDir);
  1225. // Skip the common path components from filePath and OriginalDir.
  1226. while (fileDirI != fileDirE && origDirI != origDirE &&
  1227. *fileDirI == *origDirI) {
  1228. ++fileDirI;
  1229. ++origDirI;
  1230. }
  1231. for (; origDirI != origDirE; ++origDirI)
  1232. path::append(currPCHPath, "..");
  1233. path::append(currPCHPath, fileDirI, fileDirE);
  1234. path::append(currPCHPath, path::filename(Filename));
  1235. return currPCHPath.str();
  1236. }
  1237. bool ASTReader::ReadSLocEntry(int ID) {
  1238. if (ID == 0)
  1239. return false;
  1240. if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
  1241. Error("source location entry ID out-of-range for AST file");
  1242. return true;
  1243. }
  1244. // Local helper to read the (possibly-compressed) buffer data following the
  1245. // entry record.
  1246. auto ReadBuffer = [this](
  1247. BitstreamCursor &SLocEntryCursor,
  1248. StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
  1249. RecordData Record;
  1250. StringRef Blob;
  1251. Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
  1252. if (!MaybeCode) {
  1253. Error(MaybeCode.takeError());
  1254. return nullptr;
  1255. }
  1256. unsigned Code = MaybeCode.get();
  1257. Expected<unsigned> MaybeRecCode =
  1258. SLocEntryCursor.readRecord(Code, Record, &Blob);
  1259. if (!MaybeRecCode) {
  1260. Error(MaybeRecCode.takeError());
  1261. return nullptr;
  1262. }
  1263. unsigned RecCode = MaybeRecCode.get();
  1264. if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
  1265. if (!llvm::zlib::isAvailable()) {
  1266. Error("zlib is not available");
  1267. return nullptr;
  1268. }
  1269. SmallString<0> Uncompressed;
  1270. if (llvm::Error E =
  1271. llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
  1272. Error("could not decompress embedded file contents: " +
  1273. llvm::toString(std::move(E)));
  1274. return nullptr;
  1275. }
  1276. return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
  1277. } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
  1278. return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
  1279. } else {
  1280. Error("AST record has invalid code");
  1281. return nullptr;
  1282. }
  1283. };
  1284. ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
  1285. if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
  1286. F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
  1287. Error(std::move(Err));
  1288. return true;
  1289. }
  1290. BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
  1291. unsigned BaseOffset = F->SLocEntryBaseOffset;
  1292. ++NumSLocEntriesRead;
  1293. Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
  1294. if (!MaybeEntry) {
  1295. Error(MaybeEntry.takeError());
  1296. return true;
  1297. }
  1298. llvm::BitstreamEntry Entry = MaybeEntry.get();
  1299. if (Entry.Kind != llvm::BitstreamEntry::Record) {
  1300. Error("incorrectly-formatted source location entry in AST file");
  1301. return true;
  1302. }
  1303. RecordData Record;
  1304. StringRef Blob;
  1305. Expected<unsigned> MaybeSLOC =
  1306. SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
  1307. if (!MaybeSLOC) {
  1308. Error(MaybeSLOC.takeError());
  1309. return true;
  1310. }
  1311. switch (MaybeSLOC.get()) {
  1312. default:
  1313. Error("incorrectly-formatted source location entry in AST file");
  1314. return true;
  1315. case SM_SLOC_FILE_ENTRY: {
  1316. // We will detect whether a file changed and return 'Failure' for it, but
  1317. // we will also try to fail gracefully by setting up the SLocEntry.
  1318. unsigned InputID = Record[4];
  1319. InputFile IF = getInputFile(*F, InputID);
  1320. const FileEntry *File = IF.getFile();
  1321. bool OverriddenBuffer = IF.isOverridden();
  1322. // Note that we only check if a File was returned. If it was out-of-date
  1323. // we have complained but we will continue creating a FileID to recover
  1324. // gracefully.
  1325. if (!File)
  1326. return true;
  1327. SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
  1328. if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
  1329. // This is the module's main file.
  1330. IncludeLoc = getImportLocation(F);
  1331. }
  1332. SrcMgr::CharacteristicKind
  1333. FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
  1334. // FIXME: The FileID should be created from the FileEntryRef.
  1335. FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
  1336. ID, BaseOffset + Record[0]);
  1337. SrcMgr::FileInfo &FileInfo =
  1338. const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
  1339. FileInfo.NumCreatedFIDs = Record[5];
  1340. if (Record[3])
  1341. FileInfo.setHasLineDirectives();
  1342. unsigned NumFileDecls = Record[7];
  1343. if (NumFileDecls && ContextObj) {
  1344. const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
  1345. assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
  1346. FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
  1347. NumFileDecls));
  1348. }
  1349. const SrcMgr::ContentCache *ContentCache
  1350. = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
  1351. if (OverriddenBuffer && !ContentCache->BufferOverridden &&
  1352. ContentCache->ContentsEntry == ContentCache->OrigEntry &&
  1353. !ContentCache->getRawBuffer()) {
  1354. auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
  1355. if (!Buffer)
  1356. return true;
  1357. SourceMgr.overrideFileContents(File, std::move(Buffer));
  1358. }
  1359. break;
  1360. }
  1361. case SM_SLOC_BUFFER_ENTRY: {
  1362. const char *Name = Blob.data();
  1363. unsigned Offset = Record[0];
  1364. SrcMgr::CharacteristicKind
  1365. FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
  1366. SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
  1367. if (IncludeLoc.isInvalid() && F->isModule()) {
  1368. IncludeLoc = getImportLocation(F);
  1369. }
  1370. auto Buffer = ReadBuffer(SLocEntryCursor, Name);
  1371. if (!Buffer)
  1372. return true;
  1373. SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
  1374. BaseOffset + Offset, IncludeLoc);
  1375. break;
  1376. }
  1377. case SM_SLOC_EXPANSION_ENTRY: {
  1378. SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
  1379. SourceMgr.createExpansionLoc(SpellingLoc,
  1380. ReadSourceLocation(*F, Record[2]),
  1381. ReadSourceLocation(*F, Record[3]),
  1382. Record[5],
  1383. Record[4],
  1384. ID,
  1385. BaseOffset + Record[0]);
  1386. break;
  1387. }
  1388. }
  1389. return false;
  1390. }
  1391. std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
  1392. if (ID == 0)
  1393. return std::make_pair(SourceLocation(), "");
  1394. if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
  1395. Error("source location entry ID out-of-range for AST file");
  1396. return std::make_pair(SourceLocation(), "");
  1397. }
  1398. // Find which module file this entry lands in.
  1399. ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
  1400. if (!M->isModule())
  1401. return std::make_pair(SourceLocation(), "");
  1402. // FIXME: Can we map this down to a particular submodule? That would be
  1403. // ideal.
  1404. return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
  1405. }
  1406. /// Find the location where the module F is imported.
  1407. SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
  1408. if (F->ImportLoc.isValid())
  1409. return F->ImportLoc;
  1410. // Otherwise we have a PCH. It's considered to be "imported" at the first
  1411. // location of its includer.
  1412. if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
  1413. // Main file is the importer.
  1414. assert(SourceMgr.getMainFileID().isValid() && "missing main file");
  1415. return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
  1416. }
  1417. return F->ImportedBy[0]->FirstLoc;
  1418. }
  1419. /// Enter a subblock of the specified BlockID with the specified cursor. Read
  1420. /// the abbreviations that are at the top of the block and then leave the cursor
  1421. /// pointing into the block.
  1422. bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
  1423. if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
  1424. // FIXME this drops errors on the floor.
  1425. consumeError(std::move(Err));
  1426. return true;
  1427. }
  1428. while (true) {
  1429. uint64_t Offset = Cursor.GetCurrentBitNo();
  1430. Expected<unsigned> MaybeCode = Cursor.ReadCode();
  1431. if (!MaybeCode) {
  1432. // FIXME this drops errors on the floor.
  1433. consumeError(MaybeCode.takeError());
  1434. return true;
  1435. }
  1436. unsigned Code = MaybeCode.get();
  1437. // We expect all abbrevs to be at the start of the block.
  1438. if (Code != llvm::bitc::DEFINE_ABBREV) {
  1439. if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
  1440. // FIXME this drops errors on the floor.
  1441. consumeError(std::move(Err));
  1442. return true;
  1443. }
  1444. return false;
  1445. }
  1446. if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
  1447. // FIXME this drops errors on the floor.
  1448. consumeError(std::move(Err));
  1449. return true;
  1450. }
  1451. }
  1452. }
  1453. Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
  1454. unsigned &Idx) {
  1455. Token Tok;
  1456. Tok.startToken();
  1457. Tok.setLocation(ReadSourceLocation(F, Record, Idx));
  1458. Tok.setLength(Record[Idx++]);
  1459. if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
  1460. Tok.setIdentifierInfo(II);
  1461. Tok.setKind((tok::TokenKind)Record[Idx++]);
  1462. Tok.setFlag((Token::TokenFlags)Record[Idx++]);
  1463. return Tok;
  1464. }
  1465. MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
  1466. BitstreamCursor &Stream = F.MacroCursor;
  1467. // Keep track of where we are in the stream, then jump back there
  1468. // after reading this macro.
  1469. SavedStreamPosition SavedPosition(Stream);
  1470. if (llvm::Error Err = Stream.JumpToBit(Offset)) {
  1471. // FIXME this drops errors on the floor.
  1472. consumeError(std::move(Err));
  1473. return nullptr;
  1474. }
  1475. RecordData Record;
  1476. SmallVector<IdentifierInfo*, 16> MacroParams;
  1477. MacroInfo *Macro = nullptr;
  1478. while (true) {
  1479. // Advance to the next record, but if we get to the end of the block, don't
  1480. // pop it (removing all the abbreviations from the cursor) since we want to
  1481. // be able to reseek within the block and read entries.
  1482. unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
  1483. Expected<llvm::BitstreamEntry> MaybeEntry =
  1484. Stream.advanceSkippingSubblocks(Flags);
  1485. if (!MaybeEntry) {
  1486. Error(MaybeEntry.takeError());
  1487. return Macro;
  1488. }
  1489. llvm::BitstreamEntry Entry = MaybeEntry.get();
  1490. switch (Entry.Kind) {
  1491. case llvm::BitstreamEntry::SubBlock: // Handled for us already.
  1492. case llvm::BitstreamEntry::Error:
  1493. Error("malformed block record in AST file");
  1494. return Macro;
  1495. case llvm::BitstreamEntry::EndBlock:
  1496. return Macro;
  1497. case llvm::BitstreamEntry::Record:
  1498. // The interesting case.
  1499. break;
  1500. }
  1501. // Read a record.
  1502. Record.clear();
  1503. PreprocessorRecordTypes RecType;
  1504. if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
  1505. RecType = (PreprocessorRecordTypes)MaybeRecType.get();
  1506. else {
  1507. Error(MaybeRecType.takeError());
  1508. return Macro;
  1509. }
  1510. switch (RecType) {
  1511. case PP_MODULE_MACRO:
  1512. case PP_MACRO_DIRECTIVE_HISTORY:
  1513. return Macro;
  1514. case PP_MACRO_OBJECT_LIKE:
  1515. case PP_MACRO_FUNCTION_LIKE: {
  1516. // If we already have a macro, that means that we've hit the end
  1517. // of the definition of the macro we were looking for. We're
  1518. // done.
  1519. if (Macro)
  1520. return Macro;
  1521. unsigned NextIndex = 1; // Skip identifier ID.
  1522. SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
  1523. MacroInfo *MI = PP.AllocateMacroInfo(Loc);
  1524. MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
  1525. MI->setIsUsed(Record[NextIndex++]);
  1526. MI->setUsedForHeaderGuard(Record[NextIndex++]);
  1527. if (RecType == PP_MACRO_FUNCTION_LIKE) {
  1528. // Decode function-like macro info.
  1529. bool isC99VarArgs = Record[NextIndex++];
  1530. bool isGNUVarArgs = Record[NextIndex++];
  1531. bool hasCommaPasting = Record[NextIndex++];
  1532. MacroParams.clear();
  1533. unsigned NumArgs = Record[NextIndex++];
  1534. for (unsigned i = 0; i != NumArgs; ++i)
  1535. MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
  1536. // Install function-like macro info.
  1537. MI->setIsFunctionLike();
  1538. if (isC99VarArgs) MI->setIsC99Varargs();
  1539. if (isGNUVarArgs) MI->setIsGNUVarargs();
  1540. if (hasCommaPasting) MI->setHasCommaPasting();
  1541. MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
  1542. }
  1543. // Remember that we saw this macro last so that we add the tokens that
  1544. // form its body to it.
  1545. Macro = MI;
  1546. if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
  1547. Record[NextIndex]) {
  1548. // We have a macro definition. Register the association
  1549. PreprocessedEntityID
  1550. GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
  1551. PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
  1552. PreprocessingRecord::PPEntityID PPID =
  1553. PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
  1554. MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
  1555. PPRec.getPreprocessedEntity(PPID));
  1556. if (PPDef)
  1557. PPRec.RegisterMacroDefinition(Macro, PPDef);
  1558. }
  1559. ++NumMacrosRead;
  1560. break;
  1561. }
  1562. case PP_TOKEN: {
  1563. // If we see a TOKEN before a PP_MACRO_*, then the file is
  1564. // erroneous, just pretend we didn't see this.
  1565. if (!Macro) break;
  1566. unsigned Idx = 0;
  1567. Token Tok = ReadToken(F, Record, Idx);
  1568. Macro->AddTokenToBody(Tok);
  1569. break;
  1570. }
  1571. }
  1572. }
  1573. }
  1574. PreprocessedEntityID
  1575. ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
  1576. unsigned LocalID) const {
  1577. if (!M.ModuleOffsetMap.empty())
  1578. ReadModuleOffsetMap(M);
  1579. ContinuousRangeMap<uint32_t, int, 2>::const_iterator
  1580. I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
  1581. assert(I != M.PreprocessedEntityRemap.end()
  1582. && "Invalid index into preprocessed entity index remap");
  1583. return LocalID + I->second;
  1584. }
  1585. unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
  1586. return llvm::hash_combine(ikey.Size, ikey.ModTime);
  1587. }
  1588. HeaderFileInfoTrait::internal_key_type
  1589. HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
  1590. internal_key_type ikey = {FE->getSize(),
  1591. M.HasTimestamps ? FE->getModificationTime() : 0,
  1592. FE->getName(), /*Imported*/ false};
  1593. return ikey;
  1594. }
  1595. bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
  1596. if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
  1597. return false;
  1598. if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
  1599. return true;
  1600. // Determine whether the actual files are equivalent.
  1601. FileManager &FileMgr = Reader.getFileManager();
  1602. auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
  1603. if (!Key.Imported) {
  1604. if (auto File = FileMgr.getFile(Key.Filename))
  1605. return *File;
  1606. return nullptr;
  1607. }
  1608. std::string Resolved = Key.Filename;
  1609. Reader.ResolveImportedPath(M, Resolved);
  1610. if (auto File = FileMgr.getFile(Resolved))
  1611. return *File;
  1612. return nullptr;
  1613. };
  1614. const FileEntry *FEA = GetFile(a);
  1615. const FileEntry *FEB = GetFile(b);
  1616. return FEA && FEA == FEB;
  1617. }
  1618. std::pair<unsigned, unsigned>
  1619. HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
  1620. using namespace llvm::support;
  1621. unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
  1622. unsigned DataLen = (unsigned) *d++;
  1623. return std::make_pair(KeyLen, DataLen);
  1624. }
  1625. HeaderFileInfoTrait::internal_key_type
  1626. HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
  1627. using namespace llvm::support;
  1628. internal_key_type ikey;
  1629. ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
  1630. ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
  1631. ikey.Filename = (const char *)d;
  1632. ikey.Imported = true;
  1633. return ikey;
  1634. }
  1635. HeaderFileInfoTrait::data_type
  1636. HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
  1637. unsigned DataLen) {
  1638. using namespace llvm::support;
  1639. const unsigned char *End = d + DataLen;
  1640. HeaderFileInfo HFI;
  1641. unsigned Flags = *d++;
  1642. // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
  1643. HFI.isImport |= (Flags >> 5) & 0x01;
  1644. HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
  1645. HFI.DirInfo = (Flags >> 1) & 0x07;
  1646. HFI.IndexHeaderMapHeader = Flags & 0x01;
  1647. // FIXME: Find a better way to handle this. Maybe just store a
  1648. // "has been included" flag?
  1649. HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
  1650. HFI.NumIncludes);
  1651. HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
  1652. M, endian::readNext<uint32_t, little, unaligned>(d));
  1653. if (unsigned FrameworkOffset =
  1654. endian::readNext<uint32_t, little, unaligned>(d)) {
  1655. // The framework offset is 1 greater than the actual offset,
  1656. // since 0 is used as an indicator for "no framework name".
  1657. StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
  1658. HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
  1659. }
  1660. assert((End - d) % 4 == 0 &&
  1661. "Wrong data length in HeaderFileInfo deserialization");
  1662. while (d != End) {
  1663. uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
  1664. auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
  1665. LocalSMID >>= 2;
  1666. // This header is part of a module. Associate it with the module to enable
  1667. // implicit module import.
  1668. SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
  1669. Module *Mod = Reader.getSubmodule(GlobalSMID);
  1670. FileManager &FileMgr = Reader.getFileManager();
  1671. ModuleMap &ModMap =
  1672. Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
  1673. std::string Filename = key.Filename;
  1674. if (key.Imported)
  1675. Reader.ResolveImportedPath(M, Filename);
  1676. // FIXME: This is not always the right filename-as-written, but we're not
  1677. // going to use this information to rebuild the module, so it doesn't make
  1678. // a lot of difference.
  1679. Module::Header H = { key.Filename, *FileMgr.getFile(Filename) };
  1680. ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
  1681. HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
  1682. }
  1683. // This HeaderFileInfo was externally loaded.
  1684. HFI.External = true;
  1685. HFI.IsValid = true;
  1686. return HFI;
  1687. }
  1688. void ASTReader::addPendingMacro(IdentifierInfo *II,
  1689. ModuleFile *M,
  1690. uint64_t MacroDirectivesOffset) {
  1691. assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
  1692. PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
  1693. }
  1694. void ASTReader::ReadDefinedMacros() {
  1695. // Note that we are loading defined macros.
  1696. Deserializing Macros(this);
  1697. for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
  1698. BitstreamCursor &MacroCursor = I.MacroCursor;
  1699. // If there was no preprocessor block, skip this file.
  1700. if (MacroCursor.getBitcodeBytes().empty())
  1701. continue;
  1702. BitstreamCursor Cursor = MacroCursor;
  1703. if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
  1704. Error(std::move(Err));
  1705. return;
  1706. }
  1707. RecordData Record;
  1708. while (true) {
  1709. Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
  1710. if (!MaybeE) {
  1711. Error(MaybeE.takeError());
  1712. return;
  1713. }
  1714. llvm::BitstreamEntry E = MaybeE.get();
  1715. switch (E.Kind) {
  1716. case llvm::BitstreamEntry::SubBlock: // Handled for us already.
  1717. case llvm::BitstreamEntry::Error:
  1718. Error("malformed block record in AST file");
  1719. return;
  1720. case llvm::BitstreamEntry::EndBlock:
  1721. goto NextCursor;
  1722. case llvm::BitstreamEntry::Record: {
  1723. Record.clear();
  1724. Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
  1725. if (!MaybeRecord) {
  1726. Error(MaybeRecord.takeError());
  1727. return;
  1728. }
  1729. switch (MaybeRecord.get()) {
  1730. default: // Default behavior: ignore.
  1731. break;
  1732. case PP_MACRO_OBJECT_LIKE:
  1733. case PP_MACRO_FUNCTION_LIKE: {
  1734. IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
  1735. if (II->isOutOfDate())
  1736. updateOutOfDateIdentifier(*II);
  1737. break;
  1738. }
  1739. case PP_TOKEN:
  1740. // Ignore tokens.
  1741. break;
  1742. }
  1743. break;
  1744. }
  1745. }
  1746. }
  1747. NextCursor: ;
  1748. }
  1749. }
  1750. namespace {
  1751. /// Visitor class used to look up identifirs in an AST file.
  1752. class IdentifierLookupVisitor {
  1753. StringRef Name;
  1754. unsigned NameHash;
  1755. unsigned PriorGeneration;
  1756. unsigned &NumIdentifierLookups;
  1757. unsigned &NumIdentifierLookupHits;
  1758. IdentifierInfo *Found = nullptr;
  1759. public:
  1760. IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
  1761. unsigned &NumIdentifierLookups,
  1762. unsigned &NumIdentifierLookupHits)
  1763. : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
  1764. PriorGeneration(PriorGeneration),
  1765. NumIdentifierLookups(NumIdentifierLookups),
  1766. NumIdentifierLookupHits(NumIdentifierLookupHits) {}
  1767. bool operator()(ModuleFile &M) {
  1768. // If we've already searched this module file, skip it now.
  1769. if (M.Generation <= PriorGeneration)
  1770. return true;
  1771. ASTIdentifierLookupTable *IdTable
  1772. = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
  1773. if (!IdTable)
  1774. return false;
  1775. ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
  1776. Found);
  1777. ++NumIdentifierLookups;
  1778. ASTIdentifierLookupTable::iterator Pos =
  1779. IdTable->find_hashed(Name, NameHash, &Trait);
  1780. if (Pos == IdTable->end())
  1781. return false;
  1782. // Dereferencing the iterator has the effect of building the
  1783. // IdentifierInfo node and populating it with the various
  1784. // declarations it needs.
  1785. ++NumIdentifierLookupHits;
  1786. Found = *Pos;
  1787. return true;
  1788. }
  1789. // Retrieve the identifier info found within the module
  1790. // files.
  1791. IdentifierInfo *getIdentifierInfo() const { return Found; }
  1792. };
  1793. } // namespace
  1794. void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
  1795. // Note that we are loading an identifier.
  1796. Deserializing AnIdentifier(this);
  1797. unsigned PriorGeneration = 0;
  1798. if (getContext().getLangOpts().Modules)
  1799. PriorGeneration = IdentifierGeneration[&II];
  1800. // If there is a global index, look there first to determine which modules
  1801. // provably do not have any results for this identifier.
  1802. GlobalModuleIndex::HitSet Hits;
  1803. GlobalModuleIndex::HitSet *HitsPtr = nullptr;
  1804. if (!loadGlobalIndex()) {
  1805. if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
  1806. HitsPtr = &Hits;
  1807. }
  1808. }
  1809. IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
  1810. NumIdentifierLookups,
  1811. NumIdentifierLookupHits);
  1812. ModuleMgr.visit(Visitor, HitsPtr);
  1813. markIdentifierUpToDate(&II);
  1814. }
  1815. void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
  1816. if (!II)
  1817. return;
  1818. II->setOutOfDate(false);
  1819. // Update the generation for this identifier.
  1820. if (getContext().getLangOpts().Modules)
  1821. IdentifierGeneration[II] = getGeneration();
  1822. }
  1823. void ASTReader::resolvePendingMacro(IdentifierInfo *II,
  1824. const PendingMacroInfo &PMInfo) {
  1825. ModuleFile &M = *PMInfo.M;
  1826. BitstreamCursor &Cursor = M.MacroCursor;
  1827. SavedStreamPosition SavedPosition(Cursor);
  1828. if (llvm::Error Err = Cursor.JumpToBit(PMInfo.MacroDirectivesOffset)) {
  1829. Error(std::move(Err));
  1830. return;
  1831. }
  1832. struct ModuleMacroRecord {
  1833. SubmoduleID SubModID;
  1834. MacroInfo *MI;
  1835. SmallVector<SubmoduleID, 8> Overrides;
  1836. };
  1837. llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
  1838. // We expect to see a sequence of PP_MODULE_MACRO records listing exported
  1839. // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
  1840. // macro histroy.
  1841. RecordData Record;
  1842. while (true) {
  1843. Expected<llvm::BitstreamEntry> MaybeEntry =
  1844. Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
  1845. if (!MaybeEntry) {
  1846. Error(MaybeEntry.takeError());
  1847. return;
  1848. }
  1849. llvm::BitstreamEntry Entry = MaybeEntry.get();
  1850. if (Entry.Kind != llvm::BitstreamEntry::Record) {
  1851. Error("malformed block record in AST file");
  1852. return;
  1853. }
  1854. Record.clear();
  1855. Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
  1856. if (!MaybePP) {
  1857. Error(MaybePP.takeError());
  1858. return;
  1859. }
  1860. switch ((PreprocessorRecordTypes)MaybePP.get()) {
  1861. case PP_MACRO_DIRECTIVE_HISTORY:
  1862. break;
  1863. case PP_MODULE_MACRO: {
  1864. ModuleMacros.push_back(ModuleMacroRecord());
  1865. auto &Info = ModuleMacros.back();
  1866. Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
  1867. Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
  1868. for (int I = 2, N = Record.size(); I != N; ++I)
  1869. Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
  1870. continue;
  1871. }
  1872. default:
  1873. Error("malformed block record in AST file");
  1874. return;
  1875. }
  1876. // We found the macro directive history; that's the last record
  1877. // for this macro.
  1878. break;
  1879. }
  1880. // Module macros are listed in reverse dependency order.
  1881. {
  1882. std::reverse(ModuleMacros.begin(), ModuleMacros.end());
  1883. llvm::SmallVector<ModuleMacro*, 8> Overrides;
  1884. for (auto &MMR : ModuleMacros) {
  1885. Overrides.clear();
  1886. for (unsigned ModID : MMR.Overrides) {
  1887. Module *Mod = getSubmodule(ModID);
  1888. auto *Macro = PP.getModuleMacro(Mod, II);
  1889. assert(Macro && "missing definition for overridden macro");
  1890. Overrides.push_back(Macro);
  1891. }
  1892. bool Inserted = false;
  1893. Module *Owner = getSubmodule(MMR.SubModID);
  1894. PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
  1895. }
  1896. }
  1897. // Don't read the directive history for a module; we don't have anywhere
  1898. // to put it.
  1899. if (M.isModule())
  1900. return;
  1901. // Deserialize the macro directives history in reverse source-order.
  1902. MacroDirective *Latest = nullptr, *Earliest = nullptr;
  1903. unsigned Idx = 0, N = Record.size();
  1904. while (Idx < N) {
  1905. MacroDirective *MD = nullptr;
  1906. SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
  1907. MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
  1908. switch (K) {
  1909. case MacroDirective::MD_Define: {
  1910. MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
  1911. MD = PP.AllocateDefMacroDirective(MI, Loc);
  1912. break;
  1913. }
  1914. case MacroDirective::MD_Undefine:
  1915. MD = PP.AllocateUndefMacroDirective(Loc);
  1916. break;
  1917. case MacroDirective::MD_Visibility:
  1918. bool isPublic = Record[Idx++];
  1919. MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
  1920. break;
  1921. }
  1922. if (!Latest)
  1923. Latest = MD;
  1924. if (Earliest)
  1925. Earliest->setPrevious(MD);
  1926. Earliest = MD;
  1927. }
  1928. if (Latest)
  1929. PP.setLoadedMacroDirective(II, Earliest, Latest);
  1930. }
  1931. ASTReader::InputFileInfo
  1932. ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
  1933. // Go find this input file.
  1934. BitstreamCursor &Cursor = F.InputFilesCursor;
  1935. SavedStreamPosition SavedPosition(Cursor);
  1936. if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
  1937. // FIXME this drops errors on the floor.
  1938. consumeError(std::move(Err));
  1939. }
  1940. Expected<unsigned> MaybeCode = Cursor.ReadCode();
  1941. if (!MaybeCode) {
  1942. // FIXME this drops errors on the floor.
  1943. consumeError(MaybeCode.takeError());
  1944. }
  1945. unsigned Code = MaybeCode.get();
  1946. RecordData Record;
  1947. StringRef Blob;
  1948. if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
  1949. assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
  1950. "invalid record type for input file");
  1951. else {
  1952. // FIXME this drops errors on the floor.
  1953. consumeError(Maybe.takeError());
  1954. }
  1955. assert(Record[0] == ID && "Bogus stored ID or offset");
  1956. InputFileInfo R;
  1957. R.StoredSize = static_cast<off_t>(Record[1]);
  1958. R.StoredTime = static_cast<time_t>(Record[2]);
  1959. R.Overridden = static_cast<bool>(Record[3]);
  1960. R.Transient = static_cast<bool>(Record[4]);
  1961. R.TopLevelModuleMap = static_cast<bool>(Record[5]);
  1962. R.Filename = Blob;
  1963. ResolveImportedPath(F, R.Filename);
  1964. Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
  1965. if (!MaybeEntry) // FIXME this drops errors on the floor.
  1966. consumeError(MaybeEntry.takeError());
  1967. llvm::BitstreamEntry Entry = MaybeEntry.get();
  1968. assert(Entry.Kind == llvm::BitstreamEntry::Record &&
  1969. "expected record type for input file hash");
  1970. Record.clear();
  1971. if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
  1972. assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
  1973. "invalid record type for input file hash");
  1974. else {
  1975. // FIXME this drops errors on the floor.
  1976. consumeError(Maybe.takeError());
  1977. }
  1978. R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
  1979. static_cast<uint64_t>(Record[0]);
  1980. return R;
  1981. }
  1982. static unsigned moduleKindForDiagnostic(ModuleKind Kind);
  1983. InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
  1984. // If this ID is bogus, just return an empty input file.
  1985. if (ID == 0 || ID > F.InputFilesLoaded.size())
  1986. return InputFile();
  1987. // If we've already loaded this input file, return it.
  1988. if (F.InputFilesLoaded[ID-1].getFile())
  1989. return F.InputFilesLoaded[ID-1];
  1990. if (F.InputFilesLoaded[ID-1].isNotFound())
  1991. return InputFile();
  1992. // Go find this input file.
  1993. BitstreamCursor &Cursor = F.InputFilesCursor;
  1994. SavedStreamPosition SavedPosition(Cursor);
  1995. if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
  1996. // FIXME this drops errors on the floor.
  1997. consumeError(std::move(Err));
  1998. }
  1999. InputFileInfo FI = readInputFileInfo(F, ID);
  2000. off_t StoredSize = FI.StoredSize;
  2001. time_t StoredTime = FI.StoredTime;
  2002. bool Overridden = FI.Overridden;
  2003. bool Transient = FI.Transient;
  2004. StringRef Filename = FI.Filename;
  2005. uint64_t StoredContentHash = FI.ContentHash;
  2006. const FileEntry *File = nullptr;
  2007. if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
  2008. File = *FE;
  2009. // If we didn't find the file, resolve it relative to the
  2010. // original directory from which this AST file was created.
  2011. if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
  2012. F.OriginalDir != F.BaseDirectory) {
  2013. std::string Resolved = resolveFileRelativeToOriginalDir(
  2014. Filename, F.OriginalDir, F.BaseDirectory);
  2015. if (!Resolved.empty())
  2016. if (auto FE = FileMgr.getFile(Resolved))
  2017. File = *FE;
  2018. }
  2019. // For an overridden file, create a virtual file with the stored
  2020. // size/timestamp.
  2021. if ((Overridden || Transient) && File == nullptr)
  2022. File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
  2023. if (File == nullptr) {
  2024. if (Complain) {
  2025. std::string ErrorStr = "could not find file '";
  2026. ErrorStr += Filename;
  2027. ErrorStr += "' referenced by AST file '";
  2028. ErrorStr += F.FileName;
  2029. ErrorStr += "'";
  2030. Error(ErrorStr);
  2031. }
  2032. // Record that we didn't find the file.
  2033. F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
  2034. return InputFile();
  2035. }
  2036. // Check if there was a request to override the contents of the file
  2037. // that was part of the precompiled header. Overriding such a file
  2038. // can lead to problems when lexing using the source locations from the
  2039. // PCH.
  2040. SourceManager &SM = getSourceManager();
  2041. // FIXME: Reject if the overrides are different.
  2042. if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
  2043. if (Complain)
  2044. Error(diag::err_fe_pch_file_overridden, Filename);
  2045. // After emitting the diagnostic, bypass the overriding file to recover
  2046. // (this creates a separate FileEntry).
  2047. File = SM.bypassFileContentsOverride(*File);
  2048. if (!File) {
  2049. F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
  2050. return InputFile();
  2051. }
  2052. }
  2053. enum ModificationType {
  2054. Size,
  2055. ModTime,
  2056. Content,
  2057. None,
  2058. };
  2059. auto HasInputFileChanged = [&]() {
  2060. if (StoredSize != File->getSize())
  2061. return ModificationType::Size;
  2062. if (!DisableValidation && StoredTime &&
  2063. StoredTime != File->getModificationTime()) {
  2064. // In case the modification time changes but not the content,
  2065. // accept the cached file as legit.
  2066. if (ValidateASTInputFilesContent &&
  2067. StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
  2068. auto MemBuffOrError = FileMgr.getBufferForFile(File);
  2069. if (!MemBuffOrError) {
  2070. if (!Complain)
  2071. return ModificationType::ModTime;
  2072. std::string ErrorStr = "could not get buffer for file '";
  2073. ErrorStr += File->getName();
  2074. ErrorStr += "'";
  2075. Error(ErrorStr);
  2076. return ModificationType::ModTime;
  2077. }
  2078. auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
  2079. if (StoredContentHash == static_cast<uint64_t>(ContentHash))
  2080. return ModificationType::None;
  2081. return ModificationType::Content;
  2082. }
  2083. return ModificationType::ModTime;
  2084. }
  2085. return ModificationType::None;
  2086. };
  2087. bool IsOutOfDate = false;
  2088. auto FileChange = HasInputFileChanged();
  2089. // For an overridden file, there is nothing to validate.
  2090. if (!Overridden && FileChange != ModificationType::None) {
  2091. if (Complain) {
  2092. // Build a list of the PCH imports that got us here (in reverse).
  2093. SmallVector<ModuleFile *, 4> ImportStack(1, &F);
  2094. while (!ImportStack.back()->ImportedBy.empty())
  2095. ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
  2096. // The top-level PCH is stale.
  2097. StringRef TopLevelPCHName(ImportStack.back()->FileName);
  2098. unsigned DiagnosticKind =
  2099. moduleKindForDiagnostic(ImportStack.back()->Kind);
  2100. if (DiagnosticKind == 0)
  2101. Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName,
  2102. (unsigned)FileChange);
  2103. else if (DiagnosticKind == 1)
  2104. Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName,
  2105. (unsigned)FileChange);
  2106. else
  2107. Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName,
  2108. (unsigned)FileChange);
  2109. // Print the import stack.
  2110. if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
  2111. Diag(diag::note_pch_required_by)
  2112. << Filename << ImportStack[0]->FileName;
  2113. for (unsigned I = 1; I < ImportStack.size(); ++I)
  2114. Diag(diag::note_pch_required_by)
  2115. << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
  2116. }
  2117. if (!Diags.isDiagnosticInFlight())
  2118. Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
  2119. }
  2120. IsOutOfDate = true;
  2121. }
  2122. // FIXME: If the file is overridden and we've already opened it,
  2123. // issue an error (or split it into a separate FileEntry).
  2124. InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
  2125. // Note that we've loaded this input file.
  2126. F.InputFilesLoaded[ID-1] = IF;
  2127. return IF;
  2128. }
  2129. /// If we are loading a relocatable PCH or module file, and the filename
  2130. /// is not an absolute path, add the system or module root to the beginning of
  2131. /// the file name.
  2132. void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
  2133. // Resolve relative to the base directory, if we have one.
  2134. if (!M.BaseDirectory.empty())
  2135. return ResolveImportedPath(Filename, M.BaseDirectory);
  2136. }
  2137. void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
  2138. if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
  2139. return;
  2140. SmallString<128> Buffer;
  2141. llvm::sys::path::append(Buffer, Prefix, Filename);
  2142. Filename.assign(Buffer.begin(), Buffer.end());
  2143. }
  2144. static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
  2145. switch (ARR) {
  2146. case ASTReader::Failure: return true;
  2147. case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
  2148. case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
  2149. case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
  2150. case ASTReader::ConfigurationMismatch:
  2151. return !(Caps & ASTReader::ARR_ConfigurationMismatch);
  2152. case ASTReader::HadErrors: return true;
  2153. case ASTReader::Success: return false;
  2154. }
  2155. llvm_unreachable("unknown ASTReadResult");
  2156. }
  2157. ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
  2158. BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
  2159. bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
  2160. std::string &SuggestedPredefines) {
  2161. if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
  2162. // FIXME this drops errors on the floor.
  2163. consumeError(std::move(Err));
  2164. return Failure;
  2165. }
  2166. // Read all of the records in the options block.
  2167. RecordData Record;
  2168. ASTReadResult Result = Success;
  2169. while (true) {
  2170. Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
  2171. if (!MaybeEntry) {
  2172. // FIXME this drops errors on the floor.
  2173. consumeError(MaybeEntry.takeError());
  2174. return Failure;
  2175. }
  2176. llvm::BitstreamEntry Entry = MaybeEntry.get();
  2177. switch (Entry.Kind) {
  2178. case llvm::BitstreamEntry::Error:
  2179. case llvm::BitstreamEntry::SubBlock:
  2180. return Failure;
  2181. case llvm::BitstreamEntry::EndBlock:
  2182. return Result;
  2183. case llvm::BitstreamEntry::Record:
  2184. // The interesting case.
  2185. break;
  2186. }
  2187. // Read and process a record.
  2188. Record.clear();
  2189. Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
  2190. if (!MaybeRecordType) {
  2191. // FIXME this drops errors on the floor.
  2192. consumeError(MaybeRecordType.takeError());
  2193. return Failure;
  2194. }
  2195. switch ((OptionsRecordTypes)MaybeRecordType.get()) {
  2196. case LANGUAGE_OPTIONS: {
  2197. bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
  2198. if (ParseLanguageOptions(Record, Complain, Listener,
  2199. AllowCompatibleConfigurationMismatch))
  2200. Result = ConfigurationMismatch;
  2201. break;
  2202. }
  2203. case TARGET_OPTIONS: {
  2204. bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
  2205. if (ParseTargetOptions(Record, Complain, Listener,
  2206. AllowCompatibleConfigurationMismatch))
  2207. Result = ConfigurationMismatch;
  2208. break;
  2209. }
  2210. case FILE_SYSTEM_OPTIONS: {
  2211. bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
  2212. if (!AllowCompatibleConfigurationMismatch &&
  2213. ParseFileSystemOptions(Record, Complain, Listener))
  2214. Result = ConfigurationMismatch;
  2215. break;
  2216. }
  2217. case HEADER_SEARCH_OPTIONS: {
  2218. bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
  2219. if (!AllowCompatibleConfigurationMismatch &&
  2220. ParseHeaderSearchOptions(Record, Complain, Listener))
  2221. Result = ConfigurationMismatch;
  2222. break;
  2223. }
  2224. case PREPROCESSOR_OPTIONS:
  2225. bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
  2226. if (!AllowCompatibleConfigurationMismatch &&
  2227. ParsePreprocessorOptions(Record, Complain, Listener,
  2228. SuggestedPredefines))
  2229. Result = ConfigurationMismatch;
  2230. break;
  2231. }
  2232. }
  2233. }
  2234. ASTReader::ASTReadResult
  2235. ASTReader::ReadControlBlock(ModuleFile &F,
  2236. SmallVectorImpl<ImportedModule> &Loaded,
  2237. const ModuleFile *ImportedBy,
  2238. unsigned ClientLoadCapabilities) {
  2239. BitstreamCursor &Stream = F.Stream;
  2240. ASTReadResult Result = Success;
  2241. if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
  2242. Error(std::move(Err));
  2243. return Failure;
  2244. }
  2245. // Lambda to read the unhashed control block the first time it's called.
  2246. //
  2247. // For PCM files, the unhashed control block cannot be read until after the
  2248. // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
  2249. // need to look ahead before reading the IMPORTS record. For consistency,
  2250. // this block is always read somehow (see BitstreamEntry::EndBlock).
  2251. bool HasReadUnhashedControlBlock = false;
  2252. auto readUnhashedControlBlockOnce = [&]() {
  2253. if (!HasReadUnhashedControlBlock) {
  2254. HasReadUnhashedControlBlock = true;
  2255. if (ASTReadResult Result =
  2256. readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
  2257. return Result;
  2258. }
  2259. return Success;
  2260. };
  2261. // Read all of the records and blocks in the control block.
  2262. RecordData Record;
  2263. unsigned NumInputs = 0;
  2264. unsigned NumUserInputs = 0;
  2265. StringRef BaseDirectoryAsWritten;
  2266. while (true) {
  2267. Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
  2268. if (!MaybeEntry) {
  2269. Error(MaybeEntry.takeError());
  2270. return Failure;
  2271. }
  2272. llvm::BitstreamEntry Entry = MaybeEntry.get();
  2273. switch (Entry.Kind) {
  2274. case llvm::BitstreamEntry::Error:
  2275. Error("malformed block record in AST file");
  2276. return Failure;
  2277. case llvm::BitstreamEntry::EndBlock: {
  2278. // Validate the module before returning. This call catches an AST with
  2279. // no module name and no imports.
  2280. if (ASTReadResult Result = readUnhashedControlBlockOnce())
  2281. return Result;
  2282. // Validate input files.
  2283. const HeaderSearchOptions &HSOpts =
  2284. PP.getHeaderSearchInfo().getHeaderSearchOpts();
  2285. // All user input files reside at the index range [0, NumUserInputs), and
  2286. // system input files reside at [NumUserInputs, NumInputs). For explicitly
  2287. // loaded module files, ignore missing inputs.
  2288. if (!DisableValidation && F.Kind != MK_ExplicitModule &&
  2289. F.Kind != MK_PrebuiltModule) {
  2290. bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
  2291. // If we are reading a module, we will create a verification timestamp,
  2292. // so we verify all input files. Otherwise, verify only user input
  2293. // files.
  2294. unsigned N = NumUserInputs;
  2295. if (ValidateSystemInputs ||
  2296. (HSOpts.ModulesValidateOncePerBuildSession &&
  2297. F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
  2298. F.Kind == MK_ImplicitModule))
  2299. N = NumInputs;
  2300. for (unsigned I = 0; I < N; ++I) {
  2301. InputFile IF = getInputFile(F, I+1, Complain);
  2302. if (!IF.getFile() || IF.isOutOfDate())
  2303. return OutOfDate;
  2304. }
  2305. }
  2306. if (Listener)
  2307. Listener->visitModuleFile(F.FileName, F.Kind);
  2308. if (Listener && Listener->needsInputFileVisitation()) {
  2309. unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
  2310. : NumUserInputs;
  2311. for (unsigned I = 0; I < N; ++I) {
  2312. bool IsSystem = I >= NumUserInputs;
  2313. InputFileInfo FI = readInputFileInfo(F, I+1);
  2314. Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
  2315. F.Kind == MK_ExplicitModule ||
  2316. F.Kind == MK_PrebuiltModule);
  2317. }
  2318. }
  2319. return Result;
  2320. }
  2321. case llvm::BitstreamEntry::SubBlock:
  2322. switch (Entry.ID) {
  2323. case INPUT_FILES_BLOCK_ID:
  2324. F.InputFilesCursor = Stream;
  2325. if (llvm::Error Err = Stream.SkipBlock()) {
  2326. Error(std::move(Err));
  2327. return Failure;
  2328. }
  2329. if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
  2330. Error("malformed block record in AST file");
  2331. return Failure;
  2332. }
  2333. continue;
  2334. case OPTIONS_BLOCK_ID:
  2335. // If we're reading the first module for this group, check its options
  2336. // are compatible with ours. For modules it imports, no further checking
  2337. // is required, because we checked them when we built it.
  2338. if (Listener && !ImportedBy) {
  2339. // Should we allow the configuration of the module file to differ from
  2340. // the configuration of the current translation unit in a compatible
  2341. // way?
  2342. //
  2343. // FIXME: Allow this for files explicitly specified with -include-pch.
  2344. bool AllowCompatibleConfigurationMismatch =
  2345. F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
  2346. Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
  2347. AllowCompatibleConfigurationMismatch,
  2348. *Listener, SuggestedPredefines);
  2349. if (Result == Failure) {
  2350. Error("malformed block record in AST file");
  2351. return Result;
  2352. }
  2353. if (DisableValidation ||
  2354. (AllowConfigurationMismatch && Result == ConfigurationMismatch))
  2355. Result = Success;
  2356. // If we can't load the module, exit early since we likely
  2357. // will rebuild the module anyway. The stream may be in the
  2358. // middle of a block.
  2359. if (Result != Success)
  2360. return Result;
  2361. } else if (llvm::Error Err = Stream.SkipBlock()) {
  2362. Error(std::move(Err));
  2363. return Failure;
  2364. }
  2365. continue;
  2366. default:
  2367. if (llvm::Error Err = Stream.SkipBlock()) {
  2368. Error(std::move(Err));
  2369. return Failure;
  2370. }
  2371. continue;
  2372. }
  2373. case llvm::BitstreamEntry::Record:
  2374. // The interesting case.
  2375. break;
  2376. }
  2377. // Read and process a record.
  2378. Record.clear();
  2379. StringRef Blob;
  2380. Expected<unsigned> MaybeRecordType =
  2381. Stream.readRecord(Entry.ID, Record, &Blob);
  2382. if (!MaybeRecordType) {
  2383. Error(MaybeRecordType.takeError());
  2384. return Failure;
  2385. }
  2386. switch ((ControlRecordTypes)MaybeRecordType.get()) {
  2387. case METADATA: {
  2388. if (Record[0] != VERSION_MAJOR && !DisableValidation) {
  2389. if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
  2390. Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
  2391. : diag::err_pch_version_too_new);
  2392. return VersionMismatch;
  2393. }
  2394. bool hasErrors = Record[7];
  2395. if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
  2396. Diag(diag::err_pch_with_compiler_errors);
  2397. return HadErrors;
  2398. }
  2399. if (hasErrors) {
  2400. Diags.ErrorOccurred = true;
  2401. Diags.UncompilableErrorOccurred = true;
  2402. Diags.UnrecoverableErrorOccurred = true;
  2403. }
  2404. F.RelocatablePCH = Record[4];
  2405. // Relative paths in a relocatable PCH are relative to our sysroot.
  2406. if (F.RelocatablePCH)
  2407. F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
  2408. F.HasTimestamps = Record[5];
  2409. F.PCHHasObjectFile = Record[6];
  2410. const std::string &CurBranch = getClangFullRepositoryVersion();
  2411. StringRef ASTBranch = Blob;
  2412. if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
  2413. if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
  2414. Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
  2415. return VersionMismatch;
  2416. }
  2417. break;
  2418. }
  2419. case IMPORTS: {
  2420. // Validate the AST before processing any imports (otherwise, untangling
  2421. // them can be error-prone and expensive). A module will have a name and
  2422. // will already have been validated, but this catches the PCH case.
  2423. if (ASTReadResult Result = readUnhashedControlBlockOnce())
  2424. return Result;
  2425. // Load each of the imported PCH files.
  2426. unsigned Idx = 0, N = Record.size();
  2427. while (Idx < N) {
  2428. // Read information about the AST file.
  2429. ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
  2430. // The import location will be the local one for now; we will adjust
  2431. // all import locations of module imports after the global source
  2432. // location info are setup, in ReadAST.
  2433. SourceLocation ImportLoc =
  2434. ReadUntranslatedSourceLocation(Record[Idx++]);
  2435. off_t StoredSize = (off_t)Record[Idx++];
  2436. time_t StoredModTime = (time_t)Record[Idx++];
  2437. ASTFileSignature StoredSignature = {
  2438. {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
  2439. (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
  2440. (uint32_t)Record[Idx++]}}};
  2441. std::string ImportedName = ReadString(Record, Idx);
  2442. std::string ImportedFile;
  2443. // For prebuilt and explicit modules first consult the file map for
  2444. // an override. Note that here we don't search prebuilt module
  2445. // directories, only the explicit name to file mappings. Also, we will
  2446. // still verify the size/signature making sure it is essentially the
  2447. // same file but perhaps in a different location.
  2448. if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
  2449. ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
  2450. ImportedName, /*FileMapOnly*/ true);
  2451. if (ImportedFile.empty())
  2452. // Use BaseDirectoryAsWritten to ensure we use the same path in the
  2453. // ModuleCache as when writing.
  2454. ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
  2455. else
  2456. SkipPath(Record, Idx);
  2457. // If our client can't cope with us being out of date, we can't cope with
  2458. // our dependency being missing.
  2459. unsigned Capabilities = ClientLoadCapabilities;
  2460. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  2461. Capabilities &= ~ARR_Missing;
  2462. // Load the AST file.
  2463. auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
  2464. Loaded, StoredSize, StoredModTime,
  2465. StoredSignature, Capabilities);
  2466. // If we diagnosed a problem, produce a backtrace.
  2467. if (isDiagnosedResult(Result, Capabilities))
  2468. Diag(diag::note_module_file_imported_by)
  2469. << F.FileName << !F.ModuleName.empty() << F.ModuleName;
  2470. switch (Result) {
  2471. case Failure: return Failure;
  2472. // If we have to ignore the dependency, we'll have to ignore this too.
  2473. case Missing:
  2474. case OutOfDate: return OutOfDate;
  2475. case VersionMismatch: return VersionMismatch;
  2476. case ConfigurationMismatch: return ConfigurationMismatch;
  2477. case HadErrors: return HadErrors;
  2478. case Success: break;
  2479. }
  2480. }
  2481. break;
  2482. }
  2483. case ORIGINAL_FILE:
  2484. F.OriginalSourceFileID = FileID::get(Record[0]);
  2485. F.ActualOriginalSourceFileName = Blob;
  2486. F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
  2487. ResolveImportedPath(F, F.OriginalSourceFileName);
  2488. break;
  2489. case ORIGINAL_FILE_ID:
  2490. F.OriginalSourceFileID = FileID::get(Record[0]);
  2491. break;
  2492. case ORIGINAL_PCH_DIR:
  2493. F.OriginalDir = Blob;
  2494. break;
  2495. case MODULE_NAME:
  2496. F.ModuleName = Blob;
  2497. Diag(diag::remark_module_import)
  2498. << F.ModuleName << F.FileName << (ImportedBy ? true : false)
  2499. << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
  2500. if (Listener)
  2501. Listener->ReadModuleName(F.ModuleName);
  2502. // Validate the AST as soon as we have a name so we can exit early on
  2503. // failure.
  2504. if (ASTReadResult Result = readUnhashedControlBlockOnce())
  2505. return Result;
  2506. break;
  2507. case MODULE_DIRECTORY: {
  2508. // Save the BaseDirectory as written in the PCM for computing the module
  2509. // filename for the ModuleCache.
  2510. BaseDirectoryAsWritten = Blob;
  2511. assert(!F.ModuleName.empty() &&
  2512. "MODULE_DIRECTORY found before MODULE_NAME");
  2513. // If we've already loaded a module map file covering this module, we may
  2514. // have a better path for it (relative to the current build).
  2515. Module *M = PP.getHeaderSearchInfo().lookupModule(
  2516. F.ModuleName, /*AllowSearch*/ true,
  2517. /*AllowExtraModuleMapSearch*/ true);
  2518. if (M && M->Directory) {
  2519. // If we're implicitly loading a module, the base directory can't
  2520. // change between the build and use.
  2521. // Don't emit module relocation error if we have -fno-validate-pch
  2522. if (!PP.getPreprocessorOpts().DisablePCHValidation &&
  2523. F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
  2524. auto BuildDir = PP.getFileManager().getDirectory(Blob);
  2525. if (!BuildDir || *BuildDir != M->Directory) {
  2526. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  2527. Diag(diag::err_imported_module_relocated)
  2528. << F.ModuleName << Blob << M->Directory->getName();
  2529. return OutOfDate;
  2530. }
  2531. }
  2532. F.BaseDirectory = M->Directory->getName();
  2533. } else {
  2534. F.BaseDirectory = Blob;
  2535. }
  2536. break;
  2537. }
  2538. case MODULE_MAP_FILE:
  2539. if (ASTReadResult Result =
  2540. ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
  2541. return Result;
  2542. break;
  2543. case INPUT_FILE_OFFSETS:
  2544. NumInputs = Record[0];
  2545. NumUserInputs = Record[1];
  2546. F.InputFileOffsets =
  2547. (const llvm::support::unaligned_uint64_t *)Blob.data();
  2548. F.InputFilesLoaded.resize(NumInputs);
  2549. F.NumUserInputFiles = NumUserInputs;
  2550. break;
  2551. }
  2552. }
  2553. }
  2554. ASTReader::ASTReadResult
  2555. ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
  2556. BitstreamCursor &Stream = F.Stream;
  2557. if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
  2558. Error(std::move(Err));
  2559. return Failure;
  2560. }
  2561. // Read all of the records and blocks for the AST file.
  2562. RecordData Record;
  2563. while (true) {
  2564. Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
  2565. if (!MaybeEntry) {
  2566. Error(MaybeEntry.takeError());
  2567. return Failure;
  2568. }
  2569. llvm::BitstreamEntry Entry = MaybeEntry.get();
  2570. switch (Entry.Kind) {
  2571. case llvm::BitstreamEntry::Error:
  2572. Error("error at end of module block in AST file");
  2573. return Failure;
  2574. case llvm::BitstreamEntry::EndBlock:
  2575. // Outside of C++, we do not store a lookup map for the translation unit.
  2576. // Instead, mark it as needing a lookup map to be built if this module
  2577. // contains any declarations lexically within it (which it always does!).
  2578. // This usually has no cost, since we very rarely need the lookup map for
  2579. // the translation unit outside C++.
  2580. if (ASTContext *Ctx = ContextObj) {
  2581. DeclContext *DC = Ctx->getTranslationUnitDecl();
  2582. if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
  2583. DC->setMustBuildLookupTable();
  2584. }
  2585. return Success;
  2586. case llvm::BitstreamEntry::SubBlock:
  2587. switch (Entry.ID) {
  2588. case DECLTYPES_BLOCK_ID:
  2589. // We lazily load the decls block, but we want to set up the
  2590. // DeclsCursor cursor to point into it. Clone our current bitcode
  2591. // cursor to it, enter the block and read the abbrevs in that block.
  2592. // With the main cursor, we just skip over it.
  2593. F.DeclsCursor = Stream;
  2594. if (llvm::Error Err = Stream.SkipBlock()) {
  2595. Error(std::move(Err));
  2596. return Failure;
  2597. }
  2598. if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
  2599. Error("malformed block record in AST file");
  2600. return Failure;
  2601. }
  2602. break;
  2603. case PREPROCESSOR_BLOCK_ID:
  2604. F.MacroCursor = Stream;
  2605. if (!PP.getExternalSource())
  2606. PP.setExternalSource(this);
  2607. if (llvm::Error Err = Stream.SkipBlock()) {
  2608. Error(std::move(Err));
  2609. return Failure;
  2610. }
  2611. if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
  2612. Error("malformed block record in AST file");
  2613. return Failure;
  2614. }
  2615. F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
  2616. break;
  2617. case PREPROCESSOR_DETAIL_BLOCK_ID:
  2618. F.PreprocessorDetailCursor = Stream;
  2619. if (llvm::Error Err = Stream.SkipBlock()) {
  2620. Error(std::move(Err));
  2621. return Failure;
  2622. }
  2623. if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
  2624. PREPROCESSOR_DETAIL_BLOCK_ID)) {
  2625. Error("malformed preprocessor detail record in AST file");
  2626. return Failure;
  2627. }
  2628. F.PreprocessorDetailStartOffset
  2629. = F.PreprocessorDetailCursor.GetCurrentBitNo();
  2630. if (!PP.getPreprocessingRecord())
  2631. PP.createPreprocessingRecord();
  2632. if (!PP.getPreprocessingRecord()->getExternalSource())
  2633. PP.getPreprocessingRecord()->SetExternalSource(*this);
  2634. break;
  2635. case SOURCE_MANAGER_BLOCK_ID:
  2636. if (ReadSourceManagerBlock(F))
  2637. return Failure;
  2638. break;
  2639. case SUBMODULE_BLOCK_ID:
  2640. if (ASTReadResult Result =
  2641. ReadSubmoduleBlock(F, ClientLoadCapabilities))
  2642. return Result;
  2643. break;
  2644. case COMMENTS_BLOCK_ID: {
  2645. BitstreamCursor C = Stream;
  2646. if (llvm::Error Err = Stream.SkipBlock()) {
  2647. Error(std::move(Err));
  2648. return Failure;
  2649. }
  2650. if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
  2651. Error("malformed comments block in AST file");
  2652. return Failure;
  2653. }
  2654. CommentsCursors.push_back(std::make_pair(C, &F));
  2655. break;
  2656. }
  2657. default:
  2658. if (llvm::Error Err = Stream.SkipBlock()) {
  2659. Error(std::move(Err));
  2660. return Failure;
  2661. }
  2662. break;
  2663. }
  2664. continue;
  2665. case llvm::BitstreamEntry::Record:
  2666. // The interesting case.
  2667. break;
  2668. }
  2669. // Read and process a record.
  2670. Record.clear();
  2671. StringRef Blob;
  2672. Expected<unsigned> MaybeRecordType =
  2673. Stream.readRecord(Entry.ID, Record, &Blob);
  2674. if (!MaybeRecordType) {
  2675. Error(MaybeRecordType.takeError());
  2676. return Failure;
  2677. }
  2678. ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
  2679. // If we're not loading an AST context, we don't care about most records.
  2680. if (!ContextObj) {
  2681. switch (RecordType) {
  2682. case IDENTIFIER_TABLE:
  2683. case IDENTIFIER_OFFSET:
  2684. case INTERESTING_IDENTIFIERS:
  2685. case STATISTICS:
  2686. case PP_CONDITIONAL_STACK:
  2687. case PP_COUNTER_VALUE:
  2688. case SOURCE_LOCATION_OFFSETS:
  2689. case MODULE_OFFSET_MAP:
  2690. case SOURCE_MANAGER_LINE_TABLE:
  2691. case SOURCE_LOCATION_PRELOADS:
  2692. case PPD_ENTITIES_OFFSETS:
  2693. case HEADER_SEARCH_TABLE:
  2694. case IMPORTED_MODULES:
  2695. case MACRO_OFFSET:
  2696. break;
  2697. default:
  2698. continue;
  2699. }
  2700. }
  2701. switch (RecordType) {
  2702. default: // Default behavior: ignore.
  2703. break;
  2704. case TYPE_OFFSET: {
  2705. if (F.LocalNumTypes != 0) {
  2706. Error("duplicate TYPE_OFFSET record in AST file");
  2707. return Failure;
  2708. }
  2709. F.TypeOffsets = (const uint32_t *)Blob.data();
  2710. F.LocalNumTypes = Record[0];
  2711. unsigned LocalBaseTypeIndex = Record[1];
  2712. F.BaseTypeIndex = getTotalNumTypes();
  2713. if (F.LocalNumTypes > 0) {
  2714. // Introduce the global -> local mapping for types within this module.
  2715. GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
  2716. // Introduce the local -> global mapping for types within this module.
  2717. F.TypeRemap.insertOrReplace(
  2718. std::make_pair(LocalBaseTypeIndex,
  2719. F.BaseTypeIndex - LocalBaseTypeIndex));
  2720. TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
  2721. }
  2722. break;
  2723. }
  2724. case DECL_OFFSET: {
  2725. if (F.LocalNumDecls != 0) {
  2726. Error("duplicate DECL_OFFSET record in AST file");
  2727. return Failure;
  2728. }
  2729. F.DeclOffsets = (const DeclOffset *)Blob.data();
  2730. F.LocalNumDecls = Record[0];
  2731. unsigned LocalBaseDeclID = Record[1];
  2732. F.BaseDeclID = getTotalNumDecls();
  2733. if (F.LocalNumDecls > 0) {
  2734. // Introduce the global -> local mapping for declarations within this
  2735. // module.
  2736. GlobalDeclMap.insert(
  2737. std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
  2738. // Introduce the local -> global mapping for declarations within this
  2739. // module.
  2740. F.DeclRemap.insertOrReplace(
  2741. std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
  2742. // Introduce the global -> local mapping for declarations within this
  2743. // module.
  2744. F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
  2745. DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
  2746. }
  2747. break;
  2748. }
  2749. case TU_UPDATE_LEXICAL: {
  2750. DeclContext *TU = ContextObj->getTranslationUnitDecl();
  2751. LexicalContents Contents(
  2752. reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
  2753. Blob.data()),
  2754. static_cast<unsigned int>(Blob.size() / 4));
  2755. TULexicalDecls.push_back(std::make_pair(&F, Contents));
  2756. TU->setHasExternalLexicalStorage(true);
  2757. break;
  2758. }
  2759. case UPDATE_VISIBLE: {
  2760. unsigned Idx = 0;
  2761. serialization::DeclID ID = ReadDeclID(F, Record, Idx);
  2762. auto *Data = (const unsigned char*)Blob.data();
  2763. PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
  2764. // If we've already loaded the decl, perform the updates when we finish
  2765. // loading this block.
  2766. if (Decl *D = GetExistingDecl(ID))
  2767. PendingUpdateRecords.push_back(
  2768. PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
  2769. break;
  2770. }
  2771. case IDENTIFIER_TABLE:
  2772. F.IdentifierTableData = Blob.data();
  2773. if (Record[0]) {
  2774. F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
  2775. (const unsigned char *)F.IdentifierTableData + Record[0],
  2776. (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
  2777. (const unsigned char *)F.IdentifierTableData,
  2778. ASTIdentifierLookupTrait(*this, F));
  2779. PP.getIdentifierTable().setExternalIdentifierLookup(this);
  2780. }
  2781. break;
  2782. case IDENTIFIER_OFFSET: {
  2783. if (F.LocalNumIdentifiers != 0) {
  2784. Error("duplicate IDENTIFIER_OFFSET record in AST file");
  2785. return Failure;
  2786. }
  2787. F.IdentifierOffsets = (const uint32_t *)Blob.data();
  2788. F.LocalNumIdentifiers = Record[0];
  2789. unsigned LocalBaseIdentifierID = Record[1];
  2790. F.BaseIdentifierID = getTotalNumIdentifiers();
  2791. if (F.LocalNumIdentifiers > 0) {
  2792. // Introduce the global -> local mapping for identifiers within this
  2793. // module.
  2794. GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
  2795. &F));
  2796. // Introduce the local -> global mapping for identifiers within this
  2797. // module.
  2798. F.IdentifierRemap.insertOrReplace(
  2799. std::make_pair(LocalBaseIdentifierID,
  2800. F.BaseIdentifierID - LocalBaseIdentifierID));
  2801. IdentifiersLoaded.resize(IdentifiersLoaded.size()
  2802. + F.LocalNumIdentifiers);
  2803. }
  2804. break;
  2805. }
  2806. case INTERESTING_IDENTIFIERS:
  2807. F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
  2808. break;
  2809. case EAGERLY_DESERIALIZED_DECLS:
  2810. // FIXME: Skip reading this record if our ASTConsumer doesn't care
  2811. // about "interesting" decls (for instance, if we're building a module).
  2812. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2813. EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
  2814. break;
  2815. case MODULAR_CODEGEN_DECLS:
  2816. // FIXME: Skip reading this record if our ASTConsumer doesn't care about
  2817. // them (ie: if we're not codegenerating this module).
  2818. if (F.Kind == MK_MainFile)
  2819. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2820. EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
  2821. break;
  2822. case SPECIAL_TYPES:
  2823. if (SpecialTypes.empty()) {
  2824. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2825. SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
  2826. break;
  2827. }
  2828. if (SpecialTypes.size() != Record.size()) {
  2829. Error("invalid special-types record");
  2830. return Failure;
  2831. }
  2832. for (unsigned I = 0, N = Record.size(); I != N; ++I) {
  2833. serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
  2834. if (!SpecialTypes[I])
  2835. SpecialTypes[I] = ID;
  2836. // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
  2837. // merge step?
  2838. }
  2839. break;
  2840. case STATISTICS:
  2841. TotalNumStatements += Record[0];
  2842. TotalNumMacros += Record[1];
  2843. TotalLexicalDeclContexts += Record[2];
  2844. TotalVisibleDeclContexts += Record[3];
  2845. break;
  2846. case UNUSED_FILESCOPED_DECLS:
  2847. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2848. UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
  2849. break;
  2850. case DELEGATING_CTORS:
  2851. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2852. DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
  2853. break;
  2854. case WEAK_UNDECLARED_IDENTIFIERS:
  2855. if (Record.size() % 4 != 0) {
  2856. Error("invalid weak identifiers record");
  2857. return Failure;
  2858. }
  2859. // FIXME: Ignore weak undeclared identifiers from non-original PCH
  2860. // files. This isn't the way to do it :)
  2861. WeakUndeclaredIdentifiers.clear();
  2862. // Translate the weak, undeclared identifiers into global IDs.
  2863. for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
  2864. WeakUndeclaredIdentifiers.push_back(
  2865. getGlobalIdentifierID(F, Record[I++]));
  2866. WeakUndeclaredIdentifiers.push_back(
  2867. getGlobalIdentifierID(F, Record[I++]));
  2868. WeakUndeclaredIdentifiers.push_back(
  2869. ReadSourceLocation(F, Record, I).getRawEncoding());
  2870. WeakUndeclaredIdentifiers.push_back(Record[I++]);
  2871. }
  2872. break;
  2873. case SELECTOR_OFFSETS: {
  2874. F.SelectorOffsets = (const uint32_t *)Blob.data();
  2875. F.LocalNumSelectors = Record[0];
  2876. unsigned LocalBaseSelectorID = Record[1];
  2877. F.BaseSelectorID = getTotalNumSelectors();
  2878. if (F.LocalNumSelectors > 0) {
  2879. // Introduce the global -> local mapping for selectors within this
  2880. // module.
  2881. GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
  2882. // Introduce the local -> global mapping for selectors within this
  2883. // module.
  2884. F.SelectorRemap.insertOrReplace(
  2885. std::make_pair(LocalBaseSelectorID,
  2886. F.BaseSelectorID - LocalBaseSelectorID));
  2887. SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
  2888. }
  2889. break;
  2890. }
  2891. case METHOD_POOL:
  2892. F.SelectorLookupTableData = (const unsigned char *)Blob.data();
  2893. if (Record[0])
  2894. F.SelectorLookupTable
  2895. = ASTSelectorLookupTable::Create(
  2896. F.SelectorLookupTableData + Record[0],
  2897. F.SelectorLookupTableData,
  2898. ASTSelectorLookupTrait(*this, F));
  2899. TotalNumMethodPoolEntries += Record[1];
  2900. break;
  2901. case REFERENCED_SELECTOR_POOL:
  2902. if (!Record.empty()) {
  2903. for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
  2904. ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
  2905. Record[Idx++]));
  2906. ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
  2907. getRawEncoding());
  2908. }
  2909. }
  2910. break;
  2911. case PP_CONDITIONAL_STACK:
  2912. if (!Record.empty()) {
  2913. unsigned Idx = 0, End = Record.size() - 1;
  2914. bool ReachedEOFWhileSkipping = Record[Idx++];
  2915. llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
  2916. if (ReachedEOFWhileSkipping) {
  2917. SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
  2918. SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
  2919. bool FoundNonSkipPortion = Record[Idx++];
  2920. bool FoundElse = Record[Idx++];
  2921. SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
  2922. SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
  2923. FoundElse, ElseLoc);
  2924. }
  2925. SmallVector<PPConditionalInfo, 4> ConditionalStack;
  2926. while (Idx < End) {
  2927. auto Loc = ReadSourceLocation(F, Record, Idx);
  2928. bool WasSkipping = Record[Idx++];
  2929. bool FoundNonSkip = Record[Idx++];
  2930. bool FoundElse = Record[Idx++];
  2931. ConditionalStack.push_back(
  2932. {Loc, WasSkipping, FoundNonSkip, FoundElse});
  2933. }
  2934. PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
  2935. }
  2936. break;
  2937. case PP_COUNTER_VALUE:
  2938. if (!Record.empty() && Listener)
  2939. Listener->ReadCounter(F, Record[0]);
  2940. break;
  2941. case FILE_SORTED_DECLS:
  2942. F.FileSortedDecls = (const DeclID *)Blob.data();
  2943. F.NumFileSortedDecls = Record[0];
  2944. break;
  2945. case SOURCE_LOCATION_OFFSETS: {
  2946. F.SLocEntryOffsets = (const uint32_t *)Blob.data();
  2947. F.LocalNumSLocEntries = Record[0];
  2948. unsigned SLocSpaceSize = Record[1];
  2949. std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
  2950. SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
  2951. SLocSpaceSize);
  2952. if (!F.SLocEntryBaseID) {
  2953. Error("ran out of source locations");
  2954. break;
  2955. }
  2956. // Make our entry in the range map. BaseID is negative and growing, so
  2957. // we invert it. Because we invert it, though, we need the other end of
  2958. // the range.
  2959. unsigned RangeStart =
  2960. unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
  2961. GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
  2962. F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
  2963. // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
  2964. assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
  2965. GlobalSLocOffsetMap.insert(
  2966. std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
  2967. - SLocSpaceSize,&F));
  2968. // Initialize the remapping table.
  2969. // Invalid stays invalid.
  2970. F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
  2971. // This module. Base was 2 when being compiled.
  2972. F.SLocRemap.insertOrReplace(std::make_pair(2U,
  2973. static_cast<int>(F.SLocEntryBaseOffset - 2)));
  2974. TotalNumSLocEntries += F.LocalNumSLocEntries;
  2975. break;
  2976. }
  2977. case MODULE_OFFSET_MAP:
  2978. F.ModuleOffsetMap = Blob;
  2979. break;
  2980. case SOURCE_MANAGER_LINE_TABLE:
  2981. if (ParseLineTable(F, Record))
  2982. return Failure;
  2983. break;
  2984. case SOURCE_LOCATION_PRELOADS: {
  2985. // Need to transform from the local view (1-based IDs) to the global view,
  2986. // which is based off F.SLocEntryBaseID.
  2987. if (!F.PreloadSLocEntries.empty()) {
  2988. Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
  2989. return Failure;
  2990. }
  2991. F.PreloadSLocEntries.swap(Record);
  2992. break;
  2993. }
  2994. case EXT_VECTOR_DECLS:
  2995. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2996. ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
  2997. break;
  2998. case VTABLE_USES:
  2999. if (Record.size() % 3 != 0) {
  3000. Error("Invalid VTABLE_USES record");
  3001. return Failure;
  3002. }
  3003. // Later tables overwrite earlier ones.
  3004. // FIXME: Modules will have some trouble with this. This is clearly not
  3005. // the right way to do this.
  3006. VTableUses.clear();
  3007. for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
  3008. VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
  3009. VTableUses.push_back(
  3010. ReadSourceLocation(F, Record, Idx).getRawEncoding());
  3011. VTableUses.push_back(Record[Idx++]);
  3012. }
  3013. break;
  3014. case PENDING_IMPLICIT_INSTANTIATIONS:
  3015. if (PendingInstantiations.size() % 2 != 0) {
  3016. Error("Invalid existing PendingInstantiations");
  3017. return Failure;
  3018. }
  3019. if (Record.size() % 2 != 0) {
  3020. Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
  3021. return Failure;
  3022. }
  3023. for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
  3024. PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
  3025. PendingInstantiations.push_back(
  3026. ReadSourceLocation(F, Record, I).getRawEncoding());
  3027. }
  3028. break;
  3029. case SEMA_DECL_REFS:
  3030. if (Record.size() != 3) {
  3031. Error("Invalid SEMA_DECL_REFS block");
  3032. return Failure;
  3033. }
  3034. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  3035. SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
  3036. break;
  3037. case PPD_ENTITIES_OFFSETS: {
  3038. F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
  3039. assert(Blob.size() % sizeof(PPEntityOffset) == 0);
  3040. F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
  3041. unsigned LocalBasePreprocessedEntityID = Record[0];
  3042. unsigned StartingID;
  3043. if (!PP.getPreprocessingRecord())
  3044. PP.createPreprocessingRecord();
  3045. if (!PP.getPreprocessingRecord()->getExternalSource())
  3046. PP.getPreprocessingRecord()->SetExternalSource(*this);
  3047. StartingID
  3048. = PP.getPreprocessingRecord()
  3049. ->allocateLoadedEntities(F.NumPreprocessedEntities);
  3050. F.BasePreprocessedEntityID = StartingID;
  3051. if (F.NumPreprocessedEntities > 0) {
  3052. // Introduce the global -> local mapping for preprocessed entities in
  3053. // this module.
  3054. GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
  3055. // Introduce the local -> global mapping for preprocessed entities in
  3056. // this module.
  3057. F.PreprocessedEntityRemap.insertOrReplace(
  3058. std::make_pair(LocalBasePreprocessedEntityID,
  3059. F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
  3060. }
  3061. break;
  3062. }
  3063. case PPD_SKIPPED_RANGES: {
  3064. F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
  3065. assert(Blob.size() % sizeof(PPSkippedRange) == 0);
  3066. F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
  3067. if (!PP.getPreprocessingRecord())
  3068. PP.createPreprocessingRecord();
  3069. if (!PP.getPreprocessingRecord()->getExternalSource())
  3070. PP.getPreprocessingRecord()->SetExternalSource(*this);
  3071. F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
  3072. ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
  3073. if (F.NumPreprocessedSkippedRanges > 0)
  3074. GlobalSkippedRangeMap.insert(
  3075. std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
  3076. break;
  3077. }
  3078. case DECL_UPDATE_OFFSETS:
  3079. if (Record.size() % 2 != 0) {
  3080. Error("invalid DECL_UPDATE_OFFSETS block in AST file");
  3081. return Failure;
  3082. }
  3083. for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
  3084. GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
  3085. DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
  3086. // If we've already loaded the decl, perform the updates when we finish
  3087. // loading this block.
  3088. if (Decl *D = GetExistingDecl(ID))
  3089. PendingUpdateRecords.push_back(
  3090. PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
  3091. }
  3092. break;
  3093. case OBJC_CATEGORIES_MAP:
  3094. if (F.LocalNumObjCCategoriesInMap != 0) {
  3095. Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
  3096. return Failure;
  3097. }
  3098. F.LocalNumObjCCategoriesInMap = Record[0];
  3099. F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
  3100. break;
  3101. case OBJC_CATEGORIES:
  3102. F.ObjCCategories.swap(Record);
  3103. break;
  3104. case CUDA_SPECIAL_DECL_REFS:
  3105. // Later tables overwrite earlier ones.
  3106. // FIXME: Modules will have trouble with this.
  3107. CUDASpecialDeclRefs.clear();
  3108. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  3109. CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
  3110. break;
  3111. case HEADER_SEARCH_TABLE:
  3112. F.HeaderFileInfoTableData = Blob.data();
  3113. F.LocalNumHeaderFileInfos = Record[1];
  3114. if (Record[0]) {
  3115. F.HeaderFileInfoTable
  3116. = HeaderFileInfoLookupTable::Create(
  3117. (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
  3118. (const unsigned char *)F.HeaderFileInfoTableData,
  3119. HeaderFileInfoTrait(*this, F,
  3120. &PP.getHeaderSearchInfo(),
  3121. Blob.data() + Record[2]));
  3122. PP.getHeaderSearchInfo().SetExternalSource(this);
  3123. if (!PP.getHeaderSearchInfo().getExternalLookup())
  3124. PP.getHeaderSearchInfo().SetExternalLookup(this);
  3125. }
  3126. break;
  3127. case FP_PRAGMA_OPTIONS:
  3128. // Later tables overwrite earlier ones.
  3129. FPPragmaOptions.swap(Record);
  3130. break;
  3131. case OPENCL_EXTENSIONS:
  3132. for (unsigned I = 0, E = Record.size(); I != E; ) {
  3133. auto Name = ReadString(Record, I);
  3134. auto &Opt = OpenCLExtensions.OptMap[Name];
  3135. Opt.Supported = Record[I++] != 0;
  3136. Opt.Enabled = Record[I++] != 0;
  3137. Opt.Avail = Record[I++];
  3138. Opt.Core = Record[I++];
  3139. }
  3140. break;
  3141. case OPENCL_EXTENSION_TYPES:
  3142. for (unsigned I = 0, E = Record.size(); I != E;) {
  3143. auto TypeID = static_cast<::TypeID>(Record[I++]);
  3144. auto *Type = GetType(TypeID).getTypePtr();
  3145. auto NumExt = static_cast<unsigned>(Record[I++]);
  3146. for (unsigned II = 0; II != NumExt; ++II) {
  3147. auto Ext = ReadString(Record, I);
  3148. OpenCLTypeExtMap[Type].insert(Ext);
  3149. }
  3150. }
  3151. break;
  3152. case OPENCL_EXTENSION_DECLS:
  3153. for (unsigned I = 0, E = Record.size(); I != E;) {
  3154. auto DeclID = static_cast<::DeclID>(Record[I++]);
  3155. auto *Decl = GetDecl(DeclID);
  3156. auto NumExt = static_cast<unsigned>(Record[I++]);
  3157. for (unsigned II = 0; II != NumExt; ++II) {
  3158. auto Ext = ReadString(Record, I);
  3159. OpenCLDeclExtMap[Decl].insert(Ext);
  3160. }
  3161. }
  3162. break;
  3163. case TENTATIVE_DEFINITIONS:
  3164. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  3165. TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
  3166. break;
  3167. case KNOWN_NAMESPACES:
  3168. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  3169. KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
  3170. break;
  3171. case UNDEFINED_BUT_USED:
  3172. if (UndefinedButUsed.size() % 2 != 0) {
  3173. Error("Invalid existing UndefinedButUsed");
  3174. return Failure;
  3175. }
  3176. if (Record.size() % 2 != 0) {
  3177. Error("invalid undefined-but-used record");
  3178. return Failure;
  3179. }
  3180. for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
  3181. UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
  3182. UndefinedButUsed.push_back(
  3183. ReadSourceLocation(F, Record, I).getRawEncoding());
  3184. }
  3185. break;
  3186. case DELETE_EXPRS_TO_ANALYZE:
  3187. for (unsigned I = 0, N = Record.size(); I != N;) {
  3188. DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
  3189. const uint64_t Count = Record[I++];
  3190. DelayedDeleteExprs.push_back(Count);
  3191. for (uint64_t C = 0; C < Count; ++C) {
  3192. DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
  3193. bool IsArrayForm = Record[I++] == 1;
  3194. DelayedDeleteExprs.push_back(IsArrayForm);
  3195. }
  3196. }
  3197. break;
  3198. case IMPORTED_MODULES:
  3199. if (!F.isModule()) {
  3200. // If we aren't loading a module (which has its own exports), make
  3201. // all of the imported modules visible.
  3202. // FIXME: Deal with macros-only imports.
  3203. for (unsigned I = 0, N = Record.size(); I != N; /**/) {
  3204. unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
  3205. SourceLocation Loc = ReadSourceLocation(F, Record, I);
  3206. if (GlobalID) {
  3207. ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
  3208. if (DeserializationListener)
  3209. DeserializationListener->ModuleImportRead(GlobalID, Loc);
  3210. }
  3211. }
  3212. }
  3213. break;
  3214. case MACRO_OFFSET: {
  3215. if (F.LocalNumMacros != 0) {
  3216. Error("duplicate MACRO_OFFSET record in AST file");
  3217. return Failure;
  3218. }
  3219. F.MacroOffsets = (const uint32_t *)Blob.data();
  3220. F.LocalNumMacros = Record[0];
  3221. unsigned LocalBaseMacroID = Record[1];
  3222. F.BaseMacroID = getTotalNumMacros();
  3223. if (F.LocalNumMacros > 0) {
  3224. // Introduce the global -> local mapping for macros within this module.
  3225. GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
  3226. // Introduce the local -> global mapping for macros within this module.
  3227. F.MacroRemap.insertOrReplace(
  3228. std::make_pair(LocalBaseMacroID,
  3229. F.BaseMacroID - LocalBaseMacroID));
  3230. MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
  3231. }
  3232. break;
  3233. }
  3234. case LATE_PARSED_TEMPLATE:
  3235. LateParsedTemplates.append(Record.begin(), Record.end());
  3236. break;
  3237. case OPTIMIZE_PRAGMA_OPTIONS:
  3238. if (Record.size() != 1) {
  3239. Error("invalid pragma optimize record");
  3240. return Failure;
  3241. }
  3242. OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
  3243. break;
  3244. case MSSTRUCT_PRAGMA_OPTIONS:
  3245. if (Record.size() != 1) {
  3246. Error("invalid pragma ms_struct record");
  3247. return Failure;
  3248. }
  3249. PragmaMSStructState = Record[0];
  3250. break;
  3251. case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
  3252. if (Record.size() != 2) {
  3253. Error("invalid pragma ms_struct record");
  3254. return Failure;
  3255. }
  3256. PragmaMSPointersToMembersState = Record[0];
  3257. PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
  3258. break;
  3259. case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
  3260. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  3261. UnusedLocalTypedefNameCandidates.push_back(
  3262. getGlobalDeclID(F, Record[I]));
  3263. break;
  3264. case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
  3265. if (Record.size() != 1) {
  3266. Error("invalid cuda pragma options record");
  3267. return Failure;
  3268. }
  3269. ForceCUDAHostDeviceDepth = Record[0];
  3270. break;
  3271. case PACK_PRAGMA_OPTIONS: {
  3272. if (Record.size() < 3) {
  3273. Error("invalid pragma pack record");
  3274. return Failure;
  3275. }
  3276. PragmaPackCurrentValue = Record[0];
  3277. PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
  3278. unsigned NumStackEntries = Record[2];
  3279. unsigned Idx = 3;
  3280. // Reset the stack when importing a new module.
  3281. PragmaPackStack.clear();
  3282. for (unsigned I = 0; I < NumStackEntries; ++I) {
  3283. PragmaPackStackEntry Entry;
  3284. Entry.Value = Record[Idx++];
  3285. Entry.Location = ReadSourceLocation(F, Record[Idx++]);
  3286. Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
  3287. PragmaPackStrings.push_back(ReadString(Record, Idx));
  3288. Entry.SlotLabel = PragmaPackStrings.back();
  3289. PragmaPackStack.push_back(Entry);
  3290. }
  3291. break;
  3292. }
  3293. }
  3294. }
  3295. }
  3296. void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
  3297. assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
  3298. // Additional remapping information.
  3299. const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
  3300. const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
  3301. F.ModuleOffsetMap = StringRef();
  3302. // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
  3303. if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
  3304. F.SLocRemap.insert(std::make_pair(0U, 0));
  3305. F.SLocRemap.insert(std::make_pair(2U, 1));
  3306. }
  3307. // Continuous range maps we may be updating in our module.
  3308. using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
  3309. RemapBuilder SLocRemap(F.SLocRemap);
  3310. RemapBuilder IdentifierRemap(F.IdentifierRemap);
  3311. RemapBuilder MacroRemap(F.MacroRemap);
  3312. RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
  3313. RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
  3314. RemapBuilder SelectorRemap(F.SelectorRemap);
  3315. RemapBuilder DeclRemap(F.DeclRemap);
  3316. RemapBuilder TypeRemap(F.TypeRemap);
  3317. while (Data < DataEnd) {
  3318. // FIXME: Looking up dependency modules by filename is horrible. Let's
  3319. // start fixing this with prebuilt and explicit modules and see how it
  3320. // goes...
  3321. using namespace llvm::support;
  3322. ModuleKind Kind = static_cast<ModuleKind>(
  3323. endian::readNext<uint8_t, little, unaligned>(Data));
  3324. uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
  3325. StringRef Name = StringRef((const char*)Data, Len);
  3326. Data += Len;
  3327. ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
  3328. ? ModuleMgr.lookupByModuleName(Name)
  3329. : ModuleMgr.lookupByFileName(Name));
  3330. if (!OM) {
  3331. std::string Msg =
  3332. "SourceLocation remap refers to unknown module, cannot find ";
  3333. Msg.append(Name);
  3334. Error(Msg);
  3335. return;
  3336. }
  3337. uint32_t SLocOffset =
  3338. endian::readNext<uint32_t, little, unaligned>(Data);
  3339. uint32_t IdentifierIDOffset =
  3340. endian::readNext<uint32_t, little, unaligned>(Data);
  3341. uint32_t MacroIDOffset =
  3342. endian::readNext<uint32_t, little, unaligned>(Data);
  3343. uint32_t PreprocessedEntityIDOffset =
  3344. endian::readNext<uint32_t, little, unaligned>(Data);
  3345. uint32_t SubmoduleIDOffset =
  3346. endian::readNext<uint32_t, little, unaligned>(Data);
  3347. uint32_t SelectorIDOffset =
  3348. endian::readNext<uint32_t, little, unaligned>(Data);
  3349. uint32_t DeclIDOffset =
  3350. endian::readNext<uint32_t, little, unaligned>(Data);
  3351. uint32_t TypeIndexOffset =
  3352. endian::readNext<uint32_t, little, unaligned>(Data);
  3353. uint32_t None = std::numeric_limits<uint32_t>::max();
  3354. auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
  3355. RemapBuilder &Remap) {
  3356. if (Offset != None)
  3357. Remap.insert(std::make_pair(Offset,
  3358. static_cast<int>(BaseOffset - Offset)));
  3359. };
  3360. mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
  3361. mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
  3362. mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
  3363. mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
  3364. PreprocessedEntityRemap);
  3365. mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
  3366. mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
  3367. mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
  3368. mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
  3369. // Global -> local mappings.
  3370. F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
  3371. }
  3372. }
  3373. ASTReader::ASTReadResult
  3374. ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
  3375. const ModuleFile *ImportedBy,
  3376. unsigned ClientLoadCapabilities) {
  3377. unsigned Idx = 0;
  3378. F.ModuleMapPath = ReadPath(F, Record, Idx);
  3379. // Try to resolve ModuleName in the current header search context and
  3380. // verify that it is found in the same module map file as we saved. If the
  3381. // top-level AST file is a main file, skip this check because there is no
  3382. // usable header search context.
  3383. assert(!F.ModuleName.empty() &&
  3384. "MODULE_NAME should come before MODULE_MAP_FILE");
  3385. if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
  3386. // An implicitly-loaded module file should have its module listed in some
  3387. // module map file that we've already loaded.
  3388. Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
  3389. auto &Map = PP.getHeaderSearchInfo().getModuleMap();
  3390. const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
  3391. // Don't emit module relocation error if we have -fno-validate-pch
  3392. if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
  3393. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
  3394. if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
  3395. // This module was defined by an imported (explicit) module.
  3396. Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
  3397. << ASTFE->getName();
  3398. } else {
  3399. // This module was built with a different module map.
  3400. Diag(diag::err_imported_module_not_found)
  3401. << F.ModuleName << F.FileName
  3402. << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
  3403. << !ImportedBy;
  3404. // In case it was imported by a PCH, there's a chance the user is
  3405. // just missing to include the search path to the directory containing
  3406. // the modulemap.
  3407. if (ImportedBy && ImportedBy->Kind == MK_PCH)
  3408. Diag(diag::note_imported_by_pch_module_not_found)
  3409. << llvm::sys::path::parent_path(F.ModuleMapPath);
  3410. }
  3411. }
  3412. return OutOfDate;
  3413. }
  3414. assert(M->Name == F.ModuleName && "found module with different name");
  3415. // Check the primary module map file.
  3416. auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
  3417. if (!StoredModMap || *StoredModMap != ModMap) {
  3418. assert(ModMap && "found module is missing module map file");
  3419. assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
  3420. "top-level import should be verified");
  3421. bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
  3422. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  3423. Diag(diag::err_imported_module_modmap_changed)
  3424. << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
  3425. << ModMap->getName() << F.ModuleMapPath << NotImported;
  3426. return OutOfDate;
  3427. }
  3428. llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
  3429. for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
  3430. // FIXME: we should use input files rather than storing names.
  3431. std::string Filename = ReadPath(F, Record, Idx);
  3432. auto F = FileMgr.getFile(Filename, false, false);
  3433. if (!F) {
  3434. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  3435. Error("could not find file '" + Filename +"' referenced by AST file");
  3436. return OutOfDate;
  3437. }
  3438. AdditionalStoredMaps.insert(*F);
  3439. }
  3440. // Check any additional module map files (e.g. module.private.modulemap)
  3441. // that are not in the pcm.
  3442. if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
  3443. for (const FileEntry *ModMap : *AdditionalModuleMaps) {
  3444. // Remove files that match
  3445. // Note: SmallPtrSet::erase is really remove
  3446. if (!AdditionalStoredMaps.erase(ModMap)) {
  3447. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  3448. Diag(diag::err_module_different_modmap)
  3449. << F.ModuleName << /*new*/0 << ModMap->getName();
  3450. return OutOfDate;
  3451. }
  3452. }
  3453. }
  3454. // Check any additional module map files that are in the pcm, but not
  3455. // found in header search. Cases that match are already removed.
  3456. for (const FileEntry *ModMap : AdditionalStoredMaps) {
  3457. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  3458. Diag(diag::err_module_different_modmap)
  3459. << F.ModuleName << /*not new*/1 << ModMap->getName();
  3460. return OutOfDate;
  3461. }
  3462. }
  3463. if (Listener)
  3464. Listener->ReadModuleMapFile(F.ModuleMapPath);
  3465. return Success;
  3466. }
  3467. /// Move the given method to the back of the global list of methods.
  3468. static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
  3469. // Find the entry for this selector in the method pool.
  3470. Sema::GlobalMethodPool::iterator Known
  3471. = S.MethodPool.find(Method->getSelector());
  3472. if (Known == S.MethodPool.end())
  3473. return;
  3474. // Retrieve the appropriate method list.
  3475. ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
  3476. : Known->second.second;
  3477. bool Found = false;
  3478. for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
  3479. if (!Found) {
  3480. if (List->getMethod() == Method) {
  3481. Found = true;
  3482. } else {
  3483. // Keep searching.
  3484. continue;
  3485. }
  3486. }
  3487. if (List->getNext())
  3488. List->setMethod(List->getNext()->getMethod());
  3489. else
  3490. List->setMethod(Method);
  3491. }
  3492. }
  3493. void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
  3494. assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
  3495. for (Decl *D : Names) {
  3496. bool wasHidden = D->isHidden();
  3497. D->setVisibleDespiteOwningModule();
  3498. if (wasHidden && SemaObj) {
  3499. if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
  3500. moveMethodToBackOfGlobalList(*SemaObj, Method);
  3501. }
  3502. }
  3503. }
  3504. }
  3505. void ASTReader::makeModuleVisible(Module *Mod,
  3506. Module::NameVisibilityKind NameVisibility,
  3507. SourceLocation ImportLoc) {
  3508. llvm::SmallPtrSet<Module *, 4> Visited;
  3509. SmallVector<Module *, 4> Stack;
  3510. Stack.push_back(Mod);
  3511. while (!Stack.empty()) {
  3512. Mod = Stack.pop_back_val();
  3513. if (NameVisibility <= Mod->NameVisibility) {
  3514. // This module already has this level of visibility (or greater), so
  3515. // there is nothing more to do.
  3516. continue;
  3517. }
  3518. if (!Mod->isAvailable()) {
  3519. // Modules that aren't available cannot be made visible.
  3520. continue;
  3521. }
  3522. // Update the module's name visibility.
  3523. Mod->NameVisibility = NameVisibility;
  3524. // If we've already deserialized any names from this module,
  3525. // mark them as visible.
  3526. HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
  3527. if (Hidden != HiddenNamesMap.end()) {
  3528. auto HiddenNames = std::move(*Hidden);
  3529. HiddenNamesMap.erase(Hidden);
  3530. makeNamesVisible(HiddenNames.second, HiddenNames.first);
  3531. assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
  3532. "making names visible added hidden names");
  3533. }
  3534. // Push any exported modules onto the stack to be marked as visible.
  3535. SmallVector<Module *, 16> Exports;
  3536. Mod->getExportedModules(Exports);
  3537. for (SmallVectorImpl<Module *>::iterator
  3538. I = Exports.begin(), E = Exports.end(); I != E; ++I) {
  3539. Module *Exported = *I;
  3540. if (Visited.insert(Exported).second)
  3541. Stack.push_back(Exported);
  3542. }
  3543. }
  3544. }
  3545. /// We've merged the definition \p MergedDef into the existing definition
  3546. /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
  3547. /// visible.
  3548. void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
  3549. NamedDecl *MergedDef) {
  3550. if (Def->isHidden()) {
  3551. // If MergedDef is visible or becomes visible, make the definition visible.
  3552. if (!MergedDef->isHidden())
  3553. Def->setVisibleDespiteOwningModule();
  3554. else {
  3555. getContext().mergeDefinitionIntoModule(
  3556. Def, MergedDef->getImportedOwningModule(),
  3557. /*NotifyListeners*/ false);
  3558. PendingMergedDefinitionsToDeduplicate.insert(Def);
  3559. }
  3560. }
  3561. }
  3562. bool ASTReader::loadGlobalIndex() {
  3563. if (GlobalIndex)
  3564. return false;
  3565. if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
  3566. !PP.getLangOpts().Modules)
  3567. return true;
  3568. // Try to load the global index.
  3569. TriedLoadingGlobalIndex = true;
  3570. StringRef ModuleCachePath
  3571. = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
  3572. std::pair<GlobalModuleIndex *, llvm::Error> Result =
  3573. GlobalModuleIndex::readIndex(ModuleCachePath);
  3574. if (llvm::Error Err = std::move(Result.second)) {
  3575. assert(!Result.first);
  3576. consumeError(std::move(Err)); // FIXME this drops errors on the floor.
  3577. return true;
  3578. }
  3579. GlobalIndex.reset(Result.first);
  3580. ModuleMgr.setGlobalIndex(GlobalIndex.get());
  3581. return false;
  3582. }
  3583. bool ASTReader::isGlobalIndexUnavailable() const {
  3584. return PP.getLangOpts().Modules && UseGlobalIndex &&
  3585. !hasGlobalIndex() && TriedLoadingGlobalIndex;
  3586. }
  3587. static void updateModuleTimestamp(ModuleFile &MF) {
  3588. // Overwrite the timestamp file contents so that file's mtime changes.
  3589. std::string TimestampFilename = MF.getTimestampFilename();
  3590. std::error_code EC;
  3591. llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
  3592. if (EC)
  3593. return;
  3594. OS << "Timestamp file\n";
  3595. OS.close();
  3596. OS.clear_error(); // Avoid triggering a fatal error.
  3597. }
  3598. /// Given a cursor at the start of an AST file, scan ahead and drop the
  3599. /// cursor into the start of the given block ID, returning false on success and
  3600. /// true on failure.
  3601. static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
  3602. while (true) {
  3603. Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
  3604. if (!MaybeEntry) {
  3605. // FIXME this drops errors on the floor.
  3606. consumeError(MaybeEntry.takeError());
  3607. return true;
  3608. }
  3609. llvm::BitstreamEntry Entry = MaybeEntry.get();
  3610. switch (Entry.Kind) {
  3611. case llvm::BitstreamEntry::Error:
  3612. case llvm::BitstreamEntry::EndBlock:
  3613. return true;
  3614. case llvm::BitstreamEntry::Record:
  3615. // Ignore top-level records.
  3616. if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
  3617. break;
  3618. else {
  3619. // FIXME this drops errors on the floor.
  3620. consumeError(Skipped.takeError());
  3621. return true;
  3622. }
  3623. case llvm::BitstreamEntry::SubBlock:
  3624. if (Entry.ID == BlockID) {
  3625. if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
  3626. // FIXME this drops the error on the floor.
  3627. consumeError(std::move(Err));
  3628. return true;
  3629. }
  3630. // Found it!
  3631. return false;
  3632. }
  3633. if (llvm::Error Err = Cursor.SkipBlock()) {
  3634. // FIXME this drops the error on the floor.
  3635. consumeError(std::move(Err));
  3636. return true;
  3637. }
  3638. }
  3639. }
  3640. }
  3641. ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
  3642. ModuleKind Type,
  3643. SourceLocation ImportLoc,
  3644. unsigned ClientLoadCapabilities,
  3645. SmallVectorImpl<ImportedSubmodule> *Imported) {
  3646. llvm::SaveAndRestore<SourceLocation>
  3647. SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
  3648. // Defer any pending actions until we get to the end of reading the AST file.
  3649. Deserializing AnASTFile(this);
  3650. // Bump the generation number.
  3651. unsigned PreviousGeneration = 0;
  3652. if (ContextObj)
  3653. PreviousGeneration = incrementGeneration(*ContextObj);
  3654. unsigned NumModules = ModuleMgr.size();
  3655. SmallVector<ImportedModule, 4> Loaded;
  3656. switch (ASTReadResult ReadResult =
  3657. ReadASTCore(FileName, Type, ImportLoc,
  3658. /*ImportedBy=*/nullptr, Loaded, 0, 0,
  3659. ASTFileSignature(), ClientLoadCapabilities)) {
  3660. case Failure:
  3661. case Missing:
  3662. case OutOfDate:
  3663. case VersionMismatch:
  3664. case ConfigurationMismatch:
  3665. case HadErrors: {
  3666. llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
  3667. for (const ImportedModule &IM : Loaded)
  3668. LoadedSet.insert(IM.Mod);
  3669. ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, LoadedSet,
  3670. PP.getLangOpts().Modules
  3671. ? &PP.getHeaderSearchInfo().getModuleMap()
  3672. : nullptr);
  3673. // If we find that any modules are unusable, the global index is going
  3674. // to be out-of-date. Just remove it.
  3675. GlobalIndex.reset();
  3676. ModuleMgr.setGlobalIndex(nullptr);
  3677. return ReadResult;
  3678. }
  3679. case Success:
  3680. break;
  3681. }
  3682. // Here comes stuff that we only do once the entire chain is loaded.
  3683. // Load the AST blocks of all of the modules that we loaded.
  3684. for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
  3685. MEnd = Loaded.end();
  3686. M != MEnd; ++M) {
  3687. ModuleFile &F = *M->Mod;
  3688. // Read the AST block.
  3689. if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
  3690. return Result;
  3691. // Read the extension blocks.
  3692. while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
  3693. if (ASTReadResult Result = ReadExtensionBlock(F))
  3694. return Result;
  3695. }
  3696. // Once read, set the ModuleFile bit base offset and update the size in
  3697. // bits of all files we've seen.
  3698. F.GlobalBitOffset = TotalModulesSizeInBits;
  3699. TotalModulesSizeInBits += F.SizeInBits;
  3700. GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
  3701. // Preload SLocEntries.
  3702. for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
  3703. int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
  3704. // Load it through the SourceManager and don't call ReadSLocEntry()
  3705. // directly because the entry may have already been loaded in which case
  3706. // calling ReadSLocEntry() directly would trigger an assertion in
  3707. // SourceManager.
  3708. SourceMgr.getLoadedSLocEntryByID(Index);
  3709. }
  3710. // Map the original source file ID into the ID space of the current
  3711. // compilation.
  3712. if (F.OriginalSourceFileID.isValid()) {
  3713. F.OriginalSourceFileID = FileID::get(
  3714. F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
  3715. }
  3716. // Preload all the pending interesting identifiers by marking them out of
  3717. // date.
  3718. for (auto Offset : F.PreloadIdentifierOffsets) {
  3719. const unsigned char *Data = reinterpret_cast<const unsigned char *>(
  3720. F.IdentifierTableData + Offset);
  3721. ASTIdentifierLookupTrait Trait(*this, F);
  3722. auto KeyDataLen = Trait.ReadKeyDataLength(Data);
  3723. auto Key = Trait.ReadKey(Data, KeyDataLen.first);
  3724. auto &II = PP.getIdentifierTable().getOwn(Key);
  3725. II.setOutOfDate(true);
  3726. // Mark this identifier as being from an AST file so that we can track
  3727. // whether we need to serialize it.
  3728. markIdentifierFromAST(*this, II);
  3729. // Associate the ID with the identifier so that the writer can reuse it.
  3730. auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
  3731. SetIdentifierInfo(ID, &II);
  3732. }
  3733. }
  3734. // Setup the import locations and notify the module manager that we've
  3735. // committed to these module files.
  3736. for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
  3737. MEnd = Loaded.end();
  3738. M != MEnd; ++M) {
  3739. ModuleFile &F = *M->Mod;
  3740. ModuleMgr.moduleFileAccepted(&F);
  3741. // Set the import location.
  3742. F.DirectImportLoc = ImportLoc;
  3743. // FIXME: We assume that locations from PCH / preamble do not need
  3744. // any translation.
  3745. if (!M->ImportedBy)
  3746. F.ImportLoc = M->ImportLoc;
  3747. else
  3748. F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
  3749. }
  3750. if (!PP.getLangOpts().CPlusPlus ||
  3751. (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
  3752. Type != MK_PrebuiltModule)) {
  3753. // Mark all of the identifiers in the identifier table as being out of date,
  3754. // so that various accessors know to check the loaded modules when the
  3755. // identifier is used.
  3756. //
  3757. // For C++ modules, we don't need information on many identifiers (just
  3758. // those that provide macros or are poisoned), so we mark all of
  3759. // the interesting ones via PreloadIdentifierOffsets.
  3760. for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
  3761. IdEnd = PP.getIdentifierTable().end();
  3762. Id != IdEnd; ++Id)
  3763. Id->second->setOutOfDate(true);
  3764. }
  3765. // Mark selectors as out of date.
  3766. for (auto Sel : SelectorGeneration)
  3767. SelectorOutOfDate[Sel.first] = true;
  3768. // Resolve any unresolved module exports.
  3769. for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
  3770. UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
  3771. SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
  3772. Module *ResolvedMod = getSubmodule(GlobalID);
  3773. switch (Unresolved.Kind) {
  3774. case UnresolvedModuleRef::Conflict:
  3775. if (ResolvedMod) {
  3776. Module::Conflict Conflict;
  3777. Conflict.Other = ResolvedMod;
  3778. Conflict.Message = Unresolved.String.str();
  3779. Unresolved.Mod->Conflicts.push_back(Conflict);
  3780. }
  3781. continue;
  3782. case UnresolvedModuleRef::Import:
  3783. if (ResolvedMod)
  3784. Unresolved.Mod->Imports.insert(ResolvedMod);
  3785. continue;
  3786. case UnresolvedModuleRef::Export:
  3787. if (ResolvedMod || Unresolved.IsWildcard)
  3788. Unresolved.Mod->Exports.push_back(
  3789. Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
  3790. continue;
  3791. }
  3792. }
  3793. UnresolvedModuleRefs.clear();
  3794. if (Imported)
  3795. Imported->append(ImportedModules.begin(),
  3796. ImportedModules.end());
  3797. // FIXME: How do we load the 'use'd modules? They may not be submodules.
  3798. // Might be unnecessary as use declarations are only used to build the
  3799. // module itself.
  3800. if (ContextObj)
  3801. InitializeContext();
  3802. if (SemaObj)
  3803. UpdateSema();
  3804. if (DeserializationListener)
  3805. DeserializationListener->ReaderInitialized(this);
  3806. ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
  3807. if (PrimaryModule.OriginalSourceFileID.isValid()) {
  3808. // If this AST file is a precompiled preamble, then set the
  3809. // preamble file ID of the source manager to the file source file
  3810. // from which the preamble was built.
  3811. if (Type == MK_Preamble) {
  3812. SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
  3813. } else if (Type == MK_MainFile) {
  3814. SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
  3815. }
  3816. }
  3817. // For any Objective-C class definitions we have already loaded, make sure
  3818. // that we load any additional categories.
  3819. if (ContextObj) {
  3820. for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
  3821. loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
  3822. ObjCClassesLoaded[I],
  3823. PreviousGeneration);
  3824. }
  3825. }
  3826. if (PP.getHeaderSearchInfo()
  3827. .getHeaderSearchOpts()
  3828. .ModulesValidateOncePerBuildSession) {
  3829. // Now we are certain that the module and all modules it depends on are
  3830. // up to date. Create or update timestamp files for modules that are
  3831. // located in the module cache (not for PCH files that could be anywhere
  3832. // in the filesystem).
  3833. for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
  3834. ImportedModule &M = Loaded[I];
  3835. if (M.Mod->Kind == MK_ImplicitModule) {
  3836. updateModuleTimestamp(*M.Mod);
  3837. }
  3838. }
  3839. }
  3840. return Success;
  3841. }
  3842. static ASTFileSignature readASTFileSignature(StringRef PCH);
  3843. /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
  3844. static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
  3845. // FIXME checking magic headers is done in other places such as
  3846. // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
  3847. // always done the same. Unify it all with a helper.
  3848. if (!Stream.canSkipToPos(4))
  3849. return llvm::createStringError(std::errc::illegal_byte_sequence,
  3850. "file too small to contain AST file magic");
  3851. for (unsigned C : {'C', 'P', 'C', 'H'})
  3852. if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
  3853. if (Res.get() != C)
  3854. return llvm::createStringError(
  3855. std::errc::illegal_byte_sequence,
  3856. "file doesn't start with AST file magic");
  3857. } else
  3858. return Res.takeError();
  3859. return llvm::Error::success();
  3860. }
  3861. static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
  3862. switch (Kind) {
  3863. case MK_PCH:
  3864. return 0; // PCH
  3865. case MK_ImplicitModule:
  3866. case MK_ExplicitModule:
  3867. case MK_PrebuiltModule:
  3868. return 1; // module
  3869. case MK_MainFile:
  3870. case MK_Preamble:
  3871. return 2; // main source file
  3872. }
  3873. llvm_unreachable("unknown module kind");
  3874. }
  3875. ASTReader::ASTReadResult
  3876. ASTReader::ReadASTCore(StringRef FileName,
  3877. ModuleKind Type,
  3878. SourceLocation ImportLoc,
  3879. ModuleFile *ImportedBy,
  3880. SmallVectorImpl<ImportedModule> &Loaded,
  3881. off_t ExpectedSize, time_t ExpectedModTime,
  3882. ASTFileSignature ExpectedSignature,
  3883. unsigned ClientLoadCapabilities) {
  3884. ModuleFile *M;
  3885. std::string ErrorStr;
  3886. ModuleManager::AddModuleResult AddResult
  3887. = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
  3888. getGeneration(), ExpectedSize, ExpectedModTime,
  3889. ExpectedSignature, readASTFileSignature,
  3890. M, ErrorStr);
  3891. switch (AddResult) {
  3892. case ModuleManager::AlreadyLoaded:
  3893. Diag(diag::remark_module_import)
  3894. << M->ModuleName << M->FileName << (ImportedBy ? true : false)
  3895. << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
  3896. return Success;
  3897. case ModuleManager::NewlyLoaded:
  3898. // Load module file below.
  3899. break;
  3900. case ModuleManager::Missing:
  3901. // The module file was missing; if the client can handle that, return
  3902. // it.
  3903. if (ClientLoadCapabilities & ARR_Missing)
  3904. return Missing;
  3905. // Otherwise, return an error.
  3906. Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
  3907. << FileName << !ErrorStr.empty()
  3908. << ErrorStr;
  3909. return Failure;
  3910. case ModuleManager::OutOfDate:
  3911. // We couldn't load the module file because it is out-of-date. If the
  3912. // client can handle out-of-date, return it.
  3913. if (ClientLoadCapabilities & ARR_OutOfDate)
  3914. return OutOfDate;
  3915. // Otherwise, return an error.
  3916. Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
  3917. << FileName << !ErrorStr.empty()
  3918. << ErrorStr;
  3919. return Failure;
  3920. }
  3921. assert(M && "Missing module file");
  3922. bool ShouldFinalizePCM = false;
  3923. auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
  3924. auto &MC = getModuleManager().getModuleCache();
  3925. if (ShouldFinalizePCM)
  3926. MC.finalizePCM(FileName);
  3927. else
  3928. MC.tryToDropPCM(FileName);
  3929. });
  3930. ModuleFile &F = *M;
  3931. BitstreamCursor &Stream = F.Stream;
  3932. Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
  3933. F.SizeInBits = F.Buffer->getBufferSize() * 8;
  3934. // Sniff for the signature.
  3935. if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
  3936. Diag(diag::err_module_file_invalid)
  3937. << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
  3938. return Failure;
  3939. }
  3940. // This is used for compatibility with older PCH formats.
  3941. bool HaveReadControlBlock = false;
  3942. while (true) {
  3943. Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
  3944. if (!MaybeEntry) {
  3945. Error(MaybeEntry.takeError());
  3946. return Failure;
  3947. }
  3948. llvm::BitstreamEntry Entry = MaybeEntry.get();
  3949. switch (Entry.Kind) {
  3950. case llvm::BitstreamEntry::Error:
  3951. case llvm::BitstreamEntry::Record:
  3952. case llvm::BitstreamEntry::EndBlock:
  3953. Error("invalid record at top-level of AST file");
  3954. return Failure;
  3955. case llvm::BitstreamEntry::SubBlock:
  3956. break;
  3957. }
  3958. switch (Entry.ID) {
  3959. case CONTROL_BLOCK_ID:
  3960. HaveReadControlBlock = true;
  3961. switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
  3962. case Success:
  3963. // Check that we didn't try to load a non-module AST file as a module.
  3964. //
  3965. // FIXME: Should we also perform the converse check? Loading a module as
  3966. // a PCH file sort of works, but it's a bit wonky.
  3967. if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
  3968. Type == MK_PrebuiltModule) &&
  3969. F.ModuleName.empty()) {
  3970. auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
  3971. if (Result != OutOfDate ||
  3972. (ClientLoadCapabilities & ARR_OutOfDate) == 0)
  3973. Diag(diag::err_module_file_not_module) << FileName;
  3974. return Result;
  3975. }
  3976. break;
  3977. case Failure: return Failure;
  3978. case Missing: return Missing;
  3979. case OutOfDate: return OutOfDate;
  3980. case VersionMismatch: return VersionMismatch;
  3981. case ConfigurationMismatch: return ConfigurationMismatch;
  3982. case HadErrors: return HadErrors;
  3983. }
  3984. break;
  3985. case AST_BLOCK_ID:
  3986. if (!HaveReadControlBlock) {
  3987. if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
  3988. Diag(diag::err_pch_version_too_old);
  3989. return VersionMismatch;
  3990. }
  3991. // Record that we've loaded this module.
  3992. Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
  3993. ShouldFinalizePCM = true;
  3994. return Success;
  3995. case UNHASHED_CONTROL_BLOCK_ID:
  3996. // This block is handled using look-ahead during ReadControlBlock. We
  3997. // shouldn't get here!
  3998. Error("malformed block record in AST file");
  3999. return Failure;
  4000. default:
  4001. if (llvm::Error Err = Stream.SkipBlock()) {
  4002. Error(std::move(Err));
  4003. return Failure;
  4004. }
  4005. break;
  4006. }
  4007. }
  4008. llvm_unreachable("unexpected break; expected return");
  4009. }
  4010. ASTReader::ASTReadResult
  4011. ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
  4012. unsigned ClientLoadCapabilities) {
  4013. const HeaderSearchOptions &HSOpts =
  4014. PP.getHeaderSearchInfo().getHeaderSearchOpts();
  4015. bool AllowCompatibleConfigurationMismatch =
  4016. F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
  4017. ASTReadResult Result = readUnhashedControlBlockImpl(
  4018. &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
  4019. Listener.get(),
  4020. WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
  4021. // If F was directly imported by another module, it's implicitly validated by
  4022. // the importing module.
  4023. if (DisableValidation || WasImportedBy ||
  4024. (AllowConfigurationMismatch && Result == ConfigurationMismatch))
  4025. return Success;
  4026. if (Result == Failure) {
  4027. Error("malformed block record in AST file");
  4028. return Failure;
  4029. }
  4030. if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
  4031. // If this module has already been finalized in the ModuleCache, we're stuck
  4032. // with it; we can only load a single version of each module.
  4033. //
  4034. // This can happen when a module is imported in two contexts: in one, as a
  4035. // user module; in another, as a system module (due to an import from
  4036. // another module marked with the [system] flag). It usually indicates a
  4037. // bug in the module map: this module should also be marked with [system].
  4038. //
  4039. // If -Wno-system-headers (the default), and the first import is as a
  4040. // system module, then validation will fail during the as-user import,
  4041. // since -Werror flags won't have been validated. However, it's reasonable
  4042. // to treat this consistently as a system module.
  4043. //
  4044. // If -Wsystem-headers, the PCM on disk was built with
  4045. // -Wno-system-headers, and the first import is as a user module, then
  4046. // validation will fail during the as-system import since the PCM on disk
  4047. // doesn't guarantee that -Werror was respected. However, the -Werror
  4048. // flags were checked during the initial as-user import.
  4049. if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
  4050. Diag(diag::warn_module_system_bit_conflict) << F.FileName;
  4051. return Success;
  4052. }
  4053. }
  4054. return Result;
  4055. }
  4056. ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
  4057. ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
  4058. bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
  4059. bool ValidateDiagnosticOptions) {
  4060. // Initialize a stream.
  4061. BitstreamCursor Stream(StreamData);
  4062. // Sniff for the signature.
  4063. if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
  4064. // FIXME this drops the error on the floor.
  4065. consumeError(std::move(Err));
  4066. return Failure;
  4067. }
  4068. // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
  4069. if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
  4070. return Failure;
  4071. // Read all of the records in the options block.
  4072. RecordData Record;
  4073. ASTReadResult Result = Success;
  4074. while (true) {
  4075. Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
  4076. if (!MaybeEntry) {
  4077. // FIXME this drops the error on the floor.
  4078. consumeError(MaybeEntry.takeError());
  4079. return Failure;
  4080. }
  4081. llvm::BitstreamEntry Entry = MaybeEntry.get();
  4082. switch (Entry.Kind) {
  4083. case llvm::BitstreamEntry::Error:
  4084. case llvm::BitstreamEntry::SubBlock:
  4085. return Failure;
  4086. case llvm::BitstreamEntry::EndBlock:
  4087. return Result;
  4088. case llvm::BitstreamEntry::Record:
  4089. // The interesting case.
  4090. break;
  4091. }
  4092. // Read and process a record.
  4093. Record.clear();
  4094. Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
  4095. if (!MaybeRecordType) {
  4096. // FIXME this drops the error.
  4097. return Failure;
  4098. }
  4099. switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
  4100. case SIGNATURE:
  4101. if (F)
  4102. std::copy(Record.begin(), Record.end(), F->Signature.data());
  4103. break;
  4104. case DIAGNOSTIC_OPTIONS: {
  4105. bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
  4106. if (Listener && ValidateDiagnosticOptions &&
  4107. !AllowCompatibleConfigurationMismatch &&
  4108. ParseDiagnosticOptions(Record, Complain, *Listener))
  4109. Result = OutOfDate; // Don't return early. Read the signature.
  4110. break;
  4111. }
  4112. case DIAG_PRAGMA_MAPPINGS:
  4113. if (!F)
  4114. break;
  4115. if (F->PragmaDiagMappings.empty())
  4116. F->PragmaDiagMappings.swap(Record);
  4117. else
  4118. F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
  4119. Record.begin(), Record.end());
  4120. break;
  4121. }
  4122. }
  4123. }
  4124. /// Parse a record and blob containing module file extension metadata.
  4125. static bool parseModuleFileExtensionMetadata(
  4126. const SmallVectorImpl<uint64_t> &Record,
  4127. StringRef Blob,
  4128. ModuleFileExtensionMetadata &Metadata) {
  4129. if (Record.size() < 4) return true;
  4130. Metadata.MajorVersion = Record[0];
  4131. Metadata.MinorVersion = Record[1];
  4132. unsigned BlockNameLen = Record[2];
  4133. unsigned UserInfoLen = Record[3];
  4134. if (BlockNameLen + UserInfoLen > Blob.size()) return true;
  4135. Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
  4136. Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
  4137. Blob.data() + BlockNameLen + UserInfoLen);
  4138. return false;
  4139. }
  4140. ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
  4141. BitstreamCursor &Stream = F.Stream;
  4142. RecordData Record;
  4143. while (true) {
  4144. Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
  4145. if (!MaybeEntry) {
  4146. Error(MaybeEntry.takeError());
  4147. return Failure;
  4148. }
  4149. llvm::BitstreamEntry Entry = MaybeEntry.get();
  4150. switch (Entry.Kind) {
  4151. case llvm::BitstreamEntry::SubBlock:
  4152. if (llvm::Error Err = Stream.SkipBlock()) {
  4153. Error(std::move(Err));
  4154. return Failure;
  4155. }
  4156. continue;
  4157. case llvm::BitstreamEntry::EndBlock:
  4158. return Success;
  4159. case llvm::BitstreamEntry::Error:
  4160. return HadErrors;
  4161. case llvm::BitstreamEntry::Record:
  4162. break;
  4163. }
  4164. Record.clear();
  4165. StringRef Blob;
  4166. Expected<unsigned> MaybeRecCode =
  4167. Stream.readRecord(Entry.ID, Record, &Blob);
  4168. if (!MaybeRecCode) {
  4169. Error(MaybeRecCode.takeError());
  4170. return Failure;
  4171. }
  4172. switch (MaybeRecCode.get()) {
  4173. case EXTENSION_METADATA: {
  4174. ModuleFileExtensionMetadata Metadata;
  4175. if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
  4176. return Failure;
  4177. // Find a module file extension with this block name.
  4178. auto Known = ModuleFileExtensions.find(Metadata.BlockName);
  4179. if (Known == ModuleFileExtensions.end()) break;
  4180. // Form a reader.
  4181. if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
  4182. F, Stream)) {
  4183. F.ExtensionReaders.push_back(std::move(Reader));
  4184. }
  4185. break;
  4186. }
  4187. }
  4188. }
  4189. return Success;
  4190. }
  4191. void ASTReader::InitializeContext() {
  4192. assert(ContextObj && "no context to initialize");
  4193. ASTContext &Context = *ContextObj;
  4194. // If there's a listener, notify them that we "read" the translation unit.
  4195. if (DeserializationListener)
  4196. DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
  4197. Context.getTranslationUnitDecl());
  4198. // FIXME: Find a better way to deal with collisions between these
  4199. // built-in types. Right now, we just ignore the problem.
  4200. // Load the special types.
  4201. if (SpecialTypes.size() >= NumSpecialTypeIDs) {
  4202. if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
  4203. if (!Context.CFConstantStringTypeDecl)
  4204. Context.setCFConstantStringType(GetType(String));
  4205. }
  4206. if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
  4207. QualType FileType = GetType(File);
  4208. if (FileType.isNull()) {
  4209. Error("FILE type is NULL");
  4210. return;
  4211. }
  4212. if (!Context.FILEDecl) {
  4213. if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
  4214. Context.setFILEDecl(Typedef->getDecl());
  4215. else {
  4216. const TagType *Tag = FileType->getAs<TagType>();
  4217. if (!Tag) {
  4218. Error("Invalid FILE type in AST file");
  4219. return;
  4220. }
  4221. Context.setFILEDecl(Tag->getDecl());
  4222. }
  4223. }
  4224. }
  4225. if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
  4226. QualType Jmp_bufType = GetType(Jmp_buf);
  4227. if (Jmp_bufType.isNull()) {
  4228. Error("jmp_buf type is NULL");
  4229. return;
  4230. }
  4231. if (!Context.jmp_bufDecl) {
  4232. if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
  4233. Context.setjmp_bufDecl(Typedef->getDecl());
  4234. else {
  4235. const TagType *Tag = Jmp_bufType->getAs<TagType>();
  4236. if (!Tag) {
  4237. Error("Invalid jmp_buf type in AST file");
  4238. return;
  4239. }
  4240. Context.setjmp_bufDecl(Tag->getDecl());
  4241. }
  4242. }
  4243. }
  4244. if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
  4245. QualType Sigjmp_bufType = GetType(Sigjmp_buf);
  4246. if (Sigjmp_bufType.isNull()) {
  4247. Error("sigjmp_buf type is NULL");
  4248. return;
  4249. }
  4250. if (!Context.sigjmp_bufDecl) {
  4251. if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
  4252. Context.setsigjmp_bufDecl(Typedef->getDecl());
  4253. else {
  4254. const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
  4255. assert(Tag && "Invalid sigjmp_buf type in AST file");
  4256. Context.setsigjmp_bufDecl(Tag->getDecl());
  4257. }
  4258. }
  4259. }
  4260. if (unsigned ObjCIdRedef
  4261. = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
  4262. if (Context.ObjCIdRedefinitionType.isNull())
  4263. Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
  4264. }
  4265. if (unsigned ObjCClassRedef
  4266. = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
  4267. if (Context.ObjCClassRedefinitionType.isNull())
  4268. Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
  4269. }
  4270. if (unsigned ObjCSelRedef
  4271. = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
  4272. if (Context.ObjCSelRedefinitionType.isNull())
  4273. Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
  4274. }
  4275. if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
  4276. QualType Ucontext_tType = GetType(Ucontext_t);
  4277. if (Ucontext_tType.isNull()) {
  4278. Error("ucontext_t type is NULL");
  4279. return;
  4280. }
  4281. if (!Context.ucontext_tDecl) {
  4282. if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
  4283. Context.setucontext_tDecl(Typedef->getDecl());
  4284. else {
  4285. const TagType *Tag = Ucontext_tType->getAs<TagType>();
  4286. assert(Tag && "Invalid ucontext_t type in AST file");
  4287. Context.setucontext_tDecl(Tag->getDecl());
  4288. }
  4289. }
  4290. }
  4291. }
  4292. ReadPragmaDiagnosticMappings(Context.getDiagnostics());
  4293. // If there were any CUDA special declarations, deserialize them.
  4294. if (!CUDASpecialDeclRefs.empty()) {
  4295. assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
  4296. Context.setcudaConfigureCallDecl(
  4297. cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
  4298. }
  4299. // Re-export any modules that were imported by a non-module AST file.
  4300. // FIXME: This does not make macro-only imports visible again.
  4301. for (auto &Import : ImportedModules) {
  4302. if (Module *Imported = getSubmodule(Import.ID)) {
  4303. makeModuleVisible(Imported, Module::AllVisible,
  4304. /*ImportLoc=*/Import.ImportLoc);
  4305. if (Import.ImportLoc.isValid())
  4306. PP.makeModuleVisible(Imported, Import.ImportLoc);
  4307. // FIXME: should we tell Sema to make the module visible too?
  4308. }
  4309. }
  4310. ImportedModules.clear();
  4311. }
  4312. void ASTReader::finalizeForWriting() {
  4313. // Nothing to do for now.
  4314. }
  4315. /// Reads and return the signature record from \p PCH's control block, or
  4316. /// else returns 0.
  4317. static ASTFileSignature readASTFileSignature(StringRef PCH) {
  4318. BitstreamCursor Stream(PCH);
  4319. if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
  4320. // FIXME this drops the error on the floor.
  4321. consumeError(std::move(Err));
  4322. return ASTFileSignature();
  4323. }
  4324. // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
  4325. if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
  4326. return ASTFileSignature();
  4327. // Scan for SIGNATURE inside the diagnostic options block.
  4328. ASTReader::RecordData Record;
  4329. while (true) {
  4330. Expected<llvm::BitstreamEntry> MaybeEntry =
  4331. Stream.advanceSkippingSubblocks();
  4332. if (!MaybeEntry) {
  4333. // FIXME this drops the error on the floor.
  4334. consumeError(MaybeEntry.takeError());
  4335. return ASTFileSignature();
  4336. }
  4337. llvm::BitstreamEntry Entry = MaybeEntry.get();
  4338. if (Entry.Kind != llvm::BitstreamEntry::Record)
  4339. return ASTFileSignature();
  4340. Record.clear();
  4341. StringRef Blob;
  4342. Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
  4343. if (!MaybeRecord) {
  4344. // FIXME this drops the error on the floor.
  4345. consumeError(MaybeRecord.takeError());
  4346. return ASTFileSignature();
  4347. }
  4348. if (SIGNATURE == MaybeRecord.get())
  4349. return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
  4350. (uint32_t)Record[3], (uint32_t)Record[4]}}};
  4351. }
  4352. }
  4353. /// Retrieve the name of the original source file name
  4354. /// directly from the AST file, without actually loading the AST
  4355. /// file.
  4356. std::string ASTReader::getOriginalSourceFile(
  4357. const std::string &ASTFileName, FileManager &FileMgr,
  4358. const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
  4359. // Open the AST file.
  4360. auto Buffer = FileMgr.getBufferForFile(ASTFileName);
  4361. if (!Buffer) {
  4362. Diags.Report(diag::err_fe_unable_to_read_pch_file)
  4363. << ASTFileName << Buffer.getError().message();
  4364. return std::string();
  4365. }
  4366. // Initialize the stream
  4367. BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
  4368. // Sniff for the signature.
  4369. if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
  4370. Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
  4371. return std::string();
  4372. }
  4373. // Scan for the CONTROL_BLOCK_ID block.
  4374. if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
  4375. Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
  4376. return std::string();
  4377. }
  4378. // Scan for ORIGINAL_FILE inside the control block.
  4379. RecordData Record;
  4380. while (true) {
  4381. Expected<llvm::BitstreamEntry> MaybeEntry =
  4382. Stream.advanceSkippingSubblocks();
  4383. if (!MaybeEntry) {
  4384. // FIXME this drops errors on the floor.
  4385. consumeError(MaybeEntry.takeError());
  4386. return std::string();
  4387. }
  4388. llvm::BitstreamEntry Entry = MaybeEntry.get();
  4389. if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
  4390. return std::string();
  4391. if (Entry.Kind != llvm::BitstreamEntry::Record) {
  4392. Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
  4393. return std::string();
  4394. }
  4395. Record.clear();
  4396. StringRef Blob;
  4397. Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
  4398. if (!MaybeRecord) {
  4399. // FIXME this drops the errors on the floor.
  4400. consumeError(MaybeRecord.takeError());
  4401. return std::string();
  4402. }
  4403. if (ORIGINAL_FILE == MaybeRecord.get())
  4404. return Blob.str();
  4405. }
  4406. }
  4407. namespace {
  4408. class SimplePCHValidator : public ASTReaderListener {
  4409. const LangOptions &ExistingLangOpts;
  4410. const TargetOptions &ExistingTargetOpts;
  4411. const PreprocessorOptions &ExistingPPOpts;
  4412. std::string ExistingModuleCachePath;
  4413. FileManager &FileMgr;
  4414. public:
  4415. SimplePCHValidator(const LangOptions &ExistingLangOpts,
  4416. const TargetOptions &ExistingTargetOpts,
  4417. const PreprocessorOptions &ExistingPPOpts,
  4418. StringRef ExistingModuleCachePath,
  4419. FileManager &FileMgr)
  4420. : ExistingLangOpts(ExistingLangOpts),
  4421. ExistingTargetOpts(ExistingTargetOpts),
  4422. ExistingPPOpts(ExistingPPOpts),
  4423. ExistingModuleCachePath(ExistingModuleCachePath),
  4424. FileMgr(FileMgr) {}
  4425. bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
  4426. bool AllowCompatibleDifferences) override {
  4427. return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
  4428. AllowCompatibleDifferences);
  4429. }
  4430. bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
  4431. bool AllowCompatibleDifferences) override {
  4432. return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
  4433. AllowCompatibleDifferences);
  4434. }
  4435. bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
  4436. StringRef SpecificModuleCachePath,
  4437. bool Complain) override {
  4438. return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
  4439. ExistingModuleCachePath,
  4440. nullptr, ExistingLangOpts);
  4441. }
  4442. bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
  4443. bool Complain,
  4444. std::string &SuggestedPredefines) override {
  4445. return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
  4446. SuggestedPredefines, ExistingLangOpts);
  4447. }
  4448. };
  4449. } // namespace
  4450. bool ASTReader::readASTFileControlBlock(
  4451. StringRef Filename, FileManager &FileMgr,
  4452. const PCHContainerReader &PCHContainerRdr,
  4453. bool FindModuleFileExtensions,
  4454. ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
  4455. // Open the AST file.
  4456. // FIXME: This allows use of the VFS; we do not allow use of the
  4457. // VFS when actually loading a module.
  4458. auto Buffer = FileMgr.getBufferForFile(Filename);
  4459. if (!Buffer) {
  4460. return true;
  4461. }
  4462. // Initialize the stream
  4463. StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
  4464. BitstreamCursor Stream(Bytes);
  4465. // Sniff for the signature.
  4466. if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
  4467. consumeError(std::move(Err)); // FIXME this drops errors on the floor.
  4468. return true;
  4469. }
  4470. // Scan for the CONTROL_BLOCK_ID block.
  4471. if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
  4472. return true;
  4473. bool NeedsInputFiles = Listener.needsInputFileVisitation();
  4474. bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
  4475. bool NeedsImports = Listener.needsImportVisitation();
  4476. BitstreamCursor InputFilesCursor;
  4477. RecordData Record;
  4478. std::string ModuleDir;
  4479. bool DoneWithControlBlock = false;
  4480. while (!DoneWithControlBlock) {
  4481. Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
  4482. if (!MaybeEntry) {
  4483. // FIXME this drops the error on the floor.
  4484. consumeError(MaybeEntry.takeError());
  4485. return true;
  4486. }
  4487. llvm::BitstreamEntry Entry = MaybeEntry.get();
  4488. switch (Entry.Kind) {
  4489. case llvm::BitstreamEntry::SubBlock: {
  4490. switch (Entry.ID) {
  4491. case OPTIONS_BLOCK_ID: {
  4492. std::string IgnoredSuggestedPredefines;
  4493. if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
  4494. /*AllowCompatibleConfigurationMismatch*/ false,
  4495. Listener, IgnoredSuggestedPredefines) != Success)
  4496. return true;
  4497. break;
  4498. }
  4499. case INPUT_FILES_BLOCK_ID:
  4500. InputFilesCursor = Stream;
  4501. if (llvm::Error Err = Stream.SkipBlock()) {
  4502. // FIXME this drops the error on the floor.
  4503. consumeError(std::move(Err));
  4504. return true;
  4505. }
  4506. if (NeedsInputFiles &&
  4507. ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
  4508. return true;
  4509. break;
  4510. default:
  4511. if (llvm::Error Err = Stream.SkipBlock()) {
  4512. // FIXME this drops the error on the floor.
  4513. consumeError(std::move(Err));
  4514. return true;
  4515. }
  4516. break;
  4517. }
  4518. continue;
  4519. }
  4520. case llvm::BitstreamEntry::EndBlock:
  4521. DoneWithControlBlock = true;
  4522. break;
  4523. case llvm::BitstreamEntry::Error:
  4524. return true;
  4525. case llvm::BitstreamEntry::Record:
  4526. break;
  4527. }
  4528. if (DoneWithControlBlock) break;
  4529. Record.clear();
  4530. StringRef Blob;
  4531. Expected<unsigned> MaybeRecCode =
  4532. Stream.readRecord(Entry.ID, Record, &Blob);
  4533. if (!MaybeRecCode) {
  4534. // FIXME this drops the error.
  4535. return Failure;
  4536. }
  4537. switch ((ControlRecordTypes)MaybeRecCode.get()) {
  4538. case METADATA:
  4539. if (Record[0] != VERSION_MAJOR)
  4540. return true;
  4541. if (Listener.ReadFullVersionInformation(Blob))
  4542. return true;
  4543. break;
  4544. case MODULE_NAME:
  4545. Listener.ReadModuleName(Blob);
  4546. break;
  4547. case MODULE_DIRECTORY:
  4548. ModuleDir = Blob;
  4549. break;
  4550. case MODULE_MAP_FILE: {
  4551. unsigned Idx = 0;
  4552. auto Path = ReadString(Record, Idx);
  4553. ResolveImportedPath(Path, ModuleDir);
  4554. Listener.ReadModuleMapFile(Path);
  4555. break;
  4556. }
  4557. case INPUT_FILE_OFFSETS: {
  4558. if (!NeedsInputFiles)
  4559. break;
  4560. unsigned NumInputFiles = Record[0];
  4561. unsigned NumUserFiles = Record[1];
  4562. const llvm::support::unaligned_uint64_t *InputFileOffs =
  4563. (const llvm::support::unaligned_uint64_t *)Blob.data();
  4564. for (unsigned I = 0; I != NumInputFiles; ++I) {
  4565. // Go find this input file.
  4566. bool isSystemFile = I >= NumUserFiles;
  4567. if (isSystemFile && !NeedsSystemInputFiles)
  4568. break; // the rest are system input files
  4569. BitstreamCursor &Cursor = InputFilesCursor;
  4570. SavedStreamPosition SavedPosition(Cursor);
  4571. if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
  4572. // FIXME this drops errors on the floor.
  4573. consumeError(std::move(Err));
  4574. }
  4575. Expected<unsigned> MaybeCode = Cursor.ReadCode();
  4576. if (!MaybeCode) {
  4577. // FIXME this drops errors on the floor.
  4578. consumeError(MaybeCode.takeError());
  4579. }
  4580. unsigned Code = MaybeCode.get();
  4581. RecordData Record;
  4582. StringRef Blob;
  4583. bool shouldContinue = false;
  4584. Expected<unsigned> MaybeRecordType =
  4585. Cursor.readRecord(Code, Record, &Blob);
  4586. if (!MaybeRecordType) {
  4587. // FIXME this drops errors on the floor.
  4588. consumeError(MaybeRecordType.takeError());
  4589. }
  4590. switch ((InputFileRecordTypes)MaybeRecordType.get()) {
  4591. case INPUT_FILE_HASH:
  4592. break;
  4593. case INPUT_FILE:
  4594. bool Overridden = static_cast<bool>(Record[3]);
  4595. std::string Filename = Blob;
  4596. ResolveImportedPath(Filename, ModuleDir);
  4597. shouldContinue = Listener.visitInputFile(
  4598. Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
  4599. break;
  4600. }
  4601. if (!shouldContinue)
  4602. break;
  4603. }
  4604. break;
  4605. }
  4606. case IMPORTS: {
  4607. if (!NeedsImports)
  4608. break;
  4609. unsigned Idx = 0, N = Record.size();
  4610. while (Idx < N) {
  4611. // Read information about the AST file.
  4612. Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
  4613. std::string ModuleName = ReadString(Record, Idx);
  4614. std::string Filename = ReadString(Record, Idx);
  4615. ResolveImportedPath(Filename, ModuleDir);
  4616. Listener.visitImport(ModuleName, Filename);
  4617. }
  4618. break;
  4619. }
  4620. default:
  4621. // No other validation to perform.
  4622. break;
  4623. }
  4624. }
  4625. // Look for module file extension blocks, if requested.
  4626. if (FindModuleFileExtensions) {
  4627. BitstreamCursor SavedStream = Stream;
  4628. while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
  4629. bool DoneWithExtensionBlock = false;
  4630. while (!DoneWithExtensionBlock) {
  4631. Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
  4632. if (!MaybeEntry) {
  4633. // FIXME this drops the error.
  4634. return true;
  4635. }
  4636. llvm::BitstreamEntry Entry = MaybeEntry.get();
  4637. switch (Entry.Kind) {
  4638. case llvm::BitstreamEntry::SubBlock:
  4639. if (llvm::Error Err = Stream.SkipBlock()) {
  4640. // FIXME this drops the error on the floor.
  4641. consumeError(std::move(Err));
  4642. return true;
  4643. }
  4644. continue;
  4645. case llvm::BitstreamEntry::EndBlock:
  4646. DoneWithExtensionBlock = true;
  4647. continue;
  4648. case llvm::BitstreamEntry::Error:
  4649. return true;
  4650. case llvm::BitstreamEntry::Record:
  4651. break;
  4652. }
  4653. Record.clear();
  4654. StringRef Blob;
  4655. Expected<unsigned> MaybeRecCode =
  4656. Stream.readRecord(Entry.ID, Record, &Blob);
  4657. if (!MaybeRecCode) {
  4658. // FIXME this drops the error.
  4659. return true;
  4660. }
  4661. switch (MaybeRecCode.get()) {
  4662. case EXTENSION_METADATA: {
  4663. ModuleFileExtensionMetadata Metadata;
  4664. if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
  4665. return true;
  4666. Listener.readModuleFileExtension(Metadata);
  4667. break;
  4668. }
  4669. }
  4670. }
  4671. }
  4672. Stream = SavedStream;
  4673. }
  4674. // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
  4675. if (readUnhashedControlBlockImpl(
  4676. nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
  4677. /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
  4678. ValidateDiagnosticOptions) != Success)
  4679. return true;
  4680. return false;
  4681. }
  4682. bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
  4683. const PCHContainerReader &PCHContainerRdr,
  4684. const LangOptions &LangOpts,
  4685. const TargetOptions &TargetOpts,
  4686. const PreprocessorOptions &PPOpts,
  4687. StringRef ExistingModuleCachePath) {
  4688. SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
  4689. ExistingModuleCachePath, FileMgr);
  4690. return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
  4691. /*FindModuleFileExtensions=*/false,
  4692. validator,
  4693. /*ValidateDiagnosticOptions=*/true);
  4694. }
  4695. ASTReader::ASTReadResult
  4696. ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
  4697. // Enter the submodule block.
  4698. if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
  4699. Error(std::move(Err));
  4700. return Failure;
  4701. }
  4702. ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
  4703. bool First = true;
  4704. Module *CurrentModule = nullptr;
  4705. RecordData Record;
  4706. while (true) {
  4707. Expected<llvm::BitstreamEntry> MaybeEntry =
  4708. F.Stream.advanceSkippingSubblocks();
  4709. if (!MaybeEntry) {
  4710. Error(MaybeEntry.takeError());
  4711. return Failure;
  4712. }
  4713. llvm::BitstreamEntry Entry = MaybeEntry.get();
  4714. switch (Entry.Kind) {
  4715. case llvm::BitstreamEntry::SubBlock: // Handled for us already.
  4716. case llvm::BitstreamEntry::Error:
  4717. Error("malformed block record in AST file");
  4718. return Failure;
  4719. case llvm::BitstreamEntry::EndBlock:
  4720. return Success;
  4721. case llvm::BitstreamEntry::Record:
  4722. // The interesting case.
  4723. break;
  4724. }
  4725. // Read a record.
  4726. StringRef Blob;
  4727. Record.clear();
  4728. Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
  4729. if (!MaybeKind) {
  4730. Error(MaybeKind.takeError());
  4731. return Failure;
  4732. }
  4733. unsigned Kind = MaybeKind.get();
  4734. if ((Kind == SUBMODULE_METADATA) != First) {
  4735. Error("submodule metadata record should be at beginning of block");
  4736. return Failure;
  4737. }
  4738. First = false;
  4739. // Submodule information is only valid if we have a current module.
  4740. // FIXME: Should we error on these cases?
  4741. if (!CurrentModule && Kind != SUBMODULE_METADATA &&
  4742. Kind != SUBMODULE_DEFINITION)
  4743. continue;
  4744. switch (Kind) {
  4745. default: // Default behavior: ignore.
  4746. break;
  4747. case SUBMODULE_DEFINITION: {
  4748. if (Record.size() < 12) {
  4749. Error("malformed module definition");
  4750. return Failure;
  4751. }
  4752. StringRef Name = Blob;
  4753. unsigned Idx = 0;
  4754. SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
  4755. SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
  4756. Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
  4757. bool IsFramework = Record[Idx++];
  4758. bool IsExplicit = Record[Idx++];
  4759. bool IsSystem = Record[Idx++];
  4760. bool IsExternC = Record[Idx++];
  4761. bool InferSubmodules = Record[Idx++];
  4762. bool InferExplicitSubmodules = Record[Idx++];
  4763. bool InferExportWildcard = Record[Idx++];
  4764. bool ConfigMacrosExhaustive = Record[Idx++];
  4765. bool ModuleMapIsPrivate = Record[Idx++];
  4766. Module *ParentModule = nullptr;
  4767. if (Parent)
  4768. ParentModule = getSubmodule(Parent);
  4769. // Retrieve this (sub)module from the module map, creating it if
  4770. // necessary.
  4771. CurrentModule =
  4772. ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
  4773. .first;
  4774. // FIXME: set the definition loc for CurrentModule, or call
  4775. // ModMap.setInferredModuleAllowedBy()
  4776. SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
  4777. if (GlobalIndex >= SubmodulesLoaded.size() ||
  4778. SubmodulesLoaded[GlobalIndex]) {
  4779. Error("too many submodules");
  4780. return Failure;
  4781. }
  4782. if (!ParentModule) {
  4783. if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
  4784. // Don't emit module relocation error if we have -fno-validate-pch
  4785. if (!PP.getPreprocessorOpts().DisablePCHValidation &&
  4786. CurFile != F.File) {
  4787. if (!Diags.isDiagnosticInFlight()) {
  4788. Diag(diag::err_module_file_conflict)
  4789. << CurrentModule->getTopLevelModuleName()
  4790. << CurFile->getName()
  4791. << F.File->getName();
  4792. }
  4793. return Failure;
  4794. }
  4795. }
  4796. CurrentModule->setASTFile(F.File);
  4797. CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
  4798. }
  4799. CurrentModule->Kind = Kind;
  4800. CurrentModule->Signature = F.Signature;
  4801. CurrentModule->IsFromModuleFile = true;
  4802. CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
  4803. CurrentModule->IsExternC = IsExternC;
  4804. CurrentModule->InferSubmodules = InferSubmodules;
  4805. CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
  4806. CurrentModule->InferExportWildcard = InferExportWildcard;
  4807. CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
  4808. CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
  4809. if (DeserializationListener)
  4810. DeserializationListener->ModuleRead(GlobalID, CurrentModule);
  4811. SubmodulesLoaded[GlobalIndex] = CurrentModule;
  4812. // Clear out data that will be replaced by what is in the module file.
  4813. CurrentModule->LinkLibraries.clear();
  4814. CurrentModule->ConfigMacros.clear();
  4815. CurrentModule->UnresolvedConflicts.clear();
  4816. CurrentModule->Conflicts.clear();
  4817. // The module is available unless it's missing a requirement; relevant
  4818. // requirements will be (re-)added by SUBMODULE_REQUIRES records.
  4819. // Missing headers that were present when the module was built do not
  4820. // make it unavailable -- if we got this far, this must be an explicitly
  4821. // imported module file.
  4822. CurrentModule->Requirements.clear();
  4823. CurrentModule->MissingHeaders.clear();
  4824. CurrentModule->IsMissingRequirement =
  4825. ParentModule && ParentModule->IsMissingRequirement;
  4826. CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
  4827. break;
  4828. }
  4829. case SUBMODULE_UMBRELLA_HEADER: {
  4830. std::string Filename = Blob;
  4831. ResolveImportedPath(F, Filename);
  4832. if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
  4833. if (!CurrentModule->getUmbrellaHeader())
  4834. ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
  4835. else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
  4836. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  4837. Error("mismatched umbrella headers in submodule");
  4838. return OutOfDate;
  4839. }
  4840. }
  4841. break;
  4842. }
  4843. case SUBMODULE_HEADER:
  4844. case SUBMODULE_EXCLUDED_HEADER:
  4845. case SUBMODULE_PRIVATE_HEADER:
  4846. // We lazily associate headers with their modules via the HeaderInfo table.
  4847. // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
  4848. // of complete filenames or remove it entirely.
  4849. break;
  4850. case SUBMODULE_TEXTUAL_HEADER:
  4851. case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
  4852. // FIXME: Textual headers are not marked in the HeaderInfo table. Load
  4853. // them here.
  4854. break;
  4855. case SUBMODULE_TOPHEADER:
  4856. CurrentModule->addTopHeaderFilename(Blob);
  4857. break;
  4858. case SUBMODULE_UMBRELLA_DIR: {
  4859. std::string Dirname = Blob;
  4860. ResolveImportedPath(F, Dirname);
  4861. if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
  4862. if (!CurrentModule->getUmbrellaDir())
  4863. ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
  4864. else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
  4865. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  4866. Error("mismatched umbrella directories in submodule");
  4867. return OutOfDate;
  4868. }
  4869. }
  4870. break;
  4871. }
  4872. case SUBMODULE_METADATA: {
  4873. F.BaseSubmoduleID = getTotalNumSubmodules();
  4874. F.LocalNumSubmodules = Record[0];
  4875. unsigned LocalBaseSubmoduleID = Record[1];
  4876. if (F.LocalNumSubmodules > 0) {
  4877. // Introduce the global -> local mapping for submodules within this
  4878. // module.
  4879. GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
  4880. // Introduce the local -> global mapping for submodules within this
  4881. // module.
  4882. F.SubmoduleRemap.insertOrReplace(
  4883. std::make_pair(LocalBaseSubmoduleID,
  4884. F.BaseSubmoduleID - LocalBaseSubmoduleID));
  4885. SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
  4886. }
  4887. break;
  4888. }
  4889. case SUBMODULE_IMPORTS:
  4890. for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
  4891. UnresolvedModuleRef Unresolved;
  4892. Unresolved.File = &F;
  4893. Unresolved.Mod = CurrentModule;
  4894. Unresolved.ID = Record[Idx];
  4895. Unresolved.Kind = UnresolvedModuleRef::Import;
  4896. Unresolved.IsWildcard = false;
  4897. UnresolvedModuleRefs.push_back(Unresolved);
  4898. }
  4899. break;
  4900. case SUBMODULE_EXPORTS:
  4901. for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
  4902. UnresolvedModuleRef Unresolved;
  4903. Unresolved.File = &F;
  4904. Unresolved.Mod = CurrentModule;
  4905. Unresolved.ID = Record[Idx];
  4906. Unresolved.Kind = UnresolvedModuleRef::Export;
  4907. Unresolved.IsWildcard = Record[Idx + 1];
  4908. UnresolvedModuleRefs.push_back(Unresolved);
  4909. }
  4910. // Once we've loaded the set of exports, there's no reason to keep
  4911. // the parsed, unresolved exports around.
  4912. CurrentModule->UnresolvedExports.clear();
  4913. break;
  4914. case SUBMODULE_REQUIRES:
  4915. CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
  4916. PP.getTargetInfo());
  4917. break;
  4918. case SUBMODULE_LINK_LIBRARY:
  4919. ModMap.resolveLinkAsDependencies(CurrentModule);
  4920. CurrentModule->LinkLibraries.push_back(
  4921. Module::LinkLibrary(Blob, Record[0]));
  4922. break;
  4923. case SUBMODULE_CONFIG_MACRO:
  4924. CurrentModule->ConfigMacros.push_back(Blob.str());
  4925. break;
  4926. case SUBMODULE_CONFLICT: {
  4927. UnresolvedModuleRef Unresolved;
  4928. Unresolved.File = &F;
  4929. Unresolved.Mod = CurrentModule;
  4930. Unresolved.ID = Record[0];
  4931. Unresolved.Kind = UnresolvedModuleRef::Conflict;
  4932. Unresolved.IsWildcard = false;
  4933. Unresolved.String = Blob;
  4934. UnresolvedModuleRefs.push_back(Unresolved);
  4935. break;
  4936. }
  4937. case SUBMODULE_INITIALIZERS: {
  4938. if (!ContextObj)
  4939. break;
  4940. SmallVector<uint32_t, 16> Inits;
  4941. for (auto &ID : Record)
  4942. Inits.push_back(getGlobalDeclID(F, ID));
  4943. ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
  4944. break;
  4945. }
  4946. case SUBMODULE_EXPORT_AS:
  4947. CurrentModule->ExportAsModule = Blob.str();
  4948. ModMap.addLinkAsDependency(CurrentModule);
  4949. break;
  4950. }
  4951. }
  4952. }
  4953. /// Parse the record that corresponds to a LangOptions data
  4954. /// structure.
  4955. ///
  4956. /// This routine parses the language options from the AST file and then gives
  4957. /// them to the AST listener if one is set.
  4958. ///
  4959. /// \returns true if the listener deems the file unacceptable, false otherwise.
  4960. bool ASTReader::ParseLanguageOptions(const RecordData &Record,
  4961. bool Complain,
  4962. ASTReaderListener &Listener,
  4963. bool AllowCompatibleDifferences) {
  4964. LangOptions LangOpts;
  4965. unsigned Idx = 0;
  4966. #define LANGOPT(Name, Bits, Default, Description) \
  4967. LangOpts.Name = Record[Idx++];
  4968. #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
  4969. LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
  4970. #include "clang/Basic/LangOptions.def"
  4971. #define SANITIZER(NAME, ID) \
  4972. LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
  4973. #include "clang/Basic/Sanitizers.def"
  4974. for (unsigned N = Record[Idx++]; N; --N)
  4975. LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
  4976. ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
  4977. VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
  4978. LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
  4979. LangOpts.CurrentModule = ReadString(Record, Idx);
  4980. // Comment options.
  4981. for (unsigned N = Record[Idx++]; N; --N) {
  4982. LangOpts.CommentOpts.BlockCommandNames.push_back(
  4983. ReadString(Record, Idx));
  4984. }
  4985. LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
  4986. // OpenMP offloading options.
  4987. for (unsigned N = Record[Idx++]; N; --N) {
  4988. LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
  4989. }
  4990. LangOpts.OMPHostIRFile = ReadString(Record, Idx);
  4991. return Listener.ReadLanguageOptions(LangOpts, Complain,
  4992. AllowCompatibleDifferences);
  4993. }
  4994. bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
  4995. ASTReaderListener &Listener,
  4996. bool AllowCompatibleDifferences) {
  4997. unsigned Idx = 0;
  4998. TargetOptions TargetOpts;
  4999. TargetOpts.Triple = ReadString(Record, Idx);
  5000. TargetOpts.CPU = ReadString(Record, Idx);
  5001. TargetOpts.ABI = ReadString(Record, Idx);
  5002. for (unsigned N = Record[Idx++]; N; --N) {
  5003. TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
  5004. }
  5005. for (unsigned N = Record[Idx++]; N; --N) {
  5006. TargetOpts.Features.push_back(ReadString(Record, Idx));
  5007. }
  5008. return Listener.ReadTargetOptions(TargetOpts, Complain,
  5009. AllowCompatibleDifferences);
  5010. }
  5011. bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
  5012. ASTReaderListener &Listener) {
  5013. IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
  5014. unsigned Idx = 0;
  5015. #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
  5016. #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
  5017. DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
  5018. #include "clang/Basic/DiagnosticOptions.def"
  5019. for (unsigned N = Record[Idx++]; N; --N)
  5020. DiagOpts->Warnings.push_back(ReadString(Record, Idx));
  5021. for (unsigned N = Record[Idx++]; N; --N)
  5022. DiagOpts->Remarks.push_back(ReadString(Record, Idx));
  5023. return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
  5024. }
  5025. bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
  5026. ASTReaderListener &Listener) {
  5027. FileSystemOptions FSOpts;
  5028. unsigned Idx = 0;
  5029. FSOpts.WorkingDir = ReadString(Record, Idx);
  5030. return Listener.ReadFileSystemOptions(FSOpts, Complain);
  5031. }
  5032. bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
  5033. bool Complain,
  5034. ASTReaderListener &Listener) {
  5035. HeaderSearchOptions HSOpts;
  5036. unsigned Idx = 0;
  5037. HSOpts.Sysroot = ReadString(Record, Idx);
  5038. // Include entries.
  5039. for (unsigned N = Record[Idx++]; N; --N) {
  5040. std::string Path = ReadString(Record, Idx);
  5041. frontend::IncludeDirGroup Group
  5042. = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
  5043. bool IsFramework = Record[Idx++];
  5044. bool IgnoreSysRoot = Record[Idx++];
  5045. HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
  5046. IgnoreSysRoot);
  5047. }
  5048. // System header prefixes.
  5049. for (unsigned N = Record[Idx++]; N; --N) {
  5050. std::string Prefix = ReadString(Record, Idx);
  5051. bool IsSystemHeader = Record[Idx++];
  5052. HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
  5053. }
  5054. HSOpts.ResourceDir = ReadString(Record, Idx);
  5055. HSOpts.ModuleCachePath = ReadString(Record, Idx);
  5056. HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
  5057. HSOpts.DisableModuleHash = Record[Idx++];
  5058. HSOpts.ImplicitModuleMaps = Record[Idx++];
  5059. HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
  5060. HSOpts.UseBuiltinIncludes = Record[Idx++];
  5061. HSOpts.UseStandardSystemIncludes = Record[Idx++];
  5062. HSOpts.UseStandardCXXIncludes = Record[Idx++];
  5063. HSOpts.UseLibcxx = Record[Idx++];
  5064. std::string SpecificModuleCachePath = ReadString(Record, Idx);
  5065. return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
  5066. Complain);
  5067. }
  5068. bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
  5069. bool Complain,
  5070. ASTReaderListener &Listener,
  5071. std::string &SuggestedPredefines) {
  5072. PreprocessorOptions PPOpts;
  5073. unsigned Idx = 0;
  5074. // Macro definitions/undefs
  5075. for (unsigned N = Record[Idx++]; N; --N) {
  5076. std::string Macro = ReadString(Record, Idx);
  5077. bool IsUndef = Record[Idx++];
  5078. PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
  5079. }
  5080. // Includes
  5081. for (unsigned N = Record[Idx++]; N; --N) {
  5082. PPOpts.Includes.push_back(ReadString(Record, Idx));
  5083. }
  5084. // Macro Includes
  5085. for (unsigned N = Record[Idx++]; N; --N) {
  5086. PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
  5087. }
  5088. PPOpts.UsePredefines = Record[Idx++];
  5089. PPOpts.DetailedRecord = Record[Idx++];
  5090. PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
  5091. PPOpts.ObjCXXARCStandardLibrary =
  5092. static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
  5093. SuggestedPredefines.clear();
  5094. return Listener.ReadPreprocessorOptions(PPOpts, Complain,
  5095. SuggestedPredefines);
  5096. }
  5097. std::pair<ModuleFile *, unsigned>
  5098. ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
  5099. GlobalPreprocessedEntityMapType::iterator
  5100. I = GlobalPreprocessedEntityMap.find(GlobalIndex);
  5101. assert(I != GlobalPreprocessedEntityMap.end() &&
  5102. "Corrupted global preprocessed entity map");
  5103. ModuleFile *M = I->second;
  5104. unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
  5105. return std::make_pair(M, LocalIndex);
  5106. }
  5107. llvm::iterator_range<PreprocessingRecord::iterator>
  5108. ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
  5109. if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
  5110. return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
  5111. Mod.NumPreprocessedEntities);
  5112. return llvm::make_range(PreprocessingRecord::iterator(),
  5113. PreprocessingRecord::iterator());
  5114. }
  5115. llvm::iterator_range<ASTReader::ModuleDeclIterator>
  5116. ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
  5117. return llvm::make_range(
  5118. ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
  5119. ModuleDeclIterator(this, &Mod,
  5120. Mod.FileSortedDecls + Mod.NumFileSortedDecls));
  5121. }
  5122. SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
  5123. auto I = GlobalSkippedRangeMap.find(GlobalIndex);
  5124. assert(I != GlobalSkippedRangeMap.end() &&
  5125. "Corrupted global skipped range map");
  5126. ModuleFile *M = I->second;
  5127. unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
  5128. assert(LocalIndex < M->NumPreprocessedSkippedRanges);
  5129. PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
  5130. SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
  5131. TranslateSourceLocation(*M, RawRange.getEnd()));
  5132. assert(Range.isValid());
  5133. return Range;
  5134. }
  5135. PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
  5136. PreprocessedEntityID PPID = Index+1;
  5137. std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
  5138. ModuleFile &M = *PPInfo.first;
  5139. unsigned LocalIndex = PPInfo.second;
  5140. const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
  5141. if (!PP.getPreprocessingRecord()) {
  5142. Error("no preprocessing record");
  5143. return nullptr;
  5144. }
  5145. SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
  5146. if (llvm::Error Err =
  5147. M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset)) {
  5148. Error(std::move(Err));
  5149. return nullptr;
  5150. }
  5151. Expected<llvm::BitstreamEntry> MaybeEntry =
  5152. M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
  5153. if (!MaybeEntry) {
  5154. Error(MaybeEntry.takeError());
  5155. return nullptr;
  5156. }
  5157. llvm::BitstreamEntry Entry = MaybeEntry.get();
  5158. if (Entry.Kind != llvm::BitstreamEntry::Record)
  5159. return nullptr;
  5160. // Read the record.
  5161. SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
  5162. TranslateSourceLocation(M, PPOffs.getEnd()));
  5163. PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
  5164. StringRef Blob;
  5165. RecordData Record;
  5166. Expected<unsigned> MaybeRecType =
  5167. M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
  5168. if (!MaybeRecType) {
  5169. Error(MaybeRecType.takeError());
  5170. return nullptr;
  5171. }
  5172. switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
  5173. case PPD_MACRO_EXPANSION: {
  5174. bool isBuiltin = Record[0];
  5175. IdentifierInfo *Name = nullptr;
  5176. MacroDefinitionRecord *Def = nullptr;
  5177. if (isBuiltin)
  5178. Name = getLocalIdentifier(M, Record[1]);
  5179. else {
  5180. PreprocessedEntityID GlobalID =
  5181. getGlobalPreprocessedEntityID(M, Record[1]);
  5182. Def = cast<MacroDefinitionRecord>(
  5183. PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
  5184. }
  5185. MacroExpansion *ME;
  5186. if (isBuiltin)
  5187. ME = new (PPRec) MacroExpansion(Name, Range);
  5188. else
  5189. ME = new (PPRec) MacroExpansion(Def, Range);
  5190. return ME;
  5191. }
  5192. case PPD_MACRO_DEFINITION: {
  5193. // Decode the identifier info and then check again; if the macro is
  5194. // still defined and associated with the identifier,
  5195. IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
  5196. MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
  5197. if (DeserializationListener)
  5198. DeserializationListener->MacroDefinitionRead(PPID, MD);
  5199. return MD;
  5200. }
  5201. case PPD_INCLUSION_DIRECTIVE: {
  5202. const char *FullFileNameStart = Blob.data() + Record[0];
  5203. StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
  5204. const FileEntry *File = nullptr;
  5205. if (!FullFileName.empty())
  5206. if (auto FE = PP.getFileManager().getFile(FullFileName))
  5207. File = *FE;
  5208. // FIXME: Stable encoding
  5209. InclusionDirective::InclusionKind Kind
  5210. = static_cast<InclusionDirective::InclusionKind>(Record[2]);
  5211. InclusionDirective *ID
  5212. = new (PPRec) InclusionDirective(PPRec, Kind,
  5213. StringRef(Blob.data(), Record[0]),
  5214. Record[1], Record[3],
  5215. File,
  5216. Range);
  5217. return ID;
  5218. }
  5219. }
  5220. llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
  5221. }
  5222. /// Find the next module that contains entities and return the ID
  5223. /// of the first entry.
  5224. ///
  5225. /// \param SLocMapI points at a chunk of a module that contains no
  5226. /// preprocessed entities or the entities it contains are not the ones we are
  5227. /// looking for.
  5228. PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
  5229. GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
  5230. ++SLocMapI;
  5231. for (GlobalSLocOffsetMapType::const_iterator
  5232. EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
  5233. ModuleFile &M = *SLocMapI->second;
  5234. if (M.NumPreprocessedEntities)
  5235. return M.BasePreprocessedEntityID;
  5236. }
  5237. return getTotalNumPreprocessedEntities();
  5238. }
  5239. namespace {
  5240. struct PPEntityComp {
  5241. const ASTReader &Reader;
  5242. ModuleFile &M;
  5243. PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
  5244. bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
  5245. SourceLocation LHS = getLoc(L);
  5246. SourceLocation RHS = getLoc(R);
  5247. return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
  5248. }
  5249. bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
  5250. SourceLocation LHS = getLoc(L);
  5251. return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
  5252. }
  5253. bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
  5254. SourceLocation RHS = getLoc(R);
  5255. return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
  5256. }
  5257. SourceLocation getLoc(const PPEntityOffset &PPE) const {
  5258. return Reader.TranslateSourceLocation(M, PPE.getBegin());
  5259. }
  5260. };
  5261. } // namespace
  5262. PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
  5263. bool EndsAfter) const {
  5264. if (SourceMgr.isLocalSourceLocation(Loc))
  5265. return getTotalNumPreprocessedEntities();
  5266. GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
  5267. SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
  5268. assert(SLocMapI != GlobalSLocOffsetMap.end() &&
  5269. "Corrupted global sloc offset map");
  5270. if (SLocMapI->second->NumPreprocessedEntities == 0)
  5271. return findNextPreprocessedEntity(SLocMapI);
  5272. ModuleFile &M = *SLocMapI->second;
  5273. using pp_iterator = const PPEntityOffset *;
  5274. pp_iterator pp_begin = M.PreprocessedEntityOffsets;
  5275. pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
  5276. size_t Count = M.NumPreprocessedEntities;
  5277. size_t Half;
  5278. pp_iterator First = pp_begin;
  5279. pp_iterator PPI;
  5280. if (EndsAfter) {
  5281. PPI = std::upper_bound(pp_begin, pp_end, Loc,
  5282. PPEntityComp(*this, M));
  5283. } else {
  5284. // Do a binary search manually instead of using std::lower_bound because
  5285. // The end locations of entities may be unordered (when a macro expansion
  5286. // is inside another macro argument), but for this case it is not important
  5287. // whether we get the first macro expansion or its containing macro.
  5288. while (Count > 0) {
  5289. Half = Count / 2;
  5290. PPI = First;
  5291. std::advance(PPI, Half);
  5292. if (SourceMgr.isBeforeInTranslationUnit(
  5293. TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
  5294. First = PPI;
  5295. ++First;
  5296. Count = Count - Half - 1;
  5297. } else
  5298. Count = Half;
  5299. }
  5300. }
  5301. if (PPI == pp_end)
  5302. return findNextPreprocessedEntity(SLocMapI);
  5303. return M.BasePreprocessedEntityID + (PPI - pp_begin);
  5304. }
  5305. /// Returns a pair of [Begin, End) indices of preallocated
  5306. /// preprocessed entities that \arg Range encompasses.
  5307. std::pair<unsigned, unsigned>
  5308. ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
  5309. if (Range.isInvalid())
  5310. return std::make_pair(0,0);
  5311. assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
  5312. PreprocessedEntityID BeginID =
  5313. findPreprocessedEntity(Range.getBegin(), false);
  5314. PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
  5315. return std::make_pair(BeginID, EndID);
  5316. }
  5317. /// Optionally returns true or false if the preallocated preprocessed
  5318. /// entity with index \arg Index came from file \arg FID.
  5319. Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
  5320. FileID FID) {
  5321. if (FID.isInvalid())
  5322. return false;
  5323. std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
  5324. ModuleFile &M = *PPInfo.first;
  5325. unsigned LocalIndex = PPInfo.second;
  5326. const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
  5327. SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
  5328. if (Loc.isInvalid())
  5329. return false;
  5330. if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
  5331. return true;
  5332. else
  5333. return false;
  5334. }
  5335. namespace {
  5336. /// Visitor used to search for information about a header file.
  5337. class HeaderFileInfoVisitor {
  5338. const FileEntry *FE;
  5339. Optional<HeaderFileInfo> HFI;
  5340. public:
  5341. explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
  5342. bool operator()(ModuleFile &M) {
  5343. HeaderFileInfoLookupTable *Table
  5344. = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
  5345. if (!Table)
  5346. return false;
  5347. // Look in the on-disk hash table for an entry for this file name.
  5348. HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
  5349. if (Pos == Table->end())
  5350. return false;
  5351. HFI = *Pos;
  5352. return true;
  5353. }
  5354. Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
  5355. };
  5356. } // namespace
  5357. HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
  5358. HeaderFileInfoVisitor Visitor(FE);
  5359. ModuleMgr.visit(Visitor);
  5360. if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
  5361. return *HFI;
  5362. return HeaderFileInfo();
  5363. }
  5364. void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
  5365. using DiagState = DiagnosticsEngine::DiagState;
  5366. SmallVector<DiagState *, 32> DiagStates;
  5367. for (ModuleFile &F : ModuleMgr) {
  5368. unsigned Idx = 0;
  5369. auto &Record = F.PragmaDiagMappings;
  5370. if (Record.empty())
  5371. continue;
  5372. DiagStates.clear();
  5373. auto ReadDiagState =
  5374. [&](const DiagState &BasedOn, SourceLocation Loc,
  5375. bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
  5376. unsigned BackrefID = Record[Idx++];
  5377. if (BackrefID != 0)
  5378. return DiagStates[BackrefID - 1];
  5379. // A new DiagState was created here.
  5380. Diag.DiagStates.push_back(BasedOn);
  5381. DiagState *NewState = &Diag.DiagStates.back();
  5382. DiagStates.push_back(NewState);
  5383. unsigned Size = Record[Idx++];
  5384. assert(Idx + Size * 2 <= Record.size() &&
  5385. "Invalid data, not enough diag/map pairs");
  5386. while (Size--) {
  5387. unsigned DiagID = Record[Idx++];
  5388. DiagnosticMapping NewMapping =
  5389. DiagnosticMapping::deserialize(Record[Idx++]);
  5390. if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
  5391. continue;
  5392. DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
  5393. // If this mapping was specified as a warning but the severity was
  5394. // upgraded due to diagnostic settings, simulate the current diagnostic
  5395. // settings (and use a warning).
  5396. if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
  5397. NewMapping.setSeverity(diag::Severity::Warning);
  5398. NewMapping.setUpgradedFromWarning(false);
  5399. }
  5400. Mapping = NewMapping;
  5401. }
  5402. return NewState;
  5403. };
  5404. // Read the first state.
  5405. DiagState *FirstState;
  5406. if (F.Kind == MK_ImplicitModule) {
  5407. // Implicitly-built modules are reused with different diagnostic
  5408. // settings. Use the initial diagnostic state from Diag to simulate this
  5409. // compilation's diagnostic settings.
  5410. FirstState = Diag.DiagStatesByLoc.FirstDiagState;
  5411. DiagStates.push_back(FirstState);
  5412. // Skip the initial diagnostic state from the serialized module.
  5413. assert(Record[1] == 0 &&
  5414. "Invalid data, unexpected backref in initial state");
  5415. Idx = 3 + Record[2] * 2;
  5416. assert(Idx < Record.size() &&
  5417. "Invalid data, not enough state change pairs in initial state");
  5418. } else if (F.isModule()) {
  5419. // For an explicit module, preserve the flags from the module build
  5420. // command line (-w, -Weverything, -Werror, ...) along with any explicit
  5421. // -Wblah flags.
  5422. unsigned Flags = Record[Idx++];
  5423. DiagState Initial;
  5424. Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
  5425. Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
  5426. Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
  5427. Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
  5428. Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
  5429. Initial.ExtBehavior = (diag::Severity)Flags;
  5430. FirstState = ReadDiagState(Initial, SourceLocation(), true);
  5431. assert(F.OriginalSourceFileID.isValid());
  5432. // Set up the root buffer of the module to start with the initial
  5433. // diagnostic state of the module itself, to cover files that contain no
  5434. // explicit transitions (for which we did not serialize anything).
  5435. Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
  5436. .StateTransitions.push_back({FirstState, 0});
  5437. } else {
  5438. // For prefix ASTs, start with whatever the user configured on the
  5439. // command line.
  5440. Idx++; // Skip flags.
  5441. FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
  5442. SourceLocation(), false);
  5443. }
  5444. // Read the state transitions.
  5445. unsigned NumLocations = Record[Idx++];
  5446. while (NumLocations--) {
  5447. assert(Idx < Record.size() &&
  5448. "Invalid data, missing pragma diagnostic states");
  5449. SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
  5450. auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
  5451. assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
  5452. assert(IDAndOffset.second == 0 && "not a start location for a FileID");
  5453. unsigned Transitions = Record[Idx++];
  5454. // Note that we don't need to set up Parent/ParentOffset here, because
  5455. // we won't be changing the diagnostic state within imported FileIDs
  5456. // (other than perhaps appending to the main source file, which has no
  5457. // parent).
  5458. auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
  5459. F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
  5460. for (unsigned I = 0; I != Transitions; ++I) {
  5461. unsigned Offset = Record[Idx++];
  5462. auto *State =
  5463. ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
  5464. F.StateTransitions.push_back({State, Offset});
  5465. }
  5466. }
  5467. // Read the final state.
  5468. assert(Idx < Record.size() &&
  5469. "Invalid data, missing final pragma diagnostic state");
  5470. SourceLocation CurStateLoc =
  5471. ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
  5472. auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
  5473. if (!F.isModule()) {
  5474. Diag.DiagStatesByLoc.CurDiagState = CurState;
  5475. Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
  5476. // Preserve the property that the imaginary root file describes the
  5477. // current state.
  5478. FileID NullFile;
  5479. auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
  5480. if (T.empty())
  5481. T.push_back({CurState, 0});
  5482. else
  5483. T[0].State = CurState;
  5484. }
  5485. // Don't try to read these mappings again.
  5486. Record.clear();
  5487. }
  5488. }
  5489. /// Get the correct cursor and offset for loading a type.
  5490. ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
  5491. GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
  5492. assert(I != GlobalTypeMap.end() && "Corrupted global type map");
  5493. ModuleFile *M = I->second;
  5494. return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
  5495. }
  5496. /// Read and return the type with the given index..
  5497. ///
  5498. /// The index is the type ID, shifted and minus the number of predefs. This
  5499. /// routine actually reads the record corresponding to the type at the given
  5500. /// location. It is a helper routine for GetType, which deals with reading type
  5501. /// IDs.
  5502. QualType ASTReader::readTypeRecord(unsigned Index) {
  5503. assert(ContextObj && "reading type with no AST context");
  5504. ASTContext &Context = *ContextObj;
  5505. RecordLocation Loc = TypeCursorForIndex(Index);
  5506. BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
  5507. // Keep track of where we are in the stream, then jump back there
  5508. // after reading this type.
  5509. SavedStreamPosition SavedPosition(DeclsCursor);
  5510. ReadingKindTracker ReadingKind(Read_Type, *this);
  5511. // Note that we are loading a type record.
  5512. Deserializing AType(this);
  5513. unsigned Idx = 0;
  5514. if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
  5515. Error(std::move(Err));
  5516. return QualType();
  5517. }
  5518. RecordData Record;
  5519. Expected<unsigned> MaybeCode = DeclsCursor.ReadCode();
  5520. if (!MaybeCode) {
  5521. Error(MaybeCode.takeError());
  5522. return QualType();
  5523. }
  5524. unsigned Code = MaybeCode.get();
  5525. Expected<unsigned> MaybeTypeCode = DeclsCursor.readRecord(Code, Record);
  5526. if (!MaybeTypeCode) {
  5527. Error(MaybeTypeCode.takeError());
  5528. return QualType();
  5529. }
  5530. switch ((TypeCode)MaybeTypeCode.get()) {
  5531. case TYPE_EXT_QUAL: {
  5532. if (Record.size() != 2) {
  5533. Error("Incorrect encoding of extended qualifier type");
  5534. return QualType();
  5535. }
  5536. QualType Base = readType(*Loc.F, Record, Idx);
  5537. Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
  5538. return Context.getQualifiedType(Base, Quals);
  5539. }
  5540. case TYPE_COMPLEX: {
  5541. if (Record.size() != 1) {
  5542. Error("Incorrect encoding of complex type");
  5543. return QualType();
  5544. }
  5545. QualType ElemType = readType(*Loc.F, Record, Idx);
  5546. return Context.getComplexType(ElemType);
  5547. }
  5548. case TYPE_POINTER: {
  5549. if (Record.size() != 1) {
  5550. Error("Incorrect encoding of pointer type");
  5551. return QualType();
  5552. }
  5553. QualType PointeeType = readType(*Loc.F, Record, Idx);
  5554. return Context.getPointerType(PointeeType);
  5555. }
  5556. case TYPE_DECAYED: {
  5557. if (Record.size() != 1) {
  5558. Error("Incorrect encoding of decayed type");
  5559. return QualType();
  5560. }
  5561. QualType OriginalType = readType(*Loc.F, Record, Idx);
  5562. QualType DT = Context.getAdjustedParameterType(OriginalType);
  5563. if (!isa<DecayedType>(DT))
  5564. Error("Decayed type does not decay");
  5565. return DT;
  5566. }
  5567. case TYPE_ADJUSTED: {
  5568. if (Record.size() != 2) {
  5569. Error("Incorrect encoding of adjusted type");
  5570. return QualType();
  5571. }
  5572. QualType OriginalTy = readType(*Loc.F, Record, Idx);
  5573. QualType AdjustedTy = readType(*Loc.F, Record, Idx);
  5574. return Context.getAdjustedType(OriginalTy, AdjustedTy);
  5575. }
  5576. case TYPE_BLOCK_POINTER: {
  5577. if (Record.size() != 1) {
  5578. Error("Incorrect encoding of block pointer type");
  5579. return QualType();
  5580. }
  5581. QualType PointeeType = readType(*Loc.F, Record, Idx);
  5582. return Context.getBlockPointerType(PointeeType);
  5583. }
  5584. case TYPE_LVALUE_REFERENCE: {
  5585. if (Record.size() != 2) {
  5586. Error("Incorrect encoding of lvalue reference type");
  5587. return QualType();
  5588. }
  5589. QualType PointeeType = readType(*Loc.F, Record, Idx);
  5590. return Context.getLValueReferenceType(PointeeType, Record[1]);
  5591. }
  5592. case TYPE_RVALUE_REFERENCE: {
  5593. if (Record.size() != 1) {
  5594. Error("Incorrect encoding of rvalue reference type");
  5595. return QualType();
  5596. }
  5597. QualType PointeeType = readType(*Loc.F, Record, Idx);
  5598. return Context.getRValueReferenceType(PointeeType);
  5599. }
  5600. case TYPE_MEMBER_POINTER: {
  5601. if (Record.size() != 2) {
  5602. Error("Incorrect encoding of member pointer type");
  5603. return QualType();
  5604. }
  5605. QualType PointeeType = readType(*Loc.F, Record, Idx);
  5606. QualType ClassType = readType(*Loc.F, Record, Idx);
  5607. if (PointeeType.isNull() || ClassType.isNull())
  5608. return QualType();
  5609. return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
  5610. }
  5611. case TYPE_CONSTANT_ARRAY: {
  5612. QualType ElementType = readType(*Loc.F, Record, Idx);
  5613. ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
  5614. unsigned IndexTypeQuals = Record[2];
  5615. unsigned Idx = 3;
  5616. llvm::APInt Size = ReadAPInt(Record, Idx);
  5617. Expr *SizeExpr = ReadExpr(*Loc.F);
  5618. return Context.getConstantArrayType(ElementType, Size, SizeExpr,
  5619. ASM, IndexTypeQuals);
  5620. }
  5621. case TYPE_INCOMPLETE_ARRAY: {
  5622. QualType ElementType = readType(*Loc.F, Record, Idx);
  5623. ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
  5624. unsigned IndexTypeQuals = Record[2];
  5625. return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
  5626. }
  5627. case TYPE_VARIABLE_ARRAY: {
  5628. QualType ElementType = readType(*Loc.F, Record, Idx);
  5629. ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
  5630. unsigned IndexTypeQuals = Record[2];
  5631. SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
  5632. SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
  5633. return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
  5634. ASM, IndexTypeQuals,
  5635. SourceRange(LBLoc, RBLoc));
  5636. }
  5637. case TYPE_VECTOR: {
  5638. if (Record.size() != 3) {
  5639. Error("incorrect encoding of vector type in AST file");
  5640. return QualType();
  5641. }
  5642. QualType ElementType = readType(*Loc.F, Record, Idx);
  5643. unsigned NumElements = Record[1];
  5644. unsigned VecKind = Record[2];
  5645. return Context.getVectorType(ElementType, NumElements,
  5646. (VectorType::VectorKind)VecKind);
  5647. }
  5648. case TYPE_EXT_VECTOR: {
  5649. if (Record.size() != 3) {
  5650. Error("incorrect encoding of extended vector type in AST file");
  5651. return QualType();
  5652. }
  5653. QualType ElementType = readType(*Loc.F, Record, Idx);
  5654. unsigned NumElements = Record[1];
  5655. return Context.getExtVectorType(ElementType, NumElements);
  5656. }
  5657. case TYPE_FUNCTION_NO_PROTO: {
  5658. if (Record.size() != 8) {
  5659. Error("incorrect encoding of no-proto function type");
  5660. return QualType();
  5661. }
  5662. QualType ResultType = readType(*Loc.F, Record, Idx);
  5663. FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
  5664. (CallingConv)Record[4], Record[5], Record[6],
  5665. Record[7]);
  5666. return Context.getFunctionNoProtoType(ResultType, Info);
  5667. }
  5668. case TYPE_FUNCTION_PROTO: {
  5669. QualType ResultType = readType(*Loc.F, Record, Idx);
  5670. FunctionProtoType::ExtProtoInfo EPI;
  5671. EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
  5672. /*hasregparm*/ Record[2],
  5673. /*regparm*/ Record[3],
  5674. static_cast<CallingConv>(Record[4]),
  5675. /*produces*/ Record[5],
  5676. /*nocallersavedregs*/ Record[6],
  5677. /*nocfcheck*/ Record[7]);
  5678. unsigned Idx = 8;
  5679. EPI.Variadic = Record[Idx++];
  5680. EPI.HasTrailingReturn = Record[Idx++];
  5681. EPI.TypeQuals = Qualifiers::fromOpaqueValue(Record[Idx++]);
  5682. EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
  5683. SmallVector<QualType, 8> ExceptionStorage;
  5684. readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
  5685. unsigned NumParams = Record[Idx++];
  5686. SmallVector<QualType, 16> ParamTypes;
  5687. for (unsigned I = 0; I != NumParams; ++I)
  5688. ParamTypes.push_back(readType(*Loc.F, Record, Idx));
  5689. SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
  5690. if (Idx != Record.size()) {
  5691. for (unsigned I = 0; I != NumParams; ++I)
  5692. ExtParameterInfos.push_back(
  5693. FunctionProtoType::ExtParameterInfo
  5694. ::getFromOpaqueValue(Record[Idx++]));
  5695. EPI.ExtParameterInfos = ExtParameterInfos.data();
  5696. }
  5697. assert(Idx == Record.size());
  5698. return Context.getFunctionType(ResultType, ParamTypes, EPI);
  5699. }
  5700. case TYPE_UNRESOLVED_USING: {
  5701. unsigned Idx = 0;
  5702. return Context.getTypeDeclType(
  5703. ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
  5704. }
  5705. case TYPE_TYPEDEF: {
  5706. if (Record.size() != 2) {
  5707. Error("incorrect encoding of typedef type");
  5708. return QualType();
  5709. }
  5710. unsigned Idx = 0;
  5711. TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
  5712. QualType Canonical = readType(*Loc.F, Record, Idx);
  5713. if (!Canonical.isNull())
  5714. Canonical = Context.getCanonicalType(Canonical);
  5715. return Context.getTypedefType(Decl, Canonical);
  5716. }
  5717. case TYPE_TYPEOF_EXPR:
  5718. return Context.getTypeOfExprType(ReadExpr(*Loc.F));
  5719. case TYPE_TYPEOF: {
  5720. if (Record.size() != 1) {
  5721. Error("incorrect encoding of typeof(type) in AST file");
  5722. return QualType();
  5723. }
  5724. QualType UnderlyingType = readType(*Loc.F, Record, Idx);
  5725. return Context.getTypeOfType(UnderlyingType);
  5726. }
  5727. case TYPE_DECLTYPE: {
  5728. QualType UnderlyingType = readType(*Loc.F, Record, Idx);
  5729. return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
  5730. }
  5731. case TYPE_UNARY_TRANSFORM: {
  5732. QualType BaseType = readType(*Loc.F, Record, Idx);
  5733. QualType UnderlyingType = readType(*Loc.F, Record, Idx);
  5734. UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
  5735. return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
  5736. }
  5737. case TYPE_AUTO: {
  5738. QualType Deduced = readType(*Loc.F, Record, Idx);
  5739. AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
  5740. bool IsDependent = false, IsPack = false;
  5741. if (Deduced.isNull()) {
  5742. IsDependent = Record[Idx] > 0;
  5743. IsPack = Record[Idx] > 1;
  5744. ++Idx;
  5745. }
  5746. return Context.getAutoType(Deduced, Keyword, IsDependent, IsPack);
  5747. }
  5748. case TYPE_DEDUCED_TEMPLATE_SPECIALIZATION: {
  5749. TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
  5750. QualType Deduced = readType(*Loc.F, Record, Idx);
  5751. bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
  5752. return Context.getDeducedTemplateSpecializationType(Name, Deduced,
  5753. IsDependent);
  5754. }
  5755. case TYPE_RECORD: {
  5756. if (Record.size() != 2) {
  5757. Error("incorrect encoding of record type");
  5758. return QualType();
  5759. }
  5760. unsigned Idx = 0;
  5761. bool IsDependent = Record[Idx++];
  5762. RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
  5763. RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
  5764. QualType T = Context.getRecordType(RD);
  5765. const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
  5766. return T;
  5767. }
  5768. case TYPE_ENUM: {
  5769. if (Record.size() != 2) {
  5770. Error("incorrect encoding of enum type");
  5771. return QualType();
  5772. }
  5773. unsigned Idx = 0;
  5774. bool IsDependent = Record[Idx++];
  5775. QualType T
  5776. = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
  5777. const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
  5778. return T;
  5779. }
  5780. case TYPE_ATTRIBUTED: {
  5781. if (Record.size() != 3) {
  5782. Error("incorrect encoding of attributed type");
  5783. return QualType();
  5784. }
  5785. QualType modifiedType = readType(*Loc.F, Record, Idx);
  5786. QualType equivalentType = readType(*Loc.F, Record, Idx);
  5787. AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
  5788. return Context.getAttributedType(kind, modifiedType, equivalentType);
  5789. }
  5790. case TYPE_PAREN: {
  5791. if (Record.size() != 1) {
  5792. Error("incorrect encoding of paren type");
  5793. return QualType();
  5794. }
  5795. QualType InnerType = readType(*Loc.F, Record, Idx);
  5796. return Context.getParenType(InnerType);
  5797. }
  5798. case TYPE_MACRO_QUALIFIED: {
  5799. if (Record.size() != 2) {
  5800. Error("incorrect encoding of macro defined type");
  5801. return QualType();
  5802. }
  5803. QualType UnderlyingTy = readType(*Loc.F, Record, Idx);
  5804. IdentifierInfo *MacroII = GetIdentifierInfo(*Loc.F, Record, Idx);
  5805. return Context.getMacroQualifiedType(UnderlyingTy, MacroII);
  5806. }
  5807. case TYPE_PACK_EXPANSION: {
  5808. if (Record.size() != 2) {
  5809. Error("incorrect encoding of pack expansion type");
  5810. return QualType();
  5811. }
  5812. QualType Pattern = readType(*Loc.F, Record, Idx);
  5813. if (Pattern.isNull())
  5814. return QualType();
  5815. Optional<unsigned> NumExpansions;
  5816. if (Record[1])
  5817. NumExpansions = Record[1] - 1;
  5818. return Context.getPackExpansionType(Pattern, NumExpansions);
  5819. }
  5820. case TYPE_ELABORATED: {
  5821. unsigned Idx = 0;
  5822. ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
  5823. NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
  5824. QualType NamedType = readType(*Loc.F, Record, Idx);
  5825. TagDecl *OwnedTagDecl = ReadDeclAs<TagDecl>(*Loc.F, Record, Idx);
  5826. return Context.getElaboratedType(Keyword, NNS, NamedType, OwnedTagDecl);
  5827. }
  5828. case TYPE_OBJC_INTERFACE: {
  5829. unsigned Idx = 0;
  5830. ObjCInterfaceDecl *ItfD
  5831. = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
  5832. return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
  5833. }
  5834. case TYPE_OBJC_TYPE_PARAM: {
  5835. unsigned Idx = 0;
  5836. ObjCTypeParamDecl *Decl
  5837. = ReadDeclAs<ObjCTypeParamDecl>(*Loc.F, Record, Idx);
  5838. unsigned NumProtos = Record[Idx++];
  5839. SmallVector<ObjCProtocolDecl*, 4> Protos;
  5840. for (unsigned I = 0; I != NumProtos; ++I)
  5841. Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
  5842. return Context.getObjCTypeParamType(Decl, Protos);
  5843. }
  5844. case TYPE_OBJC_OBJECT: {
  5845. unsigned Idx = 0;
  5846. QualType Base = readType(*Loc.F, Record, Idx);
  5847. unsigned NumTypeArgs = Record[Idx++];
  5848. SmallVector<QualType, 4> TypeArgs;
  5849. for (unsigned I = 0; I != NumTypeArgs; ++I)
  5850. TypeArgs.push_back(readType(*Loc.F, Record, Idx));
  5851. unsigned NumProtos = Record[Idx++];
  5852. SmallVector<ObjCProtocolDecl*, 4> Protos;
  5853. for (unsigned I = 0; I != NumProtos; ++I)
  5854. Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
  5855. bool IsKindOf = Record[Idx++];
  5856. return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
  5857. }
  5858. case TYPE_OBJC_OBJECT_POINTER: {
  5859. unsigned Idx = 0;
  5860. QualType Pointee = readType(*Loc.F, Record, Idx);
  5861. return Context.getObjCObjectPointerType(Pointee);
  5862. }
  5863. case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
  5864. unsigned Idx = 0;
  5865. QualType Parm = readType(*Loc.F, Record, Idx);
  5866. QualType Replacement = readType(*Loc.F, Record, Idx);
  5867. return Context.getSubstTemplateTypeParmType(
  5868. cast<TemplateTypeParmType>(Parm),
  5869. Context.getCanonicalType(Replacement));
  5870. }
  5871. case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
  5872. unsigned Idx = 0;
  5873. QualType Parm = readType(*Loc.F, Record, Idx);
  5874. TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
  5875. return Context.getSubstTemplateTypeParmPackType(
  5876. cast<TemplateTypeParmType>(Parm),
  5877. ArgPack);
  5878. }
  5879. case TYPE_INJECTED_CLASS_NAME: {
  5880. CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
  5881. QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
  5882. // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
  5883. // for AST reading, too much interdependencies.
  5884. const Type *T = nullptr;
  5885. for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
  5886. if (const Type *Existing = DI->getTypeForDecl()) {
  5887. T = Existing;
  5888. break;
  5889. }
  5890. }
  5891. if (!T) {
  5892. T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
  5893. for (auto *DI = D; DI; DI = DI->getPreviousDecl())
  5894. DI->setTypeForDecl(T);
  5895. }
  5896. return QualType(T, 0);
  5897. }
  5898. case TYPE_TEMPLATE_TYPE_PARM: {
  5899. unsigned Idx = 0;
  5900. unsigned Depth = Record[Idx++];
  5901. unsigned Index = Record[Idx++];
  5902. bool Pack = Record[Idx++];
  5903. TemplateTypeParmDecl *D
  5904. = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
  5905. return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
  5906. }
  5907. case TYPE_DEPENDENT_NAME: {
  5908. unsigned Idx = 0;
  5909. ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
  5910. NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
  5911. const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
  5912. QualType Canon = readType(*Loc.F, Record, Idx);
  5913. if (!Canon.isNull())
  5914. Canon = Context.getCanonicalType(Canon);
  5915. return Context.getDependentNameType(Keyword, NNS, Name, Canon);
  5916. }
  5917. case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
  5918. unsigned Idx = 0;
  5919. ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
  5920. NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
  5921. const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
  5922. unsigned NumArgs = Record[Idx++];
  5923. SmallVector<TemplateArgument, 8> Args;
  5924. Args.reserve(NumArgs);
  5925. while (NumArgs--)
  5926. Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
  5927. return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
  5928. Args);
  5929. }
  5930. case TYPE_DEPENDENT_SIZED_ARRAY: {
  5931. unsigned Idx = 0;
  5932. // ArrayType
  5933. QualType ElementType = readType(*Loc.F, Record, Idx);
  5934. ArrayType::ArraySizeModifier ASM
  5935. = (ArrayType::ArraySizeModifier)Record[Idx++];
  5936. unsigned IndexTypeQuals = Record[Idx++];
  5937. // DependentSizedArrayType
  5938. Expr *NumElts = ReadExpr(*Loc.F);
  5939. SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
  5940. return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
  5941. IndexTypeQuals, Brackets);
  5942. }
  5943. case TYPE_TEMPLATE_SPECIALIZATION: {
  5944. unsigned Idx = 0;
  5945. bool IsDependent = Record[Idx++];
  5946. TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
  5947. SmallVector<TemplateArgument, 8> Args;
  5948. ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
  5949. QualType Underlying = readType(*Loc.F, Record, Idx);
  5950. QualType T;
  5951. if (Underlying.isNull())
  5952. T = Context.getCanonicalTemplateSpecializationType(Name, Args);
  5953. else
  5954. T = Context.getTemplateSpecializationType(Name, Args, Underlying);
  5955. const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
  5956. return T;
  5957. }
  5958. case TYPE_ATOMIC: {
  5959. if (Record.size() != 1) {
  5960. Error("Incorrect encoding of atomic type");
  5961. return QualType();
  5962. }
  5963. QualType ValueType = readType(*Loc.F, Record, Idx);
  5964. return Context.getAtomicType(ValueType);
  5965. }
  5966. case TYPE_PIPE: {
  5967. if (Record.size() != 2) {
  5968. Error("Incorrect encoding of pipe type");
  5969. return QualType();
  5970. }
  5971. // Reading the pipe element type.
  5972. QualType ElementType = readType(*Loc.F, Record, Idx);
  5973. unsigned ReadOnly = Record[1];
  5974. return Context.getPipeType(ElementType, ReadOnly);
  5975. }
  5976. case TYPE_DEPENDENT_SIZED_VECTOR: {
  5977. unsigned Idx = 0;
  5978. QualType ElementType = readType(*Loc.F, Record, Idx);
  5979. Expr *SizeExpr = ReadExpr(*Loc.F);
  5980. SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
  5981. unsigned VecKind = Record[Idx];
  5982. return Context.getDependentVectorType(ElementType, SizeExpr, AttrLoc,
  5983. (VectorType::VectorKind)VecKind);
  5984. }
  5985. case TYPE_DEPENDENT_SIZED_EXT_VECTOR: {
  5986. unsigned Idx = 0;
  5987. // DependentSizedExtVectorType
  5988. QualType ElementType = readType(*Loc.F, Record, Idx);
  5989. Expr *SizeExpr = ReadExpr(*Loc.F);
  5990. SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
  5991. return Context.getDependentSizedExtVectorType(ElementType, SizeExpr,
  5992. AttrLoc);
  5993. }
  5994. case TYPE_DEPENDENT_ADDRESS_SPACE: {
  5995. unsigned Idx = 0;
  5996. // DependentAddressSpaceType
  5997. QualType PointeeType = readType(*Loc.F, Record, Idx);
  5998. Expr *AddrSpaceExpr = ReadExpr(*Loc.F);
  5999. SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
  6000. return Context.getDependentAddressSpaceType(PointeeType, AddrSpaceExpr,
  6001. AttrLoc);
  6002. }
  6003. }
  6004. llvm_unreachable("Invalid TypeCode!");
  6005. }
  6006. void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
  6007. SmallVectorImpl<QualType> &Exceptions,
  6008. FunctionProtoType::ExceptionSpecInfo &ESI,
  6009. const RecordData &Record, unsigned &Idx) {
  6010. ExceptionSpecificationType EST =
  6011. static_cast<ExceptionSpecificationType>(Record[Idx++]);
  6012. ESI.Type = EST;
  6013. if (EST == EST_Dynamic) {
  6014. for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
  6015. Exceptions.push_back(readType(ModuleFile, Record, Idx));
  6016. ESI.Exceptions = Exceptions;
  6017. } else if (isComputedNoexcept(EST)) {
  6018. ESI.NoexceptExpr = ReadExpr(ModuleFile);
  6019. } else if (EST == EST_Uninstantiated) {
  6020. ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
  6021. ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
  6022. } else if (EST == EST_Unevaluated) {
  6023. ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
  6024. }
  6025. }
  6026. namespace clang {
  6027. class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
  6028. ModuleFile *F;
  6029. ASTReader *Reader;
  6030. const ASTReader::RecordData &Record;
  6031. unsigned &Idx;
  6032. SourceLocation ReadSourceLocation() {
  6033. return Reader->ReadSourceLocation(*F, Record, Idx);
  6034. }
  6035. TypeSourceInfo *GetTypeSourceInfo() {
  6036. return Reader->GetTypeSourceInfo(*F, Record, Idx);
  6037. }
  6038. NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
  6039. return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx);
  6040. }
  6041. Attr *ReadAttr() {
  6042. return Reader->ReadAttr(*F, Record, Idx);
  6043. }
  6044. public:
  6045. TypeLocReader(ModuleFile &F, ASTReader &Reader,
  6046. const ASTReader::RecordData &Record, unsigned &Idx)
  6047. : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
  6048. // We want compile-time assurance that we've enumerated all of
  6049. // these, so unfortunately we have to declare them first, then
  6050. // define them out-of-line.
  6051. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  6052. #define TYPELOC(CLASS, PARENT) \
  6053. void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
  6054. #include "clang/AST/TypeLocNodes.def"
  6055. void VisitFunctionTypeLoc(FunctionTypeLoc);
  6056. void VisitArrayTypeLoc(ArrayTypeLoc);
  6057. };
  6058. } // namespace clang
  6059. void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
  6060. // nothing to do
  6061. }
  6062. void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
  6063. TL.setBuiltinLoc(ReadSourceLocation());
  6064. if (TL.needsExtraLocalData()) {
  6065. TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
  6066. TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
  6067. TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
  6068. TL.setModeAttr(Record[Idx++]);
  6069. }
  6070. }
  6071. void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
  6072. TL.setNameLoc(ReadSourceLocation());
  6073. }
  6074. void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
  6075. TL.setStarLoc(ReadSourceLocation());
  6076. }
  6077. void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
  6078. // nothing to do
  6079. }
  6080. void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
  6081. // nothing to do
  6082. }
  6083. void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
  6084. TL.setExpansionLoc(ReadSourceLocation());
  6085. }
  6086. void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
  6087. TL.setCaretLoc(ReadSourceLocation());
  6088. }
  6089. void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
  6090. TL.setAmpLoc(ReadSourceLocation());
  6091. }
  6092. void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
  6093. TL.setAmpAmpLoc(ReadSourceLocation());
  6094. }
  6095. void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
  6096. TL.setStarLoc(ReadSourceLocation());
  6097. TL.setClassTInfo(GetTypeSourceInfo());
  6098. }
  6099. void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
  6100. TL.setLBracketLoc(ReadSourceLocation());
  6101. TL.setRBracketLoc(ReadSourceLocation());
  6102. if (Record[Idx++])
  6103. TL.setSizeExpr(Reader->ReadExpr(*F));
  6104. else
  6105. TL.setSizeExpr(nullptr);
  6106. }
  6107. void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
  6108. VisitArrayTypeLoc(TL);
  6109. }
  6110. void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
  6111. VisitArrayTypeLoc(TL);
  6112. }
  6113. void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
  6114. VisitArrayTypeLoc(TL);
  6115. }
  6116. void TypeLocReader::VisitDependentSizedArrayTypeLoc(
  6117. DependentSizedArrayTypeLoc TL) {
  6118. VisitArrayTypeLoc(TL);
  6119. }
  6120. void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
  6121. DependentAddressSpaceTypeLoc TL) {
  6122. TL.setAttrNameLoc(ReadSourceLocation());
  6123. SourceRange range;
  6124. range.setBegin(ReadSourceLocation());
  6125. range.setEnd(ReadSourceLocation());
  6126. TL.setAttrOperandParensRange(range);
  6127. TL.setAttrExprOperand(Reader->ReadExpr(*F));
  6128. }
  6129. void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
  6130. DependentSizedExtVectorTypeLoc TL) {
  6131. TL.setNameLoc(ReadSourceLocation());
  6132. }
  6133. void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
  6134. TL.setNameLoc(ReadSourceLocation());
  6135. }
  6136. void TypeLocReader::VisitDependentVectorTypeLoc(
  6137. DependentVectorTypeLoc TL) {
  6138. TL.setNameLoc(ReadSourceLocation());
  6139. }
  6140. void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
  6141. TL.setNameLoc(ReadSourceLocation());
  6142. }
  6143. void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
  6144. TL.setLocalRangeBegin(ReadSourceLocation());
  6145. TL.setLParenLoc(ReadSourceLocation());
  6146. TL.setRParenLoc(ReadSourceLocation());
  6147. TL.setExceptionSpecRange(SourceRange(Reader->ReadSourceLocation(*F, Record, Idx),
  6148. Reader->ReadSourceLocation(*F, Record, Idx)));
  6149. TL.setLocalRangeEnd(ReadSourceLocation());
  6150. for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
  6151. TL.setParam(i, Reader->ReadDeclAs<ParmVarDecl>(*F, Record, Idx));
  6152. }
  6153. }
  6154. void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
  6155. VisitFunctionTypeLoc(TL);
  6156. }
  6157. void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
  6158. VisitFunctionTypeLoc(TL);
  6159. }
  6160. void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
  6161. TL.setNameLoc(ReadSourceLocation());
  6162. }
  6163. void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
  6164. TL.setNameLoc(ReadSourceLocation());
  6165. }
  6166. void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
  6167. TL.setTypeofLoc(ReadSourceLocation());
  6168. TL.setLParenLoc(ReadSourceLocation());
  6169. TL.setRParenLoc(ReadSourceLocation());
  6170. }
  6171. void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
  6172. TL.setTypeofLoc(ReadSourceLocation());
  6173. TL.setLParenLoc(ReadSourceLocation());
  6174. TL.setRParenLoc(ReadSourceLocation());
  6175. TL.setUnderlyingTInfo(GetTypeSourceInfo());
  6176. }
  6177. void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
  6178. TL.setNameLoc(ReadSourceLocation());
  6179. }
  6180. void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
  6181. TL.setKWLoc(ReadSourceLocation());
  6182. TL.setLParenLoc(ReadSourceLocation());
  6183. TL.setRParenLoc(ReadSourceLocation());
  6184. TL.setUnderlyingTInfo(GetTypeSourceInfo());
  6185. }
  6186. void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
  6187. TL.setNameLoc(ReadSourceLocation());
  6188. }
  6189. void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
  6190. DeducedTemplateSpecializationTypeLoc TL) {
  6191. TL.setTemplateNameLoc(ReadSourceLocation());
  6192. }
  6193. void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
  6194. TL.setNameLoc(ReadSourceLocation());
  6195. }
  6196. void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
  6197. TL.setNameLoc(ReadSourceLocation());
  6198. }
  6199. void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
  6200. TL.setAttr(ReadAttr());
  6201. }
  6202. void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
  6203. TL.setNameLoc(ReadSourceLocation());
  6204. }
  6205. void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
  6206. SubstTemplateTypeParmTypeLoc TL) {
  6207. TL.setNameLoc(ReadSourceLocation());
  6208. }
  6209. void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
  6210. SubstTemplateTypeParmPackTypeLoc TL) {
  6211. TL.setNameLoc(ReadSourceLocation());
  6212. }
  6213. void TypeLocReader::VisitTemplateSpecializationTypeLoc(
  6214. TemplateSpecializationTypeLoc TL) {
  6215. TL.setTemplateKeywordLoc(ReadSourceLocation());
  6216. TL.setTemplateNameLoc(ReadSourceLocation());
  6217. TL.setLAngleLoc(ReadSourceLocation());
  6218. TL.setRAngleLoc(ReadSourceLocation());
  6219. for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
  6220. TL.setArgLocInfo(
  6221. i,
  6222. Reader->GetTemplateArgumentLocInfo(
  6223. *F, TL.getTypePtr()->getArg(i).getKind(), Record, Idx));
  6224. }
  6225. void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
  6226. TL.setLParenLoc(ReadSourceLocation());
  6227. TL.setRParenLoc(ReadSourceLocation());
  6228. }
  6229. void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
  6230. TL.setElaboratedKeywordLoc(ReadSourceLocation());
  6231. TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
  6232. }
  6233. void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
  6234. TL.setNameLoc(ReadSourceLocation());
  6235. }
  6236. void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
  6237. TL.setElaboratedKeywordLoc(ReadSourceLocation());
  6238. TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
  6239. TL.setNameLoc(ReadSourceLocation());
  6240. }
  6241. void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
  6242. DependentTemplateSpecializationTypeLoc TL) {
  6243. TL.setElaboratedKeywordLoc(ReadSourceLocation());
  6244. TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
  6245. TL.setTemplateKeywordLoc(ReadSourceLocation());
  6246. TL.setTemplateNameLoc(ReadSourceLocation());
  6247. TL.setLAngleLoc(ReadSourceLocation());
  6248. TL.setRAngleLoc(ReadSourceLocation());
  6249. for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
  6250. TL.setArgLocInfo(
  6251. I,
  6252. Reader->GetTemplateArgumentLocInfo(
  6253. *F, TL.getTypePtr()->getArg(I).getKind(), Record, Idx));
  6254. }
  6255. void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
  6256. TL.setEllipsisLoc(ReadSourceLocation());
  6257. }
  6258. void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
  6259. TL.setNameLoc(ReadSourceLocation());
  6260. }
  6261. void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
  6262. if (TL.getNumProtocols()) {
  6263. TL.setProtocolLAngleLoc(ReadSourceLocation());
  6264. TL.setProtocolRAngleLoc(ReadSourceLocation());
  6265. }
  6266. for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
  6267. TL.setProtocolLoc(i, ReadSourceLocation());
  6268. }
  6269. void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
  6270. TL.setHasBaseTypeAsWritten(Record[Idx++]);
  6271. TL.setTypeArgsLAngleLoc(ReadSourceLocation());
  6272. TL.setTypeArgsRAngleLoc(ReadSourceLocation());
  6273. for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
  6274. TL.setTypeArgTInfo(i, GetTypeSourceInfo());
  6275. TL.setProtocolLAngleLoc(ReadSourceLocation());
  6276. TL.setProtocolRAngleLoc(ReadSourceLocation());
  6277. for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
  6278. TL.setProtocolLoc(i, ReadSourceLocation());
  6279. }
  6280. void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
  6281. TL.setStarLoc(ReadSourceLocation());
  6282. }
  6283. void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
  6284. TL.setKWLoc(ReadSourceLocation());
  6285. TL.setLParenLoc(ReadSourceLocation());
  6286. TL.setRParenLoc(ReadSourceLocation());
  6287. }
  6288. void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
  6289. TL.setKWLoc(ReadSourceLocation());
  6290. }
  6291. void ASTReader::ReadTypeLoc(ModuleFile &F, const ASTReader::RecordData &Record,
  6292. unsigned &Idx, TypeLoc TL) {
  6293. TypeLocReader TLR(F, *this, Record, Idx);
  6294. for (; !TL.isNull(); TL = TL.getNextTypeLoc())
  6295. TLR.Visit(TL);
  6296. }
  6297. TypeSourceInfo *
  6298. ASTReader::GetTypeSourceInfo(ModuleFile &F, const ASTReader::RecordData &Record,
  6299. unsigned &Idx) {
  6300. QualType InfoTy = readType(F, Record, Idx);
  6301. if (InfoTy.isNull())
  6302. return nullptr;
  6303. TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
  6304. ReadTypeLoc(F, Record, Idx, TInfo->getTypeLoc());
  6305. return TInfo;
  6306. }
  6307. QualType ASTReader::GetType(TypeID ID) {
  6308. assert(ContextObj && "reading type with no AST context");
  6309. ASTContext &Context = *ContextObj;
  6310. unsigned FastQuals = ID & Qualifiers::FastMask;
  6311. unsigned Index = ID >> Qualifiers::FastWidth;
  6312. if (Index < NUM_PREDEF_TYPE_IDS) {
  6313. QualType T;
  6314. switch ((PredefinedTypeIDs)Index) {
  6315. case PREDEF_TYPE_NULL_ID:
  6316. return QualType();
  6317. case PREDEF_TYPE_VOID_ID:
  6318. T = Context.VoidTy;
  6319. break;
  6320. case PREDEF_TYPE_BOOL_ID:
  6321. T = Context.BoolTy;
  6322. break;
  6323. case PREDEF_TYPE_CHAR_U_ID:
  6324. case PREDEF_TYPE_CHAR_S_ID:
  6325. // FIXME: Check that the signedness of CharTy is correct!
  6326. T = Context.CharTy;
  6327. break;
  6328. case PREDEF_TYPE_UCHAR_ID:
  6329. T = Context.UnsignedCharTy;
  6330. break;
  6331. case PREDEF_TYPE_USHORT_ID:
  6332. T = Context.UnsignedShortTy;
  6333. break;
  6334. case PREDEF_TYPE_UINT_ID:
  6335. T = Context.UnsignedIntTy;
  6336. break;
  6337. case PREDEF_TYPE_ULONG_ID:
  6338. T = Context.UnsignedLongTy;
  6339. break;
  6340. case PREDEF_TYPE_ULONGLONG_ID:
  6341. T = Context.UnsignedLongLongTy;
  6342. break;
  6343. case PREDEF_TYPE_UINT128_ID:
  6344. T = Context.UnsignedInt128Ty;
  6345. break;
  6346. case PREDEF_TYPE_SCHAR_ID:
  6347. T = Context.SignedCharTy;
  6348. break;
  6349. case PREDEF_TYPE_WCHAR_ID:
  6350. T = Context.WCharTy;
  6351. break;
  6352. case PREDEF_TYPE_SHORT_ID:
  6353. T = Context.ShortTy;
  6354. break;
  6355. case PREDEF_TYPE_INT_ID:
  6356. T = Context.IntTy;
  6357. break;
  6358. case PREDEF_TYPE_LONG_ID:
  6359. T = Context.LongTy;
  6360. break;
  6361. case PREDEF_TYPE_LONGLONG_ID:
  6362. T = Context.LongLongTy;
  6363. break;
  6364. case PREDEF_TYPE_INT128_ID:
  6365. T = Context.Int128Ty;
  6366. break;
  6367. case PREDEF_TYPE_HALF_ID:
  6368. T = Context.HalfTy;
  6369. break;
  6370. case PREDEF_TYPE_FLOAT_ID:
  6371. T = Context.FloatTy;
  6372. break;
  6373. case PREDEF_TYPE_DOUBLE_ID:
  6374. T = Context.DoubleTy;
  6375. break;
  6376. case PREDEF_TYPE_LONGDOUBLE_ID:
  6377. T = Context.LongDoubleTy;
  6378. break;
  6379. case PREDEF_TYPE_SHORT_ACCUM_ID:
  6380. T = Context.ShortAccumTy;
  6381. break;
  6382. case PREDEF_TYPE_ACCUM_ID:
  6383. T = Context.AccumTy;
  6384. break;
  6385. case PREDEF_TYPE_LONG_ACCUM_ID:
  6386. T = Context.LongAccumTy;
  6387. break;
  6388. case PREDEF_TYPE_USHORT_ACCUM_ID:
  6389. T = Context.UnsignedShortAccumTy;
  6390. break;
  6391. case PREDEF_TYPE_UACCUM_ID:
  6392. T = Context.UnsignedAccumTy;
  6393. break;
  6394. case PREDEF_TYPE_ULONG_ACCUM_ID:
  6395. T = Context.UnsignedLongAccumTy;
  6396. break;
  6397. case PREDEF_TYPE_SHORT_FRACT_ID:
  6398. T = Context.ShortFractTy;
  6399. break;
  6400. case PREDEF_TYPE_FRACT_ID:
  6401. T = Context.FractTy;
  6402. break;
  6403. case PREDEF_TYPE_LONG_FRACT_ID:
  6404. T = Context.LongFractTy;
  6405. break;
  6406. case PREDEF_TYPE_USHORT_FRACT_ID:
  6407. T = Context.UnsignedShortFractTy;
  6408. break;
  6409. case PREDEF_TYPE_UFRACT_ID:
  6410. T = Context.UnsignedFractTy;
  6411. break;
  6412. case PREDEF_TYPE_ULONG_FRACT_ID:
  6413. T = Context.UnsignedLongFractTy;
  6414. break;
  6415. case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
  6416. T = Context.SatShortAccumTy;
  6417. break;
  6418. case PREDEF_TYPE_SAT_ACCUM_ID:
  6419. T = Context.SatAccumTy;
  6420. break;
  6421. case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
  6422. T = Context.SatLongAccumTy;
  6423. break;
  6424. case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
  6425. T = Context.SatUnsignedShortAccumTy;
  6426. break;
  6427. case PREDEF_TYPE_SAT_UACCUM_ID:
  6428. T = Context.SatUnsignedAccumTy;
  6429. break;
  6430. case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
  6431. T = Context.SatUnsignedLongAccumTy;
  6432. break;
  6433. case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
  6434. T = Context.SatShortFractTy;
  6435. break;
  6436. case PREDEF_TYPE_SAT_FRACT_ID:
  6437. T = Context.SatFractTy;
  6438. break;
  6439. case PREDEF_TYPE_SAT_LONG_FRACT_ID:
  6440. T = Context.SatLongFractTy;
  6441. break;
  6442. case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
  6443. T = Context.SatUnsignedShortFractTy;
  6444. break;
  6445. case PREDEF_TYPE_SAT_UFRACT_ID:
  6446. T = Context.SatUnsignedFractTy;
  6447. break;
  6448. case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
  6449. T = Context.SatUnsignedLongFractTy;
  6450. break;
  6451. case PREDEF_TYPE_FLOAT16_ID:
  6452. T = Context.Float16Ty;
  6453. break;
  6454. case PREDEF_TYPE_FLOAT128_ID:
  6455. T = Context.Float128Ty;
  6456. break;
  6457. case PREDEF_TYPE_OVERLOAD_ID:
  6458. T = Context.OverloadTy;
  6459. break;
  6460. case PREDEF_TYPE_BOUND_MEMBER:
  6461. T = Context.BoundMemberTy;
  6462. break;
  6463. case PREDEF_TYPE_PSEUDO_OBJECT:
  6464. T = Context.PseudoObjectTy;
  6465. break;
  6466. case PREDEF_TYPE_DEPENDENT_ID:
  6467. T = Context.DependentTy;
  6468. break;
  6469. case PREDEF_TYPE_UNKNOWN_ANY:
  6470. T = Context.UnknownAnyTy;
  6471. break;
  6472. case PREDEF_TYPE_NULLPTR_ID:
  6473. T = Context.NullPtrTy;
  6474. break;
  6475. case PREDEF_TYPE_CHAR8_ID:
  6476. T = Context.Char8Ty;
  6477. break;
  6478. case PREDEF_TYPE_CHAR16_ID:
  6479. T = Context.Char16Ty;
  6480. break;
  6481. case PREDEF_TYPE_CHAR32_ID:
  6482. T = Context.Char32Ty;
  6483. break;
  6484. case PREDEF_TYPE_OBJC_ID:
  6485. T = Context.ObjCBuiltinIdTy;
  6486. break;
  6487. case PREDEF_TYPE_OBJC_CLASS:
  6488. T = Context.ObjCBuiltinClassTy;
  6489. break;
  6490. case PREDEF_TYPE_OBJC_SEL:
  6491. T = Context.ObjCBuiltinSelTy;
  6492. break;
  6493. #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
  6494. case PREDEF_TYPE_##Id##_ID: \
  6495. T = Context.SingletonId; \
  6496. break;
  6497. #include "clang/Basic/OpenCLImageTypes.def"
  6498. #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
  6499. case PREDEF_TYPE_##Id##_ID: \
  6500. T = Context.Id##Ty; \
  6501. break;
  6502. #include "clang/Basic/OpenCLExtensionTypes.def"
  6503. case PREDEF_TYPE_SAMPLER_ID:
  6504. T = Context.OCLSamplerTy;
  6505. break;
  6506. case PREDEF_TYPE_EVENT_ID:
  6507. T = Context.OCLEventTy;
  6508. break;
  6509. case PREDEF_TYPE_CLK_EVENT_ID:
  6510. T = Context.OCLClkEventTy;
  6511. break;
  6512. case PREDEF_TYPE_QUEUE_ID:
  6513. T = Context.OCLQueueTy;
  6514. break;
  6515. case PREDEF_TYPE_RESERVE_ID_ID:
  6516. T = Context.OCLReserveIDTy;
  6517. break;
  6518. case PREDEF_TYPE_AUTO_DEDUCT:
  6519. T = Context.getAutoDeductType();
  6520. break;
  6521. case PREDEF_TYPE_AUTO_RREF_DEDUCT:
  6522. T = Context.getAutoRRefDeductType();
  6523. break;
  6524. case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
  6525. T = Context.ARCUnbridgedCastTy;
  6526. break;
  6527. case PREDEF_TYPE_BUILTIN_FN:
  6528. T = Context.BuiltinFnTy;
  6529. break;
  6530. case PREDEF_TYPE_OMP_ARRAY_SECTION:
  6531. T = Context.OMPArraySectionTy;
  6532. break;
  6533. #define SVE_TYPE(Name, Id, SingletonId) \
  6534. case PREDEF_TYPE_##Id##_ID: \
  6535. T = Context.SingletonId; \
  6536. break;
  6537. #include "clang/Basic/AArch64SVEACLETypes.def"
  6538. }
  6539. assert(!T.isNull() && "Unknown predefined type");
  6540. return T.withFastQualifiers(FastQuals);
  6541. }
  6542. Index -= NUM_PREDEF_TYPE_IDS;
  6543. assert(Index < TypesLoaded.size() && "Type index out-of-range");
  6544. if (TypesLoaded[Index].isNull()) {
  6545. TypesLoaded[Index] = readTypeRecord(Index);
  6546. if (TypesLoaded[Index].isNull())
  6547. return QualType();
  6548. TypesLoaded[Index]->setFromAST();
  6549. if (DeserializationListener)
  6550. DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
  6551. TypesLoaded[Index]);
  6552. }
  6553. return TypesLoaded[Index].withFastQualifiers(FastQuals);
  6554. }
  6555. QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
  6556. return GetType(getGlobalTypeID(F, LocalID));
  6557. }
  6558. serialization::TypeID
  6559. ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
  6560. unsigned FastQuals = LocalID & Qualifiers::FastMask;
  6561. unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
  6562. if (LocalIndex < NUM_PREDEF_TYPE_IDS)
  6563. return LocalID;
  6564. if (!F.ModuleOffsetMap.empty())
  6565. ReadModuleOffsetMap(F);
  6566. ContinuousRangeMap<uint32_t, int, 2>::iterator I
  6567. = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
  6568. assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
  6569. unsigned GlobalIndex = LocalIndex + I->second;
  6570. return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
  6571. }
  6572. TemplateArgumentLocInfo
  6573. ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
  6574. TemplateArgument::ArgKind Kind,
  6575. const RecordData &Record,
  6576. unsigned &Index) {
  6577. switch (Kind) {
  6578. case TemplateArgument::Expression:
  6579. return ReadExpr(F);
  6580. case TemplateArgument::Type:
  6581. return GetTypeSourceInfo(F, Record, Index);
  6582. case TemplateArgument::Template: {
  6583. NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
  6584. Index);
  6585. SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
  6586. return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
  6587. SourceLocation());
  6588. }
  6589. case TemplateArgument::TemplateExpansion: {
  6590. NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
  6591. Index);
  6592. SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
  6593. SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
  6594. return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
  6595. EllipsisLoc);
  6596. }
  6597. case TemplateArgument::Null:
  6598. case TemplateArgument::Integral:
  6599. case TemplateArgument::Declaration:
  6600. case TemplateArgument::NullPtr:
  6601. case TemplateArgument::Pack:
  6602. // FIXME: Is this right?
  6603. return TemplateArgumentLocInfo();
  6604. }
  6605. llvm_unreachable("unexpected template argument loc");
  6606. }
  6607. TemplateArgumentLoc
  6608. ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
  6609. const RecordData &Record, unsigned &Index) {
  6610. TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
  6611. if (Arg.getKind() == TemplateArgument::Expression) {
  6612. if (Record[Index++]) // bool InfoHasSameExpr.
  6613. return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
  6614. }
  6615. return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
  6616. Record, Index));
  6617. }
  6618. const ASTTemplateArgumentListInfo*
  6619. ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
  6620. const RecordData &Record,
  6621. unsigned &Index) {
  6622. SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
  6623. SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
  6624. unsigned NumArgsAsWritten = Record[Index++];
  6625. TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
  6626. for (unsigned i = 0; i != NumArgsAsWritten; ++i)
  6627. TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
  6628. return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
  6629. }
  6630. Decl *ASTReader::GetExternalDecl(uint32_t ID) {
  6631. return GetDecl(ID);
  6632. }
  6633. void ASTReader::CompleteRedeclChain(const Decl *D) {
  6634. if (NumCurrentElementsDeserializing) {
  6635. // We arrange to not care about the complete redeclaration chain while we're
  6636. // deserializing. Just remember that the AST has marked this one as complete
  6637. // but that it's not actually complete yet, so we know we still need to
  6638. // complete it later.
  6639. PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
  6640. return;
  6641. }
  6642. const DeclContext *DC = D->getDeclContext()->getRedeclContext();
  6643. // If this is a named declaration, complete it by looking it up
  6644. // within its context.
  6645. //
  6646. // FIXME: Merging a function definition should merge
  6647. // all mergeable entities within it.
  6648. if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
  6649. isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
  6650. if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
  6651. if (!getContext().getLangOpts().CPlusPlus &&
  6652. isa<TranslationUnitDecl>(DC)) {
  6653. // Outside of C++, we don't have a lookup table for the TU, so update
  6654. // the identifier instead. (For C++ modules, we don't store decls
  6655. // in the serialized identifier table, so we do the lookup in the TU.)
  6656. auto *II = Name.getAsIdentifierInfo();
  6657. assert(II && "non-identifier name in C?");
  6658. if (II->isOutOfDate())
  6659. updateOutOfDateIdentifier(*II);
  6660. } else
  6661. DC->lookup(Name);
  6662. } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
  6663. // Find all declarations of this kind from the relevant context.
  6664. for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
  6665. auto *DC = cast<DeclContext>(DCDecl);
  6666. SmallVector<Decl*, 8> Decls;
  6667. FindExternalLexicalDecls(
  6668. DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
  6669. }
  6670. }
  6671. }
  6672. if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
  6673. CTSD->getSpecializedTemplate()->LoadLazySpecializations();
  6674. if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
  6675. VTSD->getSpecializedTemplate()->LoadLazySpecializations();
  6676. if (auto *FD = dyn_cast<FunctionDecl>(D)) {
  6677. if (auto *Template = FD->getPrimaryTemplate())
  6678. Template->LoadLazySpecializations();
  6679. }
  6680. }
  6681. CXXCtorInitializer **
  6682. ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
  6683. RecordLocation Loc = getLocalBitOffset(Offset);
  6684. BitstreamCursor &Cursor = Loc.F->DeclsCursor;
  6685. SavedStreamPosition SavedPosition(Cursor);
  6686. if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
  6687. Error(std::move(Err));
  6688. return nullptr;
  6689. }
  6690. ReadingKindTracker ReadingKind(Read_Decl, *this);
  6691. RecordData Record;
  6692. Expected<unsigned> MaybeCode = Cursor.ReadCode();
  6693. if (!MaybeCode) {
  6694. Error(MaybeCode.takeError());
  6695. return nullptr;
  6696. }
  6697. unsigned Code = MaybeCode.get();
  6698. Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record);
  6699. if (!MaybeRecCode) {
  6700. Error(MaybeRecCode.takeError());
  6701. return nullptr;
  6702. }
  6703. if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
  6704. Error("malformed AST file: missing C++ ctor initializers");
  6705. return nullptr;
  6706. }
  6707. unsigned Idx = 0;
  6708. return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
  6709. }
  6710. CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
  6711. assert(ContextObj && "reading base specifiers with no AST context");
  6712. ASTContext &Context = *ContextObj;
  6713. RecordLocation Loc = getLocalBitOffset(Offset);
  6714. BitstreamCursor &Cursor = Loc.F->DeclsCursor;
  6715. SavedStreamPosition SavedPosition(Cursor);
  6716. if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
  6717. Error(std::move(Err));
  6718. return nullptr;
  6719. }
  6720. ReadingKindTracker ReadingKind(Read_Decl, *this);
  6721. RecordData Record;
  6722. Expected<unsigned> MaybeCode = Cursor.ReadCode();
  6723. if (!MaybeCode) {
  6724. Error(MaybeCode.takeError());
  6725. return nullptr;
  6726. }
  6727. unsigned Code = MaybeCode.get();
  6728. Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record);
  6729. if (!MaybeRecCode) {
  6730. Error(MaybeCode.takeError());
  6731. return nullptr;
  6732. }
  6733. unsigned RecCode = MaybeRecCode.get();
  6734. if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
  6735. Error("malformed AST file: missing C++ base specifiers");
  6736. return nullptr;
  6737. }
  6738. unsigned Idx = 0;
  6739. unsigned NumBases = Record[Idx++];
  6740. void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
  6741. CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
  6742. for (unsigned I = 0; I != NumBases; ++I)
  6743. Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
  6744. return Bases;
  6745. }
  6746. serialization::DeclID
  6747. ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
  6748. if (LocalID < NUM_PREDEF_DECL_IDS)
  6749. return LocalID;
  6750. if (!F.ModuleOffsetMap.empty())
  6751. ReadModuleOffsetMap(F);
  6752. ContinuousRangeMap<uint32_t, int, 2>::iterator I
  6753. = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
  6754. assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
  6755. return LocalID + I->second;
  6756. }
  6757. bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
  6758. ModuleFile &M) const {
  6759. // Predefined decls aren't from any module.
  6760. if (ID < NUM_PREDEF_DECL_IDS)
  6761. return false;
  6762. return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
  6763. ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
  6764. }
  6765. ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
  6766. if (!D->isFromASTFile())
  6767. return nullptr;
  6768. GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
  6769. assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
  6770. return I->second;
  6771. }
  6772. SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
  6773. if (ID < NUM_PREDEF_DECL_IDS)
  6774. return SourceLocation();
  6775. unsigned Index = ID - NUM_PREDEF_DECL_IDS;
  6776. if (Index > DeclsLoaded.size()) {
  6777. Error("declaration ID out-of-range for AST file");
  6778. return SourceLocation();
  6779. }
  6780. if (Decl *D = DeclsLoaded[Index])
  6781. return D->getLocation();
  6782. SourceLocation Loc;
  6783. DeclCursorForID(ID, Loc);
  6784. return Loc;
  6785. }
  6786. static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
  6787. switch (ID) {
  6788. case PREDEF_DECL_NULL_ID:
  6789. return nullptr;
  6790. case PREDEF_DECL_TRANSLATION_UNIT_ID:
  6791. return Context.getTranslationUnitDecl();
  6792. case PREDEF_DECL_OBJC_ID_ID:
  6793. return Context.getObjCIdDecl();
  6794. case PREDEF_DECL_OBJC_SEL_ID:
  6795. return Context.getObjCSelDecl();
  6796. case PREDEF_DECL_OBJC_CLASS_ID:
  6797. return Context.getObjCClassDecl();
  6798. case PREDEF_DECL_OBJC_PROTOCOL_ID:
  6799. return Context.getObjCProtocolDecl();
  6800. case PREDEF_DECL_INT_128_ID:
  6801. return Context.getInt128Decl();
  6802. case PREDEF_DECL_UNSIGNED_INT_128_ID:
  6803. return Context.getUInt128Decl();
  6804. case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
  6805. return Context.getObjCInstanceTypeDecl();
  6806. case PREDEF_DECL_BUILTIN_VA_LIST_ID:
  6807. return Context.getBuiltinVaListDecl();
  6808. case PREDEF_DECL_VA_LIST_TAG:
  6809. return Context.getVaListTagDecl();
  6810. case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
  6811. return Context.getBuiltinMSVaListDecl();
  6812. case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
  6813. return Context.getExternCContextDecl();
  6814. case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
  6815. return Context.getMakeIntegerSeqDecl();
  6816. case PREDEF_DECL_CF_CONSTANT_STRING_ID:
  6817. return Context.getCFConstantStringDecl();
  6818. case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
  6819. return Context.getCFConstantStringTagDecl();
  6820. case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
  6821. return Context.getTypePackElementDecl();
  6822. }
  6823. llvm_unreachable("PredefinedDeclIDs unknown enum value");
  6824. }
  6825. Decl *ASTReader::GetExistingDecl(DeclID ID) {
  6826. assert(ContextObj && "reading decl with no AST context");
  6827. if (ID < NUM_PREDEF_DECL_IDS) {
  6828. Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
  6829. if (D) {
  6830. // Track that we have merged the declaration with ID \p ID into the
  6831. // pre-existing predefined declaration \p D.
  6832. auto &Merged = KeyDecls[D->getCanonicalDecl()];
  6833. if (Merged.empty())
  6834. Merged.push_back(ID);
  6835. }
  6836. return D;
  6837. }
  6838. unsigned Index = ID - NUM_PREDEF_DECL_IDS;
  6839. if (Index >= DeclsLoaded.size()) {
  6840. assert(0 && "declaration ID out-of-range for AST file");
  6841. Error("declaration ID out-of-range for AST file");
  6842. return nullptr;
  6843. }
  6844. return DeclsLoaded[Index];
  6845. }
  6846. Decl *ASTReader::GetDecl(DeclID ID) {
  6847. if (ID < NUM_PREDEF_DECL_IDS)
  6848. return GetExistingDecl(ID);
  6849. unsigned Index = ID - NUM_PREDEF_DECL_IDS;
  6850. if (Index >= DeclsLoaded.size()) {
  6851. assert(0 && "declaration ID out-of-range for AST file");
  6852. Error("declaration ID out-of-range for AST file");
  6853. return nullptr;
  6854. }
  6855. if (!DeclsLoaded[Index]) {
  6856. ReadDeclRecord(ID);
  6857. if (DeserializationListener)
  6858. DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
  6859. }
  6860. return DeclsLoaded[Index];
  6861. }
  6862. DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
  6863. DeclID GlobalID) {
  6864. if (GlobalID < NUM_PREDEF_DECL_IDS)
  6865. return GlobalID;
  6866. GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
  6867. assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
  6868. ModuleFile *Owner = I->second;
  6869. llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
  6870. = M.GlobalToLocalDeclIDs.find(Owner);
  6871. if (Pos == M.GlobalToLocalDeclIDs.end())
  6872. return 0;
  6873. return GlobalID - Owner->BaseDeclID + Pos->second;
  6874. }
  6875. serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
  6876. const RecordData &Record,
  6877. unsigned &Idx) {
  6878. if (Idx >= Record.size()) {
  6879. Error("Corrupted AST file");
  6880. return 0;
  6881. }
  6882. return getGlobalDeclID(F, Record[Idx++]);
  6883. }
  6884. /// Resolve the offset of a statement into a statement.
  6885. ///
  6886. /// This operation will read a new statement from the external
  6887. /// source each time it is called, and is meant to be used via a
  6888. /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
  6889. Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
  6890. // Switch case IDs are per Decl.
  6891. ClearSwitchCaseIDs();
  6892. // Offset here is a global offset across the entire chain.
  6893. RecordLocation Loc = getLocalBitOffset(Offset);
  6894. if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
  6895. Error(std::move(Err));
  6896. return nullptr;
  6897. }
  6898. assert(NumCurrentElementsDeserializing == 0 &&
  6899. "should not be called while already deserializing");
  6900. Deserializing D(this);
  6901. return ReadStmtFromStream(*Loc.F);
  6902. }
  6903. void ASTReader::FindExternalLexicalDecls(
  6904. const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
  6905. SmallVectorImpl<Decl *> &Decls) {
  6906. bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
  6907. auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
  6908. assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
  6909. for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
  6910. auto K = (Decl::Kind)+LexicalDecls[I];
  6911. if (!IsKindWeWant(K))
  6912. continue;
  6913. auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
  6914. // Don't add predefined declarations to the lexical context more
  6915. // than once.
  6916. if (ID < NUM_PREDEF_DECL_IDS) {
  6917. if (PredefsVisited[ID])
  6918. continue;
  6919. PredefsVisited[ID] = true;
  6920. }
  6921. if (Decl *D = GetLocalDecl(*M, ID)) {
  6922. assert(D->getKind() == K && "wrong kind for lexical decl");
  6923. if (!DC->isDeclInLexicalTraversal(D))
  6924. Decls.push_back(D);
  6925. }
  6926. }
  6927. };
  6928. if (isa<TranslationUnitDecl>(DC)) {
  6929. for (auto Lexical : TULexicalDecls)
  6930. Visit(Lexical.first, Lexical.second);
  6931. } else {
  6932. auto I = LexicalDecls.find(DC);
  6933. if (I != LexicalDecls.end())
  6934. Visit(I->second.first, I->second.second);
  6935. }
  6936. ++NumLexicalDeclContextsRead;
  6937. }
  6938. namespace {
  6939. class DeclIDComp {
  6940. ASTReader &Reader;
  6941. ModuleFile &Mod;
  6942. public:
  6943. DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
  6944. bool operator()(LocalDeclID L, LocalDeclID R) const {
  6945. SourceLocation LHS = getLocation(L);
  6946. SourceLocation RHS = getLocation(R);
  6947. return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
  6948. }
  6949. bool operator()(SourceLocation LHS, LocalDeclID R) const {
  6950. SourceLocation RHS = getLocation(R);
  6951. return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
  6952. }
  6953. bool operator()(LocalDeclID L, SourceLocation RHS) const {
  6954. SourceLocation LHS = getLocation(L);
  6955. return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
  6956. }
  6957. SourceLocation getLocation(LocalDeclID ID) const {
  6958. return Reader.getSourceManager().getFileLoc(
  6959. Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
  6960. }
  6961. };
  6962. } // namespace
  6963. void ASTReader::FindFileRegionDecls(FileID File,
  6964. unsigned Offset, unsigned Length,
  6965. SmallVectorImpl<Decl *> &Decls) {
  6966. SourceManager &SM = getSourceManager();
  6967. llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
  6968. if (I == FileDeclIDs.end())
  6969. return;
  6970. FileDeclsInfo &DInfo = I->second;
  6971. if (DInfo.Decls.empty())
  6972. return;
  6973. SourceLocation
  6974. BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
  6975. SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
  6976. DeclIDComp DIDComp(*this, *DInfo.Mod);
  6977. ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
  6978. llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
  6979. if (BeginIt != DInfo.Decls.begin())
  6980. --BeginIt;
  6981. // If we are pointing at a top-level decl inside an objc container, we need
  6982. // to backtrack until we find it otherwise we will fail to report that the
  6983. // region overlaps with an objc container.
  6984. while (BeginIt != DInfo.Decls.begin() &&
  6985. GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
  6986. ->isTopLevelDeclInObjCContainer())
  6987. --BeginIt;
  6988. ArrayRef<serialization::LocalDeclID>::iterator EndIt =
  6989. llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
  6990. if (EndIt != DInfo.Decls.end())
  6991. ++EndIt;
  6992. for (ArrayRef<serialization::LocalDeclID>::iterator
  6993. DIt = BeginIt; DIt != EndIt; ++DIt)
  6994. Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
  6995. }
  6996. bool
  6997. ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
  6998. DeclarationName Name) {
  6999. assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
  7000. "DeclContext has no visible decls in storage");
  7001. if (!Name)
  7002. return false;
  7003. auto It = Lookups.find(DC);
  7004. if (It == Lookups.end())
  7005. return false;
  7006. Deserializing LookupResults(this);
  7007. // Load the list of declarations.
  7008. SmallVector<NamedDecl *, 64> Decls;
  7009. for (DeclID ID : It->second.Table.find(Name)) {
  7010. NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
  7011. if (ND->getDeclName() == Name)
  7012. Decls.push_back(ND);
  7013. }
  7014. ++NumVisibleDeclContextsRead;
  7015. SetExternalVisibleDeclsForName(DC, Name, Decls);
  7016. return !Decls.empty();
  7017. }
  7018. void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
  7019. if (!DC->hasExternalVisibleStorage())
  7020. return;
  7021. auto It = Lookups.find(DC);
  7022. assert(It != Lookups.end() &&
  7023. "have external visible storage but no lookup tables");
  7024. DeclsMap Decls;
  7025. for (DeclID ID : It->second.Table.findAll()) {
  7026. NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
  7027. Decls[ND->getDeclName()].push_back(ND);
  7028. }
  7029. ++NumVisibleDeclContextsRead;
  7030. for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
  7031. SetExternalVisibleDeclsForName(DC, I->first, I->second);
  7032. }
  7033. const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
  7034. }
  7035. const serialization::reader::DeclContextLookupTable *
  7036. ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
  7037. auto I = Lookups.find(Primary);
  7038. return I == Lookups.end() ? nullptr : &I->second;
  7039. }
  7040. /// Under non-PCH compilation the consumer receives the objc methods
  7041. /// before receiving the implementation, and codegen depends on this.
  7042. /// We simulate this by deserializing and passing to consumer the methods of the
  7043. /// implementation before passing the deserialized implementation decl.
  7044. static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
  7045. ASTConsumer *Consumer) {
  7046. assert(ImplD && Consumer);
  7047. for (auto *I : ImplD->methods())
  7048. Consumer->HandleInterestingDecl(DeclGroupRef(I));
  7049. Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
  7050. }
  7051. void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
  7052. if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
  7053. PassObjCImplDeclToConsumer(ImplD, Consumer);
  7054. else
  7055. Consumer->HandleInterestingDecl(DeclGroupRef(D));
  7056. }
  7057. void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
  7058. this->Consumer = Consumer;
  7059. if (Consumer)
  7060. PassInterestingDeclsToConsumer();
  7061. if (DeserializationListener)
  7062. DeserializationListener->ReaderInitialized(this);
  7063. }
  7064. void ASTReader::PrintStats() {
  7065. std::fprintf(stderr, "*** AST File Statistics:\n");
  7066. unsigned NumTypesLoaded
  7067. = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
  7068. QualType());
  7069. unsigned NumDeclsLoaded
  7070. = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
  7071. (Decl *)nullptr);
  7072. unsigned NumIdentifiersLoaded
  7073. = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
  7074. IdentifiersLoaded.end(),
  7075. (IdentifierInfo *)nullptr);
  7076. unsigned NumMacrosLoaded
  7077. = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
  7078. MacrosLoaded.end(),
  7079. (MacroInfo *)nullptr);
  7080. unsigned NumSelectorsLoaded
  7081. = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
  7082. SelectorsLoaded.end(),
  7083. Selector());
  7084. if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
  7085. std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
  7086. NumSLocEntriesRead, TotalNumSLocEntries,
  7087. ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
  7088. if (!TypesLoaded.empty())
  7089. std::fprintf(stderr, " %u/%u types read (%f%%)\n",
  7090. NumTypesLoaded, (unsigned)TypesLoaded.size(),
  7091. ((float)NumTypesLoaded/TypesLoaded.size() * 100));
  7092. if (!DeclsLoaded.empty())
  7093. std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
  7094. NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
  7095. ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
  7096. if (!IdentifiersLoaded.empty())
  7097. std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
  7098. NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
  7099. ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
  7100. if (!MacrosLoaded.empty())
  7101. std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
  7102. NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
  7103. ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
  7104. if (!SelectorsLoaded.empty())
  7105. std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
  7106. NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
  7107. ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
  7108. if (TotalNumStatements)
  7109. std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
  7110. NumStatementsRead, TotalNumStatements,
  7111. ((float)NumStatementsRead/TotalNumStatements * 100));
  7112. if (TotalNumMacros)
  7113. std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
  7114. NumMacrosRead, TotalNumMacros,
  7115. ((float)NumMacrosRead/TotalNumMacros * 100));
  7116. if (TotalLexicalDeclContexts)
  7117. std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
  7118. NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
  7119. ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
  7120. * 100));
  7121. if (TotalVisibleDeclContexts)
  7122. std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
  7123. NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
  7124. ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
  7125. * 100));
  7126. if (TotalNumMethodPoolEntries)
  7127. std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
  7128. NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
  7129. ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
  7130. * 100));
  7131. if (NumMethodPoolLookups)
  7132. std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
  7133. NumMethodPoolHits, NumMethodPoolLookups,
  7134. ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
  7135. if (NumMethodPoolTableLookups)
  7136. std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
  7137. NumMethodPoolTableHits, NumMethodPoolTableLookups,
  7138. ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
  7139. * 100.0));
  7140. if (NumIdentifierLookupHits)
  7141. std::fprintf(stderr,
  7142. " %u / %u identifier table lookups succeeded (%f%%)\n",
  7143. NumIdentifierLookupHits, NumIdentifierLookups,
  7144. (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
  7145. if (GlobalIndex) {
  7146. std::fprintf(stderr, "\n");
  7147. GlobalIndex->printStats();
  7148. }
  7149. std::fprintf(stderr, "\n");
  7150. dump();
  7151. std::fprintf(stderr, "\n");
  7152. }
  7153. template<typename Key, typename ModuleFile, unsigned InitialCapacity>
  7154. LLVM_DUMP_METHOD static void
  7155. dumpModuleIDMap(StringRef Name,
  7156. const ContinuousRangeMap<Key, ModuleFile *,
  7157. InitialCapacity> &Map) {
  7158. if (Map.begin() == Map.end())
  7159. return;
  7160. using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
  7161. llvm::errs() << Name << ":\n";
  7162. for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
  7163. I != IEnd; ++I) {
  7164. llvm::errs() << " " << I->first << " -> " << I->second->FileName
  7165. << "\n";
  7166. }
  7167. }
  7168. LLVM_DUMP_METHOD void ASTReader::dump() {
  7169. llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
  7170. dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
  7171. dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
  7172. dumpModuleIDMap("Global type map", GlobalTypeMap);
  7173. dumpModuleIDMap("Global declaration map", GlobalDeclMap);
  7174. dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
  7175. dumpModuleIDMap("Global macro map", GlobalMacroMap);
  7176. dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
  7177. dumpModuleIDMap("Global selector map", GlobalSelectorMap);
  7178. dumpModuleIDMap("Global preprocessed entity map",
  7179. GlobalPreprocessedEntityMap);
  7180. llvm::errs() << "\n*** PCH/Modules Loaded:";
  7181. for (ModuleFile &M : ModuleMgr)
  7182. M.dump();
  7183. }
  7184. /// Return the amount of memory used by memory buffers, breaking down
  7185. /// by heap-backed versus mmap'ed memory.
  7186. void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
  7187. for (ModuleFile &I : ModuleMgr) {
  7188. if (llvm::MemoryBuffer *buf = I.Buffer) {
  7189. size_t bytes = buf->getBufferSize();
  7190. switch (buf->getBufferKind()) {
  7191. case llvm::MemoryBuffer::MemoryBuffer_Malloc:
  7192. sizes.malloc_bytes += bytes;
  7193. break;
  7194. case llvm::MemoryBuffer::MemoryBuffer_MMap:
  7195. sizes.mmap_bytes += bytes;
  7196. break;
  7197. }
  7198. }
  7199. }
  7200. }
  7201. void ASTReader::InitializeSema(Sema &S) {
  7202. SemaObj = &S;
  7203. S.addExternalSource(this);
  7204. // Makes sure any declarations that were deserialized "too early"
  7205. // still get added to the identifier's declaration chains.
  7206. for (uint64_t ID : PreloadedDeclIDs) {
  7207. NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
  7208. pushExternalDeclIntoScope(D, D->getDeclName());
  7209. }
  7210. PreloadedDeclIDs.clear();
  7211. // FIXME: What happens if these are changed by a module import?
  7212. if (!FPPragmaOptions.empty()) {
  7213. assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
  7214. SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
  7215. }
  7216. SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
  7217. SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
  7218. SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
  7219. UpdateSema();
  7220. }
  7221. void ASTReader::UpdateSema() {
  7222. assert(SemaObj && "no Sema to update");
  7223. // Load the offsets of the declarations that Sema references.
  7224. // They will be lazily deserialized when needed.
  7225. if (!SemaDeclRefs.empty()) {
  7226. assert(SemaDeclRefs.size() % 3 == 0);
  7227. for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
  7228. if (!SemaObj->StdNamespace)
  7229. SemaObj->StdNamespace = SemaDeclRefs[I];
  7230. if (!SemaObj->StdBadAlloc)
  7231. SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
  7232. if (!SemaObj->StdAlignValT)
  7233. SemaObj->StdAlignValT = SemaDeclRefs[I+2];
  7234. }
  7235. SemaDeclRefs.clear();
  7236. }
  7237. // Update the state of pragmas. Use the same API as if we had encountered the
  7238. // pragma in the source.
  7239. if(OptimizeOffPragmaLocation.isValid())
  7240. SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
  7241. if (PragmaMSStructState != -1)
  7242. SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
  7243. if (PointersToMembersPragmaLocation.isValid()) {
  7244. SemaObj->ActOnPragmaMSPointersToMembers(
  7245. (LangOptions::PragmaMSPointersToMembersKind)
  7246. PragmaMSPointersToMembersState,
  7247. PointersToMembersPragmaLocation);
  7248. }
  7249. SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
  7250. if (PragmaPackCurrentValue) {
  7251. // The bottom of the stack might have a default value. It must be adjusted
  7252. // to the current value to ensure that the packing state is preserved after
  7253. // popping entries that were included/imported from a PCH/module.
  7254. bool DropFirst = false;
  7255. if (!PragmaPackStack.empty() &&
  7256. PragmaPackStack.front().Location.isInvalid()) {
  7257. assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
  7258. "Expected a default alignment value");
  7259. SemaObj->PackStack.Stack.emplace_back(
  7260. PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
  7261. SemaObj->PackStack.CurrentPragmaLocation,
  7262. PragmaPackStack.front().PushLocation);
  7263. DropFirst = true;
  7264. }
  7265. for (const auto &Entry :
  7266. llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
  7267. SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
  7268. Entry.Location, Entry.PushLocation);
  7269. if (PragmaPackCurrentLocation.isInvalid()) {
  7270. assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
  7271. "Expected a default alignment value");
  7272. // Keep the current values.
  7273. } else {
  7274. SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
  7275. SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
  7276. }
  7277. }
  7278. }
  7279. IdentifierInfo *ASTReader::get(StringRef Name) {
  7280. // Note that we are loading an identifier.
  7281. Deserializing AnIdentifier(this);
  7282. IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
  7283. NumIdentifierLookups,
  7284. NumIdentifierLookupHits);
  7285. // We don't need to do identifier table lookups in C++ modules (we preload
  7286. // all interesting declarations, and don't need to use the scope for name
  7287. // lookups). Perform the lookup in PCH files, though, since we don't build
  7288. // a complete initial identifier table if we're carrying on from a PCH.
  7289. if (PP.getLangOpts().CPlusPlus) {
  7290. for (auto F : ModuleMgr.pch_modules())
  7291. if (Visitor(*F))
  7292. break;
  7293. } else {
  7294. // If there is a global index, look there first to determine which modules
  7295. // provably do not have any results for this identifier.
  7296. GlobalModuleIndex::HitSet Hits;
  7297. GlobalModuleIndex::HitSet *HitsPtr = nullptr;
  7298. if (!loadGlobalIndex()) {
  7299. if (GlobalIndex->lookupIdentifier(Name, Hits)) {
  7300. HitsPtr = &Hits;
  7301. }
  7302. }
  7303. ModuleMgr.visit(Visitor, HitsPtr);
  7304. }
  7305. IdentifierInfo *II = Visitor.getIdentifierInfo();
  7306. markIdentifierUpToDate(II);
  7307. return II;
  7308. }
  7309. namespace clang {
  7310. /// An identifier-lookup iterator that enumerates all of the
  7311. /// identifiers stored within a set of AST files.
  7312. class ASTIdentifierIterator : public IdentifierIterator {
  7313. /// The AST reader whose identifiers are being enumerated.
  7314. const ASTReader &Reader;
  7315. /// The current index into the chain of AST files stored in
  7316. /// the AST reader.
  7317. unsigned Index;
  7318. /// The current position within the identifier lookup table
  7319. /// of the current AST file.
  7320. ASTIdentifierLookupTable::key_iterator Current;
  7321. /// The end position within the identifier lookup table of
  7322. /// the current AST file.
  7323. ASTIdentifierLookupTable::key_iterator End;
  7324. /// Whether to skip any modules in the ASTReader.
  7325. bool SkipModules;
  7326. public:
  7327. explicit ASTIdentifierIterator(const ASTReader &Reader,
  7328. bool SkipModules = false);
  7329. StringRef Next() override;
  7330. };
  7331. } // namespace clang
  7332. ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
  7333. bool SkipModules)
  7334. : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
  7335. }
  7336. StringRef ASTIdentifierIterator::Next() {
  7337. while (Current == End) {
  7338. // If we have exhausted all of our AST files, we're done.
  7339. if (Index == 0)
  7340. return StringRef();
  7341. --Index;
  7342. ModuleFile &F = Reader.ModuleMgr[Index];
  7343. if (SkipModules && F.isModule())
  7344. continue;
  7345. ASTIdentifierLookupTable *IdTable =
  7346. (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
  7347. Current = IdTable->key_begin();
  7348. End = IdTable->key_end();
  7349. }
  7350. // We have any identifiers remaining in the current AST file; return
  7351. // the next one.
  7352. StringRef Result = *Current;
  7353. ++Current;
  7354. return Result;
  7355. }
  7356. namespace {
  7357. /// A utility for appending two IdentifierIterators.
  7358. class ChainedIdentifierIterator : public IdentifierIterator {
  7359. std::unique_ptr<IdentifierIterator> Current;
  7360. std::unique_ptr<IdentifierIterator> Queued;
  7361. public:
  7362. ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
  7363. std::unique_ptr<IdentifierIterator> Second)
  7364. : Current(std::move(First)), Queued(std::move(Second)) {}
  7365. StringRef Next() override {
  7366. if (!Current)
  7367. return StringRef();
  7368. StringRef result = Current->Next();
  7369. if (!result.empty())
  7370. return result;
  7371. // Try the queued iterator, which may itself be empty.
  7372. Current.reset();
  7373. std::swap(Current, Queued);
  7374. return Next();
  7375. }
  7376. };
  7377. } // namespace
  7378. IdentifierIterator *ASTReader::getIdentifiers() {
  7379. if (!loadGlobalIndex()) {
  7380. std::unique_ptr<IdentifierIterator> ReaderIter(
  7381. new ASTIdentifierIterator(*this, /*SkipModules=*/true));
  7382. std::unique_ptr<IdentifierIterator> ModulesIter(
  7383. GlobalIndex->createIdentifierIterator());
  7384. return new ChainedIdentifierIterator(std::move(ReaderIter),
  7385. std::move(ModulesIter));
  7386. }
  7387. return new ASTIdentifierIterator(*this);
  7388. }
  7389. namespace clang {
  7390. namespace serialization {
  7391. class ReadMethodPoolVisitor {
  7392. ASTReader &Reader;
  7393. Selector Sel;
  7394. unsigned PriorGeneration;
  7395. unsigned InstanceBits = 0;
  7396. unsigned FactoryBits = 0;
  7397. bool InstanceHasMoreThanOneDecl = false;
  7398. bool FactoryHasMoreThanOneDecl = false;
  7399. SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
  7400. SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
  7401. public:
  7402. ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
  7403. unsigned PriorGeneration)
  7404. : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
  7405. bool operator()(ModuleFile &M) {
  7406. if (!M.SelectorLookupTable)
  7407. return false;
  7408. // If we've already searched this module file, skip it now.
  7409. if (M.Generation <= PriorGeneration)
  7410. return true;
  7411. ++Reader.NumMethodPoolTableLookups;
  7412. ASTSelectorLookupTable *PoolTable
  7413. = (ASTSelectorLookupTable*)M.SelectorLookupTable;
  7414. ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
  7415. if (Pos == PoolTable->end())
  7416. return false;
  7417. ++Reader.NumMethodPoolTableHits;
  7418. ++Reader.NumSelectorsRead;
  7419. // FIXME: Not quite happy with the statistics here. We probably should
  7420. // disable this tracking when called via LoadSelector.
  7421. // Also, should entries without methods count as misses?
  7422. ++Reader.NumMethodPoolEntriesRead;
  7423. ASTSelectorLookupTrait::data_type Data = *Pos;
  7424. if (Reader.DeserializationListener)
  7425. Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
  7426. InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
  7427. FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
  7428. InstanceBits = Data.InstanceBits;
  7429. FactoryBits = Data.FactoryBits;
  7430. InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
  7431. FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
  7432. return true;
  7433. }
  7434. /// Retrieve the instance methods found by this visitor.
  7435. ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
  7436. return InstanceMethods;
  7437. }
  7438. /// Retrieve the instance methods found by this visitor.
  7439. ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
  7440. return FactoryMethods;
  7441. }
  7442. unsigned getInstanceBits() const { return InstanceBits; }
  7443. unsigned getFactoryBits() const { return FactoryBits; }
  7444. bool instanceHasMoreThanOneDecl() const {
  7445. return InstanceHasMoreThanOneDecl;
  7446. }
  7447. bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
  7448. };
  7449. } // namespace serialization
  7450. } // namespace clang
  7451. /// Add the given set of methods to the method list.
  7452. static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
  7453. ObjCMethodList &List) {
  7454. for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
  7455. S.addMethodToGlobalList(&List, Methods[I]);
  7456. }
  7457. }
  7458. void ASTReader::ReadMethodPool(Selector Sel) {
  7459. // Get the selector generation and update it to the current generation.
  7460. unsigned &Generation = SelectorGeneration[Sel];
  7461. unsigned PriorGeneration = Generation;
  7462. Generation = getGeneration();
  7463. SelectorOutOfDate[Sel] = false;
  7464. // Search for methods defined with this selector.
  7465. ++NumMethodPoolLookups;
  7466. ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
  7467. ModuleMgr.visit(Visitor);
  7468. if (Visitor.getInstanceMethods().empty() &&
  7469. Visitor.getFactoryMethods().empty())
  7470. return;
  7471. ++NumMethodPoolHits;
  7472. if (!getSema())
  7473. return;
  7474. Sema &S = *getSema();
  7475. Sema::GlobalMethodPool::iterator Pos
  7476. = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
  7477. Pos->second.first.setBits(Visitor.getInstanceBits());
  7478. Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
  7479. Pos->second.second.setBits(Visitor.getFactoryBits());
  7480. Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
  7481. // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
  7482. // when building a module we keep every method individually and may need to
  7483. // update hasMoreThanOneDecl as we add the methods.
  7484. addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
  7485. addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
  7486. }
  7487. void ASTReader::updateOutOfDateSelector(Selector Sel) {
  7488. if (SelectorOutOfDate[Sel])
  7489. ReadMethodPool(Sel);
  7490. }
  7491. void ASTReader::ReadKnownNamespaces(
  7492. SmallVectorImpl<NamespaceDecl *> &Namespaces) {
  7493. Namespaces.clear();
  7494. for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
  7495. if (NamespaceDecl *Namespace
  7496. = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
  7497. Namespaces.push_back(Namespace);
  7498. }
  7499. }
  7500. void ASTReader::ReadUndefinedButUsed(
  7501. llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
  7502. for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
  7503. NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
  7504. SourceLocation Loc =
  7505. SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
  7506. Undefined.insert(std::make_pair(D, Loc));
  7507. }
  7508. }
  7509. void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
  7510. FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
  7511. Exprs) {
  7512. for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
  7513. FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
  7514. uint64_t Count = DelayedDeleteExprs[Idx++];
  7515. for (uint64_t C = 0; C < Count; ++C) {
  7516. SourceLocation DeleteLoc =
  7517. SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
  7518. const bool IsArrayForm = DelayedDeleteExprs[Idx++];
  7519. Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
  7520. }
  7521. }
  7522. }
  7523. void ASTReader::ReadTentativeDefinitions(
  7524. SmallVectorImpl<VarDecl *> &TentativeDefs) {
  7525. for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
  7526. VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
  7527. if (Var)
  7528. TentativeDefs.push_back(Var);
  7529. }
  7530. TentativeDefinitions.clear();
  7531. }
  7532. void ASTReader::ReadUnusedFileScopedDecls(
  7533. SmallVectorImpl<const DeclaratorDecl *> &Decls) {
  7534. for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
  7535. DeclaratorDecl *D
  7536. = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
  7537. if (D)
  7538. Decls.push_back(D);
  7539. }
  7540. UnusedFileScopedDecls.clear();
  7541. }
  7542. void ASTReader::ReadDelegatingConstructors(
  7543. SmallVectorImpl<CXXConstructorDecl *> &Decls) {
  7544. for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
  7545. CXXConstructorDecl *D
  7546. = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
  7547. if (D)
  7548. Decls.push_back(D);
  7549. }
  7550. DelegatingCtorDecls.clear();
  7551. }
  7552. void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
  7553. for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
  7554. TypedefNameDecl *D
  7555. = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
  7556. if (D)
  7557. Decls.push_back(D);
  7558. }
  7559. ExtVectorDecls.clear();
  7560. }
  7561. void ASTReader::ReadUnusedLocalTypedefNameCandidates(
  7562. llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
  7563. for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
  7564. ++I) {
  7565. TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
  7566. GetDecl(UnusedLocalTypedefNameCandidates[I]));
  7567. if (D)
  7568. Decls.insert(D);
  7569. }
  7570. UnusedLocalTypedefNameCandidates.clear();
  7571. }
  7572. void ASTReader::ReadReferencedSelectors(
  7573. SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
  7574. if (ReferencedSelectorsData.empty())
  7575. return;
  7576. // If there are @selector references added them to its pool. This is for
  7577. // implementation of -Wselector.
  7578. unsigned int DataSize = ReferencedSelectorsData.size()-1;
  7579. unsigned I = 0;
  7580. while (I < DataSize) {
  7581. Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
  7582. SourceLocation SelLoc
  7583. = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
  7584. Sels.push_back(std::make_pair(Sel, SelLoc));
  7585. }
  7586. ReferencedSelectorsData.clear();
  7587. }
  7588. void ASTReader::ReadWeakUndeclaredIdentifiers(
  7589. SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
  7590. if (WeakUndeclaredIdentifiers.empty())
  7591. return;
  7592. for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
  7593. IdentifierInfo *WeakId
  7594. = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
  7595. IdentifierInfo *AliasId
  7596. = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
  7597. SourceLocation Loc
  7598. = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
  7599. bool Used = WeakUndeclaredIdentifiers[I++];
  7600. WeakInfo WI(AliasId, Loc);
  7601. WI.setUsed(Used);
  7602. WeakIDs.push_back(std::make_pair(WeakId, WI));
  7603. }
  7604. WeakUndeclaredIdentifiers.clear();
  7605. }
  7606. void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
  7607. for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
  7608. ExternalVTableUse VT;
  7609. VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
  7610. VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
  7611. VT.DefinitionRequired = VTableUses[Idx++];
  7612. VTables.push_back(VT);
  7613. }
  7614. VTableUses.clear();
  7615. }
  7616. void ASTReader::ReadPendingInstantiations(
  7617. SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
  7618. for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
  7619. ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
  7620. SourceLocation Loc
  7621. = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
  7622. Pending.push_back(std::make_pair(D, Loc));
  7623. }
  7624. PendingInstantiations.clear();
  7625. }
  7626. void ASTReader::ReadLateParsedTemplates(
  7627. llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
  7628. &LPTMap) {
  7629. for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
  7630. /* In loop */) {
  7631. FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
  7632. auto LT = std::make_unique<LateParsedTemplate>();
  7633. LT->D = GetDecl(LateParsedTemplates[Idx++]);
  7634. ModuleFile *F = getOwningModuleFile(LT->D);
  7635. assert(F && "No module");
  7636. unsigned TokN = LateParsedTemplates[Idx++];
  7637. LT->Toks.reserve(TokN);
  7638. for (unsigned T = 0; T < TokN; ++T)
  7639. LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
  7640. LPTMap.insert(std::make_pair(FD, std::move(LT)));
  7641. }
  7642. LateParsedTemplates.clear();
  7643. }
  7644. void ASTReader::LoadSelector(Selector Sel) {
  7645. // It would be complicated to avoid reading the methods anyway. So don't.
  7646. ReadMethodPool(Sel);
  7647. }
  7648. void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
  7649. assert(ID && "Non-zero identifier ID required");
  7650. assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
  7651. IdentifiersLoaded[ID - 1] = II;
  7652. if (DeserializationListener)
  7653. DeserializationListener->IdentifierRead(ID, II);
  7654. }
  7655. /// Set the globally-visible declarations associated with the given
  7656. /// identifier.
  7657. ///
  7658. /// If the AST reader is currently in a state where the given declaration IDs
  7659. /// cannot safely be resolved, they are queued until it is safe to resolve
  7660. /// them.
  7661. ///
  7662. /// \param II an IdentifierInfo that refers to one or more globally-visible
  7663. /// declarations.
  7664. ///
  7665. /// \param DeclIDs the set of declaration IDs with the name @p II that are
  7666. /// visible at global scope.
  7667. ///
  7668. /// \param Decls if non-null, this vector will be populated with the set of
  7669. /// deserialized declarations. These declarations will not be pushed into
  7670. /// scope.
  7671. void
  7672. ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
  7673. const SmallVectorImpl<uint32_t> &DeclIDs,
  7674. SmallVectorImpl<Decl *> *Decls) {
  7675. if (NumCurrentElementsDeserializing && !Decls) {
  7676. PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
  7677. return;
  7678. }
  7679. for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
  7680. if (!SemaObj) {
  7681. // Queue this declaration so that it will be added to the
  7682. // translation unit scope and identifier's declaration chain
  7683. // once a Sema object is known.
  7684. PreloadedDeclIDs.push_back(DeclIDs[I]);
  7685. continue;
  7686. }
  7687. NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
  7688. // If we're simply supposed to record the declarations, do so now.
  7689. if (Decls) {
  7690. Decls->push_back(D);
  7691. continue;
  7692. }
  7693. // Introduce this declaration into the translation-unit scope
  7694. // and add it to the declaration chain for this identifier, so
  7695. // that (unqualified) name lookup will find it.
  7696. pushExternalDeclIntoScope(D, II);
  7697. }
  7698. }
  7699. IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
  7700. if (ID == 0)
  7701. return nullptr;
  7702. if (IdentifiersLoaded.empty()) {
  7703. Error("no identifier table in AST file");
  7704. return nullptr;
  7705. }
  7706. ID -= 1;
  7707. if (!IdentifiersLoaded[ID]) {
  7708. GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
  7709. assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
  7710. ModuleFile *M = I->second;
  7711. unsigned Index = ID - M->BaseIdentifierID;
  7712. const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
  7713. // All of the strings in the AST file are preceded by a 16-bit length.
  7714. // Extract that 16-bit length to avoid having to execute strlen().
  7715. // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
  7716. // unsigned integers. This is important to avoid integer overflow when
  7717. // we cast them to 'unsigned'.
  7718. const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
  7719. unsigned StrLen = (((unsigned) StrLenPtr[0])
  7720. | (((unsigned) StrLenPtr[1]) << 8)) - 1;
  7721. auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
  7722. IdentifiersLoaded[ID] = &II;
  7723. markIdentifierFromAST(*this, II);
  7724. if (DeserializationListener)
  7725. DeserializationListener->IdentifierRead(ID + 1, &II);
  7726. }
  7727. return IdentifiersLoaded[ID];
  7728. }
  7729. IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
  7730. return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
  7731. }
  7732. IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
  7733. if (LocalID < NUM_PREDEF_IDENT_IDS)
  7734. return LocalID;
  7735. if (!M.ModuleOffsetMap.empty())
  7736. ReadModuleOffsetMap(M);
  7737. ContinuousRangeMap<uint32_t, int, 2>::iterator I
  7738. = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
  7739. assert(I != M.IdentifierRemap.end()
  7740. && "Invalid index into identifier index remap");
  7741. return LocalID + I->second;
  7742. }
  7743. MacroInfo *ASTReader::getMacro(MacroID ID) {
  7744. if (ID == 0)
  7745. return nullptr;
  7746. if (MacrosLoaded.empty()) {
  7747. Error("no macro table in AST file");
  7748. return nullptr;
  7749. }
  7750. ID -= NUM_PREDEF_MACRO_IDS;
  7751. if (!MacrosLoaded[ID]) {
  7752. GlobalMacroMapType::iterator I
  7753. = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
  7754. assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
  7755. ModuleFile *M = I->second;
  7756. unsigned Index = ID - M->BaseMacroID;
  7757. MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
  7758. if (DeserializationListener)
  7759. DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
  7760. MacrosLoaded[ID]);
  7761. }
  7762. return MacrosLoaded[ID];
  7763. }
  7764. MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
  7765. if (LocalID < NUM_PREDEF_MACRO_IDS)
  7766. return LocalID;
  7767. if (!M.ModuleOffsetMap.empty())
  7768. ReadModuleOffsetMap(M);
  7769. ContinuousRangeMap<uint32_t, int, 2>::iterator I
  7770. = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
  7771. assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
  7772. return LocalID + I->second;
  7773. }
  7774. serialization::SubmoduleID
  7775. ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
  7776. if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
  7777. return LocalID;
  7778. if (!M.ModuleOffsetMap.empty())
  7779. ReadModuleOffsetMap(M);
  7780. ContinuousRangeMap<uint32_t, int, 2>::iterator I
  7781. = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
  7782. assert(I != M.SubmoduleRemap.end()
  7783. && "Invalid index into submodule index remap");
  7784. return LocalID + I->second;
  7785. }
  7786. Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
  7787. if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
  7788. assert(GlobalID == 0 && "Unhandled global submodule ID");
  7789. return nullptr;
  7790. }
  7791. if (GlobalID > SubmodulesLoaded.size()) {
  7792. Error("submodule ID out of range in AST file");
  7793. return nullptr;
  7794. }
  7795. return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
  7796. }
  7797. Module *ASTReader::getModule(unsigned ID) {
  7798. return getSubmodule(ID);
  7799. }
  7800. bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
  7801. ModuleFile *MF = getOwningModuleFile(D);
  7802. return MF && MF->PCHHasObjectFile;
  7803. }
  7804. ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
  7805. if (ID & 1) {
  7806. // It's a module, look it up by submodule ID.
  7807. auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
  7808. return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
  7809. } else {
  7810. // It's a prefix (preamble, PCH, ...). Look it up by index.
  7811. unsigned IndexFromEnd = ID >> 1;
  7812. assert(IndexFromEnd && "got reference to unknown module file");
  7813. return getModuleManager().pch_modules().end()[-IndexFromEnd];
  7814. }
  7815. }
  7816. unsigned ASTReader::getModuleFileID(ModuleFile *F) {
  7817. if (!F)
  7818. return 1;
  7819. // For a file representing a module, use the submodule ID of the top-level
  7820. // module as the file ID. For any other kind of file, the number of such
  7821. // files loaded beforehand will be the same on reload.
  7822. // FIXME: Is this true even if we have an explicit module file and a PCH?
  7823. if (F->isModule())
  7824. return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
  7825. auto PCHModules = getModuleManager().pch_modules();
  7826. auto I = llvm::find(PCHModules, F);
  7827. assert(I != PCHModules.end() && "emitting reference to unknown file");
  7828. return (I - PCHModules.end()) << 1;
  7829. }
  7830. llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
  7831. ASTReader::getSourceDescriptor(unsigned ID) {
  7832. if (const Module *M = getSubmodule(ID))
  7833. return ExternalASTSource::ASTSourceDescriptor(*M);
  7834. // If there is only a single PCH, return it instead.
  7835. // Chained PCH are not supported.
  7836. const auto &PCHChain = ModuleMgr.pch_modules();
  7837. if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
  7838. ModuleFile &MF = ModuleMgr.getPrimaryModule();
  7839. StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
  7840. StringRef FileName = llvm::sys::path::filename(MF.FileName);
  7841. return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
  7842. MF.Signature);
  7843. }
  7844. return None;
  7845. }
  7846. ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
  7847. auto I = DefinitionSource.find(FD);
  7848. if (I == DefinitionSource.end())
  7849. return EK_ReplyHazy;
  7850. return I->second ? EK_Never : EK_Always;
  7851. }
  7852. Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
  7853. return DecodeSelector(getGlobalSelectorID(M, LocalID));
  7854. }
  7855. Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
  7856. if (ID == 0)
  7857. return Selector();
  7858. if (ID > SelectorsLoaded.size()) {
  7859. Error("selector ID out of range in AST file");
  7860. return Selector();
  7861. }
  7862. if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
  7863. // Load this selector from the selector table.
  7864. GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
  7865. assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
  7866. ModuleFile &M = *I->second;
  7867. ASTSelectorLookupTrait Trait(*this, M);
  7868. unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
  7869. SelectorsLoaded[ID - 1] =
  7870. Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
  7871. if (DeserializationListener)
  7872. DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
  7873. }
  7874. return SelectorsLoaded[ID - 1];
  7875. }
  7876. Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
  7877. return DecodeSelector(ID);
  7878. }
  7879. uint32_t ASTReader::GetNumExternalSelectors() {
  7880. // ID 0 (the null selector) is considered an external selector.
  7881. return getTotalNumSelectors() + 1;
  7882. }
  7883. serialization::SelectorID
  7884. ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
  7885. if (LocalID < NUM_PREDEF_SELECTOR_IDS)
  7886. return LocalID;
  7887. if (!M.ModuleOffsetMap.empty())
  7888. ReadModuleOffsetMap(M);
  7889. ContinuousRangeMap<uint32_t, int, 2>::iterator I
  7890. = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
  7891. assert(I != M.SelectorRemap.end()
  7892. && "Invalid index into selector index remap");
  7893. return LocalID + I->second;
  7894. }
  7895. DeclarationName
  7896. ASTReader::ReadDeclarationName(ModuleFile &F,
  7897. const RecordData &Record, unsigned &Idx) {
  7898. ASTContext &Context = getContext();
  7899. DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
  7900. switch (Kind) {
  7901. case DeclarationName::Identifier:
  7902. return DeclarationName(GetIdentifierInfo(F, Record, Idx));
  7903. case DeclarationName::ObjCZeroArgSelector:
  7904. case DeclarationName::ObjCOneArgSelector:
  7905. case DeclarationName::ObjCMultiArgSelector:
  7906. return DeclarationName(ReadSelector(F, Record, Idx));
  7907. case DeclarationName::CXXConstructorName:
  7908. return Context.DeclarationNames.getCXXConstructorName(
  7909. Context.getCanonicalType(readType(F, Record, Idx)));
  7910. case DeclarationName::CXXDestructorName:
  7911. return Context.DeclarationNames.getCXXDestructorName(
  7912. Context.getCanonicalType(readType(F, Record, Idx)));
  7913. case DeclarationName::CXXDeductionGuideName:
  7914. return Context.DeclarationNames.getCXXDeductionGuideName(
  7915. ReadDeclAs<TemplateDecl>(F, Record, Idx));
  7916. case DeclarationName::CXXConversionFunctionName:
  7917. return Context.DeclarationNames.getCXXConversionFunctionName(
  7918. Context.getCanonicalType(readType(F, Record, Idx)));
  7919. case DeclarationName::CXXOperatorName:
  7920. return Context.DeclarationNames.getCXXOperatorName(
  7921. (OverloadedOperatorKind)Record[Idx++]);
  7922. case DeclarationName::CXXLiteralOperatorName:
  7923. return Context.DeclarationNames.getCXXLiteralOperatorName(
  7924. GetIdentifierInfo(F, Record, Idx));
  7925. case DeclarationName::CXXUsingDirective:
  7926. return DeclarationName::getUsingDirectiveName();
  7927. }
  7928. llvm_unreachable("Invalid NameKind!");
  7929. }
  7930. void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
  7931. DeclarationNameLoc &DNLoc,
  7932. DeclarationName Name,
  7933. const RecordData &Record, unsigned &Idx) {
  7934. switch (Name.getNameKind()) {
  7935. case DeclarationName::CXXConstructorName:
  7936. case DeclarationName::CXXDestructorName:
  7937. case DeclarationName::CXXConversionFunctionName:
  7938. DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
  7939. break;
  7940. case DeclarationName::CXXOperatorName:
  7941. DNLoc.CXXOperatorName.BeginOpNameLoc
  7942. = ReadSourceLocation(F, Record, Idx).getRawEncoding();
  7943. DNLoc.CXXOperatorName.EndOpNameLoc
  7944. = ReadSourceLocation(F, Record, Idx).getRawEncoding();
  7945. break;
  7946. case DeclarationName::CXXLiteralOperatorName:
  7947. DNLoc.CXXLiteralOperatorName.OpNameLoc
  7948. = ReadSourceLocation(F, Record, Idx).getRawEncoding();
  7949. break;
  7950. case DeclarationName::Identifier:
  7951. case DeclarationName::ObjCZeroArgSelector:
  7952. case DeclarationName::ObjCOneArgSelector:
  7953. case DeclarationName::ObjCMultiArgSelector:
  7954. case DeclarationName::CXXUsingDirective:
  7955. case DeclarationName::CXXDeductionGuideName:
  7956. break;
  7957. }
  7958. }
  7959. void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
  7960. DeclarationNameInfo &NameInfo,
  7961. const RecordData &Record, unsigned &Idx) {
  7962. NameInfo.setName(ReadDeclarationName(F, Record, Idx));
  7963. NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
  7964. DeclarationNameLoc DNLoc;
  7965. ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
  7966. NameInfo.setInfo(DNLoc);
  7967. }
  7968. void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
  7969. const RecordData &Record, unsigned &Idx) {
  7970. Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
  7971. unsigned NumTPLists = Record[Idx++];
  7972. Info.NumTemplParamLists = NumTPLists;
  7973. if (NumTPLists) {
  7974. Info.TemplParamLists =
  7975. new (getContext()) TemplateParameterList *[NumTPLists];
  7976. for (unsigned i = 0; i != NumTPLists; ++i)
  7977. Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
  7978. }
  7979. }
  7980. TemplateName
  7981. ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
  7982. unsigned &Idx) {
  7983. ASTContext &Context = getContext();
  7984. TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
  7985. switch (Kind) {
  7986. case TemplateName::Template:
  7987. return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
  7988. case TemplateName::OverloadedTemplate: {
  7989. unsigned size = Record[Idx++];
  7990. UnresolvedSet<8> Decls;
  7991. while (size--)
  7992. Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
  7993. return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
  7994. }
  7995. case TemplateName::AssumedTemplate: {
  7996. DeclarationName Name = ReadDeclarationName(F, Record, Idx);
  7997. return Context.getAssumedTemplateName(Name);
  7998. }
  7999. case TemplateName::QualifiedTemplate: {
  8000. NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
  8001. bool hasTemplKeyword = Record[Idx++];
  8002. TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
  8003. return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
  8004. }
  8005. case TemplateName::DependentTemplate: {
  8006. NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
  8007. if (Record[Idx++]) // isIdentifier
  8008. return Context.getDependentTemplateName(NNS,
  8009. GetIdentifierInfo(F, Record,
  8010. Idx));
  8011. return Context.getDependentTemplateName(NNS,
  8012. (OverloadedOperatorKind)Record[Idx++]);
  8013. }
  8014. case TemplateName::SubstTemplateTemplateParm: {
  8015. TemplateTemplateParmDecl *param
  8016. = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
  8017. if (!param) return TemplateName();
  8018. TemplateName replacement = ReadTemplateName(F, Record, Idx);
  8019. return Context.getSubstTemplateTemplateParm(param, replacement);
  8020. }
  8021. case TemplateName::SubstTemplateTemplateParmPack: {
  8022. TemplateTemplateParmDecl *Param
  8023. = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
  8024. if (!Param)
  8025. return TemplateName();
  8026. TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
  8027. if (ArgPack.getKind() != TemplateArgument::Pack)
  8028. return TemplateName();
  8029. return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
  8030. }
  8031. }
  8032. llvm_unreachable("Unhandled template name kind!");
  8033. }
  8034. TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
  8035. const RecordData &Record,
  8036. unsigned &Idx,
  8037. bool Canonicalize) {
  8038. ASTContext &Context = getContext();
  8039. if (Canonicalize) {
  8040. // The caller wants a canonical template argument. Sometimes the AST only
  8041. // wants template arguments in canonical form (particularly as the template
  8042. // argument lists of template specializations) so ensure we preserve that
  8043. // canonical form across serialization.
  8044. TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
  8045. return Context.getCanonicalTemplateArgument(Arg);
  8046. }
  8047. TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
  8048. switch (Kind) {
  8049. case TemplateArgument::Null:
  8050. return TemplateArgument();
  8051. case TemplateArgument::Type:
  8052. return TemplateArgument(readType(F, Record, Idx));
  8053. case TemplateArgument::Declaration: {
  8054. ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
  8055. return TemplateArgument(D, readType(F, Record, Idx));
  8056. }
  8057. case TemplateArgument::NullPtr:
  8058. return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
  8059. case TemplateArgument::Integral: {
  8060. llvm::APSInt Value = ReadAPSInt(Record, Idx);
  8061. QualType T = readType(F, Record, Idx);
  8062. return TemplateArgument(Context, Value, T);
  8063. }
  8064. case TemplateArgument::Template:
  8065. return TemplateArgument(ReadTemplateName(F, Record, Idx));
  8066. case TemplateArgument::TemplateExpansion: {
  8067. TemplateName Name = ReadTemplateName(F, Record, Idx);
  8068. Optional<unsigned> NumTemplateExpansions;
  8069. if (unsigned NumExpansions = Record[Idx++])
  8070. NumTemplateExpansions = NumExpansions - 1;
  8071. return TemplateArgument(Name, NumTemplateExpansions);
  8072. }
  8073. case TemplateArgument::Expression:
  8074. return TemplateArgument(ReadExpr(F));
  8075. case TemplateArgument::Pack: {
  8076. unsigned NumArgs = Record[Idx++];
  8077. TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
  8078. for (unsigned I = 0; I != NumArgs; ++I)
  8079. Args[I] = ReadTemplateArgument(F, Record, Idx);
  8080. return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
  8081. }
  8082. }
  8083. llvm_unreachable("Unhandled template argument kind!");
  8084. }
  8085. TemplateParameterList *
  8086. ASTReader::ReadTemplateParameterList(ModuleFile &F,
  8087. const RecordData &Record, unsigned &Idx) {
  8088. SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
  8089. SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
  8090. SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
  8091. unsigned NumParams = Record[Idx++];
  8092. SmallVector<NamedDecl *, 16> Params;
  8093. Params.reserve(NumParams);
  8094. while (NumParams--)
  8095. Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
  8096. bool HasRequiresClause = Record[Idx++];
  8097. Expr *RequiresClause = HasRequiresClause ? ReadExpr(F) : nullptr;
  8098. TemplateParameterList *TemplateParams = TemplateParameterList::Create(
  8099. getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
  8100. return TemplateParams;
  8101. }
  8102. void
  8103. ASTReader::
  8104. ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
  8105. ModuleFile &F, const RecordData &Record,
  8106. unsigned &Idx, bool Canonicalize) {
  8107. unsigned NumTemplateArgs = Record[Idx++];
  8108. TemplArgs.reserve(NumTemplateArgs);
  8109. while (NumTemplateArgs--)
  8110. TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
  8111. }
  8112. /// Read a UnresolvedSet structure.
  8113. void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
  8114. const RecordData &Record, unsigned &Idx) {
  8115. unsigned NumDecls = Record[Idx++];
  8116. Set.reserve(getContext(), NumDecls);
  8117. while (NumDecls--) {
  8118. DeclID ID = ReadDeclID(F, Record, Idx);
  8119. AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
  8120. Set.addLazyDecl(getContext(), ID, AS);
  8121. }
  8122. }
  8123. CXXBaseSpecifier
  8124. ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
  8125. const RecordData &Record, unsigned &Idx) {
  8126. bool isVirtual = static_cast<bool>(Record[Idx++]);
  8127. bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
  8128. AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
  8129. bool inheritConstructors = static_cast<bool>(Record[Idx++]);
  8130. TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
  8131. SourceRange Range = ReadSourceRange(F, Record, Idx);
  8132. SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
  8133. CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
  8134. EllipsisLoc);
  8135. Result.setInheritConstructors(inheritConstructors);
  8136. return Result;
  8137. }
  8138. CXXCtorInitializer **
  8139. ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
  8140. unsigned &Idx) {
  8141. ASTContext &Context = getContext();
  8142. unsigned NumInitializers = Record[Idx++];
  8143. assert(NumInitializers && "wrote ctor initializers but have no inits");
  8144. auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
  8145. for (unsigned i = 0; i != NumInitializers; ++i) {
  8146. TypeSourceInfo *TInfo = nullptr;
  8147. bool IsBaseVirtual = false;
  8148. FieldDecl *Member = nullptr;
  8149. IndirectFieldDecl *IndirectMember = nullptr;
  8150. CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
  8151. switch (Type) {
  8152. case CTOR_INITIALIZER_BASE:
  8153. TInfo = GetTypeSourceInfo(F, Record, Idx);
  8154. IsBaseVirtual = Record[Idx++];
  8155. break;
  8156. case CTOR_INITIALIZER_DELEGATING:
  8157. TInfo = GetTypeSourceInfo(F, Record, Idx);
  8158. break;
  8159. case CTOR_INITIALIZER_MEMBER:
  8160. Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
  8161. break;
  8162. case CTOR_INITIALIZER_INDIRECT_MEMBER:
  8163. IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
  8164. break;
  8165. }
  8166. SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
  8167. Expr *Init = ReadExpr(F);
  8168. SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
  8169. SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
  8170. CXXCtorInitializer *BOMInit;
  8171. if (Type == CTOR_INITIALIZER_BASE)
  8172. BOMInit = new (Context)
  8173. CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
  8174. RParenLoc, MemberOrEllipsisLoc);
  8175. else if (Type == CTOR_INITIALIZER_DELEGATING)
  8176. BOMInit = new (Context)
  8177. CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
  8178. else if (Member)
  8179. BOMInit = new (Context)
  8180. CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
  8181. Init, RParenLoc);
  8182. else
  8183. BOMInit = new (Context)
  8184. CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
  8185. LParenLoc, Init, RParenLoc);
  8186. if (/*IsWritten*/Record[Idx++]) {
  8187. unsigned SourceOrder = Record[Idx++];
  8188. BOMInit->setSourceOrder(SourceOrder);
  8189. }
  8190. CtorInitializers[i] = BOMInit;
  8191. }
  8192. return CtorInitializers;
  8193. }
  8194. NestedNameSpecifier *
  8195. ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
  8196. const RecordData &Record, unsigned &Idx) {
  8197. ASTContext &Context = getContext();
  8198. unsigned N = Record[Idx++];
  8199. NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
  8200. for (unsigned I = 0; I != N; ++I) {
  8201. NestedNameSpecifier::SpecifierKind Kind
  8202. = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
  8203. switch (Kind) {
  8204. case NestedNameSpecifier::Identifier: {
  8205. IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
  8206. NNS = NestedNameSpecifier::Create(Context, Prev, II);
  8207. break;
  8208. }
  8209. case NestedNameSpecifier::Namespace: {
  8210. NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
  8211. NNS = NestedNameSpecifier::Create(Context, Prev, NS);
  8212. break;
  8213. }
  8214. case NestedNameSpecifier::NamespaceAlias: {
  8215. NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
  8216. NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
  8217. break;
  8218. }
  8219. case NestedNameSpecifier::TypeSpec:
  8220. case NestedNameSpecifier::TypeSpecWithTemplate: {
  8221. const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
  8222. if (!T)
  8223. return nullptr;
  8224. bool Template = Record[Idx++];
  8225. NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
  8226. break;
  8227. }
  8228. case NestedNameSpecifier::Global:
  8229. NNS = NestedNameSpecifier::GlobalSpecifier(Context);
  8230. // No associated value, and there can't be a prefix.
  8231. break;
  8232. case NestedNameSpecifier::Super: {
  8233. CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
  8234. NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
  8235. break;
  8236. }
  8237. }
  8238. Prev = NNS;
  8239. }
  8240. return NNS;
  8241. }
  8242. NestedNameSpecifierLoc
  8243. ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
  8244. unsigned &Idx) {
  8245. ASTContext &Context = getContext();
  8246. unsigned N = Record[Idx++];
  8247. NestedNameSpecifierLocBuilder Builder;
  8248. for (unsigned I = 0; I != N; ++I) {
  8249. NestedNameSpecifier::SpecifierKind Kind
  8250. = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
  8251. switch (Kind) {
  8252. case NestedNameSpecifier::Identifier: {
  8253. IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
  8254. SourceRange Range = ReadSourceRange(F, Record, Idx);
  8255. Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
  8256. break;
  8257. }
  8258. case NestedNameSpecifier::Namespace: {
  8259. NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
  8260. SourceRange Range = ReadSourceRange(F, Record, Idx);
  8261. Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
  8262. break;
  8263. }
  8264. case NestedNameSpecifier::NamespaceAlias: {
  8265. NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
  8266. SourceRange Range = ReadSourceRange(F, Record, Idx);
  8267. Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
  8268. break;
  8269. }
  8270. case NestedNameSpecifier::TypeSpec:
  8271. case NestedNameSpecifier::TypeSpecWithTemplate: {
  8272. bool Template = Record[Idx++];
  8273. TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
  8274. if (!T)
  8275. return NestedNameSpecifierLoc();
  8276. SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
  8277. // FIXME: 'template' keyword location not saved anywhere, so we fake it.
  8278. Builder.Extend(Context,
  8279. Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
  8280. T->getTypeLoc(), ColonColonLoc);
  8281. break;
  8282. }
  8283. case NestedNameSpecifier::Global: {
  8284. SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
  8285. Builder.MakeGlobal(Context, ColonColonLoc);
  8286. break;
  8287. }
  8288. case NestedNameSpecifier::Super: {
  8289. CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
  8290. SourceRange Range = ReadSourceRange(F, Record, Idx);
  8291. Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
  8292. break;
  8293. }
  8294. }
  8295. }
  8296. return Builder.getWithLocInContext(Context);
  8297. }
  8298. SourceRange
  8299. ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
  8300. unsigned &Idx) {
  8301. SourceLocation beg = ReadSourceLocation(F, Record, Idx);
  8302. SourceLocation end = ReadSourceLocation(F, Record, Idx);
  8303. return SourceRange(beg, end);
  8304. }
  8305. static FixedPointSemantics
  8306. ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
  8307. unsigned &Idx) {
  8308. unsigned Width = Record[Idx++];
  8309. unsigned Scale = Record[Idx++];
  8310. uint64_t Tmp = Record[Idx++];
  8311. bool IsSigned = Tmp & 0x1;
  8312. bool IsSaturated = Tmp & 0x2;
  8313. bool HasUnsignedPadding = Tmp & 0x4;
  8314. return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
  8315. HasUnsignedPadding);
  8316. }
  8317. APValue ASTReader::ReadAPValue(const RecordData &Record, unsigned &Idx) {
  8318. unsigned Kind = Record[Idx++];
  8319. switch (Kind) {
  8320. case APValue::None:
  8321. return APValue();
  8322. case APValue::Indeterminate:
  8323. return APValue::IndeterminateValue();
  8324. case APValue::Int:
  8325. return APValue(ReadAPSInt(Record, Idx));
  8326. case APValue::Float: {
  8327. const llvm::fltSemantics &FloatSema = llvm::APFloatBase::EnumToSemantics(
  8328. static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
  8329. return APValue(ReadAPFloat(Record, FloatSema, Idx));
  8330. }
  8331. case APValue::FixedPoint: {
  8332. FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
  8333. return APValue(APFixedPoint(ReadAPInt(Record, Idx), FPSema));
  8334. }
  8335. case APValue::ComplexInt: {
  8336. llvm::APSInt First = ReadAPSInt(Record, Idx);
  8337. return APValue(std::move(First), ReadAPSInt(Record, Idx));
  8338. }
  8339. case APValue::ComplexFloat: {
  8340. const llvm::fltSemantics &FloatSema1 = llvm::APFloatBase::EnumToSemantics(
  8341. static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
  8342. llvm::APFloat First = ReadAPFloat(Record, FloatSema1, Idx);
  8343. const llvm::fltSemantics &FloatSema2 = llvm::APFloatBase::EnumToSemantics(
  8344. static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
  8345. return APValue(std::move(First), ReadAPFloat(Record, FloatSema2, Idx));
  8346. }
  8347. case APValue::LValue:
  8348. case APValue::Vector:
  8349. case APValue::Array:
  8350. case APValue::Struct:
  8351. case APValue::Union:
  8352. case APValue::MemberPointer:
  8353. case APValue::AddrLabelDiff:
  8354. // TODO : Handle all these APValue::ValueKind.
  8355. return APValue();
  8356. }
  8357. llvm_unreachable("Invalid APValue::ValueKind");
  8358. }
  8359. /// Read an integral value
  8360. llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
  8361. unsigned BitWidth = Record[Idx++];
  8362. unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
  8363. llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
  8364. Idx += NumWords;
  8365. return Result;
  8366. }
  8367. /// Read a signed integral value
  8368. llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
  8369. bool isUnsigned = Record[Idx++];
  8370. return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
  8371. }
  8372. /// Read a floating-point value
  8373. llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
  8374. const llvm::fltSemantics &Sem,
  8375. unsigned &Idx) {
  8376. return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
  8377. }
  8378. // Read a string
  8379. std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
  8380. unsigned Len = Record[Idx++];
  8381. std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
  8382. Idx += Len;
  8383. return Result;
  8384. }
  8385. std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
  8386. unsigned &Idx) {
  8387. std::string Filename = ReadString(Record, Idx);
  8388. ResolveImportedPath(F, Filename);
  8389. return Filename;
  8390. }
  8391. std::string ASTReader::ReadPath(StringRef BaseDirectory,
  8392. const RecordData &Record, unsigned &Idx) {
  8393. std::string Filename = ReadString(Record, Idx);
  8394. if (!BaseDirectory.empty())
  8395. ResolveImportedPath(Filename, BaseDirectory);
  8396. return Filename;
  8397. }
  8398. VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
  8399. unsigned &Idx) {
  8400. unsigned Major = Record[Idx++];
  8401. unsigned Minor = Record[Idx++];
  8402. unsigned Subminor = Record[Idx++];
  8403. if (Minor == 0)
  8404. return VersionTuple(Major);
  8405. if (Subminor == 0)
  8406. return VersionTuple(Major, Minor - 1);
  8407. return VersionTuple(Major, Minor - 1, Subminor - 1);
  8408. }
  8409. CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
  8410. const RecordData &Record,
  8411. unsigned &Idx) {
  8412. CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
  8413. return CXXTemporary::Create(getContext(), Decl);
  8414. }
  8415. DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
  8416. return Diag(CurrentImportLoc, DiagID);
  8417. }
  8418. DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
  8419. return Diags.Report(Loc, DiagID);
  8420. }
  8421. /// Retrieve the identifier table associated with the
  8422. /// preprocessor.
  8423. IdentifierTable &ASTReader::getIdentifierTable() {
  8424. return PP.getIdentifierTable();
  8425. }
  8426. /// Record that the given ID maps to the given switch-case
  8427. /// statement.
  8428. void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
  8429. assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
  8430. "Already have a SwitchCase with this ID");
  8431. (*CurrSwitchCaseStmts)[ID] = SC;
  8432. }
  8433. /// Retrieve the switch-case statement with the given ID.
  8434. SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
  8435. assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
  8436. return (*CurrSwitchCaseStmts)[ID];
  8437. }
  8438. void ASTReader::ClearSwitchCaseIDs() {
  8439. CurrSwitchCaseStmts->clear();
  8440. }
  8441. void ASTReader::ReadComments() {
  8442. ASTContext &Context = getContext();
  8443. std::vector<RawComment *> Comments;
  8444. for (SmallVectorImpl<std::pair<BitstreamCursor,
  8445. serialization::ModuleFile *>>::iterator
  8446. I = CommentsCursors.begin(),
  8447. E = CommentsCursors.end();
  8448. I != E; ++I) {
  8449. Comments.clear();
  8450. BitstreamCursor &Cursor = I->first;
  8451. serialization::ModuleFile &F = *I->second;
  8452. SavedStreamPosition SavedPosition(Cursor);
  8453. RecordData Record;
  8454. while (true) {
  8455. Expected<llvm::BitstreamEntry> MaybeEntry =
  8456. Cursor.advanceSkippingSubblocks(
  8457. BitstreamCursor::AF_DontPopBlockAtEnd);
  8458. if (!MaybeEntry) {
  8459. Error(MaybeEntry.takeError());
  8460. return;
  8461. }
  8462. llvm::BitstreamEntry Entry = MaybeEntry.get();
  8463. switch (Entry.Kind) {
  8464. case llvm::BitstreamEntry::SubBlock: // Handled for us already.
  8465. case llvm::BitstreamEntry::Error:
  8466. Error("malformed block record in AST file");
  8467. return;
  8468. case llvm::BitstreamEntry::EndBlock:
  8469. goto NextCursor;
  8470. case llvm::BitstreamEntry::Record:
  8471. // The interesting case.
  8472. break;
  8473. }
  8474. // Read a record.
  8475. Record.clear();
  8476. Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
  8477. if (!MaybeComment) {
  8478. Error(MaybeComment.takeError());
  8479. return;
  8480. }
  8481. switch ((CommentRecordTypes)MaybeComment.get()) {
  8482. case COMMENTS_RAW_COMMENT: {
  8483. unsigned Idx = 0;
  8484. SourceRange SR = ReadSourceRange(F, Record, Idx);
  8485. RawComment::CommentKind Kind =
  8486. (RawComment::CommentKind) Record[Idx++];
  8487. bool IsTrailingComment = Record[Idx++];
  8488. bool IsAlmostTrailingComment = Record[Idx++];
  8489. Comments.push_back(new (Context) RawComment(
  8490. SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
  8491. break;
  8492. }
  8493. }
  8494. }
  8495. NextCursor:
  8496. llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
  8497. FileToOffsetToComment;
  8498. for (RawComment *C : Comments) {
  8499. SourceLocation CommentLoc = C->getBeginLoc();
  8500. if (CommentLoc.isValid()) {
  8501. std::pair<FileID, unsigned> Loc =
  8502. SourceMgr.getDecomposedLoc(CommentLoc);
  8503. if (Loc.first.isValid())
  8504. Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
  8505. }
  8506. }
  8507. }
  8508. }
  8509. void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
  8510. bool IncludeSystem, bool Complain,
  8511. llvm::function_ref<void(const serialization::InputFile &IF,
  8512. bool isSystem)> Visitor) {
  8513. unsigned NumUserInputs = MF.NumUserInputFiles;
  8514. unsigned NumInputs = MF.InputFilesLoaded.size();
  8515. assert(NumUserInputs <= NumInputs);
  8516. unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
  8517. for (unsigned I = 0; I < N; ++I) {
  8518. bool IsSystem = I >= NumUserInputs;
  8519. InputFile IF = getInputFile(MF, I+1, Complain);
  8520. Visitor(IF, IsSystem);
  8521. }
  8522. }
  8523. void ASTReader::visitTopLevelModuleMaps(
  8524. serialization::ModuleFile &MF,
  8525. llvm::function_ref<void(const FileEntry *FE)> Visitor) {
  8526. unsigned NumInputs = MF.InputFilesLoaded.size();
  8527. for (unsigned I = 0; I < NumInputs; ++I) {
  8528. InputFileInfo IFI = readInputFileInfo(MF, I + 1);
  8529. if (IFI.TopLevelModuleMap)
  8530. // FIXME: This unnecessarily re-reads the InputFileInfo.
  8531. if (auto *FE = getInputFile(MF, I + 1).getFile())
  8532. Visitor(FE);
  8533. }
  8534. }
  8535. std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
  8536. // If we know the owning module, use it.
  8537. if (Module *M = D->getImportedOwningModule())
  8538. return M->getFullModuleName();
  8539. // Otherwise, use the name of the top-level module the decl is within.
  8540. if (ModuleFile *M = getOwningModuleFile(D))
  8541. return M->ModuleName;
  8542. // Not from a module.
  8543. return {};
  8544. }
  8545. void ASTReader::finishPendingActions() {
  8546. while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
  8547. !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
  8548. !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
  8549. !PendingUpdateRecords.empty()) {
  8550. // If any identifiers with corresponding top-level declarations have
  8551. // been loaded, load those declarations now.
  8552. using TopLevelDeclsMap =
  8553. llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
  8554. TopLevelDeclsMap TopLevelDecls;
  8555. while (!PendingIdentifierInfos.empty()) {
  8556. IdentifierInfo *II = PendingIdentifierInfos.back().first;
  8557. SmallVector<uint32_t, 4> DeclIDs =
  8558. std::move(PendingIdentifierInfos.back().second);
  8559. PendingIdentifierInfos.pop_back();
  8560. SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
  8561. }
  8562. // Load each function type that we deferred loading because it was a
  8563. // deduced type that might refer to a local type declared within itself.
  8564. for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
  8565. auto *FD = PendingFunctionTypes[I].first;
  8566. FD->setType(GetType(PendingFunctionTypes[I].second));
  8567. // If we gave a function a deduced return type, remember that we need to
  8568. // propagate that along the redeclaration chain.
  8569. auto *DT = FD->getReturnType()->getContainedDeducedType();
  8570. if (DT && DT->isDeduced())
  8571. PendingDeducedTypeUpdates.insert(
  8572. {FD->getCanonicalDecl(), FD->getReturnType()});
  8573. }
  8574. PendingFunctionTypes.clear();
  8575. // For each decl chain that we wanted to complete while deserializing, mark
  8576. // it as "still needs to be completed".
  8577. for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
  8578. markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
  8579. }
  8580. PendingIncompleteDeclChains.clear();
  8581. // Load pending declaration chains.
  8582. for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
  8583. loadPendingDeclChain(PendingDeclChains[I].first,
  8584. PendingDeclChains[I].second);
  8585. PendingDeclChains.clear();
  8586. // Make the most recent of the top-level declarations visible.
  8587. for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
  8588. TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
  8589. IdentifierInfo *II = TLD->first;
  8590. for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
  8591. pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
  8592. }
  8593. }
  8594. // Load any pending macro definitions.
  8595. for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
  8596. IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
  8597. SmallVector<PendingMacroInfo, 2> GlobalIDs;
  8598. GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
  8599. // Initialize the macro history from chained-PCHs ahead of module imports.
  8600. for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
  8601. ++IDIdx) {
  8602. const PendingMacroInfo &Info = GlobalIDs[IDIdx];
  8603. if (!Info.M->isModule())
  8604. resolvePendingMacro(II, Info);
  8605. }
  8606. // Handle module imports.
  8607. for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
  8608. ++IDIdx) {
  8609. const PendingMacroInfo &Info = GlobalIDs[IDIdx];
  8610. if (Info.M->isModule())
  8611. resolvePendingMacro(II, Info);
  8612. }
  8613. }
  8614. PendingMacroIDs.clear();
  8615. // Wire up the DeclContexts for Decls that we delayed setting until
  8616. // recursive loading is completed.
  8617. while (!PendingDeclContextInfos.empty()) {
  8618. PendingDeclContextInfo Info = PendingDeclContextInfos.front();
  8619. PendingDeclContextInfos.pop_front();
  8620. DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
  8621. DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
  8622. Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
  8623. }
  8624. // Perform any pending declaration updates.
  8625. while (!PendingUpdateRecords.empty()) {
  8626. auto Update = PendingUpdateRecords.pop_back_val();
  8627. ReadingKindTracker ReadingKind(Read_Decl, *this);
  8628. loadDeclUpdateRecords(Update);
  8629. }
  8630. }
  8631. // At this point, all update records for loaded decls are in place, so any
  8632. // fake class definitions should have become real.
  8633. assert(PendingFakeDefinitionData.empty() &&
  8634. "faked up a class definition but never saw the real one");
  8635. // If we deserialized any C++ or Objective-C class definitions, any
  8636. // Objective-C protocol definitions, or any redeclarable templates, make sure
  8637. // that all redeclarations point to the definitions. Note that this can only
  8638. // happen now, after the redeclaration chains have been fully wired.
  8639. for (Decl *D : PendingDefinitions) {
  8640. if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
  8641. if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
  8642. // Make sure that the TagType points at the definition.
  8643. const_cast<TagType*>(TagT)->decl = TD;
  8644. }
  8645. if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
  8646. for (auto *R = getMostRecentExistingDecl(RD); R;
  8647. R = R->getPreviousDecl()) {
  8648. assert((R == D) ==
  8649. cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
  8650. "declaration thinks it's the definition but it isn't");
  8651. cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
  8652. }
  8653. }
  8654. continue;
  8655. }
  8656. if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
  8657. // Make sure that the ObjCInterfaceType points at the definition.
  8658. const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
  8659. ->Decl = ID;
  8660. for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
  8661. cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
  8662. continue;
  8663. }
  8664. if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
  8665. for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
  8666. cast<ObjCProtocolDecl>(R)->Data = PD->Data;
  8667. continue;
  8668. }
  8669. auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
  8670. for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
  8671. cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
  8672. }
  8673. PendingDefinitions.clear();
  8674. // Load the bodies of any functions or methods we've encountered. We do
  8675. // this now (delayed) so that we can be sure that the declaration chains
  8676. // have been fully wired up (hasBody relies on this).
  8677. // FIXME: We shouldn't require complete redeclaration chains here.
  8678. for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
  8679. PBEnd = PendingBodies.end();
  8680. PB != PBEnd; ++PB) {
  8681. if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
  8682. // For a function defined inline within a class template, force the
  8683. // canonical definition to be the one inside the canonical definition of
  8684. // the template. This ensures that we instantiate from a correct view
  8685. // of the template.
  8686. //
  8687. // Sadly we can't do this more generally: we can't be sure that all
  8688. // copies of an arbitrary class definition will have the same members
  8689. // defined (eg, some member functions may not be instantiated, and some
  8690. // special members may or may not have been implicitly defined).
  8691. if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
  8692. if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
  8693. continue;
  8694. // FIXME: Check for =delete/=default?
  8695. // FIXME: Complain about ODR violations here?
  8696. const FunctionDecl *Defn = nullptr;
  8697. if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
  8698. FD->setLazyBody(PB->second);
  8699. } else {
  8700. auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
  8701. mergeDefinitionVisibility(NonConstDefn, FD);
  8702. if (!FD->isLateTemplateParsed() &&
  8703. !NonConstDefn->isLateTemplateParsed() &&
  8704. FD->getODRHash() != NonConstDefn->getODRHash()) {
  8705. if (!isa<CXXMethodDecl>(FD)) {
  8706. PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
  8707. } else if (FD->getLexicalParent()->isFileContext() &&
  8708. NonConstDefn->getLexicalParent()->isFileContext()) {
  8709. // Only diagnose out-of-line method definitions. If they are
  8710. // in class definitions, then an error will be generated when
  8711. // processing the class bodies.
  8712. PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
  8713. }
  8714. }
  8715. }
  8716. continue;
  8717. }
  8718. ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
  8719. if (!getContext().getLangOpts().Modules || !MD->hasBody())
  8720. MD->setLazyBody(PB->second);
  8721. }
  8722. PendingBodies.clear();
  8723. // Do some cleanup.
  8724. for (auto *ND : PendingMergedDefinitionsToDeduplicate)
  8725. getContext().deduplicateMergedDefinitonsFor(ND);
  8726. PendingMergedDefinitionsToDeduplicate.clear();
  8727. }
  8728. void ASTReader::diagnoseOdrViolations() {
  8729. if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
  8730. PendingFunctionOdrMergeFailures.empty() &&
  8731. PendingEnumOdrMergeFailures.empty())
  8732. return;
  8733. // Trigger the import of the full definition of each class that had any
  8734. // odr-merging problems, so we can produce better diagnostics for them.
  8735. // These updates may in turn find and diagnose some ODR failures, so take
  8736. // ownership of the set first.
  8737. auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
  8738. PendingOdrMergeFailures.clear();
  8739. for (auto &Merge : OdrMergeFailures) {
  8740. Merge.first->buildLookup();
  8741. Merge.first->decls_begin();
  8742. Merge.first->bases_begin();
  8743. Merge.first->vbases_begin();
  8744. for (auto &RecordPair : Merge.second) {
  8745. auto *RD = RecordPair.first;
  8746. RD->decls_begin();
  8747. RD->bases_begin();
  8748. RD->vbases_begin();
  8749. }
  8750. }
  8751. // Trigger the import of functions.
  8752. auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
  8753. PendingFunctionOdrMergeFailures.clear();
  8754. for (auto &Merge : FunctionOdrMergeFailures) {
  8755. Merge.first->buildLookup();
  8756. Merge.first->decls_begin();
  8757. Merge.first->getBody();
  8758. for (auto &FD : Merge.second) {
  8759. FD->buildLookup();
  8760. FD->decls_begin();
  8761. FD->getBody();
  8762. }
  8763. }
  8764. // Trigger the import of enums.
  8765. auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
  8766. PendingEnumOdrMergeFailures.clear();
  8767. for (auto &Merge : EnumOdrMergeFailures) {
  8768. Merge.first->decls_begin();
  8769. for (auto &Enum : Merge.second) {
  8770. Enum->decls_begin();
  8771. }
  8772. }
  8773. // For each declaration from a merged context, check that the canonical
  8774. // definition of that context also contains a declaration of the same
  8775. // entity.
  8776. //
  8777. // Caution: this loop does things that might invalidate iterators into
  8778. // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
  8779. while (!PendingOdrMergeChecks.empty()) {
  8780. NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
  8781. // FIXME: Skip over implicit declarations for now. This matters for things
  8782. // like implicitly-declared special member functions. This isn't entirely
  8783. // correct; we can end up with multiple unmerged declarations of the same
  8784. // implicit entity.
  8785. if (D->isImplicit())
  8786. continue;
  8787. DeclContext *CanonDef = D->getDeclContext();
  8788. bool Found = false;
  8789. const Decl *DCanon = D->getCanonicalDecl();
  8790. for (auto RI : D->redecls()) {
  8791. if (RI->getLexicalDeclContext() == CanonDef) {
  8792. Found = true;
  8793. break;
  8794. }
  8795. }
  8796. if (Found)
  8797. continue;
  8798. // Quick check failed, time to do the slow thing. Note, we can't just
  8799. // look up the name of D in CanonDef here, because the member that is
  8800. // in CanonDef might not be found by name lookup (it might have been
  8801. // replaced by a more recent declaration in the lookup table), and we
  8802. // can't necessarily find it in the redeclaration chain because it might
  8803. // be merely mergeable, not redeclarable.
  8804. llvm::SmallVector<const NamedDecl*, 4> Candidates;
  8805. for (auto *CanonMember : CanonDef->decls()) {
  8806. if (CanonMember->getCanonicalDecl() == DCanon) {
  8807. // This can happen if the declaration is merely mergeable and not
  8808. // actually redeclarable (we looked for redeclarations earlier).
  8809. //
  8810. // FIXME: We should be able to detect this more efficiently, without
  8811. // pulling in all of the members of CanonDef.
  8812. Found = true;
  8813. break;
  8814. }
  8815. if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
  8816. if (ND->getDeclName() == D->getDeclName())
  8817. Candidates.push_back(ND);
  8818. }
  8819. if (!Found) {
  8820. // The AST doesn't like TagDecls becoming invalid after they've been
  8821. // completed. We only really need to mark FieldDecls as invalid here.
  8822. if (!isa<TagDecl>(D))
  8823. D->setInvalidDecl();
  8824. // Ensure we don't accidentally recursively enter deserialization while
  8825. // we're producing our diagnostic.
  8826. Deserializing RecursionGuard(this);
  8827. std::string CanonDefModule =
  8828. getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
  8829. Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
  8830. << D << getOwningModuleNameForDiagnostic(D)
  8831. << CanonDef << CanonDefModule.empty() << CanonDefModule;
  8832. if (Candidates.empty())
  8833. Diag(cast<Decl>(CanonDef)->getLocation(),
  8834. diag::note_module_odr_violation_no_possible_decls) << D;
  8835. else {
  8836. for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
  8837. Diag(Candidates[I]->getLocation(),
  8838. diag::note_module_odr_violation_possible_decl)
  8839. << Candidates[I];
  8840. }
  8841. DiagnosedOdrMergeFailures.insert(CanonDef);
  8842. }
  8843. }
  8844. if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
  8845. EnumOdrMergeFailures.empty())
  8846. return;
  8847. // Ensure we don't accidentally recursively enter deserialization while
  8848. // we're producing our diagnostics.
  8849. Deserializing RecursionGuard(this);
  8850. // Common code for hashing helpers.
  8851. ODRHash Hash;
  8852. auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
  8853. Hash.clear();
  8854. Hash.AddQualType(Ty);
  8855. return Hash.CalculateHash();
  8856. };
  8857. auto ComputeODRHash = [&Hash](const Stmt *S) {
  8858. assert(S);
  8859. Hash.clear();
  8860. Hash.AddStmt(S);
  8861. return Hash.CalculateHash();
  8862. };
  8863. auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
  8864. assert(D);
  8865. Hash.clear();
  8866. Hash.AddSubDecl(D);
  8867. return Hash.CalculateHash();
  8868. };
  8869. auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
  8870. Hash.clear();
  8871. Hash.AddTemplateArgument(TA);
  8872. return Hash.CalculateHash();
  8873. };
  8874. auto ComputeTemplateParameterListODRHash =
  8875. [&Hash](const TemplateParameterList *TPL) {
  8876. assert(TPL);
  8877. Hash.clear();
  8878. Hash.AddTemplateParameterList(TPL);
  8879. return Hash.CalculateHash();
  8880. };
  8881. // Issue any pending ODR-failure diagnostics.
  8882. for (auto &Merge : OdrMergeFailures) {
  8883. // If we've already pointed out a specific problem with this class, don't
  8884. // bother issuing a general "something's different" diagnostic.
  8885. if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
  8886. continue;
  8887. bool Diagnosed = false;
  8888. CXXRecordDecl *FirstRecord = Merge.first;
  8889. std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
  8890. for (auto &RecordPair : Merge.second) {
  8891. CXXRecordDecl *SecondRecord = RecordPair.first;
  8892. // Multiple different declarations got merged together; tell the user
  8893. // where they came from.
  8894. if (FirstRecord == SecondRecord)
  8895. continue;
  8896. std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
  8897. auto *FirstDD = FirstRecord->DefinitionData;
  8898. auto *SecondDD = RecordPair.second;
  8899. assert(FirstDD && SecondDD && "Definitions without DefinitionData");
  8900. // Diagnostics from DefinitionData are emitted here.
  8901. if (FirstDD != SecondDD) {
  8902. enum ODRDefinitionDataDifference {
  8903. NumBases,
  8904. NumVBases,
  8905. BaseType,
  8906. BaseVirtual,
  8907. BaseAccess,
  8908. };
  8909. auto ODRDiagError = [FirstRecord, &FirstModule,
  8910. this](SourceLocation Loc, SourceRange Range,
  8911. ODRDefinitionDataDifference DiffType) {
  8912. return Diag(Loc, diag::err_module_odr_violation_definition_data)
  8913. << FirstRecord << FirstModule.empty() << FirstModule << Range
  8914. << DiffType;
  8915. };
  8916. auto ODRDiagNote = [&SecondModule,
  8917. this](SourceLocation Loc, SourceRange Range,
  8918. ODRDefinitionDataDifference DiffType) {
  8919. return Diag(Loc, diag::note_module_odr_violation_definition_data)
  8920. << SecondModule << Range << DiffType;
  8921. };
  8922. unsigned FirstNumBases = FirstDD->NumBases;
  8923. unsigned FirstNumVBases = FirstDD->NumVBases;
  8924. unsigned SecondNumBases = SecondDD->NumBases;
  8925. unsigned SecondNumVBases = SecondDD->NumVBases;
  8926. auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
  8927. unsigned NumBases = DD->NumBases;
  8928. if (NumBases == 0) return SourceRange();
  8929. auto bases = DD->bases();
  8930. return SourceRange(bases[0].getBeginLoc(),
  8931. bases[NumBases - 1].getEndLoc());
  8932. };
  8933. if (FirstNumBases != SecondNumBases) {
  8934. ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
  8935. NumBases)
  8936. << FirstNumBases;
  8937. ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
  8938. NumBases)
  8939. << SecondNumBases;
  8940. Diagnosed = true;
  8941. break;
  8942. }
  8943. if (FirstNumVBases != SecondNumVBases) {
  8944. ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
  8945. NumVBases)
  8946. << FirstNumVBases;
  8947. ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
  8948. NumVBases)
  8949. << SecondNumVBases;
  8950. Diagnosed = true;
  8951. break;
  8952. }
  8953. auto FirstBases = FirstDD->bases();
  8954. auto SecondBases = SecondDD->bases();
  8955. unsigned i = 0;
  8956. for (i = 0; i < FirstNumBases; ++i) {
  8957. auto FirstBase = FirstBases[i];
  8958. auto SecondBase = SecondBases[i];
  8959. if (ComputeQualTypeODRHash(FirstBase.getType()) !=
  8960. ComputeQualTypeODRHash(SecondBase.getType())) {
  8961. ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
  8962. BaseType)
  8963. << (i + 1) << FirstBase.getType();
  8964. ODRDiagNote(SecondRecord->getLocation(),
  8965. SecondBase.getSourceRange(), BaseType)
  8966. << (i + 1) << SecondBase.getType();
  8967. break;
  8968. }
  8969. if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
  8970. ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
  8971. BaseVirtual)
  8972. << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
  8973. ODRDiagNote(SecondRecord->getLocation(),
  8974. SecondBase.getSourceRange(), BaseVirtual)
  8975. << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
  8976. break;
  8977. }
  8978. if (FirstBase.getAccessSpecifierAsWritten() !=
  8979. SecondBase.getAccessSpecifierAsWritten()) {
  8980. ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
  8981. BaseAccess)
  8982. << (i + 1) << FirstBase.getType()
  8983. << (int)FirstBase.getAccessSpecifierAsWritten();
  8984. ODRDiagNote(SecondRecord->getLocation(),
  8985. SecondBase.getSourceRange(), BaseAccess)
  8986. << (i + 1) << SecondBase.getType()
  8987. << (int)SecondBase.getAccessSpecifierAsWritten();
  8988. break;
  8989. }
  8990. }
  8991. if (i != FirstNumBases) {
  8992. Diagnosed = true;
  8993. break;
  8994. }
  8995. }
  8996. using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
  8997. const ClassTemplateDecl *FirstTemplate =
  8998. FirstRecord->getDescribedClassTemplate();
  8999. const ClassTemplateDecl *SecondTemplate =
  9000. SecondRecord->getDescribedClassTemplate();
  9001. assert(!FirstTemplate == !SecondTemplate &&
  9002. "Both pointers should be null or non-null");
  9003. enum ODRTemplateDifference {
  9004. ParamEmptyName,
  9005. ParamName,
  9006. ParamSingleDefaultArgument,
  9007. ParamDifferentDefaultArgument,
  9008. };
  9009. if (FirstTemplate && SecondTemplate) {
  9010. DeclHashes FirstTemplateHashes;
  9011. DeclHashes SecondTemplateHashes;
  9012. auto PopulateTemplateParameterHashs =
  9013. [&ComputeSubDeclODRHash](DeclHashes &Hashes,
  9014. const ClassTemplateDecl *TD) {
  9015. for (auto *D : TD->getTemplateParameters()->asArray()) {
  9016. Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
  9017. }
  9018. };
  9019. PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
  9020. PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
  9021. assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
  9022. "Number of template parameters should be equal.");
  9023. auto FirstIt = FirstTemplateHashes.begin();
  9024. auto FirstEnd = FirstTemplateHashes.end();
  9025. auto SecondIt = SecondTemplateHashes.begin();
  9026. for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
  9027. if (FirstIt->second == SecondIt->second)
  9028. continue;
  9029. auto ODRDiagError = [FirstRecord, &FirstModule,
  9030. this](SourceLocation Loc, SourceRange Range,
  9031. ODRTemplateDifference DiffType) {
  9032. return Diag(Loc, diag::err_module_odr_violation_template_parameter)
  9033. << FirstRecord << FirstModule.empty() << FirstModule << Range
  9034. << DiffType;
  9035. };
  9036. auto ODRDiagNote = [&SecondModule,
  9037. this](SourceLocation Loc, SourceRange Range,
  9038. ODRTemplateDifference DiffType) {
  9039. return Diag(Loc, diag::note_module_odr_violation_template_parameter)
  9040. << SecondModule << Range << DiffType;
  9041. };
  9042. const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
  9043. const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
  9044. assert(FirstDecl->getKind() == SecondDecl->getKind() &&
  9045. "Parameter Decl's should be the same kind.");
  9046. DeclarationName FirstName = FirstDecl->getDeclName();
  9047. DeclarationName SecondName = SecondDecl->getDeclName();
  9048. if (FirstName != SecondName) {
  9049. const bool FirstNameEmpty =
  9050. FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
  9051. const bool SecondNameEmpty =
  9052. SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
  9053. assert((!FirstNameEmpty || !SecondNameEmpty) &&
  9054. "Both template parameters cannot be unnamed.");
  9055. ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
  9056. FirstNameEmpty ? ParamEmptyName : ParamName)
  9057. << FirstName;
  9058. ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
  9059. SecondNameEmpty ? ParamEmptyName : ParamName)
  9060. << SecondName;
  9061. break;
  9062. }
  9063. switch (FirstDecl->getKind()) {
  9064. default:
  9065. llvm_unreachable("Invalid template parameter type.");
  9066. case Decl::TemplateTypeParm: {
  9067. const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
  9068. const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
  9069. const bool HasFirstDefaultArgument =
  9070. FirstParam->hasDefaultArgument() &&
  9071. !FirstParam->defaultArgumentWasInherited();
  9072. const bool HasSecondDefaultArgument =
  9073. SecondParam->hasDefaultArgument() &&
  9074. !SecondParam->defaultArgumentWasInherited();
  9075. if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
  9076. ODRDiagError(FirstDecl->getLocation(),
  9077. FirstDecl->getSourceRange(),
  9078. ParamSingleDefaultArgument)
  9079. << HasFirstDefaultArgument;
  9080. ODRDiagNote(SecondDecl->getLocation(),
  9081. SecondDecl->getSourceRange(),
  9082. ParamSingleDefaultArgument)
  9083. << HasSecondDefaultArgument;
  9084. break;
  9085. }
  9086. assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
  9087. "Expecting default arguments.");
  9088. ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
  9089. ParamDifferentDefaultArgument);
  9090. ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
  9091. ParamDifferentDefaultArgument);
  9092. break;
  9093. }
  9094. case Decl::NonTypeTemplateParm: {
  9095. const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
  9096. const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
  9097. const bool HasFirstDefaultArgument =
  9098. FirstParam->hasDefaultArgument() &&
  9099. !FirstParam->defaultArgumentWasInherited();
  9100. const bool HasSecondDefaultArgument =
  9101. SecondParam->hasDefaultArgument() &&
  9102. !SecondParam->defaultArgumentWasInherited();
  9103. if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
  9104. ODRDiagError(FirstDecl->getLocation(),
  9105. FirstDecl->getSourceRange(),
  9106. ParamSingleDefaultArgument)
  9107. << HasFirstDefaultArgument;
  9108. ODRDiagNote(SecondDecl->getLocation(),
  9109. SecondDecl->getSourceRange(),
  9110. ParamSingleDefaultArgument)
  9111. << HasSecondDefaultArgument;
  9112. break;
  9113. }
  9114. assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
  9115. "Expecting default arguments.");
  9116. ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
  9117. ParamDifferentDefaultArgument);
  9118. ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
  9119. ParamDifferentDefaultArgument);
  9120. break;
  9121. }
  9122. case Decl::TemplateTemplateParm: {
  9123. const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
  9124. const auto *SecondParam =
  9125. cast<TemplateTemplateParmDecl>(SecondDecl);
  9126. const bool HasFirstDefaultArgument =
  9127. FirstParam->hasDefaultArgument() &&
  9128. !FirstParam->defaultArgumentWasInherited();
  9129. const bool HasSecondDefaultArgument =
  9130. SecondParam->hasDefaultArgument() &&
  9131. !SecondParam->defaultArgumentWasInherited();
  9132. if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
  9133. ODRDiagError(FirstDecl->getLocation(),
  9134. FirstDecl->getSourceRange(),
  9135. ParamSingleDefaultArgument)
  9136. << HasFirstDefaultArgument;
  9137. ODRDiagNote(SecondDecl->getLocation(),
  9138. SecondDecl->getSourceRange(),
  9139. ParamSingleDefaultArgument)
  9140. << HasSecondDefaultArgument;
  9141. break;
  9142. }
  9143. assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
  9144. "Expecting default arguments.");
  9145. ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
  9146. ParamDifferentDefaultArgument);
  9147. ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
  9148. ParamDifferentDefaultArgument);
  9149. break;
  9150. }
  9151. }
  9152. break;
  9153. }
  9154. if (FirstIt != FirstEnd) {
  9155. Diagnosed = true;
  9156. break;
  9157. }
  9158. }
  9159. DeclHashes FirstHashes;
  9160. DeclHashes SecondHashes;
  9161. auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord](
  9162. DeclHashes &Hashes, CXXRecordDecl *Record) {
  9163. for (auto *D : Record->decls()) {
  9164. // Due to decl merging, the first CXXRecordDecl is the parent of
  9165. // Decls in both records.
  9166. if (!ODRHash::isWhitelistedDecl(D, FirstRecord))
  9167. continue;
  9168. Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
  9169. }
  9170. };
  9171. PopulateHashes(FirstHashes, FirstRecord);
  9172. PopulateHashes(SecondHashes, SecondRecord);
  9173. // Used with err_module_odr_violation_mismatch_decl and
  9174. // note_module_odr_violation_mismatch_decl
  9175. // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
  9176. enum {
  9177. EndOfClass,
  9178. PublicSpecifer,
  9179. PrivateSpecifer,
  9180. ProtectedSpecifer,
  9181. StaticAssert,
  9182. Field,
  9183. CXXMethod,
  9184. TypeAlias,
  9185. TypeDef,
  9186. Var,
  9187. Friend,
  9188. FunctionTemplate,
  9189. Other
  9190. } FirstDiffType = Other,
  9191. SecondDiffType = Other;
  9192. auto DifferenceSelector = [](Decl *D) {
  9193. assert(D && "valid Decl required");
  9194. switch (D->getKind()) {
  9195. default:
  9196. return Other;
  9197. case Decl::AccessSpec:
  9198. switch (D->getAccess()) {
  9199. case AS_public:
  9200. return PublicSpecifer;
  9201. case AS_private:
  9202. return PrivateSpecifer;
  9203. case AS_protected:
  9204. return ProtectedSpecifer;
  9205. case AS_none:
  9206. break;
  9207. }
  9208. llvm_unreachable("Invalid access specifier");
  9209. case Decl::StaticAssert:
  9210. return StaticAssert;
  9211. case Decl::Field:
  9212. return Field;
  9213. case Decl::CXXMethod:
  9214. case Decl::CXXConstructor:
  9215. case Decl::CXXDestructor:
  9216. return CXXMethod;
  9217. case Decl::TypeAlias:
  9218. return TypeAlias;
  9219. case Decl::Typedef:
  9220. return TypeDef;
  9221. case Decl::Var:
  9222. return Var;
  9223. case Decl::Friend:
  9224. return Friend;
  9225. case Decl::FunctionTemplate:
  9226. return FunctionTemplate;
  9227. }
  9228. };
  9229. Decl *FirstDecl = nullptr;
  9230. Decl *SecondDecl = nullptr;
  9231. auto FirstIt = FirstHashes.begin();
  9232. auto SecondIt = SecondHashes.begin();
  9233. // If there is a diagnoseable difference, FirstDiffType and
  9234. // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
  9235. // filled in if not EndOfClass.
  9236. while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
  9237. if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
  9238. FirstIt->second == SecondIt->second) {
  9239. ++FirstIt;
  9240. ++SecondIt;
  9241. continue;
  9242. }
  9243. FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
  9244. SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
  9245. FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass;
  9246. SecondDiffType =
  9247. SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass;
  9248. break;
  9249. }
  9250. if (FirstDiffType == Other || SecondDiffType == Other) {
  9251. // Reaching this point means an unexpected Decl was encountered
  9252. // or no difference was detected. This causes a generic error
  9253. // message to be emitted.
  9254. Diag(FirstRecord->getLocation(),
  9255. diag::err_module_odr_violation_different_definitions)
  9256. << FirstRecord << FirstModule.empty() << FirstModule;
  9257. if (FirstDecl) {
  9258. Diag(FirstDecl->getLocation(), diag::note_first_module_difference)
  9259. << FirstRecord << FirstDecl->getSourceRange();
  9260. }
  9261. Diag(SecondRecord->getLocation(),
  9262. diag::note_module_odr_violation_different_definitions)
  9263. << SecondModule;
  9264. if (SecondDecl) {
  9265. Diag(SecondDecl->getLocation(), diag::note_second_module_difference)
  9266. << SecondDecl->getSourceRange();
  9267. }
  9268. Diagnosed = true;
  9269. break;
  9270. }
  9271. if (FirstDiffType != SecondDiffType) {
  9272. SourceLocation FirstLoc;
  9273. SourceRange FirstRange;
  9274. if (FirstDiffType == EndOfClass) {
  9275. FirstLoc = FirstRecord->getBraceRange().getEnd();
  9276. } else {
  9277. FirstLoc = FirstIt->first->getLocation();
  9278. FirstRange = FirstIt->first->getSourceRange();
  9279. }
  9280. Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
  9281. << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
  9282. << FirstDiffType;
  9283. SourceLocation SecondLoc;
  9284. SourceRange SecondRange;
  9285. if (SecondDiffType == EndOfClass) {
  9286. SecondLoc = SecondRecord->getBraceRange().getEnd();
  9287. } else {
  9288. SecondLoc = SecondDecl->getLocation();
  9289. SecondRange = SecondDecl->getSourceRange();
  9290. }
  9291. Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
  9292. << SecondModule << SecondRange << SecondDiffType;
  9293. Diagnosed = true;
  9294. break;
  9295. }
  9296. assert(FirstDiffType == SecondDiffType);
  9297. // Used with err_module_odr_violation_mismatch_decl_diff and
  9298. // note_module_odr_violation_mismatch_decl_diff
  9299. enum ODRDeclDifference {
  9300. StaticAssertCondition,
  9301. StaticAssertMessage,
  9302. StaticAssertOnlyMessage,
  9303. FieldName,
  9304. FieldTypeName,
  9305. FieldSingleBitField,
  9306. FieldDifferentWidthBitField,
  9307. FieldSingleMutable,
  9308. FieldSingleInitializer,
  9309. FieldDifferentInitializers,
  9310. MethodName,
  9311. MethodDeleted,
  9312. MethodDefaulted,
  9313. MethodVirtual,
  9314. MethodStatic,
  9315. MethodVolatile,
  9316. MethodConst,
  9317. MethodInline,
  9318. MethodNumberParameters,
  9319. MethodParameterType,
  9320. MethodParameterName,
  9321. MethodParameterSingleDefaultArgument,
  9322. MethodParameterDifferentDefaultArgument,
  9323. MethodNoTemplateArguments,
  9324. MethodDifferentNumberTemplateArguments,
  9325. MethodDifferentTemplateArgument,
  9326. MethodSingleBody,
  9327. MethodDifferentBody,
  9328. TypedefName,
  9329. TypedefType,
  9330. VarName,
  9331. VarType,
  9332. VarSingleInitializer,
  9333. VarDifferentInitializer,
  9334. VarConstexpr,
  9335. FriendTypeFunction,
  9336. FriendType,
  9337. FriendFunction,
  9338. FunctionTemplateDifferentNumberParameters,
  9339. FunctionTemplateParameterDifferentKind,
  9340. FunctionTemplateParameterName,
  9341. FunctionTemplateParameterSingleDefaultArgument,
  9342. FunctionTemplateParameterDifferentDefaultArgument,
  9343. FunctionTemplateParameterDifferentType,
  9344. FunctionTemplatePackParameter,
  9345. };
  9346. // These lambdas have the common portions of the ODR diagnostics. This
  9347. // has the same return as Diag(), so addition parameters can be passed
  9348. // in with operator<<
  9349. auto ODRDiagError = [FirstRecord, &FirstModule, this](
  9350. SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
  9351. return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
  9352. << FirstRecord << FirstModule.empty() << FirstModule << Range
  9353. << DiffType;
  9354. };
  9355. auto ODRDiagNote = [&SecondModule, this](
  9356. SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
  9357. return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
  9358. << SecondModule << Range << DiffType;
  9359. };
  9360. switch (FirstDiffType) {
  9361. case Other:
  9362. case EndOfClass:
  9363. case PublicSpecifer:
  9364. case PrivateSpecifer:
  9365. case ProtectedSpecifer:
  9366. llvm_unreachable("Invalid diff type");
  9367. case StaticAssert: {
  9368. StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
  9369. StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
  9370. Expr *FirstExpr = FirstSA->getAssertExpr();
  9371. Expr *SecondExpr = SecondSA->getAssertExpr();
  9372. unsigned FirstODRHash = ComputeODRHash(FirstExpr);
  9373. unsigned SecondODRHash = ComputeODRHash(SecondExpr);
  9374. if (FirstODRHash != SecondODRHash) {
  9375. ODRDiagError(FirstExpr->getBeginLoc(), FirstExpr->getSourceRange(),
  9376. StaticAssertCondition);
  9377. ODRDiagNote(SecondExpr->getBeginLoc(), SecondExpr->getSourceRange(),
  9378. StaticAssertCondition);
  9379. Diagnosed = true;
  9380. break;
  9381. }
  9382. StringLiteral *FirstStr = FirstSA->getMessage();
  9383. StringLiteral *SecondStr = SecondSA->getMessage();
  9384. assert((FirstStr || SecondStr) && "Both messages cannot be empty");
  9385. if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
  9386. SourceLocation FirstLoc, SecondLoc;
  9387. SourceRange FirstRange, SecondRange;
  9388. if (FirstStr) {
  9389. FirstLoc = FirstStr->getBeginLoc();
  9390. FirstRange = FirstStr->getSourceRange();
  9391. } else {
  9392. FirstLoc = FirstSA->getBeginLoc();
  9393. FirstRange = FirstSA->getSourceRange();
  9394. }
  9395. if (SecondStr) {
  9396. SecondLoc = SecondStr->getBeginLoc();
  9397. SecondRange = SecondStr->getSourceRange();
  9398. } else {
  9399. SecondLoc = SecondSA->getBeginLoc();
  9400. SecondRange = SecondSA->getSourceRange();
  9401. }
  9402. ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage)
  9403. << (FirstStr == nullptr);
  9404. ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage)
  9405. << (SecondStr == nullptr);
  9406. Diagnosed = true;
  9407. break;
  9408. }
  9409. if (FirstStr && SecondStr &&
  9410. FirstStr->getString() != SecondStr->getString()) {
  9411. ODRDiagError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(),
  9412. StaticAssertMessage);
  9413. ODRDiagNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(),
  9414. StaticAssertMessage);
  9415. Diagnosed = true;
  9416. break;
  9417. }
  9418. break;
  9419. }
  9420. case Field: {
  9421. FieldDecl *FirstField = cast<FieldDecl>(FirstDecl);
  9422. FieldDecl *SecondField = cast<FieldDecl>(SecondDecl);
  9423. IdentifierInfo *FirstII = FirstField->getIdentifier();
  9424. IdentifierInfo *SecondII = SecondField->getIdentifier();
  9425. if (FirstII->getName() != SecondII->getName()) {
  9426. ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
  9427. FieldName)
  9428. << FirstII;
  9429. ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
  9430. FieldName)
  9431. << SecondII;
  9432. Diagnosed = true;
  9433. break;
  9434. }
  9435. assert(getContext().hasSameType(FirstField->getType(),
  9436. SecondField->getType()));
  9437. QualType FirstType = FirstField->getType();
  9438. QualType SecondType = SecondField->getType();
  9439. if (ComputeQualTypeODRHash(FirstType) !=
  9440. ComputeQualTypeODRHash(SecondType)) {
  9441. ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
  9442. FieldTypeName)
  9443. << FirstII << FirstType;
  9444. ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
  9445. FieldTypeName)
  9446. << SecondII << SecondType;
  9447. Diagnosed = true;
  9448. break;
  9449. }
  9450. const bool IsFirstBitField = FirstField->isBitField();
  9451. const bool IsSecondBitField = SecondField->isBitField();
  9452. if (IsFirstBitField != IsSecondBitField) {
  9453. ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
  9454. FieldSingleBitField)
  9455. << FirstII << IsFirstBitField;
  9456. ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
  9457. FieldSingleBitField)
  9458. << SecondII << IsSecondBitField;
  9459. Diagnosed = true;
  9460. break;
  9461. }
  9462. if (IsFirstBitField && IsSecondBitField) {
  9463. ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
  9464. FieldDifferentWidthBitField)
  9465. << FirstII << FirstField->getBitWidth()->getSourceRange();
  9466. ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
  9467. FieldDifferentWidthBitField)
  9468. << SecondII << SecondField->getBitWidth()->getSourceRange();
  9469. Diagnosed = true;
  9470. break;
  9471. }
  9472. const bool IsFirstMutable = FirstField->isMutable();
  9473. const bool IsSecondMutable = SecondField->isMutable();
  9474. if (IsFirstMutable != IsSecondMutable) {
  9475. ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
  9476. FieldSingleMutable)
  9477. << FirstII << IsFirstMutable;
  9478. ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
  9479. FieldSingleMutable)
  9480. << SecondII << IsSecondMutable;
  9481. Diagnosed = true;
  9482. break;
  9483. }
  9484. const Expr *FirstInitializer = FirstField->getInClassInitializer();
  9485. const Expr *SecondInitializer = SecondField->getInClassInitializer();
  9486. if ((!FirstInitializer && SecondInitializer) ||
  9487. (FirstInitializer && !SecondInitializer)) {
  9488. ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
  9489. FieldSingleInitializer)
  9490. << FirstII << (FirstInitializer != nullptr);
  9491. ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
  9492. FieldSingleInitializer)
  9493. << SecondII << (SecondInitializer != nullptr);
  9494. Diagnosed = true;
  9495. break;
  9496. }
  9497. if (FirstInitializer && SecondInitializer) {
  9498. unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
  9499. unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
  9500. if (FirstInitHash != SecondInitHash) {
  9501. ODRDiagError(FirstField->getLocation(),
  9502. FirstField->getSourceRange(),
  9503. FieldDifferentInitializers)
  9504. << FirstII << FirstInitializer->getSourceRange();
  9505. ODRDiagNote(SecondField->getLocation(),
  9506. SecondField->getSourceRange(),
  9507. FieldDifferentInitializers)
  9508. << SecondII << SecondInitializer->getSourceRange();
  9509. Diagnosed = true;
  9510. break;
  9511. }
  9512. }
  9513. break;
  9514. }
  9515. case CXXMethod: {
  9516. enum {
  9517. DiagMethod,
  9518. DiagConstructor,
  9519. DiagDestructor,
  9520. } FirstMethodType,
  9521. SecondMethodType;
  9522. auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
  9523. if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
  9524. if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
  9525. return DiagMethod;
  9526. };
  9527. const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
  9528. const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
  9529. FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
  9530. SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
  9531. auto FirstName = FirstMethod->getDeclName();
  9532. auto SecondName = SecondMethod->getDeclName();
  9533. if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
  9534. ODRDiagError(FirstMethod->getLocation(),
  9535. FirstMethod->getSourceRange(), MethodName)
  9536. << FirstMethodType << FirstName;
  9537. ODRDiagNote(SecondMethod->getLocation(),
  9538. SecondMethod->getSourceRange(), MethodName)
  9539. << SecondMethodType << SecondName;
  9540. Diagnosed = true;
  9541. break;
  9542. }
  9543. const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
  9544. const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
  9545. if (FirstDeleted != SecondDeleted) {
  9546. ODRDiagError(FirstMethod->getLocation(),
  9547. FirstMethod->getSourceRange(), MethodDeleted)
  9548. << FirstMethodType << FirstName << FirstDeleted;
  9549. ODRDiagNote(SecondMethod->getLocation(),
  9550. SecondMethod->getSourceRange(), MethodDeleted)
  9551. << SecondMethodType << SecondName << SecondDeleted;
  9552. Diagnosed = true;
  9553. break;
  9554. }
  9555. const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
  9556. const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
  9557. if (FirstDefaulted != SecondDefaulted) {
  9558. ODRDiagError(FirstMethod->getLocation(),
  9559. FirstMethod->getSourceRange(), MethodDefaulted)
  9560. << FirstMethodType << FirstName << FirstDefaulted;
  9561. ODRDiagNote(SecondMethod->getLocation(),
  9562. SecondMethod->getSourceRange(), MethodDefaulted)
  9563. << SecondMethodType << SecondName << SecondDefaulted;
  9564. Diagnosed = true;
  9565. break;
  9566. }
  9567. const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
  9568. const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
  9569. const bool FirstPure = FirstMethod->isPure();
  9570. const bool SecondPure = SecondMethod->isPure();
  9571. if ((FirstVirtual || SecondVirtual) &&
  9572. (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
  9573. ODRDiagError(FirstMethod->getLocation(),
  9574. FirstMethod->getSourceRange(), MethodVirtual)
  9575. << FirstMethodType << FirstName << FirstPure << FirstVirtual;
  9576. ODRDiagNote(SecondMethod->getLocation(),
  9577. SecondMethod->getSourceRange(), MethodVirtual)
  9578. << SecondMethodType << SecondName << SecondPure << SecondVirtual;
  9579. Diagnosed = true;
  9580. break;
  9581. }
  9582. // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging,
  9583. // FirstDecl is the canonical Decl of SecondDecl, so the storage
  9584. // class needs to be checked instead.
  9585. const auto FirstStorage = FirstMethod->getStorageClass();
  9586. const auto SecondStorage = SecondMethod->getStorageClass();
  9587. const bool FirstStatic = FirstStorage == SC_Static;
  9588. const bool SecondStatic = SecondStorage == SC_Static;
  9589. if (FirstStatic != SecondStatic) {
  9590. ODRDiagError(FirstMethod->getLocation(),
  9591. FirstMethod->getSourceRange(), MethodStatic)
  9592. << FirstMethodType << FirstName << FirstStatic;
  9593. ODRDiagNote(SecondMethod->getLocation(),
  9594. SecondMethod->getSourceRange(), MethodStatic)
  9595. << SecondMethodType << SecondName << SecondStatic;
  9596. Diagnosed = true;
  9597. break;
  9598. }
  9599. const bool FirstVolatile = FirstMethod->isVolatile();
  9600. const bool SecondVolatile = SecondMethod->isVolatile();
  9601. if (FirstVolatile != SecondVolatile) {
  9602. ODRDiagError(FirstMethod->getLocation(),
  9603. FirstMethod->getSourceRange(), MethodVolatile)
  9604. << FirstMethodType << FirstName << FirstVolatile;
  9605. ODRDiagNote(SecondMethod->getLocation(),
  9606. SecondMethod->getSourceRange(), MethodVolatile)
  9607. << SecondMethodType << SecondName << SecondVolatile;
  9608. Diagnosed = true;
  9609. break;
  9610. }
  9611. const bool FirstConst = FirstMethod->isConst();
  9612. const bool SecondConst = SecondMethod->isConst();
  9613. if (FirstConst != SecondConst) {
  9614. ODRDiagError(FirstMethod->getLocation(),
  9615. FirstMethod->getSourceRange(), MethodConst)
  9616. << FirstMethodType << FirstName << FirstConst;
  9617. ODRDiagNote(SecondMethod->getLocation(),
  9618. SecondMethod->getSourceRange(), MethodConst)
  9619. << SecondMethodType << SecondName << SecondConst;
  9620. Diagnosed = true;
  9621. break;
  9622. }
  9623. const bool FirstInline = FirstMethod->isInlineSpecified();
  9624. const bool SecondInline = SecondMethod->isInlineSpecified();
  9625. if (FirstInline != SecondInline) {
  9626. ODRDiagError(FirstMethod->getLocation(),
  9627. FirstMethod->getSourceRange(), MethodInline)
  9628. << FirstMethodType << FirstName << FirstInline;
  9629. ODRDiagNote(SecondMethod->getLocation(),
  9630. SecondMethod->getSourceRange(), MethodInline)
  9631. << SecondMethodType << SecondName << SecondInline;
  9632. Diagnosed = true;
  9633. break;
  9634. }
  9635. const unsigned FirstNumParameters = FirstMethod->param_size();
  9636. const unsigned SecondNumParameters = SecondMethod->param_size();
  9637. if (FirstNumParameters != SecondNumParameters) {
  9638. ODRDiagError(FirstMethod->getLocation(),
  9639. FirstMethod->getSourceRange(), MethodNumberParameters)
  9640. << FirstMethodType << FirstName << FirstNumParameters;
  9641. ODRDiagNote(SecondMethod->getLocation(),
  9642. SecondMethod->getSourceRange(), MethodNumberParameters)
  9643. << SecondMethodType << SecondName << SecondNumParameters;
  9644. Diagnosed = true;
  9645. break;
  9646. }
  9647. // Need this status boolean to know when break out of the switch.
  9648. bool ParameterMismatch = false;
  9649. for (unsigned I = 0; I < FirstNumParameters; ++I) {
  9650. const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
  9651. const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
  9652. QualType FirstParamType = FirstParam->getType();
  9653. QualType SecondParamType = SecondParam->getType();
  9654. if (FirstParamType != SecondParamType &&
  9655. ComputeQualTypeODRHash(FirstParamType) !=
  9656. ComputeQualTypeODRHash(SecondParamType)) {
  9657. if (const DecayedType *ParamDecayedType =
  9658. FirstParamType->getAs<DecayedType>()) {
  9659. ODRDiagError(FirstMethod->getLocation(),
  9660. FirstMethod->getSourceRange(), MethodParameterType)
  9661. << FirstMethodType << FirstName << (I + 1) << FirstParamType
  9662. << true << ParamDecayedType->getOriginalType();
  9663. } else {
  9664. ODRDiagError(FirstMethod->getLocation(),
  9665. FirstMethod->getSourceRange(), MethodParameterType)
  9666. << FirstMethodType << FirstName << (I + 1) << FirstParamType
  9667. << false;
  9668. }
  9669. if (const DecayedType *ParamDecayedType =
  9670. SecondParamType->getAs<DecayedType>()) {
  9671. ODRDiagNote(SecondMethod->getLocation(),
  9672. SecondMethod->getSourceRange(), MethodParameterType)
  9673. << SecondMethodType << SecondName << (I + 1)
  9674. << SecondParamType << true
  9675. << ParamDecayedType->getOriginalType();
  9676. } else {
  9677. ODRDiagNote(SecondMethod->getLocation(),
  9678. SecondMethod->getSourceRange(), MethodParameterType)
  9679. << SecondMethodType << SecondName << (I + 1)
  9680. << SecondParamType << false;
  9681. }
  9682. ParameterMismatch = true;
  9683. break;
  9684. }
  9685. DeclarationName FirstParamName = FirstParam->getDeclName();
  9686. DeclarationName SecondParamName = SecondParam->getDeclName();
  9687. if (FirstParamName != SecondParamName) {
  9688. ODRDiagError(FirstMethod->getLocation(),
  9689. FirstMethod->getSourceRange(), MethodParameterName)
  9690. << FirstMethodType << FirstName << (I + 1) << FirstParamName;
  9691. ODRDiagNote(SecondMethod->getLocation(),
  9692. SecondMethod->getSourceRange(), MethodParameterName)
  9693. << SecondMethodType << SecondName << (I + 1) << SecondParamName;
  9694. ParameterMismatch = true;
  9695. break;
  9696. }
  9697. const Expr *FirstInit = FirstParam->getInit();
  9698. const Expr *SecondInit = SecondParam->getInit();
  9699. if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
  9700. ODRDiagError(FirstMethod->getLocation(),
  9701. FirstMethod->getSourceRange(),
  9702. MethodParameterSingleDefaultArgument)
  9703. << FirstMethodType << FirstName << (I + 1)
  9704. << (FirstInit == nullptr)
  9705. << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
  9706. ODRDiagNote(SecondMethod->getLocation(),
  9707. SecondMethod->getSourceRange(),
  9708. MethodParameterSingleDefaultArgument)
  9709. << SecondMethodType << SecondName << (I + 1)
  9710. << (SecondInit == nullptr)
  9711. << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
  9712. ParameterMismatch = true;
  9713. break;
  9714. }
  9715. if (FirstInit && SecondInit &&
  9716. ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
  9717. ODRDiagError(FirstMethod->getLocation(),
  9718. FirstMethod->getSourceRange(),
  9719. MethodParameterDifferentDefaultArgument)
  9720. << FirstMethodType << FirstName << (I + 1)
  9721. << FirstInit->getSourceRange();
  9722. ODRDiagNote(SecondMethod->getLocation(),
  9723. SecondMethod->getSourceRange(),
  9724. MethodParameterDifferentDefaultArgument)
  9725. << SecondMethodType << SecondName << (I + 1)
  9726. << SecondInit->getSourceRange();
  9727. ParameterMismatch = true;
  9728. break;
  9729. }
  9730. }
  9731. if (ParameterMismatch) {
  9732. Diagnosed = true;
  9733. break;
  9734. }
  9735. const auto *FirstTemplateArgs =
  9736. FirstMethod->getTemplateSpecializationArgs();
  9737. const auto *SecondTemplateArgs =
  9738. SecondMethod->getTemplateSpecializationArgs();
  9739. if ((FirstTemplateArgs && !SecondTemplateArgs) ||
  9740. (!FirstTemplateArgs && SecondTemplateArgs)) {
  9741. ODRDiagError(FirstMethod->getLocation(),
  9742. FirstMethod->getSourceRange(), MethodNoTemplateArguments)
  9743. << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
  9744. ODRDiagNote(SecondMethod->getLocation(),
  9745. SecondMethod->getSourceRange(), MethodNoTemplateArguments)
  9746. << SecondMethodType << SecondName
  9747. << (SecondTemplateArgs != nullptr);
  9748. Diagnosed = true;
  9749. break;
  9750. }
  9751. if (FirstTemplateArgs && SecondTemplateArgs) {
  9752. // Remove pack expansions from argument list.
  9753. auto ExpandTemplateArgumentList =
  9754. [](const TemplateArgumentList *TAL) {
  9755. llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
  9756. for (const TemplateArgument &TA : TAL->asArray()) {
  9757. if (TA.getKind() != TemplateArgument::Pack) {
  9758. ExpandedList.push_back(&TA);
  9759. continue;
  9760. }
  9761. for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
  9762. ExpandedList.push_back(&PackTA);
  9763. }
  9764. }
  9765. return ExpandedList;
  9766. };
  9767. llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
  9768. ExpandTemplateArgumentList(FirstTemplateArgs);
  9769. llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
  9770. ExpandTemplateArgumentList(SecondTemplateArgs);
  9771. if (FirstExpandedList.size() != SecondExpandedList.size()) {
  9772. ODRDiagError(FirstMethod->getLocation(),
  9773. FirstMethod->getSourceRange(),
  9774. MethodDifferentNumberTemplateArguments)
  9775. << FirstMethodType << FirstName
  9776. << (unsigned)FirstExpandedList.size();
  9777. ODRDiagNote(SecondMethod->getLocation(),
  9778. SecondMethod->getSourceRange(),
  9779. MethodDifferentNumberTemplateArguments)
  9780. << SecondMethodType << SecondName
  9781. << (unsigned)SecondExpandedList.size();
  9782. Diagnosed = true;
  9783. break;
  9784. }
  9785. bool TemplateArgumentMismatch = false;
  9786. for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
  9787. const TemplateArgument &FirstTA = *FirstExpandedList[i],
  9788. &SecondTA = *SecondExpandedList[i];
  9789. if (ComputeTemplateArgumentODRHash(FirstTA) ==
  9790. ComputeTemplateArgumentODRHash(SecondTA)) {
  9791. continue;
  9792. }
  9793. ODRDiagError(FirstMethod->getLocation(),
  9794. FirstMethod->getSourceRange(),
  9795. MethodDifferentTemplateArgument)
  9796. << FirstMethodType << FirstName << FirstTA << i + 1;
  9797. ODRDiagNote(SecondMethod->getLocation(),
  9798. SecondMethod->getSourceRange(),
  9799. MethodDifferentTemplateArgument)
  9800. << SecondMethodType << SecondName << SecondTA << i + 1;
  9801. TemplateArgumentMismatch = true;
  9802. break;
  9803. }
  9804. if (TemplateArgumentMismatch) {
  9805. Diagnosed = true;
  9806. break;
  9807. }
  9808. }
  9809. // Compute the hash of the method as if it has no body.
  9810. auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
  9811. Hash.clear();
  9812. Hash.AddFunctionDecl(D, true /*SkipBody*/);
  9813. return Hash.CalculateHash();
  9814. };
  9815. // Compare the hash generated to the hash stored. A difference means
  9816. // that a body was present in the original source. Due to merging,
  9817. // the stardard way of detecting a body will not work.
  9818. const bool HasFirstBody =
  9819. ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
  9820. const bool HasSecondBody =
  9821. ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
  9822. if (HasFirstBody != HasSecondBody) {
  9823. ODRDiagError(FirstMethod->getLocation(),
  9824. FirstMethod->getSourceRange(), MethodSingleBody)
  9825. << FirstMethodType << FirstName << HasFirstBody;
  9826. ODRDiagNote(SecondMethod->getLocation(),
  9827. SecondMethod->getSourceRange(), MethodSingleBody)
  9828. << SecondMethodType << SecondName << HasSecondBody;
  9829. Diagnosed = true;
  9830. break;
  9831. }
  9832. if (HasFirstBody && HasSecondBody) {
  9833. ODRDiagError(FirstMethod->getLocation(),
  9834. FirstMethod->getSourceRange(), MethodDifferentBody)
  9835. << FirstMethodType << FirstName;
  9836. ODRDiagNote(SecondMethod->getLocation(),
  9837. SecondMethod->getSourceRange(), MethodDifferentBody)
  9838. << SecondMethodType << SecondName;
  9839. Diagnosed = true;
  9840. break;
  9841. }
  9842. break;
  9843. }
  9844. case TypeAlias:
  9845. case TypeDef: {
  9846. TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl);
  9847. TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl);
  9848. auto FirstName = FirstTD->getDeclName();
  9849. auto SecondName = SecondTD->getDeclName();
  9850. if (FirstName != SecondName) {
  9851. ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
  9852. TypedefName)
  9853. << (FirstDiffType == TypeAlias) << FirstName;
  9854. ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
  9855. TypedefName)
  9856. << (FirstDiffType == TypeAlias) << SecondName;
  9857. Diagnosed = true;
  9858. break;
  9859. }
  9860. QualType FirstType = FirstTD->getUnderlyingType();
  9861. QualType SecondType = SecondTD->getUnderlyingType();
  9862. if (ComputeQualTypeODRHash(FirstType) !=
  9863. ComputeQualTypeODRHash(SecondType)) {
  9864. ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
  9865. TypedefType)
  9866. << (FirstDiffType == TypeAlias) << FirstName << FirstType;
  9867. ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
  9868. TypedefType)
  9869. << (FirstDiffType == TypeAlias) << SecondName << SecondType;
  9870. Diagnosed = true;
  9871. break;
  9872. }
  9873. break;
  9874. }
  9875. case Var: {
  9876. VarDecl *FirstVD = cast<VarDecl>(FirstDecl);
  9877. VarDecl *SecondVD = cast<VarDecl>(SecondDecl);
  9878. auto FirstName = FirstVD->getDeclName();
  9879. auto SecondName = SecondVD->getDeclName();
  9880. if (FirstName != SecondName) {
  9881. ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
  9882. VarName)
  9883. << FirstName;
  9884. ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
  9885. VarName)
  9886. << SecondName;
  9887. Diagnosed = true;
  9888. break;
  9889. }
  9890. QualType FirstType = FirstVD->getType();
  9891. QualType SecondType = SecondVD->getType();
  9892. if (ComputeQualTypeODRHash(FirstType) !=
  9893. ComputeQualTypeODRHash(SecondType)) {
  9894. ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
  9895. VarType)
  9896. << FirstName << FirstType;
  9897. ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
  9898. VarType)
  9899. << SecondName << SecondType;
  9900. Diagnosed = true;
  9901. break;
  9902. }
  9903. const Expr *FirstInit = FirstVD->getInit();
  9904. const Expr *SecondInit = SecondVD->getInit();
  9905. if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
  9906. ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
  9907. VarSingleInitializer)
  9908. << FirstName << (FirstInit == nullptr)
  9909. << (FirstInit ? FirstInit->getSourceRange(): SourceRange());
  9910. ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
  9911. VarSingleInitializer)
  9912. << SecondName << (SecondInit == nullptr)
  9913. << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
  9914. Diagnosed = true;
  9915. break;
  9916. }
  9917. if (FirstInit && SecondInit &&
  9918. ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
  9919. ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
  9920. VarDifferentInitializer)
  9921. << FirstName << FirstInit->getSourceRange();
  9922. ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
  9923. VarDifferentInitializer)
  9924. << SecondName << SecondInit->getSourceRange();
  9925. Diagnosed = true;
  9926. break;
  9927. }
  9928. const bool FirstIsConstexpr = FirstVD->isConstexpr();
  9929. const bool SecondIsConstexpr = SecondVD->isConstexpr();
  9930. if (FirstIsConstexpr != SecondIsConstexpr) {
  9931. ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
  9932. VarConstexpr)
  9933. << FirstName << FirstIsConstexpr;
  9934. ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
  9935. VarConstexpr)
  9936. << SecondName << SecondIsConstexpr;
  9937. Diagnosed = true;
  9938. break;
  9939. }
  9940. break;
  9941. }
  9942. case Friend: {
  9943. FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
  9944. FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
  9945. NamedDecl *FirstND = FirstFriend->getFriendDecl();
  9946. NamedDecl *SecondND = SecondFriend->getFriendDecl();
  9947. TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
  9948. TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
  9949. if (FirstND && SecondND) {
  9950. ODRDiagError(FirstFriend->getFriendLoc(),
  9951. FirstFriend->getSourceRange(), FriendFunction)
  9952. << FirstND;
  9953. ODRDiagNote(SecondFriend->getFriendLoc(),
  9954. SecondFriend->getSourceRange(), FriendFunction)
  9955. << SecondND;
  9956. Diagnosed = true;
  9957. break;
  9958. }
  9959. if (FirstTSI && SecondTSI) {
  9960. QualType FirstFriendType = FirstTSI->getType();
  9961. QualType SecondFriendType = SecondTSI->getType();
  9962. assert(ComputeQualTypeODRHash(FirstFriendType) !=
  9963. ComputeQualTypeODRHash(SecondFriendType));
  9964. ODRDiagError(FirstFriend->getFriendLoc(),
  9965. FirstFriend->getSourceRange(), FriendType)
  9966. << FirstFriendType;
  9967. ODRDiagNote(SecondFriend->getFriendLoc(),
  9968. SecondFriend->getSourceRange(), FriendType)
  9969. << SecondFriendType;
  9970. Diagnosed = true;
  9971. break;
  9972. }
  9973. ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
  9974. FriendTypeFunction)
  9975. << (FirstTSI == nullptr);
  9976. ODRDiagNote(SecondFriend->getFriendLoc(),
  9977. SecondFriend->getSourceRange(), FriendTypeFunction)
  9978. << (SecondTSI == nullptr);
  9979. Diagnosed = true;
  9980. break;
  9981. }
  9982. case FunctionTemplate: {
  9983. FunctionTemplateDecl *FirstTemplate =
  9984. cast<FunctionTemplateDecl>(FirstDecl);
  9985. FunctionTemplateDecl *SecondTemplate =
  9986. cast<FunctionTemplateDecl>(SecondDecl);
  9987. TemplateParameterList *FirstTPL =
  9988. FirstTemplate->getTemplateParameters();
  9989. TemplateParameterList *SecondTPL =
  9990. SecondTemplate->getTemplateParameters();
  9991. if (FirstTPL->size() != SecondTPL->size()) {
  9992. ODRDiagError(FirstTemplate->getLocation(),
  9993. FirstTemplate->getSourceRange(),
  9994. FunctionTemplateDifferentNumberParameters)
  9995. << FirstTemplate << FirstTPL->size();
  9996. ODRDiagNote(SecondTemplate->getLocation(),
  9997. SecondTemplate->getSourceRange(),
  9998. FunctionTemplateDifferentNumberParameters)
  9999. << SecondTemplate << SecondTPL->size();
  10000. Diagnosed = true;
  10001. break;
  10002. }
  10003. bool ParameterMismatch = false;
  10004. for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
  10005. NamedDecl *FirstParam = FirstTPL->getParam(i);
  10006. NamedDecl *SecondParam = SecondTPL->getParam(i);
  10007. if (FirstParam->getKind() != SecondParam->getKind()) {
  10008. enum {
  10009. TemplateTypeParameter,
  10010. NonTypeTemplateParameter,
  10011. TemplateTemplateParameter,
  10012. };
  10013. auto GetParamType = [](NamedDecl *D) {
  10014. switch (D->getKind()) {
  10015. default:
  10016. llvm_unreachable("Unexpected template parameter type");
  10017. case Decl::TemplateTypeParm:
  10018. return TemplateTypeParameter;
  10019. case Decl::NonTypeTemplateParm:
  10020. return NonTypeTemplateParameter;
  10021. case Decl::TemplateTemplateParm:
  10022. return TemplateTemplateParameter;
  10023. }
  10024. };
  10025. ODRDiagError(FirstTemplate->getLocation(),
  10026. FirstTemplate->getSourceRange(),
  10027. FunctionTemplateParameterDifferentKind)
  10028. << FirstTemplate << (i + 1) << GetParamType(FirstParam);
  10029. ODRDiagNote(SecondTemplate->getLocation(),
  10030. SecondTemplate->getSourceRange(),
  10031. FunctionTemplateParameterDifferentKind)
  10032. << SecondTemplate << (i + 1) << GetParamType(SecondParam);
  10033. ParameterMismatch = true;
  10034. break;
  10035. }
  10036. if (FirstParam->getName() != SecondParam->getName()) {
  10037. ODRDiagError(FirstTemplate->getLocation(),
  10038. FirstTemplate->getSourceRange(),
  10039. FunctionTemplateParameterName)
  10040. << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
  10041. << FirstParam;
  10042. ODRDiagNote(SecondTemplate->getLocation(),
  10043. SecondTemplate->getSourceRange(),
  10044. FunctionTemplateParameterName)
  10045. << SecondTemplate << (i + 1)
  10046. << (bool)SecondParam->getIdentifier() << SecondParam;
  10047. ParameterMismatch = true;
  10048. break;
  10049. }
  10050. if (isa<TemplateTypeParmDecl>(FirstParam) &&
  10051. isa<TemplateTypeParmDecl>(SecondParam)) {
  10052. TemplateTypeParmDecl *FirstTTPD =
  10053. cast<TemplateTypeParmDecl>(FirstParam);
  10054. TemplateTypeParmDecl *SecondTTPD =
  10055. cast<TemplateTypeParmDecl>(SecondParam);
  10056. bool HasFirstDefaultArgument =
  10057. FirstTTPD->hasDefaultArgument() &&
  10058. !FirstTTPD->defaultArgumentWasInherited();
  10059. bool HasSecondDefaultArgument =
  10060. SecondTTPD->hasDefaultArgument() &&
  10061. !SecondTTPD->defaultArgumentWasInherited();
  10062. if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
  10063. ODRDiagError(FirstTemplate->getLocation(),
  10064. FirstTemplate->getSourceRange(),
  10065. FunctionTemplateParameterSingleDefaultArgument)
  10066. << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
  10067. ODRDiagNote(SecondTemplate->getLocation(),
  10068. SecondTemplate->getSourceRange(),
  10069. FunctionTemplateParameterSingleDefaultArgument)
  10070. << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
  10071. ParameterMismatch = true;
  10072. break;
  10073. }
  10074. if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
  10075. QualType FirstType = FirstTTPD->getDefaultArgument();
  10076. QualType SecondType = SecondTTPD->getDefaultArgument();
  10077. if (ComputeQualTypeODRHash(FirstType) !=
  10078. ComputeQualTypeODRHash(SecondType)) {
  10079. ODRDiagError(FirstTemplate->getLocation(),
  10080. FirstTemplate->getSourceRange(),
  10081. FunctionTemplateParameterDifferentDefaultArgument)
  10082. << FirstTemplate << (i + 1) << FirstType;
  10083. ODRDiagNote(SecondTemplate->getLocation(),
  10084. SecondTemplate->getSourceRange(),
  10085. FunctionTemplateParameterDifferentDefaultArgument)
  10086. << SecondTemplate << (i + 1) << SecondType;
  10087. ParameterMismatch = true;
  10088. break;
  10089. }
  10090. }
  10091. if (FirstTTPD->isParameterPack() !=
  10092. SecondTTPD->isParameterPack()) {
  10093. ODRDiagError(FirstTemplate->getLocation(),
  10094. FirstTemplate->getSourceRange(),
  10095. FunctionTemplatePackParameter)
  10096. << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
  10097. ODRDiagNote(SecondTemplate->getLocation(),
  10098. SecondTemplate->getSourceRange(),
  10099. FunctionTemplatePackParameter)
  10100. << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
  10101. ParameterMismatch = true;
  10102. break;
  10103. }
  10104. }
  10105. if (isa<TemplateTemplateParmDecl>(FirstParam) &&
  10106. isa<TemplateTemplateParmDecl>(SecondParam)) {
  10107. TemplateTemplateParmDecl *FirstTTPD =
  10108. cast<TemplateTemplateParmDecl>(FirstParam);
  10109. TemplateTemplateParmDecl *SecondTTPD =
  10110. cast<TemplateTemplateParmDecl>(SecondParam);
  10111. TemplateParameterList *FirstTPL =
  10112. FirstTTPD->getTemplateParameters();
  10113. TemplateParameterList *SecondTPL =
  10114. SecondTTPD->getTemplateParameters();
  10115. if (ComputeTemplateParameterListODRHash(FirstTPL) !=
  10116. ComputeTemplateParameterListODRHash(SecondTPL)) {
  10117. ODRDiagError(FirstTemplate->getLocation(),
  10118. FirstTemplate->getSourceRange(),
  10119. FunctionTemplateParameterDifferentType)
  10120. << FirstTemplate << (i + 1);
  10121. ODRDiagNote(SecondTemplate->getLocation(),
  10122. SecondTemplate->getSourceRange(),
  10123. FunctionTemplateParameterDifferentType)
  10124. << SecondTemplate << (i + 1);
  10125. ParameterMismatch = true;
  10126. break;
  10127. }
  10128. bool HasFirstDefaultArgument =
  10129. FirstTTPD->hasDefaultArgument() &&
  10130. !FirstTTPD->defaultArgumentWasInherited();
  10131. bool HasSecondDefaultArgument =
  10132. SecondTTPD->hasDefaultArgument() &&
  10133. !SecondTTPD->defaultArgumentWasInherited();
  10134. if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
  10135. ODRDiagError(FirstTemplate->getLocation(),
  10136. FirstTemplate->getSourceRange(),
  10137. FunctionTemplateParameterSingleDefaultArgument)
  10138. << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
  10139. ODRDiagNote(SecondTemplate->getLocation(),
  10140. SecondTemplate->getSourceRange(),
  10141. FunctionTemplateParameterSingleDefaultArgument)
  10142. << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
  10143. ParameterMismatch = true;
  10144. break;
  10145. }
  10146. if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
  10147. TemplateArgument FirstTA =
  10148. FirstTTPD->getDefaultArgument().getArgument();
  10149. TemplateArgument SecondTA =
  10150. SecondTTPD->getDefaultArgument().getArgument();
  10151. if (ComputeTemplateArgumentODRHash(FirstTA) !=
  10152. ComputeTemplateArgumentODRHash(SecondTA)) {
  10153. ODRDiagError(FirstTemplate->getLocation(),
  10154. FirstTemplate->getSourceRange(),
  10155. FunctionTemplateParameterDifferentDefaultArgument)
  10156. << FirstTemplate << (i + 1) << FirstTA;
  10157. ODRDiagNote(SecondTemplate->getLocation(),
  10158. SecondTemplate->getSourceRange(),
  10159. FunctionTemplateParameterDifferentDefaultArgument)
  10160. << SecondTemplate << (i + 1) << SecondTA;
  10161. ParameterMismatch = true;
  10162. break;
  10163. }
  10164. }
  10165. if (FirstTTPD->isParameterPack() !=
  10166. SecondTTPD->isParameterPack()) {
  10167. ODRDiagError(FirstTemplate->getLocation(),
  10168. FirstTemplate->getSourceRange(),
  10169. FunctionTemplatePackParameter)
  10170. << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
  10171. ODRDiagNote(SecondTemplate->getLocation(),
  10172. SecondTemplate->getSourceRange(),
  10173. FunctionTemplatePackParameter)
  10174. << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
  10175. ParameterMismatch = true;
  10176. break;
  10177. }
  10178. }
  10179. if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
  10180. isa<NonTypeTemplateParmDecl>(SecondParam)) {
  10181. NonTypeTemplateParmDecl *FirstNTTPD =
  10182. cast<NonTypeTemplateParmDecl>(FirstParam);
  10183. NonTypeTemplateParmDecl *SecondNTTPD =
  10184. cast<NonTypeTemplateParmDecl>(SecondParam);
  10185. QualType FirstType = FirstNTTPD->getType();
  10186. QualType SecondType = SecondNTTPD->getType();
  10187. if (ComputeQualTypeODRHash(FirstType) !=
  10188. ComputeQualTypeODRHash(SecondType)) {
  10189. ODRDiagError(FirstTemplate->getLocation(),
  10190. FirstTemplate->getSourceRange(),
  10191. FunctionTemplateParameterDifferentType)
  10192. << FirstTemplate << (i + 1);
  10193. ODRDiagNote(SecondTemplate->getLocation(),
  10194. SecondTemplate->getSourceRange(),
  10195. FunctionTemplateParameterDifferentType)
  10196. << SecondTemplate << (i + 1);
  10197. ParameterMismatch = true;
  10198. break;
  10199. }
  10200. bool HasFirstDefaultArgument =
  10201. FirstNTTPD->hasDefaultArgument() &&
  10202. !FirstNTTPD->defaultArgumentWasInherited();
  10203. bool HasSecondDefaultArgument =
  10204. SecondNTTPD->hasDefaultArgument() &&
  10205. !SecondNTTPD->defaultArgumentWasInherited();
  10206. if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
  10207. ODRDiagError(FirstTemplate->getLocation(),
  10208. FirstTemplate->getSourceRange(),
  10209. FunctionTemplateParameterSingleDefaultArgument)
  10210. << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
  10211. ODRDiagNote(SecondTemplate->getLocation(),
  10212. SecondTemplate->getSourceRange(),
  10213. FunctionTemplateParameterSingleDefaultArgument)
  10214. << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
  10215. ParameterMismatch = true;
  10216. break;
  10217. }
  10218. if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
  10219. Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
  10220. Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
  10221. if (ComputeODRHash(FirstDefaultArgument) !=
  10222. ComputeODRHash(SecondDefaultArgument)) {
  10223. ODRDiagError(FirstTemplate->getLocation(),
  10224. FirstTemplate->getSourceRange(),
  10225. FunctionTemplateParameterDifferentDefaultArgument)
  10226. << FirstTemplate << (i + 1) << FirstDefaultArgument;
  10227. ODRDiagNote(SecondTemplate->getLocation(),
  10228. SecondTemplate->getSourceRange(),
  10229. FunctionTemplateParameterDifferentDefaultArgument)
  10230. << SecondTemplate << (i + 1) << SecondDefaultArgument;
  10231. ParameterMismatch = true;
  10232. break;
  10233. }
  10234. }
  10235. if (FirstNTTPD->isParameterPack() !=
  10236. SecondNTTPD->isParameterPack()) {
  10237. ODRDiagError(FirstTemplate->getLocation(),
  10238. FirstTemplate->getSourceRange(),
  10239. FunctionTemplatePackParameter)
  10240. << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
  10241. ODRDiagNote(SecondTemplate->getLocation(),
  10242. SecondTemplate->getSourceRange(),
  10243. FunctionTemplatePackParameter)
  10244. << SecondTemplate << (i + 1)
  10245. << SecondNTTPD->isParameterPack();
  10246. ParameterMismatch = true;
  10247. break;
  10248. }
  10249. }
  10250. }
  10251. if (ParameterMismatch) {
  10252. Diagnosed = true;
  10253. break;
  10254. }
  10255. break;
  10256. }
  10257. }
  10258. if (Diagnosed)
  10259. continue;
  10260. Diag(FirstDecl->getLocation(),
  10261. diag::err_module_odr_violation_mismatch_decl_unknown)
  10262. << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
  10263. << FirstDecl->getSourceRange();
  10264. Diag(SecondDecl->getLocation(),
  10265. diag::note_module_odr_violation_mismatch_decl_unknown)
  10266. << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
  10267. Diagnosed = true;
  10268. }
  10269. if (!Diagnosed) {
  10270. // All definitions are updates to the same declaration. This happens if a
  10271. // module instantiates the declaration of a class template specialization
  10272. // and two or more other modules instantiate its definition.
  10273. //
  10274. // FIXME: Indicate which modules had instantiations of this definition.
  10275. // FIXME: How can this even happen?
  10276. Diag(Merge.first->getLocation(),
  10277. diag::err_module_odr_violation_different_instantiations)
  10278. << Merge.first;
  10279. }
  10280. }
  10281. // Issue ODR failures diagnostics for functions.
  10282. for (auto &Merge : FunctionOdrMergeFailures) {
  10283. enum ODRFunctionDifference {
  10284. ReturnType,
  10285. ParameterName,
  10286. ParameterType,
  10287. ParameterSingleDefaultArgument,
  10288. ParameterDifferentDefaultArgument,
  10289. FunctionBody,
  10290. };
  10291. FunctionDecl *FirstFunction = Merge.first;
  10292. std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
  10293. bool Diagnosed = false;
  10294. for (auto &SecondFunction : Merge.second) {
  10295. if (FirstFunction == SecondFunction)
  10296. continue;
  10297. std::string SecondModule =
  10298. getOwningModuleNameForDiagnostic(SecondFunction);
  10299. auto ODRDiagError = [FirstFunction, &FirstModule,
  10300. this](SourceLocation Loc, SourceRange Range,
  10301. ODRFunctionDifference DiffType) {
  10302. return Diag(Loc, diag::err_module_odr_violation_function)
  10303. << FirstFunction << FirstModule.empty() << FirstModule << Range
  10304. << DiffType;
  10305. };
  10306. auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
  10307. SourceRange Range,
  10308. ODRFunctionDifference DiffType) {
  10309. return Diag(Loc, diag::note_module_odr_violation_function)
  10310. << SecondModule << Range << DiffType;
  10311. };
  10312. if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
  10313. ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
  10314. ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
  10315. FirstFunction->getReturnTypeSourceRange(), ReturnType)
  10316. << FirstFunction->getReturnType();
  10317. ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
  10318. SecondFunction->getReturnTypeSourceRange(), ReturnType)
  10319. << SecondFunction->getReturnType();
  10320. Diagnosed = true;
  10321. break;
  10322. }
  10323. assert(FirstFunction->param_size() == SecondFunction->param_size() &&
  10324. "Merged functions with different number of parameters");
  10325. auto ParamSize = FirstFunction->param_size();
  10326. bool ParameterMismatch = false;
  10327. for (unsigned I = 0; I < ParamSize; ++I) {
  10328. auto *FirstParam = FirstFunction->getParamDecl(I);
  10329. auto *SecondParam = SecondFunction->getParamDecl(I);
  10330. assert(getContext().hasSameType(FirstParam->getType(),
  10331. SecondParam->getType()) &&
  10332. "Merged function has different parameter types.");
  10333. if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
  10334. ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
  10335. ParameterName)
  10336. << I + 1 << FirstParam->getDeclName();
  10337. ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
  10338. ParameterName)
  10339. << I + 1 << SecondParam->getDeclName();
  10340. ParameterMismatch = true;
  10341. break;
  10342. };
  10343. QualType FirstParamType = FirstParam->getType();
  10344. QualType SecondParamType = SecondParam->getType();
  10345. if (FirstParamType != SecondParamType &&
  10346. ComputeQualTypeODRHash(FirstParamType) !=
  10347. ComputeQualTypeODRHash(SecondParamType)) {
  10348. if (const DecayedType *ParamDecayedType =
  10349. FirstParamType->getAs<DecayedType>()) {
  10350. ODRDiagError(FirstParam->getLocation(),
  10351. FirstParam->getSourceRange(), ParameterType)
  10352. << (I + 1) << FirstParamType << true
  10353. << ParamDecayedType->getOriginalType();
  10354. } else {
  10355. ODRDiagError(FirstParam->getLocation(),
  10356. FirstParam->getSourceRange(), ParameterType)
  10357. << (I + 1) << FirstParamType << false;
  10358. }
  10359. if (const DecayedType *ParamDecayedType =
  10360. SecondParamType->getAs<DecayedType>()) {
  10361. ODRDiagNote(SecondParam->getLocation(),
  10362. SecondParam->getSourceRange(), ParameterType)
  10363. << (I + 1) << SecondParamType << true
  10364. << ParamDecayedType->getOriginalType();
  10365. } else {
  10366. ODRDiagNote(SecondParam->getLocation(),
  10367. SecondParam->getSourceRange(), ParameterType)
  10368. << (I + 1) << SecondParamType << false;
  10369. }
  10370. ParameterMismatch = true;
  10371. break;
  10372. }
  10373. const Expr *FirstInit = FirstParam->getInit();
  10374. const Expr *SecondInit = SecondParam->getInit();
  10375. if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
  10376. ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
  10377. ParameterSingleDefaultArgument)
  10378. << (I + 1) << (FirstInit == nullptr)
  10379. << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
  10380. ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
  10381. ParameterSingleDefaultArgument)
  10382. << (I + 1) << (SecondInit == nullptr)
  10383. << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
  10384. ParameterMismatch = true;
  10385. break;
  10386. }
  10387. if (FirstInit && SecondInit &&
  10388. ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
  10389. ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
  10390. ParameterDifferentDefaultArgument)
  10391. << (I + 1) << FirstInit->getSourceRange();
  10392. ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
  10393. ParameterDifferentDefaultArgument)
  10394. << (I + 1) << SecondInit->getSourceRange();
  10395. ParameterMismatch = true;
  10396. break;
  10397. }
  10398. assert(ComputeSubDeclODRHash(FirstParam) ==
  10399. ComputeSubDeclODRHash(SecondParam) &&
  10400. "Undiagnosed parameter difference.");
  10401. }
  10402. if (ParameterMismatch) {
  10403. Diagnosed = true;
  10404. break;
  10405. }
  10406. // If no error has been generated before now, assume the problem is in
  10407. // the body and generate a message.
  10408. ODRDiagError(FirstFunction->getLocation(),
  10409. FirstFunction->getSourceRange(), FunctionBody);
  10410. ODRDiagNote(SecondFunction->getLocation(),
  10411. SecondFunction->getSourceRange(), FunctionBody);
  10412. Diagnosed = true;
  10413. break;
  10414. }
  10415. (void)Diagnosed;
  10416. assert(Diagnosed && "Unable to emit ODR diagnostic.");
  10417. }
  10418. // Issue ODR failures diagnostics for enums.
  10419. for (auto &Merge : EnumOdrMergeFailures) {
  10420. enum ODREnumDifference {
  10421. SingleScopedEnum,
  10422. EnumTagKeywordMismatch,
  10423. SingleSpecifiedType,
  10424. DifferentSpecifiedTypes,
  10425. DifferentNumberEnumConstants,
  10426. EnumConstantName,
  10427. EnumConstantSingleInitilizer,
  10428. EnumConstantDifferentInitilizer,
  10429. };
  10430. // If we've already pointed out a specific problem with this enum, don't
  10431. // bother issuing a general "something's different" diagnostic.
  10432. if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
  10433. continue;
  10434. EnumDecl *FirstEnum = Merge.first;
  10435. std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
  10436. using DeclHashes =
  10437. llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
  10438. auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
  10439. DeclHashes &Hashes, EnumDecl *Enum) {
  10440. for (auto *D : Enum->decls()) {
  10441. // Due to decl merging, the first EnumDecl is the parent of
  10442. // Decls in both records.
  10443. if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
  10444. continue;
  10445. assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
  10446. Hashes.emplace_back(cast<EnumConstantDecl>(D),
  10447. ComputeSubDeclODRHash(D));
  10448. }
  10449. };
  10450. DeclHashes FirstHashes;
  10451. PopulateHashes(FirstHashes, FirstEnum);
  10452. bool Diagnosed = false;
  10453. for (auto &SecondEnum : Merge.second) {
  10454. if (FirstEnum == SecondEnum)
  10455. continue;
  10456. std::string SecondModule =
  10457. getOwningModuleNameForDiagnostic(SecondEnum);
  10458. auto ODRDiagError = [FirstEnum, &FirstModule,
  10459. this](SourceLocation Loc, SourceRange Range,
  10460. ODREnumDifference DiffType) {
  10461. return Diag(Loc, diag::err_module_odr_violation_enum)
  10462. << FirstEnum << FirstModule.empty() << FirstModule << Range
  10463. << DiffType;
  10464. };
  10465. auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
  10466. SourceRange Range,
  10467. ODREnumDifference DiffType) {
  10468. return Diag(Loc, diag::note_module_odr_violation_enum)
  10469. << SecondModule << Range << DiffType;
  10470. };
  10471. if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
  10472. ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
  10473. SingleScopedEnum)
  10474. << FirstEnum->isScoped();
  10475. ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
  10476. SingleScopedEnum)
  10477. << SecondEnum->isScoped();
  10478. Diagnosed = true;
  10479. continue;
  10480. }
  10481. if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
  10482. if (FirstEnum->isScopedUsingClassTag() !=
  10483. SecondEnum->isScopedUsingClassTag()) {
  10484. ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
  10485. EnumTagKeywordMismatch)
  10486. << FirstEnum->isScopedUsingClassTag();
  10487. ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
  10488. EnumTagKeywordMismatch)
  10489. << SecondEnum->isScopedUsingClassTag();
  10490. Diagnosed = true;
  10491. continue;
  10492. }
  10493. }
  10494. QualType FirstUnderlyingType =
  10495. FirstEnum->getIntegerTypeSourceInfo()
  10496. ? FirstEnum->getIntegerTypeSourceInfo()->getType()
  10497. : QualType();
  10498. QualType SecondUnderlyingType =
  10499. SecondEnum->getIntegerTypeSourceInfo()
  10500. ? SecondEnum->getIntegerTypeSourceInfo()->getType()
  10501. : QualType();
  10502. if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
  10503. ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
  10504. SingleSpecifiedType)
  10505. << !FirstUnderlyingType.isNull();
  10506. ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
  10507. SingleSpecifiedType)
  10508. << !SecondUnderlyingType.isNull();
  10509. Diagnosed = true;
  10510. continue;
  10511. }
  10512. if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
  10513. if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
  10514. ComputeQualTypeODRHash(SecondUnderlyingType)) {
  10515. ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
  10516. DifferentSpecifiedTypes)
  10517. << FirstUnderlyingType;
  10518. ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
  10519. DifferentSpecifiedTypes)
  10520. << SecondUnderlyingType;
  10521. Diagnosed = true;
  10522. continue;
  10523. }
  10524. }
  10525. DeclHashes SecondHashes;
  10526. PopulateHashes(SecondHashes, SecondEnum);
  10527. if (FirstHashes.size() != SecondHashes.size()) {
  10528. ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
  10529. DifferentNumberEnumConstants)
  10530. << (int)FirstHashes.size();
  10531. ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
  10532. DifferentNumberEnumConstants)
  10533. << (int)SecondHashes.size();
  10534. Diagnosed = true;
  10535. continue;
  10536. }
  10537. for (unsigned I = 0; I < FirstHashes.size(); ++I) {
  10538. if (FirstHashes[I].second == SecondHashes[I].second)
  10539. continue;
  10540. const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
  10541. const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
  10542. if (FirstEnumConstant->getDeclName() !=
  10543. SecondEnumConstant->getDeclName()) {
  10544. ODRDiagError(FirstEnumConstant->getLocation(),
  10545. FirstEnumConstant->getSourceRange(), EnumConstantName)
  10546. << I + 1 << FirstEnumConstant;
  10547. ODRDiagNote(SecondEnumConstant->getLocation(),
  10548. SecondEnumConstant->getSourceRange(), EnumConstantName)
  10549. << I + 1 << SecondEnumConstant;
  10550. Diagnosed = true;
  10551. break;
  10552. }
  10553. const Expr *FirstInit = FirstEnumConstant->getInitExpr();
  10554. const Expr *SecondInit = SecondEnumConstant->getInitExpr();
  10555. if (!FirstInit && !SecondInit)
  10556. continue;
  10557. if (!FirstInit || !SecondInit) {
  10558. ODRDiagError(FirstEnumConstant->getLocation(),
  10559. FirstEnumConstant->getSourceRange(),
  10560. EnumConstantSingleInitilizer)
  10561. << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
  10562. ODRDiagNote(SecondEnumConstant->getLocation(),
  10563. SecondEnumConstant->getSourceRange(),
  10564. EnumConstantSingleInitilizer)
  10565. << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
  10566. Diagnosed = true;
  10567. break;
  10568. }
  10569. if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
  10570. ODRDiagError(FirstEnumConstant->getLocation(),
  10571. FirstEnumConstant->getSourceRange(),
  10572. EnumConstantDifferentInitilizer)
  10573. << I + 1 << FirstEnumConstant;
  10574. ODRDiagNote(SecondEnumConstant->getLocation(),
  10575. SecondEnumConstant->getSourceRange(),
  10576. EnumConstantDifferentInitilizer)
  10577. << I + 1 << SecondEnumConstant;
  10578. Diagnosed = true;
  10579. break;
  10580. }
  10581. }
  10582. }
  10583. (void)Diagnosed;
  10584. assert(Diagnosed && "Unable to emit ODR diagnostic.");
  10585. }
  10586. }
  10587. void ASTReader::StartedDeserializing() {
  10588. if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
  10589. ReadTimer->startTimer();
  10590. }
  10591. void ASTReader::FinishedDeserializing() {
  10592. assert(NumCurrentElementsDeserializing &&
  10593. "FinishedDeserializing not paired with StartedDeserializing");
  10594. if (NumCurrentElementsDeserializing == 1) {
  10595. // We decrease NumCurrentElementsDeserializing only after pending actions
  10596. // are finished, to avoid recursively re-calling finishPendingActions().
  10597. finishPendingActions();
  10598. }
  10599. --NumCurrentElementsDeserializing;
  10600. if (NumCurrentElementsDeserializing == 0) {
  10601. // Propagate exception specification and deduced type updates along
  10602. // redeclaration chains.
  10603. //
  10604. // We do this now rather than in finishPendingActions because we want to
  10605. // be able to walk the complete redeclaration chains of the updated decls.
  10606. while (!PendingExceptionSpecUpdates.empty() ||
  10607. !PendingDeducedTypeUpdates.empty()) {
  10608. auto ESUpdates = std::move(PendingExceptionSpecUpdates);
  10609. PendingExceptionSpecUpdates.clear();
  10610. for (auto Update : ESUpdates) {
  10611. ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
  10612. auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
  10613. auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
  10614. if (auto *Listener = getContext().getASTMutationListener())
  10615. Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
  10616. for (auto *Redecl : Update.second->redecls())
  10617. getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
  10618. }
  10619. auto DTUpdates = std::move(PendingDeducedTypeUpdates);
  10620. PendingDeducedTypeUpdates.clear();
  10621. for (auto Update : DTUpdates) {
  10622. ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
  10623. // FIXME: If the return type is already deduced, check that it matches.
  10624. getContext().adjustDeducedFunctionResultType(Update.first,
  10625. Update.second);
  10626. }
  10627. }
  10628. if (ReadTimer)
  10629. ReadTimer->stopTimer();
  10630. diagnoseOdrViolations();
  10631. // We are not in recursive loading, so it's safe to pass the "interesting"
  10632. // decls to the consumer.
  10633. if (Consumer)
  10634. PassInterestingDeclsToConsumer();
  10635. }
  10636. }
  10637. void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
  10638. if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
  10639. // Remove any fake results before adding any real ones.
  10640. auto It = PendingFakeLookupResults.find(II);
  10641. if (It != PendingFakeLookupResults.end()) {
  10642. for (auto *ND : It->second)
  10643. SemaObj->IdResolver.RemoveDecl(ND);
  10644. // FIXME: this works around module+PCH performance issue.
  10645. // Rather than erase the result from the map, which is O(n), just clear
  10646. // the vector of NamedDecls.
  10647. It->second.clear();
  10648. }
  10649. }
  10650. if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
  10651. SemaObj->TUScope->AddDecl(D);
  10652. } else if (SemaObj->TUScope) {
  10653. // Adding the decl to IdResolver may have failed because it was already in
  10654. // (even though it was not added in scope). If it is already in, make sure
  10655. // it gets in the scope as well.
  10656. if (std::find(SemaObj->IdResolver.begin(Name),
  10657. SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
  10658. SemaObj->TUScope->AddDecl(D);
  10659. }
  10660. }
  10661. ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
  10662. ASTContext *Context,
  10663. const PCHContainerReader &PCHContainerRdr,
  10664. ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
  10665. StringRef isysroot, bool DisableValidation,
  10666. bool AllowASTWithCompilerErrors,
  10667. bool AllowConfigurationMismatch, bool ValidateSystemInputs,
  10668. bool ValidateASTInputFilesContent, bool UseGlobalIndex,
  10669. std::unique_ptr<llvm::Timer> ReadTimer)
  10670. : Listener(DisableValidation
  10671. ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
  10672. : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
  10673. SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
  10674. PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
  10675. ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
  10676. PCHContainerRdr, PP.getHeaderSearchInfo()),
  10677. DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
  10678. DisableValidation(DisableValidation),
  10679. AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
  10680. AllowConfigurationMismatch(AllowConfigurationMismatch),
  10681. ValidateSystemInputs(ValidateSystemInputs),
  10682. ValidateASTInputFilesContent(ValidateASTInputFilesContent),
  10683. UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
  10684. SourceMgr.setExternalSLocEntrySource(this);
  10685. for (const auto &Ext : Extensions) {
  10686. auto BlockName = Ext->getExtensionMetadata().BlockName;
  10687. auto Known = ModuleFileExtensions.find(BlockName);
  10688. if (Known != ModuleFileExtensions.end()) {
  10689. Diags.Report(diag::warn_duplicate_module_file_extension)
  10690. << BlockName;
  10691. continue;
  10692. }
  10693. ModuleFileExtensions.insert({BlockName, Ext});
  10694. }
  10695. }
  10696. ASTReader::~ASTReader() {
  10697. if (OwnsDeserializationListener)
  10698. delete DeserializationListener;
  10699. }
  10700. IdentifierResolver &ASTReader::getIdResolver() {
  10701. return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
  10702. }
  10703. Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
  10704. unsigned AbbrevID) {
  10705. Idx = 0;
  10706. Record.clear();
  10707. return Cursor.readRecord(AbbrevID, Record);
  10708. }
  10709. //===----------------------------------------------------------------------===//
  10710. //// OMPClauseReader implementation
  10711. ////===----------------------------------------------------------------------===//
  10712. OMPClause *OMPClauseReader::readClause() {
  10713. OMPClause *C = nullptr;
  10714. switch (Record.readInt()) {
  10715. case OMPC_if:
  10716. C = new (Context) OMPIfClause();
  10717. break;
  10718. case OMPC_final:
  10719. C = new (Context) OMPFinalClause();
  10720. break;
  10721. case OMPC_num_threads:
  10722. C = new (Context) OMPNumThreadsClause();
  10723. break;
  10724. case OMPC_safelen:
  10725. C = new (Context) OMPSafelenClause();
  10726. break;
  10727. case OMPC_simdlen:
  10728. C = new (Context) OMPSimdlenClause();
  10729. break;
  10730. case OMPC_allocator:
  10731. C = new (Context) OMPAllocatorClause();
  10732. break;
  10733. case OMPC_collapse:
  10734. C = new (Context) OMPCollapseClause();
  10735. break;
  10736. case OMPC_default:
  10737. C = new (Context) OMPDefaultClause();
  10738. break;
  10739. case OMPC_proc_bind:
  10740. C = new (Context) OMPProcBindClause();
  10741. break;
  10742. case OMPC_schedule:
  10743. C = new (Context) OMPScheduleClause();
  10744. break;
  10745. case OMPC_ordered:
  10746. C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
  10747. break;
  10748. case OMPC_nowait:
  10749. C = new (Context) OMPNowaitClause();
  10750. break;
  10751. case OMPC_untied:
  10752. C = new (Context) OMPUntiedClause();
  10753. break;
  10754. case OMPC_mergeable:
  10755. C = new (Context) OMPMergeableClause();
  10756. break;
  10757. case OMPC_read:
  10758. C = new (Context) OMPReadClause();
  10759. break;
  10760. case OMPC_write:
  10761. C = new (Context) OMPWriteClause();
  10762. break;
  10763. case OMPC_update:
  10764. C = new (Context) OMPUpdateClause();
  10765. break;
  10766. case OMPC_capture:
  10767. C = new (Context) OMPCaptureClause();
  10768. break;
  10769. case OMPC_seq_cst:
  10770. C = new (Context) OMPSeqCstClause();
  10771. break;
  10772. case OMPC_threads:
  10773. C = new (Context) OMPThreadsClause();
  10774. break;
  10775. case OMPC_simd:
  10776. C = new (Context) OMPSIMDClause();
  10777. break;
  10778. case OMPC_nogroup:
  10779. C = new (Context) OMPNogroupClause();
  10780. break;
  10781. case OMPC_unified_address:
  10782. C = new (Context) OMPUnifiedAddressClause();
  10783. break;
  10784. case OMPC_unified_shared_memory:
  10785. C = new (Context) OMPUnifiedSharedMemoryClause();
  10786. break;
  10787. case OMPC_reverse_offload:
  10788. C = new (Context) OMPReverseOffloadClause();
  10789. break;
  10790. case OMPC_dynamic_allocators:
  10791. C = new (Context) OMPDynamicAllocatorsClause();
  10792. break;
  10793. case OMPC_atomic_default_mem_order:
  10794. C = new (Context) OMPAtomicDefaultMemOrderClause();
  10795. break;
  10796. case OMPC_private:
  10797. C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
  10798. break;
  10799. case OMPC_firstprivate:
  10800. C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
  10801. break;
  10802. case OMPC_lastprivate:
  10803. C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
  10804. break;
  10805. case OMPC_shared:
  10806. C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
  10807. break;
  10808. case OMPC_reduction:
  10809. C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
  10810. break;
  10811. case OMPC_task_reduction:
  10812. C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
  10813. break;
  10814. case OMPC_in_reduction:
  10815. C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
  10816. break;
  10817. case OMPC_linear:
  10818. C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
  10819. break;
  10820. case OMPC_aligned:
  10821. C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
  10822. break;
  10823. case OMPC_copyin:
  10824. C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
  10825. break;
  10826. case OMPC_copyprivate:
  10827. C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
  10828. break;
  10829. case OMPC_flush:
  10830. C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
  10831. break;
  10832. case OMPC_depend: {
  10833. unsigned NumVars = Record.readInt();
  10834. unsigned NumLoops = Record.readInt();
  10835. C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
  10836. break;
  10837. }
  10838. case OMPC_device:
  10839. C = new (Context) OMPDeviceClause();
  10840. break;
  10841. case OMPC_map: {
  10842. OMPMappableExprListSizeTy Sizes;
  10843. Sizes.NumVars = Record.readInt();
  10844. Sizes.NumUniqueDeclarations = Record.readInt();
  10845. Sizes.NumComponentLists = Record.readInt();
  10846. Sizes.NumComponents = Record.readInt();
  10847. C = OMPMapClause::CreateEmpty(Context, Sizes);
  10848. break;
  10849. }
  10850. case OMPC_num_teams:
  10851. C = new (Context) OMPNumTeamsClause();
  10852. break;
  10853. case OMPC_thread_limit:
  10854. C = new (Context) OMPThreadLimitClause();
  10855. break;
  10856. case OMPC_priority:
  10857. C = new (Context) OMPPriorityClause();
  10858. break;
  10859. case OMPC_grainsize:
  10860. C = new (Context) OMPGrainsizeClause();
  10861. break;
  10862. case OMPC_num_tasks:
  10863. C = new (Context) OMPNumTasksClause();
  10864. break;
  10865. case OMPC_hint:
  10866. C = new (Context) OMPHintClause();
  10867. break;
  10868. case OMPC_dist_schedule:
  10869. C = new (Context) OMPDistScheduleClause();
  10870. break;
  10871. case OMPC_defaultmap:
  10872. C = new (Context) OMPDefaultmapClause();
  10873. break;
  10874. case OMPC_to: {
  10875. OMPMappableExprListSizeTy Sizes;
  10876. Sizes.NumVars = Record.readInt();
  10877. Sizes.NumUniqueDeclarations = Record.readInt();
  10878. Sizes.NumComponentLists = Record.readInt();
  10879. Sizes.NumComponents = Record.readInt();
  10880. C = OMPToClause::CreateEmpty(Context, Sizes);
  10881. break;
  10882. }
  10883. case OMPC_from: {
  10884. OMPMappableExprListSizeTy Sizes;
  10885. Sizes.NumVars = Record.readInt();
  10886. Sizes.NumUniqueDeclarations = Record.readInt();
  10887. Sizes.NumComponentLists = Record.readInt();
  10888. Sizes.NumComponents = Record.readInt();
  10889. C = OMPFromClause::CreateEmpty(Context, Sizes);
  10890. break;
  10891. }
  10892. case OMPC_use_device_ptr: {
  10893. OMPMappableExprListSizeTy Sizes;
  10894. Sizes.NumVars = Record.readInt();
  10895. Sizes.NumUniqueDeclarations = Record.readInt();
  10896. Sizes.NumComponentLists = Record.readInt();
  10897. Sizes.NumComponents = Record.readInt();
  10898. C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
  10899. break;
  10900. }
  10901. case OMPC_is_device_ptr: {
  10902. OMPMappableExprListSizeTy Sizes;
  10903. Sizes.NumVars = Record.readInt();
  10904. Sizes.NumUniqueDeclarations = Record.readInt();
  10905. Sizes.NumComponentLists = Record.readInt();
  10906. Sizes.NumComponents = Record.readInt();
  10907. C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
  10908. break;
  10909. }
  10910. case OMPC_allocate:
  10911. C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
  10912. break;
  10913. }
  10914. assert(C && "Unknown OMPClause type");
  10915. Visit(C);
  10916. C->setLocStart(Record.readSourceLocation());
  10917. C->setLocEnd(Record.readSourceLocation());
  10918. return C;
  10919. }
  10920. void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
  10921. C->setPreInitStmt(Record.readSubStmt(),
  10922. static_cast<OpenMPDirectiveKind>(Record.readInt()));
  10923. }
  10924. void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
  10925. VisitOMPClauseWithPreInit(C);
  10926. C->setPostUpdateExpr(Record.readSubExpr());
  10927. }
  10928. void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
  10929. VisitOMPClauseWithPreInit(C);
  10930. C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
  10931. C->setNameModifierLoc(Record.readSourceLocation());
  10932. C->setColonLoc(Record.readSourceLocation());
  10933. C->setCondition(Record.readSubExpr());
  10934. C->setLParenLoc(Record.readSourceLocation());
  10935. }
  10936. void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
  10937. VisitOMPClauseWithPreInit(C);
  10938. C->setCondition(Record.readSubExpr());
  10939. C->setLParenLoc(Record.readSourceLocation());
  10940. }
  10941. void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
  10942. VisitOMPClauseWithPreInit(C);
  10943. C->setNumThreads(Record.readSubExpr());
  10944. C->setLParenLoc(Record.readSourceLocation());
  10945. }
  10946. void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
  10947. C->setSafelen(Record.readSubExpr());
  10948. C->setLParenLoc(Record.readSourceLocation());
  10949. }
  10950. void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
  10951. C->setSimdlen(Record.readSubExpr());
  10952. C->setLParenLoc(Record.readSourceLocation());
  10953. }
  10954. void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
  10955. C->setAllocator(Record.readExpr());
  10956. C->setLParenLoc(Record.readSourceLocation());
  10957. }
  10958. void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
  10959. C->setNumForLoops(Record.readSubExpr());
  10960. C->setLParenLoc(Record.readSourceLocation());
  10961. }
  10962. void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
  10963. C->setDefaultKind(
  10964. static_cast<OpenMPDefaultClauseKind>(Record.readInt()));
  10965. C->setLParenLoc(Record.readSourceLocation());
  10966. C->setDefaultKindKwLoc(Record.readSourceLocation());
  10967. }
  10968. void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
  10969. C->setProcBindKind(
  10970. static_cast<OpenMPProcBindClauseKind>(Record.readInt()));
  10971. C->setLParenLoc(Record.readSourceLocation());
  10972. C->setProcBindKindKwLoc(Record.readSourceLocation());
  10973. }
  10974. void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
  10975. VisitOMPClauseWithPreInit(C);
  10976. C->setScheduleKind(
  10977. static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
  10978. C->setFirstScheduleModifier(
  10979. static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
  10980. C->setSecondScheduleModifier(
  10981. static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
  10982. C->setChunkSize(Record.readSubExpr());
  10983. C->setLParenLoc(Record.readSourceLocation());
  10984. C->setFirstScheduleModifierLoc(Record.readSourceLocation());
  10985. C->setSecondScheduleModifierLoc(Record.readSourceLocation());
  10986. C->setScheduleKindLoc(Record.readSourceLocation());
  10987. C->setCommaLoc(Record.readSourceLocation());
  10988. }
  10989. void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
  10990. C->setNumForLoops(Record.readSubExpr());
  10991. for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
  10992. C->setLoopNumIterations(I, Record.readSubExpr());
  10993. for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
  10994. C->setLoopCounter(I, Record.readSubExpr());
  10995. C->setLParenLoc(Record.readSourceLocation());
  10996. }
  10997. void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
  10998. void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
  10999. void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
  11000. void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
  11001. void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
  11002. void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
  11003. void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
  11004. void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
  11005. void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
  11006. void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
  11007. void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
  11008. void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
  11009. void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
  11010. OMPUnifiedSharedMemoryClause *) {}
  11011. void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
  11012. void
  11013. OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
  11014. }
  11015. void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
  11016. OMPAtomicDefaultMemOrderClause *C) {
  11017. C->setAtomicDefaultMemOrderKind(
  11018. static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
  11019. C->setLParenLoc(Record.readSourceLocation());
  11020. C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
  11021. }
  11022. void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
  11023. C->setLParenLoc(Record.readSourceLocation());
  11024. unsigned NumVars = C->varlist_size();
  11025. SmallVector<Expr *, 16> Vars;
  11026. Vars.reserve(NumVars);
  11027. for (unsigned i = 0; i != NumVars; ++i)
  11028. Vars.push_back(Record.readSubExpr());
  11029. C->setVarRefs(Vars);
  11030. Vars.clear();
  11031. for (unsigned i = 0; i != NumVars; ++i)
  11032. Vars.push_back(Record.readSubExpr());
  11033. C->setPrivateCopies(Vars);
  11034. }
  11035. void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
  11036. VisitOMPClauseWithPreInit(C);
  11037. C->setLParenLoc(Record.readSourceLocation());
  11038. unsigned NumVars = C->varlist_size();
  11039. SmallVector<Expr *, 16> Vars;
  11040. Vars.reserve(NumVars);
  11041. for (unsigned i = 0; i != NumVars; ++i)
  11042. Vars.push_back(Record.readSubExpr());
  11043. C->setVarRefs(Vars);
  11044. Vars.clear();
  11045. for (unsigned i = 0; i != NumVars; ++i)
  11046. Vars.push_back(Record.readSubExpr());
  11047. C->setPrivateCopies(Vars);
  11048. Vars.clear();
  11049. for (unsigned i = 0; i != NumVars; ++i)
  11050. Vars.push_back(Record.readSubExpr());
  11051. C->setInits(Vars);
  11052. }
  11053. void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
  11054. VisitOMPClauseWithPostUpdate(C);
  11055. C->setLParenLoc(Record.readSourceLocation());
  11056. unsigned NumVars = C->varlist_size();
  11057. SmallVector<Expr *, 16> Vars;
  11058. Vars.reserve(NumVars);
  11059. for (unsigned i = 0; i != NumVars; ++i)
  11060. Vars.push_back(Record.readSubExpr());
  11061. C->setVarRefs(Vars);
  11062. Vars.clear();
  11063. for (unsigned i = 0; i != NumVars; ++i)
  11064. Vars.push_back(Record.readSubExpr());
  11065. C->setPrivateCopies(Vars);
  11066. Vars.clear();
  11067. for (unsigned i = 0; i != NumVars; ++i)
  11068. Vars.push_back(Record.readSubExpr());
  11069. C->setSourceExprs(Vars);
  11070. Vars.clear();
  11071. for (unsigned i = 0; i != NumVars; ++i)
  11072. Vars.push_back(Record.readSubExpr());
  11073. C->setDestinationExprs(Vars);
  11074. Vars.clear();
  11075. for (unsigned i = 0; i != NumVars; ++i)
  11076. Vars.push_back(Record.readSubExpr());
  11077. C->setAssignmentOps(Vars);
  11078. }
  11079. void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
  11080. C->setLParenLoc(Record.readSourceLocation());
  11081. unsigned NumVars = C->varlist_size();
  11082. SmallVector<Expr *, 16> Vars;
  11083. Vars.reserve(NumVars);
  11084. for (unsigned i = 0; i != NumVars; ++i)
  11085. Vars.push_back(Record.readSubExpr());
  11086. C->setVarRefs(Vars);
  11087. }
  11088. void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
  11089. VisitOMPClauseWithPostUpdate(C);
  11090. C->setLParenLoc(Record.readSourceLocation());
  11091. C->setColonLoc(Record.readSourceLocation());
  11092. NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
  11093. DeclarationNameInfo DNI;
  11094. Record.readDeclarationNameInfo(DNI);
  11095. C->setQualifierLoc(NNSL);
  11096. C->setNameInfo(DNI);
  11097. unsigned NumVars = C->varlist_size();
  11098. SmallVector<Expr *, 16> Vars;
  11099. Vars.reserve(NumVars);
  11100. for (unsigned i = 0; i != NumVars; ++i)
  11101. Vars.push_back(Record.readSubExpr());
  11102. C->setVarRefs(Vars);
  11103. Vars.clear();
  11104. for (unsigned i = 0; i != NumVars; ++i)
  11105. Vars.push_back(Record.readSubExpr());
  11106. C->setPrivates(Vars);
  11107. Vars.clear();
  11108. for (unsigned i = 0; i != NumVars; ++i)
  11109. Vars.push_back(Record.readSubExpr());
  11110. C->setLHSExprs(Vars);
  11111. Vars.clear();
  11112. for (unsigned i = 0; i != NumVars; ++i)
  11113. Vars.push_back(Record.readSubExpr());
  11114. C->setRHSExprs(Vars);
  11115. Vars.clear();
  11116. for (unsigned i = 0; i != NumVars; ++i)
  11117. Vars.push_back(Record.readSubExpr());
  11118. C->setReductionOps(Vars);
  11119. }
  11120. void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
  11121. VisitOMPClauseWithPostUpdate(C);
  11122. C->setLParenLoc(Record.readSourceLocation());
  11123. C->setColonLoc(Record.readSourceLocation());
  11124. NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
  11125. DeclarationNameInfo DNI;
  11126. Record.readDeclarationNameInfo(DNI);
  11127. C->setQualifierLoc(NNSL);
  11128. C->setNameInfo(DNI);
  11129. unsigned NumVars = C->varlist_size();
  11130. SmallVector<Expr *, 16> Vars;
  11131. Vars.reserve(NumVars);
  11132. for (unsigned I = 0; I != NumVars; ++I)
  11133. Vars.push_back(Record.readSubExpr());
  11134. C->setVarRefs(Vars);
  11135. Vars.clear();
  11136. for (unsigned I = 0; I != NumVars; ++I)
  11137. Vars.push_back(Record.readSubExpr());
  11138. C->setPrivates(Vars);
  11139. Vars.clear();
  11140. for (unsigned I = 0; I != NumVars; ++I)
  11141. Vars.push_back(Record.readSubExpr());
  11142. C->setLHSExprs(Vars);
  11143. Vars.clear();
  11144. for (unsigned I = 0; I != NumVars; ++I)
  11145. Vars.push_back(Record.readSubExpr());
  11146. C->setRHSExprs(Vars);
  11147. Vars.clear();
  11148. for (unsigned I = 0; I != NumVars; ++I)
  11149. Vars.push_back(Record.readSubExpr());
  11150. C->setReductionOps(Vars);
  11151. }
  11152. void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
  11153. VisitOMPClauseWithPostUpdate(C);
  11154. C->setLParenLoc(Record.readSourceLocation());
  11155. C->setColonLoc(Record.readSourceLocation());
  11156. NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
  11157. DeclarationNameInfo DNI;
  11158. Record.readDeclarationNameInfo(DNI);
  11159. C->setQualifierLoc(NNSL);
  11160. C->setNameInfo(DNI);
  11161. unsigned NumVars = C->varlist_size();
  11162. SmallVector<Expr *, 16> Vars;
  11163. Vars.reserve(NumVars);
  11164. for (unsigned I = 0; I != NumVars; ++I)
  11165. Vars.push_back(Record.readSubExpr());
  11166. C->setVarRefs(Vars);
  11167. Vars.clear();
  11168. for (unsigned I = 0; I != NumVars; ++I)
  11169. Vars.push_back(Record.readSubExpr());
  11170. C->setPrivates(Vars);
  11171. Vars.clear();
  11172. for (unsigned I = 0; I != NumVars; ++I)
  11173. Vars.push_back(Record.readSubExpr());
  11174. C->setLHSExprs(Vars);
  11175. Vars.clear();
  11176. for (unsigned I = 0; I != NumVars; ++I)
  11177. Vars.push_back(Record.readSubExpr());
  11178. C->setRHSExprs(Vars);
  11179. Vars.clear();
  11180. for (unsigned I = 0; I != NumVars; ++I)
  11181. Vars.push_back(Record.readSubExpr());
  11182. C->setReductionOps(Vars);
  11183. Vars.clear();
  11184. for (unsigned I = 0; I != NumVars; ++I)
  11185. Vars.push_back(Record.readSubExpr());
  11186. C->setTaskgroupDescriptors(Vars);
  11187. }
  11188. void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
  11189. VisitOMPClauseWithPostUpdate(C);
  11190. C->setLParenLoc(Record.readSourceLocation());
  11191. C->setColonLoc(Record.readSourceLocation());
  11192. C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
  11193. C->setModifierLoc(Record.readSourceLocation());
  11194. unsigned NumVars = C->varlist_size();
  11195. SmallVector<Expr *, 16> Vars;
  11196. Vars.reserve(NumVars);
  11197. for (unsigned i = 0; i != NumVars; ++i)
  11198. Vars.push_back(Record.readSubExpr());
  11199. C->setVarRefs(Vars);
  11200. Vars.clear();
  11201. for (unsigned i = 0; i != NumVars; ++i)
  11202. Vars.push_back(Record.readSubExpr());
  11203. C->setPrivates(Vars);
  11204. Vars.clear();
  11205. for (unsigned i = 0; i != NumVars; ++i)
  11206. Vars.push_back(Record.readSubExpr());
  11207. C->setInits(Vars);
  11208. Vars.clear();
  11209. for (unsigned i = 0; i != NumVars; ++i)
  11210. Vars.push_back(Record.readSubExpr());
  11211. C->setUpdates(Vars);
  11212. Vars.clear();
  11213. for (unsigned i = 0; i != NumVars; ++i)
  11214. Vars.push_back(Record.readSubExpr());
  11215. C->setFinals(Vars);
  11216. C->setStep(Record.readSubExpr());
  11217. C->setCalcStep(Record.readSubExpr());
  11218. Vars.clear();
  11219. for (unsigned I = 0; I != NumVars + 1; ++I)
  11220. Vars.push_back(Record.readSubExpr());
  11221. C->setUsedExprs(Vars);
  11222. }
  11223. void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
  11224. C->setLParenLoc(Record.readSourceLocation());
  11225. C->setColonLoc(Record.readSourceLocation());
  11226. unsigned NumVars = C->varlist_size();
  11227. SmallVector<Expr *, 16> Vars;
  11228. Vars.reserve(NumVars);
  11229. for (unsigned i = 0; i != NumVars; ++i)
  11230. Vars.push_back(Record.readSubExpr());
  11231. C->setVarRefs(Vars);
  11232. C->setAlignment(Record.readSubExpr());
  11233. }
  11234. void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
  11235. C->setLParenLoc(Record.readSourceLocation());
  11236. unsigned NumVars = C->varlist_size();
  11237. SmallVector<Expr *, 16> Exprs;
  11238. Exprs.reserve(NumVars);
  11239. for (unsigned i = 0; i != NumVars; ++i)
  11240. Exprs.push_back(Record.readSubExpr());
  11241. C->setVarRefs(Exprs);
  11242. Exprs.clear();
  11243. for (unsigned i = 0; i != NumVars; ++i)
  11244. Exprs.push_back(Record.readSubExpr());
  11245. C->setSourceExprs(Exprs);
  11246. Exprs.clear();
  11247. for (unsigned i = 0; i != NumVars; ++i)
  11248. Exprs.push_back(Record.readSubExpr());
  11249. C->setDestinationExprs(Exprs);
  11250. Exprs.clear();
  11251. for (unsigned i = 0; i != NumVars; ++i)
  11252. Exprs.push_back(Record.readSubExpr());
  11253. C->setAssignmentOps(Exprs);
  11254. }
  11255. void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
  11256. C->setLParenLoc(Record.readSourceLocation());
  11257. unsigned NumVars = C->varlist_size();
  11258. SmallVector<Expr *, 16> Exprs;
  11259. Exprs.reserve(NumVars);
  11260. for (unsigned i = 0; i != NumVars; ++i)
  11261. Exprs.push_back(Record.readSubExpr());
  11262. C->setVarRefs(Exprs);
  11263. Exprs.clear();
  11264. for (unsigned i = 0; i != NumVars; ++i)
  11265. Exprs.push_back(Record.readSubExpr());
  11266. C->setSourceExprs(Exprs);
  11267. Exprs.clear();
  11268. for (unsigned i = 0; i != NumVars; ++i)
  11269. Exprs.push_back(Record.readSubExpr());
  11270. C->setDestinationExprs(Exprs);
  11271. Exprs.clear();
  11272. for (unsigned i = 0; i != NumVars; ++i)
  11273. Exprs.push_back(Record.readSubExpr());
  11274. C->setAssignmentOps(Exprs);
  11275. }
  11276. void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
  11277. C->setLParenLoc(Record.readSourceLocation());
  11278. unsigned NumVars = C->varlist_size();
  11279. SmallVector<Expr *, 16> Vars;
  11280. Vars.reserve(NumVars);
  11281. for (unsigned i = 0; i != NumVars; ++i)
  11282. Vars.push_back(Record.readSubExpr());
  11283. C->setVarRefs(Vars);
  11284. }
  11285. void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
  11286. C->setLParenLoc(Record.readSourceLocation());
  11287. C->setDependencyKind(
  11288. static_cast<OpenMPDependClauseKind>(Record.readInt()));
  11289. C->setDependencyLoc(Record.readSourceLocation());
  11290. C->setColonLoc(Record.readSourceLocation());
  11291. unsigned NumVars = C->varlist_size();
  11292. SmallVector<Expr *, 16> Vars;
  11293. Vars.reserve(NumVars);
  11294. for (unsigned I = 0; I != NumVars; ++I)
  11295. Vars.push_back(Record.readSubExpr());
  11296. C->setVarRefs(Vars);
  11297. for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
  11298. C->setLoopData(I, Record.readSubExpr());
  11299. }
  11300. void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
  11301. VisitOMPClauseWithPreInit(C);
  11302. C->setDevice(Record.readSubExpr());
  11303. C->setLParenLoc(Record.readSourceLocation());
  11304. }
  11305. void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
  11306. C->setLParenLoc(Record.readSourceLocation());
  11307. for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
  11308. C->setMapTypeModifier(
  11309. I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
  11310. C->setMapTypeModifierLoc(I, Record.readSourceLocation());
  11311. }
  11312. C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
  11313. DeclarationNameInfo DNI;
  11314. Record.readDeclarationNameInfo(DNI);
  11315. C->setMapperIdInfo(DNI);
  11316. C->setMapType(
  11317. static_cast<OpenMPMapClauseKind>(Record.readInt()));
  11318. C->setMapLoc(Record.readSourceLocation());
  11319. C->setColonLoc(Record.readSourceLocation());
  11320. auto NumVars = C->varlist_size();
  11321. auto UniqueDecls = C->getUniqueDeclarationsNum();
  11322. auto TotalLists = C->getTotalComponentListNum();
  11323. auto TotalComponents = C->getTotalComponentsNum();
  11324. SmallVector<Expr *, 16> Vars;
  11325. Vars.reserve(NumVars);
  11326. for (unsigned i = 0; i != NumVars; ++i)
  11327. Vars.push_back(Record.readExpr());
  11328. C->setVarRefs(Vars);
  11329. SmallVector<Expr *, 16> UDMappers;
  11330. UDMappers.reserve(NumVars);
  11331. for (unsigned I = 0; I < NumVars; ++I)
  11332. UDMappers.push_back(Record.readExpr());
  11333. C->setUDMapperRefs(UDMappers);
  11334. SmallVector<ValueDecl *, 16> Decls;
  11335. Decls.reserve(UniqueDecls);
  11336. for (unsigned i = 0; i < UniqueDecls; ++i)
  11337. Decls.push_back(Record.readDeclAs<ValueDecl>());
  11338. C->setUniqueDecls(Decls);
  11339. SmallVector<unsigned, 16> ListsPerDecl;
  11340. ListsPerDecl.reserve(UniqueDecls);
  11341. for (unsigned i = 0; i < UniqueDecls; ++i)
  11342. ListsPerDecl.push_back(Record.readInt());
  11343. C->setDeclNumLists(ListsPerDecl);
  11344. SmallVector<unsigned, 32> ListSizes;
  11345. ListSizes.reserve(TotalLists);
  11346. for (unsigned i = 0; i < TotalLists; ++i)
  11347. ListSizes.push_back(Record.readInt());
  11348. C->setComponentListSizes(ListSizes);
  11349. SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
  11350. Components.reserve(TotalComponents);
  11351. for (unsigned i = 0; i < TotalComponents; ++i) {
  11352. Expr *AssociatedExpr = Record.readExpr();
  11353. auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
  11354. Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
  11355. AssociatedExpr, AssociatedDecl));
  11356. }
  11357. C->setComponents(Components, ListSizes);
  11358. }
  11359. void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
  11360. C->setLParenLoc(Record.readSourceLocation());
  11361. C->setColonLoc(Record.readSourceLocation());
  11362. C->setAllocator(Record.readSubExpr());
  11363. unsigned NumVars = C->varlist_size();
  11364. SmallVector<Expr *, 16> Vars;
  11365. Vars.reserve(NumVars);
  11366. for (unsigned i = 0; i != NumVars; ++i)
  11367. Vars.push_back(Record.readSubExpr());
  11368. C->setVarRefs(Vars);
  11369. }
  11370. void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
  11371. VisitOMPClauseWithPreInit(C);
  11372. C->setNumTeams(Record.readSubExpr());
  11373. C->setLParenLoc(Record.readSourceLocation());
  11374. }
  11375. void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
  11376. VisitOMPClauseWithPreInit(C);
  11377. C->setThreadLimit(Record.readSubExpr());
  11378. C->setLParenLoc(Record.readSourceLocation());
  11379. }
  11380. void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
  11381. VisitOMPClauseWithPreInit(C);
  11382. C->setPriority(Record.readSubExpr());
  11383. C->setLParenLoc(Record.readSourceLocation());
  11384. }
  11385. void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
  11386. VisitOMPClauseWithPreInit(C);
  11387. C->setGrainsize(Record.readSubExpr());
  11388. C->setLParenLoc(Record.readSourceLocation());
  11389. }
  11390. void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
  11391. VisitOMPClauseWithPreInit(C);
  11392. C->setNumTasks(Record.readSubExpr());
  11393. C->setLParenLoc(Record.readSourceLocation());
  11394. }
  11395. void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
  11396. C->setHint(Record.readSubExpr());
  11397. C->setLParenLoc(Record.readSourceLocation());
  11398. }
  11399. void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
  11400. VisitOMPClauseWithPreInit(C);
  11401. C->setDistScheduleKind(
  11402. static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
  11403. C->setChunkSize(Record.readSubExpr());
  11404. C->setLParenLoc(Record.readSourceLocation());
  11405. C->setDistScheduleKindLoc(Record.readSourceLocation());
  11406. C->setCommaLoc(Record.readSourceLocation());
  11407. }
  11408. void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
  11409. C->setDefaultmapKind(
  11410. static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
  11411. C->setDefaultmapModifier(
  11412. static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
  11413. C->setLParenLoc(Record.readSourceLocation());
  11414. C->setDefaultmapModifierLoc(Record.readSourceLocation());
  11415. C->setDefaultmapKindLoc(Record.readSourceLocation());
  11416. }
  11417. void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
  11418. C->setLParenLoc(Record.readSourceLocation());
  11419. C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
  11420. DeclarationNameInfo DNI;
  11421. Record.readDeclarationNameInfo(DNI);
  11422. C->setMapperIdInfo(DNI);
  11423. auto NumVars = C->varlist_size();
  11424. auto UniqueDecls = C->getUniqueDeclarationsNum();
  11425. auto TotalLists = C->getTotalComponentListNum();
  11426. auto TotalComponents = C->getTotalComponentsNum();
  11427. SmallVector<Expr *, 16> Vars;
  11428. Vars.reserve(NumVars);
  11429. for (unsigned i = 0; i != NumVars; ++i)
  11430. Vars.push_back(Record.readSubExpr());
  11431. C->setVarRefs(Vars);
  11432. SmallVector<Expr *, 16> UDMappers;
  11433. UDMappers.reserve(NumVars);
  11434. for (unsigned I = 0; I < NumVars; ++I)
  11435. UDMappers.push_back(Record.readSubExpr());
  11436. C->setUDMapperRefs(UDMappers);
  11437. SmallVector<ValueDecl *, 16> Decls;
  11438. Decls.reserve(UniqueDecls);
  11439. for (unsigned i = 0; i < UniqueDecls; ++i)
  11440. Decls.push_back(Record.readDeclAs<ValueDecl>());
  11441. C->setUniqueDecls(Decls);
  11442. SmallVector<unsigned, 16> ListsPerDecl;
  11443. ListsPerDecl.reserve(UniqueDecls);
  11444. for (unsigned i = 0; i < UniqueDecls; ++i)
  11445. ListsPerDecl.push_back(Record.readInt());
  11446. C->setDeclNumLists(ListsPerDecl);
  11447. SmallVector<unsigned, 32> ListSizes;
  11448. ListSizes.reserve(TotalLists);
  11449. for (unsigned i = 0; i < TotalLists; ++i)
  11450. ListSizes.push_back(Record.readInt());
  11451. C->setComponentListSizes(ListSizes);
  11452. SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
  11453. Components.reserve(TotalComponents);
  11454. for (unsigned i = 0; i < TotalComponents; ++i) {
  11455. Expr *AssociatedExpr = Record.readSubExpr();
  11456. auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
  11457. Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
  11458. AssociatedExpr, AssociatedDecl));
  11459. }
  11460. C->setComponents(Components, ListSizes);
  11461. }
  11462. void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
  11463. C->setLParenLoc(Record.readSourceLocation());
  11464. C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
  11465. DeclarationNameInfo DNI;
  11466. Record.readDeclarationNameInfo(DNI);
  11467. C->setMapperIdInfo(DNI);
  11468. auto NumVars = C->varlist_size();
  11469. auto UniqueDecls = C->getUniqueDeclarationsNum();
  11470. auto TotalLists = C->getTotalComponentListNum();
  11471. auto TotalComponents = C->getTotalComponentsNum();
  11472. SmallVector<Expr *, 16> Vars;
  11473. Vars.reserve(NumVars);
  11474. for (unsigned i = 0; i != NumVars; ++i)
  11475. Vars.push_back(Record.readSubExpr());
  11476. C->setVarRefs(Vars);
  11477. SmallVector<Expr *, 16> UDMappers;
  11478. UDMappers.reserve(NumVars);
  11479. for (unsigned I = 0; I < NumVars; ++I)
  11480. UDMappers.push_back(Record.readSubExpr());
  11481. C->setUDMapperRefs(UDMappers);
  11482. SmallVector<ValueDecl *, 16> Decls;
  11483. Decls.reserve(UniqueDecls);
  11484. for (unsigned i = 0; i < UniqueDecls; ++i)
  11485. Decls.push_back(Record.readDeclAs<ValueDecl>());
  11486. C->setUniqueDecls(Decls);
  11487. SmallVector<unsigned, 16> ListsPerDecl;
  11488. ListsPerDecl.reserve(UniqueDecls);
  11489. for (unsigned i = 0; i < UniqueDecls; ++i)
  11490. ListsPerDecl.push_back(Record.readInt());
  11491. C->setDeclNumLists(ListsPerDecl);
  11492. SmallVector<unsigned, 32> ListSizes;
  11493. ListSizes.reserve(TotalLists);
  11494. for (unsigned i = 0; i < TotalLists; ++i)
  11495. ListSizes.push_back(Record.readInt());
  11496. C->setComponentListSizes(ListSizes);
  11497. SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
  11498. Components.reserve(TotalComponents);
  11499. for (unsigned i = 0; i < TotalComponents; ++i) {
  11500. Expr *AssociatedExpr = Record.readSubExpr();
  11501. auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
  11502. Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
  11503. AssociatedExpr, AssociatedDecl));
  11504. }
  11505. C->setComponents(Components, ListSizes);
  11506. }
  11507. void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
  11508. C->setLParenLoc(Record.readSourceLocation());
  11509. auto NumVars = C->varlist_size();
  11510. auto UniqueDecls = C->getUniqueDeclarationsNum();
  11511. auto TotalLists = C->getTotalComponentListNum();
  11512. auto TotalComponents = C->getTotalComponentsNum();
  11513. SmallVector<Expr *, 16> Vars;
  11514. Vars.reserve(NumVars);
  11515. for (unsigned i = 0; i != NumVars; ++i)
  11516. Vars.push_back(Record.readSubExpr());
  11517. C->setVarRefs(Vars);
  11518. Vars.clear();
  11519. for (unsigned i = 0; i != NumVars; ++i)
  11520. Vars.push_back(Record.readSubExpr());
  11521. C->setPrivateCopies(Vars);
  11522. Vars.clear();
  11523. for (unsigned i = 0; i != NumVars; ++i)
  11524. Vars.push_back(Record.readSubExpr());
  11525. C->setInits(Vars);
  11526. SmallVector<ValueDecl *, 16> Decls;
  11527. Decls.reserve(UniqueDecls);
  11528. for (unsigned i = 0; i < UniqueDecls; ++i)
  11529. Decls.push_back(Record.readDeclAs<ValueDecl>());
  11530. C->setUniqueDecls(Decls);
  11531. SmallVector<unsigned, 16> ListsPerDecl;
  11532. ListsPerDecl.reserve(UniqueDecls);
  11533. for (unsigned i = 0; i < UniqueDecls; ++i)
  11534. ListsPerDecl.push_back(Record.readInt());
  11535. C->setDeclNumLists(ListsPerDecl);
  11536. SmallVector<unsigned, 32> ListSizes;
  11537. ListSizes.reserve(TotalLists);
  11538. for (unsigned i = 0; i < TotalLists; ++i)
  11539. ListSizes.push_back(Record.readInt());
  11540. C->setComponentListSizes(ListSizes);
  11541. SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
  11542. Components.reserve(TotalComponents);
  11543. for (unsigned i = 0; i < TotalComponents; ++i) {
  11544. Expr *AssociatedExpr = Record.readSubExpr();
  11545. auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
  11546. Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
  11547. AssociatedExpr, AssociatedDecl));
  11548. }
  11549. C->setComponents(Components, ListSizes);
  11550. }
  11551. void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
  11552. C->setLParenLoc(Record.readSourceLocation());
  11553. auto NumVars = C->varlist_size();
  11554. auto UniqueDecls = C->getUniqueDeclarationsNum();
  11555. auto TotalLists = C->getTotalComponentListNum();
  11556. auto TotalComponents = C->getTotalComponentsNum();
  11557. SmallVector<Expr *, 16> Vars;
  11558. Vars.reserve(NumVars);
  11559. for (unsigned i = 0; i != NumVars; ++i)
  11560. Vars.push_back(Record.readSubExpr());
  11561. C->setVarRefs(Vars);
  11562. Vars.clear();
  11563. SmallVector<ValueDecl *, 16> Decls;
  11564. Decls.reserve(UniqueDecls);
  11565. for (unsigned i = 0; i < UniqueDecls; ++i)
  11566. Decls.push_back(Record.readDeclAs<ValueDecl>());
  11567. C->setUniqueDecls(Decls);
  11568. SmallVector<unsigned, 16> ListsPerDecl;
  11569. ListsPerDecl.reserve(UniqueDecls);
  11570. for (unsigned i = 0; i < UniqueDecls; ++i)
  11571. ListsPerDecl.push_back(Record.readInt());
  11572. C->setDeclNumLists(ListsPerDecl);
  11573. SmallVector<unsigned, 32> ListSizes;
  11574. ListSizes.reserve(TotalLists);
  11575. for (unsigned i = 0; i < TotalLists; ++i)
  11576. ListSizes.push_back(Record.readInt());
  11577. C->setComponentListSizes(ListSizes);
  11578. SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
  11579. Components.reserve(TotalComponents);
  11580. for (unsigned i = 0; i < TotalComponents; ++i) {
  11581. Expr *AssociatedExpr = Record.readSubExpr();
  11582. auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
  11583. Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
  11584. AssociatedExpr, AssociatedDecl));
  11585. }
  11586. C->setComponents(Components, ListSizes);
  11587. }