ASTReader.cpp 449 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561
  1. //===- ASTReader.cpp - AST File Reader ------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the ASTReader class, which reads AST files.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Serialization/ASTReader.h"
  14. #include "ASTCommon.h"
  15. #include "ASTReaderInternals.h"
  16. #include "clang/AST/ASTConsumer.h"
  17. #include "clang/AST/ASTContext.h"
  18. #include "clang/AST/ASTMutationListener.h"
  19. #include "clang/AST/ASTUnresolvedSet.h"
  20. #include "clang/AST/Decl.h"
  21. #include "clang/AST/DeclBase.h"
  22. #include "clang/AST/DeclCXX.h"
  23. #include "clang/AST/DeclFriend.h"
  24. #include "clang/AST/DeclGroup.h"
  25. #include "clang/AST/DeclObjC.h"
  26. #include "clang/AST/DeclTemplate.h"
  27. #include "clang/AST/DeclarationName.h"
  28. #include "clang/AST/Expr.h"
  29. #include "clang/AST/ExprCXX.h"
  30. #include "clang/AST/ExternalASTSource.h"
  31. #include "clang/AST/NestedNameSpecifier.h"
  32. #include "clang/AST/ODRHash.h"
  33. #include "clang/AST/RawCommentList.h"
  34. #include "clang/AST/TemplateBase.h"
  35. #include "clang/AST/TemplateName.h"
  36. #include "clang/AST/Type.h"
  37. #include "clang/AST/TypeLoc.h"
  38. #include "clang/AST/TypeLocVisitor.h"
  39. #include "clang/AST/UnresolvedSet.h"
  40. #include "clang/Basic/CommentOptions.h"
  41. #include "clang/Basic/Diagnostic.h"
  42. #include "clang/Basic/DiagnosticOptions.h"
  43. #include "clang/Basic/ExceptionSpecificationType.h"
  44. #include "clang/Basic/FileManager.h"
  45. #include "clang/Basic/FileSystemOptions.h"
  46. #include "clang/Basic/IdentifierTable.h"
  47. #include "clang/Basic/LLVM.h"
  48. #include "clang/Basic/LangOptions.h"
  49. #include "clang/Basic/MemoryBufferCache.h"
  50. #include "clang/Basic/Module.h"
  51. #include "clang/Basic/ObjCRuntime.h"
  52. #include "clang/Basic/OperatorKinds.h"
  53. #include "clang/Basic/PragmaKinds.h"
  54. #include "clang/Basic/Sanitizers.h"
  55. #include "clang/Basic/SourceLocation.h"
  56. #include "clang/Basic/SourceManager.h"
  57. #include "clang/Basic/SourceManagerInternals.h"
  58. #include "clang/Basic/Specifiers.h"
  59. #include "clang/Basic/TargetInfo.h"
  60. #include "clang/Basic/TargetOptions.h"
  61. #include "clang/Basic/TokenKinds.h"
  62. #include "clang/Basic/Version.h"
  63. #include "clang/Frontend/PCHContainerOperations.h"
  64. #include "clang/Lex/HeaderSearch.h"
  65. #include "clang/Lex/HeaderSearchOptions.h"
  66. #include "clang/Lex/MacroInfo.h"
  67. #include "clang/Lex/ModuleMap.h"
  68. #include "clang/Lex/PreprocessingRecord.h"
  69. #include "clang/Lex/Preprocessor.h"
  70. #include "clang/Lex/PreprocessorOptions.h"
  71. #include "clang/Lex/Token.h"
  72. #include "clang/Sema/ObjCMethodList.h"
  73. #include "clang/Sema/Scope.h"
  74. #include "clang/Sema/Sema.h"
  75. #include "clang/Sema/Weak.h"
  76. #include "clang/Serialization/ASTBitCodes.h"
  77. #include "clang/Serialization/ASTDeserializationListener.h"
  78. #include "clang/Serialization/ContinuousRangeMap.h"
  79. #include "clang/Serialization/GlobalModuleIndex.h"
  80. #include "clang/Serialization/Module.h"
  81. #include "clang/Serialization/ModuleFileExtension.h"
  82. #include "clang/Serialization/ModuleManager.h"
  83. #include "clang/Serialization/SerializationDiagnostic.h"
  84. #include "llvm/ADT/APFloat.h"
  85. #include "llvm/ADT/APInt.h"
  86. #include "llvm/ADT/APSInt.h"
  87. #include "llvm/ADT/ArrayRef.h"
  88. #include "llvm/ADT/DenseMap.h"
  89. #include "llvm/ADT/FoldingSet.h"
  90. #include "llvm/ADT/Hashing.h"
  91. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  92. #include "llvm/ADT/None.h"
  93. #include "llvm/ADT/Optional.h"
  94. #include "llvm/ADT/STLExtras.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/Bitcode/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. Cursor.JumpToBit(Offset);
  1007. RecordData Record;
  1008. StringRef Blob;
  1009. unsigned Code = Cursor.ReadCode();
  1010. unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
  1011. if (RecCode != DECL_CONTEXT_LEXICAL) {
  1012. Error("Expected lexical block");
  1013. return true;
  1014. }
  1015. assert(!isa<TranslationUnitDecl>(DC) &&
  1016. "expected a TU_UPDATE_LEXICAL record for TU");
  1017. // If we are handling a C++ class template instantiation, we can see multiple
  1018. // lexical updates for the same record. It's important that we select only one
  1019. // of them, so that field numbering works properly. Just pick the first one we
  1020. // see.
  1021. auto &Lex = LexicalDecls[DC];
  1022. if (!Lex.first) {
  1023. Lex = std::make_pair(
  1024. &M, llvm::makeArrayRef(
  1025. reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
  1026. Blob.data()),
  1027. Blob.size() / 4));
  1028. }
  1029. DC->setHasExternalLexicalStorage(true);
  1030. return false;
  1031. }
  1032. bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
  1033. BitstreamCursor &Cursor,
  1034. uint64_t Offset,
  1035. DeclID ID) {
  1036. assert(Offset != 0);
  1037. SavedStreamPosition SavedPosition(Cursor);
  1038. Cursor.JumpToBit(Offset);
  1039. RecordData Record;
  1040. StringRef Blob;
  1041. unsigned Code = Cursor.ReadCode();
  1042. unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
  1043. if (RecCode != DECL_CONTEXT_VISIBLE) {
  1044. Error("Expected visible lookup table block");
  1045. return true;
  1046. }
  1047. // We can't safely determine the primary context yet, so delay attaching the
  1048. // lookup table until we're done with recursive deserialization.
  1049. auto *Data = (const unsigned char*)Blob.data();
  1050. PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
  1051. return false;
  1052. }
  1053. void ASTReader::Error(StringRef Msg) const {
  1054. Error(diag::err_fe_pch_malformed, Msg);
  1055. if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
  1056. !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
  1057. Diag(diag::note_module_cache_path)
  1058. << PP.getHeaderSearchInfo().getModuleCachePath();
  1059. }
  1060. }
  1061. void ASTReader::Error(unsigned DiagID,
  1062. StringRef Arg1, StringRef Arg2) const {
  1063. if (Diags.isDiagnosticInFlight())
  1064. Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
  1065. else
  1066. Diag(DiagID) << Arg1 << Arg2;
  1067. }
  1068. //===----------------------------------------------------------------------===//
  1069. // Source Manager Deserialization
  1070. //===----------------------------------------------------------------------===//
  1071. /// Read the line table in the source manager block.
  1072. /// \returns true if there was an error.
  1073. bool ASTReader::ParseLineTable(ModuleFile &F,
  1074. const RecordData &Record) {
  1075. unsigned Idx = 0;
  1076. LineTableInfo &LineTable = SourceMgr.getLineTable();
  1077. // Parse the file names
  1078. std::map<int, int> FileIDs;
  1079. FileIDs[-1] = -1; // For unspecified filenames.
  1080. for (unsigned I = 0; Record[Idx]; ++I) {
  1081. // Extract the file name
  1082. auto Filename = ReadPath(F, Record, Idx);
  1083. FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
  1084. }
  1085. ++Idx;
  1086. // Parse the line entries
  1087. std::vector<LineEntry> Entries;
  1088. while (Idx < Record.size()) {
  1089. int FID = Record[Idx++];
  1090. assert(FID >= 0 && "Serialized line entries for non-local file.");
  1091. // Remap FileID from 1-based old view.
  1092. FID += F.SLocEntryBaseID - 1;
  1093. // Extract the line entries
  1094. unsigned NumEntries = Record[Idx++];
  1095. assert(NumEntries && "no line entries for file ID");
  1096. Entries.clear();
  1097. Entries.reserve(NumEntries);
  1098. for (unsigned I = 0; I != NumEntries; ++I) {
  1099. unsigned FileOffset = Record[Idx++];
  1100. unsigned LineNo = Record[Idx++];
  1101. int FilenameID = FileIDs[Record[Idx++]];
  1102. SrcMgr::CharacteristicKind FileKind
  1103. = (SrcMgr::CharacteristicKind)Record[Idx++];
  1104. unsigned IncludeOffset = Record[Idx++];
  1105. Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
  1106. FileKind, IncludeOffset));
  1107. }
  1108. LineTable.AddEntry(FileID::get(FID), Entries);
  1109. }
  1110. return false;
  1111. }
  1112. /// Read a source manager block
  1113. bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
  1114. using namespace SrcMgr;
  1115. BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
  1116. // Set the source-location entry cursor to the current position in
  1117. // the stream. This cursor will be used to read the contents of the
  1118. // source manager block initially, and then lazily read
  1119. // source-location entries as needed.
  1120. SLocEntryCursor = F.Stream;
  1121. // The stream itself is going to skip over the source manager block.
  1122. if (F.Stream.SkipBlock()) {
  1123. Error("malformed block record in AST file");
  1124. return true;
  1125. }
  1126. // Enter the source manager block.
  1127. if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
  1128. Error("malformed source manager block record in AST file");
  1129. return true;
  1130. }
  1131. RecordData Record;
  1132. while (true) {
  1133. llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
  1134. switch (E.Kind) {
  1135. case llvm::BitstreamEntry::SubBlock: // Handled for us already.
  1136. case llvm::BitstreamEntry::Error:
  1137. Error("malformed block record in AST file");
  1138. return true;
  1139. case llvm::BitstreamEntry::EndBlock:
  1140. return false;
  1141. case llvm::BitstreamEntry::Record:
  1142. // The interesting case.
  1143. break;
  1144. }
  1145. // Read a record.
  1146. Record.clear();
  1147. StringRef Blob;
  1148. switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
  1149. default: // Default behavior: ignore.
  1150. break;
  1151. case SM_SLOC_FILE_ENTRY:
  1152. case SM_SLOC_BUFFER_ENTRY:
  1153. case SM_SLOC_EXPANSION_ENTRY:
  1154. // Once we hit one of the source location entries, we're done.
  1155. return false;
  1156. }
  1157. }
  1158. }
  1159. /// If a header file is not found at the path that we expect it to be
  1160. /// and the PCH file was moved from its original location, try to resolve the
  1161. /// file by assuming that header+PCH were moved together and the header is in
  1162. /// the same place relative to the PCH.
  1163. static std::string
  1164. resolveFileRelativeToOriginalDir(const std::string &Filename,
  1165. const std::string &OriginalDir,
  1166. const std::string &CurrDir) {
  1167. assert(OriginalDir != CurrDir &&
  1168. "No point trying to resolve the file if the PCH dir didn't change");
  1169. using namespace llvm::sys;
  1170. SmallString<128> filePath(Filename);
  1171. fs::make_absolute(filePath);
  1172. assert(path::is_absolute(OriginalDir));
  1173. SmallString<128> currPCHPath(CurrDir);
  1174. path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
  1175. fileDirE = path::end(path::parent_path(filePath));
  1176. path::const_iterator origDirI = path::begin(OriginalDir),
  1177. origDirE = path::end(OriginalDir);
  1178. // Skip the common path components from filePath and OriginalDir.
  1179. while (fileDirI != fileDirE && origDirI != origDirE &&
  1180. *fileDirI == *origDirI) {
  1181. ++fileDirI;
  1182. ++origDirI;
  1183. }
  1184. for (; origDirI != origDirE; ++origDirI)
  1185. path::append(currPCHPath, "..");
  1186. path::append(currPCHPath, fileDirI, fileDirE);
  1187. path::append(currPCHPath, path::filename(Filename));
  1188. return currPCHPath.str();
  1189. }
  1190. bool ASTReader::ReadSLocEntry(int ID) {
  1191. if (ID == 0)
  1192. return false;
  1193. if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
  1194. Error("source location entry ID out-of-range for AST file");
  1195. return true;
  1196. }
  1197. // Local helper to read the (possibly-compressed) buffer data following the
  1198. // entry record.
  1199. auto ReadBuffer = [this](
  1200. BitstreamCursor &SLocEntryCursor,
  1201. StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
  1202. RecordData Record;
  1203. StringRef Blob;
  1204. unsigned Code = SLocEntryCursor.ReadCode();
  1205. unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
  1206. if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
  1207. if (!llvm::zlib::isAvailable()) {
  1208. Error("zlib is not available");
  1209. return nullptr;
  1210. }
  1211. SmallString<0> Uncompressed;
  1212. if (llvm::Error E =
  1213. llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
  1214. Error("could not decompress embedded file contents: " +
  1215. llvm::toString(std::move(E)));
  1216. return nullptr;
  1217. }
  1218. return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
  1219. } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
  1220. return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
  1221. } else {
  1222. Error("AST record has invalid code");
  1223. return nullptr;
  1224. }
  1225. };
  1226. ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
  1227. F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
  1228. BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
  1229. unsigned BaseOffset = F->SLocEntryBaseOffset;
  1230. ++NumSLocEntriesRead;
  1231. llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
  1232. if (Entry.Kind != llvm::BitstreamEntry::Record) {
  1233. Error("incorrectly-formatted source location entry in AST file");
  1234. return true;
  1235. }
  1236. RecordData Record;
  1237. StringRef Blob;
  1238. switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
  1239. default:
  1240. Error("incorrectly-formatted source location entry in AST file");
  1241. return true;
  1242. case SM_SLOC_FILE_ENTRY: {
  1243. // We will detect whether a file changed and return 'Failure' for it, but
  1244. // we will also try to fail gracefully by setting up the SLocEntry.
  1245. unsigned InputID = Record[4];
  1246. InputFile IF = getInputFile(*F, InputID);
  1247. const FileEntry *File = IF.getFile();
  1248. bool OverriddenBuffer = IF.isOverridden();
  1249. // Note that we only check if a File was returned. If it was out-of-date
  1250. // we have complained but we will continue creating a FileID to recover
  1251. // gracefully.
  1252. if (!File)
  1253. return true;
  1254. SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
  1255. if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
  1256. // This is the module's main file.
  1257. IncludeLoc = getImportLocation(F);
  1258. }
  1259. SrcMgr::CharacteristicKind
  1260. FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
  1261. FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
  1262. ID, BaseOffset + Record[0]);
  1263. SrcMgr::FileInfo &FileInfo =
  1264. const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
  1265. FileInfo.NumCreatedFIDs = Record[5];
  1266. if (Record[3])
  1267. FileInfo.setHasLineDirectives();
  1268. const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
  1269. unsigned NumFileDecls = Record[7];
  1270. if (NumFileDecls && ContextObj) {
  1271. assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
  1272. FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
  1273. NumFileDecls));
  1274. }
  1275. const SrcMgr::ContentCache *ContentCache
  1276. = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
  1277. if (OverriddenBuffer && !ContentCache->BufferOverridden &&
  1278. ContentCache->ContentsEntry == ContentCache->OrigEntry &&
  1279. !ContentCache->getRawBuffer()) {
  1280. auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
  1281. if (!Buffer)
  1282. return true;
  1283. SourceMgr.overrideFileContents(File, std::move(Buffer));
  1284. }
  1285. break;
  1286. }
  1287. case SM_SLOC_BUFFER_ENTRY: {
  1288. const char *Name = Blob.data();
  1289. unsigned Offset = Record[0];
  1290. SrcMgr::CharacteristicKind
  1291. FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
  1292. SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
  1293. if (IncludeLoc.isInvalid() && F->isModule()) {
  1294. IncludeLoc = getImportLocation(F);
  1295. }
  1296. auto Buffer = ReadBuffer(SLocEntryCursor, Name);
  1297. if (!Buffer)
  1298. return true;
  1299. SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
  1300. BaseOffset + Offset, IncludeLoc);
  1301. break;
  1302. }
  1303. case SM_SLOC_EXPANSION_ENTRY: {
  1304. SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
  1305. SourceMgr.createExpansionLoc(SpellingLoc,
  1306. ReadSourceLocation(*F, Record[2]),
  1307. ReadSourceLocation(*F, Record[3]),
  1308. Record[5],
  1309. Record[4],
  1310. ID,
  1311. BaseOffset + Record[0]);
  1312. break;
  1313. }
  1314. }
  1315. return false;
  1316. }
  1317. std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
  1318. if (ID == 0)
  1319. return std::make_pair(SourceLocation(), "");
  1320. if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
  1321. Error("source location entry ID out-of-range for AST file");
  1322. return std::make_pair(SourceLocation(), "");
  1323. }
  1324. // Find which module file this entry lands in.
  1325. ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
  1326. if (!M->isModule())
  1327. return std::make_pair(SourceLocation(), "");
  1328. // FIXME: Can we map this down to a particular submodule? That would be
  1329. // ideal.
  1330. return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
  1331. }
  1332. /// Find the location where the module F is imported.
  1333. SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
  1334. if (F->ImportLoc.isValid())
  1335. return F->ImportLoc;
  1336. // Otherwise we have a PCH. It's considered to be "imported" at the first
  1337. // location of its includer.
  1338. if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
  1339. // Main file is the importer.
  1340. assert(SourceMgr.getMainFileID().isValid() && "missing main file");
  1341. return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
  1342. }
  1343. return F->ImportedBy[0]->FirstLoc;
  1344. }
  1345. /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
  1346. /// specified cursor. Read the abbreviations that are at the top of the block
  1347. /// and then leave the cursor pointing into the block.
  1348. bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
  1349. if (Cursor.EnterSubBlock(BlockID))
  1350. return true;
  1351. while (true) {
  1352. uint64_t Offset = Cursor.GetCurrentBitNo();
  1353. unsigned Code = Cursor.ReadCode();
  1354. // We expect all abbrevs to be at the start of the block.
  1355. if (Code != llvm::bitc::DEFINE_ABBREV) {
  1356. Cursor.JumpToBit(Offset);
  1357. return false;
  1358. }
  1359. Cursor.ReadAbbrevRecord();
  1360. }
  1361. }
  1362. Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
  1363. unsigned &Idx) {
  1364. Token Tok;
  1365. Tok.startToken();
  1366. Tok.setLocation(ReadSourceLocation(F, Record, Idx));
  1367. Tok.setLength(Record[Idx++]);
  1368. if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
  1369. Tok.setIdentifierInfo(II);
  1370. Tok.setKind((tok::TokenKind)Record[Idx++]);
  1371. Tok.setFlag((Token::TokenFlags)Record[Idx++]);
  1372. return Tok;
  1373. }
  1374. MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
  1375. BitstreamCursor &Stream = F.MacroCursor;
  1376. // Keep track of where we are in the stream, then jump back there
  1377. // after reading this macro.
  1378. SavedStreamPosition SavedPosition(Stream);
  1379. Stream.JumpToBit(Offset);
  1380. RecordData Record;
  1381. SmallVector<IdentifierInfo*, 16> MacroParams;
  1382. MacroInfo *Macro = nullptr;
  1383. while (true) {
  1384. // Advance to the next record, but if we get to the end of the block, don't
  1385. // pop it (removing all the abbreviations from the cursor) since we want to
  1386. // be able to reseek within the block and read entries.
  1387. unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
  1388. llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
  1389. switch (Entry.Kind) {
  1390. case llvm::BitstreamEntry::SubBlock: // Handled for us already.
  1391. case llvm::BitstreamEntry::Error:
  1392. Error("malformed block record in AST file");
  1393. return Macro;
  1394. case llvm::BitstreamEntry::EndBlock:
  1395. return Macro;
  1396. case llvm::BitstreamEntry::Record:
  1397. // The interesting case.
  1398. break;
  1399. }
  1400. // Read a record.
  1401. Record.clear();
  1402. PreprocessorRecordTypes RecType =
  1403. (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
  1404. switch (RecType) {
  1405. case PP_MODULE_MACRO:
  1406. case PP_MACRO_DIRECTIVE_HISTORY:
  1407. return Macro;
  1408. case PP_MACRO_OBJECT_LIKE:
  1409. case PP_MACRO_FUNCTION_LIKE: {
  1410. // If we already have a macro, that means that we've hit the end
  1411. // of the definition of the macro we were looking for. We're
  1412. // done.
  1413. if (Macro)
  1414. return Macro;
  1415. unsigned NextIndex = 1; // Skip identifier ID.
  1416. SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
  1417. MacroInfo *MI = PP.AllocateMacroInfo(Loc);
  1418. MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
  1419. MI->setIsUsed(Record[NextIndex++]);
  1420. MI->setUsedForHeaderGuard(Record[NextIndex++]);
  1421. if (RecType == PP_MACRO_FUNCTION_LIKE) {
  1422. // Decode function-like macro info.
  1423. bool isC99VarArgs = Record[NextIndex++];
  1424. bool isGNUVarArgs = Record[NextIndex++];
  1425. bool hasCommaPasting = Record[NextIndex++];
  1426. MacroParams.clear();
  1427. unsigned NumArgs = Record[NextIndex++];
  1428. for (unsigned i = 0; i != NumArgs; ++i)
  1429. MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
  1430. // Install function-like macro info.
  1431. MI->setIsFunctionLike();
  1432. if (isC99VarArgs) MI->setIsC99Varargs();
  1433. if (isGNUVarArgs) MI->setIsGNUVarargs();
  1434. if (hasCommaPasting) MI->setHasCommaPasting();
  1435. MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
  1436. }
  1437. // Remember that we saw this macro last so that we add the tokens that
  1438. // form its body to it.
  1439. Macro = MI;
  1440. if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
  1441. Record[NextIndex]) {
  1442. // We have a macro definition. Register the association
  1443. PreprocessedEntityID
  1444. GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
  1445. PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
  1446. PreprocessingRecord::PPEntityID PPID =
  1447. PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
  1448. MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
  1449. PPRec.getPreprocessedEntity(PPID));
  1450. if (PPDef)
  1451. PPRec.RegisterMacroDefinition(Macro, PPDef);
  1452. }
  1453. ++NumMacrosRead;
  1454. break;
  1455. }
  1456. case PP_TOKEN: {
  1457. // If we see a TOKEN before a PP_MACRO_*, then the file is
  1458. // erroneous, just pretend we didn't see this.
  1459. if (!Macro) break;
  1460. unsigned Idx = 0;
  1461. Token Tok = ReadToken(F, Record, Idx);
  1462. Macro->AddTokenToBody(Tok);
  1463. break;
  1464. }
  1465. }
  1466. }
  1467. }
  1468. PreprocessedEntityID
  1469. ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
  1470. unsigned LocalID) const {
  1471. if (!M.ModuleOffsetMap.empty())
  1472. ReadModuleOffsetMap(M);
  1473. ContinuousRangeMap<uint32_t, int, 2>::const_iterator
  1474. I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
  1475. assert(I != M.PreprocessedEntityRemap.end()
  1476. && "Invalid index into preprocessed entity index remap");
  1477. return LocalID + I->second;
  1478. }
  1479. unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
  1480. return llvm::hash_combine(ikey.Size, ikey.ModTime);
  1481. }
  1482. HeaderFileInfoTrait::internal_key_type
  1483. HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
  1484. internal_key_type ikey = {FE->getSize(),
  1485. M.HasTimestamps ? FE->getModificationTime() : 0,
  1486. FE->getName(), /*Imported*/ false};
  1487. return ikey;
  1488. }
  1489. bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
  1490. if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
  1491. return false;
  1492. if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
  1493. return true;
  1494. // Determine whether the actual files are equivalent.
  1495. FileManager &FileMgr = Reader.getFileManager();
  1496. auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
  1497. if (!Key.Imported)
  1498. return FileMgr.getFile(Key.Filename);
  1499. std::string Resolved = Key.Filename;
  1500. Reader.ResolveImportedPath(M, Resolved);
  1501. return FileMgr.getFile(Resolved);
  1502. };
  1503. const FileEntry *FEA = GetFile(a);
  1504. const FileEntry *FEB = GetFile(b);
  1505. return FEA && FEA == FEB;
  1506. }
  1507. std::pair<unsigned, unsigned>
  1508. HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
  1509. using namespace llvm::support;
  1510. unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
  1511. unsigned DataLen = (unsigned) *d++;
  1512. return std::make_pair(KeyLen, DataLen);
  1513. }
  1514. HeaderFileInfoTrait::internal_key_type
  1515. HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
  1516. using namespace llvm::support;
  1517. internal_key_type ikey;
  1518. ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
  1519. ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
  1520. ikey.Filename = (const char *)d;
  1521. ikey.Imported = true;
  1522. return ikey;
  1523. }
  1524. HeaderFileInfoTrait::data_type
  1525. HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
  1526. unsigned DataLen) {
  1527. using namespace llvm::support;
  1528. const unsigned char *End = d + DataLen;
  1529. HeaderFileInfo HFI;
  1530. unsigned Flags = *d++;
  1531. // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
  1532. HFI.isImport |= (Flags >> 5) & 0x01;
  1533. HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
  1534. HFI.DirInfo = (Flags >> 1) & 0x07;
  1535. HFI.IndexHeaderMapHeader = Flags & 0x01;
  1536. // FIXME: Find a better way to handle this. Maybe just store a
  1537. // "has been included" flag?
  1538. HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
  1539. HFI.NumIncludes);
  1540. HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
  1541. M, endian::readNext<uint32_t, little, unaligned>(d));
  1542. if (unsigned FrameworkOffset =
  1543. endian::readNext<uint32_t, little, unaligned>(d)) {
  1544. // The framework offset is 1 greater than the actual offset,
  1545. // since 0 is used as an indicator for "no framework name".
  1546. StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
  1547. HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
  1548. }
  1549. assert((End - d) % 4 == 0 &&
  1550. "Wrong data length in HeaderFileInfo deserialization");
  1551. while (d != End) {
  1552. uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
  1553. auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
  1554. LocalSMID >>= 2;
  1555. // This header is part of a module. Associate it with the module to enable
  1556. // implicit module import.
  1557. SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
  1558. Module *Mod = Reader.getSubmodule(GlobalSMID);
  1559. FileManager &FileMgr = Reader.getFileManager();
  1560. ModuleMap &ModMap =
  1561. Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
  1562. std::string Filename = key.Filename;
  1563. if (key.Imported)
  1564. Reader.ResolveImportedPath(M, Filename);
  1565. // FIXME: This is not always the right filename-as-written, but we're not
  1566. // going to use this information to rebuild the module, so it doesn't make
  1567. // a lot of difference.
  1568. Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
  1569. ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
  1570. HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
  1571. }
  1572. // This HeaderFileInfo was externally loaded.
  1573. HFI.External = true;
  1574. HFI.IsValid = true;
  1575. return HFI;
  1576. }
  1577. void ASTReader::addPendingMacro(IdentifierInfo *II,
  1578. ModuleFile *M,
  1579. uint64_t MacroDirectivesOffset) {
  1580. assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
  1581. PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
  1582. }
  1583. void ASTReader::ReadDefinedMacros() {
  1584. // Note that we are loading defined macros.
  1585. Deserializing Macros(this);
  1586. for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
  1587. BitstreamCursor &MacroCursor = I.MacroCursor;
  1588. // If there was no preprocessor block, skip this file.
  1589. if (MacroCursor.getBitcodeBytes().empty())
  1590. continue;
  1591. BitstreamCursor Cursor = MacroCursor;
  1592. Cursor.JumpToBit(I.MacroStartOffset);
  1593. RecordData Record;
  1594. while (true) {
  1595. llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
  1596. switch (E.Kind) {
  1597. case llvm::BitstreamEntry::SubBlock: // Handled for us already.
  1598. case llvm::BitstreamEntry::Error:
  1599. Error("malformed block record in AST file");
  1600. return;
  1601. case llvm::BitstreamEntry::EndBlock:
  1602. goto NextCursor;
  1603. case llvm::BitstreamEntry::Record:
  1604. Record.clear();
  1605. switch (Cursor.readRecord(E.ID, Record)) {
  1606. default: // Default behavior: ignore.
  1607. break;
  1608. case PP_MACRO_OBJECT_LIKE:
  1609. case PP_MACRO_FUNCTION_LIKE: {
  1610. IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
  1611. if (II->isOutOfDate())
  1612. updateOutOfDateIdentifier(*II);
  1613. break;
  1614. }
  1615. case PP_TOKEN:
  1616. // Ignore tokens.
  1617. break;
  1618. }
  1619. break;
  1620. }
  1621. }
  1622. NextCursor: ;
  1623. }
  1624. }
  1625. namespace {
  1626. /// Visitor class used to look up identifirs in an AST file.
  1627. class IdentifierLookupVisitor {
  1628. StringRef Name;
  1629. unsigned NameHash;
  1630. unsigned PriorGeneration;
  1631. unsigned &NumIdentifierLookups;
  1632. unsigned &NumIdentifierLookupHits;
  1633. IdentifierInfo *Found = nullptr;
  1634. public:
  1635. IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
  1636. unsigned &NumIdentifierLookups,
  1637. unsigned &NumIdentifierLookupHits)
  1638. : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
  1639. PriorGeneration(PriorGeneration),
  1640. NumIdentifierLookups(NumIdentifierLookups),
  1641. NumIdentifierLookupHits(NumIdentifierLookupHits) {}
  1642. bool operator()(ModuleFile &M) {
  1643. // If we've already searched this module file, skip it now.
  1644. if (M.Generation <= PriorGeneration)
  1645. return true;
  1646. ASTIdentifierLookupTable *IdTable
  1647. = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
  1648. if (!IdTable)
  1649. return false;
  1650. ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
  1651. Found);
  1652. ++NumIdentifierLookups;
  1653. ASTIdentifierLookupTable::iterator Pos =
  1654. IdTable->find_hashed(Name, NameHash, &Trait);
  1655. if (Pos == IdTable->end())
  1656. return false;
  1657. // Dereferencing the iterator has the effect of building the
  1658. // IdentifierInfo node and populating it with the various
  1659. // declarations it needs.
  1660. ++NumIdentifierLookupHits;
  1661. Found = *Pos;
  1662. return true;
  1663. }
  1664. // Retrieve the identifier info found within the module
  1665. // files.
  1666. IdentifierInfo *getIdentifierInfo() const { return Found; }
  1667. };
  1668. } // namespace
  1669. void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
  1670. // Note that we are loading an identifier.
  1671. Deserializing AnIdentifier(this);
  1672. unsigned PriorGeneration = 0;
  1673. if (getContext().getLangOpts().Modules)
  1674. PriorGeneration = IdentifierGeneration[&II];
  1675. // If there is a global index, look there first to determine which modules
  1676. // provably do not have any results for this identifier.
  1677. GlobalModuleIndex::HitSet Hits;
  1678. GlobalModuleIndex::HitSet *HitsPtr = nullptr;
  1679. if (!loadGlobalIndex()) {
  1680. if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
  1681. HitsPtr = &Hits;
  1682. }
  1683. }
  1684. IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
  1685. NumIdentifierLookups,
  1686. NumIdentifierLookupHits);
  1687. ModuleMgr.visit(Visitor, HitsPtr);
  1688. markIdentifierUpToDate(&II);
  1689. }
  1690. void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
  1691. if (!II)
  1692. return;
  1693. II->setOutOfDate(false);
  1694. // Update the generation for this identifier.
  1695. if (getContext().getLangOpts().Modules)
  1696. IdentifierGeneration[II] = getGeneration();
  1697. }
  1698. void ASTReader::resolvePendingMacro(IdentifierInfo *II,
  1699. const PendingMacroInfo &PMInfo) {
  1700. ModuleFile &M = *PMInfo.M;
  1701. BitstreamCursor &Cursor = M.MacroCursor;
  1702. SavedStreamPosition SavedPosition(Cursor);
  1703. Cursor.JumpToBit(PMInfo.MacroDirectivesOffset);
  1704. struct ModuleMacroRecord {
  1705. SubmoduleID SubModID;
  1706. MacroInfo *MI;
  1707. SmallVector<SubmoduleID, 8> Overrides;
  1708. };
  1709. llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
  1710. // We expect to see a sequence of PP_MODULE_MACRO records listing exported
  1711. // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
  1712. // macro histroy.
  1713. RecordData Record;
  1714. while (true) {
  1715. llvm::BitstreamEntry Entry =
  1716. Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
  1717. if (Entry.Kind != llvm::BitstreamEntry::Record) {
  1718. Error("malformed block record in AST file");
  1719. return;
  1720. }
  1721. Record.clear();
  1722. switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
  1723. case PP_MACRO_DIRECTIVE_HISTORY:
  1724. break;
  1725. case PP_MODULE_MACRO: {
  1726. ModuleMacros.push_back(ModuleMacroRecord());
  1727. auto &Info = ModuleMacros.back();
  1728. Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
  1729. Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
  1730. for (int I = 2, N = Record.size(); I != N; ++I)
  1731. Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
  1732. continue;
  1733. }
  1734. default:
  1735. Error("malformed block record in AST file");
  1736. return;
  1737. }
  1738. // We found the macro directive history; that's the last record
  1739. // for this macro.
  1740. break;
  1741. }
  1742. // Module macros are listed in reverse dependency order.
  1743. {
  1744. std::reverse(ModuleMacros.begin(), ModuleMacros.end());
  1745. llvm::SmallVector<ModuleMacro*, 8> Overrides;
  1746. for (auto &MMR : ModuleMacros) {
  1747. Overrides.clear();
  1748. for (unsigned ModID : MMR.Overrides) {
  1749. Module *Mod = getSubmodule(ModID);
  1750. auto *Macro = PP.getModuleMacro(Mod, II);
  1751. assert(Macro && "missing definition for overridden macro");
  1752. Overrides.push_back(Macro);
  1753. }
  1754. bool Inserted = false;
  1755. Module *Owner = getSubmodule(MMR.SubModID);
  1756. PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
  1757. }
  1758. }
  1759. // Don't read the directive history for a module; we don't have anywhere
  1760. // to put it.
  1761. if (M.isModule())
  1762. return;
  1763. // Deserialize the macro directives history in reverse source-order.
  1764. MacroDirective *Latest = nullptr, *Earliest = nullptr;
  1765. unsigned Idx = 0, N = Record.size();
  1766. while (Idx < N) {
  1767. MacroDirective *MD = nullptr;
  1768. SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
  1769. MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
  1770. switch (K) {
  1771. case MacroDirective::MD_Define: {
  1772. MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
  1773. MD = PP.AllocateDefMacroDirective(MI, Loc);
  1774. break;
  1775. }
  1776. case MacroDirective::MD_Undefine:
  1777. MD = PP.AllocateUndefMacroDirective(Loc);
  1778. break;
  1779. case MacroDirective::MD_Visibility:
  1780. bool isPublic = Record[Idx++];
  1781. MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
  1782. break;
  1783. }
  1784. if (!Latest)
  1785. Latest = MD;
  1786. if (Earliest)
  1787. Earliest->setPrevious(MD);
  1788. Earliest = MD;
  1789. }
  1790. if (Latest)
  1791. PP.setLoadedMacroDirective(II, Earliest, Latest);
  1792. }
  1793. ASTReader::InputFileInfo
  1794. ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
  1795. // Go find this input file.
  1796. BitstreamCursor &Cursor = F.InputFilesCursor;
  1797. SavedStreamPosition SavedPosition(Cursor);
  1798. Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
  1799. unsigned Code = Cursor.ReadCode();
  1800. RecordData Record;
  1801. StringRef Blob;
  1802. unsigned Result = Cursor.readRecord(Code, Record, &Blob);
  1803. assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
  1804. "invalid record type for input file");
  1805. (void)Result;
  1806. assert(Record[0] == ID && "Bogus stored ID or offset");
  1807. InputFileInfo R;
  1808. R.StoredSize = static_cast<off_t>(Record[1]);
  1809. R.StoredTime = static_cast<time_t>(Record[2]);
  1810. R.Overridden = static_cast<bool>(Record[3]);
  1811. R.Transient = static_cast<bool>(Record[4]);
  1812. R.TopLevelModuleMap = static_cast<bool>(Record[5]);
  1813. R.Filename = Blob;
  1814. ResolveImportedPath(F, R.Filename);
  1815. return R;
  1816. }
  1817. static unsigned moduleKindForDiagnostic(ModuleKind Kind);
  1818. InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
  1819. // If this ID is bogus, just return an empty input file.
  1820. if (ID == 0 || ID > F.InputFilesLoaded.size())
  1821. return InputFile();
  1822. // If we've already loaded this input file, return it.
  1823. if (F.InputFilesLoaded[ID-1].getFile())
  1824. return F.InputFilesLoaded[ID-1];
  1825. if (F.InputFilesLoaded[ID-1].isNotFound())
  1826. return InputFile();
  1827. // Go find this input file.
  1828. BitstreamCursor &Cursor = F.InputFilesCursor;
  1829. SavedStreamPosition SavedPosition(Cursor);
  1830. Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
  1831. InputFileInfo FI = readInputFileInfo(F, ID);
  1832. off_t StoredSize = FI.StoredSize;
  1833. time_t StoredTime = FI.StoredTime;
  1834. bool Overridden = FI.Overridden;
  1835. bool Transient = FI.Transient;
  1836. StringRef Filename = FI.Filename;
  1837. const FileEntry *File = FileMgr.getFile(Filename, /*OpenFile=*/false);
  1838. // If we didn't find the file, resolve it relative to the
  1839. // original directory from which this AST file was created.
  1840. if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
  1841. F.OriginalDir != F.BaseDirectory) {
  1842. std::string Resolved = resolveFileRelativeToOriginalDir(
  1843. Filename, F.OriginalDir, F.BaseDirectory);
  1844. if (!Resolved.empty())
  1845. File = FileMgr.getFile(Resolved);
  1846. }
  1847. // For an overridden file, create a virtual file with the stored
  1848. // size/timestamp.
  1849. if ((Overridden || Transient) && File == nullptr)
  1850. File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
  1851. if (File == nullptr) {
  1852. if (Complain) {
  1853. std::string ErrorStr = "could not find file '";
  1854. ErrorStr += Filename;
  1855. ErrorStr += "' referenced by AST file '";
  1856. ErrorStr += F.FileName;
  1857. ErrorStr += "'";
  1858. Error(ErrorStr);
  1859. }
  1860. // Record that we didn't find the file.
  1861. F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
  1862. return InputFile();
  1863. }
  1864. // Check if there was a request to override the contents of the file
  1865. // that was part of the precompiled header. Overriding such a file
  1866. // can lead to problems when lexing using the source locations from the
  1867. // PCH.
  1868. SourceManager &SM = getSourceManager();
  1869. // FIXME: Reject if the overrides are different.
  1870. if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
  1871. if (Complain)
  1872. Error(diag::err_fe_pch_file_overridden, Filename);
  1873. // After emitting the diagnostic, recover by disabling the override so
  1874. // that the original file will be used.
  1875. //
  1876. // FIXME: This recovery is just as broken as the original state; there may
  1877. // be another precompiled module that's using the overridden contents, or
  1878. // we might be half way through parsing it. Instead, we should treat the
  1879. // overridden contents as belonging to a separate FileEntry.
  1880. SM.disableFileContentsOverride(File);
  1881. // The FileEntry is a virtual file entry with the size of the contents
  1882. // that would override the original contents. Set it to the original's
  1883. // size/time.
  1884. FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
  1885. StoredSize, StoredTime);
  1886. }
  1887. bool IsOutOfDate = false;
  1888. // For an overridden file, there is nothing to validate.
  1889. if (!Overridden && //
  1890. (StoredSize != File->getSize() ||
  1891. (StoredTime && StoredTime != File->getModificationTime() &&
  1892. !DisableValidation)
  1893. )) {
  1894. if (Complain) {
  1895. // Build a list of the PCH imports that got us here (in reverse).
  1896. SmallVector<ModuleFile *, 4> ImportStack(1, &F);
  1897. while (!ImportStack.back()->ImportedBy.empty())
  1898. ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
  1899. // The top-level PCH is stale.
  1900. StringRef TopLevelPCHName(ImportStack.back()->FileName);
  1901. unsigned DiagnosticKind = moduleKindForDiagnostic(ImportStack.back()->Kind);
  1902. if (DiagnosticKind == 0)
  1903. Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
  1904. else if (DiagnosticKind == 1)
  1905. Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName);
  1906. else
  1907. Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName);
  1908. // Print the import stack.
  1909. if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
  1910. Diag(diag::note_pch_required_by)
  1911. << Filename << ImportStack[0]->FileName;
  1912. for (unsigned I = 1; I < ImportStack.size(); ++I)
  1913. Diag(diag::note_pch_required_by)
  1914. << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
  1915. }
  1916. if (!Diags.isDiagnosticInFlight())
  1917. Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
  1918. }
  1919. IsOutOfDate = true;
  1920. }
  1921. // FIXME: If the file is overridden and we've already opened it,
  1922. // issue an error (or split it into a separate FileEntry).
  1923. InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
  1924. // Note that we've loaded this input file.
  1925. F.InputFilesLoaded[ID-1] = IF;
  1926. return IF;
  1927. }
  1928. /// If we are loading a relocatable PCH or module file, and the filename
  1929. /// is not an absolute path, add the system or module root to the beginning of
  1930. /// the file name.
  1931. void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
  1932. // Resolve relative to the base directory, if we have one.
  1933. if (!M.BaseDirectory.empty())
  1934. return ResolveImportedPath(Filename, M.BaseDirectory);
  1935. }
  1936. void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
  1937. if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
  1938. return;
  1939. SmallString<128> Buffer;
  1940. llvm::sys::path::append(Buffer, Prefix, Filename);
  1941. Filename.assign(Buffer.begin(), Buffer.end());
  1942. }
  1943. static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
  1944. switch (ARR) {
  1945. case ASTReader::Failure: return true;
  1946. case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
  1947. case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
  1948. case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
  1949. case ASTReader::ConfigurationMismatch:
  1950. return !(Caps & ASTReader::ARR_ConfigurationMismatch);
  1951. case ASTReader::HadErrors: return true;
  1952. case ASTReader::Success: return false;
  1953. }
  1954. llvm_unreachable("unknown ASTReadResult");
  1955. }
  1956. ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
  1957. BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
  1958. bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
  1959. std::string &SuggestedPredefines) {
  1960. if (Stream.EnterSubBlock(OPTIONS_BLOCK_ID))
  1961. return Failure;
  1962. // Read all of the records in the options block.
  1963. RecordData Record;
  1964. ASTReadResult Result = Success;
  1965. while (true) {
  1966. llvm::BitstreamEntry Entry = Stream.advance();
  1967. switch (Entry.Kind) {
  1968. case llvm::BitstreamEntry::Error:
  1969. case llvm::BitstreamEntry::SubBlock:
  1970. return Failure;
  1971. case llvm::BitstreamEntry::EndBlock:
  1972. return Result;
  1973. case llvm::BitstreamEntry::Record:
  1974. // The interesting case.
  1975. break;
  1976. }
  1977. // Read and process a record.
  1978. Record.clear();
  1979. switch ((OptionsRecordTypes)Stream.readRecord(Entry.ID, Record)) {
  1980. case LANGUAGE_OPTIONS: {
  1981. bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
  1982. if (ParseLanguageOptions(Record, Complain, Listener,
  1983. AllowCompatibleConfigurationMismatch))
  1984. Result = ConfigurationMismatch;
  1985. break;
  1986. }
  1987. case TARGET_OPTIONS: {
  1988. bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
  1989. if (ParseTargetOptions(Record, Complain, Listener,
  1990. AllowCompatibleConfigurationMismatch))
  1991. Result = ConfigurationMismatch;
  1992. break;
  1993. }
  1994. case FILE_SYSTEM_OPTIONS: {
  1995. bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
  1996. if (!AllowCompatibleConfigurationMismatch &&
  1997. ParseFileSystemOptions(Record, Complain, Listener))
  1998. Result = ConfigurationMismatch;
  1999. break;
  2000. }
  2001. case HEADER_SEARCH_OPTIONS: {
  2002. bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
  2003. if (!AllowCompatibleConfigurationMismatch &&
  2004. ParseHeaderSearchOptions(Record, Complain, Listener))
  2005. Result = ConfigurationMismatch;
  2006. break;
  2007. }
  2008. case PREPROCESSOR_OPTIONS:
  2009. bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
  2010. if (!AllowCompatibleConfigurationMismatch &&
  2011. ParsePreprocessorOptions(Record, Complain, Listener,
  2012. SuggestedPredefines))
  2013. Result = ConfigurationMismatch;
  2014. break;
  2015. }
  2016. }
  2017. }
  2018. ASTReader::ASTReadResult
  2019. ASTReader::ReadControlBlock(ModuleFile &F,
  2020. SmallVectorImpl<ImportedModule> &Loaded,
  2021. const ModuleFile *ImportedBy,
  2022. unsigned ClientLoadCapabilities) {
  2023. BitstreamCursor &Stream = F.Stream;
  2024. ASTReadResult Result = Success;
  2025. if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
  2026. Error("malformed block record in AST file");
  2027. return Failure;
  2028. }
  2029. // Lambda to read the unhashed control block the first time it's called.
  2030. //
  2031. // For PCM files, the unhashed control block cannot be read until after the
  2032. // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
  2033. // need to look ahead before reading the IMPORTS record. For consistency,
  2034. // this block is always read somehow (see BitstreamEntry::EndBlock).
  2035. bool HasReadUnhashedControlBlock = false;
  2036. auto readUnhashedControlBlockOnce = [&]() {
  2037. if (!HasReadUnhashedControlBlock) {
  2038. HasReadUnhashedControlBlock = true;
  2039. if (ASTReadResult Result =
  2040. readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
  2041. return Result;
  2042. }
  2043. return Success;
  2044. };
  2045. // Read all of the records and blocks in the control block.
  2046. RecordData Record;
  2047. unsigned NumInputs = 0;
  2048. unsigned NumUserInputs = 0;
  2049. while (true) {
  2050. llvm::BitstreamEntry Entry = Stream.advance();
  2051. switch (Entry.Kind) {
  2052. case llvm::BitstreamEntry::Error:
  2053. Error("malformed block record in AST file");
  2054. return Failure;
  2055. case llvm::BitstreamEntry::EndBlock: {
  2056. // Validate the module before returning. This call catches an AST with
  2057. // no module name and no imports.
  2058. if (ASTReadResult Result = readUnhashedControlBlockOnce())
  2059. return Result;
  2060. // Validate input files.
  2061. const HeaderSearchOptions &HSOpts =
  2062. PP.getHeaderSearchInfo().getHeaderSearchOpts();
  2063. // All user input files reside at the index range [0, NumUserInputs), and
  2064. // system input files reside at [NumUserInputs, NumInputs). For explicitly
  2065. // loaded module files, ignore missing inputs.
  2066. if (!DisableValidation && F.Kind != MK_ExplicitModule &&
  2067. F.Kind != MK_PrebuiltModule) {
  2068. bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
  2069. // If we are reading a module, we will create a verification timestamp,
  2070. // so we verify all input files. Otherwise, verify only user input
  2071. // files.
  2072. unsigned N = NumUserInputs;
  2073. if (ValidateSystemInputs ||
  2074. (HSOpts.ModulesValidateOncePerBuildSession &&
  2075. F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
  2076. F.Kind == MK_ImplicitModule))
  2077. N = NumInputs;
  2078. for (unsigned I = 0; I < N; ++I) {
  2079. InputFile IF = getInputFile(F, I+1, Complain);
  2080. if (!IF.getFile() || IF.isOutOfDate())
  2081. return OutOfDate;
  2082. }
  2083. }
  2084. if (Listener)
  2085. Listener->visitModuleFile(F.FileName, F.Kind);
  2086. if (Listener && Listener->needsInputFileVisitation()) {
  2087. unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
  2088. : NumUserInputs;
  2089. for (unsigned I = 0; I < N; ++I) {
  2090. bool IsSystem = I >= NumUserInputs;
  2091. InputFileInfo FI = readInputFileInfo(F, I+1);
  2092. Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
  2093. F.Kind == MK_ExplicitModule ||
  2094. F.Kind == MK_PrebuiltModule);
  2095. }
  2096. }
  2097. return Result;
  2098. }
  2099. case llvm::BitstreamEntry::SubBlock:
  2100. switch (Entry.ID) {
  2101. case INPUT_FILES_BLOCK_ID:
  2102. F.InputFilesCursor = Stream;
  2103. if (Stream.SkipBlock() || // Skip with the main cursor
  2104. // Read the abbreviations
  2105. ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
  2106. Error("malformed block record in AST file");
  2107. return Failure;
  2108. }
  2109. continue;
  2110. case OPTIONS_BLOCK_ID:
  2111. // If we're reading the first module for this group, check its options
  2112. // are compatible with ours. For modules it imports, no further checking
  2113. // is required, because we checked them when we built it.
  2114. if (Listener && !ImportedBy) {
  2115. // Should we allow the configuration of the module file to differ from
  2116. // the configuration of the current translation unit in a compatible
  2117. // way?
  2118. //
  2119. // FIXME: Allow this for files explicitly specified with -include-pch.
  2120. bool AllowCompatibleConfigurationMismatch =
  2121. F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
  2122. Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
  2123. AllowCompatibleConfigurationMismatch,
  2124. *Listener, SuggestedPredefines);
  2125. if (Result == Failure) {
  2126. Error("malformed block record in AST file");
  2127. return Result;
  2128. }
  2129. if (DisableValidation ||
  2130. (AllowConfigurationMismatch && Result == ConfigurationMismatch))
  2131. Result = Success;
  2132. // If we can't load the module, exit early since we likely
  2133. // will rebuild the module anyway. The stream may be in the
  2134. // middle of a block.
  2135. if (Result != Success)
  2136. return Result;
  2137. } else if (Stream.SkipBlock()) {
  2138. Error("malformed block record in AST file");
  2139. return Failure;
  2140. }
  2141. continue;
  2142. default:
  2143. if (Stream.SkipBlock()) {
  2144. Error("malformed block record in AST file");
  2145. return Failure;
  2146. }
  2147. continue;
  2148. }
  2149. case llvm::BitstreamEntry::Record:
  2150. // The interesting case.
  2151. break;
  2152. }
  2153. // Read and process a record.
  2154. Record.clear();
  2155. StringRef Blob;
  2156. switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
  2157. case METADATA: {
  2158. if (Record[0] != VERSION_MAJOR && !DisableValidation) {
  2159. if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
  2160. Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
  2161. : diag::err_pch_version_too_new);
  2162. return VersionMismatch;
  2163. }
  2164. bool hasErrors = Record[7];
  2165. if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
  2166. Diag(diag::err_pch_with_compiler_errors);
  2167. return HadErrors;
  2168. }
  2169. if (hasErrors) {
  2170. Diags.ErrorOccurred = true;
  2171. Diags.UncompilableErrorOccurred = true;
  2172. Diags.UnrecoverableErrorOccurred = true;
  2173. }
  2174. F.RelocatablePCH = Record[4];
  2175. // Relative paths in a relocatable PCH are relative to our sysroot.
  2176. if (F.RelocatablePCH)
  2177. F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
  2178. F.HasTimestamps = Record[5];
  2179. F.PCHHasObjectFile = Record[6];
  2180. const std::string &CurBranch = getClangFullRepositoryVersion();
  2181. StringRef ASTBranch = Blob;
  2182. if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
  2183. if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
  2184. Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
  2185. return VersionMismatch;
  2186. }
  2187. break;
  2188. }
  2189. case IMPORTS: {
  2190. // Validate the AST before processing any imports (otherwise, untangling
  2191. // them can be error-prone and expensive). A module will have a name and
  2192. // will already have been validated, but this catches the PCH case.
  2193. if (ASTReadResult Result = readUnhashedControlBlockOnce())
  2194. return Result;
  2195. // Load each of the imported PCH files.
  2196. unsigned Idx = 0, N = Record.size();
  2197. while (Idx < N) {
  2198. // Read information about the AST file.
  2199. ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
  2200. // The import location will be the local one for now; we will adjust
  2201. // all import locations of module imports after the global source
  2202. // location info are setup, in ReadAST.
  2203. SourceLocation ImportLoc =
  2204. ReadUntranslatedSourceLocation(Record[Idx++]);
  2205. off_t StoredSize = (off_t)Record[Idx++];
  2206. time_t StoredModTime = (time_t)Record[Idx++];
  2207. ASTFileSignature StoredSignature = {
  2208. {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
  2209. (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
  2210. (uint32_t)Record[Idx++]}}};
  2211. std::string ImportedName = ReadString(Record, Idx);
  2212. std::string ImportedFile;
  2213. // For prebuilt and explicit modules first consult the file map for
  2214. // an override. Note that here we don't search prebuilt module
  2215. // directories, only the explicit name to file mappings. Also, we will
  2216. // still verify the size/signature making sure it is essentially the
  2217. // same file but perhaps in a different location.
  2218. if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
  2219. ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
  2220. ImportedName, /*FileMapOnly*/ true);
  2221. if (ImportedFile.empty())
  2222. ImportedFile = ReadPath(F, Record, Idx);
  2223. else
  2224. SkipPath(Record, Idx);
  2225. // If our client can't cope with us being out of date, we can't cope with
  2226. // our dependency being missing.
  2227. unsigned Capabilities = ClientLoadCapabilities;
  2228. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  2229. Capabilities &= ~ARR_Missing;
  2230. // Load the AST file.
  2231. auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
  2232. Loaded, StoredSize, StoredModTime,
  2233. StoredSignature, Capabilities);
  2234. // If we diagnosed a problem, produce a backtrace.
  2235. if (isDiagnosedResult(Result, Capabilities))
  2236. Diag(diag::note_module_file_imported_by)
  2237. << F.FileName << !F.ModuleName.empty() << F.ModuleName;
  2238. switch (Result) {
  2239. case Failure: return Failure;
  2240. // If we have to ignore the dependency, we'll have to ignore this too.
  2241. case Missing:
  2242. case OutOfDate: return OutOfDate;
  2243. case VersionMismatch: return VersionMismatch;
  2244. case ConfigurationMismatch: return ConfigurationMismatch;
  2245. case HadErrors: return HadErrors;
  2246. case Success: break;
  2247. }
  2248. }
  2249. break;
  2250. }
  2251. case ORIGINAL_FILE:
  2252. F.OriginalSourceFileID = FileID::get(Record[0]);
  2253. F.ActualOriginalSourceFileName = Blob;
  2254. F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
  2255. ResolveImportedPath(F, F.OriginalSourceFileName);
  2256. break;
  2257. case ORIGINAL_FILE_ID:
  2258. F.OriginalSourceFileID = FileID::get(Record[0]);
  2259. break;
  2260. case ORIGINAL_PCH_DIR:
  2261. F.OriginalDir = Blob;
  2262. break;
  2263. case MODULE_NAME:
  2264. F.ModuleName = Blob;
  2265. if (Listener)
  2266. Listener->ReadModuleName(F.ModuleName);
  2267. // Validate the AST as soon as we have a name so we can exit early on
  2268. // failure.
  2269. if (ASTReadResult Result = readUnhashedControlBlockOnce())
  2270. return Result;
  2271. break;
  2272. case MODULE_DIRECTORY: {
  2273. assert(!F.ModuleName.empty() &&
  2274. "MODULE_DIRECTORY found before MODULE_NAME");
  2275. // If we've already loaded a module map file covering this module, we may
  2276. // have a better path for it (relative to the current build).
  2277. Module *M = PP.getHeaderSearchInfo().lookupModule(
  2278. F.ModuleName, /*AllowSearch*/ true,
  2279. /*AllowExtraModuleMapSearch*/ true);
  2280. if (M && M->Directory) {
  2281. // If we're implicitly loading a module, the base directory can't
  2282. // change between the build and use.
  2283. // Don't emit module relocation error if we have -fno-validate-pch
  2284. if (!PP.getPreprocessorOpts().DisablePCHValidation &&
  2285. F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
  2286. const DirectoryEntry *BuildDir =
  2287. PP.getFileManager().getDirectory(Blob);
  2288. if (!BuildDir || BuildDir != M->Directory) {
  2289. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  2290. Diag(diag::err_imported_module_relocated)
  2291. << F.ModuleName << Blob << M->Directory->getName();
  2292. return OutOfDate;
  2293. }
  2294. }
  2295. F.BaseDirectory = M->Directory->getName();
  2296. } else {
  2297. F.BaseDirectory = Blob;
  2298. }
  2299. break;
  2300. }
  2301. case MODULE_MAP_FILE:
  2302. if (ASTReadResult Result =
  2303. ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
  2304. return Result;
  2305. break;
  2306. case INPUT_FILE_OFFSETS:
  2307. NumInputs = Record[0];
  2308. NumUserInputs = Record[1];
  2309. F.InputFileOffsets =
  2310. (const llvm::support::unaligned_uint64_t *)Blob.data();
  2311. F.InputFilesLoaded.resize(NumInputs);
  2312. F.NumUserInputFiles = NumUserInputs;
  2313. break;
  2314. }
  2315. }
  2316. }
  2317. ASTReader::ASTReadResult
  2318. ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
  2319. BitstreamCursor &Stream = F.Stream;
  2320. if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
  2321. Error("malformed block record in AST file");
  2322. return Failure;
  2323. }
  2324. // Read all of the records and blocks for the AST file.
  2325. RecordData Record;
  2326. while (true) {
  2327. llvm::BitstreamEntry Entry = Stream.advance();
  2328. switch (Entry.Kind) {
  2329. case llvm::BitstreamEntry::Error:
  2330. Error("error at end of module block in AST file");
  2331. return Failure;
  2332. case llvm::BitstreamEntry::EndBlock:
  2333. // Outside of C++, we do not store a lookup map for the translation unit.
  2334. // Instead, mark it as needing a lookup map to be built if this module
  2335. // contains any declarations lexically within it (which it always does!).
  2336. // This usually has no cost, since we very rarely need the lookup map for
  2337. // the translation unit outside C++.
  2338. if (ASTContext *Ctx = ContextObj) {
  2339. DeclContext *DC = Ctx->getTranslationUnitDecl();
  2340. if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
  2341. DC->setMustBuildLookupTable();
  2342. }
  2343. return Success;
  2344. case llvm::BitstreamEntry::SubBlock:
  2345. switch (Entry.ID) {
  2346. case DECLTYPES_BLOCK_ID:
  2347. // We lazily load the decls block, but we want to set up the
  2348. // DeclsCursor cursor to point into it. Clone our current bitcode
  2349. // cursor to it, enter the block and read the abbrevs in that block.
  2350. // With the main cursor, we just skip over it.
  2351. F.DeclsCursor = Stream;
  2352. if (Stream.SkipBlock() || // Skip with the main cursor.
  2353. // Read the abbrevs.
  2354. ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
  2355. Error("malformed block record in AST file");
  2356. return Failure;
  2357. }
  2358. break;
  2359. case PREPROCESSOR_BLOCK_ID:
  2360. F.MacroCursor = Stream;
  2361. if (!PP.getExternalSource())
  2362. PP.setExternalSource(this);
  2363. if (Stream.SkipBlock() ||
  2364. ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
  2365. Error("malformed block record in AST file");
  2366. return Failure;
  2367. }
  2368. F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
  2369. break;
  2370. case PREPROCESSOR_DETAIL_BLOCK_ID:
  2371. F.PreprocessorDetailCursor = Stream;
  2372. if (Stream.SkipBlock() ||
  2373. ReadBlockAbbrevs(F.PreprocessorDetailCursor,
  2374. PREPROCESSOR_DETAIL_BLOCK_ID)) {
  2375. Error("malformed preprocessor detail record in AST file");
  2376. return Failure;
  2377. }
  2378. F.PreprocessorDetailStartOffset
  2379. = F.PreprocessorDetailCursor.GetCurrentBitNo();
  2380. if (!PP.getPreprocessingRecord())
  2381. PP.createPreprocessingRecord();
  2382. if (!PP.getPreprocessingRecord()->getExternalSource())
  2383. PP.getPreprocessingRecord()->SetExternalSource(*this);
  2384. break;
  2385. case SOURCE_MANAGER_BLOCK_ID:
  2386. if (ReadSourceManagerBlock(F))
  2387. return Failure;
  2388. break;
  2389. case SUBMODULE_BLOCK_ID:
  2390. if (ASTReadResult Result =
  2391. ReadSubmoduleBlock(F, ClientLoadCapabilities))
  2392. return Result;
  2393. break;
  2394. case COMMENTS_BLOCK_ID: {
  2395. BitstreamCursor C = Stream;
  2396. if (Stream.SkipBlock() ||
  2397. ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
  2398. Error("malformed comments block in AST file");
  2399. return Failure;
  2400. }
  2401. CommentsCursors.push_back(std::make_pair(C, &F));
  2402. break;
  2403. }
  2404. default:
  2405. if (Stream.SkipBlock()) {
  2406. Error("malformed block record in AST file");
  2407. return Failure;
  2408. }
  2409. break;
  2410. }
  2411. continue;
  2412. case llvm::BitstreamEntry::Record:
  2413. // The interesting case.
  2414. break;
  2415. }
  2416. // Read and process a record.
  2417. Record.clear();
  2418. StringRef Blob;
  2419. auto RecordType =
  2420. (ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob);
  2421. // If we're not loading an AST context, we don't care about most records.
  2422. if (!ContextObj) {
  2423. switch (RecordType) {
  2424. case IDENTIFIER_TABLE:
  2425. case IDENTIFIER_OFFSET:
  2426. case INTERESTING_IDENTIFIERS:
  2427. case STATISTICS:
  2428. case PP_CONDITIONAL_STACK:
  2429. case PP_COUNTER_VALUE:
  2430. case SOURCE_LOCATION_OFFSETS:
  2431. case MODULE_OFFSET_MAP:
  2432. case SOURCE_MANAGER_LINE_TABLE:
  2433. case SOURCE_LOCATION_PRELOADS:
  2434. case PPD_ENTITIES_OFFSETS:
  2435. case HEADER_SEARCH_TABLE:
  2436. case IMPORTED_MODULES:
  2437. case MACRO_OFFSET:
  2438. break;
  2439. default:
  2440. continue;
  2441. }
  2442. }
  2443. switch (RecordType) {
  2444. default: // Default behavior: ignore.
  2445. break;
  2446. case TYPE_OFFSET: {
  2447. if (F.LocalNumTypes != 0) {
  2448. Error("duplicate TYPE_OFFSET record in AST file");
  2449. return Failure;
  2450. }
  2451. F.TypeOffsets = (const uint32_t *)Blob.data();
  2452. F.LocalNumTypes = Record[0];
  2453. unsigned LocalBaseTypeIndex = Record[1];
  2454. F.BaseTypeIndex = getTotalNumTypes();
  2455. if (F.LocalNumTypes > 0) {
  2456. // Introduce the global -> local mapping for types within this module.
  2457. GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
  2458. // Introduce the local -> global mapping for types within this module.
  2459. F.TypeRemap.insertOrReplace(
  2460. std::make_pair(LocalBaseTypeIndex,
  2461. F.BaseTypeIndex - LocalBaseTypeIndex));
  2462. TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
  2463. }
  2464. break;
  2465. }
  2466. case DECL_OFFSET: {
  2467. if (F.LocalNumDecls != 0) {
  2468. Error("duplicate DECL_OFFSET record in AST file");
  2469. return Failure;
  2470. }
  2471. F.DeclOffsets = (const DeclOffset *)Blob.data();
  2472. F.LocalNumDecls = Record[0];
  2473. unsigned LocalBaseDeclID = Record[1];
  2474. F.BaseDeclID = getTotalNumDecls();
  2475. if (F.LocalNumDecls > 0) {
  2476. // Introduce the global -> local mapping for declarations within this
  2477. // module.
  2478. GlobalDeclMap.insert(
  2479. std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
  2480. // Introduce the local -> global mapping for declarations within this
  2481. // module.
  2482. F.DeclRemap.insertOrReplace(
  2483. std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
  2484. // Introduce the global -> local mapping for declarations within this
  2485. // module.
  2486. F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
  2487. DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
  2488. }
  2489. break;
  2490. }
  2491. case TU_UPDATE_LEXICAL: {
  2492. DeclContext *TU = ContextObj->getTranslationUnitDecl();
  2493. LexicalContents Contents(
  2494. reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
  2495. Blob.data()),
  2496. static_cast<unsigned int>(Blob.size() / 4));
  2497. TULexicalDecls.push_back(std::make_pair(&F, Contents));
  2498. TU->setHasExternalLexicalStorage(true);
  2499. break;
  2500. }
  2501. case UPDATE_VISIBLE: {
  2502. unsigned Idx = 0;
  2503. serialization::DeclID ID = ReadDeclID(F, Record, Idx);
  2504. auto *Data = (const unsigned char*)Blob.data();
  2505. PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
  2506. // If we've already loaded the decl, perform the updates when we finish
  2507. // loading this block.
  2508. if (Decl *D = GetExistingDecl(ID))
  2509. PendingUpdateRecords.push_back(
  2510. PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
  2511. break;
  2512. }
  2513. case IDENTIFIER_TABLE:
  2514. F.IdentifierTableData = Blob.data();
  2515. if (Record[0]) {
  2516. F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
  2517. (const unsigned char *)F.IdentifierTableData + Record[0],
  2518. (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
  2519. (const unsigned char *)F.IdentifierTableData,
  2520. ASTIdentifierLookupTrait(*this, F));
  2521. PP.getIdentifierTable().setExternalIdentifierLookup(this);
  2522. }
  2523. break;
  2524. case IDENTIFIER_OFFSET: {
  2525. if (F.LocalNumIdentifiers != 0) {
  2526. Error("duplicate IDENTIFIER_OFFSET record in AST file");
  2527. return Failure;
  2528. }
  2529. F.IdentifierOffsets = (const uint32_t *)Blob.data();
  2530. F.LocalNumIdentifiers = Record[0];
  2531. unsigned LocalBaseIdentifierID = Record[1];
  2532. F.BaseIdentifierID = getTotalNumIdentifiers();
  2533. if (F.LocalNumIdentifiers > 0) {
  2534. // Introduce the global -> local mapping for identifiers within this
  2535. // module.
  2536. GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
  2537. &F));
  2538. // Introduce the local -> global mapping for identifiers within this
  2539. // module.
  2540. F.IdentifierRemap.insertOrReplace(
  2541. std::make_pair(LocalBaseIdentifierID,
  2542. F.BaseIdentifierID - LocalBaseIdentifierID));
  2543. IdentifiersLoaded.resize(IdentifiersLoaded.size()
  2544. + F.LocalNumIdentifiers);
  2545. }
  2546. break;
  2547. }
  2548. case INTERESTING_IDENTIFIERS:
  2549. F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
  2550. break;
  2551. case EAGERLY_DESERIALIZED_DECLS:
  2552. // FIXME: Skip reading this record if our ASTConsumer doesn't care
  2553. // about "interesting" decls (for instance, if we're building a module).
  2554. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2555. EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
  2556. break;
  2557. case MODULAR_CODEGEN_DECLS:
  2558. // FIXME: Skip reading this record if our ASTConsumer doesn't care about
  2559. // them (ie: if we're not codegenerating this module).
  2560. if (F.Kind == MK_MainFile)
  2561. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2562. EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
  2563. break;
  2564. case SPECIAL_TYPES:
  2565. if (SpecialTypes.empty()) {
  2566. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2567. SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
  2568. break;
  2569. }
  2570. if (SpecialTypes.size() != Record.size()) {
  2571. Error("invalid special-types record");
  2572. return Failure;
  2573. }
  2574. for (unsigned I = 0, N = Record.size(); I != N; ++I) {
  2575. serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
  2576. if (!SpecialTypes[I])
  2577. SpecialTypes[I] = ID;
  2578. // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
  2579. // merge step?
  2580. }
  2581. break;
  2582. case STATISTICS:
  2583. TotalNumStatements += Record[0];
  2584. TotalNumMacros += Record[1];
  2585. TotalLexicalDeclContexts += Record[2];
  2586. TotalVisibleDeclContexts += Record[3];
  2587. break;
  2588. case UNUSED_FILESCOPED_DECLS:
  2589. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2590. UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
  2591. break;
  2592. case DELEGATING_CTORS:
  2593. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2594. DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
  2595. break;
  2596. case WEAK_UNDECLARED_IDENTIFIERS:
  2597. if (Record.size() % 4 != 0) {
  2598. Error("invalid weak identifiers record");
  2599. return Failure;
  2600. }
  2601. // FIXME: Ignore weak undeclared identifiers from non-original PCH
  2602. // files. This isn't the way to do it :)
  2603. WeakUndeclaredIdentifiers.clear();
  2604. // Translate the weak, undeclared identifiers into global IDs.
  2605. for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
  2606. WeakUndeclaredIdentifiers.push_back(
  2607. getGlobalIdentifierID(F, Record[I++]));
  2608. WeakUndeclaredIdentifiers.push_back(
  2609. getGlobalIdentifierID(F, Record[I++]));
  2610. WeakUndeclaredIdentifiers.push_back(
  2611. ReadSourceLocation(F, Record, I).getRawEncoding());
  2612. WeakUndeclaredIdentifiers.push_back(Record[I++]);
  2613. }
  2614. break;
  2615. case SELECTOR_OFFSETS: {
  2616. F.SelectorOffsets = (const uint32_t *)Blob.data();
  2617. F.LocalNumSelectors = Record[0];
  2618. unsigned LocalBaseSelectorID = Record[1];
  2619. F.BaseSelectorID = getTotalNumSelectors();
  2620. if (F.LocalNumSelectors > 0) {
  2621. // Introduce the global -> local mapping for selectors within this
  2622. // module.
  2623. GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
  2624. // Introduce the local -> global mapping for selectors within this
  2625. // module.
  2626. F.SelectorRemap.insertOrReplace(
  2627. std::make_pair(LocalBaseSelectorID,
  2628. F.BaseSelectorID - LocalBaseSelectorID));
  2629. SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
  2630. }
  2631. break;
  2632. }
  2633. case METHOD_POOL:
  2634. F.SelectorLookupTableData = (const unsigned char *)Blob.data();
  2635. if (Record[0])
  2636. F.SelectorLookupTable
  2637. = ASTSelectorLookupTable::Create(
  2638. F.SelectorLookupTableData + Record[0],
  2639. F.SelectorLookupTableData,
  2640. ASTSelectorLookupTrait(*this, F));
  2641. TotalNumMethodPoolEntries += Record[1];
  2642. break;
  2643. case REFERENCED_SELECTOR_POOL:
  2644. if (!Record.empty()) {
  2645. for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
  2646. ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
  2647. Record[Idx++]));
  2648. ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
  2649. getRawEncoding());
  2650. }
  2651. }
  2652. break;
  2653. case PP_CONDITIONAL_STACK:
  2654. if (!Record.empty()) {
  2655. unsigned Idx = 0, End = Record.size() - 1;
  2656. bool ReachedEOFWhileSkipping = Record[Idx++];
  2657. llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
  2658. if (ReachedEOFWhileSkipping) {
  2659. SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
  2660. SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
  2661. bool FoundNonSkipPortion = Record[Idx++];
  2662. bool FoundElse = Record[Idx++];
  2663. SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
  2664. SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
  2665. FoundElse, ElseLoc);
  2666. }
  2667. SmallVector<PPConditionalInfo, 4> ConditionalStack;
  2668. while (Idx < End) {
  2669. auto Loc = ReadSourceLocation(F, Record, Idx);
  2670. bool WasSkipping = Record[Idx++];
  2671. bool FoundNonSkip = Record[Idx++];
  2672. bool FoundElse = Record[Idx++];
  2673. ConditionalStack.push_back(
  2674. {Loc, WasSkipping, FoundNonSkip, FoundElse});
  2675. }
  2676. PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
  2677. }
  2678. break;
  2679. case PP_COUNTER_VALUE:
  2680. if (!Record.empty() && Listener)
  2681. Listener->ReadCounter(F, Record[0]);
  2682. break;
  2683. case FILE_SORTED_DECLS:
  2684. F.FileSortedDecls = (const DeclID *)Blob.data();
  2685. F.NumFileSortedDecls = Record[0];
  2686. break;
  2687. case SOURCE_LOCATION_OFFSETS: {
  2688. F.SLocEntryOffsets = (const uint32_t *)Blob.data();
  2689. F.LocalNumSLocEntries = Record[0];
  2690. unsigned SLocSpaceSize = Record[1];
  2691. std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
  2692. SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
  2693. SLocSpaceSize);
  2694. if (!F.SLocEntryBaseID) {
  2695. Error("ran out of source locations");
  2696. break;
  2697. }
  2698. // Make our entry in the range map. BaseID is negative and growing, so
  2699. // we invert it. Because we invert it, though, we need the other end of
  2700. // the range.
  2701. unsigned RangeStart =
  2702. unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
  2703. GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
  2704. F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
  2705. // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
  2706. assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
  2707. GlobalSLocOffsetMap.insert(
  2708. std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
  2709. - SLocSpaceSize,&F));
  2710. // Initialize the remapping table.
  2711. // Invalid stays invalid.
  2712. F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
  2713. // This module. Base was 2 when being compiled.
  2714. F.SLocRemap.insertOrReplace(std::make_pair(2U,
  2715. static_cast<int>(F.SLocEntryBaseOffset - 2)));
  2716. TotalNumSLocEntries += F.LocalNumSLocEntries;
  2717. break;
  2718. }
  2719. case MODULE_OFFSET_MAP:
  2720. F.ModuleOffsetMap = Blob;
  2721. break;
  2722. case SOURCE_MANAGER_LINE_TABLE:
  2723. if (ParseLineTable(F, Record))
  2724. return Failure;
  2725. break;
  2726. case SOURCE_LOCATION_PRELOADS: {
  2727. // Need to transform from the local view (1-based IDs) to the global view,
  2728. // which is based off F.SLocEntryBaseID.
  2729. if (!F.PreloadSLocEntries.empty()) {
  2730. Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
  2731. return Failure;
  2732. }
  2733. F.PreloadSLocEntries.swap(Record);
  2734. break;
  2735. }
  2736. case EXT_VECTOR_DECLS:
  2737. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2738. ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
  2739. break;
  2740. case VTABLE_USES:
  2741. if (Record.size() % 3 != 0) {
  2742. Error("Invalid VTABLE_USES record");
  2743. return Failure;
  2744. }
  2745. // Later tables overwrite earlier ones.
  2746. // FIXME: Modules will have some trouble with this. This is clearly not
  2747. // the right way to do this.
  2748. VTableUses.clear();
  2749. for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
  2750. VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
  2751. VTableUses.push_back(
  2752. ReadSourceLocation(F, Record, Idx).getRawEncoding());
  2753. VTableUses.push_back(Record[Idx++]);
  2754. }
  2755. break;
  2756. case PENDING_IMPLICIT_INSTANTIATIONS:
  2757. if (PendingInstantiations.size() % 2 != 0) {
  2758. Error("Invalid existing PendingInstantiations");
  2759. return Failure;
  2760. }
  2761. if (Record.size() % 2 != 0) {
  2762. Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
  2763. return Failure;
  2764. }
  2765. for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
  2766. PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
  2767. PendingInstantiations.push_back(
  2768. ReadSourceLocation(F, Record, I).getRawEncoding());
  2769. }
  2770. break;
  2771. case SEMA_DECL_REFS:
  2772. if (Record.size() != 3) {
  2773. Error("Invalid SEMA_DECL_REFS block");
  2774. return Failure;
  2775. }
  2776. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2777. SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
  2778. break;
  2779. case PPD_ENTITIES_OFFSETS: {
  2780. F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
  2781. assert(Blob.size() % sizeof(PPEntityOffset) == 0);
  2782. F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
  2783. unsigned LocalBasePreprocessedEntityID = Record[0];
  2784. unsigned StartingID;
  2785. if (!PP.getPreprocessingRecord())
  2786. PP.createPreprocessingRecord();
  2787. if (!PP.getPreprocessingRecord()->getExternalSource())
  2788. PP.getPreprocessingRecord()->SetExternalSource(*this);
  2789. StartingID
  2790. = PP.getPreprocessingRecord()
  2791. ->allocateLoadedEntities(F.NumPreprocessedEntities);
  2792. F.BasePreprocessedEntityID = StartingID;
  2793. if (F.NumPreprocessedEntities > 0) {
  2794. // Introduce the global -> local mapping for preprocessed entities in
  2795. // this module.
  2796. GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
  2797. // Introduce the local -> global mapping for preprocessed entities in
  2798. // this module.
  2799. F.PreprocessedEntityRemap.insertOrReplace(
  2800. std::make_pair(LocalBasePreprocessedEntityID,
  2801. F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
  2802. }
  2803. break;
  2804. }
  2805. case PPD_SKIPPED_RANGES: {
  2806. F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
  2807. assert(Blob.size() % sizeof(PPSkippedRange) == 0);
  2808. F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
  2809. if (!PP.getPreprocessingRecord())
  2810. PP.createPreprocessingRecord();
  2811. if (!PP.getPreprocessingRecord()->getExternalSource())
  2812. PP.getPreprocessingRecord()->SetExternalSource(*this);
  2813. F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
  2814. ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
  2815. if (F.NumPreprocessedSkippedRanges > 0)
  2816. GlobalSkippedRangeMap.insert(
  2817. std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
  2818. break;
  2819. }
  2820. case DECL_UPDATE_OFFSETS:
  2821. if (Record.size() % 2 != 0) {
  2822. Error("invalid DECL_UPDATE_OFFSETS block in AST file");
  2823. return Failure;
  2824. }
  2825. for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
  2826. GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
  2827. DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
  2828. // If we've already loaded the decl, perform the updates when we finish
  2829. // loading this block.
  2830. if (Decl *D = GetExistingDecl(ID))
  2831. PendingUpdateRecords.push_back(
  2832. PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
  2833. }
  2834. break;
  2835. case OBJC_CATEGORIES_MAP:
  2836. if (F.LocalNumObjCCategoriesInMap != 0) {
  2837. Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
  2838. return Failure;
  2839. }
  2840. F.LocalNumObjCCategoriesInMap = Record[0];
  2841. F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
  2842. break;
  2843. case OBJC_CATEGORIES:
  2844. F.ObjCCategories.swap(Record);
  2845. break;
  2846. case CUDA_SPECIAL_DECL_REFS:
  2847. // Later tables overwrite earlier ones.
  2848. // FIXME: Modules will have trouble with this.
  2849. CUDASpecialDeclRefs.clear();
  2850. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2851. CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
  2852. break;
  2853. case HEADER_SEARCH_TABLE:
  2854. F.HeaderFileInfoTableData = Blob.data();
  2855. F.LocalNumHeaderFileInfos = Record[1];
  2856. if (Record[0]) {
  2857. F.HeaderFileInfoTable
  2858. = HeaderFileInfoLookupTable::Create(
  2859. (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
  2860. (const unsigned char *)F.HeaderFileInfoTableData,
  2861. HeaderFileInfoTrait(*this, F,
  2862. &PP.getHeaderSearchInfo(),
  2863. Blob.data() + Record[2]));
  2864. PP.getHeaderSearchInfo().SetExternalSource(this);
  2865. if (!PP.getHeaderSearchInfo().getExternalLookup())
  2866. PP.getHeaderSearchInfo().SetExternalLookup(this);
  2867. }
  2868. break;
  2869. case FP_PRAGMA_OPTIONS:
  2870. // Later tables overwrite earlier ones.
  2871. FPPragmaOptions.swap(Record);
  2872. break;
  2873. case OPENCL_EXTENSIONS:
  2874. for (unsigned I = 0, E = Record.size(); I != E; ) {
  2875. auto Name = ReadString(Record, I);
  2876. auto &Opt = OpenCLExtensions.OptMap[Name];
  2877. Opt.Supported = Record[I++] != 0;
  2878. Opt.Enabled = Record[I++] != 0;
  2879. Opt.Avail = Record[I++];
  2880. Opt.Core = Record[I++];
  2881. }
  2882. break;
  2883. case OPENCL_EXTENSION_TYPES:
  2884. for (unsigned I = 0, E = Record.size(); I != E;) {
  2885. auto TypeID = static_cast<::TypeID>(Record[I++]);
  2886. auto *Type = GetType(TypeID).getTypePtr();
  2887. auto NumExt = static_cast<unsigned>(Record[I++]);
  2888. for (unsigned II = 0; II != NumExt; ++II) {
  2889. auto Ext = ReadString(Record, I);
  2890. OpenCLTypeExtMap[Type].insert(Ext);
  2891. }
  2892. }
  2893. break;
  2894. case OPENCL_EXTENSION_DECLS:
  2895. for (unsigned I = 0, E = Record.size(); I != E;) {
  2896. auto DeclID = static_cast<::DeclID>(Record[I++]);
  2897. auto *Decl = GetDecl(DeclID);
  2898. auto NumExt = static_cast<unsigned>(Record[I++]);
  2899. for (unsigned II = 0; II != NumExt; ++II) {
  2900. auto Ext = ReadString(Record, I);
  2901. OpenCLDeclExtMap[Decl].insert(Ext);
  2902. }
  2903. }
  2904. break;
  2905. case TENTATIVE_DEFINITIONS:
  2906. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2907. TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
  2908. break;
  2909. case KNOWN_NAMESPACES:
  2910. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  2911. KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
  2912. break;
  2913. case UNDEFINED_BUT_USED:
  2914. if (UndefinedButUsed.size() % 2 != 0) {
  2915. Error("Invalid existing UndefinedButUsed");
  2916. return Failure;
  2917. }
  2918. if (Record.size() % 2 != 0) {
  2919. Error("invalid undefined-but-used record");
  2920. return Failure;
  2921. }
  2922. for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
  2923. UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
  2924. UndefinedButUsed.push_back(
  2925. ReadSourceLocation(F, Record, I).getRawEncoding());
  2926. }
  2927. break;
  2928. case DELETE_EXPRS_TO_ANALYZE:
  2929. for (unsigned I = 0, N = Record.size(); I != N;) {
  2930. DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
  2931. const uint64_t Count = Record[I++];
  2932. DelayedDeleteExprs.push_back(Count);
  2933. for (uint64_t C = 0; C < Count; ++C) {
  2934. DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
  2935. bool IsArrayForm = Record[I++] == 1;
  2936. DelayedDeleteExprs.push_back(IsArrayForm);
  2937. }
  2938. }
  2939. break;
  2940. case IMPORTED_MODULES:
  2941. if (!F.isModule()) {
  2942. // If we aren't loading a module (which has its own exports), make
  2943. // all of the imported modules visible.
  2944. // FIXME: Deal with macros-only imports.
  2945. for (unsigned I = 0, N = Record.size(); I != N; /**/) {
  2946. unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
  2947. SourceLocation Loc = ReadSourceLocation(F, Record, I);
  2948. if (GlobalID) {
  2949. ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
  2950. if (DeserializationListener)
  2951. DeserializationListener->ModuleImportRead(GlobalID, Loc);
  2952. }
  2953. }
  2954. }
  2955. break;
  2956. case MACRO_OFFSET: {
  2957. if (F.LocalNumMacros != 0) {
  2958. Error("duplicate MACRO_OFFSET record in AST file");
  2959. return Failure;
  2960. }
  2961. F.MacroOffsets = (const uint32_t *)Blob.data();
  2962. F.LocalNumMacros = Record[0];
  2963. unsigned LocalBaseMacroID = Record[1];
  2964. F.BaseMacroID = getTotalNumMacros();
  2965. if (F.LocalNumMacros > 0) {
  2966. // Introduce the global -> local mapping for macros within this module.
  2967. GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
  2968. // Introduce the local -> global mapping for macros within this module.
  2969. F.MacroRemap.insertOrReplace(
  2970. std::make_pair(LocalBaseMacroID,
  2971. F.BaseMacroID - LocalBaseMacroID));
  2972. MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
  2973. }
  2974. break;
  2975. }
  2976. case LATE_PARSED_TEMPLATE:
  2977. LateParsedTemplates.append(Record.begin(), Record.end());
  2978. break;
  2979. case OPTIMIZE_PRAGMA_OPTIONS:
  2980. if (Record.size() != 1) {
  2981. Error("invalid pragma optimize record");
  2982. return Failure;
  2983. }
  2984. OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
  2985. break;
  2986. case MSSTRUCT_PRAGMA_OPTIONS:
  2987. if (Record.size() != 1) {
  2988. Error("invalid pragma ms_struct record");
  2989. return Failure;
  2990. }
  2991. PragmaMSStructState = Record[0];
  2992. break;
  2993. case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
  2994. if (Record.size() != 2) {
  2995. Error("invalid pragma ms_struct record");
  2996. return Failure;
  2997. }
  2998. PragmaMSPointersToMembersState = Record[0];
  2999. PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
  3000. break;
  3001. case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
  3002. for (unsigned I = 0, N = Record.size(); I != N; ++I)
  3003. UnusedLocalTypedefNameCandidates.push_back(
  3004. getGlobalDeclID(F, Record[I]));
  3005. break;
  3006. case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
  3007. if (Record.size() != 1) {
  3008. Error("invalid cuda pragma options record");
  3009. return Failure;
  3010. }
  3011. ForceCUDAHostDeviceDepth = Record[0];
  3012. break;
  3013. case PACK_PRAGMA_OPTIONS: {
  3014. if (Record.size() < 3) {
  3015. Error("invalid pragma pack record");
  3016. return Failure;
  3017. }
  3018. PragmaPackCurrentValue = Record[0];
  3019. PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
  3020. unsigned NumStackEntries = Record[2];
  3021. unsigned Idx = 3;
  3022. // Reset the stack when importing a new module.
  3023. PragmaPackStack.clear();
  3024. for (unsigned I = 0; I < NumStackEntries; ++I) {
  3025. PragmaPackStackEntry Entry;
  3026. Entry.Value = Record[Idx++];
  3027. Entry.Location = ReadSourceLocation(F, Record[Idx++]);
  3028. Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
  3029. PragmaPackStrings.push_back(ReadString(Record, Idx));
  3030. Entry.SlotLabel = PragmaPackStrings.back();
  3031. PragmaPackStack.push_back(Entry);
  3032. }
  3033. break;
  3034. }
  3035. }
  3036. }
  3037. }
  3038. void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
  3039. assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
  3040. // Additional remapping information.
  3041. const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
  3042. const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
  3043. F.ModuleOffsetMap = StringRef();
  3044. // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
  3045. if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
  3046. F.SLocRemap.insert(std::make_pair(0U, 0));
  3047. F.SLocRemap.insert(std::make_pair(2U, 1));
  3048. }
  3049. // Continuous range maps we may be updating in our module.
  3050. using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
  3051. RemapBuilder SLocRemap(F.SLocRemap);
  3052. RemapBuilder IdentifierRemap(F.IdentifierRemap);
  3053. RemapBuilder MacroRemap(F.MacroRemap);
  3054. RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
  3055. RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
  3056. RemapBuilder SelectorRemap(F.SelectorRemap);
  3057. RemapBuilder DeclRemap(F.DeclRemap);
  3058. RemapBuilder TypeRemap(F.TypeRemap);
  3059. while (Data < DataEnd) {
  3060. // FIXME: Looking up dependency modules by filename is horrible. Let's
  3061. // start fixing this with prebuilt and explicit modules and see how it
  3062. // goes...
  3063. using namespace llvm::support;
  3064. ModuleKind Kind = static_cast<ModuleKind>(
  3065. endian::readNext<uint8_t, little, unaligned>(Data));
  3066. uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
  3067. StringRef Name = StringRef((const char*)Data, Len);
  3068. Data += Len;
  3069. ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
  3070. ? ModuleMgr.lookupByModuleName(Name)
  3071. : ModuleMgr.lookupByFileName(Name));
  3072. if (!OM) {
  3073. std::string Msg =
  3074. "SourceLocation remap refers to unknown module, cannot find ";
  3075. Msg.append(Name);
  3076. Error(Msg);
  3077. return;
  3078. }
  3079. uint32_t SLocOffset =
  3080. endian::readNext<uint32_t, little, unaligned>(Data);
  3081. uint32_t IdentifierIDOffset =
  3082. endian::readNext<uint32_t, little, unaligned>(Data);
  3083. uint32_t MacroIDOffset =
  3084. endian::readNext<uint32_t, little, unaligned>(Data);
  3085. uint32_t PreprocessedEntityIDOffset =
  3086. endian::readNext<uint32_t, little, unaligned>(Data);
  3087. uint32_t SubmoduleIDOffset =
  3088. endian::readNext<uint32_t, little, unaligned>(Data);
  3089. uint32_t SelectorIDOffset =
  3090. endian::readNext<uint32_t, little, unaligned>(Data);
  3091. uint32_t DeclIDOffset =
  3092. endian::readNext<uint32_t, little, unaligned>(Data);
  3093. uint32_t TypeIndexOffset =
  3094. endian::readNext<uint32_t, little, unaligned>(Data);
  3095. uint32_t None = std::numeric_limits<uint32_t>::max();
  3096. auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
  3097. RemapBuilder &Remap) {
  3098. if (Offset != None)
  3099. Remap.insert(std::make_pair(Offset,
  3100. static_cast<int>(BaseOffset - Offset)));
  3101. };
  3102. mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
  3103. mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
  3104. mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
  3105. mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
  3106. PreprocessedEntityRemap);
  3107. mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
  3108. mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
  3109. mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
  3110. mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
  3111. // Global -> local mappings.
  3112. F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
  3113. }
  3114. }
  3115. ASTReader::ASTReadResult
  3116. ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
  3117. const ModuleFile *ImportedBy,
  3118. unsigned ClientLoadCapabilities) {
  3119. unsigned Idx = 0;
  3120. F.ModuleMapPath = ReadPath(F, Record, Idx);
  3121. // Try to resolve ModuleName in the current header search context and
  3122. // verify that it is found in the same module map file as we saved. If the
  3123. // top-level AST file is a main file, skip this check because there is no
  3124. // usable header search context.
  3125. assert(!F.ModuleName.empty() &&
  3126. "MODULE_NAME should come before MODULE_MAP_FILE");
  3127. if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
  3128. // An implicitly-loaded module file should have its module listed in some
  3129. // module map file that we've already loaded.
  3130. Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
  3131. auto &Map = PP.getHeaderSearchInfo().getModuleMap();
  3132. const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
  3133. // Don't emit module relocation error if we have -fno-validate-pch
  3134. if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
  3135. assert(ImportedBy && "top-level import should be verified");
  3136. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
  3137. if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
  3138. // This module was defined by an imported (explicit) module.
  3139. Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
  3140. << ASTFE->getName();
  3141. } else {
  3142. // This module was built with a different module map.
  3143. Diag(diag::err_imported_module_not_found)
  3144. << F.ModuleName << F.FileName << ImportedBy->FileName
  3145. << F.ModuleMapPath;
  3146. // In case it was imported by a PCH, there's a chance the user is
  3147. // just missing to include the search path to the directory containing
  3148. // the modulemap.
  3149. if (ImportedBy->Kind == MK_PCH)
  3150. Diag(diag::note_imported_by_pch_module_not_found)
  3151. << llvm::sys::path::parent_path(F.ModuleMapPath);
  3152. }
  3153. }
  3154. return OutOfDate;
  3155. }
  3156. assert(M->Name == F.ModuleName && "found module with different name");
  3157. // Check the primary module map file.
  3158. const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
  3159. if (StoredModMap == nullptr || StoredModMap != ModMap) {
  3160. assert(ModMap && "found module is missing module map file");
  3161. assert(ImportedBy && "top-level import should be verified");
  3162. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  3163. Diag(diag::err_imported_module_modmap_changed)
  3164. << F.ModuleName << ImportedBy->FileName
  3165. << ModMap->getName() << F.ModuleMapPath;
  3166. return OutOfDate;
  3167. }
  3168. llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
  3169. for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
  3170. // FIXME: we should use input files rather than storing names.
  3171. std::string Filename = ReadPath(F, Record, Idx);
  3172. const FileEntry *F =
  3173. FileMgr.getFile(Filename, false, false);
  3174. if (F == nullptr) {
  3175. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  3176. Error("could not find file '" + Filename +"' referenced by AST file");
  3177. return OutOfDate;
  3178. }
  3179. AdditionalStoredMaps.insert(F);
  3180. }
  3181. // Check any additional module map files (e.g. module.private.modulemap)
  3182. // that are not in the pcm.
  3183. if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
  3184. for (const FileEntry *ModMap : *AdditionalModuleMaps) {
  3185. // Remove files that match
  3186. // Note: SmallPtrSet::erase is really remove
  3187. if (!AdditionalStoredMaps.erase(ModMap)) {
  3188. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  3189. Diag(diag::err_module_different_modmap)
  3190. << F.ModuleName << /*new*/0 << ModMap->getName();
  3191. return OutOfDate;
  3192. }
  3193. }
  3194. }
  3195. // Check any additional module map files that are in the pcm, but not
  3196. // found in header search. Cases that match are already removed.
  3197. for (const FileEntry *ModMap : AdditionalStoredMaps) {
  3198. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  3199. Diag(diag::err_module_different_modmap)
  3200. << F.ModuleName << /*not new*/1 << ModMap->getName();
  3201. return OutOfDate;
  3202. }
  3203. }
  3204. if (Listener)
  3205. Listener->ReadModuleMapFile(F.ModuleMapPath);
  3206. return Success;
  3207. }
  3208. /// Move the given method to the back of the global list of methods.
  3209. static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
  3210. // Find the entry for this selector in the method pool.
  3211. Sema::GlobalMethodPool::iterator Known
  3212. = S.MethodPool.find(Method->getSelector());
  3213. if (Known == S.MethodPool.end())
  3214. return;
  3215. // Retrieve the appropriate method list.
  3216. ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
  3217. : Known->second.second;
  3218. bool Found = false;
  3219. for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
  3220. if (!Found) {
  3221. if (List->getMethod() == Method) {
  3222. Found = true;
  3223. } else {
  3224. // Keep searching.
  3225. continue;
  3226. }
  3227. }
  3228. if (List->getNext())
  3229. List->setMethod(List->getNext()->getMethod());
  3230. else
  3231. List->setMethod(Method);
  3232. }
  3233. }
  3234. void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
  3235. assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
  3236. for (Decl *D : Names) {
  3237. bool wasHidden = D->isHidden();
  3238. D->setVisibleDespiteOwningModule();
  3239. if (wasHidden && SemaObj) {
  3240. if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
  3241. moveMethodToBackOfGlobalList(*SemaObj, Method);
  3242. }
  3243. }
  3244. }
  3245. }
  3246. void ASTReader::makeModuleVisible(Module *Mod,
  3247. Module::NameVisibilityKind NameVisibility,
  3248. SourceLocation ImportLoc) {
  3249. llvm::SmallPtrSet<Module *, 4> Visited;
  3250. SmallVector<Module *, 4> Stack;
  3251. Stack.push_back(Mod);
  3252. while (!Stack.empty()) {
  3253. Mod = Stack.pop_back_val();
  3254. if (NameVisibility <= Mod->NameVisibility) {
  3255. // This module already has this level of visibility (or greater), so
  3256. // there is nothing more to do.
  3257. continue;
  3258. }
  3259. if (!Mod->isAvailable()) {
  3260. // Modules that aren't available cannot be made visible.
  3261. continue;
  3262. }
  3263. // Update the module's name visibility.
  3264. Mod->NameVisibility = NameVisibility;
  3265. // If we've already deserialized any names from this module,
  3266. // mark them as visible.
  3267. HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
  3268. if (Hidden != HiddenNamesMap.end()) {
  3269. auto HiddenNames = std::move(*Hidden);
  3270. HiddenNamesMap.erase(Hidden);
  3271. makeNamesVisible(HiddenNames.second, HiddenNames.first);
  3272. assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
  3273. "making names visible added hidden names");
  3274. }
  3275. // Push any exported modules onto the stack to be marked as visible.
  3276. SmallVector<Module *, 16> Exports;
  3277. Mod->getExportedModules(Exports);
  3278. for (SmallVectorImpl<Module *>::iterator
  3279. I = Exports.begin(), E = Exports.end(); I != E; ++I) {
  3280. Module *Exported = *I;
  3281. if (Visited.insert(Exported).second)
  3282. Stack.push_back(Exported);
  3283. }
  3284. }
  3285. }
  3286. /// We've merged the definition \p MergedDef into the existing definition
  3287. /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
  3288. /// visible.
  3289. void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
  3290. NamedDecl *MergedDef) {
  3291. if (Def->isHidden()) {
  3292. // If MergedDef is visible or becomes visible, make the definition visible.
  3293. if (!MergedDef->isHidden())
  3294. Def->setVisibleDespiteOwningModule();
  3295. else {
  3296. getContext().mergeDefinitionIntoModule(
  3297. Def, MergedDef->getImportedOwningModule(),
  3298. /*NotifyListeners*/ false);
  3299. PendingMergedDefinitionsToDeduplicate.insert(Def);
  3300. }
  3301. }
  3302. }
  3303. bool ASTReader::loadGlobalIndex() {
  3304. if (GlobalIndex)
  3305. return false;
  3306. if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
  3307. !PP.getLangOpts().Modules)
  3308. return true;
  3309. // Try to load the global index.
  3310. TriedLoadingGlobalIndex = true;
  3311. StringRef ModuleCachePath
  3312. = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
  3313. std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
  3314. = GlobalModuleIndex::readIndex(ModuleCachePath);
  3315. if (!Result.first)
  3316. return true;
  3317. GlobalIndex.reset(Result.first);
  3318. ModuleMgr.setGlobalIndex(GlobalIndex.get());
  3319. return false;
  3320. }
  3321. bool ASTReader::isGlobalIndexUnavailable() const {
  3322. return PP.getLangOpts().Modules && UseGlobalIndex &&
  3323. !hasGlobalIndex() && TriedLoadingGlobalIndex;
  3324. }
  3325. static void updateModuleTimestamp(ModuleFile &MF) {
  3326. // Overwrite the timestamp file contents so that file's mtime changes.
  3327. std::string TimestampFilename = MF.getTimestampFilename();
  3328. std::error_code EC;
  3329. llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
  3330. if (EC)
  3331. return;
  3332. OS << "Timestamp file\n";
  3333. OS.close();
  3334. OS.clear_error(); // Avoid triggering a fatal error.
  3335. }
  3336. /// Given a cursor at the start of an AST file, scan ahead and drop the
  3337. /// cursor into the start of the given block ID, returning false on success and
  3338. /// true on failure.
  3339. static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
  3340. while (true) {
  3341. llvm::BitstreamEntry Entry = Cursor.advance();
  3342. switch (Entry.Kind) {
  3343. case llvm::BitstreamEntry::Error:
  3344. case llvm::BitstreamEntry::EndBlock:
  3345. return true;
  3346. case llvm::BitstreamEntry::Record:
  3347. // Ignore top-level records.
  3348. Cursor.skipRecord(Entry.ID);
  3349. break;
  3350. case llvm::BitstreamEntry::SubBlock:
  3351. if (Entry.ID == BlockID) {
  3352. if (Cursor.EnterSubBlock(BlockID))
  3353. return true;
  3354. // Found it!
  3355. return false;
  3356. }
  3357. if (Cursor.SkipBlock())
  3358. return true;
  3359. }
  3360. }
  3361. }
  3362. ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
  3363. ModuleKind Type,
  3364. SourceLocation ImportLoc,
  3365. unsigned ClientLoadCapabilities,
  3366. SmallVectorImpl<ImportedSubmodule> *Imported) {
  3367. llvm::SaveAndRestore<SourceLocation>
  3368. SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
  3369. // Defer any pending actions until we get to the end of reading the AST file.
  3370. Deserializing AnASTFile(this);
  3371. // Bump the generation number.
  3372. unsigned PreviousGeneration = 0;
  3373. if (ContextObj)
  3374. PreviousGeneration = incrementGeneration(*ContextObj);
  3375. unsigned NumModules = ModuleMgr.size();
  3376. SmallVector<ImportedModule, 4> Loaded;
  3377. switch (ASTReadResult ReadResult =
  3378. ReadASTCore(FileName, Type, ImportLoc,
  3379. /*ImportedBy=*/nullptr, Loaded, 0, 0,
  3380. ASTFileSignature(), ClientLoadCapabilities)) {
  3381. case Failure:
  3382. case Missing:
  3383. case OutOfDate:
  3384. case VersionMismatch:
  3385. case ConfigurationMismatch:
  3386. case HadErrors: {
  3387. llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
  3388. for (const ImportedModule &IM : Loaded)
  3389. LoadedSet.insert(IM.Mod);
  3390. ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, LoadedSet,
  3391. PP.getLangOpts().Modules
  3392. ? &PP.getHeaderSearchInfo().getModuleMap()
  3393. : nullptr);
  3394. // If we find that any modules are unusable, the global index is going
  3395. // to be out-of-date. Just remove it.
  3396. GlobalIndex.reset();
  3397. ModuleMgr.setGlobalIndex(nullptr);
  3398. return ReadResult;
  3399. }
  3400. case Success:
  3401. break;
  3402. }
  3403. // Here comes stuff that we only do once the entire chain is loaded.
  3404. // Load the AST blocks of all of the modules that we loaded.
  3405. for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
  3406. MEnd = Loaded.end();
  3407. M != MEnd; ++M) {
  3408. ModuleFile &F = *M->Mod;
  3409. // Read the AST block.
  3410. if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
  3411. return Result;
  3412. // Read the extension blocks.
  3413. while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
  3414. if (ASTReadResult Result = ReadExtensionBlock(F))
  3415. return Result;
  3416. }
  3417. // Once read, set the ModuleFile bit base offset and update the size in
  3418. // bits of all files we've seen.
  3419. F.GlobalBitOffset = TotalModulesSizeInBits;
  3420. TotalModulesSizeInBits += F.SizeInBits;
  3421. GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
  3422. // Preload SLocEntries.
  3423. for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
  3424. int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
  3425. // Load it through the SourceManager and don't call ReadSLocEntry()
  3426. // directly because the entry may have already been loaded in which case
  3427. // calling ReadSLocEntry() directly would trigger an assertion in
  3428. // SourceManager.
  3429. SourceMgr.getLoadedSLocEntryByID(Index);
  3430. }
  3431. // Map the original source file ID into the ID space of the current
  3432. // compilation.
  3433. if (F.OriginalSourceFileID.isValid()) {
  3434. F.OriginalSourceFileID = FileID::get(
  3435. F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
  3436. }
  3437. // Preload all the pending interesting identifiers by marking them out of
  3438. // date.
  3439. for (auto Offset : F.PreloadIdentifierOffsets) {
  3440. const unsigned char *Data = reinterpret_cast<const unsigned char *>(
  3441. F.IdentifierTableData + Offset);
  3442. ASTIdentifierLookupTrait Trait(*this, F);
  3443. auto KeyDataLen = Trait.ReadKeyDataLength(Data);
  3444. auto Key = Trait.ReadKey(Data, KeyDataLen.first);
  3445. auto &II = PP.getIdentifierTable().getOwn(Key);
  3446. II.setOutOfDate(true);
  3447. // Mark this identifier as being from an AST file so that we can track
  3448. // whether we need to serialize it.
  3449. markIdentifierFromAST(*this, II);
  3450. // Associate the ID with the identifier so that the writer can reuse it.
  3451. auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
  3452. SetIdentifierInfo(ID, &II);
  3453. }
  3454. }
  3455. // Setup the import locations and notify the module manager that we've
  3456. // committed to these module files.
  3457. for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
  3458. MEnd = Loaded.end();
  3459. M != MEnd; ++M) {
  3460. ModuleFile &F = *M->Mod;
  3461. ModuleMgr.moduleFileAccepted(&F);
  3462. // Set the import location.
  3463. F.DirectImportLoc = ImportLoc;
  3464. // FIXME: We assume that locations from PCH / preamble do not need
  3465. // any translation.
  3466. if (!M->ImportedBy)
  3467. F.ImportLoc = M->ImportLoc;
  3468. else
  3469. F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
  3470. }
  3471. if (!PP.getLangOpts().CPlusPlus ||
  3472. (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
  3473. Type != MK_PrebuiltModule)) {
  3474. // Mark all of the identifiers in the identifier table as being out of date,
  3475. // so that various accessors know to check the loaded modules when the
  3476. // identifier is used.
  3477. //
  3478. // For C++ modules, we don't need information on many identifiers (just
  3479. // those that provide macros or are poisoned), so we mark all of
  3480. // the interesting ones via PreloadIdentifierOffsets.
  3481. for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
  3482. IdEnd = PP.getIdentifierTable().end();
  3483. Id != IdEnd; ++Id)
  3484. Id->second->setOutOfDate(true);
  3485. }
  3486. // Mark selectors as out of date.
  3487. for (auto Sel : SelectorGeneration)
  3488. SelectorOutOfDate[Sel.first] = true;
  3489. // Resolve any unresolved module exports.
  3490. for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
  3491. UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
  3492. SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
  3493. Module *ResolvedMod = getSubmodule(GlobalID);
  3494. switch (Unresolved.Kind) {
  3495. case UnresolvedModuleRef::Conflict:
  3496. if (ResolvedMod) {
  3497. Module::Conflict Conflict;
  3498. Conflict.Other = ResolvedMod;
  3499. Conflict.Message = Unresolved.String.str();
  3500. Unresolved.Mod->Conflicts.push_back(Conflict);
  3501. }
  3502. continue;
  3503. case UnresolvedModuleRef::Import:
  3504. if (ResolvedMod)
  3505. Unresolved.Mod->Imports.insert(ResolvedMod);
  3506. continue;
  3507. case UnresolvedModuleRef::Export:
  3508. if (ResolvedMod || Unresolved.IsWildcard)
  3509. Unresolved.Mod->Exports.push_back(
  3510. Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
  3511. continue;
  3512. }
  3513. }
  3514. UnresolvedModuleRefs.clear();
  3515. if (Imported)
  3516. Imported->append(ImportedModules.begin(),
  3517. ImportedModules.end());
  3518. // FIXME: How do we load the 'use'd modules? They may not be submodules.
  3519. // Might be unnecessary as use declarations are only used to build the
  3520. // module itself.
  3521. if (ContextObj)
  3522. InitializeContext();
  3523. if (SemaObj)
  3524. UpdateSema();
  3525. if (DeserializationListener)
  3526. DeserializationListener->ReaderInitialized(this);
  3527. ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
  3528. if (PrimaryModule.OriginalSourceFileID.isValid()) {
  3529. // If this AST file is a precompiled preamble, then set the
  3530. // preamble file ID of the source manager to the file source file
  3531. // from which the preamble was built.
  3532. if (Type == MK_Preamble) {
  3533. SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
  3534. } else if (Type == MK_MainFile) {
  3535. SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
  3536. }
  3537. }
  3538. // For any Objective-C class definitions we have already loaded, make sure
  3539. // that we load any additional categories.
  3540. if (ContextObj) {
  3541. for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
  3542. loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
  3543. ObjCClassesLoaded[I],
  3544. PreviousGeneration);
  3545. }
  3546. }
  3547. if (PP.getHeaderSearchInfo()
  3548. .getHeaderSearchOpts()
  3549. .ModulesValidateOncePerBuildSession) {
  3550. // Now we are certain that the module and all modules it depends on are
  3551. // up to date. Create or update timestamp files for modules that are
  3552. // located in the module cache (not for PCH files that could be anywhere
  3553. // in the filesystem).
  3554. for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
  3555. ImportedModule &M = Loaded[I];
  3556. if (M.Mod->Kind == MK_ImplicitModule) {
  3557. updateModuleTimestamp(*M.Mod);
  3558. }
  3559. }
  3560. }
  3561. return Success;
  3562. }
  3563. static ASTFileSignature readASTFileSignature(StringRef PCH);
  3564. /// Whether \p Stream starts with the AST/PCH file magic number 'CPCH'.
  3565. static bool startsWithASTFileMagic(BitstreamCursor &Stream) {
  3566. return Stream.canSkipToPos(4) &&
  3567. Stream.Read(8) == 'C' &&
  3568. Stream.Read(8) == 'P' &&
  3569. Stream.Read(8) == 'C' &&
  3570. Stream.Read(8) == 'H';
  3571. }
  3572. static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
  3573. switch (Kind) {
  3574. case MK_PCH:
  3575. return 0; // PCH
  3576. case MK_ImplicitModule:
  3577. case MK_ExplicitModule:
  3578. case MK_PrebuiltModule:
  3579. return 1; // module
  3580. case MK_MainFile:
  3581. case MK_Preamble:
  3582. return 2; // main source file
  3583. }
  3584. llvm_unreachable("unknown module kind");
  3585. }
  3586. ASTReader::ASTReadResult
  3587. ASTReader::ReadASTCore(StringRef FileName,
  3588. ModuleKind Type,
  3589. SourceLocation ImportLoc,
  3590. ModuleFile *ImportedBy,
  3591. SmallVectorImpl<ImportedModule> &Loaded,
  3592. off_t ExpectedSize, time_t ExpectedModTime,
  3593. ASTFileSignature ExpectedSignature,
  3594. unsigned ClientLoadCapabilities) {
  3595. ModuleFile *M;
  3596. std::string ErrorStr;
  3597. ModuleManager::AddModuleResult AddResult
  3598. = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
  3599. getGeneration(), ExpectedSize, ExpectedModTime,
  3600. ExpectedSignature, readASTFileSignature,
  3601. M, ErrorStr);
  3602. switch (AddResult) {
  3603. case ModuleManager::AlreadyLoaded:
  3604. return Success;
  3605. case ModuleManager::NewlyLoaded:
  3606. // Load module file below.
  3607. break;
  3608. case ModuleManager::Missing:
  3609. // The module file was missing; if the client can handle that, return
  3610. // it.
  3611. if (ClientLoadCapabilities & ARR_Missing)
  3612. return Missing;
  3613. // Otherwise, return an error.
  3614. Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
  3615. << FileName << !ErrorStr.empty()
  3616. << ErrorStr;
  3617. return Failure;
  3618. case ModuleManager::OutOfDate:
  3619. // We couldn't load the module file because it is out-of-date. If the
  3620. // client can handle out-of-date, return it.
  3621. if (ClientLoadCapabilities & ARR_OutOfDate)
  3622. return OutOfDate;
  3623. // Otherwise, return an error.
  3624. Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
  3625. << FileName << !ErrorStr.empty()
  3626. << ErrorStr;
  3627. return Failure;
  3628. }
  3629. assert(M && "Missing module file");
  3630. ModuleFile &F = *M;
  3631. BitstreamCursor &Stream = F.Stream;
  3632. Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
  3633. F.SizeInBits = F.Buffer->getBufferSize() * 8;
  3634. // Sniff for the signature.
  3635. if (!startsWithASTFileMagic(Stream)) {
  3636. Diag(diag::err_module_file_invalid) << moduleKindForDiagnostic(Type)
  3637. << FileName;
  3638. return Failure;
  3639. }
  3640. // This is used for compatibility with older PCH formats.
  3641. bool HaveReadControlBlock = false;
  3642. while (true) {
  3643. llvm::BitstreamEntry Entry = Stream.advance();
  3644. switch (Entry.Kind) {
  3645. case llvm::BitstreamEntry::Error:
  3646. case llvm::BitstreamEntry::Record:
  3647. case llvm::BitstreamEntry::EndBlock:
  3648. Error("invalid record at top-level of AST file");
  3649. return Failure;
  3650. case llvm::BitstreamEntry::SubBlock:
  3651. break;
  3652. }
  3653. switch (Entry.ID) {
  3654. case CONTROL_BLOCK_ID:
  3655. HaveReadControlBlock = true;
  3656. switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
  3657. case Success:
  3658. // Check that we didn't try to load a non-module AST file as a module.
  3659. //
  3660. // FIXME: Should we also perform the converse check? Loading a module as
  3661. // a PCH file sort of works, but it's a bit wonky.
  3662. if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
  3663. Type == MK_PrebuiltModule) &&
  3664. F.ModuleName.empty()) {
  3665. auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
  3666. if (Result != OutOfDate ||
  3667. (ClientLoadCapabilities & ARR_OutOfDate) == 0)
  3668. Diag(diag::err_module_file_not_module) << FileName;
  3669. return Result;
  3670. }
  3671. break;
  3672. case Failure: return Failure;
  3673. case Missing: return Missing;
  3674. case OutOfDate: return OutOfDate;
  3675. case VersionMismatch: return VersionMismatch;
  3676. case ConfigurationMismatch: return ConfigurationMismatch;
  3677. case HadErrors: return HadErrors;
  3678. }
  3679. break;
  3680. case AST_BLOCK_ID:
  3681. if (!HaveReadControlBlock) {
  3682. if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
  3683. Diag(diag::err_pch_version_too_old);
  3684. return VersionMismatch;
  3685. }
  3686. // Record that we've loaded this module.
  3687. Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
  3688. return Success;
  3689. case UNHASHED_CONTROL_BLOCK_ID:
  3690. // This block is handled using look-ahead during ReadControlBlock. We
  3691. // shouldn't get here!
  3692. Error("malformed block record in AST file");
  3693. return Failure;
  3694. default:
  3695. if (Stream.SkipBlock()) {
  3696. Error("malformed block record in AST file");
  3697. return Failure;
  3698. }
  3699. break;
  3700. }
  3701. }
  3702. return Success;
  3703. }
  3704. ASTReader::ASTReadResult
  3705. ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
  3706. unsigned ClientLoadCapabilities) {
  3707. const HeaderSearchOptions &HSOpts =
  3708. PP.getHeaderSearchInfo().getHeaderSearchOpts();
  3709. bool AllowCompatibleConfigurationMismatch =
  3710. F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
  3711. ASTReadResult Result = readUnhashedControlBlockImpl(
  3712. &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
  3713. Listener.get(),
  3714. WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
  3715. // If F was directly imported by another module, it's implicitly validated by
  3716. // the importing module.
  3717. if (DisableValidation || WasImportedBy ||
  3718. (AllowConfigurationMismatch && Result == ConfigurationMismatch))
  3719. return Success;
  3720. if (Result == Failure) {
  3721. Error("malformed block record in AST file");
  3722. return Failure;
  3723. }
  3724. if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
  3725. // If this module has already been finalized in the PCMCache, we're stuck
  3726. // with it; we can only load a single version of each module.
  3727. //
  3728. // This can happen when a module is imported in two contexts: in one, as a
  3729. // user module; in another, as a system module (due to an import from
  3730. // another module marked with the [system] flag). It usually indicates a
  3731. // bug in the module map: this module should also be marked with [system].
  3732. //
  3733. // If -Wno-system-headers (the default), and the first import is as a
  3734. // system module, then validation will fail during the as-user import,
  3735. // since -Werror flags won't have been validated. However, it's reasonable
  3736. // to treat this consistently as a system module.
  3737. //
  3738. // If -Wsystem-headers, the PCM on disk was built with
  3739. // -Wno-system-headers, and the first import is as a user module, then
  3740. // validation will fail during the as-system import since the PCM on disk
  3741. // doesn't guarantee that -Werror was respected. However, the -Werror
  3742. // flags were checked during the initial as-user import.
  3743. if (PCMCache.isBufferFinal(F.FileName)) {
  3744. Diag(diag::warn_module_system_bit_conflict) << F.FileName;
  3745. return Success;
  3746. }
  3747. }
  3748. return Result;
  3749. }
  3750. ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
  3751. ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
  3752. bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
  3753. bool ValidateDiagnosticOptions) {
  3754. // Initialize a stream.
  3755. BitstreamCursor Stream(StreamData);
  3756. // Sniff for the signature.
  3757. if (!startsWithASTFileMagic(Stream))
  3758. return Failure;
  3759. // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
  3760. if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
  3761. return Failure;
  3762. // Read all of the records in the options block.
  3763. RecordData Record;
  3764. ASTReadResult Result = Success;
  3765. while (true) {
  3766. llvm::BitstreamEntry Entry = Stream.advance();
  3767. switch (Entry.Kind) {
  3768. case llvm::BitstreamEntry::Error:
  3769. case llvm::BitstreamEntry::SubBlock:
  3770. return Failure;
  3771. case llvm::BitstreamEntry::EndBlock:
  3772. return Result;
  3773. case llvm::BitstreamEntry::Record:
  3774. // The interesting case.
  3775. break;
  3776. }
  3777. // Read and process a record.
  3778. Record.clear();
  3779. switch (
  3780. (UnhashedControlBlockRecordTypes)Stream.readRecord(Entry.ID, Record)) {
  3781. case SIGNATURE:
  3782. if (F)
  3783. std::copy(Record.begin(), Record.end(), F->Signature.data());
  3784. break;
  3785. case DIAGNOSTIC_OPTIONS: {
  3786. bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
  3787. if (Listener && ValidateDiagnosticOptions &&
  3788. !AllowCompatibleConfigurationMismatch &&
  3789. ParseDiagnosticOptions(Record, Complain, *Listener))
  3790. Result = OutOfDate; // Don't return early. Read the signature.
  3791. break;
  3792. }
  3793. case DIAG_PRAGMA_MAPPINGS:
  3794. if (!F)
  3795. break;
  3796. if (F->PragmaDiagMappings.empty())
  3797. F->PragmaDiagMappings.swap(Record);
  3798. else
  3799. F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
  3800. Record.begin(), Record.end());
  3801. break;
  3802. }
  3803. }
  3804. }
  3805. /// Parse a record and blob containing module file extension metadata.
  3806. static bool parseModuleFileExtensionMetadata(
  3807. const SmallVectorImpl<uint64_t> &Record,
  3808. StringRef Blob,
  3809. ModuleFileExtensionMetadata &Metadata) {
  3810. if (Record.size() < 4) return true;
  3811. Metadata.MajorVersion = Record[0];
  3812. Metadata.MinorVersion = Record[1];
  3813. unsigned BlockNameLen = Record[2];
  3814. unsigned UserInfoLen = Record[3];
  3815. if (BlockNameLen + UserInfoLen > Blob.size()) return true;
  3816. Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
  3817. Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
  3818. Blob.data() + BlockNameLen + UserInfoLen);
  3819. return false;
  3820. }
  3821. ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
  3822. BitstreamCursor &Stream = F.Stream;
  3823. RecordData Record;
  3824. while (true) {
  3825. llvm::BitstreamEntry Entry = Stream.advance();
  3826. switch (Entry.Kind) {
  3827. case llvm::BitstreamEntry::SubBlock:
  3828. if (Stream.SkipBlock())
  3829. return Failure;
  3830. continue;
  3831. case llvm::BitstreamEntry::EndBlock:
  3832. return Success;
  3833. case llvm::BitstreamEntry::Error:
  3834. return HadErrors;
  3835. case llvm::BitstreamEntry::Record:
  3836. break;
  3837. }
  3838. Record.clear();
  3839. StringRef Blob;
  3840. unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
  3841. switch (RecCode) {
  3842. case EXTENSION_METADATA: {
  3843. ModuleFileExtensionMetadata Metadata;
  3844. if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
  3845. return Failure;
  3846. // Find a module file extension with this block name.
  3847. auto Known = ModuleFileExtensions.find(Metadata.BlockName);
  3848. if (Known == ModuleFileExtensions.end()) break;
  3849. // Form a reader.
  3850. if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
  3851. F, Stream)) {
  3852. F.ExtensionReaders.push_back(std::move(Reader));
  3853. }
  3854. break;
  3855. }
  3856. }
  3857. }
  3858. return Success;
  3859. }
  3860. void ASTReader::InitializeContext() {
  3861. assert(ContextObj && "no context to initialize");
  3862. ASTContext &Context = *ContextObj;
  3863. // If there's a listener, notify them that we "read" the translation unit.
  3864. if (DeserializationListener)
  3865. DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
  3866. Context.getTranslationUnitDecl());
  3867. // FIXME: Find a better way to deal with collisions between these
  3868. // built-in types. Right now, we just ignore the problem.
  3869. // Load the special types.
  3870. if (SpecialTypes.size() >= NumSpecialTypeIDs) {
  3871. if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
  3872. if (!Context.CFConstantStringTypeDecl)
  3873. Context.setCFConstantStringType(GetType(String));
  3874. }
  3875. if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
  3876. QualType FileType = GetType(File);
  3877. if (FileType.isNull()) {
  3878. Error("FILE type is NULL");
  3879. return;
  3880. }
  3881. if (!Context.FILEDecl) {
  3882. if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
  3883. Context.setFILEDecl(Typedef->getDecl());
  3884. else {
  3885. const TagType *Tag = FileType->getAs<TagType>();
  3886. if (!Tag) {
  3887. Error("Invalid FILE type in AST file");
  3888. return;
  3889. }
  3890. Context.setFILEDecl(Tag->getDecl());
  3891. }
  3892. }
  3893. }
  3894. if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
  3895. QualType Jmp_bufType = GetType(Jmp_buf);
  3896. if (Jmp_bufType.isNull()) {
  3897. Error("jmp_buf type is NULL");
  3898. return;
  3899. }
  3900. if (!Context.jmp_bufDecl) {
  3901. if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
  3902. Context.setjmp_bufDecl(Typedef->getDecl());
  3903. else {
  3904. const TagType *Tag = Jmp_bufType->getAs<TagType>();
  3905. if (!Tag) {
  3906. Error("Invalid jmp_buf type in AST file");
  3907. return;
  3908. }
  3909. Context.setjmp_bufDecl(Tag->getDecl());
  3910. }
  3911. }
  3912. }
  3913. if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
  3914. QualType Sigjmp_bufType = GetType(Sigjmp_buf);
  3915. if (Sigjmp_bufType.isNull()) {
  3916. Error("sigjmp_buf type is NULL");
  3917. return;
  3918. }
  3919. if (!Context.sigjmp_bufDecl) {
  3920. if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
  3921. Context.setsigjmp_bufDecl(Typedef->getDecl());
  3922. else {
  3923. const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
  3924. assert(Tag && "Invalid sigjmp_buf type in AST file");
  3925. Context.setsigjmp_bufDecl(Tag->getDecl());
  3926. }
  3927. }
  3928. }
  3929. if (unsigned ObjCIdRedef
  3930. = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
  3931. if (Context.ObjCIdRedefinitionType.isNull())
  3932. Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
  3933. }
  3934. if (unsigned ObjCClassRedef
  3935. = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
  3936. if (Context.ObjCClassRedefinitionType.isNull())
  3937. Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
  3938. }
  3939. if (unsigned ObjCSelRedef
  3940. = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
  3941. if (Context.ObjCSelRedefinitionType.isNull())
  3942. Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
  3943. }
  3944. if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
  3945. QualType Ucontext_tType = GetType(Ucontext_t);
  3946. if (Ucontext_tType.isNull()) {
  3947. Error("ucontext_t type is NULL");
  3948. return;
  3949. }
  3950. if (!Context.ucontext_tDecl) {
  3951. if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
  3952. Context.setucontext_tDecl(Typedef->getDecl());
  3953. else {
  3954. const TagType *Tag = Ucontext_tType->getAs<TagType>();
  3955. assert(Tag && "Invalid ucontext_t type in AST file");
  3956. Context.setucontext_tDecl(Tag->getDecl());
  3957. }
  3958. }
  3959. }
  3960. }
  3961. ReadPragmaDiagnosticMappings(Context.getDiagnostics());
  3962. // If there were any CUDA special declarations, deserialize them.
  3963. if (!CUDASpecialDeclRefs.empty()) {
  3964. assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
  3965. Context.setcudaConfigureCallDecl(
  3966. cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
  3967. }
  3968. // Re-export any modules that were imported by a non-module AST file.
  3969. // FIXME: This does not make macro-only imports visible again.
  3970. for (auto &Import : ImportedModules) {
  3971. if (Module *Imported = getSubmodule(Import.ID)) {
  3972. makeModuleVisible(Imported, Module::AllVisible,
  3973. /*ImportLoc=*/Import.ImportLoc);
  3974. if (Import.ImportLoc.isValid())
  3975. PP.makeModuleVisible(Imported, Import.ImportLoc);
  3976. // FIXME: should we tell Sema to make the module visible too?
  3977. }
  3978. }
  3979. ImportedModules.clear();
  3980. }
  3981. void ASTReader::finalizeForWriting() {
  3982. // Nothing to do for now.
  3983. }
  3984. /// Reads and return the signature record from \p PCH's control block, or
  3985. /// else returns 0.
  3986. static ASTFileSignature readASTFileSignature(StringRef PCH) {
  3987. BitstreamCursor Stream(PCH);
  3988. if (!startsWithASTFileMagic(Stream))
  3989. return ASTFileSignature();
  3990. // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
  3991. if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
  3992. return ASTFileSignature();
  3993. // Scan for SIGNATURE inside the diagnostic options block.
  3994. ASTReader::RecordData Record;
  3995. while (true) {
  3996. llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
  3997. if (Entry.Kind != llvm::BitstreamEntry::Record)
  3998. return ASTFileSignature();
  3999. Record.clear();
  4000. StringRef Blob;
  4001. if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
  4002. return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
  4003. (uint32_t)Record[3], (uint32_t)Record[4]}}};
  4004. }
  4005. }
  4006. /// Retrieve the name of the original source file name
  4007. /// directly from the AST file, without actually loading the AST
  4008. /// file.
  4009. std::string ASTReader::getOriginalSourceFile(
  4010. const std::string &ASTFileName, FileManager &FileMgr,
  4011. const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
  4012. // Open the AST file.
  4013. auto Buffer = FileMgr.getBufferForFile(ASTFileName);
  4014. if (!Buffer) {
  4015. Diags.Report(diag::err_fe_unable_to_read_pch_file)
  4016. << ASTFileName << Buffer.getError().message();
  4017. return std::string();
  4018. }
  4019. // Initialize the stream
  4020. BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
  4021. // Sniff for the signature.
  4022. if (!startsWithASTFileMagic(Stream)) {
  4023. Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
  4024. return std::string();
  4025. }
  4026. // Scan for the CONTROL_BLOCK_ID block.
  4027. if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
  4028. Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
  4029. return std::string();
  4030. }
  4031. // Scan for ORIGINAL_FILE inside the control block.
  4032. RecordData Record;
  4033. while (true) {
  4034. llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
  4035. if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
  4036. return std::string();
  4037. if (Entry.Kind != llvm::BitstreamEntry::Record) {
  4038. Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
  4039. return std::string();
  4040. }
  4041. Record.clear();
  4042. StringRef Blob;
  4043. if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
  4044. return Blob.str();
  4045. }
  4046. }
  4047. namespace {
  4048. class SimplePCHValidator : public ASTReaderListener {
  4049. const LangOptions &ExistingLangOpts;
  4050. const TargetOptions &ExistingTargetOpts;
  4051. const PreprocessorOptions &ExistingPPOpts;
  4052. std::string ExistingModuleCachePath;
  4053. FileManager &FileMgr;
  4054. public:
  4055. SimplePCHValidator(const LangOptions &ExistingLangOpts,
  4056. const TargetOptions &ExistingTargetOpts,
  4057. const PreprocessorOptions &ExistingPPOpts,
  4058. StringRef ExistingModuleCachePath,
  4059. FileManager &FileMgr)
  4060. : ExistingLangOpts(ExistingLangOpts),
  4061. ExistingTargetOpts(ExistingTargetOpts),
  4062. ExistingPPOpts(ExistingPPOpts),
  4063. ExistingModuleCachePath(ExistingModuleCachePath),
  4064. FileMgr(FileMgr) {}
  4065. bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
  4066. bool AllowCompatibleDifferences) override {
  4067. return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
  4068. AllowCompatibleDifferences);
  4069. }
  4070. bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
  4071. bool AllowCompatibleDifferences) override {
  4072. return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
  4073. AllowCompatibleDifferences);
  4074. }
  4075. bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
  4076. StringRef SpecificModuleCachePath,
  4077. bool Complain) override {
  4078. return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
  4079. ExistingModuleCachePath,
  4080. nullptr, ExistingLangOpts);
  4081. }
  4082. bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
  4083. bool Complain,
  4084. std::string &SuggestedPredefines) override {
  4085. return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
  4086. SuggestedPredefines, ExistingLangOpts);
  4087. }
  4088. };
  4089. } // namespace
  4090. bool ASTReader::readASTFileControlBlock(
  4091. StringRef Filename, FileManager &FileMgr,
  4092. const PCHContainerReader &PCHContainerRdr,
  4093. bool FindModuleFileExtensions,
  4094. ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
  4095. // Open the AST file.
  4096. // FIXME: This allows use of the VFS; we do not allow use of the
  4097. // VFS when actually loading a module.
  4098. auto Buffer = FileMgr.getBufferForFile(Filename);
  4099. if (!Buffer) {
  4100. return true;
  4101. }
  4102. // Initialize the stream
  4103. StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
  4104. BitstreamCursor Stream(Bytes);
  4105. // Sniff for the signature.
  4106. if (!startsWithASTFileMagic(Stream))
  4107. return true;
  4108. // Scan for the CONTROL_BLOCK_ID block.
  4109. if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
  4110. return true;
  4111. bool NeedsInputFiles = Listener.needsInputFileVisitation();
  4112. bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
  4113. bool NeedsImports = Listener.needsImportVisitation();
  4114. BitstreamCursor InputFilesCursor;
  4115. RecordData Record;
  4116. std::string ModuleDir;
  4117. bool DoneWithControlBlock = false;
  4118. while (!DoneWithControlBlock) {
  4119. llvm::BitstreamEntry Entry = Stream.advance();
  4120. switch (Entry.Kind) {
  4121. case llvm::BitstreamEntry::SubBlock: {
  4122. switch (Entry.ID) {
  4123. case OPTIONS_BLOCK_ID: {
  4124. std::string IgnoredSuggestedPredefines;
  4125. if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
  4126. /*AllowCompatibleConfigurationMismatch*/ false,
  4127. Listener, IgnoredSuggestedPredefines) != Success)
  4128. return true;
  4129. break;
  4130. }
  4131. case INPUT_FILES_BLOCK_ID:
  4132. InputFilesCursor = Stream;
  4133. if (Stream.SkipBlock() ||
  4134. (NeedsInputFiles &&
  4135. ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)))
  4136. return true;
  4137. break;
  4138. default:
  4139. if (Stream.SkipBlock())
  4140. return true;
  4141. break;
  4142. }
  4143. continue;
  4144. }
  4145. case llvm::BitstreamEntry::EndBlock:
  4146. DoneWithControlBlock = true;
  4147. break;
  4148. case llvm::BitstreamEntry::Error:
  4149. return true;
  4150. case llvm::BitstreamEntry::Record:
  4151. break;
  4152. }
  4153. if (DoneWithControlBlock) break;
  4154. Record.clear();
  4155. StringRef Blob;
  4156. unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
  4157. switch ((ControlRecordTypes)RecCode) {
  4158. case METADATA:
  4159. if (Record[0] != VERSION_MAJOR)
  4160. return true;
  4161. if (Listener.ReadFullVersionInformation(Blob))
  4162. return true;
  4163. break;
  4164. case MODULE_NAME:
  4165. Listener.ReadModuleName(Blob);
  4166. break;
  4167. case MODULE_DIRECTORY:
  4168. ModuleDir = Blob;
  4169. break;
  4170. case MODULE_MAP_FILE: {
  4171. unsigned Idx = 0;
  4172. auto Path = ReadString(Record, Idx);
  4173. ResolveImportedPath(Path, ModuleDir);
  4174. Listener.ReadModuleMapFile(Path);
  4175. break;
  4176. }
  4177. case INPUT_FILE_OFFSETS: {
  4178. if (!NeedsInputFiles)
  4179. break;
  4180. unsigned NumInputFiles = Record[0];
  4181. unsigned NumUserFiles = Record[1];
  4182. const llvm::support::unaligned_uint64_t *InputFileOffs =
  4183. (const llvm::support::unaligned_uint64_t *)Blob.data();
  4184. for (unsigned I = 0; I != NumInputFiles; ++I) {
  4185. // Go find this input file.
  4186. bool isSystemFile = I >= NumUserFiles;
  4187. if (isSystemFile && !NeedsSystemInputFiles)
  4188. break; // the rest are system input files
  4189. BitstreamCursor &Cursor = InputFilesCursor;
  4190. SavedStreamPosition SavedPosition(Cursor);
  4191. Cursor.JumpToBit(InputFileOffs[I]);
  4192. unsigned Code = Cursor.ReadCode();
  4193. RecordData Record;
  4194. StringRef Blob;
  4195. bool shouldContinue = false;
  4196. switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
  4197. case INPUT_FILE:
  4198. bool Overridden = static_cast<bool>(Record[3]);
  4199. std::string Filename = Blob;
  4200. ResolveImportedPath(Filename, ModuleDir);
  4201. shouldContinue = Listener.visitInputFile(
  4202. Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
  4203. break;
  4204. }
  4205. if (!shouldContinue)
  4206. break;
  4207. }
  4208. break;
  4209. }
  4210. case IMPORTS: {
  4211. if (!NeedsImports)
  4212. break;
  4213. unsigned Idx = 0, N = Record.size();
  4214. while (Idx < N) {
  4215. // Read information about the AST file.
  4216. Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
  4217. std::string ModuleName = ReadString(Record, Idx);
  4218. std::string Filename = ReadString(Record, Idx);
  4219. ResolveImportedPath(Filename, ModuleDir);
  4220. Listener.visitImport(ModuleName, Filename);
  4221. }
  4222. break;
  4223. }
  4224. default:
  4225. // No other validation to perform.
  4226. break;
  4227. }
  4228. }
  4229. // Look for module file extension blocks, if requested.
  4230. if (FindModuleFileExtensions) {
  4231. BitstreamCursor SavedStream = Stream;
  4232. while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
  4233. bool DoneWithExtensionBlock = false;
  4234. while (!DoneWithExtensionBlock) {
  4235. llvm::BitstreamEntry Entry = Stream.advance();
  4236. switch (Entry.Kind) {
  4237. case llvm::BitstreamEntry::SubBlock:
  4238. if (Stream.SkipBlock())
  4239. return true;
  4240. continue;
  4241. case llvm::BitstreamEntry::EndBlock:
  4242. DoneWithExtensionBlock = true;
  4243. continue;
  4244. case llvm::BitstreamEntry::Error:
  4245. return true;
  4246. case llvm::BitstreamEntry::Record:
  4247. break;
  4248. }
  4249. Record.clear();
  4250. StringRef Blob;
  4251. unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
  4252. switch (RecCode) {
  4253. case EXTENSION_METADATA: {
  4254. ModuleFileExtensionMetadata Metadata;
  4255. if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
  4256. return true;
  4257. Listener.readModuleFileExtension(Metadata);
  4258. break;
  4259. }
  4260. }
  4261. }
  4262. }
  4263. Stream = SavedStream;
  4264. }
  4265. // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
  4266. if (readUnhashedControlBlockImpl(
  4267. nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
  4268. /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
  4269. ValidateDiagnosticOptions) != Success)
  4270. return true;
  4271. return false;
  4272. }
  4273. bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
  4274. const PCHContainerReader &PCHContainerRdr,
  4275. const LangOptions &LangOpts,
  4276. const TargetOptions &TargetOpts,
  4277. const PreprocessorOptions &PPOpts,
  4278. StringRef ExistingModuleCachePath) {
  4279. SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
  4280. ExistingModuleCachePath, FileMgr);
  4281. return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
  4282. /*FindModuleFileExtensions=*/false,
  4283. validator,
  4284. /*ValidateDiagnosticOptions=*/true);
  4285. }
  4286. ASTReader::ASTReadResult
  4287. ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
  4288. // Enter the submodule block.
  4289. if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
  4290. Error("malformed submodule block record in AST file");
  4291. return Failure;
  4292. }
  4293. ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
  4294. bool First = true;
  4295. Module *CurrentModule = nullptr;
  4296. RecordData Record;
  4297. while (true) {
  4298. llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
  4299. switch (Entry.Kind) {
  4300. case llvm::BitstreamEntry::SubBlock: // Handled for us already.
  4301. case llvm::BitstreamEntry::Error:
  4302. Error("malformed block record in AST file");
  4303. return Failure;
  4304. case llvm::BitstreamEntry::EndBlock:
  4305. return Success;
  4306. case llvm::BitstreamEntry::Record:
  4307. // The interesting case.
  4308. break;
  4309. }
  4310. // Read a record.
  4311. StringRef Blob;
  4312. Record.clear();
  4313. auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
  4314. if ((Kind == SUBMODULE_METADATA) != First) {
  4315. Error("submodule metadata record should be at beginning of block");
  4316. return Failure;
  4317. }
  4318. First = false;
  4319. // Submodule information is only valid if we have a current module.
  4320. // FIXME: Should we error on these cases?
  4321. if (!CurrentModule && Kind != SUBMODULE_METADATA &&
  4322. Kind != SUBMODULE_DEFINITION)
  4323. continue;
  4324. switch (Kind) {
  4325. default: // Default behavior: ignore.
  4326. break;
  4327. case SUBMODULE_DEFINITION: {
  4328. if (Record.size() < 12) {
  4329. Error("malformed module definition");
  4330. return Failure;
  4331. }
  4332. StringRef Name = Blob;
  4333. unsigned Idx = 0;
  4334. SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
  4335. SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
  4336. Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
  4337. bool IsFramework = Record[Idx++];
  4338. bool IsExplicit = Record[Idx++];
  4339. bool IsSystem = Record[Idx++];
  4340. bool IsExternC = Record[Idx++];
  4341. bool InferSubmodules = Record[Idx++];
  4342. bool InferExplicitSubmodules = Record[Idx++];
  4343. bool InferExportWildcard = Record[Idx++];
  4344. bool ConfigMacrosExhaustive = Record[Idx++];
  4345. bool ModuleMapIsPrivate = Record[Idx++];
  4346. Module *ParentModule = nullptr;
  4347. if (Parent)
  4348. ParentModule = getSubmodule(Parent);
  4349. // Retrieve this (sub)module from the module map, creating it if
  4350. // necessary.
  4351. CurrentModule =
  4352. ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
  4353. .first;
  4354. // FIXME: set the definition loc for CurrentModule, or call
  4355. // ModMap.setInferredModuleAllowedBy()
  4356. SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
  4357. if (GlobalIndex >= SubmodulesLoaded.size() ||
  4358. SubmodulesLoaded[GlobalIndex]) {
  4359. Error("too many submodules");
  4360. return Failure;
  4361. }
  4362. if (!ParentModule) {
  4363. if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
  4364. // Don't emit module relocation error if we have -fno-validate-pch
  4365. if (!PP.getPreprocessorOpts().DisablePCHValidation &&
  4366. CurFile != F.File) {
  4367. if (!Diags.isDiagnosticInFlight()) {
  4368. Diag(diag::err_module_file_conflict)
  4369. << CurrentModule->getTopLevelModuleName()
  4370. << CurFile->getName()
  4371. << F.File->getName();
  4372. }
  4373. return Failure;
  4374. }
  4375. }
  4376. CurrentModule->setASTFile(F.File);
  4377. CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
  4378. }
  4379. CurrentModule->Kind = Kind;
  4380. CurrentModule->Signature = F.Signature;
  4381. CurrentModule->IsFromModuleFile = true;
  4382. CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
  4383. CurrentModule->IsExternC = IsExternC;
  4384. CurrentModule->InferSubmodules = InferSubmodules;
  4385. CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
  4386. CurrentModule->InferExportWildcard = InferExportWildcard;
  4387. CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
  4388. CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
  4389. if (DeserializationListener)
  4390. DeserializationListener->ModuleRead(GlobalID, CurrentModule);
  4391. SubmodulesLoaded[GlobalIndex] = CurrentModule;
  4392. // Clear out data that will be replaced by what is in the module file.
  4393. CurrentModule->LinkLibraries.clear();
  4394. CurrentModule->ConfigMacros.clear();
  4395. CurrentModule->UnresolvedConflicts.clear();
  4396. CurrentModule->Conflicts.clear();
  4397. // The module is available unless it's missing a requirement; relevant
  4398. // requirements will be (re-)added by SUBMODULE_REQUIRES records.
  4399. // Missing headers that were present when the module was built do not
  4400. // make it unavailable -- if we got this far, this must be an explicitly
  4401. // imported module file.
  4402. CurrentModule->Requirements.clear();
  4403. CurrentModule->MissingHeaders.clear();
  4404. CurrentModule->IsMissingRequirement =
  4405. ParentModule && ParentModule->IsMissingRequirement;
  4406. CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
  4407. break;
  4408. }
  4409. case SUBMODULE_UMBRELLA_HEADER: {
  4410. std::string Filename = Blob;
  4411. ResolveImportedPath(F, Filename);
  4412. if (auto *Umbrella = PP.getFileManager().getFile(Filename)) {
  4413. if (!CurrentModule->getUmbrellaHeader())
  4414. ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob);
  4415. else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) {
  4416. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  4417. Error("mismatched umbrella headers in submodule");
  4418. return OutOfDate;
  4419. }
  4420. }
  4421. break;
  4422. }
  4423. case SUBMODULE_HEADER:
  4424. case SUBMODULE_EXCLUDED_HEADER:
  4425. case SUBMODULE_PRIVATE_HEADER:
  4426. // We lazily associate headers with their modules via the HeaderInfo table.
  4427. // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
  4428. // of complete filenames or remove it entirely.
  4429. break;
  4430. case SUBMODULE_TEXTUAL_HEADER:
  4431. case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
  4432. // FIXME: Textual headers are not marked in the HeaderInfo table. Load
  4433. // them here.
  4434. break;
  4435. case SUBMODULE_TOPHEADER:
  4436. CurrentModule->addTopHeaderFilename(Blob);
  4437. break;
  4438. case SUBMODULE_UMBRELLA_DIR: {
  4439. std::string Dirname = Blob;
  4440. ResolveImportedPath(F, Dirname);
  4441. if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) {
  4442. if (!CurrentModule->getUmbrellaDir())
  4443. ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob);
  4444. else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) {
  4445. if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
  4446. Error("mismatched umbrella directories in submodule");
  4447. return OutOfDate;
  4448. }
  4449. }
  4450. break;
  4451. }
  4452. case SUBMODULE_METADATA: {
  4453. F.BaseSubmoduleID = getTotalNumSubmodules();
  4454. F.LocalNumSubmodules = Record[0];
  4455. unsigned LocalBaseSubmoduleID = Record[1];
  4456. if (F.LocalNumSubmodules > 0) {
  4457. // Introduce the global -> local mapping for submodules within this
  4458. // module.
  4459. GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
  4460. // Introduce the local -> global mapping for submodules within this
  4461. // module.
  4462. F.SubmoduleRemap.insertOrReplace(
  4463. std::make_pair(LocalBaseSubmoduleID,
  4464. F.BaseSubmoduleID - LocalBaseSubmoduleID));
  4465. SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
  4466. }
  4467. break;
  4468. }
  4469. case SUBMODULE_IMPORTS:
  4470. for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
  4471. UnresolvedModuleRef Unresolved;
  4472. Unresolved.File = &F;
  4473. Unresolved.Mod = CurrentModule;
  4474. Unresolved.ID = Record[Idx];
  4475. Unresolved.Kind = UnresolvedModuleRef::Import;
  4476. Unresolved.IsWildcard = false;
  4477. UnresolvedModuleRefs.push_back(Unresolved);
  4478. }
  4479. break;
  4480. case SUBMODULE_EXPORTS:
  4481. for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
  4482. UnresolvedModuleRef Unresolved;
  4483. Unresolved.File = &F;
  4484. Unresolved.Mod = CurrentModule;
  4485. Unresolved.ID = Record[Idx];
  4486. Unresolved.Kind = UnresolvedModuleRef::Export;
  4487. Unresolved.IsWildcard = Record[Idx + 1];
  4488. UnresolvedModuleRefs.push_back(Unresolved);
  4489. }
  4490. // Once we've loaded the set of exports, there's no reason to keep
  4491. // the parsed, unresolved exports around.
  4492. CurrentModule->UnresolvedExports.clear();
  4493. break;
  4494. case SUBMODULE_REQUIRES:
  4495. CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
  4496. PP.getTargetInfo());
  4497. break;
  4498. case SUBMODULE_LINK_LIBRARY:
  4499. ModMap.resolveLinkAsDependencies(CurrentModule);
  4500. CurrentModule->LinkLibraries.push_back(
  4501. Module::LinkLibrary(Blob, Record[0]));
  4502. break;
  4503. case SUBMODULE_CONFIG_MACRO:
  4504. CurrentModule->ConfigMacros.push_back(Blob.str());
  4505. break;
  4506. case SUBMODULE_CONFLICT: {
  4507. UnresolvedModuleRef Unresolved;
  4508. Unresolved.File = &F;
  4509. Unresolved.Mod = CurrentModule;
  4510. Unresolved.ID = Record[0];
  4511. Unresolved.Kind = UnresolvedModuleRef::Conflict;
  4512. Unresolved.IsWildcard = false;
  4513. Unresolved.String = Blob;
  4514. UnresolvedModuleRefs.push_back(Unresolved);
  4515. break;
  4516. }
  4517. case SUBMODULE_INITIALIZERS: {
  4518. if (!ContextObj)
  4519. break;
  4520. SmallVector<uint32_t, 16> Inits;
  4521. for (auto &ID : Record)
  4522. Inits.push_back(getGlobalDeclID(F, ID));
  4523. ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
  4524. break;
  4525. }
  4526. case SUBMODULE_EXPORT_AS:
  4527. CurrentModule->ExportAsModule = Blob.str();
  4528. ModMap.addLinkAsDependency(CurrentModule);
  4529. break;
  4530. }
  4531. }
  4532. }
  4533. /// Parse the record that corresponds to a LangOptions data
  4534. /// structure.
  4535. ///
  4536. /// This routine parses the language options from the AST file and then gives
  4537. /// them to the AST listener if one is set.
  4538. ///
  4539. /// \returns true if the listener deems the file unacceptable, false otherwise.
  4540. bool ASTReader::ParseLanguageOptions(const RecordData &Record,
  4541. bool Complain,
  4542. ASTReaderListener &Listener,
  4543. bool AllowCompatibleDifferences) {
  4544. LangOptions LangOpts;
  4545. unsigned Idx = 0;
  4546. #define LANGOPT(Name, Bits, Default, Description) \
  4547. LangOpts.Name = Record[Idx++];
  4548. #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
  4549. LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
  4550. #include "clang/Basic/LangOptions.def"
  4551. #define SANITIZER(NAME, ID) \
  4552. LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
  4553. #include "clang/Basic/Sanitizers.def"
  4554. for (unsigned N = Record[Idx++]; N; --N)
  4555. LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
  4556. ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
  4557. VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
  4558. LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
  4559. LangOpts.CurrentModule = ReadString(Record, Idx);
  4560. // Comment options.
  4561. for (unsigned N = Record[Idx++]; N; --N) {
  4562. LangOpts.CommentOpts.BlockCommandNames.push_back(
  4563. ReadString(Record, Idx));
  4564. }
  4565. LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
  4566. // OpenMP offloading options.
  4567. for (unsigned N = Record[Idx++]; N; --N) {
  4568. LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
  4569. }
  4570. LangOpts.OMPHostIRFile = ReadString(Record, Idx);
  4571. return Listener.ReadLanguageOptions(LangOpts, Complain,
  4572. AllowCompatibleDifferences);
  4573. }
  4574. bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
  4575. ASTReaderListener &Listener,
  4576. bool AllowCompatibleDifferences) {
  4577. unsigned Idx = 0;
  4578. TargetOptions TargetOpts;
  4579. TargetOpts.Triple = ReadString(Record, Idx);
  4580. TargetOpts.CPU = ReadString(Record, Idx);
  4581. TargetOpts.ABI = ReadString(Record, Idx);
  4582. for (unsigned N = Record[Idx++]; N; --N) {
  4583. TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
  4584. }
  4585. for (unsigned N = Record[Idx++]; N; --N) {
  4586. TargetOpts.Features.push_back(ReadString(Record, Idx));
  4587. }
  4588. return Listener.ReadTargetOptions(TargetOpts, Complain,
  4589. AllowCompatibleDifferences);
  4590. }
  4591. bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
  4592. ASTReaderListener &Listener) {
  4593. IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
  4594. unsigned Idx = 0;
  4595. #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
  4596. #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
  4597. DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
  4598. #include "clang/Basic/DiagnosticOptions.def"
  4599. for (unsigned N = Record[Idx++]; N; --N)
  4600. DiagOpts->Warnings.push_back(ReadString(Record, Idx));
  4601. for (unsigned N = Record[Idx++]; N; --N)
  4602. DiagOpts->Remarks.push_back(ReadString(Record, Idx));
  4603. return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
  4604. }
  4605. bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
  4606. ASTReaderListener &Listener) {
  4607. FileSystemOptions FSOpts;
  4608. unsigned Idx = 0;
  4609. FSOpts.WorkingDir = ReadString(Record, Idx);
  4610. return Listener.ReadFileSystemOptions(FSOpts, Complain);
  4611. }
  4612. bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
  4613. bool Complain,
  4614. ASTReaderListener &Listener) {
  4615. HeaderSearchOptions HSOpts;
  4616. unsigned Idx = 0;
  4617. HSOpts.Sysroot = ReadString(Record, Idx);
  4618. // Include entries.
  4619. for (unsigned N = Record[Idx++]; N; --N) {
  4620. std::string Path = ReadString(Record, Idx);
  4621. frontend::IncludeDirGroup Group
  4622. = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
  4623. bool IsFramework = Record[Idx++];
  4624. bool IgnoreSysRoot = Record[Idx++];
  4625. HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
  4626. IgnoreSysRoot);
  4627. }
  4628. // System header prefixes.
  4629. for (unsigned N = Record[Idx++]; N; --N) {
  4630. std::string Prefix = ReadString(Record, Idx);
  4631. bool IsSystemHeader = Record[Idx++];
  4632. HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
  4633. }
  4634. HSOpts.ResourceDir = ReadString(Record, Idx);
  4635. HSOpts.ModuleCachePath = ReadString(Record, Idx);
  4636. HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
  4637. HSOpts.DisableModuleHash = Record[Idx++];
  4638. HSOpts.ImplicitModuleMaps = Record[Idx++];
  4639. HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
  4640. HSOpts.UseBuiltinIncludes = Record[Idx++];
  4641. HSOpts.UseStandardSystemIncludes = Record[Idx++];
  4642. HSOpts.UseStandardCXXIncludes = Record[Idx++];
  4643. HSOpts.UseLibcxx = Record[Idx++];
  4644. std::string SpecificModuleCachePath = ReadString(Record, Idx);
  4645. return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
  4646. Complain);
  4647. }
  4648. bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
  4649. bool Complain,
  4650. ASTReaderListener &Listener,
  4651. std::string &SuggestedPredefines) {
  4652. PreprocessorOptions PPOpts;
  4653. unsigned Idx = 0;
  4654. // Macro definitions/undefs
  4655. for (unsigned N = Record[Idx++]; N; --N) {
  4656. std::string Macro = ReadString(Record, Idx);
  4657. bool IsUndef = Record[Idx++];
  4658. PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
  4659. }
  4660. // Includes
  4661. for (unsigned N = Record[Idx++]; N; --N) {
  4662. PPOpts.Includes.push_back(ReadString(Record, Idx));
  4663. }
  4664. // Macro Includes
  4665. for (unsigned N = Record[Idx++]; N; --N) {
  4666. PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
  4667. }
  4668. PPOpts.UsePredefines = Record[Idx++];
  4669. PPOpts.DetailedRecord = Record[Idx++];
  4670. PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
  4671. PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
  4672. PPOpts.ObjCXXARCStandardLibrary =
  4673. static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
  4674. SuggestedPredefines.clear();
  4675. return Listener.ReadPreprocessorOptions(PPOpts, Complain,
  4676. SuggestedPredefines);
  4677. }
  4678. std::pair<ModuleFile *, unsigned>
  4679. ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
  4680. GlobalPreprocessedEntityMapType::iterator
  4681. I = GlobalPreprocessedEntityMap.find(GlobalIndex);
  4682. assert(I != GlobalPreprocessedEntityMap.end() &&
  4683. "Corrupted global preprocessed entity map");
  4684. ModuleFile *M = I->second;
  4685. unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
  4686. return std::make_pair(M, LocalIndex);
  4687. }
  4688. llvm::iterator_range<PreprocessingRecord::iterator>
  4689. ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
  4690. if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
  4691. return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
  4692. Mod.NumPreprocessedEntities);
  4693. return llvm::make_range(PreprocessingRecord::iterator(),
  4694. PreprocessingRecord::iterator());
  4695. }
  4696. llvm::iterator_range<ASTReader::ModuleDeclIterator>
  4697. ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
  4698. return llvm::make_range(
  4699. ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
  4700. ModuleDeclIterator(this, &Mod,
  4701. Mod.FileSortedDecls + Mod.NumFileSortedDecls));
  4702. }
  4703. SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
  4704. auto I = GlobalSkippedRangeMap.find(GlobalIndex);
  4705. assert(I != GlobalSkippedRangeMap.end() &&
  4706. "Corrupted global skipped range map");
  4707. ModuleFile *M = I->second;
  4708. unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
  4709. assert(LocalIndex < M->NumPreprocessedSkippedRanges);
  4710. PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
  4711. SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
  4712. TranslateSourceLocation(*M, RawRange.getEnd()));
  4713. assert(Range.isValid());
  4714. return Range;
  4715. }
  4716. PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
  4717. PreprocessedEntityID PPID = Index+1;
  4718. std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
  4719. ModuleFile &M = *PPInfo.first;
  4720. unsigned LocalIndex = PPInfo.second;
  4721. const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
  4722. if (!PP.getPreprocessingRecord()) {
  4723. Error("no preprocessing record");
  4724. return nullptr;
  4725. }
  4726. SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
  4727. M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
  4728. llvm::BitstreamEntry Entry =
  4729. M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
  4730. if (Entry.Kind != llvm::BitstreamEntry::Record)
  4731. return nullptr;
  4732. // Read the record.
  4733. SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
  4734. TranslateSourceLocation(M, PPOffs.getEnd()));
  4735. PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
  4736. StringRef Blob;
  4737. RecordData Record;
  4738. PreprocessorDetailRecordTypes RecType =
  4739. (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
  4740. Entry.ID, Record, &Blob);
  4741. switch (RecType) {
  4742. case PPD_MACRO_EXPANSION: {
  4743. bool isBuiltin = Record[0];
  4744. IdentifierInfo *Name = nullptr;
  4745. MacroDefinitionRecord *Def = nullptr;
  4746. if (isBuiltin)
  4747. Name = getLocalIdentifier(M, Record[1]);
  4748. else {
  4749. PreprocessedEntityID GlobalID =
  4750. getGlobalPreprocessedEntityID(M, Record[1]);
  4751. Def = cast<MacroDefinitionRecord>(
  4752. PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
  4753. }
  4754. MacroExpansion *ME;
  4755. if (isBuiltin)
  4756. ME = new (PPRec) MacroExpansion(Name, Range);
  4757. else
  4758. ME = new (PPRec) MacroExpansion(Def, Range);
  4759. return ME;
  4760. }
  4761. case PPD_MACRO_DEFINITION: {
  4762. // Decode the identifier info and then check again; if the macro is
  4763. // still defined and associated with the identifier,
  4764. IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
  4765. MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
  4766. if (DeserializationListener)
  4767. DeserializationListener->MacroDefinitionRead(PPID, MD);
  4768. return MD;
  4769. }
  4770. case PPD_INCLUSION_DIRECTIVE: {
  4771. const char *FullFileNameStart = Blob.data() + Record[0];
  4772. StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
  4773. const FileEntry *File = nullptr;
  4774. if (!FullFileName.empty())
  4775. File = PP.getFileManager().getFile(FullFileName);
  4776. // FIXME: Stable encoding
  4777. InclusionDirective::InclusionKind Kind
  4778. = static_cast<InclusionDirective::InclusionKind>(Record[2]);
  4779. InclusionDirective *ID
  4780. = new (PPRec) InclusionDirective(PPRec, Kind,
  4781. StringRef(Blob.data(), Record[0]),
  4782. Record[1], Record[3],
  4783. File,
  4784. Range);
  4785. return ID;
  4786. }
  4787. }
  4788. llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
  4789. }
  4790. /// Find the next module that contains entities and return the ID
  4791. /// of the first entry.
  4792. ///
  4793. /// \param SLocMapI points at a chunk of a module that contains no
  4794. /// preprocessed entities or the entities it contains are not the ones we are
  4795. /// looking for.
  4796. PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
  4797. GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
  4798. ++SLocMapI;
  4799. for (GlobalSLocOffsetMapType::const_iterator
  4800. EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
  4801. ModuleFile &M = *SLocMapI->second;
  4802. if (M.NumPreprocessedEntities)
  4803. return M.BasePreprocessedEntityID;
  4804. }
  4805. return getTotalNumPreprocessedEntities();
  4806. }
  4807. namespace {
  4808. struct PPEntityComp {
  4809. const ASTReader &Reader;
  4810. ModuleFile &M;
  4811. PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
  4812. bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
  4813. SourceLocation LHS = getLoc(L);
  4814. SourceLocation RHS = getLoc(R);
  4815. return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
  4816. }
  4817. bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
  4818. SourceLocation LHS = getLoc(L);
  4819. return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
  4820. }
  4821. bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
  4822. SourceLocation RHS = getLoc(R);
  4823. return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
  4824. }
  4825. SourceLocation getLoc(const PPEntityOffset &PPE) const {
  4826. return Reader.TranslateSourceLocation(M, PPE.getBegin());
  4827. }
  4828. };
  4829. } // namespace
  4830. PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
  4831. bool EndsAfter) const {
  4832. if (SourceMgr.isLocalSourceLocation(Loc))
  4833. return getTotalNumPreprocessedEntities();
  4834. GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
  4835. SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
  4836. assert(SLocMapI != GlobalSLocOffsetMap.end() &&
  4837. "Corrupted global sloc offset map");
  4838. if (SLocMapI->second->NumPreprocessedEntities == 0)
  4839. return findNextPreprocessedEntity(SLocMapI);
  4840. ModuleFile &M = *SLocMapI->second;
  4841. using pp_iterator = const PPEntityOffset *;
  4842. pp_iterator pp_begin = M.PreprocessedEntityOffsets;
  4843. pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
  4844. size_t Count = M.NumPreprocessedEntities;
  4845. size_t Half;
  4846. pp_iterator First = pp_begin;
  4847. pp_iterator PPI;
  4848. if (EndsAfter) {
  4849. PPI = std::upper_bound(pp_begin, pp_end, Loc,
  4850. PPEntityComp(*this, M));
  4851. } else {
  4852. // Do a binary search manually instead of using std::lower_bound because
  4853. // The end locations of entities may be unordered (when a macro expansion
  4854. // is inside another macro argument), but for this case it is not important
  4855. // whether we get the first macro expansion or its containing macro.
  4856. while (Count > 0) {
  4857. Half = Count / 2;
  4858. PPI = First;
  4859. std::advance(PPI, Half);
  4860. if (SourceMgr.isBeforeInTranslationUnit(
  4861. TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
  4862. First = PPI;
  4863. ++First;
  4864. Count = Count - Half - 1;
  4865. } else
  4866. Count = Half;
  4867. }
  4868. }
  4869. if (PPI == pp_end)
  4870. return findNextPreprocessedEntity(SLocMapI);
  4871. return M.BasePreprocessedEntityID + (PPI - pp_begin);
  4872. }
  4873. /// Returns a pair of [Begin, End) indices of preallocated
  4874. /// preprocessed entities that \arg Range encompasses.
  4875. std::pair<unsigned, unsigned>
  4876. ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
  4877. if (Range.isInvalid())
  4878. return std::make_pair(0,0);
  4879. assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
  4880. PreprocessedEntityID BeginID =
  4881. findPreprocessedEntity(Range.getBegin(), false);
  4882. PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
  4883. return std::make_pair(BeginID, EndID);
  4884. }
  4885. /// Optionally returns true or false if the preallocated preprocessed
  4886. /// entity with index \arg Index came from file \arg FID.
  4887. Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
  4888. FileID FID) {
  4889. if (FID.isInvalid())
  4890. return false;
  4891. std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
  4892. ModuleFile &M = *PPInfo.first;
  4893. unsigned LocalIndex = PPInfo.second;
  4894. const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
  4895. SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
  4896. if (Loc.isInvalid())
  4897. return false;
  4898. if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
  4899. return true;
  4900. else
  4901. return false;
  4902. }
  4903. namespace {
  4904. /// Visitor used to search for information about a header file.
  4905. class HeaderFileInfoVisitor {
  4906. const FileEntry *FE;
  4907. Optional<HeaderFileInfo> HFI;
  4908. public:
  4909. explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
  4910. bool operator()(ModuleFile &M) {
  4911. HeaderFileInfoLookupTable *Table
  4912. = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
  4913. if (!Table)
  4914. return false;
  4915. // Look in the on-disk hash table for an entry for this file name.
  4916. HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
  4917. if (Pos == Table->end())
  4918. return false;
  4919. HFI = *Pos;
  4920. return true;
  4921. }
  4922. Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
  4923. };
  4924. } // namespace
  4925. HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
  4926. HeaderFileInfoVisitor Visitor(FE);
  4927. ModuleMgr.visit(Visitor);
  4928. if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
  4929. return *HFI;
  4930. return HeaderFileInfo();
  4931. }
  4932. void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
  4933. using DiagState = DiagnosticsEngine::DiagState;
  4934. SmallVector<DiagState *, 32> DiagStates;
  4935. for (ModuleFile &F : ModuleMgr) {
  4936. unsigned Idx = 0;
  4937. auto &Record = F.PragmaDiagMappings;
  4938. if (Record.empty())
  4939. continue;
  4940. DiagStates.clear();
  4941. auto ReadDiagState =
  4942. [&](const DiagState &BasedOn, SourceLocation Loc,
  4943. bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
  4944. unsigned BackrefID = Record[Idx++];
  4945. if (BackrefID != 0)
  4946. return DiagStates[BackrefID - 1];
  4947. // A new DiagState was created here.
  4948. Diag.DiagStates.push_back(BasedOn);
  4949. DiagState *NewState = &Diag.DiagStates.back();
  4950. DiagStates.push_back(NewState);
  4951. unsigned Size = Record[Idx++];
  4952. assert(Idx + Size * 2 <= Record.size() &&
  4953. "Invalid data, not enough diag/map pairs");
  4954. while (Size--) {
  4955. unsigned DiagID = Record[Idx++];
  4956. DiagnosticMapping NewMapping =
  4957. DiagnosticMapping::deserialize(Record[Idx++]);
  4958. if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
  4959. continue;
  4960. DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
  4961. // If this mapping was specified as a warning but the severity was
  4962. // upgraded due to diagnostic settings, simulate the current diagnostic
  4963. // settings (and use a warning).
  4964. if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
  4965. NewMapping.setSeverity(diag::Severity::Warning);
  4966. NewMapping.setUpgradedFromWarning(false);
  4967. }
  4968. Mapping = NewMapping;
  4969. }
  4970. return NewState;
  4971. };
  4972. // Read the first state.
  4973. DiagState *FirstState;
  4974. if (F.Kind == MK_ImplicitModule) {
  4975. // Implicitly-built modules are reused with different diagnostic
  4976. // settings. Use the initial diagnostic state from Diag to simulate this
  4977. // compilation's diagnostic settings.
  4978. FirstState = Diag.DiagStatesByLoc.FirstDiagState;
  4979. DiagStates.push_back(FirstState);
  4980. // Skip the initial diagnostic state from the serialized module.
  4981. assert(Record[1] == 0 &&
  4982. "Invalid data, unexpected backref in initial state");
  4983. Idx = 3 + Record[2] * 2;
  4984. assert(Idx < Record.size() &&
  4985. "Invalid data, not enough state change pairs in initial state");
  4986. } else if (F.isModule()) {
  4987. // For an explicit module, preserve the flags from the module build
  4988. // command line (-w, -Weverything, -Werror, ...) along with any explicit
  4989. // -Wblah flags.
  4990. unsigned Flags = Record[Idx++];
  4991. DiagState Initial;
  4992. Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
  4993. Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
  4994. Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
  4995. Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
  4996. Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
  4997. Initial.ExtBehavior = (diag::Severity)Flags;
  4998. FirstState = ReadDiagState(Initial, SourceLocation(), true);
  4999. assert(F.OriginalSourceFileID.isValid());
  5000. // Set up the root buffer of the module to start with the initial
  5001. // diagnostic state of the module itself, to cover files that contain no
  5002. // explicit transitions (for which we did not serialize anything).
  5003. Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
  5004. .StateTransitions.push_back({FirstState, 0});
  5005. } else {
  5006. // For prefix ASTs, start with whatever the user configured on the
  5007. // command line.
  5008. Idx++; // Skip flags.
  5009. FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
  5010. SourceLocation(), false);
  5011. }
  5012. // Read the state transitions.
  5013. unsigned NumLocations = Record[Idx++];
  5014. while (NumLocations--) {
  5015. assert(Idx < Record.size() &&
  5016. "Invalid data, missing pragma diagnostic states");
  5017. SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
  5018. auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
  5019. assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
  5020. assert(IDAndOffset.second == 0 && "not a start location for a FileID");
  5021. unsigned Transitions = Record[Idx++];
  5022. // Note that we don't need to set up Parent/ParentOffset here, because
  5023. // we won't be changing the diagnostic state within imported FileIDs
  5024. // (other than perhaps appending to the main source file, which has no
  5025. // parent).
  5026. auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
  5027. F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
  5028. for (unsigned I = 0; I != Transitions; ++I) {
  5029. unsigned Offset = Record[Idx++];
  5030. auto *State =
  5031. ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
  5032. F.StateTransitions.push_back({State, Offset});
  5033. }
  5034. }
  5035. // Read the final state.
  5036. assert(Idx < Record.size() &&
  5037. "Invalid data, missing final pragma diagnostic state");
  5038. SourceLocation CurStateLoc =
  5039. ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
  5040. auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
  5041. if (!F.isModule()) {
  5042. Diag.DiagStatesByLoc.CurDiagState = CurState;
  5043. Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
  5044. // Preserve the property that the imaginary root file describes the
  5045. // current state.
  5046. FileID NullFile;
  5047. auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
  5048. if (T.empty())
  5049. T.push_back({CurState, 0});
  5050. else
  5051. T[0].State = CurState;
  5052. }
  5053. // Don't try to read these mappings again.
  5054. Record.clear();
  5055. }
  5056. }
  5057. /// Get the correct cursor and offset for loading a type.
  5058. ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
  5059. GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
  5060. assert(I != GlobalTypeMap.end() && "Corrupted global type map");
  5061. ModuleFile *M = I->second;
  5062. return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
  5063. }
  5064. /// Read and return the type with the given index..
  5065. ///
  5066. /// The index is the type ID, shifted and minus the number of predefs. This
  5067. /// routine actually reads the record corresponding to the type at the given
  5068. /// location. It is a helper routine for GetType, which deals with reading type
  5069. /// IDs.
  5070. QualType ASTReader::readTypeRecord(unsigned Index) {
  5071. assert(ContextObj && "reading type with no AST context");
  5072. ASTContext &Context = *ContextObj;
  5073. RecordLocation Loc = TypeCursorForIndex(Index);
  5074. BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
  5075. // Keep track of where we are in the stream, then jump back there
  5076. // after reading this type.
  5077. SavedStreamPosition SavedPosition(DeclsCursor);
  5078. ReadingKindTracker ReadingKind(Read_Type, *this);
  5079. // Note that we are loading a type record.
  5080. Deserializing AType(this);
  5081. unsigned Idx = 0;
  5082. DeclsCursor.JumpToBit(Loc.Offset);
  5083. RecordData Record;
  5084. unsigned Code = DeclsCursor.ReadCode();
  5085. switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
  5086. case TYPE_EXT_QUAL: {
  5087. if (Record.size() != 2) {
  5088. Error("Incorrect encoding of extended qualifier type");
  5089. return QualType();
  5090. }
  5091. QualType Base = readType(*Loc.F, Record, Idx);
  5092. Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
  5093. return Context.getQualifiedType(Base, Quals);
  5094. }
  5095. case TYPE_COMPLEX: {
  5096. if (Record.size() != 1) {
  5097. Error("Incorrect encoding of complex type");
  5098. return QualType();
  5099. }
  5100. QualType ElemType = readType(*Loc.F, Record, Idx);
  5101. return Context.getComplexType(ElemType);
  5102. }
  5103. case TYPE_POINTER: {
  5104. if (Record.size() != 1) {
  5105. Error("Incorrect encoding of pointer type");
  5106. return QualType();
  5107. }
  5108. QualType PointeeType = readType(*Loc.F, Record, Idx);
  5109. return Context.getPointerType(PointeeType);
  5110. }
  5111. case TYPE_DECAYED: {
  5112. if (Record.size() != 1) {
  5113. Error("Incorrect encoding of decayed type");
  5114. return QualType();
  5115. }
  5116. QualType OriginalType = readType(*Loc.F, Record, Idx);
  5117. QualType DT = Context.getAdjustedParameterType(OriginalType);
  5118. if (!isa<DecayedType>(DT))
  5119. Error("Decayed type does not decay");
  5120. return DT;
  5121. }
  5122. case TYPE_ADJUSTED: {
  5123. if (Record.size() != 2) {
  5124. Error("Incorrect encoding of adjusted type");
  5125. return QualType();
  5126. }
  5127. QualType OriginalTy = readType(*Loc.F, Record, Idx);
  5128. QualType AdjustedTy = readType(*Loc.F, Record, Idx);
  5129. return Context.getAdjustedType(OriginalTy, AdjustedTy);
  5130. }
  5131. case TYPE_BLOCK_POINTER: {
  5132. if (Record.size() != 1) {
  5133. Error("Incorrect encoding of block pointer type");
  5134. return QualType();
  5135. }
  5136. QualType PointeeType = readType(*Loc.F, Record, Idx);
  5137. return Context.getBlockPointerType(PointeeType);
  5138. }
  5139. case TYPE_LVALUE_REFERENCE: {
  5140. if (Record.size() != 2) {
  5141. Error("Incorrect encoding of lvalue reference type");
  5142. return QualType();
  5143. }
  5144. QualType PointeeType = readType(*Loc.F, Record, Idx);
  5145. return Context.getLValueReferenceType(PointeeType, Record[1]);
  5146. }
  5147. case TYPE_RVALUE_REFERENCE: {
  5148. if (Record.size() != 1) {
  5149. Error("Incorrect encoding of rvalue reference type");
  5150. return QualType();
  5151. }
  5152. QualType PointeeType = readType(*Loc.F, Record, Idx);
  5153. return Context.getRValueReferenceType(PointeeType);
  5154. }
  5155. case TYPE_MEMBER_POINTER: {
  5156. if (Record.size() != 2) {
  5157. Error("Incorrect encoding of member pointer type");
  5158. return QualType();
  5159. }
  5160. QualType PointeeType = readType(*Loc.F, Record, Idx);
  5161. QualType ClassType = readType(*Loc.F, Record, Idx);
  5162. if (PointeeType.isNull() || ClassType.isNull())
  5163. return QualType();
  5164. return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
  5165. }
  5166. case TYPE_CONSTANT_ARRAY: {
  5167. QualType ElementType = readType(*Loc.F, Record, Idx);
  5168. ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
  5169. unsigned IndexTypeQuals = Record[2];
  5170. unsigned Idx = 3;
  5171. llvm::APInt Size = ReadAPInt(Record, Idx);
  5172. return Context.getConstantArrayType(ElementType, Size,
  5173. ASM, IndexTypeQuals);
  5174. }
  5175. case TYPE_INCOMPLETE_ARRAY: {
  5176. QualType ElementType = readType(*Loc.F, Record, Idx);
  5177. ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
  5178. unsigned IndexTypeQuals = Record[2];
  5179. return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
  5180. }
  5181. case TYPE_VARIABLE_ARRAY: {
  5182. QualType ElementType = readType(*Loc.F, Record, Idx);
  5183. ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
  5184. unsigned IndexTypeQuals = Record[2];
  5185. SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
  5186. SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
  5187. return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
  5188. ASM, IndexTypeQuals,
  5189. SourceRange(LBLoc, RBLoc));
  5190. }
  5191. case TYPE_VECTOR: {
  5192. if (Record.size() != 3) {
  5193. Error("incorrect encoding of vector type in AST file");
  5194. return QualType();
  5195. }
  5196. QualType ElementType = readType(*Loc.F, Record, Idx);
  5197. unsigned NumElements = Record[1];
  5198. unsigned VecKind = Record[2];
  5199. return Context.getVectorType(ElementType, NumElements,
  5200. (VectorType::VectorKind)VecKind);
  5201. }
  5202. case TYPE_EXT_VECTOR: {
  5203. if (Record.size() != 3) {
  5204. Error("incorrect encoding of extended vector type in AST file");
  5205. return QualType();
  5206. }
  5207. QualType ElementType = readType(*Loc.F, Record, Idx);
  5208. unsigned NumElements = Record[1];
  5209. return Context.getExtVectorType(ElementType, NumElements);
  5210. }
  5211. case TYPE_FUNCTION_NO_PROTO: {
  5212. if (Record.size() != 8) {
  5213. Error("incorrect encoding of no-proto function type");
  5214. return QualType();
  5215. }
  5216. QualType ResultType = readType(*Loc.F, Record, Idx);
  5217. FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
  5218. (CallingConv)Record[4], Record[5], Record[6],
  5219. Record[7]);
  5220. return Context.getFunctionNoProtoType(ResultType, Info);
  5221. }
  5222. case TYPE_FUNCTION_PROTO: {
  5223. QualType ResultType = readType(*Loc.F, Record, Idx);
  5224. FunctionProtoType::ExtProtoInfo EPI;
  5225. EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
  5226. /*hasregparm*/ Record[2],
  5227. /*regparm*/ Record[3],
  5228. static_cast<CallingConv>(Record[4]),
  5229. /*produces*/ Record[5],
  5230. /*nocallersavedregs*/ Record[6],
  5231. /*nocfcheck*/ Record[7]);
  5232. unsigned Idx = 8;
  5233. EPI.Variadic = Record[Idx++];
  5234. EPI.HasTrailingReturn = Record[Idx++];
  5235. EPI.TypeQuals = Record[Idx++];
  5236. EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
  5237. SmallVector<QualType, 8> ExceptionStorage;
  5238. readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
  5239. unsigned NumParams = Record[Idx++];
  5240. SmallVector<QualType, 16> ParamTypes;
  5241. for (unsigned I = 0; I != NumParams; ++I)
  5242. ParamTypes.push_back(readType(*Loc.F, Record, Idx));
  5243. SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
  5244. if (Idx != Record.size()) {
  5245. for (unsigned I = 0; I != NumParams; ++I)
  5246. ExtParameterInfos.push_back(
  5247. FunctionProtoType::ExtParameterInfo
  5248. ::getFromOpaqueValue(Record[Idx++]));
  5249. EPI.ExtParameterInfos = ExtParameterInfos.data();
  5250. }
  5251. assert(Idx == Record.size());
  5252. return Context.getFunctionType(ResultType, ParamTypes, EPI);
  5253. }
  5254. case TYPE_UNRESOLVED_USING: {
  5255. unsigned Idx = 0;
  5256. return Context.getTypeDeclType(
  5257. ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
  5258. }
  5259. case TYPE_TYPEDEF: {
  5260. if (Record.size() != 2) {
  5261. Error("incorrect encoding of typedef type");
  5262. return QualType();
  5263. }
  5264. unsigned Idx = 0;
  5265. TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
  5266. QualType Canonical = readType(*Loc.F, Record, Idx);
  5267. if (!Canonical.isNull())
  5268. Canonical = Context.getCanonicalType(Canonical);
  5269. return Context.getTypedefType(Decl, Canonical);
  5270. }
  5271. case TYPE_TYPEOF_EXPR:
  5272. return Context.getTypeOfExprType(ReadExpr(*Loc.F));
  5273. case TYPE_TYPEOF: {
  5274. if (Record.size() != 1) {
  5275. Error("incorrect encoding of typeof(type) in AST file");
  5276. return QualType();
  5277. }
  5278. QualType UnderlyingType = readType(*Loc.F, Record, Idx);
  5279. return Context.getTypeOfType(UnderlyingType);
  5280. }
  5281. case TYPE_DECLTYPE: {
  5282. QualType UnderlyingType = readType(*Loc.F, Record, Idx);
  5283. return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
  5284. }
  5285. case TYPE_UNARY_TRANSFORM: {
  5286. QualType BaseType = readType(*Loc.F, Record, Idx);
  5287. QualType UnderlyingType = readType(*Loc.F, Record, Idx);
  5288. UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
  5289. return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
  5290. }
  5291. case TYPE_AUTO: {
  5292. QualType Deduced = readType(*Loc.F, Record, Idx);
  5293. AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
  5294. bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
  5295. return Context.getAutoType(Deduced, Keyword, IsDependent);
  5296. }
  5297. case TYPE_DEDUCED_TEMPLATE_SPECIALIZATION: {
  5298. TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
  5299. QualType Deduced = readType(*Loc.F, Record, Idx);
  5300. bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
  5301. return Context.getDeducedTemplateSpecializationType(Name, Deduced,
  5302. IsDependent);
  5303. }
  5304. case TYPE_RECORD: {
  5305. if (Record.size() != 2) {
  5306. Error("incorrect encoding of record type");
  5307. return QualType();
  5308. }
  5309. unsigned Idx = 0;
  5310. bool IsDependent = Record[Idx++];
  5311. RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
  5312. RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
  5313. QualType T = Context.getRecordType(RD);
  5314. const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
  5315. return T;
  5316. }
  5317. case TYPE_ENUM: {
  5318. if (Record.size() != 2) {
  5319. Error("incorrect encoding of enum type");
  5320. return QualType();
  5321. }
  5322. unsigned Idx = 0;
  5323. bool IsDependent = Record[Idx++];
  5324. QualType T
  5325. = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
  5326. const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
  5327. return T;
  5328. }
  5329. case TYPE_ATTRIBUTED: {
  5330. if (Record.size() != 3) {
  5331. Error("incorrect encoding of attributed type");
  5332. return QualType();
  5333. }
  5334. QualType modifiedType = readType(*Loc.F, Record, Idx);
  5335. QualType equivalentType = readType(*Loc.F, Record, Idx);
  5336. AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
  5337. return Context.getAttributedType(kind, modifiedType, equivalentType);
  5338. }
  5339. case TYPE_PAREN: {
  5340. if (Record.size() != 1) {
  5341. Error("incorrect encoding of paren type");
  5342. return QualType();
  5343. }
  5344. QualType InnerType = readType(*Loc.F, Record, Idx);
  5345. return Context.getParenType(InnerType);
  5346. }
  5347. case TYPE_PACK_EXPANSION: {
  5348. if (Record.size() != 2) {
  5349. Error("incorrect encoding of pack expansion type");
  5350. return QualType();
  5351. }
  5352. QualType Pattern = readType(*Loc.F, Record, Idx);
  5353. if (Pattern.isNull())
  5354. return QualType();
  5355. Optional<unsigned> NumExpansions;
  5356. if (Record[1])
  5357. NumExpansions = Record[1] - 1;
  5358. return Context.getPackExpansionType(Pattern, NumExpansions);
  5359. }
  5360. case TYPE_ELABORATED: {
  5361. unsigned Idx = 0;
  5362. ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
  5363. NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
  5364. QualType NamedType = readType(*Loc.F, Record, Idx);
  5365. TagDecl *OwnedTagDecl = ReadDeclAs<TagDecl>(*Loc.F, Record, Idx);
  5366. return Context.getElaboratedType(Keyword, NNS, NamedType, OwnedTagDecl);
  5367. }
  5368. case TYPE_OBJC_INTERFACE: {
  5369. unsigned Idx = 0;
  5370. ObjCInterfaceDecl *ItfD
  5371. = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
  5372. return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
  5373. }
  5374. case TYPE_OBJC_TYPE_PARAM: {
  5375. unsigned Idx = 0;
  5376. ObjCTypeParamDecl *Decl
  5377. = ReadDeclAs<ObjCTypeParamDecl>(*Loc.F, Record, Idx);
  5378. unsigned NumProtos = Record[Idx++];
  5379. SmallVector<ObjCProtocolDecl*, 4> Protos;
  5380. for (unsigned I = 0; I != NumProtos; ++I)
  5381. Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
  5382. return Context.getObjCTypeParamType(Decl, Protos);
  5383. }
  5384. case TYPE_OBJC_OBJECT: {
  5385. unsigned Idx = 0;
  5386. QualType Base = readType(*Loc.F, Record, Idx);
  5387. unsigned NumTypeArgs = Record[Idx++];
  5388. SmallVector<QualType, 4> TypeArgs;
  5389. for (unsigned I = 0; I != NumTypeArgs; ++I)
  5390. TypeArgs.push_back(readType(*Loc.F, Record, Idx));
  5391. unsigned NumProtos = Record[Idx++];
  5392. SmallVector<ObjCProtocolDecl*, 4> Protos;
  5393. for (unsigned I = 0; I != NumProtos; ++I)
  5394. Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
  5395. bool IsKindOf = Record[Idx++];
  5396. return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
  5397. }
  5398. case TYPE_OBJC_OBJECT_POINTER: {
  5399. unsigned Idx = 0;
  5400. QualType Pointee = readType(*Loc.F, Record, Idx);
  5401. return Context.getObjCObjectPointerType(Pointee);
  5402. }
  5403. case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
  5404. unsigned Idx = 0;
  5405. QualType Parm = readType(*Loc.F, Record, Idx);
  5406. QualType Replacement = readType(*Loc.F, Record, Idx);
  5407. return Context.getSubstTemplateTypeParmType(
  5408. cast<TemplateTypeParmType>(Parm),
  5409. Context.getCanonicalType(Replacement));
  5410. }
  5411. case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
  5412. unsigned Idx = 0;
  5413. QualType Parm = readType(*Loc.F, Record, Idx);
  5414. TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
  5415. return Context.getSubstTemplateTypeParmPackType(
  5416. cast<TemplateTypeParmType>(Parm),
  5417. ArgPack);
  5418. }
  5419. case TYPE_INJECTED_CLASS_NAME: {
  5420. CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
  5421. QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
  5422. // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
  5423. // for AST reading, too much interdependencies.
  5424. const Type *T = nullptr;
  5425. for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
  5426. if (const Type *Existing = DI->getTypeForDecl()) {
  5427. T = Existing;
  5428. break;
  5429. }
  5430. }
  5431. if (!T) {
  5432. T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
  5433. for (auto *DI = D; DI; DI = DI->getPreviousDecl())
  5434. DI->setTypeForDecl(T);
  5435. }
  5436. return QualType(T, 0);
  5437. }
  5438. case TYPE_TEMPLATE_TYPE_PARM: {
  5439. unsigned Idx = 0;
  5440. unsigned Depth = Record[Idx++];
  5441. unsigned Index = Record[Idx++];
  5442. bool Pack = Record[Idx++];
  5443. TemplateTypeParmDecl *D
  5444. = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
  5445. return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
  5446. }
  5447. case TYPE_DEPENDENT_NAME: {
  5448. unsigned Idx = 0;
  5449. ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
  5450. NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
  5451. const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
  5452. QualType Canon = readType(*Loc.F, Record, Idx);
  5453. if (!Canon.isNull())
  5454. Canon = Context.getCanonicalType(Canon);
  5455. return Context.getDependentNameType(Keyword, NNS, Name, Canon);
  5456. }
  5457. case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
  5458. unsigned Idx = 0;
  5459. ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
  5460. NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
  5461. const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
  5462. unsigned NumArgs = Record[Idx++];
  5463. SmallVector<TemplateArgument, 8> Args;
  5464. Args.reserve(NumArgs);
  5465. while (NumArgs--)
  5466. Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
  5467. return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
  5468. Args);
  5469. }
  5470. case TYPE_DEPENDENT_SIZED_ARRAY: {
  5471. unsigned Idx = 0;
  5472. // ArrayType
  5473. QualType ElementType = readType(*Loc.F, Record, Idx);
  5474. ArrayType::ArraySizeModifier ASM
  5475. = (ArrayType::ArraySizeModifier)Record[Idx++];
  5476. unsigned IndexTypeQuals = Record[Idx++];
  5477. // DependentSizedArrayType
  5478. Expr *NumElts = ReadExpr(*Loc.F);
  5479. SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
  5480. return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
  5481. IndexTypeQuals, Brackets);
  5482. }
  5483. case TYPE_TEMPLATE_SPECIALIZATION: {
  5484. unsigned Idx = 0;
  5485. bool IsDependent = Record[Idx++];
  5486. TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
  5487. SmallVector<TemplateArgument, 8> Args;
  5488. ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
  5489. QualType Underlying = readType(*Loc.F, Record, Idx);
  5490. QualType T;
  5491. if (Underlying.isNull())
  5492. T = Context.getCanonicalTemplateSpecializationType(Name, Args);
  5493. else
  5494. T = Context.getTemplateSpecializationType(Name, Args, Underlying);
  5495. const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
  5496. return T;
  5497. }
  5498. case TYPE_ATOMIC: {
  5499. if (Record.size() != 1) {
  5500. Error("Incorrect encoding of atomic type");
  5501. return QualType();
  5502. }
  5503. QualType ValueType = readType(*Loc.F, Record, Idx);
  5504. return Context.getAtomicType(ValueType);
  5505. }
  5506. case TYPE_PIPE: {
  5507. if (Record.size() != 2) {
  5508. Error("Incorrect encoding of pipe type");
  5509. return QualType();
  5510. }
  5511. // Reading the pipe element type.
  5512. QualType ElementType = readType(*Loc.F, Record, Idx);
  5513. unsigned ReadOnly = Record[1];
  5514. return Context.getPipeType(ElementType, ReadOnly);
  5515. }
  5516. case TYPE_DEPENDENT_SIZED_VECTOR: {
  5517. unsigned Idx = 0;
  5518. QualType ElementType = readType(*Loc.F, Record, Idx);
  5519. Expr *SizeExpr = ReadExpr(*Loc.F);
  5520. SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
  5521. unsigned VecKind = Record[Idx];
  5522. return Context.getDependentVectorType(ElementType, SizeExpr, AttrLoc,
  5523. (VectorType::VectorKind)VecKind);
  5524. }
  5525. case TYPE_DEPENDENT_SIZED_EXT_VECTOR: {
  5526. unsigned Idx = 0;
  5527. // DependentSizedExtVectorType
  5528. QualType ElementType = readType(*Loc.F, Record, Idx);
  5529. Expr *SizeExpr = ReadExpr(*Loc.F);
  5530. SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
  5531. return Context.getDependentSizedExtVectorType(ElementType, SizeExpr,
  5532. AttrLoc);
  5533. }
  5534. case TYPE_DEPENDENT_ADDRESS_SPACE: {
  5535. unsigned Idx = 0;
  5536. // DependentAddressSpaceType
  5537. QualType PointeeType = readType(*Loc.F, Record, Idx);
  5538. Expr *AddrSpaceExpr = ReadExpr(*Loc.F);
  5539. SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
  5540. return Context.getDependentAddressSpaceType(PointeeType, AddrSpaceExpr,
  5541. AttrLoc);
  5542. }
  5543. }
  5544. llvm_unreachable("Invalid TypeCode!");
  5545. }
  5546. void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
  5547. SmallVectorImpl<QualType> &Exceptions,
  5548. FunctionProtoType::ExceptionSpecInfo &ESI,
  5549. const RecordData &Record, unsigned &Idx) {
  5550. ExceptionSpecificationType EST =
  5551. static_cast<ExceptionSpecificationType>(Record[Idx++]);
  5552. ESI.Type = EST;
  5553. if (EST == EST_Dynamic) {
  5554. for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
  5555. Exceptions.push_back(readType(ModuleFile, Record, Idx));
  5556. ESI.Exceptions = Exceptions;
  5557. } else if (isComputedNoexcept(EST)) {
  5558. ESI.NoexceptExpr = ReadExpr(ModuleFile);
  5559. } else if (EST == EST_Uninstantiated) {
  5560. ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
  5561. ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
  5562. } else if (EST == EST_Unevaluated) {
  5563. ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
  5564. }
  5565. }
  5566. namespace clang {
  5567. class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
  5568. ModuleFile *F;
  5569. ASTReader *Reader;
  5570. const ASTReader::RecordData &Record;
  5571. unsigned &Idx;
  5572. SourceLocation ReadSourceLocation() {
  5573. return Reader->ReadSourceLocation(*F, Record, Idx);
  5574. }
  5575. TypeSourceInfo *GetTypeSourceInfo() {
  5576. return Reader->GetTypeSourceInfo(*F, Record, Idx);
  5577. }
  5578. NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
  5579. return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx);
  5580. }
  5581. Attr *ReadAttr() {
  5582. return Reader->ReadAttr(*F, Record, Idx);
  5583. }
  5584. public:
  5585. TypeLocReader(ModuleFile &F, ASTReader &Reader,
  5586. const ASTReader::RecordData &Record, unsigned &Idx)
  5587. : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
  5588. // We want compile-time assurance that we've enumerated all of
  5589. // these, so unfortunately we have to declare them first, then
  5590. // define them out-of-line.
  5591. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  5592. #define TYPELOC(CLASS, PARENT) \
  5593. void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
  5594. #include "clang/AST/TypeLocNodes.def"
  5595. void VisitFunctionTypeLoc(FunctionTypeLoc);
  5596. void VisitArrayTypeLoc(ArrayTypeLoc);
  5597. };
  5598. } // namespace clang
  5599. void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
  5600. // nothing to do
  5601. }
  5602. void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
  5603. TL.setBuiltinLoc(ReadSourceLocation());
  5604. if (TL.needsExtraLocalData()) {
  5605. TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
  5606. TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
  5607. TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
  5608. TL.setModeAttr(Record[Idx++]);
  5609. }
  5610. }
  5611. void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
  5612. TL.setNameLoc(ReadSourceLocation());
  5613. }
  5614. void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
  5615. TL.setStarLoc(ReadSourceLocation());
  5616. }
  5617. void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
  5618. // nothing to do
  5619. }
  5620. void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
  5621. // nothing to do
  5622. }
  5623. void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
  5624. TL.setCaretLoc(ReadSourceLocation());
  5625. }
  5626. void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
  5627. TL.setAmpLoc(ReadSourceLocation());
  5628. }
  5629. void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
  5630. TL.setAmpAmpLoc(ReadSourceLocation());
  5631. }
  5632. void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
  5633. TL.setStarLoc(ReadSourceLocation());
  5634. TL.setClassTInfo(GetTypeSourceInfo());
  5635. }
  5636. void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
  5637. TL.setLBracketLoc(ReadSourceLocation());
  5638. TL.setRBracketLoc(ReadSourceLocation());
  5639. if (Record[Idx++])
  5640. TL.setSizeExpr(Reader->ReadExpr(*F));
  5641. else
  5642. TL.setSizeExpr(nullptr);
  5643. }
  5644. void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
  5645. VisitArrayTypeLoc(TL);
  5646. }
  5647. void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
  5648. VisitArrayTypeLoc(TL);
  5649. }
  5650. void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
  5651. VisitArrayTypeLoc(TL);
  5652. }
  5653. void TypeLocReader::VisitDependentSizedArrayTypeLoc(
  5654. DependentSizedArrayTypeLoc TL) {
  5655. VisitArrayTypeLoc(TL);
  5656. }
  5657. void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
  5658. DependentAddressSpaceTypeLoc TL) {
  5659. TL.setAttrNameLoc(ReadSourceLocation());
  5660. SourceRange range;
  5661. range.setBegin(ReadSourceLocation());
  5662. range.setEnd(ReadSourceLocation());
  5663. TL.setAttrOperandParensRange(range);
  5664. TL.setAttrExprOperand(Reader->ReadExpr(*F));
  5665. }
  5666. void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
  5667. DependentSizedExtVectorTypeLoc TL) {
  5668. TL.setNameLoc(ReadSourceLocation());
  5669. }
  5670. void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
  5671. TL.setNameLoc(ReadSourceLocation());
  5672. }
  5673. void TypeLocReader::VisitDependentVectorTypeLoc(
  5674. DependentVectorTypeLoc TL) {
  5675. TL.setNameLoc(ReadSourceLocation());
  5676. }
  5677. void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
  5678. TL.setNameLoc(ReadSourceLocation());
  5679. }
  5680. void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
  5681. TL.setLocalRangeBegin(ReadSourceLocation());
  5682. TL.setLParenLoc(ReadSourceLocation());
  5683. TL.setRParenLoc(ReadSourceLocation());
  5684. TL.setExceptionSpecRange(SourceRange(Reader->ReadSourceLocation(*F, Record, Idx),
  5685. Reader->ReadSourceLocation(*F, Record, Idx)));
  5686. TL.setLocalRangeEnd(ReadSourceLocation());
  5687. for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
  5688. TL.setParam(i, Reader->ReadDeclAs<ParmVarDecl>(*F, Record, Idx));
  5689. }
  5690. }
  5691. void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
  5692. VisitFunctionTypeLoc(TL);
  5693. }
  5694. void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
  5695. VisitFunctionTypeLoc(TL);
  5696. }
  5697. void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
  5698. TL.setNameLoc(ReadSourceLocation());
  5699. }
  5700. void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
  5701. TL.setNameLoc(ReadSourceLocation());
  5702. }
  5703. void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
  5704. TL.setTypeofLoc(ReadSourceLocation());
  5705. TL.setLParenLoc(ReadSourceLocation());
  5706. TL.setRParenLoc(ReadSourceLocation());
  5707. }
  5708. void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
  5709. TL.setTypeofLoc(ReadSourceLocation());
  5710. TL.setLParenLoc(ReadSourceLocation());
  5711. TL.setRParenLoc(ReadSourceLocation());
  5712. TL.setUnderlyingTInfo(GetTypeSourceInfo());
  5713. }
  5714. void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
  5715. TL.setNameLoc(ReadSourceLocation());
  5716. }
  5717. void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
  5718. TL.setKWLoc(ReadSourceLocation());
  5719. TL.setLParenLoc(ReadSourceLocation());
  5720. TL.setRParenLoc(ReadSourceLocation());
  5721. TL.setUnderlyingTInfo(GetTypeSourceInfo());
  5722. }
  5723. void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
  5724. TL.setNameLoc(ReadSourceLocation());
  5725. }
  5726. void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
  5727. DeducedTemplateSpecializationTypeLoc TL) {
  5728. TL.setTemplateNameLoc(ReadSourceLocation());
  5729. }
  5730. void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
  5731. TL.setNameLoc(ReadSourceLocation());
  5732. }
  5733. void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
  5734. TL.setNameLoc(ReadSourceLocation());
  5735. }
  5736. void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
  5737. TL.setAttr(ReadAttr());
  5738. }
  5739. void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
  5740. TL.setNameLoc(ReadSourceLocation());
  5741. }
  5742. void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
  5743. SubstTemplateTypeParmTypeLoc TL) {
  5744. TL.setNameLoc(ReadSourceLocation());
  5745. }
  5746. void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
  5747. SubstTemplateTypeParmPackTypeLoc TL) {
  5748. TL.setNameLoc(ReadSourceLocation());
  5749. }
  5750. void TypeLocReader::VisitTemplateSpecializationTypeLoc(
  5751. TemplateSpecializationTypeLoc TL) {
  5752. TL.setTemplateKeywordLoc(ReadSourceLocation());
  5753. TL.setTemplateNameLoc(ReadSourceLocation());
  5754. TL.setLAngleLoc(ReadSourceLocation());
  5755. TL.setRAngleLoc(ReadSourceLocation());
  5756. for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
  5757. TL.setArgLocInfo(
  5758. i,
  5759. Reader->GetTemplateArgumentLocInfo(
  5760. *F, TL.getTypePtr()->getArg(i).getKind(), Record, Idx));
  5761. }
  5762. void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
  5763. TL.setLParenLoc(ReadSourceLocation());
  5764. TL.setRParenLoc(ReadSourceLocation());
  5765. }
  5766. void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
  5767. TL.setElaboratedKeywordLoc(ReadSourceLocation());
  5768. TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
  5769. }
  5770. void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
  5771. TL.setNameLoc(ReadSourceLocation());
  5772. }
  5773. void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
  5774. TL.setElaboratedKeywordLoc(ReadSourceLocation());
  5775. TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
  5776. TL.setNameLoc(ReadSourceLocation());
  5777. }
  5778. void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
  5779. DependentTemplateSpecializationTypeLoc TL) {
  5780. TL.setElaboratedKeywordLoc(ReadSourceLocation());
  5781. TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
  5782. TL.setTemplateKeywordLoc(ReadSourceLocation());
  5783. TL.setTemplateNameLoc(ReadSourceLocation());
  5784. TL.setLAngleLoc(ReadSourceLocation());
  5785. TL.setRAngleLoc(ReadSourceLocation());
  5786. for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
  5787. TL.setArgLocInfo(
  5788. I,
  5789. Reader->GetTemplateArgumentLocInfo(
  5790. *F, TL.getTypePtr()->getArg(I).getKind(), Record, Idx));
  5791. }
  5792. void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
  5793. TL.setEllipsisLoc(ReadSourceLocation());
  5794. }
  5795. void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
  5796. TL.setNameLoc(ReadSourceLocation());
  5797. }
  5798. void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
  5799. if (TL.getNumProtocols()) {
  5800. TL.setProtocolLAngleLoc(ReadSourceLocation());
  5801. TL.setProtocolRAngleLoc(ReadSourceLocation());
  5802. }
  5803. for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
  5804. TL.setProtocolLoc(i, ReadSourceLocation());
  5805. }
  5806. void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
  5807. TL.setHasBaseTypeAsWritten(Record[Idx++]);
  5808. TL.setTypeArgsLAngleLoc(ReadSourceLocation());
  5809. TL.setTypeArgsRAngleLoc(ReadSourceLocation());
  5810. for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
  5811. TL.setTypeArgTInfo(i, GetTypeSourceInfo());
  5812. TL.setProtocolLAngleLoc(ReadSourceLocation());
  5813. TL.setProtocolRAngleLoc(ReadSourceLocation());
  5814. for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
  5815. TL.setProtocolLoc(i, ReadSourceLocation());
  5816. }
  5817. void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
  5818. TL.setStarLoc(ReadSourceLocation());
  5819. }
  5820. void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
  5821. TL.setKWLoc(ReadSourceLocation());
  5822. TL.setLParenLoc(ReadSourceLocation());
  5823. TL.setRParenLoc(ReadSourceLocation());
  5824. }
  5825. void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
  5826. TL.setKWLoc(ReadSourceLocation());
  5827. }
  5828. void ASTReader::ReadTypeLoc(ModuleFile &F, const ASTReader::RecordData &Record,
  5829. unsigned &Idx, TypeLoc TL) {
  5830. TypeLocReader TLR(F, *this, Record, Idx);
  5831. for (; !TL.isNull(); TL = TL.getNextTypeLoc())
  5832. TLR.Visit(TL);
  5833. }
  5834. TypeSourceInfo *
  5835. ASTReader::GetTypeSourceInfo(ModuleFile &F, const ASTReader::RecordData &Record,
  5836. unsigned &Idx) {
  5837. QualType InfoTy = readType(F, Record, Idx);
  5838. if (InfoTy.isNull())
  5839. return nullptr;
  5840. TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
  5841. ReadTypeLoc(F, Record, Idx, TInfo->getTypeLoc());
  5842. return TInfo;
  5843. }
  5844. QualType ASTReader::GetType(TypeID ID) {
  5845. assert(ContextObj && "reading type with no AST context");
  5846. ASTContext &Context = *ContextObj;
  5847. unsigned FastQuals = ID & Qualifiers::FastMask;
  5848. unsigned Index = ID >> Qualifiers::FastWidth;
  5849. if (Index < NUM_PREDEF_TYPE_IDS) {
  5850. QualType T;
  5851. switch ((PredefinedTypeIDs)Index) {
  5852. case PREDEF_TYPE_NULL_ID:
  5853. return QualType();
  5854. case PREDEF_TYPE_VOID_ID:
  5855. T = Context.VoidTy;
  5856. break;
  5857. case PREDEF_TYPE_BOOL_ID:
  5858. T = Context.BoolTy;
  5859. break;
  5860. case PREDEF_TYPE_CHAR_U_ID:
  5861. case PREDEF_TYPE_CHAR_S_ID:
  5862. // FIXME: Check that the signedness of CharTy is correct!
  5863. T = Context.CharTy;
  5864. break;
  5865. case PREDEF_TYPE_UCHAR_ID:
  5866. T = Context.UnsignedCharTy;
  5867. break;
  5868. case PREDEF_TYPE_USHORT_ID:
  5869. T = Context.UnsignedShortTy;
  5870. break;
  5871. case PREDEF_TYPE_UINT_ID:
  5872. T = Context.UnsignedIntTy;
  5873. break;
  5874. case PREDEF_TYPE_ULONG_ID:
  5875. T = Context.UnsignedLongTy;
  5876. break;
  5877. case PREDEF_TYPE_ULONGLONG_ID:
  5878. T = Context.UnsignedLongLongTy;
  5879. break;
  5880. case PREDEF_TYPE_UINT128_ID:
  5881. T = Context.UnsignedInt128Ty;
  5882. break;
  5883. case PREDEF_TYPE_SCHAR_ID:
  5884. T = Context.SignedCharTy;
  5885. break;
  5886. case PREDEF_TYPE_WCHAR_ID:
  5887. T = Context.WCharTy;
  5888. break;
  5889. case PREDEF_TYPE_SHORT_ID:
  5890. T = Context.ShortTy;
  5891. break;
  5892. case PREDEF_TYPE_INT_ID:
  5893. T = Context.IntTy;
  5894. break;
  5895. case PREDEF_TYPE_LONG_ID:
  5896. T = Context.LongTy;
  5897. break;
  5898. case PREDEF_TYPE_LONGLONG_ID:
  5899. T = Context.LongLongTy;
  5900. break;
  5901. case PREDEF_TYPE_INT128_ID:
  5902. T = Context.Int128Ty;
  5903. break;
  5904. case PREDEF_TYPE_HALF_ID:
  5905. T = Context.HalfTy;
  5906. break;
  5907. case PREDEF_TYPE_FLOAT_ID:
  5908. T = Context.FloatTy;
  5909. break;
  5910. case PREDEF_TYPE_DOUBLE_ID:
  5911. T = Context.DoubleTy;
  5912. break;
  5913. case PREDEF_TYPE_LONGDOUBLE_ID:
  5914. T = Context.LongDoubleTy;
  5915. break;
  5916. case PREDEF_TYPE_SHORT_ACCUM_ID:
  5917. T = Context.ShortAccumTy;
  5918. break;
  5919. case PREDEF_TYPE_ACCUM_ID:
  5920. T = Context.AccumTy;
  5921. break;
  5922. case PREDEF_TYPE_LONG_ACCUM_ID:
  5923. T = Context.LongAccumTy;
  5924. break;
  5925. case PREDEF_TYPE_USHORT_ACCUM_ID:
  5926. T = Context.UnsignedShortAccumTy;
  5927. break;
  5928. case PREDEF_TYPE_UACCUM_ID:
  5929. T = Context.UnsignedAccumTy;
  5930. break;
  5931. case PREDEF_TYPE_ULONG_ACCUM_ID:
  5932. T = Context.UnsignedLongAccumTy;
  5933. break;
  5934. case PREDEF_TYPE_SHORT_FRACT_ID:
  5935. T = Context.ShortFractTy;
  5936. break;
  5937. case PREDEF_TYPE_FRACT_ID:
  5938. T = Context.FractTy;
  5939. break;
  5940. case PREDEF_TYPE_LONG_FRACT_ID:
  5941. T = Context.LongFractTy;
  5942. break;
  5943. case PREDEF_TYPE_USHORT_FRACT_ID:
  5944. T = Context.UnsignedShortFractTy;
  5945. break;
  5946. case PREDEF_TYPE_UFRACT_ID:
  5947. T = Context.UnsignedFractTy;
  5948. break;
  5949. case PREDEF_TYPE_ULONG_FRACT_ID:
  5950. T = Context.UnsignedLongFractTy;
  5951. break;
  5952. case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
  5953. T = Context.SatShortAccumTy;
  5954. break;
  5955. case PREDEF_TYPE_SAT_ACCUM_ID:
  5956. T = Context.SatAccumTy;
  5957. break;
  5958. case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
  5959. T = Context.SatLongAccumTy;
  5960. break;
  5961. case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
  5962. T = Context.SatUnsignedShortAccumTy;
  5963. break;
  5964. case PREDEF_TYPE_SAT_UACCUM_ID:
  5965. T = Context.SatUnsignedAccumTy;
  5966. break;
  5967. case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
  5968. T = Context.SatUnsignedLongAccumTy;
  5969. break;
  5970. case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
  5971. T = Context.SatShortFractTy;
  5972. break;
  5973. case PREDEF_TYPE_SAT_FRACT_ID:
  5974. T = Context.SatFractTy;
  5975. break;
  5976. case PREDEF_TYPE_SAT_LONG_FRACT_ID:
  5977. T = Context.SatLongFractTy;
  5978. break;
  5979. case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
  5980. T = Context.SatUnsignedShortFractTy;
  5981. break;
  5982. case PREDEF_TYPE_SAT_UFRACT_ID:
  5983. T = Context.SatUnsignedFractTy;
  5984. break;
  5985. case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
  5986. T = Context.SatUnsignedLongFractTy;
  5987. break;
  5988. case PREDEF_TYPE_FLOAT16_ID:
  5989. T = Context.Float16Ty;
  5990. break;
  5991. case PREDEF_TYPE_FLOAT128_ID:
  5992. T = Context.Float128Ty;
  5993. break;
  5994. case PREDEF_TYPE_OVERLOAD_ID:
  5995. T = Context.OverloadTy;
  5996. break;
  5997. case PREDEF_TYPE_BOUND_MEMBER:
  5998. T = Context.BoundMemberTy;
  5999. break;
  6000. case PREDEF_TYPE_PSEUDO_OBJECT:
  6001. T = Context.PseudoObjectTy;
  6002. break;
  6003. case PREDEF_TYPE_DEPENDENT_ID:
  6004. T = Context.DependentTy;
  6005. break;
  6006. case PREDEF_TYPE_UNKNOWN_ANY:
  6007. T = Context.UnknownAnyTy;
  6008. break;
  6009. case PREDEF_TYPE_NULLPTR_ID:
  6010. T = Context.NullPtrTy;
  6011. break;
  6012. case PREDEF_TYPE_CHAR8_ID:
  6013. T = Context.Char8Ty;
  6014. break;
  6015. case PREDEF_TYPE_CHAR16_ID:
  6016. T = Context.Char16Ty;
  6017. break;
  6018. case PREDEF_TYPE_CHAR32_ID:
  6019. T = Context.Char32Ty;
  6020. break;
  6021. case PREDEF_TYPE_OBJC_ID:
  6022. T = Context.ObjCBuiltinIdTy;
  6023. break;
  6024. case PREDEF_TYPE_OBJC_CLASS:
  6025. T = Context.ObjCBuiltinClassTy;
  6026. break;
  6027. case PREDEF_TYPE_OBJC_SEL:
  6028. T = Context.ObjCBuiltinSelTy;
  6029. break;
  6030. #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
  6031. case PREDEF_TYPE_##Id##_ID: \
  6032. T = Context.SingletonId; \
  6033. break;
  6034. #include "clang/Basic/OpenCLImageTypes.def"
  6035. #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
  6036. case PREDEF_TYPE_##Id##_ID: \
  6037. T = Context.Id##Ty; \
  6038. break;
  6039. #include "clang/Basic/OpenCLExtensionTypes.def"
  6040. case PREDEF_TYPE_SAMPLER_ID:
  6041. T = Context.OCLSamplerTy;
  6042. break;
  6043. case PREDEF_TYPE_EVENT_ID:
  6044. T = Context.OCLEventTy;
  6045. break;
  6046. case PREDEF_TYPE_CLK_EVENT_ID:
  6047. T = Context.OCLClkEventTy;
  6048. break;
  6049. case PREDEF_TYPE_QUEUE_ID:
  6050. T = Context.OCLQueueTy;
  6051. break;
  6052. case PREDEF_TYPE_RESERVE_ID_ID:
  6053. T = Context.OCLReserveIDTy;
  6054. break;
  6055. case PREDEF_TYPE_AUTO_DEDUCT:
  6056. T = Context.getAutoDeductType();
  6057. break;
  6058. case PREDEF_TYPE_AUTO_RREF_DEDUCT:
  6059. T = Context.getAutoRRefDeductType();
  6060. break;
  6061. case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
  6062. T = Context.ARCUnbridgedCastTy;
  6063. break;
  6064. case PREDEF_TYPE_BUILTIN_FN:
  6065. T = Context.BuiltinFnTy;
  6066. break;
  6067. case PREDEF_TYPE_OMP_ARRAY_SECTION:
  6068. T = Context.OMPArraySectionTy;
  6069. break;
  6070. }
  6071. assert(!T.isNull() && "Unknown predefined type");
  6072. return T.withFastQualifiers(FastQuals);
  6073. }
  6074. Index -= NUM_PREDEF_TYPE_IDS;
  6075. assert(Index < TypesLoaded.size() && "Type index out-of-range");
  6076. if (TypesLoaded[Index].isNull()) {
  6077. TypesLoaded[Index] = readTypeRecord(Index);
  6078. if (TypesLoaded[Index].isNull())
  6079. return QualType();
  6080. TypesLoaded[Index]->setFromAST();
  6081. if (DeserializationListener)
  6082. DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
  6083. TypesLoaded[Index]);
  6084. }
  6085. return TypesLoaded[Index].withFastQualifiers(FastQuals);
  6086. }
  6087. QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
  6088. return GetType(getGlobalTypeID(F, LocalID));
  6089. }
  6090. serialization::TypeID
  6091. ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
  6092. unsigned FastQuals = LocalID & Qualifiers::FastMask;
  6093. unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
  6094. if (LocalIndex < NUM_PREDEF_TYPE_IDS)
  6095. return LocalID;
  6096. if (!F.ModuleOffsetMap.empty())
  6097. ReadModuleOffsetMap(F);
  6098. ContinuousRangeMap<uint32_t, int, 2>::iterator I
  6099. = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
  6100. assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
  6101. unsigned GlobalIndex = LocalIndex + I->second;
  6102. return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
  6103. }
  6104. TemplateArgumentLocInfo
  6105. ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
  6106. TemplateArgument::ArgKind Kind,
  6107. const RecordData &Record,
  6108. unsigned &Index) {
  6109. switch (Kind) {
  6110. case TemplateArgument::Expression:
  6111. return ReadExpr(F);
  6112. case TemplateArgument::Type:
  6113. return GetTypeSourceInfo(F, Record, Index);
  6114. case TemplateArgument::Template: {
  6115. NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
  6116. Index);
  6117. SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
  6118. return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
  6119. SourceLocation());
  6120. }
  6121. case TemplateArgument::TemplateExpansion: {
  6122. NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
  6123. Index);
  6124. SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
  6125. SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
  6126. return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
  6127. EllipsisLoc);
  6128. }
  6129. case TemplateArgument::Null:
  6130. case TemplateArgument::Integral:
  6131. case TemplateArgument::Declaration:
  6132. case TemplateArgument::NullPtr:
  6133. case TemplateArgument::Pack:
  6134. // FIXME: Is this right?
  6135. return TemplateArgumentLocInfo();
  6136. }
  6137. llvm_unreachable("unexpected template argument loc");
  6138. }
  6139. TemplateArgumentLoc
  6140. ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
  6141. const RecordData &Record, unsigned &Index) {
  6142. TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
  6143. if (Arg.getKind() == TemplateArgument::Expression) {
  6144. if (Record[Index++]) // bool InfoHasSameExpr.
  6145. return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
  6146. }
  6147. return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
  6148. Record, Index));
  6149. }
  6150. const ASTTemplateArgumentListInfo*
  6151. ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
  6152. const RecordData &Record,
  6153. unsigned &Index) {
  6154. SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
  6155. SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
  6156. unsigned NumArgsAsWritten = Record[Index++];
  6157. TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
  6158. for (unsigned i = 0; i != NumArgsAsWritten; ++i)
  6159. TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
  6160. return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
  6161. }
  6162. Decl *ASTReader::GetExternalDecl(uint32_t ID) {
  6163. return GetDecl(ID);
  6164. }
  6165. void ASTReader::CompleteRedeclChain(const Decl *D) {
  6166. if (NumCurrentElementsDeserializing) {
  6167. // We arrange to not care about the complete redeclaration chain while we're
  6168. // deserializing. Just remember that the AST has marked this one as complete
  6169. // but that it's not actually complete yet, so we know we still need to
  6170. // complete it later.
  6171. PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
  6172. return;
  6173. }
  6174. const DeclContext *DC = D->getDeclContext()->getRedeclContext();
  6175. // If this is a named declaration, complete it by looking it up
  6176. // within its context.
  6177. //
  6178. // FIXME: Merging a function definition should merge
  6179. // all mergeable entities within it.
  6180. if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
  6181. isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
  6182. if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
  6183. if (!getContext().getLangOpts().CPlusPlus &&
  6184. isa<TranslationUnitDecl>(DC)) {
  6185. // Outside of C++, we don't have a lookup table for the TU, so update
  6186. // the identifier instead. (For C++ modules, we don't store decls
  6187. // in the serialized identifier table, so we do the lookup in the TU.)
  6188. auto *II = Name.getAsIdentifierInfo();
  6189. assert(II && "non-identifier name in C?");
  6190. if (II->isOutOfDate())
  6191. updateOutOfDateIdentifier(*II);
  6192. } else
  6193. DC->lookup(Name);
  6194. } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
  6195. // Find all declarations of this kind from the relevant context.
  6196. for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
  6197. auto *DC = cast<DeclContext>(DCDecl);
  6198. SmallVector<Decl*, 8> Decls;
  6199. FindExternalLexicalDecls(
  6200. DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
  6201. }
  6202. }
  6203. }
  6204. if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
  6205. CTSD->getSpecializedTemplate()->LoadLazySpecializations();
  6206. if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
  6207. VTSD->getSpecializedTemplate()->LoadLazySpecializations();
  6208. if (auto *FD = dyn_cast<FunctionDecl>(D)) {
  6209. if (auto *Template = FD->getPrimaryTemplate())
  6210. Template->LoadLazySpecializations();
  6211. }
  6212. }
  6213. CXXCtorInitializer **
  6214. ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
  6215. RecordLocation Loc = getLocalBitOffset(Offset);
  6216. BitstreamCursor &Cursor = Loc.F->DeclsCursor;
  6217. SavedStreamPosition SavedPosition(Cursor);
  6218. Cursor.JumpToBit(Loc.Offset);
  6219. ReadingKindTracker ReadingKind(Read_Decl, *this);
  6220. RecordData Record;
  6221. unsigned Code = Cursor.ReadCode();
  6222. unsigned RecCode = Cursor.readRecord(Code, Record);
  6223. if (RecCode != DECL_CXX_CTOR_INITIALIZERS) {
  6224. Error("malformed AST file: missing C++ ctor initializers");
  6225. return nullptr;
  6226. }
  6227. unsigned Idx = 0;
  6228. return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
  6229. }
  6230. CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
  6231. assert(ContextObj && "reading base specifiers with no AST context");
  6232. ASTContext &Context = *ContextObj;
  6233. RecordLocation Loc = getLocalBitOffset(Offset);
  6234. BitstreamCursor &Cursor = Loc.F->DeclsCursor;
  6235. SavedStreamPosition SavedPosition(Cursor);
  6236. Cursor.JumpToBit(Loc.Offset);
  6237. ReadingKindTracker ReadingKind(Read_Decl, *this);
  6238. RecordData Record;
  6239. unsigned Code = Cursor.ReadCode();
  6240. unsigned RecCode = Cursor.readRecord(Code, Record);
  6241. if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
  6242. Error("malformed AST file: missing C++ base specifiers");
  6243. return nullptr;
  6244. }
  6245. unsigned Idx = 0;
  6246. unsigned NumBases = Record[Idx++];
  6247. void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
  6248. CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
  6249. for (unsigned I = 0; I != NumBases; ++I)
  6250. Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
  6251. return Bases;
  6252. }
  6253. serialization::DeclID
  6254. ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
  6255. if (LocalID < NUM_PREDEF_DECL_IDS)
  6256. return LocalID;
  6257. if (!F.ModuleOffsetMap.empty())
  6258. ReadModuleOffsetMap(F);
  6259. ContinuousRangeMap<uint32_t, int, 2>::iterator I
  6260. = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
  6261. assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
  6262. return LocalID + I->second;
  6263. }
  6264. bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
  6265. ModuleFile &M) const {
  6266. // Predefined decls aren't from any module.
  6267. if (ID < NUM_PREDEF_DECL_IDS)
  6268. return false;
  6269. return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
  6270. ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
  6271. }
  6272. ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
  6273. if (!D->isFromASTFile())
  6274. return nullptr;
  6275. GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
  6276. assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
  6277. return I->second;
  6278. }
  6279. SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
  6280. if (ID < NUM_PREDEF_DECL_IDS)
  6281. return SourceLocation();
  6282. unsigned Index = ID - NUM_PREDEF_DECL_IDS;
  6283. if (Index > DeclsLoaded.size()) {
  6284. Error("declaration ID out-of-range for AST file");
  6285. return SourceLocation();
  6286. }
  6287. if (Decl *D = DeclsLoaded[Index])
  6288. return D->getLocation();
  6289. SourceLocation Loc;
  6290. DeclCursorForID(ID, Loc);
  6291. return Loc;
  6292. }
  6293. static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
  6294. switch (ID) {
  6295. case PREDEF_DECL_NULL_ID:
  6296. return nullptr;
  6297. case PREDEF_DECL_TRANSLATION_UNIT_ID:
  6298. return Context.getTranslationUnitDecl();
  6299. case PREDEF_DECL_OBJC_ID_ID:
  6300. return Context.getObjCIdDecl();
  6301. case PREDEF_DECL_OBJC_SEL_ID:
  6302. return Context.getObjCSelDecl();
  6303. case PREDEF_DECL_OBJC_CLASS_ID:
  6304. return Context.getObjCClassDecl();
  6305. case PREDEF_DECL_OBJC_PROTOCOL_ID:
  6306. return Context.getObjCProtocolDecl();
  6307. case PREDEF_DECL_INT_128_ID:
  6308. return Context.getInt128Decl();
  6309. case PREDEF_DECL_UNSIGNED_INT_128_ID:
  6310. return Context.getUInt128Decl();
  6311. case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
  6312. return Context.getObjCInstanceTypeDecl();
  6313. case PREDEF_DECL_BUILTIN_VA_LIST_ID:
  6314. return Context.getBuiltinVaListDecl();
  6315. case PREDEF_DECL_VA_LIST_TAG:
  6316. return Context.getVaListTagDecl();
  6317. case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
  6318. return Context.getBuiltinMSVaListDecl();
  6319. case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
  6320. return Context.getExternCContextDecl();
  6321. case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
  6322. return Context.getMakeIntegerSeqDecl();
  6323. case PREDEF_DECL_CF_CONSTANT_STRING_ID:
  6324. return Context.getCFConstantStringDecl();
  6325. case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
  6326. return Context.getCFConstantStringTagDecl();
  6327. case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
  6328. return Context.getTypePackElementDecl();
  6329. }
  6330. llvm_unreachable("PredefinedDeclIDs unknown enum value");
  6331. }
  6332. Decl *ASTReader::GetExistingDecl(DeclID ID) {
  6333. assert(ContextObj && "reading decl with no AST context");
  6334. if (ID < NUM_PREDEF_DECL_IDS) {
  6335. Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
  6336. if (D) {
  6337. // Track that we have merged the declaration with ID \p ID into the
  6338. // pre-existing predefined declaration \p D.
  6339. auto &Merged = KeyDecls[D->getCanonicalDecl()];
  6340. if (Merged.empty())
  6341. Merged.push_back(ID);
  6342. }
  6343. return D;
  6344. }
  6345. unsigned Index = ID - NUM_PREDEF_DECL_IDS;
  6346. if (Index >= DeclsLoaded.size()) {
  6347. assert(0 && "declaration ID out-of-range for AST file");
  6348. Error("declaration ID out-of-range for AST file");
  6349. return nullptr;
  6350. }
  6351. return DeclsLoaded[Index];
  6352. }
  6353. Decl *ASTReader::GetDecl(DeclID ID) {
  6354. if (ID < NUM_PREDEF_DECL_IDS)
  6355. return GetExistingDecl(ID);
  6356. unsigned Index = ID - NUM_PREDEF_DECL_IDS;
  6357. if (Index >= DeclsLoaded.size()) {
  6358. assert(0 && "declaration ID out-of-range for AST file");
  6359. Error("declaration ID out-of-range for AST file");
  6360. return nullptr;
  6361. }
  6362. if (!DeclsLoaded[Index]) {
  6363. ReadDeclRecord(ID);
  6364. if (DeserializationListener)
  6365. DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
  6366. }
  6367. return DeclsLoaded[Index];
  6368. }
  6369. DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
  6370. DeclID GlobalID) {
  6371. if (GlobalID < NUM_PREDEF_DECL_IDS)
  6372. return GlobalID;
  6373. GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
  6374. assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
  6375. ModuleFile *Owner = I->second;
  6376. llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
  6377. = M.GlobalToLocalDeclIDs.find(Owner);
  6378. if (Pos == M.GlobalToLocalDeclIDs.end())
  6379. return 0;
  6380. return GlobalID - Owner->BaseDeclID + Pos->second;
  6381. }
  6382. serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
  6383. const RecordData &Record,
  6384. unsigned &Idx) {
  6385. if (Idx >= Record.size()) {
  6386. Error("Corrupted AST file");
  6387. return 0;
  6388. }
  6389. return getGlobalDeclID(F, Record[Idx++]);
  6390. }
  6391. /// Resolve the offset of a statement into a statement.
  6392. ///
  6393. /// This operation will read a new statement from the external
  6394. /// source each time it is called, and is meant to be used via a
  6395. /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
  6396. Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
  6397. // Switch case IDs are per Decl.
  6398. ClearSwitchCaseIDs();
  6399. // Offset here is a global offset across the entire chain.
  6400. RecordLocation Loc = getLocalBitOffset(Offset);
  6401. Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
  6402. assert(NumCurrentElementsDeserializing == 0 &&
  6403. "should not be called while already deserializing");
  6404. Deserializing D(this);
  6405. return ReadStmtFromStream(*Loc.F);
  6406. }
  6407. void ASTReader::FindExternalLexicalDecls(
  6408. const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
  6409. SmallVectorImpl<Decl *> &Decls) {
  6410. bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
  6411. auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
  6412. assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
  6413. for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
  6414. auto K = (Decl::Kind)+LexicalDecls[I];
  6415. if (!IsKindWeWant(K))
  6416. continue;
  6417. auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
  6418. // Don't add predefined declarations to the lexical context more
  6419. // than once.
  6420. if (ID < NUM_PREDEF_DECL_IDS) {
  6421. if (PredefsVisited[ID])
  6422. continue;
  6423. PredefsVisited[ID] = true;
  6424. }
  6425. if (Decl *D = GetLocalDecl(*M, ID)) {
  6426. assert(D->getKind() == K && "wrong kind for lexical decl");
  6427. if (!DC->isDeclInLexicalTraversal(D))
  6428. Decls.push_back(D);
  6429. }
  6430. }
  6431. };
  6432. if (isa<TranslationUnitDecl>(DC)) {
  6433. for (auto Lexical : TULexicalDecls)
  6434. Visit(Lexical.first, Lexical.second);
  6435. } else {
  6436. auto I = LexicalDecls.find(DC);
  6437. if (I != LexicalDecls.end())
  6438. Visit(I->second.first, I->second.second);
  6439. }
  6440. ++NumLexicalDeclContextsRead;
  6441. }
  6442. namespace {
  6443. class DeclIDComp {
  6444. ASTReader &Reader;
  6445. ModuleFile &Mod;
  6446. public:
  6447. DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
  6448. bool operator()(LocalDeclID L, LocalDeclID R) const {
  6449. SourceLocation LHS = getLocation(L);
  6450. SourceLocation RHS = getLocation(R);
  6451. return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
  6452. }
  6453. bool operator()(SourceLocation LHS, LocalDeclID R) const {
  6454. SourceLocation RHS = getLocation(R);
  6455. return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
  6456. }
  6457. bool operator()(LocalDeclID L, SourceLocation RHS) const {
  6458. SourceLocation LHS = getLocation(L);
  6459. return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
  6460. }
  6461. SourceLocation getLocation(LocalDeclID ID) const {
  6462. return Reader.getSourceManager().getFileLoc(
  6463. Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
  6464. }
  6465. };
  6466. } // namespace
  6467. void ASTReader::FindFileRegionDecls(FileID File,
  6468. unsigned Offset, unsigned Length,
  6469. SmallVectorImpl<Decl *> &Decls) {
  6470. SourceManager &SM = getSourceManager();
  6471. llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
  6472. if (I == FileDeclIDs.end())
  6473. return;
  6474. FileDeclsInfo &DInfo = I->second;
  6475. if (DInfo.Decls.empty())
  6476. return;
  6477. SourceLocation
  6478. BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
  6479. SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
  6480. DeclIDComp DIDComp(*this, *DInfo.Mod);
  6481. ArrayRef<serialization::LocalDeclID>::iterator
  6482. BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
  6483. BeginLoc, DIDComp);
  6484. if (BeginIt != DInfo.Decls.begin())
  6485. --BeginIt;
  6486. // If we are pointing at a top-level decl inside an objc container, we need
  6487. // to backtrack until we find it otherwise we will fail to report that the
  6488. // region overlaps with an objc container.
  6489. while (BeginIt != DInfo.Decls.begin() &&
  6490. GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
  6491. ->isTopLevelDeclInObjCContainer())
  6492. --BeginIt;
  6493. ArrayRef<serialization::LocalDeclID>::iterator
  6494. EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
  6495. EndLoc, DIDComp);
  6496. if (EndIt != DInfo.Decls.end())
  6497. ++EndIt;
  6498. for (ArrayRef<serialization::LocalDeclID>::iterator
  6499. DIt = BeginIt; DIt != EndIt; ++DIt)
  6500. Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
  6501. }
  6502. bool
  6503. ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
  6504. DeclarationName Name) {
  6505. assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
  6506. "DeclContext has no visible decls in storage");
  6507. if (!Name)
  6508. return false;
  6509. auto It = Lookups.find(DC);
  6510. if (It == Lookups.end())
  6511. return false;
  6512. Deserializing LookupResults(this);
  6513. // Load the list of declarations.
  6514. SmallVector<NamedDecl *, 64> Decls;
  6515. for (DeclID ID : It->second.Table.find(Name)) {
  6516. NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
  6517. if (ND->getDeclName() == Name)
  6518. Decls.push_back(ND);
  6519. }
  6520. ++NumVisibleDeclContextsRead;
  6521. SetExternalVisibleDeclsForName(DC, Name, Decls);
  6522. return !Decls.empty();
  6523. }
  6524. void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
  6525. if (!DC->hasExternalVisibleStorage())
  6526. return;
  6527. auto It = Lookups.find(DC);
  6528. assert(It != Lookups.end() &&
  6529. "have external visible storage but no lookup tables");
  6530. DeclsMap Decls;
  6531. for (DeclID ID : It->second.Table.findAll()) {
  6532. NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
  6533. Decls[ND->getDeclName()].push_back(ND);
  6534. }
  6535. ++NumVisibleDeclContextsRead;
  6536. for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
  6537. SetExternalVisibleDeclsForName(DC, I->first, I->second);
  6538. }
  6539. const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
  6540. }
  6541. const serialization::reader::DeclContextLookupTable *
  6542. ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
  6543. auto I = Lookups.find(Primary);
  6544. return I == Lookups.end() ? nullptr : &I->second;
  6545. }
  6546. /// Under non-PCH compilation the consumer receives the objc methods
  6547. /// before receiving the implementation, and codegen depends on this.
  6548. /// We simulate this by deserializing and passing to consumer the methods of the
  6549. /// implementation before passing the deserialized implementation decl.
  6550. static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
  6551. ASTConsumer *Consumer) {
  6552. assert(ImplD && Consumer);
  6553. for (auto *I : ImplD->methods())
  6554. Consumer->HandleInterestingDecl(DeclGroupRef(I));
  6555. Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
  6556. }
  6557. void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
  6558. if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
  6559. PassObjCImplDeclToConsumer(ImplD, Consumer);
  6560. else
  6561. Consumer->HandleInterestingDecl(DeclGroupRef(D));
  6562. }
  6563. void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
  6564. this->Consumer = Consumer;
  6565. if (Consumer)
  6566. PassInterestingDeclsToConsumer();
  6567. if (DeserializationListener)
  6568. DeserializationListener->ReaderInitialized(this);
  6569. }
  6570. void ASTReader::PrintStats() {
  6571. std::fprintf(stderr, "*** AST File Statistics:\n");
  6572. unsigned NumTypesLoaded
  6573. = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
  6574. QualType());
  6575. unsigned NumDeclsLoaded
  6576. = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
  6577. (Decl *)nullptr);
  6578. unsigned NumIdentifiersLoaded
  6579. = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
  6580. IdentifiersLoaded.end(),
  6581. (IdentifierInfo *)nullptr);
  6582. unsigned NumMacrosLoaded
  6583. = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
  6584. MacrosLoaded.end(),
  6585. (MacroInfo *)nullptr);
  6586. unsigned NumSelectorsLoaded
  6587. = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
  6588. SelectorsLoaded.end(),
  6589. Selector());
  6590. if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
  6591. std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
  6592. NumSLocEntriesRead, TotalNumSLocEntries,
  6593. ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
  6594. if (!TypesLoaded.empty())
  6595. std::fprintf(stderr, " %u/%u types read (%f%%)\n",
  6596. NumTypesLoaded, (unsigned)TypesLoaded.size(),
  6597. ((float)NumTypesLoaded/TypesLoaded.size() * 100));
  6598. if (!DeclsLoaded.empty())
  6599. std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
  6600. NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
  6601. ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
  6602. if (!IdentifiersLoaded.empty())
  6603. std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
  6604. NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
  6605. ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
  6606. if (!MacrosLoaded.empty())
  6607. std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
  6608. NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
  6609. ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
  6610. if (!SelectorsLoaded.empty())
  6611. std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
  6612. NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
  6613. ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
  6614. if (TotalNumStatements)
  6615. std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
  6616. NumStatementsRead, TotalNumStatements,
  6617. ((float)NumStatementsRead/TotalNumStatements * 100));
  6618. if (TotalNumMacros)
  6619. std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
  6620. NumMacrosRead, TotalNumMacros,
  6621. ((float)NumMacrosRead/TotalNumMacros * 100));
  6622. if (TotalLexicalDeclContexts)
  6623. std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
  6624. NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
  6625. ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
  6626. * 100));
  6627. if (TotalVisibleDeclContexts)
  6628. std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
  6629. NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
  6630. ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
  6631. * 100));
  6632. if (TotalNumMethodPoolEntries)
  6633. std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
  6634. NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
  6635. ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
  6636. * 100));
  6637. if (NumMethodPoolLookups)
  6638. std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
  6639. NumMethodPoolHits, NumMethodPoolLookups,
  6640. ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
  6641. if (NumMethodPoolTableLookups)
  6642. std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
  6643. NumMethodPoolTableHits, NumMethodPoolTableLookups,
  6644. ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
  6645. * 100.0));
  6646. if (NumIdentifierLookupHits)
  6647. std::fprintf(stderr,
  6648. " %u / %u identifier table lookups succeeded (%f%%)\n",
  6649. NumIdentifierLookupHits, NumIdentifierLookups,
  6650. (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
  6651. if (GlobalIndex) {
  6652. std::fprintf(stderr, "\n");
  6653. GlobalIndex->printStats();
  6654. }
  6655. std::fprintf(stderr, "\n");
  6656. dump();
  6657. std::fprintf(stderr, "\n");
  6658. }
  6659. template<typename Key, typename ModuleFile, unsigned InitialCapacity>
  6660. LLVM_DUMP_METHOD static void
  6661. dumpModuleIDMap(StringRef Name,
  6662. const ContinuousRangeMap<Key, ModuleFile *,
  6663. InitialCapacity> &Map) {
  6664. if (Map.begin() == Map.end())
  6665. return;
  6666. using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
  6667. llvm::errs() << Name << ":\n";
  6668. for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
  6669. I != IEnd; ++I) {
  6670. llvm::errs() << " " << I->first << " -> " << I->second->FileName
  6671. << "\n";
  6672. }
  6673. }
  6674. LLVM_DUMP_METHOD void ASTReader::dump() {
  6675. llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
  6676. dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
  6677. dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
  6678. dumpModuleIDMap("Global type map", GlobalTypeMap);
  6679. dumpModuleIDMap("Global declaration map", GlobalDeclMap);
  6680. dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
  6681. dumpModuleIDMap("Global macro map", GlobalMacroMap);
  6682. dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
  6683. dumpModuleIDMap("Global selector map", GlobalSelectorMap);
  6684. dumpModuleIDMap("Global preprocessed entity map",
  6685. GlobalPreprocessedEntityMap);
  6686. llvm::errs() << "\n*** PCH/Modules Loaded:";
  6687. for (ModuleFile &M : ModuleMgr)
  6688. M.dump();
  6689. }
  6690. /// Return the amount of memory used by memory buffers, breaking down
  6691. /// by heap-backed versus mmap'ed memory.
  6692. void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
  6693. for (ModuleFile &I : ModuleMgr) {
  6694. if (llvm::MemoryBuffer *buf = I.Buffer) {
  6695. size_t bytes = buf->getBufferSize();
  6696. switch (buf->getBufferKind()) {
  6697. case llvm::MemoryBuffer::MemoryBuffer_Malloc:
  6698. sizes.malloc_bytes += bytes;
  6699. break;
  6700. case llvm::MemoryBuffer::MemoryBuffer_MMap:
  6701. sizes.mmap_bytes += bytes;
  6702. break;
  6703. }
  6704. }
  6705. }
  6706. }
  6707. void ASTReader::InitializeSema(Sema &S) {
  6708. SemaObj = &S;
  6709. S.addExternalSource(this);
  6710. // Makes sure any declarations that were deserialized "too early"
  6711. // still get added to the identifier's declaration chains.
  6712. for (uint64_t ID : PreloadedDeclIDs) {
  6713. NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
  6714. pushExternalDeclIntoScope(D, D->getDeclName());
  6715. }
  6716. PreloadedDeclIDs.clear();
  6717. // FIXME: What happens if these are changed by a module import?
  6718. if (!FPPragmaOptions.empty()) {
  6719. assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
  6720. SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
  6721. }
  6722. SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
  6723. SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
  6724. SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
  6725. UpdateSema();
  6726. }
  6727. void ASTReader::UpdateSema() {
  6728. assert(SemaObj && "no Sema to update");
  6729. // Load the offsets of the declarations that Sema references.
  6730. // They will be lazily deserialized when needed.
  6731. if (!SemaDeclRefs.empty()) {
  6732. assert(SemaDeclRefs.size() % 3 == 0);
  6733. for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
  6734. if (!SemaObj->StdNamespace)
  6735. SemaObj->StdNamespace = SemaDeclRefs[I];
  6736. if (!SemaObj->StdBadAlloc)
  6737. SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
  6738. if (!SemaObj->StdAlignValT)
  6739. SemaObj->StdAlignValT = SemaDeclRefs[I+2];
  6740. }
  6741. SemaDeclRefs.clear();
  6742. }
  6743. // Update the state of pragmas. Use the same API as if we had encountered the
  6744. // pragma in the source.
  6745. if(OptimizeOffPragmaLocation.isValid())
  6746. SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
  6747. if (PragmaMSStructState != -1)
  6748. SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
  6749. if (PointersToMembersPragmaLocation.isValid()) {
  6750. SemaObj->ActOnPragmaMSPointersToMembers(
  6751. (LangOptions::PragmaMSPointersToMembersKind)
  6752. PragmaMSPointersToMembersState,
  6753. PointersToMembersPragmaLocation);
  6754. }
  6755. SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
  6756. if (PragmaPackCurrentValue) {
  6757. // The bottom of the stack might have a default value. It must be adjusted
  6758. // to the current value to ensure that the packing state is preserved after
  6759. // popping entries that were included/imported from a PCH/module.
  6760. bool DropFirst = false;
  6761. if (!PragmaPackStack.empty() &&
  6762. PragmaPackStack.front().Location.isInvalid()) {
  6763. assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
  6764. "Expected a default alignment value");
  6765. SemaObj->PackStack.Stack.emplace_back(
  6766. PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
  6767. SemaObj->PackStack.CurrentPragmaLocation,
  6768. PragmaPackStack.front().PushLocation);
  6769. DropFirst = true;
  6770. }
  6771. for (const auto &Entry :
  6772. llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
  6773. SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
  6774. Entry.Location, Entry.PushLocation);
  6775. if (PragmaPackCurrentLocation.isInvalid()) {
  6776. assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
  6777. "Expected a default alignment value");
  6778. // Keep the current values.
  6779. } else {
  6780. SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
  6781. SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
  6782. }
  6783. }
  6784. }
  6785. IdentifierInfo *ASTReader::get(StringRef Name) {
  6786. // Note that we are loading an identifier.
  6787. Deserializing AnIdentifier(this);
  6788. IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
  6789. NumIdentifierLookups,
  6790. NumIdentifierLookupHits);
  6791. // We don't need to do identifier table lookups in C++ modules (we preload
  6792. // all interesting declarations, and don't need to use the scope for name
  6793. // lookups). Perform the lookup in PCH files, though, since we don't build
  6794. // a complete initial identifier table if we're carrying on from a PCH.
  6795. if (PP.getLangOpts().CPlusPlus) {
  6796. for (auto F : ModuleMgr.pch_modules())
  6797. if (Visitor(*F))
  6798. break;
  6799. } else {
  6800. // If there is a global index, look there first to determine which modules
  6801. // provably do not have any results for this identifier.
  6802. GlobalModuleIndex::HitSet Hits;
  6803. GlobalModuleIndex::HitSet *HitsPtr = nullptr;
  6804. if (!loadGlobalIndex()) {
  6805. if (GlobalIndex->lookupIdentifier(Name, Hits)) {
  6806. HitsPtr = &Hits;
  6807. }
  6808. }
  6809. ModuleMgr.visit(Visitor, HitsPtr);
  6810. }
  6811. IdentifierInfo *II = Visitor.getIdentifierInfo();
  6812. markIdentifierUpToDate(II);
  6813. return II;
  6814. }
  6815. namespace clang {
  6816. /// An identifier-lookup iterator that enumerates all of the
  6817. /// identifiers stored within a set of AST files.
  6818. class ASTIdentifierIterator : public IdentifierIterator {
  6819. /// The AST reader whose identifiers are being enumerated.
  6820. const ASTReader &Reader;
  6821. /// The current index into the chain of AST files stored in
  6822. /// the AST reader.
  6823. unsigned Index;
  6824. /// The current position within the identifier lookup table
  6825. /// of the current AST file.
  6826. ASTIdentifierLookupTable::key_iterator Current;
  6827. /// The end position within the identifier lookup table of
  6828. /// the current AST file.
  6829. ASTIdentifierLookupTable::key_iterator End;
  6830. /// Whether to skip any modules in the ASTReader.
  6831. bool SkipModules;
  6832. public:
  6833. explicit ASTIdentifierIterator(const ASTReader &Reader,
  6834. bool SkipModules = false);
  6835. StringRef Next() override;
  6836. };
  6837. } // namespace clang
  6838. ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
  6839. bool SkipModules)
  6840. : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
  6841. }
  6842. StringRef ASTIdentifierIterator::Next() {
  6843. while (Current == End) {
  6844. // If we have exhausted all of our AST files, we're done.
  6845. if (Index == 0)
  6846. return StringRef();
  6847. --Index;
  6848. ModuleFile &F = Reader.ModuleMgr[Index];
  6849. if (SkipModules && F.isModule())
  6850. continue;
  6851. ASTIdentifierLookupTable *IdTable =
  6852. (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
  6853. Current = IdTable->key_begin();
  6854. End = IdTable->key_end();
  6855. }
  6856. // We have any identifiers remaining in the current AST file; return
  6857. // the next one.
  6858. StringRef Result = *Current;
  6859. ++Current;
  6860. return Result;
  6861. }
  6862. namespace {
  6863. /// A utility for appending two IdentifierIterators.
  6864. class ChainedIdentifierIterator : public IdentifierIterator {
  6865. std::unique_ptr<IdentifierIterator> Current;
  6866. std::unique_ptr<IdentifierIterator> Queued;
  6867. public:
  6868. ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
  6869. std::unique_ptr<IdentifierIterator> Second)
  6870. : Current(std::move(First)), Queued(std::move(Second)) {}
  6871. StringRef Next() override {
  6872. if (!Current)
  6873. return StringRef();
  6874. StringRef result = Current->Next();
  6875. if (!result.empty())
  6876. return result;
  6877. // Try the queued iterator, which may itself be empty.
  6878. Current.reset();
  6879. std::swap(Current, Queued);
  6880. return Next();
  6881. }
  6882. };
  6883. } // namespace
  6884. IdentifierIterator *ASTReader::getIdentifiers() {
  6885. if (!loadGlobalIndex()) {
  6886. std::unique_ptr<IdentifierIterator> ReaderIter(
  6887. new ASTIdentifierIterator(*this, /*SkipModules=*/true));
  6888. std::unique_ptr<IdentifierIterator> ModulesIter(
  6889. GlobalIndex->createIdentifierIterator());
  6890. return new ChainedIdentifierIterator(std::move(ReaderIter),
  6891. std::move(ModulesIter));
  6892. }
  6893. return new ASTIdentifierIterator(*this);
  6894. }
  6895. namespace clang {
  6896. namespace serialization {
  6897. class ReadMethodPoolVisitor {
  6898. ASTReader &Reader;
  6899. Selector Sel;
  6900. unsigned PriorGeneration;
  6901. unsigned InstanceBits = 0;
  6902. unsigned FactoryBits = 0;
  6903. bool InstanceHasMoreThanOneDecl = false;
  6904. bool FactoryHasMoreThanOneDecl = false;
  6905. SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
  6906. SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
  6907. public:
  6908. ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
  6909. unsigned PriorGeneration)
  6910. : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
  6911. bool operator()(ModuleFile &M) {
  6912. if (!M.SelectorLookupTable)
  6913. return false;
  6914. // If we've already searched this module file, skip it now.
  6915. if (M.Generation <= PriorGeneration)
  6916. return true;
  6917. ++Reader.NumMethodPoolTableLookups;
  6918. ASTSelectorLookupTable *PoolTable
  6919. = (ASTSelectorLookupTable*)M.SelectorLookupTable;
  6920. ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
  6921. if (Pos == PoolTable->end())
  6922. return false;
  6923. ++Reader.NumMethodPoolTableHits;
  6924. ++Reader.NumSelectorsRead;
  6925. // FIXME: Not quite happy with the statistics here. We probably should
  6926. // disable this tracking when called via LoadSelector.
  6927. // Also, should entries without methods count as misses?
  6928. ++Reader.NumMethodPoolEntriesRead;
  6929. ASTSelectorLookupTrait::data_type Data = *Pos;
  6930. if (Reader.DeserializationListener)
  6931. Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
  6932. InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
  6933. FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
  6934. InstanceBits = Data.InstanceBits;
  6935. FactoryBits = Data.FactoryBits;
  6936. InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
  6937. FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
  6938. return true;
  6939. }
  6940. /// Retrieve the instance methods found by this visitor.
  6941. ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
  6942. return InstanceMethods;
  6943. }
  6944. /// Retrieve the instance methods found by this visitor.
  6945. ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
  6946. return FactoryMethods;
  6947. }
  6948. unsigned getInstanceBits() const { return InstanceBits; }
  6949. unsigned getFactoryBits() const { return FactoryBits; }
  6950. bool instanceHasMoreThanOneDecl() const {
  6951. return InstanceHasMoreThanOneDecl;
  6952. }
  6953. bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
  6954. };
  6955. } // namespace serialization
  6956. } // namespace clang
  6957. /// Add the given set of methods to the method list.
  6958. static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
  6959. ObjCMethodList &List) {
  6960. for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
  6961. S.addMethodToGlobalList(&List, Methods[I]);
  6962. }
  6963. }
  6964. void ASTReader::ReadMethodPool(Selector Sel) {
  6965. // Get the selector generation and update it to the current generation.
  6966. unsigned &Generation = SelectorGeneration[Sel];
  6967. unsigned PriorGeneration = Generation;
  6968. Generation = getGeneration();
  6969. SelectorOutOfDate[Sel] = false;
  6970. // Search for methods defined with this selector.
  6971. ++NumMethodPoolLookups;
  6972. ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
  6973. ModuleMgr.visit(Visitor);
  6974. if (Visitor.getInstanceMethods().empty() &&
  6975. Visitor.getFactoryMethods().empty())
  6976. return;
  6977. ++NumMethodPoolHits;
  6978. if (!getSema())
  6979. return;
  6980. Sema &S = *getSema();
  6981. Sema::GlobalMethodPool::iterator Pos
  6982. = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
  6983. Pos->second.first.setBits(Visitor.getInstanceBits());
  6984. Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
  6985. Pos->second.second.setBits(Visitor.getFactoryBits());
  6986. Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
  6987. // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
  6988. // when building a module we keep every method individually and may need to
  6989. // update hasMoreThanOneDecl as we add the methods.
  6990. addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
  6991. addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
  6992. }
  6993. void ASTReader::updateOutOfDateSelector(Selector Sel) {
  6994. if (SelectorOutOfDate[Sel])
  6995. ReadMethodPool(Sel);
  6996. }
  6997. void ASTReader::ReadKnownNamespaces(
  6998. SmallVectorImpl<NamespaceDecl *> &Namespaces) {
  6999. Namespaces.clear();
  7000. for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
  7001. if (NamespaceDecl *Namespace
  7002. = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
  7003. Namespaces.push_back(Namespace);
  7004. }
  7005. }
  7006. void ASTReader::ReadUndefinedButUsed(
  7007. llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
  7008. for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
  7009. NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
  7010. SourceLocation Loc =
  7011. SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
  7012. Undefined.insert(std::make_pair(D, Loc));
  7013. }
  7014. }
  7015. void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
  7016. FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
  7017. Exprs) {
  7018. for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
  7019. FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
  7020. uint64_t Count = DelayedDeleteExprs[Idx++];
  7021. for (uint64_t C = 0; C < Count; ++C) {
  7022. SourceLocation DeleteLoc =
  7023. SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
  7024. const bool IsArrayForm = DelayedDeleteExprs[Idx++];
  7025. Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
  7026. }
  7027. }
  7028. }
  7029. void ASTReader::ReadTentativeDefinitions(
  7030. SmallVectorImpl<VarDecl *> &TentativeDefs) {
  7031. for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
  7032. VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
  7033. if (Var)
  7034. TentativeDefs.push_back(Var);
  7035. }
  7036. TentativeDefinitions.clear();
  7037. }
  7038. void ASTReader::ReadUnusedFileScopedDecls(
  7039. SmallVectorImpl<const DeclaratorDecl *> &Decls) {
  7040. for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
  7041. DeclaratorDecl *D
  7042. = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
  7043. if (D)
  7044. Decls.push_back(D);
  7045. }
  7046. UnusedFileScopedDecls.clear();
  7047. }
  7048. void ASTReader::ReadDelegatingConstructors(
  7049. SmallVectorImpl<CXXConstructorDecl *> &Decls) {
  7050. for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
  7051. CXXConstructorDecl *D
  7052. = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
  7053. if (D)
  7054. Decls.push_back(D);
  7055. }
  7056. DelegatingCtorDecls.clear();
  7057. }
  7058. void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
  7059. for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
  7060. TypedefNameDecl *D
  7061. = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
  7062. if (D)
  7063. Decls.push_back(D);
  7064. }
  7065. ExtVectorDecls.clear();
  7066. }
  7067. void ASTReader::ReadUnusedLocalTypedefNameCandidates(
  7068. llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
  7069. for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
  7070. ++I) {
  7071. TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
  7072. GetDecl(UnusedLocalTypedefNameCandidates[I]));
  7073. if (D)
  7074. Decls.insert(D);
  7075. }
  7076. UnusedLocalTypedefNameCandidates.clear();
  7077. }
  7078. void ASTReader::ReadReferencedSelectors(
  7079. SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
  7080. if (ReferencedSelectorsData.empty())
  7081. return;
  7082. // If there are @selector references added them to its pool. This is for
  7083. // implementation of -Wselector.
  7084. unsigned int DataSize = ReferencedSelectorsData.size()-1;
  7085. unsigned I = 0;
  7086. while (I < DataSize) {
  7087. Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
  7088. SourceLocation SelLoc
  7089. = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
  7090. Sels.push_back(std::make_pair(Sel, SelLoc));
  7091. }
  7092. ReferencedSelectorsData.clear();
  7093. }
  7094. void ASTReader::ReadWeakUndeclaredIdentifiers(
  7095. SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
  7096. if (WeakUndeclaredIdentifiers.empty())
  7097. return;
  7098. for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
  7099. IdentifierInfo *WeakId
  7100. = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
  7101. IdentifierInfo *AliasId
  7102. = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
  7103. SourceLocation Loc
  7104. = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
  7105. bool Used = WeakUndeclaredIdentifiers[I++];
  7106. WeakInfo WI(AliasId, Loc);
  7107. WI.setUsed(Used);
  7108. WeakIDs.push_back(std::make_pair(WeakId, WI));
  7109. }
  7110. WeakUndeclaredIdentifiers.clear();
  7111. }
  7112. void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
  7113. for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
  7114. ExternalVTableUse VT;
  7115. VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
  7116. VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
  7117. VT.DefinitionRequired = VTableUses[Idx++];
  7118. VTables.push_back(VT);
  7119. }
  7120. VTableUses.clear();
  7121. }
  7122. void ASTReader::ReadPendingInstantiations(
  7123. SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
  7124. for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
  7125. ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
  7126. SourceLocation Loc
  7127. = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
  7128. Pending.push_back(std::make_pair(D, Loc));
  7129. }
  7130. PendingInstantiations.clear();
  7131. }
  7132. void ASTReader::ReadLateParsedTemplates(
  7133. llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
  7134. &LPTMap) {
  7135. for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
  7136. /* In loop */) {
  7137. FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
  7138. auto LT = llvm::make_unique<LateParsedTemplate>();
  7139. LT->D = GetDecl(LateParsedTemplates[Idx++]);
  7140. ModuleFile *F = getOwningModuleFile(LT->D);
  7141. assert(F && "No module");
  7142. unsigned TokN = LateParsedTemplates[Idx++];
  7143. LT->Toks.reserve(TokN);
  7144. for (unsigned T = 0; T < TokN; ++T)
  7145. LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
  7146. LPTMap.insert(std::make_pair(FD, std::move(LT)));
  7147. }
  7148. LateParsedTemplates.clear();
  7149. }
  7150. void ASTReader::LoadSelector(Selector Sel) {
  7151. // It would be complicated to avoid reading the methods anyway. So don't.
  7152. ReadMethodPool(Sel);
  7153. }
  7154. void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
  7155. assert(ID && "Non-zero identifier ID required");
  7156. assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
  7157. IdentifiersLoaded[ID - 1] = II;
  7158. if (DeserializationListener)
  7159. DeserializationListener->IdentifierRead(ID, II);
  7160. }
  7161. /// Set the globally-visible declarations associated with the given
  7162. /// identifier.
  7163. ///
  7164. /// If the AST reader is currently in a state where the given declaration IDs
  7165. /// cannot safely be resolved, they are queued until it is safe to resolve
  7166. /// them.
  7167. ///
  7168. /// \param II an IdentifierInfo that refers to one or more globally-visible
  7169. /// declarations.
  7170. ///
  7171. /// \param DeclIDs the set of declaration IDs with the name @p II that are
  7172. /// visible at global scope.
  7173. ///
  7174. /// \param Decls if non-null, this vector will be populated with the set of
  7175. /// deserialized declarations. These declarations will not be pushed into
  7176. /// scope.
  7177. void
  7178. ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
  7179. const SmallVectorImpl<uint32_t> &DeclIDs,
  7180. SmallVectorImpl<Decl *> *Decls) {
  7181. if (NumCurrentElementsDeserializing && !Decls) {
  7182. PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
  7183. return;
  7184. }
  7185. for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
  7186. if (!SemaObj) {
  7187. // Queue this declaration so that it will be added to the
  7188. // translation unit scope and identifier's declaration chain
  7189. // once a Sema object is known.
  7190. PreloadedDeclIDs.push_back(DeclIDs[I]);
  7191. continue;
  7192. }
  7193. NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
  7194. // If we're simply supposed to record the declarations, do so now.
  7195. if (Decls) {
  7196. Decls->push_back(D);
  7197. continue;
  7198. }
  7199. // Introduce this declaration into the translation-unit scope
  7200. // and add it to the declaration chain for this identifier, so
  7201. // that (unqualified) name lookup will find it.
  7202. pushExternalDeclIntoScope(D, II);
  7203. }
  7204. }
  7205. IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
  7206. if (ID == 0)
  7207. return nullptr;
  7208. if (IdentifiersLoaded.empty()) {
  7209. Error("no identifier table in AST file");
  7210. return nullptr;
  7211. }
  7212. ID -= 1;
  7213. if (!IdentifiersLoaded[ID]) {
  7214. GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
  7215. assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
  7216. ModuleFile *M = I->second;
  7217. unsigned Index = ID - M->BaseIdentifierID;
  7218. const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
  7219. // All of the strings in the AST file are preceded by a 16-bit length.
  7220. // Extract that 16-bit length to avoid having to execute strlen().
  7221. // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
  7222. // unsigned integers. This is important to avoid integer overflow when
  7223. // we cast them to 'unsigned'.
  7224. const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
  7225. unsigned StrLen = (((unsigned) StrLenPtr[0])
  7226. | (((unsigned) StrLenPtr[1]) << 8)) - 1;
  7227. auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
  7228. IdentifiersLoaded[ID] = &II;
  7229. markIdentifierFromAST(*this, II);
  7230. if (DeserializationListener)
  7231. DeserializationListener->IdentifierRead(ID + 1, &II);
  7232. }
  7233. return IdentifiersLoaded[ID];
  7234. }
  7235. IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
  7236. return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
  7237. }
  7238. IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
  7239. if (LocalID < NUM_PREDEF_IDENT_IDS)
  7240. return LocalID;
  7241. if (!M.ModuleOffsetMap.empty())
  7242. ReadModuleOffsetMap(M);
  7243. ContinuousRangeMap<uint32_t, int, 2>::iterator I
  7244. = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
  7245. assert(I != M.IdentifierRemap.end()
  7246. && "Invalid index into identifier index remap");
  7247. return LocalID + I->second;
  7248. }
  7249. MacroInfo *ASTReader::getMacro(MacroID ID) {
  7250. if (ID == 0)
  7251. return nullptr;
  7252. if (MacrosLoaded.empty()) {
  7253. Error("no macro table in AST file");
  7254. return nullptr;
  7255. }
  7256. ID -= NUM_PREDEF_MACRO_IDS;
  7257. if (!MacrosLoaded[ID]) {
  7258. GlobalMacroMapType::iterator I
  7259. = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
  7260. assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
  7261. ModuleFile *M = I->second;
  7262. unsigned Index = ID - M->BaseMacroID;
  7263. MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
  7264. if (DeserializationListener)
  7265. DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
  7266. MacrosLoaded[ID]);
  7267. }
  7268. return MacrosLoaded[ID];
  7269. }
  7270. MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
  7271. if (LocalID < NUM_PREDEF_MACRO_IDS)
  7272. return LocalID;
  7273. if (!M.ModuleOffsetMap.empty())
  7274. ReadModuleOffsetMap(M);
  7275. ContinuousRangeMap<uint32_t, int, 2>::iterator I
  7276. = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
  7277. assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
  7278. return LocalID + I->second;
  7279. }
  7280. serialization::SubmoduleID
  7281. ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
  7282. if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
  7283. return LocalID;
  7284. if (!M.ModuleOffsetMap.empty())
  7285. ReadModuleOffsetMap(M);
  7286. ContinuousRangeMap<uint32_t, int, 2>::iterator I
  7287. = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
  7288. assert(I != M.SubmoduleRemap.end()
  7289. && "Invalid index into submodule index remap");
  7290. return LocalID + I->second;
  7291. }
  7292. Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
  7293. if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
  7294. assert(GlobalID == 0 && "Unhandled global submodule ID");
  7295. return nullptr;
  7296. }
  7297. if (GlobalID > SubmodulesLoaded.size()) {
  7298. Error("submodule ID out of range in AST file");
  7299. return nullptr;
  7300. }
  7301. return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
  7302. }
  7303. Module *ASTReader::getModule(unsigned ID) {
  7304. return getSubmodule(ID);
  7305. }
  7306. bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
  7307. ModuleFile *MF = getOwningModuleFile(D);
  7308. return MF && MF->PCHHasObjectFile;
  7309. }
  7310. ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
  7311. if (ID & 1) {
  7312. // It's a module, look it up by submodule ID.
  7313. auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
  7314. return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
  7315. } else {
  7316. // It's a prefix (preamble, PCH, ...). Look it up by index.
  7317. unsigned IndexFromEnd = ID >> 1;
  7318. assert(IndexFromEnd && "got reference to unknown module file");
  7319. return getModuleManager().pch_modules().end()[-IndexFromEnd];
  7320. }
  7321. }
  7322. unsigned ASTReader::getModuleFileID(ModuleFile *F) {
  7323. if (!F)
  7324. return 1;
  7325. // For a file representing a module, use the submodule ID of the top-level
  7326. // module as the file ID. For any other kind of file, the number of such
  7327. // files loaded beforehand will be the same on reload.
  7328. // FIXME: Is this true even if we have an explicit module file and a PCH?
  7329. if (F->isModule())
  7330. return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
  7331. auto PCHModules = getModuleManager().pch_modules();
  7332. auto I = std::find(PCHModules.begin(), PCHModules.end(), F);
  7333. assert(I != PCHModules.end() && "emitting reference to unknown file");
  7334. return (I - PCHModules.end()) << 1;
  7335. }
  7336. llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
  7337. ASTReader::getSourceDescriptor(unsigned ID) {
  7338. if (const Module *M = getSubmodule(ID))
  7339. return ExternalASTSource::ASTSourceDescriptor(*M);
  7340. // If there is only a single PCH, return it instead.
  7341. // Chained PCH are not supported.
  7342. const auto &PCHChain = ModuleMgr.pch_modules();
  7343. if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
  7344. ModuleFile &MF = ModuleMgr.getPrimaryModule();
  7345. StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
  7346. StringRef FileName = llvm::sys::path::filename(MF.FileName);
  7347. return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
  7348. MF.Signature);
  7349. }
  7350. return None;
  7351. }
  7352. ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
  7353. auto I = DefinitionSource.find(FD);
  7354. if (I == DefinitionSource.end())
  7355. return EK_ReplyHazy;
  7356. return I->second ? EK_Never : EK_Always;
  7357. }
  7358. Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
  7359. return DecodeSelector(getGlobalSelectorID(M, LocalID));
  7360. }
  7361. Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
  7362. if (ID == 0)
  7363. return Selector();
  7364. if (ID > SelectorsLoaded.size()) {
  7365. Error("selector ID out of range in AST file");
  7366. return Selector();
  7367. }
  7368. if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
  7369. // Load this selector from the selector table.
  7370. GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
  7371. assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
  7372. ModuleFile &M = *I->second;
  7373. ASTSelectorLookupTrait Trait(*this, M);
  7374. unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
  7375. SelectorsLoaded[ID - 1] =
  7376. Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
  7377. if (DeserializationListener)
  7378. DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
  7379. }
  7380. return SelectorsLoaded[ID - 1];
  7381. }
  7382. Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
  7383. return DecodeSelector(ID);
  7384. }
  7385. uint32_t ASTReader::GetNumExternalSelectors() {
  7386. // ID 0 (the null selector) is considered an external selector.
  7387. return getTotalNumSelectors() + 1;
  7388. }
  7389. serialization::SelectorID
  7390. ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
  7391. if (LocalID < NUM_PREDEF_SELECTOR_IDS)
  7392. return LocalID;
  7393. if (!M.ModuleOffsetMap.empty())
  7394. ReadModuleOffsetMap(M);
  7395. ContinuousRangeMap<uint32_t, int, 2>::iterator I
  7396. = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
  7397. assert(I != M.SelectorRemap.end()
  7398. && "Invalid index into selector index remap");
  7399. return LocalID + I->second;
  7400. }
  7401. DeclarationName
  7402. ASTReader::ReadDeclarationName(ModuleFile &F,
  7403. const RecordData &Record, unsigned &Idx) {
  7404. ASTContext &Context = getContext();
  7405. DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
  7406. switch (Kind) {
  7407. case DeclarationName::Identifier:
  7408. return DeclarationName(GetIdentifierInfo(F, Record, Idx));
  7409. case DeclarationName::ObjCZeroArgSelector:
  7410. case DeclarationName::ObjCOneArgSelector:
  7411. case DeclarationName::ObjCMultiArgSelector:
  7412. return DeclarationName(ReadSelector(F, Record, Idx));
  7413. case DeclarationName::CXXConstructorName:
  7414. return Context.DeclarationNames.getCXXConstructorName(
  7415. Context.getCanonicalType(readType(F, Record, Idx)));
  7416. case DeclarationName::CXXDestructorName:
  7417. return Context.DeclarationNames.getCXXDestructorName(
  7418. Context.getCanonicalType(readType(F, Record, Idx)));
  7419. case DeclarationName::CXXDeductionGuideName:
  7420. return Context.DeclarationNames.getCXXDeductionGuideName(
  7421. ReadDeclAs<TemplateDecl>(F, Record, Idx));
  7422. case DeclarationName::CXXConversionFunctionName:
  7423. return Context.DeclarationNames.getCXXConversionFunctionName(
  7424. Context.getCanonicalType(readType(F, Record, Idx)));
  7425. case DeclarationName::CXXOperatorName:
  7426. return Context.DeclarationNames.getCXXOperatorName(
  7427. (OverloadedOperatorKind)Record[Idx++]);
  7428. case DeclarationName::CXXLiteralOperatorName:
  7429. return Context.DeclarationNames.getCXXLiteralOperatorName(
  7430. GetIdentifierInfo(F, Record, Idx));
  7431. case DeclarationName::CXXUsingDirective:
  7432. return DeclarationName::getUsingDirectiveName();
  7433. }
  7434. llvm_unreachable("Invalid NameKind!");
  7435. }
  7436. void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
  7437. DeclarationNameLoc &DNLoc,
  7438. DeclarationName Name,
  7439. const RecordData &Record, unsigned &Idx) {
  7440. switch (Name.getNameKind()) {
  7441. case DeclarationName::CXXConstructorName:
  7442. case DeclarationName::CXXDestructorName:
  7443. case DeclarationName::CXXConversionFunctionName:
  7444. DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
  7445. break;
  7446. case DeclarationName::CXXOperatorName:
  7447. DNLoc.CXXOperatorName.BeginOpNameLoc
  7448. = ReadSourceLocation(F, Record, Idx).getRawEncoding();
  7449. DNLoc.CXXOperatorName.EndOpNameLoc
  7450. = ReadSourceLocation(F, Record, Idx).getRawEncoding();
  7451. break;
  7452. case DeclarationName::CXXLiteralOperatorName:
  7453. DNLoc.CXXLiteralOperatorName.OpNameLoc
  7454. = ReadSourceLocation(F, Record, Idx).getRawEncoding();
  7455. break;
  7456. case DeclarationName::Identifier:
  7457. case DeclarationName::ObjCZeroArgSelector:
  7458. case DeclarationName::ObjCOneArgSelector:
  7459. case DeclarationName::ObjCMultiArgSelector:
  7460. case DeclarationName::CXXUsingDirective:
  7461. case DeclarationName::CXXDeductionGuideName:
  7462. break;
  7463. }
  7464. }
  7465. void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
  7466. DeclarationNameInfo &NameInfo,
  7467. const RecordData &Record, unsigned &Idx) {
  7468. NameInfo.setName(ReadDeclarationName(F, Record, Idx));
  7469. NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
  7470. DeclarationNameLoc DNLoc;
  7471. ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
  7472. NameInfo.setInfo(DNLoc);
  7473. }
  7474. void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
  7475. const RecordData &Record, unsigned &Idx) {
  7476. Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
  7477. unsigned NumTPLists = Record[Idx++];
  7478. Info.NumTemplParamLists = NumTPLists;
  7479. if (NumTPLists) {
  7480. Info.TemplParamLists =
  7481. new (getContext()) TemplateParameterList *[NumTPLists];
  7482. for (unsigned i = 0; i != NumTPLists; ++i)
  7483. Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
  7484. }
  7485. }
  7486. TemplateName
  7487. ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
  7488. unsigned &Idx) {
  7489. ASTContext &Context = getContext();
  7490. TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
  7491. switch (Kind) {
  7492. case TemplateName::Template:
  7493. return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
  7494. case TemplateName::OverloadedTemplate: {
  7495. unsigned size = Record[Idx++];
  7496. UnresolvedSet<8> Decls;
  7497. while (size--)
  7498. Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
  7499. return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
  7500. }
  7501. case TemplateName::QualifiedTemplate: {
  7502. NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
  7503. bool hasTemplKeyword = Record[Idx++];
  7504. TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
  7505. return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
  7506. }
  7507. case TemplateName::DependentTemplate: {
  7508. NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
  7509. if (Record[Idx++]) // isIdentifier
  7510. return Context.getDependentTemplateName(NNS,
  7511. GetIdentifierInfo(F, Record,
  7512. Idx));
  7513. return Context.getDependentTemplateName(NNS,
  7514. (OverloadedOperatorKind)Record[Idx++]);
  7515. }
  7516. case TemplateName::SubstTemplateTemplateParm: {
  7517. TemplateTemplateParmDecl *param
  7518. = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
  7519. if (!param) return TemplateName();
  7520. TemplateName replacement = ReadTemplateName(F, Record, Idx);
  7521. return Context.getSubstTemplateTemplateParm(param, replacement);
  7522. }
  7523. case TemplateName::SubstTemplateTemplateParmPack: {
  7524. TemplateTemplateParmDecl *Param
  7525. = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
  7526. if (!Param)
  7527. return TemplateName();
  7528. TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
  7529. if (ArgPack.getKind() != TemplateArgument::Pack)
  7530. return TemplateName();
  7531. return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
  7532. }
  7533. }
  7534. llvm_unreachable("Unhandled template name kind!");
  7535. }
  7536. TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
  7537. const RecordData &Record,
  7538. unsigned &Idx,
  7539. bool Canonicalize) {
  7540. ASTContext &Context = getContext();
  7541. if (Canonicalize) {
  7542. // The caller wants a canonical template argument. Sometimes the AST only
  7543. // wants template arguments in canonical form (particularly as the template
  7544. // argument lists of template specializations) so ensure we preserve that
  7545. // canonical form across serialization.
  7546. TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
  7547. return Context.getCanonicalTemplateArgument(Arg);
  7548. }
  7549. TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
  7550. switch (Kind) {
  7551. case TemplateArgument::Null:
  7552. return TemplateArgument();
  7553. case TemplateArgument::Type:
  7554. return TemplateArgument(readType(F, Record, Idx));
  7555. case TemplateArgument::Declaration: {
  7556. ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
  7557. return TemplateArgument(D, readType(F, Record, Idx));
  7558. }
  7559. case TemplateArgument::NullPtr:
  7560. return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
  7561. case TemplateArgument::Integral: {
  7562. llvm::APSInt Value = ReadAPSInt(Record, Idx);
  7563. QualType T = readType(F, Record, Idx);
  7564. return TemplateArgument(Context, Value, T);
  7565. }
  7566. case TemplateArgument::Template:
  7567. return TemplateArgument(ReadTemplateName(F, Record, Idx));
  7568. case TemplateArgument::TemplateExpansion: {
  7569. TemplateName Name = ReadTemplateName(F, Record, Idx);
  7570. Optional<unsigned> NumTemplateExpansions;
  7571. if (unsigned NumExpansions = Record[Idx++])
  7572. NumTemplateExpansions = NumExpansions - 1;
  7573. return TemplateArgument(Name, NumTemplateExpansions);
  7574. }
  7575. case TemplateArgument::Expression:
  7576. return TemplateArgument(ReadExpr(F));
  7577. case TemplateArgument::Pack: {
  7578. unsigned NumArgs = Record[Idx++];
  7579. TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
  7580. for (unsigned I = 0; I != NumArgs; ++I)
  7581. Args[I] = ReadTemplateArgument(F, Record, Idx);
  7582. return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
  7583. }
  7584. }
  7585. llvm_unreachable("Unhandled template argument kind!");
  7586. }
  7587. TemplateParameterList *
  7588. ASTReader::ReadTemplateParameterList(ModuleFile &F,
  7589. const RecordData &Record, unsigned &Idx) {
  7590. SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
  7591. SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
  7592. SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
  7593. unsigned NumParams = Record[Idx++];
  7594. SmallVector<NamedDecl *, 16> Params;
  7595. Params.reserve(NumParams);
  7596. while (NumParams--)
  7597. Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
  7598. // TODO: Concepts
  7599. TemplateParameterList *TemplateParams = TemplateParameterList::Create(
  7600. getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, nullptr);
  7601. return TemplateParams;
  7602. }
  7603. void
  7604. ASTReader::
  7605. ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
  7606. ModuleFile &F, const RecordData &Record,
  7607. unsigned &Idx, bool Canonicalize) {
  7608. unsigned NumTemplateArgs = Record[Idx++];
  7609. TemplArgs.reserve(NumTemplateArgs);
  7610. while (NumTemplateArgs--)
  7611. TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
  7612. }
  7613. /// Read a UnresolvedSet structure.
  7614. void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
  7615. const RecordData &Record, unsigned &Idx) {
  7616. unsigned NumDecls = Record[Idx++];
  7617. Set.reserve(getContext(), NumDecls);
  7618. while (NumDecls--) {
  7619. DeclID ID = ReadDeclID(F, Record, Idx);
  7620. AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
  7621. Set.addLazyDecl(getContext(), ID, AS);
  7622. }
  7623. }
  7624. CXXBaseSpecifier
  7625. ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
  7626. const RecordData &Record, unsigned &Idx) {
  7627. bool isVirtual = static_cast<bool>(Record[Idx++]);
  7628. bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
  7629. AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
  7630. bool inheritConstructors = static_cast<bool>(Record[Idx++]);
  7631. TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
  7632. SourceRange Range = ReadSourceRange(F, Record, Idx);
  7633. SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
  7634. CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
  7635. EllipsisLoc);
  7636. Result.setInheritConstructors(inheritConstructors);
  7637. return Result;
  7638. }
  7639. CXXCtorInitializer **
  7640. ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
  7641. unsigned &Idx) {
  7642. ASTContext &Context = getContext();
  7643. unsigned NumInitializers = Record[Idx++];
  7644. assert(NumInitializers && "wrote ctor initializers but have no inits");
  7645. auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
  7646. for (unsigned i = 0; i != NumInitializers; ++i) {
  7647. TypeSourceInfo *TInfo = nullptr;
  7648. bool IsBaseVirtual = false;
  7649. FieldDecl *Member = nullptr;
  7650. IndirectFieldDecl *IndirectMember = nullptr;
  7651. CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
  7652. switch (Type) {
  7653. case CTOR_INITIALIZER_BASE:
  7654. TInfo = GetTypeSourceInfo(F, Record, Idx);
  7655. IsBaseVirtual = Record[Idx++];
  7656. break;
  7657. case CTOR_INITIALIZER_DELEGATING:
  7658. TInfo = GetTypeSourceInfo(F, Record, Idx);
  7659. break;
  7660. case CTOR_INITIALIZER_MEMBER:
  7661. Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
  7662. break;
  7663. case CTOR_INITIALIZER_INDIRECT_MEMBER:
  7664. IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
  7665. break;
  7666. }
  7667. SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
  7668. Expr *Init = ReadExpr(F);
  7669. SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
  7670. SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
  7671. CXXCtorInitializer *BOMInit;
  7672. if (Type == CTOR_INITIALIZER_BASE)
  7673. BOMInit = new (Context)
  7674. CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
  7675. RParenLoc, MemberOrEllipsisLoc);
  7676. else if (Type == CTOR_INITIALIZER_DELEGATING)
  7677. BOMInit = new (Context)
  7678. CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
  7679. else if (Member)
  7680. BOMInit = new (Context)
  7681. CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
  7682. Init, RParenLoc);
  7683. else
  7684. BOMInit = new (Context)
  7685. CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
  7686. LParenLoc, Init, RParenLoc);
  7687. if (/*IsWritten*/Record[Idx++]) {
  7688. unsigned SourceOrder = Record[Idx++];
  7689. BOMInit->setSourceOrder(SourceOrder);
  7690. }
  7691. CtorInitializers[i] = BOMInit;
  7692. }
  7693. return CtorInitializers;
  7694. }
  7695. NestedNameSpecifier *
  7696. ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
  7697. const RecordData &Record, unsigned &Idx) {
  7698. ASTContext &Context = getContext();
  7699. unsigned N = Record[Idx++];
  7700. NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
  7701. for (unsigned I = 0; I != N; ++I) {
  7702. NestedNameSpecifier::SpecifierKind Kind
  7703. = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
  7704. switch (Kind) {
  7705. case NestedNameSpecifier::Identifier: {
  7706. IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
  7707. NNS = NestedNameSpecifier::Create(Context, Prev, II);
  7708. break;
  7709. }
  7710. case NestedNameSpecifier::Namespace: {
  7711. NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
  7712. NNS = NestedNameSpecifier::Create(Context, Prev, NS);
  7713. break;
  7714. }
  7715. case NestedNameSpecifier::NamespaceAlias: {
  7716. NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
  7717. NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
  7718. break;
  7719. }
  7720. case NestedNameSpecifier::TypeSpec:
  7721. case NestedNameSpecifier::TypeSpecWithTemplate: {
  7722. const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
  7723. if (!T)
  7724. return nullptr;
  7725. bool Template = Record[Idx++];
  7726. NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
  7727. break;
  7728. }
  7729. case NestedNameSpecifier::Global:
  7730. NNS = NestedNameSpecifier::GlobalSpecifier(Context);
  7731. // No associated value, and there can't be a prefix.
  7732. break;
  7733. case NestedNameSpecifier::Super: {
  7734. CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
  7735. NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
  7736. break;
  7737. }
  7738. }
  7739. Prev = NNS;
  7740. }
  7741. return NNS;
  7742. }
  7743. NestedNameSpecifierLoc
  7744. ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
  7745. unsigned &Idx) {
  7746. ASTContext &Context = getContext();
  7747. unsigned N = Record[Idx++];
  7748. NestedNameSpecifierLocBuilder Builder;
  7749. for (unsigned I = 0; I != N; ++I) {
  7750. NestedNameSpecifier::SpecifierKind Kind
  7751. = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
  7752. switch (Kind) {
  7753. case NestedNameSpecifier::Identifier: {
  7754. IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
  7755. SourceRange Range = ReadSourceRange(F, Record, Idx);
  7756. Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
  7757. break;
  7758. }
  7759. case NestedNameSpecifier::Namespace: {
  7760. NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
  7761. SourceRange Range = ReadSourceRange(F, Record, Idx);
  7762. Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
  7763. break;
  7764. }
  7765. case NestedNameSpecifier::NamespaceAlias: {
  7766. NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
  7767. SourceRange Range = ReadSourceRange(F, Record, Idx);
  7768. Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
  7769. break;
  7770. }
  7771. case NestedNameSpecifier::TypeSpec:
  7772. case NestedNameSpecifier::TypeSpecWithTemplate: {
  7773. bool Template = Record[Idx++];
  7774. TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
  7775. if (!T)
  7776. return NestedNameSpecifierLoc();
  7777. SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
  7778. // FIXME: 'template' keyword location not saved anywhere, so we fake it.
  7779. Builder.Extend(Context,
  7780. Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
  7781. T->getTypeLoc(), ColonColonLoc);
  7782. break;
  7783. }
  7784. case NestedNameSpecifier::Global: {
  7785. SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
  7786. Builder.MakeGlobal(Context, ColonColonLoc);
  7787. break;
  7788. }
  7789. case NestedNameSpecifier::Super: {
  7790. CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
  7791. SourceRange Range = ReadSourceRange(F, Record, Idx);
  7792. Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
  7793. break;
  7794. }
  7795. }
  7796. }
  7797. return Builder.getWithLocInContext(Context);
  7798. }
  7799. SourceRange
  7800. ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
  7801. unsigned &Idx) {
  7802. SourceLocation beg = ReadSourceLocation(F, Record, Idx);
  7803. SourceLocation end = ReadSourceLocation(F, Record, Idx);
  7804. return SourceRange(beg, end);
  7805. }
  7806. /// Read an integral value
  7807. llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
  7808. unsigned BitWidth = Record[Idx++];
  7809. unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
  7810. llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
  7811. Idx += NumWords;
  7812. return Result;
  7813. }
  7814. /// Read a signed integral value
  7815. llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
  7816. bool isUnsigned = Record[Idx++];
  7817. return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
  7818. }
  7819. /// Read a floating-point value
  7820. llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
  7821. const llvm::fltSemantics &Sem,
  7822. unsigned &Idx) {
  7823. return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
  7824. }
  7825. // Read a string
  7826. std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
  7827. unsigned Len = Record[Idx++];
  7828. std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
  7829. Idx += Len;
  7830. return Result;
  7831. }
  7832. std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
  7833. unsigned &Idx) {
  7834. std::string Filename = ReadString(Record, Idx);
  7835. ResolveImportedPath(F, Filename);
  7836. return Filename;
  7837. }
  7838. VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
  7839. unsigned &Idx) {
  7840. unsigned Major = Record[Idx++];
  7841. unsigned Minor = Record[Idx++];
  7842. unsigned Subminor = Record[Idx++];
  7843. if (Minor == 0)
  7844. return VersionTuple(Major);
  7845. if (Subminor == 0)
  7846. return VersionTuple(Major, Minor - 1);
  7847. return VersionTuple(Major, Minor - 1, Subminor - 1);
  7848. }
  7849. CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
  7850. const RecordData &Record,
  7851. unsigned &Idx) {
  7852. CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
  7853. return CXXTemporary::Create(getContext(), Decl);
  7854. }
  7855. DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
  7856. return Diag(CurrentImportLoc, DiagID);
  7857. }
  7858. DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
  7859. return Diags.Report(Loc, DiagID);
  7860. }
  7861. /// Retrieve the identifier table associated with the
  7862. /// preprocessor.
  7863. IdentifierTable &ASTReader::getIdentifierTable() {
  7864. return PP.getIdentifierTable();
  7865. }
  7866. /// Record that the given ID maps to the given switch-case
  7867. /// statement.
  7868. void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
  7869. assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
  7870. "Already have a SwitchCase with this ID");
  7871. (*CurrSwitchCaseStmts)[ID] = SC;
  7872. }
  7873. /// Retrieve the switch-case statement with the given ID.
  7874. SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
  7875. assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
  7876. return (*CurrSwitchCaseStmts)[ID];
  7877. }
  7878. void ASTReader::ClearSwitchCaseIDs() {
  7879. CurrSwitchCaseStmts->clear();
  7880. }
  7881. void ASTReader::ReadComments() {
  7882. ASTContext &Context = getContext();
  7883. std::vector<RawComment *> Comments;
  7884. for (SmallVectorImpl<std::pair<BitstreamCursor,
  7885. serialization::ModuleFile *>>::iterator
  7886. I = CommentsCursors.begin(),
  7887. E = CommentsCursors.end();
  7888. I != E; ++I) {
  7889. Comments.clear();
  7890. BitstreamCursor &Cursor = I->first;
  7891. serialization::ModuleFile &F = *I->second;
  7892. SavedStreamPosition SavedPosition(Cursor);
  7893. RecordData Record;
  7894. while (true) {
  7895. llvm::BitstreamEntry Entry =
  7896. Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
  7897. switch (Entry.Kind) {
  7898. case llvm::BitstreamEntry::SubBlock: // Handled for us already.
  7899. case llvm::BitstreamEntry::Error:
  7900. Error("malformed block record in AST file");
  7901. return;
  7902. case llvm::BitstreamEntry::EndBlock:
  7903. goto NextCursor;
  7904. case llvm::BitstreamEntry::Record:
  7905. // The interesting case.
  7906. break;
  7907. }
  7908. // Read a record.
  7909. Record.clear();
  7910. switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
  7911. case COMMENTS_RAW_COMMENT: {
  7912. unsigned Idx = 0;
  7913. SourceRange SR = ReadSourceRange(F, Record, Idx);
  7914. RawComment::CommentKind Kind =
  7915. (RawComment::CommentKind) Record[Idx++];
  7916. bool IsTrailingComment = Record[Idx++];
  7917. bool IsAlmostTrailingComment = Record[Idx++];
  7918. Comments.push_back(new (Context) RawComment(
  7919. SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
  7920. break;
  7921. }
  7922. }
  7923. }
  7924. NextCursor:
  7925. // De-serialized SourceLocations get negative FileIDs for other modules,
  7926. // potentially invalidating the original order. Sort it again.
  7927. llvm::sort(Comments, BeforeThanCompare<RawComment>(SourceMgr));
  7928. Context.Comments.addDeserializedComments(Comments);
  7929. }
  7930. }
  7931. void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
  7932. bool IncludeSystem, bool Complain,
  7933. llvm::function_ref<void(const serialization::InputFile &IF,
  7934. bool isSystem)> Visitor) {
  7935. unsigned NumUserInputs = MF.NumUserInputFiles;
  7936. unsigned NumInputs = MF.InputFilesLoaded.size();
  7937. assert(NumUserInputs <= NumInputs);
  7938. unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
  7939. for (unsigned I = 0; I < N; ++I) {
  7940. bool IsSystem = I >= NumUserInputs;
  7941. InputFile IF = getInputFile(MF, I+1, Complain);
  7942. Visitor(IF, IsSystem);
  7943. }
  7944. }
  7945. void ASTReader::visitTopLevelModuleMaps(
  7946. serialization::ModuleFile &MF,
  7947. llvm::function_ref<void(const FileEntry *FE)> Visitor) {
  7948. unsigned NumInputs = MF.InputFilesLoaded.size();
  7949. for (unsigned I = 0; I < NumInputs; ++I) {
  7950. InputFileInfo IFI = readInputFileInfo(MF, I + 1);
  7951. if (IFI.TopLevelModuleMap)
  7952. // FIXME: This unnecessarily re-reads the InputFileInfo.
  7953. if (auto *FE = getInputFile(MF, I + 1).getFile())
  7954. Visitor(FE);
  7955. }
  7956. }
  7957. std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
  7958. // If we know the owning module, use it.
  7959. if (Module *M = D->getImportedOwningModule())
  7960. return M->getFullModuleName();
  7961. // Otherwise, use the name of the top-level module the decl is within.
  7962. if (ModuleFile *M = getOwningModuleFile(D))
  7963. return M->ModuleName;
  7964. // Not from a module.
  7965. return {};
  7966. }
  7967. void ASTReader::finishPendingActions() {
  7968. while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
  7969. !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
  7970. !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
  7971. !PendingUpdateRecords.empty()) {
  7972. // If any identifiers with corresponding top-level declarations have
  7973. // been loaded, load those declarations now.
  7974. using TopLevelDeclsMap =
  7975. llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
  7976. TopLevelDeclsMap TopLevelDecls;
  7977. while (!PendingIdentifierInfos.empty()) {
  7978. IdentifierInfo *II = PendingIdentifierInfos.back().first;
  7979. SmallVector<uint32_t, 4> DeclIDs =
  7980. std::move(PendingIdentifierInfos.back().second);
  7981. PendingIdentifierInfos.pop_back();
  7982. SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
  7983. }
  7984. // Load each function type that we deferred loading because it was a
  7985. // deduced type that might refer to a local type declared within itself.
  7986. for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
  7987. auto *FD = PendingFunctionTypes[I].first;
  7988. FD->setType(GetType(PendingFunctionTypes[I].second));
  7989. // If we gave a function a deduced return type, remember that we need to
  7990. // propagate that along the redeclaration chain.
  7991. auto *DT = FD->getReturnType()->getContainedDeducedType();
  7992. if (DT && DT->isDeduced())
  7993. PendingDeducedTypeUpdates.insert(
  7994. {FD->getCanonicalDecl(), FD->getReturnType()});
  7995. }
  7996. PendingFunctionTypes.clear();
  7997. // For each decl chain that we wanted to complete while deserializing, mark
  7998. // it as "still needs to be completed".
  7999. for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
  8000. markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
  8001. }
  8002. PendingIncompleteDeclChains.clear();
  8003. // Load pending declaration chains.
  8004. for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
  8005. loadPendingDeclChain(PendingDeclChains[I].first,
  8006. PendingDeclChains[I].second);
  8007. PendingDeclChains.clear();
  8008. // Make the most recent of the top-level declarations visible.
  8009. for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
  8010. TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
  8011. IdentifierInfo *II = TLD->first;
  8012. for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
  8013. pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
  8014. }
  8015. }
  8016. // Load any pending macro definitions.
  8017. for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
  8018. IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
  8019. SmallVector<PendingMacroInfo, 2> GlobalIDs;
  8020. GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
  8021. // Initialize the macro history from chained-PCHs ahead of module imports.
  8022. for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
  8023. ++IDIdx) {
  8024. const PendingMacroInfo &Info = GlobalIDs[IDIdx];
  8025. if (!Info.M->isModule())
  8026. resolvePendingMacro(II, Info);
  8027. }
  8028. // Handle module imports.
  8029. for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
  8030. ++IDIdx) {
  8031. const PendingMacroInfo &Info = GlobalIDs[IDIdx];
  8032. if (Info.M->isModule())
  8033. resolvePendingMacro(II, Info);
  8034. }
  8035. }
  8036. PendingMacroIDs.clear();
  8037. // Wire up the DeclContexts for Decls that we delayed setting until
  8038. // recursive loading is completed.
  8039. while (!PendingDeclContextInfos.empty()) {
  8040. PendingDeclContextInfo Info = PendingDeclContextInfos.front();
  8041. PendingDeclContextInfos.pop_front();
  8042. DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
  8043. DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
  8044. Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
  8045. }
  8046. // Perform any pending declaration updates.
  8047. while (!PendingUpdateRecords.empty()) {
  8048. auto Update = PendingUpdateRecords.pop_back_val();
  8049. ReadingKindTracker ReadingKind(Read_Decl, *this);
  8050. loadDeclUpdateRecords(Update);
  8051. }
  8052. }
  8053. // At this point, all update records for loaded decls are in place, so any
  8054. // fake class definitions should have become real.
  8055. assert(PendingFakeDefinitionData.empty() &&
  8056. "faked up a class definition but never saw the real one");
  8057. // If we deserialized any C++ or Objective-C class definitions, any
  8058. // Objective-C protocol definitions, or any redeclarable templates, make sure
  8059. // that all redeclarations point to the definitions. Note that this can only
  8060. // happen now, after the redeclaration chains have been fully wired.
  8061. for (Decl *D : PendingDefinitions) {
  8062. if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
  8063. if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
  8064. // Make sure that the TagType points at the definition.
  8065. const_cast<TagType*>(TagT)->decl = TD;
  8066. }
  8067. if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
  8068. for (auto *R = getMostRecentExistingDecl(RD); R;
  8069. R = R->getPreviousDecl()) {
  8070. assert((R == D) ==
  8071. cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
  8072. "declaration thinks it's the definition but it isn't");
  8073. cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
  8074. }
  8075. }
  8076. continue;
  8077. }
  8078. if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
  8079. // Make sure that the ObjCInterfaceType points at the definition.
  8080. const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
  8081. ->Decl = ID;
  8082. for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
  8083. cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
  8084. continue;
  8085. }
  8086. if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
  8087. for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
  8088. cast<ObjCProtocolDecl>(R)->Data = PD->Data;
  8089. continue;
  8090. }
  8091. auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
  8092. for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
  8093. cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
  8094. }
  8095. PendingDefinitions.clear();
  8096. // Load the bodies of any functions or methods we've encountered. We do
  8097. // this now (delayed) so that we can be sure that the declaration chains
  8098. // have been fully wired up (hasBody relies on this).
  8099. // FIXME: We shouldn't require complete redeclaration chains here.
  8100. for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
  8101. PBEnd = PendingBodies.end();
  8102. PB != PBEnd; ++PB) {
  8103. if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
  8104. // For a function defined inline within a class template, force the
  8105. // canonical definition to be the one inside the canonical definition of
  8106. // the template. This ensures that we instantiate from a correct view
  8107. // of the template.
  8108. //
  8109. // Sadly we can't do this more generally: we can't be sure that all
  8110. // copies of an arbitrary class definition will have the same members
  8111. // defined (eg, some member functions may not be instantiated, and some
  8112. // special members may or may not have been implicitly defined).
  8113. if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
  8114. if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
  8115. continue;
  8116. // FIXME: Check for =delete/=default?
  8117. // FIXME: Complain about ODR violations here?
  8118. const FunctionDecl *Defn = nullptr;
  8119. if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
  8120. FD->setLazyBody(PB->second);
  8121. } else {
  8122. auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
  8123. mergeDefinitionVisibility(NonConstDefn, FD);
  8124. if (!FD->isLateTemplateParsed() &&
  8125. !NonConstDefn->isLateTemplateParsed() &&
  8126. FD->getODRHash() != NonConstDefn->getODRHash()) {
  8127. if (!isa<CXXMethodDecl>(FD)) {
  8128. PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
  8129. } else if (FD->getLexicalParent()->isFileContext() &&
  8130. NonConstDefn->getLexicalParent()->isFileContext()) {
  8131. // Only diagnose out-of-line method definitions. If they are
  8132. // in class definitions, then an error will be generated when
  8133. // processing the class bodies.
  8134. PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
  8135. }
  8136. }
  8137. }
  8138. continue;
  8139. }
  8140. ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
  8141. if (!getContext().getLangOpts().Modules || !MD->hasBody())
  8142. MD->setLazyBody(PB->second);
  8143. }
  8144. PendingBodies.clear();
  8145. // Do some cleanup.
  8146. for (auto *ND : PendingMergedDefinitionsToDeduplicate)
  8147. getContext().deduplicateMergedDefinitonsFor(ND);
  8148. PendingMergedDefinitionsToDeduplicate.clear();
  8149. }
  8150. void ASTReader::diagnoseOdrViolations() {
  8151. if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
  8152. PendingFunctionOdrMergeFailures.empty() &&
  8153. PendingEnumOdrMergeFailures.empty())
  8154. return;
  8155. // Trigger the import of the full definition of each class that had any
  8156. // odr-merging problems, so we can produce better diagnostics for them.
  8157. // These updates may in turn find and diagnose some ODR failures, so take
  8158. // ownership of the set first.
  8159. auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
  8160. PendingOdrMergeFailures.clear();
  8161. for (auto &Merge : OdrMergeFailures) {
  8162. Merge.first->buildLookup();
  8163. Merge.first->decls_begin();
  8164. Merge.first->bases_begin();
  8165. Merge.first->vbases_begin();
  8166. for (auto &RecordPair : Merge.second) {
  8167. auto *RD = RecordPair.first;
  8168. RD->decls_begin();
  8169. RD->bases_begin();
  8170. RD->vbases_begin();
  8171. }
  8172. }
  8173. // Trigger the import of functions.
  8174. auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
  8175. PendingFunctionOdrMergeFailures.clear();
  8176. for (auto &Merge : FunctionOdrMergeFailures) {
  8177. Merge.first->buildLookup();
  8178. Merge.first->decls_begin();
  8179. Merge.first->getBody();
  8180. for (auto &FD : Merge.second) {
  8181. FD->buildLookup();
  8182. FD->decls_begin();
  8183. FD->getBody();
  8184. }
  8185. }
  8186. // Trigger the import of enums.
  8187. auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
  8188. PendingEnumOdrMergeFailures.clear();
  8189. for (auto &Merge : EnumOdrMergeFailures) {
  8190. Merge.first->decls_begin();
  8191. for (auto &Enum : Merge.second) {
  8192. Enum->decls_begin();
  8193. }
  8194. }
  8195. // For each declaration from a merged context, check that the canonical
  8196. // definition of that context also contains a declaration of the same
  8197. // entity.
  8198. //
  8199. // Caution: this loop does things that might invalidate iterators into
  8200. // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
  8201. while (!PendingOdrMergeChecks.empty()) {
  8202. NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
  8203. // FIXME: Skip over implicit declarations for now. This matters for things
  8204. // like implicitly-declared special member functions. This isn't entirely
  8205. // correct; we can end up with multiple unmerged declarations of the same
  8206. // implicit entity.
  8207. if (D->isImplicit())
  8208. continue;
  8209. DeclContext *CanonDef = D->getDeclContext();
  8210. bool Found = false;
  8211. const Decl *DCanon = D->getCanonicalDecl();
  8212. for (auto RI : D->redecls()) {
  8213. if (RI->getLexicalDeclContext() == CanonDef) {
  8214. Found = true;
  8215. break;
  8216. }
  8217. }
  8218. if (Found)
  8219. continue;
  8220. // Quick check failed, time to do the slow thing. Note, we can't just
  8221. // look up the name of D in CanonDef here, because the member that is
  8222. // in CanonDef might not be found by name lookup (it might have been
  8223. // replaced by a more recent declaration in the lookup table), and we
  8224. // can't necessarily find it in the redeclaration chain because it might
  8225. // be merely mergeable, not redeclarable.
  8226. llvm::SmallVector<const NamedDecl*, 4> Candidates;
  8227. for (auto *CanonMember : CanonDef->decls()) {
  8228. if (CanonMember->getCanonicalDecl() == DCanon) {
  8229. // This can happen if the declaration is merely mergeable and not
  8230. // actually redeclarable (we looked for redeclarations earlier).
  8231. //
  8232. // FIXME: We should be able to detect this more efficiently, without
  8233. // pulling in all of the members of CanonDef.
  8234. Found = true;
  8235. break;
  8236. }
  8237. if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
  8238. if (ND->getDeclName() == D->getDeclName())
  8239. Candidates.push_back(ND);
  8240. }
  8241. if (!Found) {
  8242. // The AST doesn't like TagDecls becoming invalid after they've been
  8243. // completed. We only really need to mark FieldDecls as invalid here.
  8244. if (!isa<TagDecl>(D))
  8245. D->setInvalidDecl();
  8246. // Ensure we don't accidentally recursively enter deserialization while
  8247. // we're producing our diagnostic.
  8248. Deserializing RecursionGuard(this);
  8249. std::string CanonDefModule =
  8250. getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
  8251. Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
  8252. << D << getOwningModuleNameForDiagnostic(D)
  8253. << CanonDef << CanonDefModule.empty() << CanonDefModule;
  8254. if (Candidates.empty())
  8255. Diag(cast<Decl>(CanonDef)->getLocation(),
  8256. diag::note_module_odr_violation_no_possible_decls) << D;
  8257. else {
  8258. for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
  8259. Diag(Candidates[I]->getLocation(),
  8260. diag::note_module_odr_violation_possible_decl)
  8261. << Candidates[I];
  8262. }
  8263. DiagnosedOdrMergeFailures.insert(CanonDef);
  8264. }
  8265. }
  8266. if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
  8267. EnumOdrMergeFailures.empty())
  8268. return;
  8269. // Ensure we don't accidentally recursively enter deserialization while
  8270. // we're producing our diagnostics.
  8271. Deserializing RecursionGuard(this);
  8272. // Common code for hashing helpers.
  8273. ODRHash Hash;
  8274. auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
  8275. Hash.clear();
  8276. Hash.AddQualType(Ty);
  8277. return Hash.CalculateHash();
  8278. };
  8279. auto ComputeODRHash = [&Hash](const Stmt *S) {
  8280. assert(S);
  8281. Hash.clear();
  8282. Hash.AddStmt(S);
  8283. return Hash.CalculateHash();
  8284. };
  8285. auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
  8286. assert(D);
  8287. Hash.clear();
  8288. Hash.AddSubDecl(D);
  8289. return Hash.CalculateHash();
  8290. };
  8291. auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
  8292. Hash.clear();
  8293. Hash.AddTemplateArgument(TA);
  8294. return Hash.CalculateHash();
  8295. };
  8296. auto ComputeTemplateParameterListODRHash =
  8297. [&Hash](const TemplateParameterList *TPL) {
  8298. assert(TPL);
  8299. Hash.clear();
  8300. Hash.AddTemplateParameterList(TPL);
  8301. return Hash.CalculateHash();
  8302. };
  8303. // Issue any pending ODR-failure diagnostics.
  8304. for (auto &Merge : OdrMergeFailures) {
  8305. // If we've already pointed out a specific problem with this class, don't
  8306. // bother issuing a general "something's different" diagnostic.
  8307. if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
  8308. continue;
  8309. bool Diagnosed = false;
  8310. CXXRecordDecl *FirstRecord = Merge.first;
  8311. std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
  8312. for (auto &RecordPair : Merge.second) {
  8313. CXXRecordDecl *SecondRecord = RecordPair.first;
  8314. // Multiple different declarations got merged together; tell the user
  8315. // where they came from.
  8316. if (FirstRecord == SecondRecord)
  8317. continue;
  8318. std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
  8319. auto *FirstDD = FirstRecord->DefinitionData;
  8320. auto *SecondDD = RecordPair.second;
  8321. assert(FirstDD && SecondDD && "Definitions without DefinitionData");
  8322. // Diagnostics from DefinitionData are emitted here.
  8323. if (FirstDD != SecondDD) {
  8324. enum ODRDefinitionDataDifference {
  8325. NumBases,
  8326. NumVBases,
  8327. BaseType,
  8328. BaseVirtual,
  8329. BaseAccess,
  8330. };
  8331. auto ODRDiagError = [FirstRecord, &FirstModule,
  8332. this](SourceLocation Loc, SourceRange Range,
  8333. ODRDefinitionDataDifference DiffType) {
  8334. return Diag(Loc, diag::err_module_odr_violation_definition_data)
  8335. << FirstRecord << FirstModule.empty() << FirstModule << Range
  8336. << DiffType;
  8337. };
  8338. auto ODRDiagNote = [&SecondModule,
  8339. this](SourceLocation Loc, SourceRange Range,
  8340. ODRDefinitionDataDifference DiffType) {
  8341. return Diag(Loc, diag::note_module_odr_violation_definition_data)
  8342. << SecondModule << Range << DiffType;
  8343. };
  8344. unsigned FirstNumBases = FirstDD->NumBases;
  8345. unsigned FirstNumVBases = FirstDD->NumVBases;
  8346. unsigned SecondNumBases = SecondDD->NumBases;
  8347. unsigned SecondNumVBases = SecondDD->NumVBases;
  8348. auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
  8349. unsigned NumBases = DD->NumBases;
  8350. if (NumBases == 0) return SourceRange();
  8351. auto bases = DD->bases();
  8352. return SourceRange(bases[0].getBeginLoc(),
  8353. bases[NumBases - 1].getEndLoc());
  8354. };
  8355. if (FirstNumBases != SecondNumBases) {
  8356. ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
  8357. NumBases)
  8358. << FirstNumBases;
  8359. ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
  8360. NumBases)
  8361. << SecondNumBases;
  8362. Diagnosed = true;
  8363. break;
  8364. }
  8365. if (FirstNumVBases != SecondNumVBases) {
  8366. ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
  8367. NumVBases)
  8368. << FirstNumVBases;
  8369. ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
  8370. NumVBases)
  8371. << SecondNumVBases;
  8372. Diagnosed = true;
  8373. break;
  8374. }
  8375. auto FirstBases = FirstDD->bases();
  8376. auto SecondBases = SecondDD->bases();
  8377. unsigned i = 0;
  8378. for (i = 0; i < FirstNumBases; ++i) {
  8379. auto FirstBase = FirstBases[i];
  8380. auto SecondBase = SecondBases[i];
  8381. if (ComputeQualTypeODRHash(FirstBase.getType()) !=
  8382. ComputeQualTypeODRHash(SecondBase.getType())) {
  8383. ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
  8384. BaseType)
  8385. << (i + 1) << FirstBase.getType();
  8386. ODRDiagNote(SecondRecord->getLocation(),
  8387. SecondBase.getSourceRange(), BaseType)
  8388. << (i + 1) << SecondBase.getType();
  8389. break;
  8390. }
  8391. if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
  8392. ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
  8393. BaseVirtual)
  8394. << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
  8395. ODRDiagNote(SecondRecord->getLocation(),
  8396. SecondBase.getSourceRange(), BaseVirtual)
  8397. << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
  8398. break;
  8399. }
  8400. if (FirstBase.getAccessSpecifierAsWritten() !=
  8401. SecondBase.getAccessSpecifierAsWritten()) {
  8402. ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
  8403. BaseAccess)
  8404. << (i + 1) << FirstBase.getType()
  8405. << (int)FirstBase.getAccessSpecifierAsWritten();
  8406. ODRDiagNote(SecondRecord->getLocation(),
  8407. SecondBase.getSourceRange(), BaseAccess)
  8408. << (i + 1) << SecondBase.getType()
  8409. << (int)SecondBase.getAccessSpecifierAsWritten();
  8410. break;
  8411. }
  8412. }
  8413. if (i != FirstNumBases) {
  8414. Diagnosed = true;
  8415. break;
  8416. }
  8417. }
  8418. using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
  8419. const ClassTemplateDecl *FirstTemplate =
  8420. FirstRecord->getDescribedClassTemplate();
  8421. const ClassTemplateDecl *SecondTemplate =
  8422. SecondRecord->getDescribedClassTemplate();
  8423. assert(!FirstTemplate == !SecondTemplate &&
  8424. "Both pointers should be null or non-null");
  8425. enum ODRTemplateDifference {
  8426. ParamEmptyName,
  8427. ParamName,
  8428. ParamSingleDefaultArgument,
  8429. ParamDifferentDefaultArgument,
  8430. };
  8431. if (FirstTemplate && SecondTemplate) {
  8432. DeclHashes FirstTemplateHashes;
  8433. DeclHashes SecondTemplateHashes;
  8434. auto PopulateTemplateParameterHashs =
  8435. [&ComputeSubDeclODRHash](DeclHashes &Hashes,
  8436. const ClassTemplateDecl *TD) {
  8437. for (auto *D : TD->getTemplateParameters()->asArray()) {
  8438. Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
  8439. }
  8440. };
  8441. PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
  8442. PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
  8443. assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
  8444. "Number of template parameters should be equal.");
  8445. auto FirstIt = FirstTemplateHashes.begin();
  8446. auto FirstEnd = FirstTemplateHashes.end();
  8447. auto SecondIt = SecondTemplateHashes.begin();
  8448. for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
  8449. if (FirstIt->second == SecondIt->second)
  8450. continue;
  8451. auto ODRDiagError = [FirstRecord, &FirstModule,
  8452. this](SourceLocation Loc, SourceRange Range,
  8453. ODRTemplateDifference DiffType) {
  8454. return Diag(Loc, diag::err_module_odr_violation_template_parameter)
  8455. << FirstRecord << FirstModule.empty() << FirstModule << Range
  8456. << DiffType;
  8457. };
  8458. auto ODRDiagNote = [&SecondModule,
  8459. this](SourceLocation Loc, SourceRange Range,
  8460. ODRTemplateDifference DiffType) {
  8461. return Diag(Loc, diag::note_module_odr_violation_template_parameter)
  8462. << SecondModule << Range << DiffType;
  8463. };
  8464. const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
  8465. const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
  8466. assert(FirstDecl->getKind() == SecondDecl->getKind() &&
  8467. "Parameter Decl's should be the same kind.");
  8468. DeclarationName FirstName = FirstDecl->getDeclName();
  8469. DeclarationName SecondName = SecondDecl->getDeclName();
  8470. if (FirstName != SecondName) {
  8471. const bool FirstNameEmpty =
  8472. FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
  8473. const bool SecondNameEmpty =
  8474. SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
  8475. assert((!FirstNameEmpty || !SecondNameEmpty) &&
  8476. "Both template parameters cannot be unnamed.");
  8477. ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
  8478. FirstNameEmpty ? ParamEmptyName : ParamName)
  8479. << FirstName;
  8480. ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
  8481. SecondNameEmpty ? ParamEmptyName : ParamName)
  8482. << SecondName;
  8483. break;
  8484. }
  8485. switch (FirstDecl->getKind()) {
  8486. default:
  8487. llvm_unreachable("Invalid template parameter type.");
  8488. case Decl::TemplateTypeParm: {
  8489. const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
  8490. const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
  8491. const bool HasFirstDefaultArgument =
  8492. FirstParam->hasDefaultArgument() &&
  8493. !FirstParam->defaultArgumentWasInherited();
  8494. const bool HasSecondDefaultArgument =
  8495. SecondParam->hasDefaultArgument() &&
  8496. !SecondParam->defaultArgumentWasInherited();
  8497. if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
  8498. ODRDiagError(FirstDecl->getLocation(),
  8499. FirstDecl->getSourceRange(),
  8500. ParamSingleDefaultArgument)
  8501. << HasFirstDefaultArgument;
  8502. ODRDiagNote(SecondDecl->getLocation(),
  8503. SecondDecl->getSourceRange(),
  8504. ParamSingleDefaultArgument)
  8505. << HasSecondDefaultArgument;
  8506. break;
  8507. }
  8508. assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
  8509. "Expecting default arguments.");
  8510. ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
  8511. ParamDifferentDefaultArgument);
  8512. ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
  8513. ParamDifferentDefaultArgument);
  8514. break;
  8515. }
  8516. case Decl::NonTypeTemplateParm: {
  8517. const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
  8518. const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
  8519. const bool HasFirstDefaultArgument =
  8520. FirstParam->hasDefaultArgument() &&
  8521. !FirstParam->defaultArgumentWasInherited();
  8522. const bool HasSecondDefaultArgument =
  8523. SecondParam->hasDefaultArgument() &&
  8524. !SecondParam->defaultArgumentWasInherited();
  8525. if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
  8526. ODRDiagError(FirstDecl->getLocation(),
  8527. FirstDecl->getSourceRange(),
  8528. ParamSingleDefaultArgument)
  8529. << HasFirstDefaultArgument;
  8530. ODRDiagNote(SecondDecl->getLocation(),
  8531. SecondDecl->getSourceRange(),
  8532. ParamSingleDefaultArgument)
  8533. << HasSecondDefaultArgument;
  8534. break;
  8535. }
  8536. assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
  8537. "Expecting default arguments.");
  8538. ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
  8539. ParamDifferentDefaultArgument);
  8540. ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
  8541. ParamDifferentDefaultArgument);
  8542. break;
  8543. }
  8544. case Decl::TemplateTemplateParm: {
  8545. const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
  8546. const auto *SecondParam =
  8547. cast<TemplateTemplateParmDecl>(SecondDecl);
  8548. const bool HasFirstDefaultArgument =
  8549. FirstParam->hasDefaultArgument() &&
  8550. !FirstParam->defaultArgumentWasInherited();
  8551. const bool HasSecondDefaultArgument =
  8552. SecondParam->hasDefaultArgument() &&
  8553. !SecondParam->defaultArgumentWasInherited();
  8554. if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
  8555. ODRDiagError(FirstDecl->getLocation(),
  8556. FirstDecl->getSourceRange(),
  8557. ParamSingleDefaultArgument)
  8558. << HasFirstDefaultArgument;
  8559. ODRDiagNote(SecondDecl->getLocation(),
  8560. SecondDecl->getSourceRange(),
  8561. ParamSingleDefaultArgument)
  8562. << HasSecondDefaultArgument;
  8563. break;
  8564. }
  8565. assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
  8566. "Expecting default arguments.");
  8567. ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
  8568. ParamDifferentDefaultArgument);
  8569. ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
  8570. ParamDifferentDefaultArgument);
  8571. break;
  8572. }
  8573. }
  8574. break;
  8575. }
  8576. if (FirstIt != FirstEnd) {
  8577. Diagnosed = true;
  8578. break;
  8579. }
  8580. }
  8581. DeclHashes FirstHashes;
  8582. DeclHashes SecondHashes;
  8583. auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord](
  8584. DeclHashes &Hashes, CXXRecordDecl *Record) {
  8585. for (auto *D : Record->decls()) {
  8586. // Due to decl merging, the first CXXRecordDecl is the parent of
  8587. // Decls in both records.
  8588. if (!ODRHash::isWhitelistedDecl(D, FirstRecord))
  8589. continue;
  8590. Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
  8591. }
  8592. };
  8593. PopulateHashes(FirstHashes, FirstRecord);
  8594. PopulateHashes(SecondHashes, SecondRecord);
  8595. // Used with err_module_odr_violation_mismatch_decl and
  8596. // note_module_odr_violation_mismatch_decl
  8597. // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
  8598. enum {
  8599. EndOfClass,
  8600. PublicSpecifer,
  8601. PrivateSpecifer,
  8602. ProtectedSpecifer,
  8603. StaticAssert,
  8604. Field,
  8605. CXXMethod,
  8606. TypeAlias,
  8607. TypeDef,
  8608. Var,
  8609. Friend,
  8610. FunctionTemplate,
  8611. Other
  8612. } FirstDiffType = Other,
  8613. SecondDiffType = Other;
  8614. auto DifferenceSelector = [](Decl *D) {
  8615. assert(D && "valid Decl required");
  8616. switch (D->getKind()) {
  8617. default:
  8618. return Other;
  8619. case Decl::AccessSpec:
  8620. switch (D->getAccess()) {
  8621. case AS_public:
  8622. return PublicSpecifer;
  8623. case AS_private:
  8624. return PrivateSpecifer;
  8625. case AS_protected:
  8626. return ProtectedSpecifer;
  8627. case AS_none:
  8628. break;
  8629. }
  8630. llvm_unreachable("Invalid access specifier");
  8631. case Decl::StaticAssert:
  8632. return StaticAssert;
  8633. case Decl::Field:
  8634. return Field;
  8635. case Decl::CXXMethod:
  8636. case Decl::CXXConstructor:
  8637. case Decl::CXXDestructor:
  8638. return CXXMethod;
  8639. case Decl::TypeAlias:
  8640. return TypeAlias;
  8641. case Decl::Typedef:
  8642. return TypeDef;
  8643. case Decl::Var:
  8644. return Var;
  8645. case Decl::Friend:
  8646. return Friend;
  8647. case Decl::FunctionTemplate:
  8648. return FunctionTemplate;
  8649. }
  8650. };
  8651. Decl *FirstDecl = nullptr;
  8652. Decl *SecondDecl = nullptr;
  8653. auto FirstIt = FirstHashes.begin();
  8654. auto SecondIt = SecondHashes.begin();
  8655. // If there is a diagnoseable difference, FirstDiffType and
  8656. // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
  8657. // filled in if not EndOfClass.
  8658. while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
  8659. if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
  8660. FirstIt->second == SecondIt->second) {
  8661. ++FirstIt;
  8662. ++SecondIt;
  8663. continue;
  8664. }
  8665. FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
  8666. SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
  8667. FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass;
  8668. SecondDiffType =
  8669. SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass;
  8670. break;
  8671. }
  8672. if (FirstDiffType == Other || SecondDiffType == Other) {
  8673. // Reaching this point means an unexpected Decl was encountered
  8674. // or no difference was detected. This causes a generic error
  8675. // message to be emitted.
  8676. Diag(FirstRecord->getLocation(),
  8677. diag::err_module_odr_violation_different_definitions)
  8678. << FirstRecord << FirstModule.empty() << FirstModule;
  8679. if (FirstDecl) {
  8680. Diag(FirstDecl->getLocation(), diag::note_first_module_difference)
  8681. << FirstRecord << FirstDecl->getSourceRange();
  8682. }
  8683. Diag(SecondRecord->getLocation(),
  8684. diag::note_module_odr_violation_different_definitions)
  8685. << SecondModule;
  8686. if (SecondDecl) {
  8687. Diag(SecondDecl->getLocation(), diag::note_second_module_difference)
  8688. << SecondDecl->getSourceRange();
  8689. }
  8690. Diagnosed = true;
  8691. break;
  8692. }
  8693. if (FirstDiffType != SecondDiffType) {
  8694. SourceLocation FirstLoc;
  8695. SourceRange FirstRange;
  8696. if (FirstDiffType == EndOfClass) {
  8697. FirstLoc = FirstRecord->getBraceRange().getEnd();
  8698. } else {
  8699. FirstLoc = FirstIt->first->getLocation();
  8700. FirstRange = FirstIt->first->getSourceRange();
  8701. }
  8702. Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
  8703. << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
  8704. << FirstDiffType;
  8705. SourceLocation SecondLoc;
  8706. SourceRange SecondRange;
  8707. if (SecondDiffType == EndOfClass) {
  8708. SecondLoc = SecondRecord->getBraceRange().getEnd();
  8709. } else {
  8710. SecondLoc = SecondDecl->getLocation();
  8711. SecondRange = SecondDecl->getSourceRange();
  8712. }
  8713. Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
  8714. << SecondModule << SecondRange << SecondDiffType;
  8715. Diagnosed = true;
  8716. break;
  8717. }
  8718. assert(FirstDiffType == SecondDiffType);
  8719. // Used with err_module_odr_violation_mismatch_decl_diff and
  8720. // note_module_odr_violation_mismatch_decl_diff
  8721. enum ODRDeclDifference {
  8722. StaticAssertCondition,
  8723. StaticAssertMessage,
  8724. StaticAssertOnlyMessage,
  8725. FieldName,
  8726. FieldTypeName,
  8727. FieldSingleBitField,
  8728. FieldDifferentWidthBitField,
  8729. FieldSingleMutable,
  8730. FieldSingleInitializer,
  8731. FieldDifferentInitializers,
  8732. MethodName,
  8733. MethodDeleted,
  8734. MethodDefaulted,
  8735. MethodVirtual,
  8736. MethodStatic,
  8737. MethodVolatile,
  8738. MethodConst,
  8739. MethodInline,
  8740. MethodNumberParameters,
  8741. MethodParameterType,
  8742. MethodParameterName,
  8743. MethodParameterSingleDefaultArgument,
  8744. MethodParameterDifferentDefaultArgument,
  8745. MethodNoTemplateArguments,
  8746. MethodDifferentNumberTemplateArguments,
  8747. MethodDifferentTemplateArgument,
  8748. MethodSingleBody,
  8749. MethodDifferentBody,
  8750. TypedefName,
  8751. TypedefType,
  8752. VarName,
  8753. VarType,
  8754. VarSingleInitializer,
  8755. VarDifferentInitializer,
  8756. VarConstexpr,
  8757. FriendTypeFunction,
  8758. FriendType,
  8759. FriendFunction,
  8760. FunctionTemplateDifferentNumberParameters,
  8761. FunctionTemplateParameterDifferentKind,
  8762. FunctionTemplateParameterName,
  8763. FunctionTemplateParameterSingleDefaultArgument,
  8764. FunctionTemplateParameterDifferentDefaultArgument,
  8765. FunctionTemplateParameterDifferentType,
  8766. FunctionTemplatePackParameter,
  8767. };
  8768. // These lambdas have the common portions of the ODR diagnostics. This
  8769. // has the same return as Diag(), so addition parameters can be passed
  8770. // in with operator<<
  8771. auto ODRDiagError = [FirstRecord, &FirstModule, this](
  8772. SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
  8773. return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
  8774. << FirstRecord << FirstModule.empty() << FirstModule << Range
  8775. << DiffType;
  8776. };
  8777. auto ODRDiagNote = [&SecondModule, this](
  8778. SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
  8779. return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
  8780. << SecondModule << Range << DiffType;
  8781. };
  8782. switch (FirstDiffType) {
  8783. case Other:
  8784. case EndOfClass:
  8785. case PublicSpecifer:
  8786. case PrivateSpecifer:
  8787. case ProtectedSpecifer:
  8788. llvm_unreachable("Invalid diff type");
  8789. case StaticAssert: {
  8790. StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
  8791. StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
  8792. Expr *FirstExpr = FirstSA->getAssertExpr();
  8793. Expr *SecondExpr = SecondSA->getAssertExpr();
  8794. unsigned FirstODRHash = ComputeODRHash(FirstExpr);
  8795. unsigned SecondODRHash = ComputeODRHash(SecondExpr);
  8796. if (FirstODRHash != SecondODRHash) {
  8797. ODRDiagError(FirstExpr->getBeginLoc(), FirstExpr->getSourceRange(),
  8798. StaticAssertCondition);
  8799. ODRDiagNote(SecondExpr->getBeginLoc(), SecondExpr->getSourceRange(),
  8800. StaticAssertCondition);
  8801. Diagnosed = true;
  8802. break;
  8803. }
  8804. StringLiteral *FirstStr = FirstSA->getMessage();
  8805. StringLiteral *SecondStr = SecondSA->getMessage();
  8806. assert((FirstStr || SecondStr) && "Both messages cannot be empty");
  8807. if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
  8808. SourceLocation FirstLoc, SecondLoc;
  8809. SourceRange FirstRange, SecondRange;
  8810. if (FirstStr) {
  8811. FirstLoc = FirstStr->getBeginLoc();
  8812. FirstRange = FirstStr->getSourceRange();
  8813. } else {
  8814. FirstLoc = FirstSA->getBeginLoc();
  8815. FirstRange = FirstSA->getSourceRange();
  8816. }
  8817. if (SecondStr) {
  8818. SecondLoc = SecondStr->getBeginLoc();
  8819. SecondRange = SecondStr->getSourceRange();
  8820. } else {
  8821. SecondLoc = SecondSA->getBeginLoc();
  8822. SecondRange = SecondSA->getSourceRange();
  8823. }
  8824. ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage)
  8825. << (FirstStr == nullptr);
  8826. ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage)
  8827. << (SecondStr == nullptr);
  8828. Diagnosed = true;
  8829. break;
  8830. }
  8831. if (FirstStr && SecondStr &&
  8832. FirstStr->getString() != SecondStr->getString()) {
  8833. ODRDiagError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(),
  8834. StaticAssertMessage);
  8835. ODRDiagNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(),
  8836. StaticAssertMessage);
  8837. Diagnosed = true;
  8838. break;
  8839. }
  8840. break;
  8841. }
  8842. case Field: {
  8843. FieldDecl *FirstField = cast<FieldDecl>(FirstDecl);
  8844. FieldDecl *SecondField = cast<FieldDecl>(SecondDecl);
  8845. IdentifierInfo *FirstII = FirstField->getIdentifier();
  8846. IdentifierInfo *SecondII = SecondField->getIdentifier();
  8847. if (FirstII->getName() != SecondII->getName()) {
  8848. ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
  8849. FieldName)
  8850. << FirstII;
  8851. ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
  8852. FieldName)
  8853. << SecondII;
  8854. Diagnosed = true;
  8855. break;
  8856. }
  8857. assert(getContext().hasSameType(FirstField->getType(),
  8858. SecondField->getType()));
  8859. QualType FirstType = FirstField->getType();
  8860. QualType SecondType = SecondField->getType();
  8861. if (ComputeQualTypeODRHash(FirstType) !=
  8862. ComputeQualTypeODRHash(SecondType)) {
  8863. ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
  8864. FieldTypeName)
  8865. << FirstII << FirstType;
  8866. ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
  8867. FieldTypeName)
  8868. << SecondII << SecondType;
  8869. Diagnosed = true;
  8870. break;
  8871. }
  8872. const bool IsFirstBitField = FirstField->isBitField();
  8873. const bool IsSecondBitField = SecondField->isBitField();
  8874. if (IsFirstBitField != IsSecondBitField) {
  8875. ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
  8876. FieldSingleBitField)
  8877. << FirstII << IsFirstBitField;
  8878. ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
  8879. FieldSingleBitField)
  8880. << SecondII << IsSecondBitField;
  8881. Diagnosed = true;
  8882. break;
  8883. }
  8884. if (IsFirstBitField && IsSecondBitField) {
  8885. ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
  8886. FieldDifferentWidthBitField)
  8887. << FirstII << FirstField->getBitWidth()->getSourceRange();
  8888. ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
  8889. FieldDifferentWidthBitField)
  8890. << SecondII << SecondField->getBitWidth()->getSourceRange();
  8891. Diagnosed = true;
  8892. break;
  8893. }
  8894. const bool IsFirstMutable = FirstField->isMutable();
  8895. const bool IsSecondMutable = SecondField->isMutable();
  8896. if (IsFirstMutable != IsSecondMutable) {
  8897. ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
  8898. FieldSingleMutable)
  8899. << FirstII << IsFirstMutable;
  8900. ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
  8901. FieldSingleMutable)
  8902. << SecondII << IsSecondMutable;
  8903. Diagnosed = true;
  8904. break;
  8905. }
  8906. const Expr *FirstInitializer = FirstField->getInClassInitializer();
  8907. const Expr *SecondInitializer = SecondField->getInClassInitializer();
  8908. if ((!FirstInitializer && SecondInitializer) ||
  8909. (FirstInitializer && !SecondInitializer)) {
  8910. ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
  8911. FieldSingleInitializer)
  8912. << FirstII << (FirstInitializer != nullptr);
  8913. ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
  8914. FieldSingleInitializer)
  8915. << SecondII << (SecondInitializer != nullptr);
  8916. Diagnosed = true;
  8917. break;
  8918. }
  8919. if (FirstInitializer && SecondInitializer) {
  8920. unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
  8921. unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
  8922. if (FirstInitHash != SecondInitHash) {
  8923. ODRDiagError(FirstField->getLocation(),
  8924. FirstField->getSourceRange(),
  8925. FieldDifferentInitializers)
  8926. << FirstII << FirstInitializer->getSourceRange();
  8927. ODRDiagNote(SecondField->getLocation(),
  8928. SecondField->getSourceRange(),
  8929. FieldDifferentInitializers)
  8930. << SecondII << SecondInitializer->getSourceRange();
  8931. Diagnosed = true;
  8932. break;
  8933. }
  8934. }
  8935. break;
  8936. }
  8937. case CXXMethod: {
  8938. enum {
  8939. DiagMethod,
  8940. DiagConstructor,
  8941. DiagDestructor,
  8942. } FirstMethodType,
  8943. SecondMethodType;
  8944. auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
  8945. if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
  8946. if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
  8947. return DiagMethod;
  8948. };
  8949. const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
  8950. const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
  8951. FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
  8952. SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
  8953. auto FirstName = FirstMethod->getDeclName();
  8954. auto SecondName = SecondMethod->getDeclName();
  8955. if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
  8956. ODRDiagError(FirstMethod->getLocation(),
  8957. FirstMethod->getSourceRange(), MethodName)
  8958. << FirstMethodType << FirstName;
  8959. ODRDiagNote(SecondMethod->getLocation(),
  8960. SecondMethod->getSourceRange(), MethodName)
  8961. << SecondMethodType << SecondName;
  8962. Diagnosed = true;
  8963. break;
  8964. }
  8965. const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
  8966. const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
  8967. if (FirstDeleted != SecondDeleted) {
  8968. ODRDiagError(FirstMethod->getLocation(),
  8969. FirstMethod->getSourceRange(), MethodDeleted)
  8970. << FirstMethodType << FirstName << FirstDeleted;
  8971. ODRDiagNote(SecondMethod->getLocation(),
  8972. SecondMethod->getSourceRange(), MethodDeleted)
  8973. << SecondMethodType << SecondName << SecondDeleted;
  8974. Diagnosed = true;
  8975. break;
  8976. }
  8977. const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
  8978. const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
  8979. if (FirstDefaulted != SecondDefaulted) {
  8980. ODRDiagError(FirstMethod->getLocation(),
  8981. FirstMethod->getSourceRange(), MethodDefaulted)
  8982. << FirstMethodType << FirstName << FirstDefaulted;
  8983. ODRDiagNote(SecondMethod->getLocation(),
  8984. SecondMethod->getSourceRange(), MethodDefaulted)
  8985. << SecondMethodType << SecondName << SecondDefaulted;
  8986. Diagnosed = true;
  8987. break;
  8988. }
  8989. const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
  8990. const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
  8991. const bool FirstPure = FirstMethod->isPure();
  8992. const bool SecondPure = SecondMethod->isPure();
  8993. if ((FirstVirtual || SecondVirtual) &&
  8994. (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
  8995. ODRDiagError(FirstMethod->getLocation(),
  8996. FirstMethod->getSourceRange(), MethodVirtual)
  8997. << FirstMethodType << FirstName << FirstPure << FirstVirtual;
  8998. ODRDiagNote(SecondMethod->getLocation(),
  8999. SecondMethod->getSourceRange(), MethodVirtual)
  9000. << SecondMethodType << SecondName << SecondPure << SecondVirtual;
  9001. Diagnosed = true;
  9002. break;
  9003. }
  9004. // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging,
  9005. // FirstDecl is the canonical Decl of SecondDecl, so the storage
  9006. // class needs to be checked instead.
  9007. const auto FirstStorage = FirstMethod->getStorageClass();
  9008. const auto SecondStorage = SecondMethod->getStorageClass();
  9009. const bool FirstStatic = FirstStorage == SC_Static;
  9010. const bool SecondStatic = SecondStorage == SC_Static;
  9011. if (FirstStatic != SecondStatic) {
  9012. ODRDiagError(FirstMethod->getLocation(),
  9013. FirstMethod->getSourceRange(), MethodStatic)
  9014. << FirstMethodType << FirstName << FirstStatic;
  9015. ODRDiagNote(SecondMethod->getLocation(),
  9016. SecondMethod->getSourceRange(), MethodStatic)
  9017. << SecondMethodType << SecondName << SecondStatic;
  9018. Diagnosed = true;
  9019. break;
  9020. }
  9021. const bool FirstVolatile = FirstMethod->isVolatile();
  9022. const bool SecondVolatile = SecondMethod->isVolatile();
  9023. if (FirstVolatile != SecondVolatile) {
  9024. ODRDiagError(FirstMethod->getLocation(),
  9025. FirstMethod->getSourceRange(), MethodVolatile)
  9026. << FirstMethodType << FirstName << FirstVolatile;
  9027. ODRDiagNote(SecondMethod->getLocation(),
  9028. SecondMethod->getSourceRange(), MethodVolatile)
  9029. << SecondMethodType << SecondName << SecondVolatile;
  9030. Diagnosed = true;
  9031. break;
  9032. }
  9033. const bool FirstConst = FirstMethod->isConst();
  9034. const bool SecondConst = SecondMethod->isConst();
  9035. if (FirstConst != SecondConst) {
  9036. ODRDiagError(FirstMethod->getLocation(),
  9037. FirstMethod->getSourceRange(), MethodConst)
  9038. << FirstMethodType << FirstName << FirstConst;
  9039. ODRDiagNote(SecondMethod->getLocation(),
  9040. SecondMethod->getSourceRange(), MethodConst)
  9041. << SecondMethodType << SecondName << SecondConst;
  9042. Diagnosed = true;
  9043. break;
  9044. }
  9045. const bool FirstInline = FirstMethod->isInlineSpecified();
  9046. const bool SecondInline = SecondMethod->isInlineSpecified();
  9047. if (FirstInline != SecondInline) {
  9048. ODRDiagError(FirstMethod->getLocation(),
  9049. FirstMethod->getSourceRange(), MethodInline)
  9050. << FirstMethodType << FirstName << FirstInline;
  9051. ODRDiagNote(SecondMethod->getLocation(),
  9052. SecondMethod->getSourceRange(), MethodInline)
  9053. << SecondMethodType << SecondName << SecondInline;
  9054. Diagnosed = true;
  9055. break;
  9056. }
  9057. const unsigned FirstNumParameters = FirstMethod->param_size();
  9058. const unsigned SecondNumParameters = SecondMethod->param_size();
  9059. if (FirstNumParameters != SecondNumParameters) {
  9060. ODRDiagError(FirstMethod->getLocation(),
  9061. FirstMethod->getSourceRange(), MethodNumberParameters)
  9062. << FirstMethodType << FirstName << FirstNumParameters;
  9063. ODRDiagNote(SecondMethod->getLocation(),
  9064. SecondMethod->getSourceRange(), MethodNumberParameters)
  9065. << SecondMethodType << SecondName << SecondNumParameters;
  9066. Diagnosed = true;
  9067. break;
  9068. }
  9069. // Need this status boolean to know when break out of the switch.
  9070. bool ParameterMismatch = false;
  9071. for (unsigned I = 0; I < FirstNumParameters; ++I) {
  9072. const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
  9073. const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
  9074. QualType FirstParamType = FirstParam->getType();
  9075. QualType SecondParamType = SecondParam->getType();
  9076. if (FirstParamType != SecondParamType &&
  9077. ComputeQualTypeODRHash(FirstParamType) !=
  9078. ComputeQualTypeODRHash(SecondParamType)) {
  9079. if (const DecayedType *ParamDecayedType =
  9080. FirstParamType->getAs<DecayedType>()) {
  9081. ODRDiagError(FirstMethod->getLocation(),
  9082. FirstMethod->getSourceRange(), MethodParameterType)
  9083. << FirstMethodType << FirstName << (I + 1) << FirstParamType
  9084. << true << ParamDecayedType->getOriginalType();
  9085. } else {
  9086. ODRDiagError(FirstMethod->getLocation(),
  9087. FirstMethod->getSourceRange(), MethodParameterType)
  9088. << FirstMethodType << FirstName << (I + 1) << FirstParamType
  9089. << false;
  9090. }
  9091. if (const DecayedType *ParamDecayedType =
  9092. SecondParamType->getAs<DecayedType>()) {
  9093. ODRDiagNote(SecondMethod->getLocation(),
  9094. SecondMethod->getSourceRange(), MethodParameterType)
  9095. << SecondMethodType << SecondName << (I + 1)
  9096. << SecondParamType << true
  9097. << ParamDecayedType->getOriginalType();
  9098. } else {
  9099. ODRDiagNote(SecondMethod->getLocation(),
  9100. SecondMethod->getSourceRange(), MethodParameterType)
  9101. << SecondMethodType << SecondName << (I + 1)
  9102. << SecondParamType << false;
  9103. }
  9104. ParameterMismatch = true;
  9105. break;
  9106. }
  9107. DeclarationName FirstParamName = FirstParam->getDeclName();
  9108. DeclarationName SecondParamName = SecondParam->getDeclName();
  9109. if (FirstParamName != SecondParamName) {
  9110. ODRDiagError(FirstMethod->getLocation(),
  9111. FirstMethod->getSourceRange(), MethodParameterName)
  9112. << FirstMethodType << FirstName << (I + 1) << FirstParamName;
  9113. ODRDiagNote(SecondMethod->getLocation(),
  9114. SecondMethod->getSourceRange(), MethodParameterName)
  9115. << SecondMethodType << SecondName << (I + 1) << SecondParamName;
  9116. ParameterMismatch = true;
  9117. break;
  9118. }
  9119. const Expr *FirstInit = FirstParam->getInit();
  9120. const Expr *SecondInit = SecondParam->getInit();
  9121. if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
  9122. ODRDiagError(FirstMethod->getLocation(),
  9123. FirstMethod->getSourceRange(),
  9124. MethodParameterSingleDefaultArgument)
  9125. << FirstMethodType << FirstName << (I + 1)
  9126. << (FirstInit == nullptr)
  9127. << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
  9128. ODRDiagNote(SecondMethod->getLocation(),
  9129. SecondMethod->getSourceRange(),
  9130. MethodParameterSingleDefaultArgument)
  9131. << SecondMethodType << SecondName << (I + 1)
  9132. << (SecondInit == nullptr)
  9133. << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
  9134. ParameterMismatch = true;
  9135. break;
  9136. }
  9137. if (FirstInit && SecondInit &&
  9138. ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
  9139. ODRDiagError(FirstMethod->getLocation(),
  9140. FirstMethod->getSourceRange(),
  9141. MethodParameterDifferentDefaultArgument)
  9142. << FirstMethodType << FirstName << (I + 1)
  9143. << FirstInit->getSourceRange();
  9144. ODRDiagNote(SecondMethod->getLocation(),
  9145. SecondMethod->getSourceRange(),
  9146. MethodParameterDifferentDefaultArgument)
  9147. << SecondMethodType << SecondName << (I + 1)
  9148. << SecondInit->getSourceRange();
  9149. ParameterMismatch = true;
  9150. break;
  9151. }
  9152. }
  9153. if (ParameterMismatch) {
  9154. Diagnosed = true;
  9155. break;
  9156. }
  9157. const auto *FirstTemplateArgs =
  9158. FirstMethod->getTemplateSpecializationArgs();
  9159. const auto *SecondTemplateArgs =
  9160. SecondMethod->getTemplateSpecializationArgs();
  9161. if ((FirstTemplateArgs && !SecondTemplateArgs) ||
  9162. (!FirstTemplateArgs && SecondTemplateArgs)) {
  9163. ODRDiagError(FirstMethod->getLocation(),
  9164. FirstMethod->getSourceRange(), MethodNoTemplateArguments)
  9165. << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
  9166. ODRDiagNote(SecondMethod->getLocation(),
  9167. SecondMethod->getSourceRange(), MethodNoTemplateArguments)
  9168. << SecondMethodType << SecondName
  9169. << (SecondTemplateArgs != nullptr);
  9170. Diagnosed = true;
  9171. break;
  9172. }
  9173. if (FirstTemplateArgs && SecondTemplateArgs) {
  9174. // Remove pack expansions from argument list.
  9175. auto ExpandTemplateArgumentList =
  9176. [](const TemplateArgumentList *TAL) {
  9177. llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
  9178. for (const TemplateArgument &TA : TAL->asArray()) {
  9179. if (TA.getKind() != TemplateArgument::Pack) {
  9180. ExpandedList.push_back(&TA);
  9181. continue;
  9182. }
  9183. for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
  9184. ExpandedList.push_back(&PackTA);
  9185. }
  9186. }
  9187. return ExpandedList;
  9188. };
  9189. llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
  9190. ExpandTemplateArgumentList(FirstTemplateArgs);
  9191. llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
  9192. ExpandTemplateArgumentList(SecondTemplateArgs);
  9193. if (FirstExpandedList.size() != SecondExpandedList.size()) {
  9194. ODRDiagError(FirstMethod->getLocation(),
  9195. FirstMethod->getSourceRange(),
  9196. MethodDifferentNumberTemplateArguments)
  9197. << FirstMethodType << FirstName
  9198. << (unsigned)FirstExpandedList.size();
  9199. ODRDiagNote(SecondMethod->getLocation(),
  9200. SecondMethod->getSourceRange(),
  9201. MethodDifferentNumberTemplateArguments)
  9202. << SecondMethodType << SecondName
  9203. << (unsigned)SecondExpandedList.size();
  9204. Diagnosed = true;
  9205. break;
  9206. }
  9207. bool TemplateArgumentMismatch = false;
  9208. for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
  9209. const TemplateArgument &FirstTA = *FirstExpandedList[i],
  9210. &SecondTA = *SecondExpandedList[i];
  9211. if (ComputeTemplateArgumentODRHash(FirstTA) ==
  9212. ComputeTemplateArgumentODRHash(SecondTA)) {
  9213. continue;
  9214. }
  9215. ODRDiagError(FirstMethod->getLocation(),
  9216. FirstMethod->getSourceRange(),
  9217. MethodDifferentTemplateArgument)
  9218. << FirstMethodType << FirstName << FirstTA << i + 1;
  9219. ODRDiagNote(SecondMethod->getLocation(),
  9220. SecondMethod->getSourceRange(),
  9221. MethodDifferentTemplateArgument)
  9222. << SecondMethodType << SecondName << SecondTA << i + 1;
  9223. TemplateArgumentMismatch = true;
  9224. break;
  9225. }
  9226. if (TemplateArgumentMismatch) {
  9227. Diagnosed = true;
  9228. break;
  9229. }
  9230. }
  9231. // Compute the hash of the method as if it has no body.
  9232. auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
  9233. Hash.clear();
  9234. Hash.AddFunctionDecl(D, true /*SkipBody*/);
  9235. return Hash.CalculateHash();
  9236. };
  9237. // Compare the hash generated to the hash stored. A difference means
  9238. // that a body was present in the original source. Due to merging,
  9239. // the stardard way of detecting a body will not work.
  9240. const bool HasFirstBody =
  9241. ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
  9242. const bool HasSecondBody =
  9243. ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
  9244. if (HasFirstBody != HasSecondBody) {
  9245. ODRDiagError(FirstMethod->getLocation(),
  9246. FirstMethod->getSourceRange(), MethodSingleBody)
  9247. << FirstMethodType << FirstName << HasFirstBody;
  9248. ODRDiagNote(SecondMethod->getLocation(),
  9249. SecondMethod->getSourceRange(), MethodSingleBody)
  9250. << SecondMethodType << SecondName << HasSecondBody;
  9251. Diagnosed = true;
  9252. break;
  9253. }
  9254. if (HasFirstBody && HasSecondBody) {
  9255. ODRDiagError(FirstMethod->getLocation(),
  9256. FirstMethod->getSourceRange(), MethodDifferentBody)
  9257. << FirstMethodType << FirstName;
  9258. ODRDiagNote(SecondMethod->getLocation(),
  9259. SecondMethod->getSourceRange(), MethodDifferentBody)
  9260. << SecondMethodType << SecondName;
  9261. Diagnosed = true;
  9262. break;
  9263. }
  9264. break;
  9265. }
  9266. case TypeAlias:
  9267. case TypeDef: {
  9268. TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl);
  9269. TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl);
  9270. auto FirstName = FirstTD->getDeclName();
  9271. auto SecondName = SecondTD->getDeclName();
  9272. if (FirstName != SecondName) {
  9273. ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
  9274. TypedefName)
  9275. << (FirstDiffType == TypeAlias) << FirstName;
  9276. ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
  9277. TypedefName)
  9278. << (FirstDiffType == TypeAlias) << SecondName;
  9279. Diagnosed = true;
  9280. break;
  9281. }
  9282. QualType FirstType = FirstTD->getUnderlyingType();
  9283. QualType SecondType = SecondTD->getUnderlyingType();
  9284. if (ComputeQualTypeODRHash(FirstType) !=
  9285. ComputeQualTypeODRHash(SecondType)) {
  9286. ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
  9287. TypedefType)
  9288. << (FirstDiffType == TypeAlias) << FirstName << FirstType;
  9289. ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
  9290. TypedefType)
  9291. << (FirstDiffType == TypeAlias) << SecondName << SecondType;
  9292. Diagnosed = true;
  9293. break;
  9294. }
  9295. break;
  9296. }
  9297. case Var: {
  9298. VarDecl *FirstVD = cast<VarDecl>(FirstDecl);
  9299. VarDecl *SecondVD = cast<VarDecl>(SecondDecl);
  9300. auto FirstName = FirstVD->getDeclName();
  9301. auto SecondName = SecondVD->getDeclName();
  9302. if (FirstName != SecondName) {
  9303. ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
  9304. VarName)
  9305. << FirstName;
  9306. ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
  9307. VarName)
  9308. << SecondName;
  9309. Diagnosed = true;
  9310. break;
  9311. }
  9312. QualType FirstType = FirstVD->getType();
  9313. QualType SecondType = SecondVD->getType();
  9314. if (ComputeQualTypeODRHash(FirstType) !=
  9315. ComputeQualTypeODRHash(SecondType)) {
  9316. ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
  9317. VarType)
  9318. << FirstName << FirstType;
  9319. ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
  9320. VarType)
  9321. << SecondName << SecondType;
  9322. Diagnosed = true;
  9323. break;
  9324. }
  9325. const Expr *FirstInit = FirstVD->getInit();
  9326. const Expr *SecondInit = SecondVD->getInit();
  9327. if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
  9328. ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
  9329. VarSingleInitializer)
  9330. << FirstName << (FirstInit == nullptr)
  9331. << (FirstInit ? FirstInit->getSourceRange(): SourceRange());
  9332. ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
  9333. VarSingleInitializer)
  9334. << SecondName << (SecondInit == nullptr)
  9335. << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
  9336. Diagnosed = true;
  9337. break;
  9338. }
  9339. if (FirstInit && SecondInit &&
  9340. ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
  9341. ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
  9342. VarDifferentInitializer)
  9343. << FirstName << FirstInit->getSourceRange();
  9344. ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
  9345. VarDifferentInitializer)
  9346. << SecondName << SecondInit->getSourceRange();
  9347. Diagnosed = true;
  9348. break;
  9349. }
  9350. const bool FirstIsConstexpr = FirstVD->isConstexpr();
  9351. const bool SecondIsConstexpr = SecondVD->isConstexpr();
  9352. if (FirstIsConstexpr != SecondIsConstexpr) {
  9353. ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
  9354. VarConstexpr)
  9355. << FirstName << FirstIsConstexpr;
  9356. ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
  9357. VarConstexpr)
  9358. << SecondName << SecondIsConstexpr;
  9359. Diagnosed = true;
  9360. break;
  9361. }
  9362. break;
  9363. }
  9364. case Friend: {
  9365. FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
  9366. FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
  9367. NamedDecl *FirstND = FirstFriend->getFriendDecl();
  9368. NamedDecl *SecondND = SecondFriend->getFriendDecl();
  9369. TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
  9370. TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
  9371. if (FirstND && SecondND) {
  9372. ODRDiagError(FirstFriend->getFriendLoc(),
  9373. FirstFriend->getSourceRange(), FriendFunction)
  9374. << FirstND;
  9375. ODRDiagNote(SecondFriend->getFriendLoc(),
  9376. SecondFriend->getSourceRange(), FriendFunction)
  9377. << SecondND;
  9378. Diagnosed = true;
  9379. break;
  9380. }
  9381. if (FirstTSI && SecondTSI) {
  9382. QualType FirstFriendType = FirstTSI->getType();
  9383. QualType SecondFriendType = SecondTSI->getType();
  9384. assert(ComputeQualTypeODRHash(FirstFriendType) !=
  9385. ComputeQualTypeODRHash(SecondFriendType));
  9386. ODRDiagError(FirstFriend->getFriendLoc(),
  9387. FirstFriend->getSourceRange(), FriendType)
  9388. << FirstFriendType;
  9389. ODRDiagNote(SecondFriend->getFriendLoc(),
  9390. SecondFriend->getSourceRange(), FriendType)
  9391. << SecondFriendType;
  9392. Diagnosed = true;
  9393. break;
  9394. }
  9395. ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
  9396. FriendTypeFunction)
  9397. << (FirstTSI == nullptr);
  9398. ODRDiagNote(SecondFriend->getFriendLoc(),
  9399. SecondFriend->getSourceRange(), FriendTypeFunction)
  9400. << (SecondTSI == nullptr);
  9401. Diagnosed = true;
  9402. break;
  9403. }
  9404. case FunctionTemplate: {
  9405. FunctionTemplateDecl *FirstTemplate =
  9406. cast<FunctionTemplateDecl>(FirstDecl);
  9407. FunctionTemplateDecl *SecondTemplate =
  9408. cast<FunctionTemplateDecl>(SecondDecl);
  9409. TemplateParameterList *FirstTPL =
  9410. FirstTemplate->getTemplateParameters();
  9411. TemplateParameterList *SecondTPL =
  9412. SecondTemplate->getTemplateParameters();
  9413. if (FirstTPL->size() != SecondTPL->size()) {
  9414. ODRDiagError(FirstTemplate->getLocation(),
  9415. FirstTemplate->getSourceRange(),
  9416. FunctionTemplateDifferentNumberParameters)
  9417. << FirstTemplate << FirstTPL->size();
  9418. ODRDiagNote(SecondTemplate->getLocation(),
  9419. SecondTemplate->getSourceRange(),
  9420. FunctionTemplateDifferentNumberParameters)
  9421. << SecondTemplate << SecondTPL->size();
  9422. Diagnosed = true;
  9423. break;
  9424. }
  9425. bool ParameterMismatch = false;
  9426. for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
  9427. NamedDecl *FirstParam = FirstTPL->getParam(i);
  9428. NamedDecl *SecondParam = SecondTPL->getParam(i);
  9429. if (FirstParam->getKind() != SecondParam->getKind()) {
  9430. enum {
  9431. TemplateTypeParameter,
  9432. NonTypeTemplateParameter,
  9433. TemplateTemplateParameter,
  9434. };
  9435. auto GetParamType = [](NamedDecl *D) {
  9436. switch (D->getKind()) {
  9437. default:
  9438. llvm_unreachable("Unexpected template parameter type");
  9439. case Decl::TemplateTypeParm:
  9440. return TemplateTypeParameter;
  9441. case Decl::NonTypeTemplateParm:
  9442. return NonTypeTemplateParameter;
  9443. case Decl::TemplateTemplateParm:
  9444. return TemplateTemplateParameter;
  9445. }
  9446. };
  9447. ODRDiagError(FirstTemplate->getLocation(),
  9448. FirstTemplate->getSourceRange(),
  9449. FunctionTemplateParameterDifferentKind)
  9450. << FirstTemplate << (i + 1) << GetParamType(FirstParam);
  9451. ODRDiagNote(SecondTemplate->getLocation(),
  9452. SecondTemplate->getSourceRange(),
  9453. FunctionTemplateParameterDifferentKind)
  9454. << SecondTemplate << (i + 1) << GetParamType(SecondParam);
  9455. ParameterMismatch = true;
  9456. break;
  9457. }
  9458. if (FirstParam->getName() != SecondParam->getName()) {
  9459. ODRDiagError(FirstTemplate->getLocation(),
  9460. FirstTemplate->getSourceRange(),
  9461. FunctionTemplateParameterName)
  9462. << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
  9463. << FirstParam;
  9464. ODRDiagNote(SecondTemplate->getLocation(),
  9465. SecondTemplate->getSourceRange(),
  9466. FunctionTemplateParameterName)
  9467. << SecondTemplate << (i + 1)
  9468. << (bool)SecondParam->getIdentifier() << SecondParam;
  9469. ParameterMismatch = true;
  9470. break;
  9471. }
  9472. if (isa<TemplateTypeParmDecl>(FirstParam) &&
  9473. isa<TemplateTypeParmDecl>(SecondParam)) {
  9474. TemplateTypeParmDecl *FirstTTPD =
  9475. cast<TemplateTypeParmDecl>(FirstParam);
  9476. TemplateTypeParmDecl *SecondTTPD =
  9477. cast<TemplateTypeParmDecl>(SecondParam);
  9478. bool HasFirstDefaultArgument =
  9479. FirstTTPD->hasDefaultArgument() &&
  9480. !FirstTTPD->defaultArgumentWasInherited();
  9481. bool HasSecondDefaultArgument =
  9482. SecondTTPD->hasDefaultArgument() &&
  9483. !SecondTTPD->defaultArgumentWasInherited();
  9484. if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
  9485. ODRDiagError(FirstTemplate->getLocation(),
  9486. FirstTemplate->getSourceRange(),
  9487. FunctionTemplateParameterSingleDefaultArgument)
  9488. << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
  9489. ODRDiagNote(SecondTemplate->getLocation(),
  9490. SecondTemplate->getSourceRange(),
  9491. FunctionTemplateParameterSingleDefaultArgument)
  9492. << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
  9493. ParameterMismatch = true;
  9494. break;
  9495. }
  9496. if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
  9497. QualType FirstType = FirstTTPD->getDefaultArgument();
  9498. QualType SecondType = SecondTTPD->getDefaultArgument();
  9499. if (ComputeQualTypeODRHash(FirstType) !=
  9500. ComputeQualTypeODRHash(SecondType)) {
  9501. ODRDiagError(FirstTemplate->getLocation(),
  9502. FirstTemplate->getSourceRange(),
  9503. FunctionTemplateParameterDifferentDefaultArgument)
  9504. << FirstTemplate << (i + 1) << FirstType;
  9505. ODRDiagNote(SecondTemplate->getLocation(),
  9506. SecondTemplate->getSourceRange(),
  9507. FunctionTemplateParameterDifferentDefaultArgument)
  9508. << SecondTemplate << (i + 1) << SecondType;
  9509. ParameterMismatch = true;
  9510. break;
  9511. }
  9512. }
  9513. if (FirstTTPD->isParameterPack() !=
  9514. SecondTTPD->isParameterPack()) {
  9515. ODRDiagError(FirstTemplate->getLocation(),
  9516. FirstTemplate->getSourceRange(),
  9517. FunctionTemplatePackParameter)
  9518. << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
  9519. ODRDiagNote(SecondTemplate->getLocation(),
  9520. SecondTemplate->getSourceRange(),
  9521. FunctionTemplatePackParameter)
  9522. << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
  9523. ParameterMismatch = true;
  9524. break;
  9525. }
  9526. }
  9527. if (isa<TemplateTemplateParmDecl>(FirstParam) &&
  9528. isa<TemplateTemplateParmDecl>(SecondParam)) {
  9529. TemplateTemplateParmDecl *FirstTTPD =
  9530. cast<TemplateTemplateParmDecl>(FirstParam);
  9531. TemplateTemplateParmDecl *SecondTTPD =
  9532. cast<TemplateTemplateParmDecl>(SecondParam);
  9533. TemplateParameterList *FirstTPL =
  9534. FirstTTPD->getTemplateParameters();
  9535. TemplateParameterList *SecondTPL =
  9536. SecondTTPD->getTemplateParameters();
  9537. if (ComputeTemplateParameterListODRHash(FirstTPL) !=
  9538. ComputeTemplateParameterListODRHash(SecondTPL)) {
  9539. ODRDiagError(FirstTemplate->getLocation(),
  9540. FirstTemplate->getSourceRange(),
  9541. FunctionTemplateParameterDifferentType)
  9542. << FirstTemplate << (i + 1);
  9543. ODRDiagNote(SecondTemplate->getLocation(),
  9544. SecondTemplate->getSourceRange(),
  9545. FunctionTemplateParameterDifferentType)
  9546. << SecondTemplate << (i + 1);
  9547. ParameterMismatch = true;
  9548. break;
  9549. }
  9550. bool HasFirstDefaultArgument =
  9551. FirstTTPD->hasDefaultArgument() &&
  9552. !FirstTTPD->defaultArgumentWasInherited();
  9553. bool HasSecondDefaultArgument =
  9554. SecondTTPD->hasDefaultArgument() &&
  9555. !SecondTTPD->defaultArgumentWasInherited();
  9556. if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
  9557. ODRDiagError(FirstTemplate->getLocation(),
  9558. FirstTemplate->getSourceRange(),
  9559. FunctionTemplateParameterSingleDefaultArgument)
  9560. << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
  9561. ODRDiagNote(SecondTemplate->getLocation(),
  9562. SecondTemplate->getSourceRange(),
  9563. FunctionTemplateParameterSingleDefaultArgument)
  9564. << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
  9565. ParameterMismatch = true;
  9566. break;
  9567. }
  9568. if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
  9569. TemplateArgument FirstTA =
  9570. FirstTTPD->getDefaultArgument().getArgument();
  9571. TemplateArgument SecondTA =
  9572. SecondTTPD->getDefaultArgument().getArgument();
  9573. if (ComputeTemplateArgumentODRHash(FirstTA) !=
  9574. ComputeTemplateArgumentODRHash(SecondTA)) {
  9575. ODRDiagError(FirstTemplate->getLocation(),
  9576. FirstTemplate->getSourceRange(),
  9577. FunctionTemplateParameterDifferentDefaultArgument)
  9578. << FirstTemplate << (i + 1) << FirstTA;
  9579. ODRDiagNote(SecondTemplate->getLocation(),
  9580. SecondTemplate->getSourceRange(),
  9581. FunctionTemplateParameterDifferentDefaultArgument)
  9582. << SecondTemplate << (i + 1) << SecondTA;
  9583. ParameterMismatch = true;
  9584. break;
  9585. }
  9586. }
  9587. if (FirstTTPD->isParameterPack() !=
  9588. SecondTTPD->isParameterPack()) {
  9589. ODRDiagError(FirstTemplate->getLocation(),
  9590. FirstTemplate->getSourceRange(),
  9591. FunctionTemplatePackParameter)
  9592. << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
  9593. ODRDiagNote(SecondTemplate->getLocation(),
  9594. SecondTemplate->getSourceRange(),
  9595. FunctionTemplatePackParameter)
  9596. << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
  9597. ParameterMismatch = true;
  9598. break;
  9599. }
  9600. }
  9601. if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
  9602. isa<NonTypeTemplateParmDecl>(SecondParam)) {
  9603. NonTypeTemplateParmDecl *FirstNTTPD =
  9604. cast<NonTypeTemplateParmDecl>(FirstParam);
  9605. NonTypeTemplateParmDecl *SecondNTTPD =
  9606. cast<NonTypeTemplateParmDecl>(SecondParam);
  9607. QualType FirstType = FirstNTTPD->getType();
  9608. QualType SecondType = SecondNTTPD->getType();
  9609. if (ComputeQualTypeODRHash(FirstType) !=
  9610. ComputeQualTypeODRHash(SecondType)) {
  9611. ODRDiagError(FirstTemplate->getLocation(),
  9612. FirstTemplate->getSourceRange(),
  9613. FunctionTemplateParameterDifferentType)
  9614. << FirstTemplate << (i + 1);
  9615. ODRDiagNote(SecondTemplate->getLocation(),
  9616. SecondTemplate->getSourceRange(),
  9617. FunctionTemplateParameterDifferentType)
  9618. << SecondTemplate << (i + 1);
  9619. ParameterMismatch = true;
  9620. break;
  9621. }
  9622. bool HasFirstDefaultArgument =
  9623. FirstNTTPD->hasDefaultArgument() &&
  9624. !FirstNTTPD->defaultArgumentWasInherited();
  9625. bool HasSecondDefaultArgument =
  9626. SecondNTTPD->hasDefaultArgument() &&
  9627. !SecondNTTPD->defaultArgumentWasInherited();
  9628. if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
  9629. ODRDiagError(FirstTemplate->getLocation(),
  9630. FirstTemplate->getSourceRange(),
  9631. FunctionTemplateParameterSingleDefaultArgument)
  9632. << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
  9633. ODRDiagNote(SecondTemplate->getLocation(),
  9634. SecondTemplate->getSourceRange(),
  9635. FunctionTemplateParameterSingleDefaultArgument)
  9636. << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
  9637. ParameterMismatch = true;
  9638. break;
  9639. }
  9640. if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
  9641. Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
  9642. Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
  9643. if (ComputeODRHash(FirstDefaultArgument) !=
  9644. ComputeODRHash(SecondDefaultArgument)) {
  9645. ODRDiagError(FirstTemplate->getLocation(),
  9646. FirstTemplate->getSourceRange(),
  9647. FunctionTemplateParameterDifferentDefaultArgument)
  9648. << FirstTemplate << (i + 1) << FirstDefaultArgument;
  9649. ODRDiagNote(SecondTemplate->getLocation(),
  9650. SecondTemplate->getSourceRange(),
  9651. FunctionTemplateParameterDifferentDefaultArgument)
  9652. << SecondTemplate << (i + 1) << SecondDefaultArgument;
  9653. ParameterMismatch = true;
  9654. break;
  9655. }
  9656. }
  9657. if (FirstNTTPD->isParameterPack() !=
  9658. SecondNTTPD->isParameterPack()) {
  9659. ODRDiagError(FirstTemplate->getLocation(),
  9660. FirstTemplate->getSourceRange(),
  9661. FunctionTemplatePackParameter)
  9662. << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
  9663. ODRDiagNote(SecondTemplate->getLocation(),
  9664. SecondTemplate->getSourceRange(),
  9665. FunctionTemplatePackParameter)
  9666. << SecondTemplate << (i + 1)
  9667. << SecondNTTPD->isParameterPack();
  9668. ParameterMismatch = true;
  9669. break;
  9670. }
  9671. }
  9672. }
  9673. if (ParameterMismatch) {
  9674. Diagnosed = true;
  9675. break;
  9676. }
  9677. break;
  9678. }
  9679. }
  9680. if (Diagnosed)
  9681. continue;
  9682. Diag(FirstDecl->getLocation(),
  9683. diag::err_module_odr_violation_mismatch_decl_unknown)
  9684. << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
  9685. << FirstDecl->getSourceRange();
  9686. Diag(SecondDecl->getLocation(),
  9687. diag::note_module_odr_violation_mismatch_decl_unknown)
  9688. << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
  9689. Diagnosed = true;
  9690. }
  9691. if (!Diagnosed) {
  9692. // All definitions are updates to the same declaration. This happens if a
  9693. // module instantiates the declaration of a class template specialization
  9694. // and two or more other modules instantiate its definition.
  9695. //
  9696. // FIXME: Indicate which modules had instantiations of this definition.
  9697. // FIXME: How can this even happen?
  9698. Diag(Merge.first->getLocation(),
  9699. diag::err_module_odr_violation_different_instantiations)
  9700. << Merge.first;
  9701. }
  9702. }
  9703. // Issue ODR failures diagnostics for functions.
  9704. for (auto &Merge : FunctionOdrMergeFailures) {
  9705. enum ODRFunctionDifference {
  9706. ReturnType,
  9707. ParameterName,
  9708. ParameterType,
  9709. ParameterSingleDefaultArgument,
  9710. ParameterDifferentDefaultArgument,
  9711. FunctionBody,
  9712. };
  9713. FunctionDecl *FirstFunction = Merge.first;
  9714. std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
  9715. bool Diagnosed = false;
  9716. for (auto &SecondFunction : Merge.second) {
  9717. if (FirstFunction == SecondFunction)
  9718. continue;
  9719. std::string SecondModule =
  9720. getOwningModuleNameForDiagnostic(SecondFunction);
  9721. auto ODRDiagError = [FirstFunction, &FirstModule,
  9722. this](SourceLocation Loc, SourceRange Range,
  9723. ODRFunctionDifference DiffType) {
  9724. return Diag(Loc, diag::err_module_odr_violation_function)
  9725. << FirstFunction << FirstModule.empty() << FirstModule << Range
  9726. << DiffType;
  9727. };
  9728. auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
  9729. SourceRange Range,
  9730. ODRFunctionDifference DiffType) {
  9731. return Diag(Loc, diag::note_module_odr_violation_function)
  9732. << SecondModule << Range << DiffType;
  9733. };
  9734. if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
  9735. ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
  9736. ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
  9737. FirstFunction->getReturnTypeSourceRange(), ReturnType)
  9738. << FirstFunction->getReturnType();
  9739. ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
  9740. SecondFunction->getReturnTypeSourceRange(), ReturnType)
  9741. << SecondFunction->getReturnType();
  9742. Diagnosed = true;
  9743. break;
  9744. }
  9745. assert(FirstFunction->param_size() == SecondFunction->param_size() &&
  9746. "Merged functions with different number of parameters");
  9747. auto ParamSize = FirstFunction->param_size();
  9748. bool ParameterMismatch = false;
  9749. for (unsigned I = 0; I < ParamSize; ++I) {
  9750. auto *FirstParam = FirstFunction->getParamDecl(I);
  9751. auto *SecondParam = SecondFunction->getParamDecl(I);
  9752. assert(getContext().hasSameType(FirstParam->getType(),
  9753. SecondParam->getType()) &&
  9754. "Merged function has different parameter types.");
  9755. if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
  9756. ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
  9757. ParameterName)
  9758. << I + 1 << FirstParam->getDeclName();
  9759. ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
  9760. ParameterName)
  9761. << I + 1 << SecondParam->getDeclName();
  9762. ParameterMismatch = true;
  9763. break;
  9764. };
  9765. QualType FirstParamType = FirstParam->getType();
  9766. QualType SecondParamType = SecondParam->getType();
  9767. if (FirstParamType != SecondParamType &&
  9768. ComputeQualTypeODRHash(FirstParamType) !=
  9769. ComputeQualTypeODRHash(SecondParamType)) {
  9770. if (const DecayedType *ParamDecayedType =
  9771. FirstParamType->getAs<DecayedType>()) {
  9772. ODRDiagError(FirstParam->getLocation(),
  9773. FirstParam->getSourceRange(), ParameterType)
  9774. << (I + 1) << FirstParamType << true
  9775. << ParamDecayedType->getOriginalType();
  9776. } else {
  9777. ODRDiagError(FirstParam->getLocation(),
  9778. FirstParam->getSourceRange(), ParameterType)
  9779. << (I + 1) << FirstParamType << false;
  9780. }
  9781. if (const DecayedType *ParamDecayedType =
  9782. SecondParamType->getAs<DecayedType>()) {
  9783. ODRDiagNote(SecondParam->getLocation(),
  9784. SecondParam->getSourceRange(), ParameterType)
  9785. << (I + 1) << SecondParamType << true
  9786. << ParamDecayedType->getOriginalType();
  9787. } else {
  9788. ODRDiagNote(SecondParam->getLocation(),
  9789. SecondParam->getSourceRange(), ParameterType)
  9790. << (I + 1) << SecondParamType << false;
  9791. }
  9792. ParameterMismatch = true;
  9793. break;
  9794. }
  9795. const Expr *FirstInit = FirstParam->getInit();
  9796. const Expr *SecondInit = SecondParam->getInit();
  9797. if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
  9798. ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
  9799. ParameterSingleDefaultArgument)
  9800. << (I + 1) << (FirstInit == nullptr)
  9801. << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
  9802. ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
  9803. ParameterSingleDefaultArgument)
  9804. << (I + 1) << (SecondInit == nullptr)
  9805. << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
  9806. ParameterMismatch = true;
  9807. break;
  9808. }
  9809. if (FirstInit && SecondInit &&
  9810. ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
  9811. ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
  9812. ParameterDifferentDefaultArgument)
  9813. << (I + 1) << FirstInit->getSourceRange();
  9814. ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
  9815. ParameterDifferentDefaultArgument)
  9816. << (I + 1) << SecondInit->getSourceRange();
  9817. ParameterMismatch = true;
  9818. break;
  9819. }
  9820. assert(ComputeSubDeclODRHash(FirstParam) ==
  9821. ComputeSubDeclODRHash(SecondParam) &&
  9822. "Undiagnosed parameter difference.");
  9823. }
  9824. if (ParameterMismatch) {
  9825. Diagnosed = true;
  9826. break;
  9827. }
  9828. // If no error has been generated before now, assume the problem is in
  9829. // the body and generate a message.
  9830. ODRDiagError(FirstFunction->getLocation(),
  9831. FirstFunction->getSourceRange(), FunctionBody);
  9832. ODRDiagNote(SecondFunction->getLocation(),
  9833. SecondFunction->getSourceRange(), FunctionBody);
  9834. Diagnosed = true;
  9835. break;
  9836. }
  9837. (void)Diagnosed;
  9838. assert(Diagnosed && "Unable to emit ODR diagnostic.");
  9839. }
  9840. // Issue ODR failures diagnostics for enums.
  9841. for (auto &Merge : EnumOdrMergeFailures) {
  9842. enum ODREnumDifference {
  9843. SingleScopedEnum,
  9844. EnumTagKeywordMismatch,
  9845. SingleSpecifiedType,
  9846. DifferentSpecifiedTypes,
  9847. DifferentNumberEnumConstants,
  9848. EnumConstantName,
  9849. EnumConstantSingleInitilizer,
  9850. EnumConstantDifferentInitilizer,
  9851. };
  9852. // If we've already pointed out a specific problem with this enum, don't
  9853. // bother issuing a general "something's different" diagnostic.
  9854. if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
  9855. continue;
  9856. EnumDecl *FirstEnum = Merge.first;
  9857. std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
  9858. using DeclHashes =
  9859. llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
  9860. auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
  9861. DeclHashes &Hashes, EnumDecl *Enum) {
  9862. for (auto *D : Enum->decls()) {
  9863. // Due to decl merging, the first EnumDecl is the parent of
  9864. // Decls in both records.
  9865. if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
  9866. continue;
  9867. assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
  9868. Hashes.emplace_back(cast<EnumConstantDecl>(D),
  9869. ComputeSubDeclODRHash(D));
  9870. }
  9871. };
  9872. DeclHashes FirstHashes;
  9873. PopulateHashes(FirstHashes, FirstEnum);
  9874. bool Diagnosed = false;
  9875. for (auto &SecondEnum : Merge.second) {
  9876. if (FirstEnum == SecondEnum)
  9877. continue;
  9878. std::string SecondModule =
  9879. getOwningModuleNameForDiagnostic(SecondEnum);
  9880. auto ODRDiagError = [FirstEnum, &FirstModule,
  9881. this](SourceLocation Loc, SourceRange Range,
  9882. ODREnumDifference DiffType) {
  9883. return Diag(Loc, diag::err_module_odr_violation_enum)
  9884. << FirstEnum << FirstModule.empty() << FirstModule << Range
  9885. << DiffType;
  9886. };
  9887. auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
  9888. SourceRange Range,
  9889. ODREnumDifference DiffType) {
  9890. return Diag(Loc, diag::note_module_odr_violation_enum)
  9891. << SecondModule << Range << DiffType;
  9892. };
  9893. if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
  9894. ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
  9895. SingleScopedEnum)
  9896. << FirstEnum->isScoped();
  9897. ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
  9898. SingleScopedEnum)
  9899. << SecondEnum->isScoped();
  9900. Diagnosed = true;
  9901. continue;
  9902. }
  9903. if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
  9904. if (FirstEnum->isScopedUsingClassTag() !=
  9905. SecondEnum->isScopedUsingClassTag()) {
  9906. ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
  9907. EnumTagKeywordMismatch)
  9908. << FirstEnum->isScopedUsingClassTag();
  9909. ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
  9910. EnumTagKeywordMismatch)
  9911. << SecondEnum->isScopedUsingClassTag();
  9912. Diagnosed = true;
  9913. continue;
  9914. }
  9915. }
  9916. QualType FirstUnderlyingType =
  9917. FirstEnum->getIntegerTypeSourceInfo()
  9918. ? FirstEnum->getIntegerTypeSourceInfo()->getType()
  9919. : QualType();
  9920. QualType SecondUnderlyingType =
  9921. SecondEnum->getIntegerTypeSourceInfo()
  9922. ? SecondEnum->getIntegerTypeSourceInfo()->getType()
  9923. : QualType();
  9924. if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
  9925. ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
  9926. SingleSpecifiedType)
  9927. << !FirstUnderlyingType.isNull();
  9928. ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
  9929. SingleSpecifiedType)
  9930. << !SecondUnderlyingType.isNull();
  9931. Diagnosed = true;
  9932. continue;
  9933. }
  9934. if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
  9935. if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
  9936. ComputeQualTypeODRHash(SecondUnderlyingType)) {
  9937. ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
  9938. DifferentSpecifiedTypes)
  9939. << FirstUnderlyingType;
  9940. ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
  9941. DifferentSpecifiedTypes)
  9942. << SecondUnderlyingType;
  9943. Diagnosed = true;
  9944. continue;
  9945. }
  9946. }
  9947. DeclHashes SecondHashes;
  9948. PopulateHashes(SecondHashes, SecondEnum);
  9949. if (FirstHashes.size() != SecondHashes.size()) {
  9950. ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
  9951. DifferentNumberEnumConstants)
  9952. << (int)FirstHashes.size();
  9953. ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
  9954. DifferentNumberEnumConstants)
  9955. << (int)SecondHashes.size();
  9956. Diagnosed = true;
  9957. continue;
  9958. }
  9959. for (unsigned I = 0; I < FirstHashes.size(); ++I) {
  9960. if (FirstHashes[I].second == SecondHashes[I].second)
  9961. continue;
  9962. const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
  9963. const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
  9964. if (FirstEnumConstant->getDeclName() !=
  9965. SecondEnumConstant->getDeclName()) {
  9966. ODRDiagError(FirstEnumConstant->getLocation(),
  9967. FirstEnumConstant->getSourceRange(), EnumConstantName)
  9968. << I + 1 << FirstEnumConstant;
  9969. ODRDiagNote(SecondEnumConstant->getLocation(),
  9970. SecondEnumConstant->getSourceRange(), EnumConstantName)
  9971. << I + 1 << SecondEnumConstant;
  9972. Diagnosed = true;
  9973. break;
  9974. }
  9975. const Expr *FirstInit = FirstEnumConstant->getInitExpr();
  9976. const Expr *SecondInit = SecondEnumConstant->getInitExpr();
  9977. if (!FirstInit && !SecondInit)
  9978. continue;
  9979. if (!FirstInit || !SecondInit) {
  9980. ODRDiagError(FirstEnumConstant->getLocation(),
  9981. FirstEnumConstant->getSourceRange(),
  9982. EnumConstantSingleInitilizer)
  9983. << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
  9984. ODRDiagNote(SecondEnumConstant->getLocation(),
  9985. SecondEnumConstant->getSourceRange(),
  9986. EnumConstantSingleInitilizer)
  9987. << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
  9988. Diagnosed = true;
  9989. break;
  9990. }
  9991. if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
  9992. ODRDiagError(FirstEnumConstant->getLocation(),
  9993. FirstEnumConstant->getSourceRange(),
  9994. EnumConstantDifferentInitilizer)
  9995. << I + 1 << FirstEnumConstant;
  9996. ODRDiagNote(SecondEnumConstant->getLocation(),
  9997. SecondEnumConstant->getSourceRange(),
  9998. EnumConstantDifferentInitilizer)
  9999. << I + 1 << SecondEnumConstant;
  10000. Diagnosed = true;
  10001. break;
  10002. }
  10003. }
  10004. }
  10005. (void)Diagnosed;
  10006. assert(Diagnosed && "Unable to emit ODR diagnostic.");
  10007. }
  10008. }
  10009. void ASTReader::StartedDeserializing() {
  10010. if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
  10011. ReadTimer->startTimer();
  10012. }
  10013. void ASTReader::FinishedDeserializing() {
  10014. assert(NumCurrentElementsDeserializing &&
  10015. "FinishedDeserializing not paired with StartedDeserializing");
  10016. if (NumCurrentElementsDeserializing == 1) {
  10017. // We decrease NumCurrentElementsDeserializing only after pending actions
  10018. // are finished, to avoid recursively re-calling finishPendingActions().
  10019. finishPendingActions();
  10020. }
  10021. --NumCurrentElementsDeserializing;
  10022. if (NumCurrentElementsDeserializing == 0) {
  10023. // Propagate exception specification and deduced type updates along
  10024. // redeclaration chains.
  10025. //
  10026. // We do this now rather than in finishPendingActions because we want to
  10027. // be able to walk the complete redeclaration chains of the updated decls.
  10028. while (!PendingExceptionSpecUpdates.empty() ||
  10029. !PendingDeducedTypeUpdates.empty()) {
  10030. auto ESUpdates = std::move(PendingExceptionSpecUpdates);
  10031. PendingExceptionSpecUpdates.clear();
  10032. for (auto Update : ESUpdates) {
  10033. ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
  10034. auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
  10035. auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
  10036. if (auto *Listener = getContext().getASTMutationListener())
  10037. Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
  10038. for (auto *Redecl : Update.second->redecls())
  10039. getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
  10040. }
  10041. auto DTUpdates = std::move(PendingDeducedTypeUpdates);
  10042. PendingDeducedTypeUpdates.clear();
  10043. for (auto Update : DTUpdates) {
  10044. ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
  10045. // FIXME: If the return type is already deduced, check that it matches.
  10046. getContext().adjustDeducedFunctionResultType(Update.first,
  10047. Update.second);
  10048. }
  10049. }
  10050. if (ReadTimer)
  10051. ReadTimer->stopTimer();
  10052. diagnoseOdrViolations();
  10053. // We are not in recursive loading, so it's safe to pass the "interesting"
  10054. // decls to the consumer.
  10055. if (Consumer)
  10056. PassInterestingDeclsToConsumer();
  10057. }
  10058. }
  10059. void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
  10060. if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
  10061. // Remove any fake results before adding any real ones.
  10062. auto It = PendingFakeLookupResults.find(II);
  10063. if (It != PendingFakeLookupResults.end()) {
  10064. for (auto *ND : It->second)
  10065. SemaObj->IdResolver.RemoveDecl(ND);
  10066. // FIXME: this works around module+PCH performance issue.
  10067. // Rather than erase the result from the map, which is O(n), just clear
  10068. // the vector of NamedDecls.
  10069. It->second.clear();
  10070. }
  10071. }
  10072. if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
  10073. SemaObj->TUScope->AddDecl(D);
  10074. } else if (SemaObj->TUScope) {
  10075. // Adding the decl to IdResolver may have failed because it was already in
  10076. // (even though it was not added in scope). If it is already in, make sure
  10077. // it gets in the scope as well.
  10078. if (std::find(SemaObj->IdResolver.begin(Name),
  10079. SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
  10080. SemaObj->TUScope->AddDecl(D);
  10081. }
  10082. }
  10083. ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
  10084. const PCHContainerReader &PCHContainerRdr,
  10085. ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
  10086. StringRef isysroot, bool DisableValidation,
  10087. bool AllowASTWithCompilerErrors,
  10088. bool AllowConfigurationMismatch, bool ValidateSystemInputs,
  10089. bool UseGlobalIndex,
  10090. std::unique_ptr<llvm::Timer> ReadTimer)
  10091. : Listener(DisableValidation
  10092. ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
  10093. : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
  10094. SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
  10095. PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
  10096. ContextObj(Context),
  10097. ModuleMgr(PP.getFileManager(), PP.getPCMCache(), PCHContainerRdr,
  10098. PP.getHeaderSearchInfo()),
  10099. PCMCache(PP.getPCMCache()), DummyIdResolver(PP),
  10100. ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
  10101. DisableValidation(DisableValidation),
  10102. AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
  10103. AllowConfigurationMismatch(AllowConfigurationMismatch),
  10104. ValidateSystemInputs(ValidateSystemInputs),
  10105. UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
  10106. SourceMgr.setExternalSLocEntrySource(this);
  10107. for (const auto &Ext : Extensions) {
  10108. auto BlockName = Ext->getExtensionMetadata().BlockName;
  10109. auto Known = ModuleFileExtensions.find(BlockName);
  10110. if (Known != ModuleFileExtensions.end()) {
  10111. Diags.Report(diag::warn_duplicate_module_file_extension)
  10112. << BlockName;
  10113. continue;
  10114. }
  10115. ModuleFileExtensions.insert({BlockName, Ext});
  10116. }
  10117. }
  10118. ASTReader::~ASTReader() {
  10119. if (OwnsDeserializationListener)
  10120. delete DeserializationListener;
  10121. }
  10122. IdentifierResolver &ASTReader::getIdResolver() {
  10123. return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
  10124. }
  10125. unsigned ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
  10126. unsigned AbbrevID) {
  10127. Idx = 0;
  10128. Record.clear();
  10129. return Cursor.readRecord(AbbrevID, Record);
  10130. }
  10131. //===----------------------------------------------------------------------===//
  10132. //// OMPClauseReader implementation
  10133. ////===----------------------------------------------------------------------===//
  10134. OMPClause *OMPClauseReader::readClause() {
  10135. OMPClause *C;
  10136. switch (Record.readInt()) {
  10137. case OMPC_if:
  10138. C = new (Context) OMPIfClause();
  10139. break;
  10140. case OMPC_final:
  10141. C = new (Context) OMPFinalClause();
  10142. break;
  10143. case OMPC_num_threads:
  10144. C = new (Context) OMPNumThreadsClause();
  10145. break;
  10146. case OMPC_safelen:
  10147. C = new (Context) OMPSafelenClause();
  10148. break;
  10149. case OMPC_simdlen:
  10150. C = new (Context) OMPSimdlenClause();
  10151. break;
  10152. case OMPC_collapse:
  10153. C = new (Context) OMPCollapseClause();
  10154. break;
  10155. case OMPC_default:
  10156. C = new (Context) OMPDefaultClause();
  10157. break;
  10158. case OMPC_proc_bind:
  10159. C = new (Context) OMPProcBindClause();
  10160. break;
  10161. case OMPC_schedule:
  10162. C = new (Context) OMPScheduleClause();
  10163. break;
  10164. case OMPC_ordered:
  10165. C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
  10166. break;
  10167. case OMPC_nowait:
  10168. C = new (Context) OMPNowaitClause();
  10169. break;
  10170. case OMPC_untied:
  10171. C = new (Context) OMPUntiedClause();
  10172. break;
  10173. case OMPC_mergeable:
  10174. C = new (Context) OMPMergeableClause();
  10175. break;
  10176. case OMPC_read:
  10177. C = new (Context) OMPReadClause();
  10178. break;
  10179. case OMPC_write:
  10180. C = new (Context) OMPWriteClause();
  10181. break;
  10182. case OMPC_update:
  10183. C = new (Context) OMPUpdateClause();
  10184. break;
  10185. case OMPC_capture:
  10186. C = new (Context) OMPCaptureClause();
  10187. break;
  10188. case OMPC_seq_cst:
  10189. C = new (Context) OMPSeqCstClause();
  10190. break;
  10191. case OMPC_threads:
  10192. C = new (Context) OMPThreadsClause();
  10193. break;
  10194. case OMPC_simd:
  10195. C = new (Context) OMPSIMDClause();
  10196. break;
  10197. case OMPC_nogroup:
  10198. C = new (Context) OMPNogroupClause();
  10199. break;
  10200. case OMPC_unified_address:
  10201. C = new (Context) OMPUnifiedAddressClause();
  10202. break;
  10203. case OMPC_unified_shared_memory:
  10204. C = new (Context) OMPUnifiedSharedMemoryClause();
  10205. break;
  10206. case OMPC_reverse_offload:
  10207. C = new (Context) OMPReverseOffloadClause();
  10208. break;
  10209. case OMPC_dynamic_allocators:
  10210. C = new (Context) OMPDynamicAllocatorsClause();
  10211. break;
  10212. case OMPC_atomic_default_mem_order:
  10213. C = new (Context) OMPAtomicDefaultMemOrderClause();
  10214. break;
  10215. case OMPC_private:
  10216. C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
  10217. break;
  10218. case OMPC_firstprivate:
  10219. C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
  10220. break;
  10221. case OMPC_lastprivate:
  10222. C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
  10223. break;
  10224. case OMPC_shared:
  10225. C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
  10226. break;
  10227. case OMPC_reduction:
  10228. C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
  10229. break;
  10230. case OMPC_task_reduction:
  10231. C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
  10232. break;
  10233. case OMPC_in_reduction:
  10234. C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
  10235. break;
  10236. case OMPC_linear:
  10237. C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
  10238. break;
  10239. case OMPC_aligned:
  10240. C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
  10241. break;
  10242. case OMPC_copyin:
  10243. C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
  10244. break;
  10245. case OMPC_copyprivate:
  10246. C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
  10247. break;
  10248. case OMPC_flush:
  10249. C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
  10250. break;
  10251. case OMPC_depend: {
  10252. unsigned NumVars = Record.readInt();
  10253. unsigned NumLoops = Record.readInt();
  10254. C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
  10255. break;
  10256. }
  10257. case OMPC_device:
  10258. C = new (Context) OMPDeviceClause();
  10259. break;
  10260. case OMPC_map: {
  10261. unsigned NumVars = Record.readInt();
  10262. unsigned NumDeclarations = Record.readInt();
  10263. unsigned NumLists = Record.readInt();
  10264. unsigned NumComponents = Record.readInt();
  10265. C = OMPMapClause::CreateEmpty(Context, NumVars, NumDeclarations, NumLists,
  10266. NumComponents);
  10267. break;
  10268. }
  10269. case OMPC_num_teams:
  10270. C = new (Context) OMPNumTeamsClause();
  10271. break;
  10272. case OMPC_thread_limit:
  10273. C = new (Context) OMPThreadLimitClause();
  10274. break;
  10275. case OMPC_priority:
  10276. C = new (Context) OMPPriorityClause();
  10277. break;
  10278. case OMPC_grainsize:
  10279. C = new (Context) OMPGrainsizeClause();
  10280. break;
  10281. case OMPC_num_tasks:
  10282. C = new (Context) OMPNumTasksClause();
  10283. break;
  10284. case OMPC_hint:
  10285. C = new (Context) OMPHintClause();
  10286. break;
  10287. case OMPC_dist_schedule:
  10288. C = new (Context) OMPDistScheduleClause();
  10289. break;
  10290. case OMPC_defaultmap:
  10291. C = new (Context) OMPDefaultmapClause();
  10292. break;
  10293. case OMPC_to: {
  10294. unsigned NumVars = Record.readInt();
  10295. unsigned NumDeclarations = Record.readInt();
  10296. unsigned NumLists = Record.readInt();
  10297. unsigned NumComponents = Record.readInt();
  10298. C = OMPToClause::CreateEmpty(Context, NumVars, NumDeclarations, NumLists,
  10299. NumComponents);
  10300. break;
  10301. }
  10302. case OMPC_from: {
  10303. unsigned NumVars = Record.readInt();
  10304. unsigned NumDeclarations = Record.readInt();
  10305. unsigned NumLists = Record.readInt();
  10306. unsigned NumComponents = Record.readInt();
  10307. C = OMPFromClause::CreateEmpty(Context, NumVars, NumDeclarations, NumLists,
  10308. NumComponents);
  10309. break;
  10310. }
  10311. case OMPC_use_device_ptr: {
  10312. unsigned NumVars = Record.readInt();
  10313. unsigned NumDeclarations = Record.readInt();
  10314. unsigned NumLists = Record.readInt();
  10315. unsigned NumComponents = Record.readInt();
  10316. C = OMPUseDevicePtrClause::CreateEmpty(Context, NumVars, NumDeclarations,
  10317. NumLists, NumComponents);
  10318. break;
  10319. }
  10320. case OMPC_is_device_ptr: {
  10321. unsigned NumVars = Record.readInt();
  10322. unsigned NumDeclarations = Record.readInt();
  10323. unsigned NumLists = Record.readInt();
  10324. unsigned NumComponents = Record.readInt();
  10325. C = OMPIsDevicePtrClause::CreateEmpty(Context, NumVars, NumDeclarations,
  10326. NumLists, NumComponents);
  10327. break;
  10328. }
  10329. }
  10330. Visit(C);
  10331. C->setLocStart(Record.readSourceLocation());
  10332. C->setLocEnd(Record.readSourceLocation());
  10333. return C;
  10334. }
  10335. void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
  10336. C->setPreInitStmt(Record.readSubStmt(),
  10337. static_cast<OpenMPDirectiveKind>(Record.readInt()));
  10338. }
  10339. void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
  10340. VisitOMPClauseWithPreInit(C);
  10341. C->setPostUpdateExpr(Record.readSubExpr());
  10342. }
  10343. void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
  10344. VisitOMPClauseWithPreInit(C);
  10345. C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
  10346. C->setNameModifierLoc(Record.readSourceLocation());
  10347. C->setColonLoc(Record.readSourceLocation());
  10348. C->setCondition(Record.readSubExpr());
  10349. C->setLParenLoc(Record.readSourceLocation());
  10350. }
  10351. void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
  10352. C->setCondition(Record.readSubExpr());
  10353. C->setLParenLoc(Record.readSourceLocation());
  10354. }
  10355. void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
  10356. VisitOMPClauseWithPreInit(C);
  10357. C->setNumThreads(Record.readSubExpr());
  10358. C->setLParenLoc(Record.readSourceLocation());
  10359. }
  10360. void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
  10361. C->setSafelen(Record.readSubExpr());
  10362. C->setLParenLoc(Record.readSourceLocation());
  10363. }
  10364. void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
  10365. C->setSimdlen(Record.readSubExpr());
  10366. C->setLParenLoc(Record.readSourceLocation());
  10367. }
  10368. void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
  10369. C->setNumForLoops(Record.readSubExpr());
  10370. C->setLParenLoc(Record.readSourceLocation());
  10371. }
  10372. void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
  10373. C->setDefaultKind(
  10374. static_cast<OpenMPDefaultClauseKind>(Record.readInt()));
  10375. C->setLParenLoc(Record.readSourceLocation());
  10376. C->setDefaultKindKwLoc(Record.readSourceLocation());
  10377. }
  10378. void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
  10379. C->setProcBindKind(
  10380. static_cast<OpenMPProcBindClauseKind>(Record.readInt()));
  10381. C->setLParenLoc(Record.readSourceLocation());
  10382. C->setProcBindKindKwLoc(Record.readSourceLocation());
  10383. }
  10384. void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
  10385. VisitOMPClauseWithPreInit(C);
  10386. C->setScheduleKind(
  10387. static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
  10388. C->setFirstScheduleModifier(
  10389. static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
  10390. C->setSecondScheduleModifier(
  10391. static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
  10392. C->setChunkSize(Record.readSubExpr());
  10393. C->setLParenLoc(Record.readSourceLocation());
  10394. C->setFirstScheduleModifierLoc(Record.readSourceLocation());
  10395. C->setSecondScheduleModifierLoc(Record.readSourceLocation());
  10396. C->setScheduleKindLoc(Record.readSourceLocation());
  10397. C->setCommaLoc(Record.readSourceLocation());
  10398. }
  10399. void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
  10400. C->setNumForLoops(Record.readSubExpr());
  10401. for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
  10402. C->setLoopNumIterations(I, Record.readSubExpr());
  10403. for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
  10404. C->setLoopCounter(I, Record.readSubExpr());
  10405. C->setLParenLoc(Record.readSourceLocation());
  10406. }
  10407. void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
  10408. void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
  10409. void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
  10410. void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
  10411. void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
  10412. void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
  10413. void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
  10414. void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
  10415. void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
  10416. void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
  10417. void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
  10418. void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
  10419. void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
  10420. OMPUnifiedSharedMemoryClause *) {}
  10421. void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
  10422. void
  10423. OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
  10424. }
  10425. void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
  10426. OMPAtomicDefaultMemOrderClause *C) {
  10427. C->setAtomicDefaultMemOrderKind(
  10428. static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
  10429. C->setLParenLoc(Record.readSourceLocation());
  10430. C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
  10431. }
  10432. void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
  10433. C->setLParenLoc(Record.readSourceLocation());
  10434. unsigned NumVars = C->varlist_size();
  10435. SmallVector<Expr *, 16> Vars;
  10436. Vars.reserve(NumVars);
  10437. for (unsigned i = 0; i != NumVars; ++i)
  10438. Vars.push_back(Record.readSubExpr());
  10439. C->setVarRefs(Vars);
  10440. Vars.clear();
  10441. for (unsigned i = 0; i != NumVars; ++i)
  10442. Vars.push_back(Record.readSubExpr());
  10443. C->setPrivateCopies(Vars);
  10444. }
  10445. void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
  10446. VisitOMPClauseWithPreInit(C);
  10447. C->setLParenLoc(Record.readSourceLocation());
  10448. unsigned NumVars = C->varlist_size();
  10449. SmallVector<Expr *, 16> Vars;
  10450. Vars.reserve(NumVars);
  10451. for (unsigned i = 0; i != NumVars; ++i)
  10452. Vars.push_back(Record.readSubExpr());
  10453. C->setVarRefs(Vars);
  10454. Vars.clear();
  10455. for (unsigned i = 0; i != NumVars; ++i)
  10456. Vars.push_back(Record.readSubExpr());
  10457. C->setPrivateCopies(Vars);
  10458. Vars.clear();
  10459. for (unsigned i = 0; i != NumVars; ++i)
  10460. Vars.push_back(Record.readSubExpr());
  10461. C->setInits(Vars);
  10462. }
  10463. void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
  10464. VisitOMPClauseWithPostUpdate(C);
  10465. C->setLParenLoc(Record.readSourceLocation());
  10466. unsigned NumVars = C->varlist_size();
  10467. SmallVector<Expr *, 16> Vars;
  10468. Vars.reserve(NumVars);
  10469. for (unsigned i = 0; i != NumVars; ++i)
  10470. Vars.push_back(Record.readSubExpr());
  10471. C->setVarRefs(Vars);
  10472. Vars.clear();
  10473. for (unsigned i = 0; i != NumVars; ++i)
  10474. Vars.push_back(Record.readSubExpr());
  10475. C->setPrivateCopies(Vars);
  10476. Vars.clear();
  10477. for (unsigned i = 0; i != NumVars; ++i)
  10478. Vars.push_back(Record.readSubExpr());
  10479. C->setSourceExprs(Vars);
  10480. Vars.clear();
  10481. for (unsigned i = 0; i != NumVars; ++i)
  10482. Vars.push_back(Record.readSubExpr());
  10483. C->setDestinationExprs(Vars);
  10484. Vars.clear();
  10485. for (unsigned i = 0; i != NumVars; ++i)
  10486. Vars.push_back(Record.readSubExpr());
  10487. C->setAssignmentOps(Vars);
  10488. }
  10489. void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
  10490. C->setLParenLoc(Record.readSourceLocation());
  10491. unsigned NumVars = C->varlist_size();
  10492. SmallVector<Expr *, 16> Vars;
  10493. Vars.reserve(NumVars);
  10494. for (unsigned i = 0; i != NumVars; ++i)
  10495. Vars.push_back(Record.readSubExpr());
  10496. C->setVarRefs(Vars);
  10497. }
  10498. void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
  10499. VisitOMPClauseWithPostUpdate(C);
  10500. C->setLParenLoc(Record.readSourceLocation());
  10501. C->setColonLoc(Record.readSourceLocation());
  10502. NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
  10503. DeclarationNameInfo DNI;
  10504. Record.readDeclarationNameInfo(DNI);
  10505. C->setQualifierLoc(NNSL);
  10506. C->setNameInfo(DNI);
  10507. unsigned NumVars = C->varlist_size();
  10508. SmallVector<Expr *, 16> Vars;
  10509. Vars.reserve(NumVars);
  10510. for (unsigned i = 0; i != NumVars; ++i)
  10511. Vars.push_back(Record.readSubExpr());
  10512. C->setVarRefs(Vars);
  10513. Vars.clear();
  10514. for (unsigned i = 0; i != NumVars; ++i)
  10515. Vars.push_back(Record.readSubExpr());
  10516. C->setPrivates(Vars);
  10517. Vars.clear();
  10518. for (unsigned i = 0; i != NumVars; ++i)
  10519. Vars.push_back(Record.readSubExpr());
  10520. C->setLHSExprs(Vars);
  10521. Vars.clear();
  10522. for (unsigned i = 0; i != NumVars; ++i)
  10523. Vars.push_back(Record.readSubExpr());
  10524. C->setRHSExprs(Vars);
  10525. Vars.clear();
  10526. for (unsigned i = 0; i != NumVars; ++i)
  10527. Vars.push_back(Record.readSubExpr());
  10528. C->setReductionOps(Vars);
  10529. }
  10530. void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
  10531. VisitOMPClauseWithPostUpdate(C);
  10532. C->setLParenLoc(Record.readSourceLocation());
  10533. C->setColonLoc(Record.readSourceLocation());
  10534. NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
  10535. DeclarationNameInfo DNI;
  10536. Record.readDeclarationNameInfo(DNI);
  10537. C->setQualifierLoc(NNSL);
  10538. C->setNameInfo(DNI);
  10539. unsigned NumVars = C->varlist_size();
  10540. SmallVector<Expr *, 16> Vars;
  10541. Vars.reserve(NumVars);
  10542. for (unsigned I = 0; I != NumVars; ++I)
  10543. Vars.push_back(Record.readSubExpr());
  10544. C->setVarRefs(Vars);
  10545. Vars.clear();
  10546. for (unsigned I = 0; I != NumVars; ++I)
  10547. Vars.push_back(Record.readSubExpr());
  10548. C->setPrivates(Vars);
  10549. Vars.clear();
  10550. for (unsigned I = 0; I != NumVars; ++I)
  10551. Vars.push_back(Record.readSubExpr());
  10552. C->setLHSExprs(Vars);
  10553. Vars.clear();
  10554. for (unsigned I = 0; I != NumVars; ++I)
  10555. Vars.push_back(Record.readSubExpr());
  10556. C->setRHSExprs(Vars);
  10557. Vars.clear();
  10558. for (unsigned I = 0; I != NumVars; ++I)
  10559. Vars.push_back(Record.readSubExpr());
  10560. C->setReductionOps(Vars);
  10561. }
  10562. void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
  10563. VisitOMPClauseWithPostUpdate(C);
  10564. C->setLParenLoc(Record.readSourceLocation());
  10565. C->setColonLoc(Record.readSourceLocation());
  10566. NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
  10567. DeclarationNameInfo DNI;
  10568. Record.readDeclarationNameInfo(DNI);
  10569. C->setQualifierLoc(NNSL);
  10570. C->setNameInfo(DNI);
  10571. unsigned NumVars = C->varlist_size();
  10572. SmallVector<Expr *, 16> Vars;
  10573. Vars.reserve(NumVars);
  10574. for (unsigned I = 0; I != NumVars; ++I)
  10575. Vars.push_back(Record.readSubExpr());
  10576. C->setVarRefs(Vars);
  10577. Vars.clear();
  10578. for (unsigned I = 0; I != NumVars; ++I)
  10579. Vars.push_back(Record.readSubExpr());
  10580. C->setPrivates(Vars);
  10581. Vars.clear();
  10582. for (unsigned I = 0; I != NumVars; ++I)
  10583. Vars.push_back(Record.readSubExpr());
  10584. C->setLHSExprs(Vars);
  10585. Vars.clear();
  10586. for (unsigned I = 0; I != NumVars; ++I)
  10587. Vars.push_back(Record.readSubExpr());
  10588. C->setRHSExprs(Vars);
  10589. Vars.clear();
  10590. for (unsigned I = 0; I != NumVars; ++I)
  10591. Vars.push_back(Record.readSubExpr());
  10592. C->setReductionOps(Vars);
  10593. Vars.clear();
  10594. for (unsigned I = 0; I != NumVars; ++I)
  10595. Vars.push_back(Record.readSubExpr());
  10596. C->setTaskgroupDescriptors(Vars);
  10597. }
  10598. void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
  10599. VisitOMPClauseWithPostUpdate(C);
  10600. C->setLParenLoc(Record.readSourceLocation());
  10601. C->setColonLoc(Record.readSourceLocation());
  10602. C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
  10603. C->setModifierLoc(Record.readSourceLocation());
  10604. unsigned NumVars = C->varlist_size();
  10605. SmallVector<Expr *, 16> Vars;
  10606. Vars.reserve(NumVars);
  10607. for (unsigned i = 0; i != NumVars; ++i)
  10608. Vars.push_back(Record.readSubExpr());
  10609. C->setVarRefs(Vars);
  10610. Vars.clear();
  10611. for (unsigned i = 0; i != NumVars; ++i)
  10612. Vars.push_back(Record.readSubExpr());
  10613. C->setPrivates(Vars);
  10614. Vars.clear();
  10615. for (unsigned i = 0; i != NumVars; ++i)
  10616. Vars.push_back(Record.readSubExpr());
  10617. C->setInits(Vars);
  10618. Vars.clear();
  10619. for (unsigned i = 0; i != NumVars; ++i)
  10620. Vars.push_back(Record.readSubExpr());
  10621. C->setUpdates(Vars);
  10622. Vars.clear();
  10623. for (unsigned i = 0; i != NumVars; ++i)
  10624. Vars.push_back(Record.readSubExpr());
  10625. C->setFinals(Vars);
  10626. C->setStep(Record.readSubExpr());
  10627. C->setCalcStep(Record.readSubExpr());
  10628. }
  10629. void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
  10630. C->setLParenLoc(Record.readSourceLocation());
  10631. C->setColonLoc(Record.readSourceLocation());
  10632. unsigned NumVars = C->varlist_size();
  10633. SmallVector<Expr *, 16> Vars;
  10634. Vars.reserve(NumVars);
  10635. for (unsigned i = 0; i != NumVars; ++i)
  10636. Vars.push_back(Record.readSubExpr());
  10637. C->setVarRefs(Vars);
  10638. C->setAlignment(Record.readSubExpr());
  10639. }
  10640. void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
  10641. C->setLParenLoc(Record.readSourceLocation());
  10642. unsigned NumVars = C->varlist_size();
  10643. SmallVector<Expr *, 16> Exprs;
  10644. Exprs.reserve(NumVars);
  10645. for (unsigned i = 0; i != NumVars; ++i)
  10646. Exprs.push_back(Record.readSubExpr());
  10647. C->setVarRefs(Exprs);
  10648. Exprs.clear();
  10649. for (unsigned i = 0; i != NumVars; ++i)
  10650. Exprs.push_back(Record.readSubExpr());
  10651. C->setSourceExprs(Exprs);
  10652. Exprs.clear();
  10653. for (unsigned i = 0; i != NumVars; ++i)
  10654. Exprs.push_back(Record.readSubExpr());
  10655. C->setDestinationExprs(Exprs);
  10656. Exprs.clear();
  10657. for (unsigned i = 0; i != NumVars; ++i)
  10658. Exprs.push_back(Record.readSubExpr());
  10659. C->setAssignmentOps(Exprs);
  10660. }
  10661. void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
  10662. C->setLParenLoc(Record.readSourceLocation());
  10663. unsigned NumVars = C->varlist_size();
  10664. SmallVector<Expr *, 16> Exprs;
  10665. Exprs.reserve(NumVars);
  10666. for (unsigned i = 0; i != NumVars; ++i)
  10667. Exprs.push_back(Record.readSubExpr());
  10668. C->setVarRefs(Exprs);
  10669. Exprs.clear();
  10670. for (unsigned i = 0; i != NumVars; ++i)
  10671. Exprs.push_back(Record.readSubExpr());
  10672. C->setSourceExprs(Exprs);
  10673. Exprs.clear();
  10674. for (unsigned i = 0; i != NumVars; ++i)
  10675. Exprs.push_back(Record.readSubExpr());
  10676. C->setDestinationExprs(Exprs);
  10677. Exprs.clear();
  10678. for (unsigned i = 0; i != NumVars; ++i)
  10679. Exprs.push_back(Record.readSubExpr());
  10680. C->setAssignmentOps(Exprs);
  10681. }
  10682. void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
  10683. C->setLParenLoc(Record.readSourceLocation());
  10684. unsigned NumVars = C->varlist_size();
  10685. SmallVector<Expr *, 16> Vars;
  10686. Vars.reserve(NumVars);
  10687. for (unsigned i = 0; i != NumVars; ++i)
  10688. Vars.push_back(Record.readSubExpr());
  10689. C->setVarRefs(Vars);
  10690. }
  10691. void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
  10692. C->setLParenLoc(Record.readSourceLocation());
  10693. C->setDependencyKind(
  10694. static_cast<OpenMPDependClauseKind>(Record.readInt()));
  10695. C->setDependencyLoc(Record.readSourceLocation());
  10696. C->setColonLoc(Record.readSourceLocation());
  10697. unsigned NumVars = C->varlist_size();
  10698. SmallVector<Expr *, 16> Vars;
  10699. Vars.reserve(NumVars);
  10700. for (unsigned I = 0; I != NumVars; ++I)
  10701. Vars.push_back(Record.readSubExpr());
  10702. C->setVarRefs(Vars);
  10703. for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
  10704. C->setLoopData(I, Record.readSubExpr());
  10705. }
  10706. void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
  10707. VisitOMPClauseWithPreInit(C);
  10708. C->setDevice(Record.readSubExpr());
  10709. C->setLParenLoc(Record.readSourceLocation());
  10710. }
  10711. void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
  10712. C->setLParenLoc(Record.readSourceLocation());
  10713. C->setMapTypeModifier(
  10714. static_cast<OpenMPMapClauseKind>(Record.readInt()));
  10715. C->setMapType(
  10716. static_cast<OpenMPMapClauseKind>(Record.readInt()));
  10717. C->setMapLoc(Record.readSourceLocation());
  10718. C->setColonLoc(Record.readSourceLocation());
  10719. auto NumVars = C->varlist_size();
  10720. auto UniqueDecls = C->getUniqueDeclarationsNum();
  10721. auto TotalLists = C->getTotalComponentListNum();
  10722. auto TotalComponents = C->getTotalComponentsNum();
  10723. SmallVector<Expr *, 16> Vars;
  10724. Vars.reserve(NumVars);
  10725. for (unsigned i = 0; i != NumVars; ++i)
  10726. Vars.push_back(Record.readSubExpr());
  10727. C->setVarRefs(Vars);
  10728. SmallVector<ValueDecl *, 16> Decls;
  10729. Decls.reserve(UniqueDecls);
  10730. for (unsigned i = 0; i < UniqueDecls; ++i)
  10731. Decls.push_back(Record.readDeclAs<ValueDecl>());
  10732. C->setUniqueDecls(Decls);
  10733. SmallVector<unsigned, 16> ListsPerDecl;
  10734. ListsPerDecl.reserve(UniqueDecls);
  10735. for (unsigned i = 0; i < UniqueDecls; ++i)
  10736. ListsPerDecl.push_back(Record.readInt());
  10737. C->setDeclNumLists(ListsPerDecl);
  10738. SmallVector<unsigned, 32> ListSizes;
  10739. ListSizes.reserve(TotalLists);
  10740. for (unsigned i = 0; i < TotalLists; ++i)
  10741. ListSizes.push_back(Record.readInt());
  10742. C->setComponentListSizes(ListSizes);
  10743. SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
  10744. Components.reserve(TotalComponents);
  10745. for (unsigned i = 0; i < TotalComponents; ++i) {
  10746. Expr *AssociatedExpr = Record.readSubExpr();
  10747. auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
  10748. Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
  10749. AssociatedExpr, AssociatedDecl));
  10750. }
  10751. C->setComponents(Components, ListSizes);
  10752. }
  10753. void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
  10754. VisitOMPClauseWithPreInit(C);
  10755. C->setNumTeams(Record.readSubExpr());
  10756. C->setLParenLoc(Record.readSourceLocation());
  10757. }
  10758. void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
  10759. VisitOMPClauseWithPreInit(C);
  10760. C->setThreadLimit(Record.readSubExpr());
  10761. C->setLParenLoc(Record.readSourceLocation());
  10762. }
  10763. void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
  10764. C->setPriority(Record.readSubExpr());
  10765. C->setLParenLoc(Record.readSourceLocation());
  10766. }
  10767. void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
  10768. C->setGrainsize(Record.readSubExpr());
  10769. C->setLParenLoc(Record.readSourceLocation());
  10770. }
  10771. void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
  10772. C->setNumTasks(Record.readSubExpr());
  10773. C->setLParenLoc(Record.readSourceLocation());
  10774. }
  10775. void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
  10776. C->setHint(Record.readSubExpr());
  10777. C->setLParenLoc(Record.readSourceLocation());
  10778. }
  10779. void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
  10780. VisitOMPClauseWithPreInit(C);
  10781. C->setDistScheduleKind(
  10782. static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
  10783. C->setChunkSize(Record.readSubExpr());
  10784. C->setLParenLoc(Record.readSourceLocation());
  10785. C->setDistScheduleKindLoc(Record.readSourceLocation());
  10786. C->setCommaLoc(Record.readSourceLocation());
  10787. }
  10788. void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
  10789. C->setDefaultmapKind(
  10790. static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
  10791. C->setDefaultmapModifier(
  10792. static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
  10793. C->setLParenLoc(Record.readSourceLocation());
  10794. C->setDefaultmapModifierLoc(Record.readSourceLocation());
  10795. C->setDefaultmapKindLoc(Record.readSourceLocation());
  10796. }
  10797. void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
  10798. C->setLParenLoc(Record.readSourceLocation());
  10799. auto NumVars = C->varlist_size();
  10800. auto UniqueDecls = C->getUniqueDeclarationsNum();
  10801. auto TotalLists = C->getTotalComponentListNum();
  10802. auto TotalComponents = C->getTotalComponentsNum();
  10803. SmallVector<Expr *, 16> Vars;
  10804. Vars.reserve(NumVars);
  10805. for (unsigned i = 0; i != NumVars; ++i)
  10806. Vars.push_back(Record.readSubExpr());
  10807. C->setVarRefs(Vars);
  10808. SmallVector<ValueDecl *, 16> Decls;
  10809. Decls.reserve(UniqueDecls);
  10810. for (unsigned i = 0; i < UniqueDecls; ++i)
  10811. Decls.push_back(Record.readDeclAs<ValueDecl>());
  10812. C->setUniqueDecls(Decls);
  10813. SmallVector<unsigned, 16> ListsPerDecl;
  10814. ListsPerDecl.reserve(UniqueDecls);
  10815. for (unsigned i = 0; i < UniqueDecls; ++i)
  10816. ListsPerDecl.push_back(Record.readInt());
  10817. C->setDeclNumLists(ListsPerDecl);
  10818. SmallVector<unsigned, 32> ListSizes;
  10819. ListSizes.reserve(TotalLists);
  10820. for (unsigned i = 0; i < TotalLists; ++i)
  10821. ListSizes.push_back(Record.readInt());
  10822. C->setComponentListSizes(ListSizes);
  10823. SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
  10824. Components.reserve(TotalComponents);
  10825. for (unsigned i = 0; i < TotalComponents; ++i) {
  10826. Expr *AssociatedExpr = Record.readSubExpr();
  10827. auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
  10828. Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
  10829. AssociatedExpr, AssociatedDecl));
  10830. }
  10831. C->setComponents(Components, ListSizes);
  10832. }
  10833. void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
  10834. C->setLParenLoc(Record.readSourceLocation());
  10835. auto NumVars = C->varlist_size();
  10836. auto UniqueDecls = C->getUniqueDeclarationsNum();
  10837. auto TotalLists = C->getTotalComponentListNum();
  10838. auto TotalComponents = C->getTotalComponentsNum();
  10839. SmallVector<Expr *, 16> Vars;
  10840. Vars.reserve(NumVars);
  10841. for (unsigned i = 0; i != NumVars; ++i)
  10842. Vars.push_back(Record.readSubExpr());
  10843. C->setVarRefs(Vars);
  10844. SmallVector<ValueDecl *, 16> Decls;
  10845. Decls.reserve(UniqueDecls);
  10846. for (unsigned i = 0; i < UniqueDecls; ++i)
  10847. Decls.push_back(Record.readDeclAs<ValueDecl>());
  10848. C->setUniqueDecls(Decls);
  10849. SmallVector<unsigned, 16> ListsPerDecl;
  10850. ListsPerDecl.reserve(UniqueDecls);
  10851. for (unsigned i = 0; i < UniqueDecls; ++i)
  10852. ListsPerDecl.push_back(Record.readInt());
  10853. C->setDeclNumLists(ListsPerDecl);
  10854. SmallVector<unsigned, 32> ListSizes;
  10855. ListSizes.reserve(TotalLists);
  10856. for (unsigned i = 0; i < TotalLists; ++i)
  10857. ListSizes.push_back(Record.readInt());
  10858. C->setComponentListSizes(ListSizes);
  10859. SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
  10860. Components.reserve(TotalComponents);
  10861. for (unsigned i = 0; i < TotalComponents; ++i) {
  10862. Expr *AssociatedExpr = Record.readSubExpr();
  10863. auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
  10864. Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
  10865. AssociatedExpr, AssociatedDecl));
  10866. }
  10867. C->setComponents(Components, ListSizes);
  10868. }
  10869. void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
  10870. C->setLParenLoc(Record.readSourceLocation());
  10871. auto NumVars = C->varlist_size();
  10872. auto UniqueDecls = C->getUniqueDeclarationsNum();
  10873. auto TotalLists = C->getTotalComponentListNum();
  10874. auto TotalComponents = C->getTotalComponentsNum();
  10875. SmallVector<Expr *, 16> Vars;
  10876. Vars.reserve(NumVars);
  10877. for (unsigned i = 0; i != NumVars; ++i)
  10878. Vars.push_back(Record.readSubExpr());
  10879. C->setVarRefs(Vars);
  10880. Vars.clear();
  10881. for (unsigned i = 0; i != NumVars; ++i)
  10882. Vars.push_back(Record.readSubExpr());
  10883. C->setPrivateCopies(Vars);
  10884. Vars.clear();
  10885. for (unsigned i = 0; i != NumVars; ++i)
  10886. Vars.push_back(Record.readSubExpr());
  10887. C->setInits(Vars);
  10888. SmallVector<ValueDecl *, 16> Decls;
  10889. Decls.reserve(UniqueDecls);
  10890. for (unsigned i = 0; i < UniqueDecls; ++i)
  10891. Decls.push_back(Record.readDeclAs<ValueDecl>());
  10892. C->setUniqueDecls(Decls);
  10893. SmallVector<unsigned, 16> ListsPerDecl;
  10894. ListsPerDecl.reserve(UniqueDecls);
  10895. for (unsigned i = 0; i < UniqueDecls; ++i)
  10896. ListsPerDecl.push_back(Record.readInt());
  10897. C->setDeclNumLists(ListsPerDecl);
  10898. SmallVector<unsigned, 32> ListSizes;
  10899. ListSizes.reserve(TotalLists);
  10900. for (unsigned i = 0; i < TotalLists; ++i)
  10901. ListSizes.push_back(Record.readInt());
  10902. C->setComponentListSizes(ListSizes);
  10903. SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
  10904. Components.reserve(TotalComponents);
  10905. for (unsigned i = 0; i < TotalComponents; ++i) {
  10906. Expr *AssociatedExpr = Record.readSubExpr();
  10907. auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
  10908. Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
  10909. AssociatedExpr, AssociatedDecl));
  10910. }
  10911. C->setComponents(Components, ListSizes);
  10912. }
  10913. void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
  10914. C->setLParenLoc(Record.readSourceLocation());
  10915. auto NumVars = C->varlist_size();
  10916. auto UniqueDecls = C->getUniqueDeclarationsNum();
  10917. auto TotalLists = C->getTotalComponentListNum();
  10918. auto TotalComponents = C->getTotalComponentsNum();
  10919. SmallVector<Expr *, 16> Vars;
  10920. Vars.reserve(NumVars);
  10921. for (unsigned i = 0; i != NumVars; ++i)
  10922. Vars.push_back(Record.readSubExpr());
  10923. C->setVarRefs(Vars);
  10924. Vars.clear();
  10925. SmallVector<ValueDecl *, 16> Decls;
  10926. Decls.reserve(UniqueDecls);
  10927. for (unsigned i = 0; i < UniqueDecls; ++i)
  10928. Decls.push_back(Record.readDeclAs<ValueDecl>());
  10929. C->setUniqueDecls(Decls);
  10930. SmallVector<unsigned, 16> ListsPerDecl;
  10931. ListsPerDecl.reserve(UniqueDecls);
  10932. for (unsigned i = 0; i < UniqueDecls; ++i)
  10933. ListsPerDecl.push_back(Record.readInt());
  10934. C->setDeclNumLists(ListsPerDecl);
  10935. SmallVector<unsigned, 32> ListSizes;
  10936. ListSizes.reserve(TotalLists);
  10937. for (unsigned i = 0; i < TotalLists; ++i)
  10938. ListSizes.push_back(Record.readInt());
  10939. C->setComponentListSizes(ListSizes);
  10940. SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
  10941. Components.reserve(TotalComponents);
  10942. for (unsigned i = 0; i < TotalComponents; ++i) {
  10943. Expr *AssociatedExpr = Record.readSubExpr();
  10944. auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
  10945. Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
  10946. AssociatedExpr, AssociatedDecl));
  10947. }
  10948. C->setComponents(Components, ListSizes);
  10949. }