ASTImporterTest.cpp 182 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330
  1. //===- unittest/AST/ASTImporterTest.cpp - AST node import test ------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Tests for the correct import of AST nodes from one AST context to another.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ADT/StringMap.h"
  13. #include "clang/AST/DeclContextInternals.h"
  14. #include "ASTImporterFixtures.h"
  15. #include "MatchVerifier.h"
  16. namespace clang {
  17. namespace ast_matchers {
  18. using internal::Matcher;
  19. using internal::BindableMatcher;
  20. using llvm::StringMap;
  21. // Base class for those tests which use the family of `testImport` functions.
  22. class TestImportBase : public CompilerOptionSpecificTest,
  23. public ::testing::WithParamInterface<ArgVector> {
  24. template <typename NodeType>
  25. llvm::Expected<NodeType> importNode(ASTUnit *From, ASTUnit *To,
  26. ASTImporter &Importer, NodeType Node) {
  27. ASTContext &ToCtx = To->getASTContext();
  28. // Add 'From' file to virtual file system so importer can 'find' it
  29. // while importing SourceLocations. It is safe to add same file multiple
  30. // times - it just isn't replaced.
  31. StringRef FromFileName = From->getMainFileName();
  32. createVirtualFileIfNeeded(To, FromFileName,
  33. From->getBufferForFile(FromFileName));
  34. auto Imported = Importer.Import(Node);
  35. if (Imported) {
  36. // This should dump source locations and assert if some source locations
  37. // were not imported.
  38. SmallString<1024> ImportChecker;
  39. llvm::raw_svector_ostream ToNothing(ImportChecker);
  40. ToCtx.getTranslationUnitDecl()->print(ToNothing);
  41. // This traverses the AST to catch certain bugs like poorly or not
  42. // implemented subtrees.
  43. (*Imported)->dump(ToNothing);
  44. }
  45. return Imported;
  46. }
  47. template <typename NodeType>
  48. testing::AssertionResult
  49. testImport(const std::string &FromCode, const ArgVector &FromArgs,
  50. const std::string &ToCode, const ArgVector &ToArgs,
  51. MatchVerifier<NodeType> &Verifier,
  52. const BindableMatcher<NodeType> &SearchMatcher,
  53. const BindableMatcher<NodeType> &VerificationMatcher) {
  54. const char *const InputFileName = "input.cc";
  55. const char *const OutputFileName = "output.cc";
  56. std::unique_ptr<ASTUnit> FromAST = tooling::buildASTFromCodeWithArgs(
  57. FromCode, FromArgs, InputFileName),
  58. ToAST = tooling::buildASTFromCodeWithArgs(
  59. ToCode, ToArgs, OutputFileName);
  60. ASTContext &FromCtx = FromAST->getASTContext(),
  61. &ToCtx = ToAST->getASTContext();
  62. ASTImporter Importer(ToCtx, ToAST->getFileManager(), FromCtx,
  63. FromAST->getFileManager(), false);
  64. auto FoundNodes = match(SearchMatcher, FromCtx);
  65. if (FoundNodes.size() != 1)
  66. return testing::AssertionFailure()
  67. << "Multiple potential nodes were found!";
  68. auto ToImport = selectFirst<NodeType>(DeclToImportID, FoundNodes);
  69. if (!ToImport)
  70. return testing::AssertionFailure() << "Node type mismatch!";
  71. // Sanity check: the node being imported should match in the same way as
  72. // the result node.
  73. BindableMatcher<NodeType> WrapperMatcher(VerificationMatcher);
  74. EXPECT_TRUE(Verifier.match(ToImport, WrapperMatcher));
  75. auto Imported = importNode(FromAST.get(), ToAST.get(), Importer, ToImport);
  76. if (!Imported) {
  77. std::string ErrorText;
  78. handleAllErrors(
  79. Imported.takeError(),
  80. [&ErrorText](const ImportError &Err) { ErrorText = Err.message(); });
  81. return testing::AssertionFailure()
  82. << "Import failed, error: \"" << ErrorText << "\"!";
  83. }
  84. return Verifier.match(*Imported, WrapperMatcher);
  85. }
  86. template <typename NodeType>
  87. testing::AssertionResult
  88. testImport(const std::string &FromCode, const ArgVector &FromArgs,
  89. const std::string &ToCode, const ArgVector &ToArgs,
  90. MatchVerifier<NodeType> &Verifier,
  91. const BindableMatcher<NodeType> &VerificationMatcher) {
  92. return testImport(
  93. FromCode, FromArgs, ToCode, ToArgs, Verifier,
  94. translationUnitDecl(
  95. has(namedDecl(hasName(DeclToImportID)).bind(DeclToImportID))),
  96. VerificationMatcher);
  97. }
  98. protected:
  99. ArgVector getExtraArgs() const override { return GetParam(); }
  100. public:
  101. /// Test how AST node named "declToImport" located in the translation unit
  102. /// of "FromCode" virtual file is imported to "ToCode" virtual file.
  103. /// The verification is done by running AMatcher over the imported node.
  104. template <typename NodeType, typename MatcherType>
  105. void testImport(const std::string &FromCode, Language FromLang,
  106. const std::string &ToCode, Language ToLang,
  107. MatchVerifier<NodeType> &Verifier,
  108. const MatcherType &AMatcher) {
  109. ArgVector FromArgs = getArgVectorForLanguage(FromLang),
  110. ToArgs = getArgVectorForLanguage(ToLang);
  111. EXPECT_TRUE(
  112. testImport(FromCode, FromArgs, ToCode, ToArgs, Verifier, AMatcher));
  113. }
  114. struct ImportAction {
  115. StringRef FromFilename;
  116. StringRef ToFilename;
  117. // FIXME: Generalize this to support other node kinds.
  118. BindableMatcher<Decl> ImportPredicate;
  119. ImportAction(StringRef FromFilename, StringRef ToFilename,
  120. DeclarationMatcher ImportPredicate)
  121. : FromFilename(FromFilename), ToFilename(ToFilename),
  122. ImportPredicate(ImportPredicate) {}
  123. ImportAction(StringRef FromFilename, StringRef ToFilename,
  124. const std::string &DeclName)
  125. : FromFilename(FromFilename), ToFilename(ToFilename),
  126. ImportPredicate(namedDecl(hasName(DeclName))) {}
  127. };
  128. using SingleASTUnit = std::unique_ptr<ASTUnit>;
  129. using AllASTUnits = StringMap<SingleASTUnit>;
  130. struct CodeEntry {
  131. std::string CodeSample;
  132. Language Lang;
  133. };
  134. using CodeFiles = StringMap<CodeEntry>;
  135. /// Builds an ASTUnit for one potential compile options set.
  136. SingleASTUnit createASTUnit(StringRef FileName, const CodeEntry &CE) const {
  137. ArgVector Args = getArgVectorForLanguage(CE.Lang);
  138. auto AST = tooling::buildASTFromCodeWithArgs(CE.CodeSample, Args, FileName);
  139. EXPECT_TRUE(AST.get());
  140. return AST;
  141. }
  142. /// Test an arbitrary sequence of imports for a set of given in-memory files.
  143. /// The verification is done by running VerificationMatcher against a
  144. /// specified AST node inside of one of given files.
  145. /// \param CodeSamples Map whose key is the file name and the value is the
  146. /// file content.
  147. /// \param ImportActions Sequence of imports. Each import in sequence
  148. /// specifies "from file" and "to file" and a matcher that is used for
  149. /// searching a declaration for import in "from file".
  150. /// \param FileForFinalCheck Name of virtual file for which the final check is
  151. /// applied.
  152. /// \param FinalSelectPredicate Matcher that specifies the AST node in the
  153. /// FileForFinalCheck for which the verification will be done.
  154. /// \param VerificationMatcher Matcher that will be used for verification
  155. /// after all imports in sequence are done.
  156. void testImportSequence(const CodeFiles &CodeSamples,
  157. const std::vector<ImportAction> &ImportActions,
  158. StringRef FileForFinalCheck,
  159. BindableMatcher<Decl> FinalSelectPredicate,
  160. BindableMatcher<Decl> VerificationMatcher) {
  161. AllASTUnits AllASTs;
  162. using ImporterKey = std::pair<const ASTUnit *, const ASTUnit *>;
  163. llvm::DenseMap<ImporterKey, std::unique_ptr<ASTImporter>> Importers;
  164. auto GenASTsIfNeeded = [this, &AllASTs, &CodeSamples](StringRef Filename) {
  165. if (!AllASTs.count(Filename)) {
  166. auto Found = CodeSamples.find(Filename);
  167. assert(Found != CodeSamples.end() && "Wrong file for import!");
  168. AllASTs[Filename] = createASTUnit(Filename, Found->getValue());
  169. }
  170. };
  171. for (const ImportAction &Action : ImportActions) {
  172. StringRef FromFile = Action.FromFilename, ToFile = Action.ToFilename;
  173. GenASTsIfNeeded(FromFile);
  174. GenASTsIfNeeded(ToFile);
  175. ASTUnit *From = AllASTs[FromFile].get();
  176. ASTUnit *To = AllASTs[ToFile].get();
  177. // Create a new importer if needed.
  178. std::unique_ptr<ASTImporter> &ImporterRef = Importers[{From, To}];
  179. if (!ImporterRef)
  180. ImporterRef.reset(new ASTImporter(
  181. To->getASTContext(), To->getFileManager(), From->getASTContext(),
  182. From->getFileManager(), false));
  183. // Find the declaration and import it.
  184. auto FoundDecl = match(Action.ImportPredicate.bind(DeclToImportID),
  185. From->getASTContext());
  186. EXPECT_TRUE(FoundDecl.size() == 1);
  187. const Decl *ToImport = selectFirst<Decl>(DeclToImportID, FoundDecl);
  188. auto Imported = importNode(From, To, *ImporterRef, ToImport);
  189. EXPECT_TRUE(static_cast<bool>(Imported));
  190. if (!Imported)
  191. llvm::consumeError(Imported.takeError());
  192. }
  193. // Find the declaration and import it.
  194. auto FoundDecl = match(FinalSelectPredicate.bind(DeclToVerifyID),
  195. AllASTs[FileForFinalCheck]->getASTContext());
  196. EXPECT_TRUE(FoundDecl.size() == 1);
  197. const Decl *ToVerify = selectFirst<Decl>(DeclToVerifyID, FoundDecl);
  198. MatchVerifier<Decl> Verifier;
  199. EXPECT_TRUE(
  200. Verifier.match(ToVerify, BindableMatcher<Decl>(VerificationMatcher)));
  201. }
  202. };
  203. template <typename T> RecordDecl *getRecordDecl(T *D) {
  204. auto *ET = cast<ElaboratedType>(D->getType().getTypePtr());
  205. return cast<RecordType>(ET->getNamedType().getTypePtr())->getDecl();
  206. }
  207. struct ImportExpr : TestImportBase {};
  208. struct ImportType : TestImportBase {};
  209. struct ImportDecl : TestImportBase {};
  210. struct CanonicalRedeclChain : ASTImporterOptionSpecificTestBase {};
  211. TEST_P(CanonicalRedeclChain, ShouldBeConsequentWithMatchers) {
  212. Decl *FromTU = getTuDecl("void f();", Lang_CXX);
  213. auto Pattern = functionDecl(hasName("f"));
  214. auto *D0 = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  215. auto Redecls = getCanonicalForwardRedeclChain(D0);
  216. ASSERT_EQ(Redecls.size(), 1u);
  217. EXPECT_EQ(D0, Redecls[0]);
  218. }
  219. TEST_P(CanonicalRedeclChain, ShouldBeConsequentWithMatchers2) {
  220. Decl *FromTU = getTuDecl("void f(); void f(); void f();", Lang_CXX);
  221. auto Pattern = functionDecl(hasName("f"));
  222. auto *D0 = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  223. auto *D2 = LastDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  224. FunctionDecl *D1 = D2->getPreviousDecl();
  225. auto Redecls = getCanonicalForwardRedeclChain(D0);
  226. ASSERT_EQ(Redecls.size(), 3u);
  227. EXPECT_EQ(D0, Redecls[0]);
  228. EXPECT_EQ(D1, Redecls[1]);
  229. EXPECT_EQ(D2, Redecls[2]);
  230. }
  231. TEST_P(CanonicalRedeclChain, ShouldBeSameForAllDeclInTheChain) {
  232. Decl *FromTU = getTuDecl("void f(); void f(); void f();", Lang_CXX);
  233. auto Pattern = functionDecl(hasName("f"));
  234. auto *D0 = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  235. auto *D2 = LastDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  236. FunctionDecl *D1 = D2->getPreviousDecl();
  237. auto RedeclsD0 = getCanonicalForwardRedeclChain(D0);
  238. auto RedeclsD1 = getCanonicalForwardRedeclChain(D1);
  239. auto RedeclsD2 = getCanonicalForwardRedeclChain(D2);
  240. EXPECT_THAT(RedeclsD0, ::testing::ContainerEq(RedeclsD1));
  241. EXPECT_THAT(RedeclsD1, ::testing::ContainerEq(RedeclsD2));
  242. }
  243. namespace {
  244. struct RedirectingImporter : public ASTImporter {
  245. using ASTImporter::ASTImporter;
  246. protected:
  247. llvm::Expected<Decl *> ImportImpl(Decl *FromD) override {
  248. auto *ND = dyn_cast<NamedDecl>(FromD);
  249. if (!ND || ND->getName() != "shouldNotBeImported")
  250. return ASTImporter::ImportImpl(FromD);
  251. for (Decl *D : getToContext().getTranslationUnitDecl()->decls()) {
  252. if (auto *ND = dyn_cast<NamedDecl>(D))
  253. if (ND->getName() == "realDecl") {
  254. RegisterImportedDecl(FromD, ND);
  255. return ND;
  256. }
  257. }
  258. return ASTImporter::ImportImpl(FromD);
  259. }
  260. };
  261. } // namespace
  262. struct RedirectingImporterTest : ASTImporterOptionSpecificTestBase {
  263. RedirectingImporterTest() {
  264. Creator = [](ASTContext &ToContext, FileManager &ToFileManager,
  265. ASTContext &FromContext, FileManager &FromFileManager,
  266. bool MinimalImport,
  267. const std::shared_ptr<ASTImporterSharedState> &SharedState) {
  268. return new RedirectingImporter(ToContext, ToFileManager, FromContext,
  269. FromFileManager, MinimalImport,
  270. SharedState);
  271. };
  272. }
  273. };
  274. // Test that an ASTImporter subclass can intercept an import call.
  275. TEST_P(RedirectingImporterTest, InterceptImport) {
  276. Decl *From, *To;
  277. std::tie(From, To) =
  278. getImportedDecl("class shouldNotBeImported {};", Lang_CXX,
  279. "class realDecl {};", Lang_CXX, "shouldNotBeImported");
  280. auto *Imported = cast<CXXRecordDecl>(To);
  281. EXPECT_EQ(Imported->getQualifiedNameAsString(), "realDecl");
  282. // Make sure our importer prevented the importing of the decl.
  283. auto *ToTU = Imported->getTranslationUnitDecl();
  284. auto Pattern = functionDecl(hasName("shouldNotBeImported"));
  285. unsigned count =
  286. DeclCounterWithPredicate<CXXRecordDecl>().match(ToTU, Pattern);
  287. EXPECT_EQ(0U, count);
  288. }
  289. // Test that when we indirectly import a declaration the custom ASTImporter
  290. // is still intercepting the import.
  291. TEST_P(RedirectingImporterTest, InterceptIndirectImport) {
  292. Decl *From, *To;
  293. std::tie(From, To) =
  294. getImportedDecl("class shouldNotBeImported {};"
  295. "class F { shouldNotBeImported f; };",
  296. Lang_CXX, "class realDecl {};", Lang_CXX, "F");
  297. // Make sure our ASTImporter prevented the importing of the decl.
  298. auto *ToTU = To->getTranslationUnitDecl();
  299. auto Pattern = functionDecl(hasName("shouldNotBeImported"));
  300. unsigned count =
  301. DeclCounterWithPredicate<CXXRecordDecl>().match(ToTU, Pattern);
  302. EXPECT_EQ(0U, count);
  303. }
  304. struct ImportPath : ASTImporterOptionSpecificTestBase {
  305. Decl *FromTU;
  306. FunctionDecl *D0, *D1, *D2;
  307. ImportPath() {
  308. FromTU = getTuDecl("void f(); void f(); void f();", Lang_CXX);
  309. auto Pattern = functionDecl(hasName("f"));
  310. D0 = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  311. D2 = LastDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  312. D1 = D2->getPreviousDecl();
  313. }
  314. };
  315. TEST_P(ImportPath, Push) {
  316. ASTImporter::ImportPathTy path;
  317. path.push(D0);
  318. EXPECT_FALSE(path.hasCycleAtBack());
  319. }
  320. TEST_P(ImportPath, SmallCycle) {
  321. ASTImporter::ImportPathTy path;
  322. path.push(D0);
  323. path.push(D0);
  324. EXPECT_TRUE(path.hasCycleAtBack());
  325. path.pop();
  326. EXPECT_FALSE(path.hasCycleAtBack());
  327. path.push(D0);
  328. EXPECT_TRUE(path.hasCycleAtBack());
  329. }
  330. TEST_P(ImportPath, GetSmallCycle) {
  331. ASTImporter::ImportPathTy path;
  332. path.push(D0);
  333. path.push(D0);
  334. EXPECT_TRUE(path.hasCycleAtBack());
  335. std::array<Decl* ,2> Res;
  336. int i = 0;
  337. for (Decl *Di : path.getCycleAtBack()) {
  338. Res[i++] = Di;
  339. }
  340. ASSERT_EQ(i, 2);
  341. EXPECT_EQ(Res[0], D0);
  342. EXPECT_EQ(Res[1], D0);
  343. }
  344. TEST_P(ImportPath, GetCycle) {
  345. ASTImporter::ImportPathTy path;
  346. path.push(D0);
  347. path.push(D1);
  348. path.push(D2);
  349. path.push(D0);
  350. EXPECT_TRUE(path.hasCycleAtBack());
  351. std::array<Decl* ,4> Res;
  352. int i = 0;
  353. for (Decl *Di : path.getCycleAtBack()) {
  354. Res[i++] = Di;
  355. }
  356. ASSERT_EQ(i, 4);
  357. EXPECT_EQ(Res[0], D0);
  358. EXPECT_EQ(Res[1], D2);
  359. EXPECT_EQ(Res[2], D1);
  360. EXPECT_EQ(Res[3], D0);
  361. }
  362. TEST_P(ImportPath, CycleAfterCycle) {
  363. ASTImporter::ImportPathTy path;
  364. path.push(D0);
  365. path.push(D1);
  366. path.push(D0);
  367. path.push(D1);
  368. path.push(D2);
  369. path.push(D0);
  370. EXPECT_TRUE(path.hasCycleAtBack());
  371. std::array<Decl* ,4> Res;
  372. int i = 0;
  373. for (Decl *Di : path.getCycleAtBack()) {
  374. Res[i++] = Di;
  375. }
  376. ASSERT_EQ(i, 4);
  377. EXPECT_EQ(Res[0], D0);
  378. EXPECT_EQ(Res[1], D2);
  379. EXPECT_EQ(Res[2], D1);
  380. EXPECT_EQ(Res[3], D0);
  381. path.pop();
  382. path.pop();
  383. path.pop();
  384. EXPECT_TRUE(path.hasCycleAtBack());
  385. i = 0;
  386. for (Decl *Di : path.getCycleAtBack()) {
  387. Res[i++] = Di;
  388. }
  389. ASSERT_EQ(i, 3);
  390. EXPECT_EQ(Res[0], D0);
  391. EXPECT_EQ(Res[1], D1);
  392. EXPECT_EQ(Res[2], D0);
  393. path.pop();
  394. EXPECT_FALSE(path.hasCycleAtBack());
  395. }
  396. TEST_P(ImportExpr, ImportStringLiteral) {
  397. MatchVerifier<Decl> Verifier;
  398. testImport(
  399. "void declToImport() { (void)\"foo\"; }",
  400. Lang_CXX, "", Lang_CXX, Verifier,
  401. functionDecl(hasDescendant(
  402. stringLiteral(hasType(asString("const char [4]"))))));
  403. testImport(
  404. "void declToImport() { (void)L\"foo\"; }",
  405. Lang_CXX, "", Lang_CXX, Verifier,
  406. functionDecl(hasDescendant(
  407. stringLiteral(hasType(asString("const wchar_t [4]"))))));
  408. testImport(
  409. "void declToImport() { (void) \"foo\" \"bar\"; }",
  410. Lang_CXX, "", Lang_CXX, Verifier,
  411. functionDecl(hasDescendant(
  412. stringLiteral(hasType(asString("const char [7]"))))));
  413. }
  414. TEST_P(ImportExpr, ImportChooseExpr) {
  415. MatchVerifier<Decl> Verifier;
  416. // This case tests C code that is not condition-dependent and has a true
  417. // condition.
  418. testImport(
  419. "void declToImport() { (void)__builtin_choose_expr(1, 2, 3); }",
  420. Lang_C, "", Lang_C, Verifier,
  421. functionDecl(hasDescendant(chooseExpr())));
  422. }
  423. TEST_P(ImportExpr, ImportGNUNullExpr) {
  424. MatchVerifier<Decl> Verifier;
  425. testImport(
  426. "void declToImport() { (void)__null; }",
  427. Lang_CXX, "", Lang_CXX, Verifier,
  428. functionDecl(hasDescendant(gnuNullExpr(hasType(isInteger())))));
  429. }
  430. TEST_P(ImportExpr, ImportCXXNullPtrLiteralExpr) {
  431. MatchVerifier<Decl> Verifier;
  432. testImport(
  433. "void declToImport() { (void)nullptr; }",
  434. Lang_CXX11, "", Lang_CXX11, Verifier,
  435. functionDecl(hasDescendant(cxxNullPtrLiteralExpr())));
  436. }
  437. TEST_P(ImportExpr, ImportFloatinglLiteralExpr) {
  438. MatchVerifier<Decl> Verifier;
  439. testImport(
  440. "void declToImport() { (void)1.0; }",
  441. Lang_C, "", Lang_C, Verifier,
  442. functionDecl(hasDescendant(
  443. floatLiteral(equals(1.0), hasType(asString("double"))))));
  444. testImport(
  445. "void declToImport() { (void)1.0e-5f; }",
  446. Lang_C, "", Lang_C, Verifier,
  447. functionDecl(hasDescendant(
  448. floatLiteral(equals(1.0e-5f), hasType(asString("float"))))));
  449. }
  450. TEST_P(ImportExpr, ImportImaginaryLiteralExpr) {
  451. MatchVerifier<Decl> Verifier;
  452. testImport(
  453. "void declToImport() { (void)1.0i; }",
  454. Lang_CXX14, "", Lang_CXX14, Verifier,
  455. functionDecl(hasDescendant(imaginaryLiteral())));
  456. }
  457. TEST_P(ImportExpr, ImportCompoundLiteralExpr) {
  458. MatchVerifier<Decl> Verifier;
  459. testImport(
  460. "void declToImport() {"
  461. " struct s { int x; long y; unsigned z; }; "
  462. " (void)(struct s){ 42, 0L, 1U }; }",
  463. Lang_CXX, "", Lang_CXX, Verifier,
  464. functionDecl(hasDescendant(
  465. compoundLiteralExpr(
  466. hasType(asString("struct s")),
  467. has(initListExpr(
  468. hasType(asString("struct s")),
  469. has(integerLiteral(
  470. equals(42), hasType(asString("int")))),
  471. has(integerLiteral(
  472. equals(0), hasType(asString("long")))),
  473. has(integerLiteral(
  474. equals(1), hasType(asString("unsigned int"))))))))));
  475. }
  476. TEST_P(ImportExpr, ImportCXXThisExpr) {
  477. MatchVerifier<Decl> Verifier;
  478. testImport(
  479. "class declToImport { void f() { (void)this; } };",
  480. Lang_CXX, "", Lang_CXX, Verifier,
  481. cxxRecordDecl(
  482. hasMethod(
  483. hasDescendant(
  484. cxxThisExpr(
  485. hasType(
  486. asString("class declToImport *")))))));
  487. }
  488. TEST_P(ImportExpr, ImportAtomicExpr) {
  489. MatchVerifier<Decl> Verifier;
  490. testImport(
  491. "void declToImport() { int *ptr; __atomic_load_n(ptr, 1); }",
  492. Lang_C, "", Lang_C, Verifier,
  493. functionDecl(hasDescendant(
  494. atomicExpr(
  495. has(ignoringParenImpCasts(
  496. declRefExpr(hasDeclaration(varDecl(hasName("ptr"))),
  497. hasType(asString("int *"))))),
  498. has(integerLiteral(equals(1), hasType(asString("int"))))))));
  499. }
  500. TEST_P(ImportExpr, ImportLabelDeclAndAddrLabelExpr) {
  501. MatchVerifier<Decl> Verifier;
  502. testImport(
  503. "void declToImport() { loop: goto loop; (void)&&loop; }",
  504. Lang_C, "", Lang_C, Verifier,
  505. functionDecl(
  506. hasDescendant(
  507. labelStmt(hasDeclaration(labelDecl(hasName("loop"))))),
  508. hasDescendant(
  509. addrLabelExpr(hasDeclaration(labelDecl(hasName("loop")))))));
  510. }
  511. AST_MATCHER_P(TemplateDecl, hasTemplateDecl,
  512. internal::Matcher<NamedDecl>, InnerMatcher) {
  513. const NamedDecl *Template = Node.getTemplatedDecl();
  514. return Template && InnerMatcher.matches(*Template, Finder, Builder);
  515. }
  516. TEST_P(ImportExpr, ImportParenListExpr) {
  517. MatchVerifier<Decl> Verifier;
  518. testImport(
  519. "template<typename T> class dummy { void f() { dummy X(*this); } };"
  520. "typedef dummy<int> declToImport;"
  521. "template class dummy<int>;",
  522. Lang_CXX, "", Lang_CXX, Verifier,
  523. typedefDecl(hasType(templateSpecializationType(
  524. hasDeclaration(classTemplateSpecializationDecl(hasSpecializedTemplate(
  525. classTemplateDecl(hasTemplateDecl(cxxRecordDecl(hasMethod(allOf(
  526. hasName("f"),
  527. hasBody(compoundStmt(has(declStmt(hasSingleDecl(
  528. varDecl(hasInitializer(parenListExpr(has(unaryOperator(
  529. hasOperatorName("*"),
  530. hasUnaryOperand(cxxThisExpr())))))))))))))))))))))));
  531. }
  532. TEST_P(ImportExpr, ImportSwitch) {
  533. MatchVerifier<Decl> Verifier;
  534. testImport(
  535. "void declToImport() { int b; switch (b) { case 1: break; } }",
  536. Lang_C, "", Lang_C, Verifier,
  537. functionDecl(hasDescendant(
  538. switchStmt(has(compoundStmt(has(caseStmt())))))));
  539. }
  540. TEST_P(ImportExpr, ImportStmtExpr) {
  541. MatchVerifier<Decl> Verifier;
  542. testImport(
  543. "void declToImport() { int b; int a = b ?: 1; int C = ({int X=4; X;}); }",
  544. Lang_C, "", Lang_C, Verifier,
  545. functionDecl(hasDescendant(
  546. varDecl(
  547. hasName("C"),
  548. hasType(asString("int")),
  549. hasInitializer(
  550. stmtExpr(
  551. hasAnySubstatement(declStmt(hasSingleDecl(
  552. varDecl(
  553. hasName("X"),
  554. hasType(asString("int")),
  555. hasInitializer(
  556. integerLiteral(equals(4))))))),
  557. hasDescendant(
  558. implicitCastExpr())))))));
  559. }
  560. TEST_P(ImportExpr, ImportConditionalOperator) {
  561. MatchVerifier<Decl> Verifier;
  562. testImport(
  563. "void declToImport() { (void)(true ? 1 : -5); }",
  564. Lang_CXX, "", Lang_CXX, Verifier,
  565. functionDecl(hasDescendant(
  566. conditionalOperator(
  567. hasCondition(cxxBoolLiteral(equals(true))),
  568. hasTrueExpression(integerLiteral(equals(1))),
  569. hasFalseExpression(
  570. unaryOperator(hasUnaryOperand(integerLiteral(equals(5))))))
  571. )));
  572. }
  573. TEST_P(ImportExpr, ImportBinaryConditionalOperator) {
  574. MatchVerifier<Decl> Verifier;
  575. testImport(
  576. "void declToImport() { (void)(1 ?: -5); }",
  577. Lang_CXX, "", Lang_CXX, Verifier,
  578. functionDecl(hasDescendant(
  579. binaryConditionalOperator(
  580. hasCondition(
  581. implicitCastExpr(
  582. hasSourceExpression(opaqueValueExpr(
  583. hasSourceExpression(integerLiteral(equals(1))))),
  584. hasType(booleanType()))),
  585. hasTrueExpression(
  586. opaqueValueExpr(
  587. hasSourceExpression(integerLiteral(equals(1))))),
  588. hasFalseExpression(
  589. unaryOperator(
  590. hasOperatorName("-"),
  591. hasUnaryOperand(integerLiteral(equals(5)))))))));
  592. }
  593. TEST_P(ImportExpr, ImportDesignatedInitExpr) {
  594. MatchVerifier<Decl> Verifier;
  595. testImport(
  596. "void declToImport() {"
  597. " struct point { double x; double y; };"
  598. " struct point ptarray[10] = "
  599. "{ [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; }",
  600. Lang_C, "", Lang_C, Verifier,
  601. functionDecl(hasDescendant(
  602. initListExpr(
  603. has(designatedInitExpr(
  604. designatorCountIs(2),
  605. hasDescendant(floatLiteral(equals(1.0))),
  606. hasDescendant(integerLiteral(equals(2))))),
  607. has(designatedInitExpr(
  608. designatorCountIs(2),
  609. hasDescendant(floatLiteral(equals(2.0))),
  610. hasDescendant(integerLiteral(equals(2))))),
  611. has(designatedInitExpr(
  612. designatorCountIs(2),
  613. hasDescendant(floatLiteral(equals(1.0))),
  614. hasDescendant(integerLiteral(equals(0)))))))));
  615. }
  616. TEST_P(ImportExpr, ImportPredefinedExpr) {
  617. MatchVerifier<Decl> Verifier;
  618. // __func__ expands as StringLiteral("declToImport")
  619. testImport(
  620. "void declToImport() { (void)__func__; }",
  621. Lang_CXX, "", Lang_CXX, Verifier,
  622. functionDecl(hasDescendant(
  623. predefinedExpr(
  624. hasType(
  625. asString("const char [13]")),
  626. has(stringLiteral(hasType(
  627. asString("const char [13]"))))))));
  628. }
  629. TEST_P(ImportExpr, ImportInitListExpr) {
  630. MatchVerifier<Decl> Verifier;
  631. testImport(
  632. "void declToImport() {"
  633. " struct point { double x; double y; };"
  634. " point ptarray[10] = { [2].y = 1.0, [2].x = 2.0,"
  635. " [0].x = 1.0 }; }",
  636. Lang_CXX, "", Lang_CXX, Verifier,
  637. functionDecl(hasDescendant(
  638. initListExpr(
  639. has(
  640. cxxConstructExpr(
  641. requiresZeroInitialization())),
  642. has(
  643. initListExpr(
  644. hasType(asString("struct point")),
  645. has(floatLiteral(equals(1.0))),
  646. has(implicitValueInitExpr(
  647. hasType(asString("double")))))),
  648. has(
  649. initListExpr(
  650. hasType(asString("struct point")),
  651. has(floatLiteral(equals(2.0))),
  652. has(floatLiteral(equals(1.0)))))))));
  653. }
  654. const internal::VariadicDynCastAllOfMatcher<Expr, VAArgExpr> vaArgExpr;
  655. TEST_P(ImportExpr, ImportVAArgExpr) {
  656. MatchVerifier<Decl> Verifier;
  657. testImport(
  658. "void declToImport(__builtin_va_list list, ...) {"
  659. " (void)__builtin_va_arg(list, int); }",
  660. Lang_CXX, "", Lang_CXX, Verifier,
  661. functionDecl(hasDescendant(
  662. cStyleCastExpr(hasSourceExpression(vaArgExpr())))));
  663. }
  664. TEST_P(ImportExpr, CXXTemporaryObjectExpr) {
  665. MatchVerifier<Decl> Verifier;
  666. testImport(
  667. "struct C {};"
  668. "void declToImport() { C c = C(); }",
  669. Lang_CXX, "", Lang_CXX, Verifier,
  670. functionDecl(hasDescendant(
  671. exprWithCleanups(has(cxxConstructExpr(
  672. has(materializeTemporaryExpr(has(implicitCastExpr(
  673. has(cxxTemporaryObjectExpr())))))))))));
  674. }
  675. TEST_P(ImportType, ImportAtomicType) {
  676. MatchVerifier<Decl> Verifier;
  677. testImport(
  678. "void declToImport() { typedef _Atomic(int) a_int; }",
  679. Lang_CXX11, "", Lang_CXX11, Verifier,
  680. functionDecl(hasDescendant(typedefDecl(has(atomicType())))));
  681. }
  682. TEST_P(ImportDecl, ImportFunctionTemplateDecl) {
  683. MatchVerifier<Decl> Verifier;
  684. testImport(
  685. "template <typename T> void declToImport() { };",
  686. Lang_CXX, "", Lang_CXX, Verifier,
  687. functionTemplateDecl());
  688. }
  689. TEST_P(ImportExpr, ImportCXXDependentScopeMemberExpr) {
  690. MatchVerifier<Decl> Verifier;
  691. testImport(
  692. "template <typename T> struct C { T t; };"
  693. "template <typename T> void declToImport() {"
  694. " C<T> d;"
  695. " (void)d.t;"
  696. "}"
  697. "void instantiate() { declToImport<int>(); }",
  698. Lang_CXX, "", Lang_CXX, Verifier,
  699. functionTemplateDecl(hasDescendant(
  700. cStyleCastExpr(has(cxxDependentScopeMemberExpr())))));
  701. testImport(
  702. "template <typename T> struct C { T t; };"
  703. "template <typename T> void declToImport() {"
  704. " C<T> d;"
  705. " (void)(&d)->t;"
  706. "}"
  707. "void instantiate() { declToImport<int>(); }",
  708. Lang_CXX, "", Lang_CXX, Verifier,
  709. functionTemplateDecl(hasDescendant(
  710. cStyleCastExpr(has(cxxDependentScopeMemberExpr())))));
  711. }
  712. TEST_P(ImportType, ImportTypeAliasTemplate) {
  713. MatchVerifier<Decl> Verifier;
  714. testImport(
  715. "template <int K>"
  716. "struct dummy { static const int i = K; };"
  717. "template <int K> using dummy2 = dummy<K>;"
  718. "int declToImport() { return dummy2<3>::i; }",
  719. Lang_CXX11, "", Lang_CXX11, Verifier,
  720. functionDecl(
  721. hasDescendant(implicitCastExpr(has(declRefExpr()))),
  722. unless(hasAncestor(translationUnitDecl(has(typeAliasDecl()))))));
  723. }
  724. const internal::VariadicDynCastAllOfMatcher<Decl, VarTemplateSpecializationDecl>
  725. varTemplateSpecializationDecl;
  726. TEST_P(ImportDecl, ImportVarTemplate) {
  727. MatchVerifier<Decl> Verifier;
  728. testImport(
  729. "template <typename T>"
  730. "T pi = T(3.1415926535897932385L);"
  731. "void declToImport() { (void)pi<int>; }",
  732. Lang_CXX14, "", Lang_CXX14, Verifier,
  733. functionDecl(
  734. hasDescendant(declRefExpr(to(varTemplateSpecializationDecl()))),
  735. unless(hasAncestor(translationUnitDecl(has(varDecl(
  736. hasName("pi"), unless(varTemplateSpecializationDecl()))))))));
  737. }
  738. TEST_P(ImportType, ImportPackExpansion) {
  739. MatchVerifier<Decl> Verifier;
  740. testImport(
  741. "template <typename... Args>"
  742. "struct dummy {"
  743. " dummy(Args... args) {}"
  744. " static const int i = 4;"
  745. "};"
  746. "int declToImport() { return dummy<int>::i; }",
  747. Lang_CXX11, "", Lang_CXX11, Verifier,
  748. functionDecl(hasDescendant(
  749. returnStmt(has(implicitCastExpr(has(declRefExpr())))))));
  750. }
  751. const internal::VariadicDynCastAllOfMatcher<Type,
  752. DependentTemplateSpecializationType>
  753. dependentTemplateSpecializationType;
  754. TEST_P(ImportType, ImportDependentTemplateSpecialization) {
  755. MatchVerifier<Decl> Verifier;
  756. testImport(
  757. "template<typename T>"
  758. "struct A;"
  759. "template<typename T>"
  760. "struct declToImport {"
  761. " typename A<T>::template B<T> a;"
  762. "};",
  763. Lang_CXX, "", Lang_CXX, Verifier,
  764. classTemplateDecl(has(cxxRecordDecl(has(
  765. fieldDecl(hasType(dependentTemplateSpecializationType())))))));
  766. }
  767. const internal::VariadicDynCastAllOfMatcher<Stmt, SizeOfPackExpr>
  768. sizeOfPackExpr;
  769. TEST_P(ImportExpr, ImportSizeOfPackExpr) {
  770. MatchVerifier<Decl> Verifier;
  771. testImport(
  772. "template <typename... Ts>"
  773. "void declToImport() {"
  774. " const int i = sizeof...(Ts);"
  775. "};"
  776. "void g() { declToImport<int>(); }",
  777. Lang_CXX11, "", Lang_CXX11, Verifier,
  778. functionTemplateDecl(hasDescendant(sizeOfPackExpr())));
  779. testImport(
  780. "template <typename... Ts>"
  781. "using X = int[sizeof...(Ts)];"
  782. "template <typename... Us>"
  783. "struct Y {"
  784. " X<Us..., int, double, int, Us...> f;"
  785. "};"
  786. "Y<float, int> declToImport;",
  787. Lang_CXX11, "", Lang_CXX11, Verifier,
  788. varDecl(hasType(classTemplateSpecializationDecl(has(fieldDecl(hasType(
  789. hasUnqualifiedDesugaredType(constantArrayType(hasSize(7))))))))));
  790. }
  791. /// \brief Matches __builtin_types_compatible_p:
  792. /// GNU extension to check equivalent types
  793. /// Given
  794. /// \code
  795. /// __builtin_types_compatible_p(int, int)
  796. /// \endcode
  797. // will generate TypeTraitExpr <...> 'int'
  798. const internal::VariadicDynCastAllOfMatcher<Stmt, TypeTraitExpr> typeTraitExpr;
  799. TEST_P(ImportExpr, ImportTypeTraitExpr) {
  800. MatchVerifier<Decl> Verifier;
  801. testImport(
  802. "void declToImport() { "
  803. " (void)__builtin_types_compatible_p(int, int);"
  804. "}",
  805. Lang_C, "", Lang_C, Verifier,
  806. functionDecl(hasDescendant(typeTraitExpr(hasType(asString("int"))))));
  807. }
  808. const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTypeidExpr> cxxTypeidExpr;
  809. TEST_P(ImportExpr, ImportCXXTypeidExpr) {
  810. MatchVerifier<Decl> Verifier;
  811. testImport(
  812. "namespace std { class type_info {}; }"
  813. "void declToImport() {"
  814. " int x;"
  815. " auto a = typeid(int); auto b = typeid(x);"
  816. "}",
  817. Lang_CXX11, "", Lang_CXX11, Verifier,
  818. functionDecl(
  819. hasDescendant(varDecl(
  820. hasName("a"), hasInitializer(hasDescendant(cxxTypeidExpr())))),
  821. hasDescendant(varDecl(
  822. hasName("b"), hasInitializer(hasDescendant(cxxTypeidExpr()))))));
  823. }
  824. TEST_P(ImportExpr, ImportTypeTraitExprValDep) {
  825. MatchVerifier<Decl> Verifier;
  826. testImport(
  827. "template<typename T> struct declToImport {"
  828. " void m() { (void)__is_pod(T); }"
  829. "};"
  830. "void f() { declToImport<int>().m(); }",
  831. Lang_CXX11, "", Lang_CXX11, Verifier,
  832. classTemplateDecl(has(cxxRecordDecl(has(
  833. functionDecl(hasDescendant(
  834. typeTraitExpr(hasType(booleanType())))))))));
  835. }
  836. TEST_P(ImportDecl, ImportRecordDeclInFunc) {
  837. MatchVerifier<Decl> Verifier;
  838. testImport("int declToImport() { "
  839. " struct data_t {int a;int b;};"
  840. " struct data_t d;"
  841. " return 0;"
  842. "}",
  843. Lang_C, "", Lang_C, Verifier,
  844. functionDecl(hasBody(compoundStmt(
  845. has(declStmt(hasSingleDecl(varDecl(hasName("d")))))))));
  846. }
  847. TEST_P(ASTImporterOptionSpecificTestBase, ImportRecordTypeInFunc) {
  848. Decl *FromTU = getTuDecl("int declToImport() { "
  849. " struct data_t {int a;int b;};"
  850. " struct data_t d;"
  851. " return 0;"
  852. "}",
  853. Lang_C, "input.c");
  854. auto *FromVar =
  855. FirstDeclMatcher<VarDecl>().match(FromTU, varDecl(hasName("d")));
  856. ASSERT_TRUE(FromVar);
  857. auto ToType =
  858. ImportType(FromVar->getType().getCanonicalType(), FromVar, Lang_C);
  859. EXPECT_FALSE(ToType.isNull());
  860. }
  861. TEST_P(ASTImporterOptionSpecificTestBase, ImportRecordDeclInFuncParams) {
  862. // This construct is not supported by ASTImporter.
  863. Decl *FromTU = getTuDecl(
  864. "int declToImport(struct data_t{int a;int b;} ***d){ return 0; }",
  865. Lang_C, "input.c");
  866. auto *From = FirstDeclMatcher<FunctionDecl>().match(
  867. FromTU, functionDecl(hasName("declToImport")));
  868. ASSERT_TRUE(From);
  869. auto *To = Import(From, Lang_C);
  870. EXPECT_EQ(To, nullptr);
  871. }
  872. TEST_P(ASTImporterOptionSpecificTestBase, ImportRecordDeclInFuncFromMacro) {
  873. Decl *FromTU = getTuDecl(
  874. "#define NONAME_SIZEOF(type) sizeof(struct{type *dummy;}) \n"
  875. "int declToImport(){ return NONAME_SIZEOF(int); }",
  876. Lang_C, "input.c");
  877. auto *From = FirstDeclMatcher<FunctionDecl>().match(
  878. FromTU, functionDecl(hasName("declToImport")));
  879. ASSERT_TRUE(From);
  880. auto *To = Import(From, Lang_C);
  881. ASSERT_TRUE(To);
  882. EXPECT_TRUE(MatchVerifier<FunctionDecl>().match(
  883. To, functionDecl(hasName("declToImport"),
  884. hasDescendant(unaryExprOrTypeTraitExpr()))));
  885. }
  886. TEST_P(ASTImporterOptionSpecificTestBase,
  887. ImportRecordDeclInFuncParamsFromMacro) {
  888. // This construct is not supported by ASTImporter.
  889. Decl *FromTU = getTuDecl(
  890. "#define PAIR_STRUCT(type) struct data_t{type a;type b;} \n"
  891. "int declToImport(PAIR_STRUCT(int) ***d){ return 0; }",
  892. Lang_C, "input.c");
  893. auto *From = FirstDeclMatcher<FunctionDecl>().match(
  894. FromTU, functionDecl(hasName("declToImport")));
  895. ASSERT_TRUE(From);
  896. auto *To = Import(From, Lang_C);
  897. EXPECT_EQ(To, nullptr);
  898. }
  899. const internal::VariadicDynCastAllOfMatcher<Expr, CXXPseudoDestructorExpr>
  900. cxxPseudoDestructorExpr;
  901. TEST_P(ImportExpr, ImportCXXPseudoDestructorExpr) {
  902. MatchVerifier<Decl> Verifier;
  903. testImport(
  904. "typedef int T;"
  905. "void declToImport(int *p) {"
  906. " T t;"
  907. " p->T::~T();"
  908. "}",
  909. Lang_CXX, "", Lang_CXX, Verifier,
  910. functionDecl(hasDescendant(
  911. callExpr(has(cxxPseudoDestructorExpr())))));
  912. }
  913. TEST_P(ImportDecl, ImportUsingDecl) {
  914. MatchVerifier<Decl> Verifier;
  915. testImport(
  916. "namespace foo { int bar; }"
  917. "void declToImport() { using foo::bar; }",
  918. Lang_CXX, "", Lang_CXX, Verifier,
  919. functionDecl(hasDescendant(usingDecl())));
  920. }
  921. /// \brief Matches shadow declarations introduced into a scope by a
  922. /// (resolved) using declaration.
  923. ///
  924. /// Given
  925. /// \code
  926. /// namespace n { int f; }
  927. /// namespace declToImport { using n::f; }
  928. /// \endcode
  929. /// usingShadowDecl()
  930. /// matches \code f \endcode
  931. const internal::VariadicDynCastAllOfMatcher<Decl,
  932. UsingShadowDecl> usingShadowDecl;
  933. TEST_P(ImportDecl, ImportUsingShadowDecl) {
  934. MatchVerifier<Decl> Verifier;
  935. testImport(
  936. "namespace foo { int bar; }"
  937. "namespace declToImport { using foo::bar; }",
  938. Lang_CXX, "", Lang_CXX, Verifier,
  939. namespaceDecl(has(usingShadowDecl())));
  940. }
  941. TEST_P(ImportExpr, ImportUnresolvedLookupExpr) {
  942. MatchVerifier<Decl> Verifier;
  943. testImport(
  944. "template<typename T> int foo();"
  945. "template <typename T> void declToImport() {"
  946. " (void)::foo<T>;"
  947. " (void)::template foo<T>;"
  948. "}"
  949. "void instantiate() { declToImport<int>(); }",
  950. Lang_CXX, "", Lang_CXX, Verifier,
  951. functionTemplateDecl(hasDescendant(unresolvedLookupExpr())));
  952. }
  953. TEST_P(ImportExpr, ImportCXXUnresolvedConstructExpr) {
  954. MatchVerifier<Decl> Verifier;
  955. testImport(
  956. "template <typename T> struct C { T t; };"
  957. "template <typename T> void declToImport() {"
  958. " C<T> d;"
  959. " d.t = T();"
  960. "}"
  961. "void instantiate() { declToImport<int>(); }",
  962. Lang_CXX, "", Lang_CXX, Verifier,
  963. functionTemplateDecl(hasDescendant(
  964. binaryOperator(has(cxxUnresolvedConstructExpr())))));
  965. testImport(
  966. "template <typename T> struct C { T t; };"
  967. "template <typename T> void declToImport() {"
  968. " C<T> d;"
  969. " (&d)->t = T();"
  970. "}"
  971. "void instantiate() { declToImport<int>(); }",
  972. Lang_CXX, "", Lang_CXX, Verifier,
  973. functionTemplateDecl(hasDescendant(
  974. binaryOperator(has(cxxUnresolvedConstructExpr())))));
  975. }
  976. /// Check that function "declToImport()" (which is the templated function
  977. /// for corresponding FunctionTemplateDecl) is not added into DeclContext.
  978. /// Same for class template declarations.
  979. TEST_P(ImportDecl, ImportTemplatedDeclForTemplate) {
  980. MatchVerifier<Decl> Verifier;
  981. testImport(
  982. "template <typename T> void declToImport() { T a = 1; }"
  983. "void instantiate() { declToImport<int>(); }",
  984. Lang_CXX, "", Lang_CXX, Verifier,
  985. functionTemplateDecl(hasAncestor(translationUnitDecl(
  986. unless(has(functionDecl(hasName("declToImport"))))))));
  987. testImport(
  988. "template <typename T> struct declToImport { T t; };"
  989. "void instantiate() { declToImport<int>(); }",
  990. Lang_CXX, "", Lang_CXX, Verifier,
  991. classTemplateDecl(hasAncestor(translationUnitDecl(
  992. unless(has(cxxRecordDecl(hasName("declToImport"))))))));
  993. }
  994. TEST_P(ImportDecl, ImportClassTemplatePartialSpecialization) {
  995. MatchVerifier<Decl> Verifier;
  996. auto Code =
  997. R"s(
  998. struct declToImport {
  999. template <typename T0> struct X;
  1000. template <typename T0> struct X<T0 *> {};
  1001. };
  1002. )s";
  1003. testImport(Code, Lang_CXX, "", Lang_CXX, Verifier,
  1004. recordDecl(has(classTemplateDecl()),
  1005. has(classTemplateSpecializationDecl())));
  1006. }
  1007. TEST_P(ImportExpr, CXXOperatorCallExpr) {
  1008. MatchVerifier<Decl> Verifier;
  1009. testImport(
  1010. "class declToImport {"
  1011. " void f() { *this = declToImport(); }"
  1012. "};",
  1013. Lang_CXX, "", Lang_CXX, Verifier,
  1014. cxxRecordDecl(has(cxxMethodDecl(hasDescendant(
  1015. cxxOperatorCallExpr())))));
  1016. }
  1017. TEST_P(ImportExpr, DependentSizedArrayType) {
  1018. MatchVerifier<Decl> Verifier;
  1019. testImport(
  1020. "template<typename T, int Size> class declToImport {"
  1021. " T data[Size];"
  1022. "};",
  1023. Lang_CXX, "", Lang_CXX, Verifier,
  1024. classTemplateDecl(has(cxxRecordDecl(
  1025. has(fieldDecl(hasType(dependentSizedArrayType())))))));
  1026. }
  1027. TEST_P(ASTImporterOptionSpecificTestBase, ImportBeginLocOfDeclRefExpr) {
  1028. Decl *FromTU = getTuDecl(
  1029. "class A { public: static int X; }; void f() { (void)A::X; }", Lang_CXX);
  1030. auto From = FirstDeclMatcher<FunctionDecl>().match(
  1031. FromTU, functionDecl(hasName("f")));
  1032. ASSERT_TRUE(From);
  1033. ASSERT_TRUE(
  1034. cast<CStyleCastExpr>(cast<CompoundStmt>(From->getBody())->body_front())
  1035. ->getSubExpr()
  1036. ->getBeginLoc()
  1037. .isValid());
  1038. FunctionDecl *To = Import(From, Lang_CXX);
  1039. ASSERT_TRUE(To);
  1040. ASSERT_TRUE(
  1041. cast<CStyleCastExpr>(cast<CompoundStmt>(To->getBody())->body_front())
  1042. ->getSubExpr()
  1043. ->getBeginLoc()
  1044. .isValid());
  1045. }
  1046. TEST_P(ASTImporterOptionSpecificTestBase,
  1047. ImportOfTemplatedDeclOfClassTemplateDecl) {
  1048. Decl *FromTU = getTuDecl("template<class X> struct S{};", Lang_CXX);
  1049. auto From =
  1050. FirstDeclMatcher<ClassTemplateDecl>().match(FromTU, classTemplateDecl());
  1051. ASSERT_TRUE(From);
  1052. auto To = cast<ClassTemplateDecl>(Import(From, Lang_CXX));
  1053. ASSERT_TRUE(To);
  1054. Decl *ToTemplated = To->getTemplatedDecl();
  1055. Decl *ToTemplated1 = Import(From->getTemplatedDecl(), Lang_CXX);
  1056. EXPECT_TRUE(ToTemplated1);
  1057. EXPECT_EQ(ToTemplated1, ToTemplated);
  1058. }
  1059. TEST_P(ASTImporterOptionSpecificTestBase,
  1060. ImportOfTemplatedDeclOfFunctionTemplateDecl) {
  1061. Decl *FromTU = getTuDecl("template<class X> void f(){}", Lang_CXX);
  1062. auto From = FirstDeclMatcher<FunctionTemplateDecl>().match(
  1063. FromTU, functionTemplateDecl());
  1064. ASSERT_TRUE(From);
  1065. auto To = cast<FunctionTemplateDecl>(Import(From, Lang_CXX));
  1066. ASSERT_TRUE(To);
  1067. Decl *ToTemplated = To->getTemplatedDecl();
  1068. Decl *ToTemplated1 = Import(From->getTemplatedDecl(), Lang_CXX);
  1069. EXPECT_TRUE(ToTemplated1);
  1070. EXPECT_EQ(ToTemplated1, ToTemplated);
  1071. }
  1072. TEST_P(ASTImporterOptionSpecificTestBase,
  1073. ImportOfTemplatedDeclShouldImportTheClassTemplateDecl) {
  1074. Decl *FromTU = getTuDecl("template<class X> struct S{};", Lang_CXX);
  1075. auto FromFT =
  1076. FirstDeclMatcher<ClassTemplateDecl>().match(FromTU, classTemplateDecl());
  1077. ASSERT_TRUE(FromFT);
  1078. auto ToTemplated =
  1079. cast<CXXRecordDecl>(Import(FromFT->getTemplatedDecl(), Lang_CXX));
  1080. EXPECT_TRUE(ToTemplated);
  1081. auto ToTU = ToTemplated->getTranslationUnitDecl();
  1082. auto ToFT =
  1083. FirstDeclMatcher<ClassTemplateDecl>().match(ToTU, classTemplateDecl());
  1084. EXPECT_TRUE(ToFT);
  1085. }
  1086. TEST_P(ASTImporterOptionSpecificTestBase,
  1087. ImportOfTemplatedDeclShouldImportTheFunctionTemplateDecl) {
  1088. Decl *FromTU = getTuDecl("template<class X> void f(){}", Lang_CXX);
  1089. auto FromFT = FirstDeclMatcher<FunctionTemplateDecl>().match(
  1090. FromTU, functionTemplateDecl());
  1091. ASSERT_TRUE(FromFT);
  1092. auto ToTemplated =
  1093. cast<FunctionDecl>(Import(FromFT->getTemplatedDecl(), Lang_CXX));
  1094. EXPECT_TRUE(ToTemplated);
  1095. auto ToTU = ToTemplated->getTranslationUnitDecl();
  1096. auto ToFT = FirstDeclMatcher<FunctionTemplateDecl>().match(
  1097. ToTU, functionTemplateDecl());
  1098. EXPECT_TRUE(ToFT);
  1099. }
  1100. TEST_P(ASTImporterOptionSpecificTestBase, ImportCorrectTemplatedDecl) {
  1101. auto Code =
  1102. R"(
  1103. namespace x {
  1104. template<class X> struct S1{};
  1105. template<class X> struct S2{};
  1106. template<class X> struct S3{};
  1107. }
  1108. )";
  1109. Decl *FromTU = getTuDecl(Code, Lang_CXX);
  1110. auto FromNs =
  1111. FirstDeclMatcher<NamespaceDecl>().match(FromTU, namespaceDecl());
  1112. auto ToNs = cast<NamespaceDecl>(Import(FromNs, Lang_CXX));
  1113. ASSERT_TRUE(ToNs);
  1114. auto From =
  1115. FirstDeclMatcher<ClassTemplateDecl>().match(FromTU,
  1116. classTemplateDecl(
  1117. hasName("S2")));
  1118. auto To =
  1119. FirstDeclMatcher<ClassTemplateDecl>().match(ToNs,
  1120. classTemplateDecl(
  1121. hasName("S2")));
  1122. ASSERT_TRUE(From);
  1123. ASSERT_TRUE(To);
  1124. auto ToTemplated = To->getTemplatedDecl();
  1125. auto ToTemplated1 =
  1126. cast<CXXRecordDecl>(Import(From->getTemplatedDecl(), Lang_CXX));
  1127. EXPECT_TRUE(ToTemplated1);
  1128. ASSERT_EQ(ToTemplated1, ToTemplated);
  1129. }
  1130. TEST_P(ASTImporterOptionSpecificTestBase, ImportChooseExpr) {
  1131. // This tests the import of isConditionTrue directly to make sure the importer
  1132. // gets it right.
  1133. Decl *From, *To;
  1134. std::tie(From, To) = getImportedDecl(
  1135. "void declToImport() { (void)__builtin_choose_expr(1, 0, 1); }",
  1136. Lang_C, "", Lang_C);
  1137. auto ToResults = match(chooseExpr().bind("choose"), To->getASTContext());
  1138. auto FromResults = match(chooseExpr().bind("choose"), From->getASTContext());
  1139. const ChooseExpr *FromChooseExpr =
  1140. selectFirst<ChooseExpr>("choose", FromResults);
  1141. ASSERT_TRUE(FromChooseExpr);
  1142. const ChooseExpr *ToChooseExpr = selectFirst<ChooseExpr>("choose", ToResults);
  1143. ASSERT_TRUE(ToChooseExpr);
  1144. EXPECT_EQ(FromChooseExpr->isConditionTrue(), ToChooseExpr->isConditionTrue());
  1145. EXPECT_EQ(FromChooseExpr->isConditionDependent(),
  1146. ToChooseExpr->isConditionDependent());
  1147. }
  1148. TEST_P(ASTImporterOptionSpecificTestBase,
  1149. ImportFunctionWithBackReferringParameter) {
  1150. Decl *From, *To;
  1151. std::tie(From, To) = getImportedDecl(
  1152. R"(
  1153. template <typename T> struct X {};
  1154. void declToImport(int y, X<int> &x) {}
  1155. template <> struct X<int> {
  1156. void g() {
  1157. X<int> x;
  1158. declToImport(0, x);
  1159. }
  1160. };
  1161. )",
  1162. Lang_CXX, "", Lang_CXX);
  1163. MatchVerifier<Decl> Verifier;
  1164. auto Matcher = functionDecl(hasName("declToImport"),
  1165. parameterCountIs(2),
  1166. hasParameter(0, hasName("y")),
  1167. hasParameter(1, hasName("x")),
  1168. hasParameter(1, hasType(asString("X<int> &"))));
  1169. ASSERT_TRUE(Verifier.match(From, Matcher));
  1170. EXPECT_TRUE(Verifier.match(To, Matcher));
  1171. }
  1172. TEST_P(ASTImporterOptionSpecificTestBase,
  1173. TUshouldNotContainTemplatedDeclOfFunctionTemplates) {
  1174. Decl *From, *To;
  1175. std::tie(From, To) =
  1176. getImportedDecl("template <typename T> void declToImport() { T a = 1; }"
  1177. "void instantiate() { declToImport<int>(); }",
  1178. Lang_CXX, "", Lang_CXX);
  1179. auto Check = [](Decl *D) -> bool {
  1180. auto TU = D->getTranslationUnitDecl();
  1181. for (auto Child : TU->decls()) {
  1182. if (auto *FD = dyn_cast<FunctionDecl>(Child)) {
  1183. if (FD->getNameAsString() == "declToImport") {
  1184. GTEST_NONFATAL_FAILURE_(
  1185. "TU should not contain any FunctionDecl with name declToImport");
  1186. return false;
  1187. }
  1188. }
  1189. }
  1190. return true;
  1191. };
  1192. ASSERT_TRUE(Check(From));
  1193. EXPECT_TRUE(Check(To));
  1194. }
  1195. TEST_P(ASTImporterOptionSpecificTestBase,
  1196. TUshouldNotContainTemplatedDeclOfClassTemplates) {
  1197. Decl *From, *To;
  1198. std::tie(From, To) =
  1199. getImportedDecl("template <typename T> struct declToImport { T t; };"
  1200. "void instantiate() { declToImport<int>(); }",
  1201. Lang_CXX, "", Lang_CXX);
  1202. auto Check = [](Decl *D) -> bool {
  1203. auto TU = D->getTranslationUnitDecl();
  1204. for (auto Child : TU->decls()) {
  1205. if (auto *RD = dyn_cast<CXXRecordDecl>(Child)) {
  1206. if (RD->getNameAsString() == "declToImport") {
  1207. GTEST_NONFATAL_FAILURE_(
  1208. "TU should not contain any CXXRecordDecl with name declToImport");
  1209. return false;
  1210. }
  1211. }
  1212. }
  1213. return true;
  1214. };
  1215. ASSERT_TRUE(Check(From));
  1216. EXPECT_TRUE(Check(To));
  1217. }
  1218. TEST_P(ASTImporterOptionSpecificTestBase,
  1219. TUshouldNotContainTemplatedDeclOfTypeAlias) {
  1220. Decl *From, *To;
  1221. std::tie(From, To) =
  1222. getImportedDecl(
  1223. "template <typename T> struct X {};"
  1224. "template <typename T> using declToImport = X<T>;"
  1225. "void instantiate() { declToImport<int> a; }",
  1226. Lang_CXX11, "", Lang_CXX11);
  1227. auto Check = [](Decl *D) -> bool {
  1228. auto TU = D->getTranslationUnitDecl();
  1229. for (auto Child : TU->decls()) {
  1230. if (auto *AD = dyn_cast<TypeAliasDecl>(Child)) {
  1231. if (AD->getNameAsString() == "declToImport") {
  1232. GTEST_NONFATAL_FAILURE_(
  1233. "TU should not contain any TypeAliasDecl with name declToImport");
  1234. return false;
  1235. }
  1236. }
  1237. }
  1238. return true;
  1239. };
  1240. ASSERT_TRUE(Check(From));
  1241. EXPECT_TRUE(Check(To));
  1242. }
  1243. TEST_P(ASTImporterOptionSpecificTestBase,
  1244. TUshouldNotContainClassTemplateSpecializationOfImplicitInstantiation) {
  1245. Decl *From, *To;
  1246. std::tie(From, To) = getImportedDecl(
  1247. R"(
  1248. template<class T>
  1249. class Base {};
  1250. class declToImport : public Base<declToImport> {};
  1251. )",
  1252. Lang_CXX, "", Lang_CXX);
  1253. // Check that the ClassTemplateSpecializationDecl is NOT the child of the TU.
  1254. auto Pattern =
  1255. translationUnitDecl(unless(has(classTemplateSpecializationDecl())));
  1256. ASSERT_TRUE(
  1257. MatchVerifier<Decl>{}.match(From->getTranslationUnitDecl(), Pattern));
  1258. EXPECT_TRUE(
  1259. MatchVerifier<Decl>{}.match(To->getTranslationUnitDecl(), Pattern));
  1260. // Check that the ClassTemplateSpecializationDecl is the child of the
  1261. // ClassTemplateDecl.
  1262. Pattern = translationUnitDecl(has(classTemplateDecl(
  1263. hasName("Base"), has(classTemplateSpecializationDecl()))));
  1264. ASSERT_TRUE(
  1265. MatchVerifier<Decl>{}.match(From->getTranslationUnitDecl(), Pattern));
  1266. EXPECT_TRUE(
  1267. MatchVerifier<Decl>{}.match(To->getTranslationUnitDecl(), Pattern));
  1268. }
  1269. AST_MATCHER_P(RecordDecl, hasFieldOrder, std::vector<StringRef>, Order) {
  1270. size_t Index = 0;
  1271. for (FieldDecl *Field : Node.fields()) {
  1272. if (Index == Order.size())
  1273. return false;
  1274. if (Field->getName() != Order[Index])
  1275. return false;
  1276. ++Index;
  1277. }
  1278. return Index == Order.size();
  1279. }
  1280. TEST_P(ASTImporterOptionSpecificTestBase,
  1281. TUshouldContainClassTemplateSpecializationOfExplicitInstantiation) {
  1282. Decl *From, *To;
  1283. std::tie(From, To) = getImportedDecl(
  1284. R"(
  1285. namespace NS {
  1286. template<class T>
  1287. class X {};
  1288. template class X<int>;
  1289. }
  1290. )",
  1291. Lang_CXX, "", Lang_CXX, "NS");
  1292. // Check that the ClassTemplateSpecializationDecl is NOT the child of the
  1293. // ClassTemplateDecl.
  1294. auto Pattern = namespaceDecl(has(classTemplateDecl(
  1295. hasName("X"), unless(has(classTemplateSpecializationDecl())))));
  1296. ASSERT_TRUE(MatchVerifier<Decl>{}.match(From, Pattern));
  1297. EXPECT_TRUE(MatchVerifier<Decl>{}.match(To, Pattern));
  1298. // Check that the ClassTemplateSpecializationDecl is the child of the
  1299. // NamespaceDecl.
  1300. Pattern = namespaceDecl(has(classTemplateSpecializationDecl(hasName("X"))));
  1301. ASSERT_TRUE(MatchVerifier<Decl>{}.match(From, Pattern));
  1302. EXPECT_TRUE(MatchVerifier<Decl>{}.match(To, Pattern));
  1303. }
  1304. TEST_P(ASTImporterOptionSpecificTestBase,
  1305. CXXRecordDeclFieldsShouldBeInCorrectOrder) {
  1306. Decl *From, *To;
  1307. std::tie(From, To) =
  1308. getImportedDecl(
  1309. "struct declToImport { int a; int b; };",
  1310. Lang_CXX11, "", Lang_CXX11);
  1311. MatchVerifier<Decl> Verifier;
  1312. ASSERT_TRUE(Verifier.match(From, cxxRecordDecl(hasFieldOrder({"a", "b"}))));
  1313. EXPECT_TRUE(Verifier.match(To, cxxRecordDecl(hasFieldOrder({"a", "b"}))));
  1314. }
  1315. TEST_P(ASTImporterOptionSpecificTestBase,
  1316. CXXRecordDeclFieldOrderShouldNotDependOnImportOrder) {
  1317. Decl *From, *To;
  1318. std::tie(From, To) = getImportedDecl(
  1319. // The original recursive algorithm of ASTImporter first imports 'c' then
  1320. // 'b' and lastly 'a'. Therefore we must restore the order somehow.
  1321. R"s(
  1322. struct declToImport {
  1323. int a = c + b;
  1324. int b = 1;
  1325. int c = 2;
  1326. };
  1327. )s",
  1328. Lang_CXX11, "", Lang_CXX11);
  1329. MatchVerifier<Decl> Verifier;
  1330. ASSERT_TRUE(
  1331. Verifier.match(From, cxxRecordDecl(hasFieldOrder({"a", "b", "c"}))));
  1332. EXPECT_TRUE(
  1333. Verifier.match(To, cxxRecordDecl(hasFieldOrder({"a", "b", "c"}))));
  1334. }
  1335. TEST_P(ASTImporterOptionSpecificTestBase, ShouldImportImplicitCXXRecordDecl) {
  1336. Decl *From, *To;
  1337. std::tie(From, To) = getImportedDecl(
  1338. R"(
  1339. struct declToImport {
  1340. };
  1341. )",
  1342. Lang_CXX, "", Lang_CXX);
  1343. MatchVerifier<Decl> Verifier;
  1344. // Match the implicit Decl.
  1345. auto Matcher = cxxRecordDecl(has(cxxRecordDecl()));
  1346. ASSERT_TRUE(Verifier.match(From, Matcher));
  1347. EXPECT_TRUE(Verifier.match(To, Matcher));
  1348. }
  1349. TEST_P(ASTImporterOptionSpecificTestBase,
  1350. ShouldImportImplicitCXXRecordDeclOfClassTemplate) {
  1351. Decl *From, *To;
  1352. std::tie(From, To) = getImportedDecl(
  1353. R"(
  1354. template <typename U>
  1355. struct declToImport {
  1356. };
  1357. )",
  1358. Lang_CXX, "", Lang_CXX);
  1359. MatchVerifier<Decl> Verifier;
  1360. // Match the implicit Decl.
  1361. auto Matcher = classTemplateDecl(has(cxxRecordDecl(has(cxxRecordDecl()))));
  1362. ASSERT_TRUE(Verifier.match(From, Matcher));
  1363. EXPECT_TRUE(Verifier.match(To, Matcher));
  1364. }
  1365. TEST_P(ASTImporterOptionSpecificTestBase,
  1366. ShouldImportImplicitCXXRecordDeclOfClassTemplateSpecializationDecl) {
  1367. Decl *From, *To;
  1368. std::tie(From, To) = getImportedDecl(
  1369. R"(
  1370. template<class T>
  1371. class Base {};
  1372. class declToImport : public Base<declToImport> {};
  1373. )",
  1374. Lang_CXX, "", Lang_CXX);
  1375. auto hasImplicitClass = has(cxxRecordDecl());
  1376. auto Pattern = translationUnitDecl(has(classTemplateDecl(
  1377. hasName("Base"),
  1378. has(classTemplateSpecializationDecl(hasImplicitClass)))));
  1379. ASSERT_TRUE(
  1380. MatchVerifier<Decl>{}.match(From->getTranslationUnitDecl(), Pattern));
  1381. EXPECT_TRUE(
  1382. MatchVerifier<Decl>{}.match(To->getTranslationUnitDecl(), Pattern));
  1383. }
  1384. TEST_P(ASTImporterOptionSpecificTestBase, IDNSOrdinary) {
  1385. Decl *From, *To;
  1386. std::tie(From, To) =
  1387. getImportedDecl("void declToImport() {}", Lang_CXX, "", Lang_CXX);
  1388. MatchVerifier<Decl> Verifier;
  1389. auto Matcher = functionDecl();
  1390. ASSERT_TRUE(Verifier.match(From, Matcher));
  1391. EXPECT_TRUE(Verifier.match(To, Matcher));
  1392. EXPECT_EQ(From->getIdentifierNamespace(), To->getIdentifierNamespace());
  1393. }
  1394. TEST_P(ASTImporterOptionSpecificTestBase, IDNSOfNonmemberOperator) {
  1395. Decl *FromTU = getTuDecl(
  1396. R"(
  1397. struct X {};
  1398. void operator<<(int, X);
  1399. )",
  1400. Lang_CXX);
  1401. Decl *From = LastDeclMatcher<Decl>{}.match(FromTU, functionDecl());
  1402. const Decl *To = Import(From, Lang_CXX);
  1403. EXPECT_EQ(From->getIdentifierNamespace(), To->getIdentifierNamespace());
  1404. }
  1405. TEST_P(ASTImporterOptionSpecificTestBase,
  1406. ShouldImportMembersOfClassTemplateSpecializationDecl) {
  1407. Decl *From, *To;
  1408. std::tie(From, To) = getImportedDecl(
  1409. R"(
  1410. template<class T>
  1411. class Base { int a; };
  1412. class declToImport : Base<declToImport> {};
  1413. )",
  1414. Lang_CXX, "", Lang_CXX);
  1415. auto Pattern = translationUnitDecl(has(classTemplateDecl(
  1416. hasName("Base"),
  1417. has(classTemplateSpecializationDecl(has(fieldDecl(hasName("a"))))))));
  1418. ASSERT_TRUE(
  1419. MatchVerifier<Decl>{}.match(From->getTranslationUnitDecl(), Pattern));
  1420. EXPECT_TRUE(
  1421. MatchVerifier<Decl>{}.match(To->getTranslationUnitDecl(), Pattern));
  1422. }
  1423. TEST_P(ASTImporterOptionSpecificTestBase,
  1424. ImportDefinitionOfClassTemplateAfterFwdDecl) {
  1425. {
  1426. Decl *FromTU = getTuDecl(
  1427. R"(
  1428. template <typename T>
  1429. struct B;
  1430. )",
  1431. Lang_CXX, "input0.cc");
  1432. auto *FromD = FirstDeclMatcher<ClassTemplateDecl>().match(
  1433. FromTU, classTemplateDecl(hasName("B")));
  1434. Import(FromD, Lang_CXX);
  1435. }
  1436. {
  1437. Decl *FromTU = getTuDecl(
  1438. R"(
  1439. template <typename T>
  1440. struct B {
  1441. void f();
  1442. };
  1443. )",
  1444. Lang_CXX, "input1.cc");
  1445. FunctionDecl *FromD = FirstDeclMatcher<FunctionDecl>().match(
  1446. FromTU, functionDecl(hasName("f")));
  1447. Import(FromD, Lang_CXX);
  1448. auto *FromCTD = FirstDeclMatcher<ClassTemplateDecl>().match(
  1449. FromTU, classTemplateDecl(hasName("B")));
  1450. auto *ToCTD = cast<ClassTemplateDecl>(Import(FromCTD, Lang_CXX));
  1451. EXPECT_TRUE(ToCTD->isThisDeclarationADefinition());
  1452. }
  1453. }
  1454. TEST_P(ASTImporterOptionSpecificTestBase,
  1455. ImportDefinitionOfClassTemplateIfThereIsAnExistingFwdDeclAndDefinition) {
  1456. Decl *ToTU = getToTuDecl(
  1457. R"(
  1458. template <typename T>
  1459. struct B {
  1460. void f();
  1461. };
  1462. template <typename T>
  1463. struct B;
  1464. )",
  1465. Lang_CXX);
  1466. ASSERT_EQ(1u, DeclCounterWithPredicate<ClassTemplateDecl>(
  1467. [](const ClassTemplateDecl *T) {
  1468. return T->isThisDeclarationADefinition();
  1469. })
  1470. .match(ToTU, classTemplateDecl()));
  1471. Decl *FromTU = getTuDecl(
  1472. R"(
  1473. template <typename T>
  1474. struct B {
  1475. void f();
  1476. };
  1477. )",
  1478. Lang_CXX, "input1.cc");
  1479. ClassTemplateDecl *FromD = FirstDeclMatcher<ClassTemplateDecl>().match(
  1480. FromTU, classTemplateDecl(hasName("B")));
  1481. Import(FromD, Lang_CXX);
  1482. // We should have only one definition.
  1483. EXPECT_EQ(1u, DeclCounterWithPredicate<ClassTemplateDecl>(
  1484. [](const ClassTemplateDecl *T) {
  1485. return T->isThisDeclarationADefinition();
  1486. })
  1487. .match(ToTU, classTemplateDecl()));
  1488. }
  1489. TEST_P(ASTImporterOptionSpecificTestBase,
  1490. ImportDefinitionOfClassIfThereIsAnExistingFwdDeclAndDefinition) {
  1491. Decl *ToTU = getToTuDecl(
  1492. R"(
  1493. struct B {
  1494. void f();
  1495. };
  1496. struct B;
  1497. )",
  1498. Lang_CXX);
  1499. ASSERT_EQ(2u, DeclCounter<CXXRecordDecl>().match(
  1500. ToTU, cxxRecordDecl(unless(isImplicit()))));
  1501. Decl *FromTU = getTuDecl(
  1502. R"(
  1503. struct B {
  1504. void f();
  1505. };
  1506. )",
  1507. Lang_CXX, "input1.cc");
  1508. auto *FromD = FirstDeclMatcher<CXXRecordDecl>().match(
  1509. FromTU, cxxRecordDecl(hasName("B")));
  1510. Import(FromD, Lang_CXX);
  1511. EXPECT_EQ(2u, DeclCounter<CXXRecordDecl>().match(
  1512. ToTU, cxxRecordDecl(unless(isImplicit()))));
  1513. }
  1514. static void CompareSourceLocs(FullSourceLoc Loc1, FullSourceLoc Loc2) {
  1515. EXPECT_EQ(Loc1.getExpansionLineNumber(), Loc2.getExpansionLineNumber());
  1516. EXPECT_EQ(Loc1.getExpansionColumnNumber(), Loc2.getExpansionColumnNumber());
  1517. EXPECT_EQ(Loc1.getSpellingLineNumber(), Loc2.getSpellingLineNumber());
  1518. EXPECT_EQ(Loc1.getSpellingColumnNumber(), Loc2.getSpellingColumnNumber());
  1519. }
  1520. static void CompareSourceRanges(SourceRange Range1, SourceRange Range2,
  1521. SourceManager &SM1, SourceManager &SM2) {
  1522. CompareSourceLocs(FullSourceLoc{ Range1.getBegin(), SM1 },
  1523. FullSourceLoc{ Range2.getBegin(), SM2 });
  1524. CompareSourceLocs(FullSourceLoc{ Range1.getEnd(), SM1 },
  1525. FullSourceLoc{ Range2.getEnd(), SM2 });
  1526. }
  1527. TEST_P(ASTImporterOptionSpecificTestBase, ImportSourceLocs) {
  1528. Decl *FromTU = getTuDecl(
  1529. R"(
  1530. #define MFOO(arg) arg = arg + 1
  1531. void foo() {
  1532. int a = 5;
  1533. MFOO(a);
  1534. }
  1535. )",
  1536. Lang_CXX);
  1537. auto FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, functionDecl());
  1538. auto ToD = Import(FromD, Lang_CXX);
  1539. auto ToLHS = LastDeclMatcher<DeclRefExpr>().match(ToD, declRefExpr());
  1540. auto FromLHS = LastDeclMatcher<DeclRefExpr>().match(FromTU, declRefExpr());
  1541. auto ToRHS = LastDeclMatcher<IntegerLiteral>().match(ToD, integerLiteral());
  1542. auto FromRHS =
  1543. LastDeclMatcher<IntegerLiteral>().match(FromTU, integerLiteral());
  1544. SourceManager &ToSM = ToAST->getASTContext().getSourceManager();
  1545. SourceManager &FromSM = FromD->getASTContext().getSourceManager();
  1546. CompareSourceRanges(ToD->getSourceRange(), FromD->getSourceRange(), ToSM,
  1547. FromSM);
  1548. CompareSourceRanges(ToLHS->getSourceRange(), FromLHS->getSourceRange(), ToSM,
  1549. FromSM);
  1550. CompareSourceRanges(ToRHS->getSourceRange(), FromRHS->getSourceRange(), ToSM,
  1551. FromSM);
  1552. }
  1553. TEST_P(ASTImporterOptionSpecificTestBase, ImportNestedMacro) {
  1554. Decl *FromTU = getTuDecl(
  1555. R"(
  1556. #define FUNC_INT void declToImport
  1557. #define FUNC FUNC_INT
  1558. FUNC(int a);
  1559. )",
  1560. Lang_CXX);
  1561. auto FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, functionDecl());
  1562. auto ToD = Import(FromD, Lang_CXX);
  1563. SourceManager &ToSM = ToAST->getASTContext().getSourceManager();
  1564. SourceManager &FromSM = FromD->getASTContext().getSourceManager();
  1565. CompareSourceRanges(ToD->getSourceRange(), FromD->getSourceRange(), ToSM,
  1566. FromSM);
  1567. }
  1568. TEST_P(
  1569. ASTImporterOptionSpecificTestBase,
  1570. ImportDefinitionOfClassTemplateSpecIfThereIsAnExistingFwdDeclAndDefinition) {
  1571. Decl *ToTU = getToTuDecl(
  1572. R"(
  1573. template <typename T>
  1574. struct B;
  1575. template <>
  1576. struct B<int> {};
  1577. template <>
  1578. struct B<int>;
  1579. )",
  1580. Lang_CXX);
  1581. // We should have only one definition.
  1582. ASSERT_EQ(1u, DeclCounterWithPredicate<ClassTemplateSpecializationDecl>(
  1583. [](const ClassTemplateSpecializationDecl *T) {
  1584. return T->isThisDeclarationADefinition();
  1585. })
  1586. .match(ToTU, classTemplateSpecializationDecl()));
  1587. Decl *FromTU = getTuDecl(
  1588. R"(
  1589. template <typename T>
  1590. struct B;
  1591. template <>
  1592. struct B<int> {};
  1593. )",
  1594. Lang_CXX, "input1.cc");
  1595. auto *FromD = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  1596. FromTU, classTemplateSpecializationDecl(hasName("B")));
  1597. Import(FromD, Lang_CXX);
  1598. // We should have only one definition.
  1599. EXPECT_EQ(1u, DeclCounterWithPredicate<ClassTemplateSpecializationDecl>(
  1600. [](const ClassTemplateSpecializationDecl *T) {
  1601. return T->isThisDeclarationADefinition();
  1602. })
  1603. .match(ToTU, classTemplateSpecializationDecl()));
  1604. }
  1605. TEST_P(ASTImporterOptionSpecificTestBase, ObjectsWithUnnamedStructType) {
  1606. Decl *FromTU = getTuDecl(
  1607. R"(
  1608. struct { int a; int b; } object0 = { 2, 3 };
  1609. struct { int x; int y; int z; } object1;
  1610. )",
  1611. Lang_CXX, "input0.cc");
  1612. auto *Obj0 =
  1613. FirstDeclMatcher<VarDecl>().match(FromTU, varDecl(hasName("object0")));
  1614. auto *From0 = getRecordDecl(Obj0);
  1615. auto *Obj1 =
  1616. FirstDeclMatcher<VarDecl>().match(FromTU, varDecl(hasName("object1")));
  1617. auto *From1 = getRecordDecl(Obj1);
  1618. auto *To0 = Import(From0, Lang_CXX);
  1619. auto *To1 = Import(From1, Lang_CXX);
  1620. EXPECT_TRUE(To0);
  1621. EXPECT_TRUE(To1);
  1622. EXPECT_NE(To0, To1);
  1623. EXPECT_NE(To0->getCanonicalDecl(), To1->getCanonicalDecl());
  1624. }
  1625. TEST_P(ASTImporterOptionSpecificTestBase, AnonymousRecords) {
  1626. auto *Code =
  1627. R"(
  1628. struct X {
  1629. struct { int a; };
  1630. struct { int b; };
  1631. };
  1632. )";
  1633. Decl *FromTU0 = getTuDecl(Code, Lang_C, "input0.c");
  1634. Decl *FromTU1 = getTuDecl(Code, Lang_C, "input1.c");
  1635. auto *X0 =
  1636. FirstDeclMatcher<RecordDecl>().match(FromTU0, recordDecl(hasName("X")));
  1637. auto *X1 =
  1638. FirstDeclMatcher<RecordDecl>().match(FromTU1, recordDecl(hasName("X")));
  1639. Import(X0, Lang_C);
  1640. Import(X1, Lang_C);
  1641. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1642. // We expect no (ODR) warning during the import.
  1643. EXPECT_EQ(0u, ToTU->getASTContext().getDiagnostics().getNumWarnings());
  1644. EXPECT_EQ(1u,
  1645. DeclCounter<RecordDecl>().match(ToTU, recordDecl(hasName("X"))));
  1646. }
  1647. TEST_P(ASTImporterOptionSpecificTestBase, AnonymousRecordsReversed) {
  1648. Decl *FromTU0 = getTuDecl(
  1649. R"(
  1650. struct X {
  1651. struct { int a; };
  1652. struct { int b; };
  1653. };
  1654. )",
  1655. Lang_C, "input0.c");
  1656. Decl *FromTU1 = getTuDecl(
  1657. R"(
  1658. struct X { // reversed order
  1659. struct { int b; };
  1660. struct { int a; };
  1661. };
  1662. )",
  1663. Lang_C, "input1.c");
  1664. auto *X0 =
  1665. FirstDeclMatcher<RecordDecl>().match(FromTU0, recordDecl(hasName("X")));
  1666. auto *X1 =
  1667. FirstDeclMatcher<RecordDecl>().match(FromTU1, recordDecl(hasName("X")));
  1668. Import(X0, Lang_C);
  1669. Import(X1, Lang_C);
  1670. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1671. // We expect one (ODR) warning during the import.
  1672. EXPECT_EQ(1u, ToTU->getASTContext().getDiagnostics().getNumWarnings());
  1673. EXPECT_EQ(2u,
  1674. DeclCounter<RecordDecl>().match(ToTU, recordDecl(hasName("X"))));
  1675. }
  1676. TEST_P(ASTImporterOptionSpecificTestBase, ImportDoesUpdateUsedFlag) {
  1677. auto Pattern = varDecl(hasName("x"));
  1678. VarDecl *Imported1;
  1679. {
  1680. Decl *FromTU = getTuDecl("extern int x;", Lang_CXX, "input0.cc");
  1681. auto *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern);
  1682. Imported1 = cast<VarDecl>(Import(FromD, Lang_CXX));
  1683. }
  1684. VarDecl *Imported2;
  1685. {
  1686. Decl *FromTU = getTuDecl("int x;", Lang_CXX, "input1.cc");
  1687. auto *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern);
  1688. Imported2 = cast<VarDecl>(Import(FromD, Lang_CXX));
  1689. }
  1690. EXPECT_EQ(Imported1->getCanonicalDecl(), Imported2->getCanonicalDecl());
  1691. EXPECT_FALSE(Imported2->isUsed(false));
  1692. {
  1693. Decl *FromTU =
  1694. getTuDecl("extern int x; int f() { return x; }", Lang_CXX, "input2.cc");
  1695. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  1696. FromTU, functionDecl(hasName("f")));
  1697. Import(FromD, Lang_CXX);
  1698. }
  1699. EXPECT_TRUE(Imported2->isUsed(false));
  1700. }
  1701. TEST_P(ASTImporterOptionSpecificTestBase, ImportDoesUpdateUsedFlag2) {
  1702. auto Pattern = varDecl(hasName("x"));
  1703. VarDecl *ExistingD;
  1704. {
  1705. Decl *ToTU = getToTuDecl("int x = 1;", Lang_CXX);
  1706. ExistingD = FirstDeclMatcher<VarDecl>().match(ToTU, Pattern);
  1707. }
  1708. EXPECT_FALSE(ExistingD->isUsed(false));
  1709. {
  1710. Decl *FromTU = getTuDecl(
  1711. "int x = 1; int f() { return x; }", Lang_CXX, "input1.cc");
  1712. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  1713. FromTU, functionDecl(hasName("f")));
  1714. Import(FromD, Lang_CXX);
  1715. }
  1716. EXPECT_TRUE(ExistingD->isUsed(false));
  1717. }
  1718. TEST_P(ASTImporterOptionSpecificTestBase, ImportDoesUpdateUsedFlag3) {
  1719. auto Pattern = varDecl(hasName("a"));
  1720. VarDecl *ExistingD;
  1721. {
  1722. Decl *ToTU = getToTuDecl(
  1723. R"(
  1724. struct A {
  1725. static const int a = 1;
  1726. };
  1727. )", Lang_CXX);
  1728. ExistingD = FirstDeclMatcher<VarDecl>().match(ToTU, Pattern);
  1729. }
  1730. EXPECT_FALSE(ExistingD->isUsed(false));
  1731. {
  1732. Decl *FromTU = getTuDecl(
  1733. R"(
  1734. struct A {
  1735. static const int a = 1;
  1736. };
  1737. const int *f() { return &A::a; } // requires storage,
  1738. // thus used flag will be set
  1739. )", Lang_CXX, "input1.cc");
  1740. auto *FromFunD = FirstDeclMatcher<FunctionDecl>().match(
  1741. FromTU, functionDecl(hasName("f")));
  1742. auto *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern);
  1743. ASSERT_TRUE(FromD->isUsed(false));
  1744. Import(FromFunD, Lang_CXX);
  1745. }
  1746. EXPECT_TRUE(ExistingD->isUsed(false));
  1747. }
  1748. TEST_P(ASTImporterOptionSpecificTestBase, ReimportWithUsedFlag) {
  1749. auto Pattern = varDecl(hasName("x"));
  1750. Decl *FromTU = getTuDecl("int x;", Lang_CXX, "input0.cc");
  1751. auto *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern);
  1752. auto *Imported1 = cast<VarDecl>(Import(FromD, Lang_CXX));
  1753. ASSERT_FALSE(Imported1->isUsed(false));
  1754. FromD->setIsUsed();
  1755. auto *Imported2 = cast<VarDecl>(Import(FromD, Lang_CXX));
  1756. EXPECT_EQ(Imported1, Imported2);
  1757. EXPECT_TRUE(Imported2->isUsed(false));
  1758. }
  1759. struct ImportFunctions : ASTImporterOptionSpecificTestBase {};
  1760. TEST_P(ImportFunctions, ImportPrototypeOfRecursiveFunction) {
  1761. Decl *FromTU = getTuDecl("void f(); void f() { f(); }", Lang_CXX);
  1762. auto Pattern = functionDecl(hasName("f"));
  1763. auto *From =
  1764. FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern); // Proto
  1765. Decl *ImportedD = Import(From, Lang_CXX);
  1766. Decl *ToTU = ImportedD->getTranslationUnitDecl();
  1767. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1768. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1769. auto *To1 = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1770. EXPECT_TRUE(ImportedD == To0);
  1771. EXPECT_FALSE(To0->doesThisDeclarationHaveABody());
  1772. EXPECT_TRUE(To1->doesThisDeclarationHaveABody());
  1773. EXPECT_EQ(To1->getPreviousDecl(), To0);
  1774. }
  1775. TEST_P(ImportFunctions, ImportDefinitionOfRecursiveFunction) {
  1776. Decl *FromTU = getTuDecl("void f(); void f() { f(); }", Lang_CXX);
  1777. auto Pattern = functionDecl(hasName("f"));
  1778. auto *From =
  1779. LastDeclMatcher<FunctionDecl>().match(FromTU, Pattern); // Def
  1780. Decl *ImportedD = Import(From, Lang_CXX);
  1781. Decl *ToTU = ImportedD->getTranslationUnitDecl();
  1782. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1783. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1784. auto *To1 = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1785. EXPECT_TRUE(ImportedD == To1);
  1786. EXPECT_FALSE(To0->doesThisDeclarationHaveABody());
  1787. EXPECT_TRUE(To1->doesThisDeclarationHaveABody());
  1788. EXPECT_EQ(To1->getPreviousDecl(), To0);
  1789. }
  1790. TEST_P(ImportFunctions, OverriddenMethodsShouldBeImported) {
  1791. auto Code =
  1792. R"(
  1793. struct B { virtual void f(); };
  1794. void B::f() {}
  1795. struct D : B { void f(); };
  1796. )";
  1797. auto Pattern =
  1798. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("D"))));
  1799. Decl *FromTU = getTuDecl(Code, Lang_CXX);
  1800. CXXMethodDecl *Proto =
  1801. FirstDeclMatcher<CXXMethodDecl>().match(FromTU, Pattern);
  1802. ASSERT_EQ(Proto->size_overridden_methods(), 1u);
  1803. CXXMethodDecl *To = cast<CXXMethodDecl>(Import(Proto, Lang_CXX));
  1804. EXPECT_EQ(To->size_overridden_methods(), 1u);
  1805. }
  1806. TEST_P(ImportFunctions, VirtualFlagShouldBePreservedWhenImportingPrototype) {
  1807. auto Code =
  1808. R"(
  1809. struct B { virtual void f(); };
  1810. void B::f() {}
  1811. )";
  1812. auto Pattern =
  1813. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("B"))));
  1814. Decl *FromTU = getTuDecl(Code, Lang_CXX);
  1815. CXXMethodDecl *Proto =
  1816. FirstDeclMatcher<CXXMethodDecl>().match(FromTU, Pattern);
  1817. CXXMethodDecl *Def = LastDeclMatcher<CXXMethodDecl>().match(FromTU, Pattern);
  1818. ASSERT_TRUE(Proto->isVirtual());
  1819. ASSERT_TRUE(Def->isVirtual());
  1820. CXXMethodDecl *To = cast<CXXMethodDecl>(Import(Proto, Lang_CXX));
  1821. EXPECT_TRUE(To->isVirtual());
  1822. }
  1823. TEST_P(ImportFunctions,
  1824. ImportDefinitionIfThereIsAnExistingDefinitionAndFwdDecl) {
  1825. Decl *ToTU = getToTuDecl(
  1826. R"(
  1827. void f() {}
  1828. void f();
  1829. )",
  1830. Lang_CXX);
  1831. ASSERT_EQ(1u,
  1832. DeclCounterWithPredicate<FunctionDecl>([](const FunctionDecl *FD) {
  1833. return FD->doesThisDeclarationHaveABody();
  1834. }).match(ToTU, functionDecl()));
  1835. Decl *FromTU = getTuDecl("void f() {}", Lang_CXX, "input0.cc");
  1836. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, functionDecl());
  1837. Import(FromD, Lang_CXX);
  1838. EXPECT_EQ(1u,
  1839. DeclCounterWithPredicate<FunctionDecl>([](const FunctionDecl *FD) {
  1840. return FD->doesThisDeclarationHaveABody();
  1841. }).match(ToTU, functionDecl()));
  1842. }
  1843. TEST_P(ImportFunctions, ImportOverriddenMethodTwice) {
  1844. auto Code =
  1845. R"(
  1846. struct B { virtual void f(); };
  1847. struct D:B { void f(); };
  1848. )";
  1849. auto BFP =
  1850. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("B"))));
  1851. auto DFP =
  1852. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("D"))));
  1853. Decl *FromTU0 = getTuDecl(Code, Lang_CXX);
  1854. auto *DF = FirstDeclMatcher<CXXMethodDecl>().match(FromTU0, DFP);
  1855. Import(DF, Lang_CXX);
  1856. Decl *FromTU1 = getTuDecl(Code, Lang_CXX, "input1.cc");
  1857. auto *BF = FirstDeclMatcher<CXXMethodDecl>().match(FromTU1, BFP);
  1858. Import(BF, Lang_CXX);
  1859. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1860. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, BFP), 1u);
  1861. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, DFP), 1u);
  1862. }
  1863. TEST_P(ImportFunctions, ImportOverriddenMethodTwiceDefinitionFirst) {
  1864. auto CodeWithoutDef =
  1865. R"(
  1866. struct B { virtual void f(); };
  1867. struct D:B { void f(); };
  1868. )";
  1869. auto CodeWithDef =
  1870. R"(
  1871. struct B { virtual void f(){}; };
  1872. struct D:B { void f(){}; };
  1873. )";
  1874. auto BFP =
  1875. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("B"))));
  1876. auto DFP =
  1877. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("D"))));
  1878. auto BFDefP = cxxMethodDecl(
  1879. hasName("f"), hasParent(cxxRecordDecl(hasName("B"))), isDefinition());
  1880. auto DFDefP = cxxMethodDecl(
  1881. hasName("f"), hasParent(cxxRecordDecl(hasName("D"))), isDefinition());
  1882. auto FDefAllP = cxxMethodDecl(hasName("f"), isDefinition());
  1883. {
  1884. Decl *FromTU = getTuDecl(CodeWithDef, Lang_CXX, "input0.cc");
  1885. auto *FromD = FirstDeclMatcher<CXXMethodDecl>().match(FromTU, DFP);
  1886. Import(FromD, Lang_CXX);
  1887. }
  1888. {
  1889. Decl *FromTU = getTuDecl(CodeWithoutDef, Lang_CXX, "input1.cc");
  1890. auto *FromB = FirstDeclMatcher<CXXMethodDecl>().match(FromTU, BFP);
  1891. Import(FromB, Lang_CXX);
  1892. }
  1893. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1894. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, BFP), 1u);
  1895. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, DFP), 1u);
  1896. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, BFDefP), 1u);
  1897. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, DFDefP), 1u);
  1898. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, FDefAllP), 2u);
  1899. }
  1900. TEST_P(ImportFunctions, ImportOverriddenMethodTwiceOutOfClassDef) {
  1901. auto Code =
  1902. R"(
  1903. struct B { virtual void f(); };
  1904. struct D:B { void f(); };
  1905. void B::f(){};
  1906. )";
  1907. auto BFP =
  1908. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("B"))));
  1909. auto BFDefP = cxxMethodDecl(
  1910. hasName("f"), hasParent(cxxRecordDecl(hasName("B"))), isDefinition());
  1911. auto DFP = cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("D"))),
  1912. unless(isDefinition()));
  1913. Decl *FromTU0 = getTuDecl(Code, Lang_CXX);
  1914. auto *D = FirstDeclMatcher<CXXMethodDecl>().match(FromTU0, DFP);
  1915. Import(D, Lang_CXX);
  1916. Decl *FromTU1 = getTuDecl(Code, Lang_CXX, "input1.cc");
  1917. auto *B = FirstDeclMatcher<CXXMethodDecl>().match(FromTU1, BFP);
  1918. Import(B, Lang_CXX);
  1919. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1920. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, BFP), 1u);
  1921. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, BFDefP), 0u);
  1922. auto *ToB = FirstDeclMatcher<CXXRecordDecl>().match(
  1923. ToTU, cxxRecordDecl(hasName("B")));
  1924. auto *ToBFInClass = FirstDeclMatcher<CXXMethodDecl>().match(ToTU, BFP);
  1925. auto *ToBFOutOfClass = FirstDeclMatcher<CXXMethodDecl>().match(
  1926. ToTU, cxxMethodDecl(hasName("f"), isDefinition()));
  1927. // The definition should be out-of-class.
  1928. EXPECT_NE(ToBFInClass, ToBFOutOfClass);
  1929. EXPECT_NE(ToBFInClass->getLexicalDeclContext(),
  1930. ToBFOutOfClass->getLexicalDeclContext());
  1931. EXPECT_EQ(ToBFOutOfClass->getDeclContext(), ToB);
  1932. EXPECT_EQ(ToBFOutOfClass->getLexicalDeclContext(), ToTU);
  1933. // Check that the redecl chain is intact.
  1934. EXPECT_EQ(ToBFOutOfClass->getPreviousDecl(), ToBFInClass);
  1935. }
  1936. TEST_P(ImportFunctions,
  1937. ImportOverriddenMethodTwiceOutOfClassDefInSeparateCode) {
  1938. auto CodeTU0 =
  1939. R"(
  1940. struct B { virtual void f(); };
  1941. struct D:B { void f(); };
  1942. )";
  1943. auto CodeTU1 =
  1944. R"(
  1945. struct B { virtual void f(); };
  1946. struct D:B { void f(); };
  1947. void B::f(){}
  1948. void D::f(){}
  1949. void foo(B &b, D &d) { b.f(); d.f(); }
  1950. )";
  1951. auto BFP =
  1952. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("B"))));
  1953. auto BFDefP = cxxMethodDecl(
  1954. hasName("f"), hasParent(cxxRecordDecl(hasName("B"))), isDefinition());
  1955. auto DFP =
  1956. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("D"))));
  1957. auto DFDefP = cxxMethodDecl(
  1958. hasName("f"), hasParent(cxxRecordDecl(hasName("D"))), isDefinition());
  1959. auto FooDef = functionDecl(hasName("foo"));
  1960. {
  1961. Decl *FromTU0 = getTuDecl(CodeTU0, Lang_CXX, "input0.cc");
  1962. auto *D = FirstDeclMatcher<CXXMethodDecl>().match(FromTU0, DFP);
  1963. Import(D, Lang_CXX);
  1964. }
  1965. {
  1966. Decl *FromTU1 = getTuDecl(CodeTU1, Lang_CXX, "input1.cc");
  1967. auto *Foo = FirstDeclMatcher<FunctionDecl>().match(FromTU1, FooDef);
  1968. Import(Foo, Lang_CXX);
  1969. }
  1970. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1971. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, BFP), 1u);
  1972. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, DFP), 1u);
  1973. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, BFDefP), 0u);
  1974. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, DFDefP), 0u);
  1975. auto *ToB = FirstDeclMatcher<CXXRecordDecl>().match(
  1976. ToTU, cxxRecordDecl(hasName("B")));
  1977. auto *ToD = FirstDeclMatcher<CXXRecordDecl>().match(
  1978. ToTU, cxxRecordDecl(hasName("D")));
  1979. auto *ToBFInClass = FirstDeclMatcher<CXXMethodDecl>().match(ToTU, BFP);
  1980. auto *ToBFOutOfClass = FirstDeclMatcher<CXXMethodDecl>().match(
  1981. ToTU, cxxMethodDecl(hasName("f"), isDefinition()));
  1982. auto *ToDFInClass = FirstDeclMatcher<CXXMethodDecl>().match(ToTU, DFP);
  1983. auto *ToDFOutOfClass = LastDeclMatcher<CXXMethodDecl>().match(
  1984. ToTU, cxxMethodDecl(hasName("f"), isDefinition()));
  1985. // The definition should be out-of-class.
  1986. EXPECT_NE(ToBFInClass, ToBFOutOfClass);
  1987. EXPECT_NE(ToBFInClass->getLexicalDeclContext(),
  1988. ToBFOutOfClass->getLexicalDeclContext());
  1989. EXPECT_EQ(ToBFOutOfClass->getDeclContext(), ToB);
  1990. EXPECT_EQ(ToBFOutOfClass->getLexicalDeclContext(), ToTU);
  1991. EXPECT_NE(ToDFInClass, ToDFOutOfClass);
  1992. EXPECT_NE(ToDFInClass->getLexicalDeclContext(),
  1993. ToDFOutOfClass->getLexicalDeclContext());
  1994. EXPECT_EQ(ToDFOutOfClass->getDeclContext(), ToD);
  1995. EXPECT_EQ(ToDFOutOfClass->getLexicalDeclContext(), ToTU);
  1996. // Check that the redecl chain is intact.
  1997. EXPECT_EQ(ToBFOutOfClass->getPreviousDecl(), ToBFInClass);
  1998. EXPECT_EQ(ToDFOutOfClass->getPreviousDecl(), ToDFInClass);
  1999. }
  2000. TEST_P(ASTImporterOptionSpecificTestBase, ImportVariableChainInC) {
  2001. std::string Code = "static int v; static int v = 0;";
  2002. auto Pattern = varDecl(hasName("v"));
  2003. TranslationUnitDecl *FromTu = getTuDecl(Code, Lang_C, "input0.c");
  2004. auto *From0 = FirstDeclMatcher<VarDecl>().match(FromTu, Pattern);
  2005. auto *From1 = LastDeclMatcher<VarDecl>().match(FromTu, Pattern);
  2006. auto *To0 = Import(From0, Lang_C);
  2007. auto *To1 = Import(From1, Lang_C);
  2008. EXPECT_TRUE(To0);
  2009. ASSERT_TRUE(To1);
  2010. EXPECT_NE(To0, To1);
  2011. EXPECT_EQ(To1->getPreviousDecl(), To0);
  2012. }
  2013. TEST_P(ImportFunctions, ImportFromDifferentScopedAnonNamespace) {
  2014. TranslationUnitDecl *FromTu = getTuDecl(
  2015. "namespace NS0 { namespace { void f(); } }"
  2016. "namespace NS1 { namespace { void f(); } }",
  2017. Lang_CXX, "input0.cc");
  2018. auto Pattern = functionDecl(hasName("f"));
  2019. auto *FromF0 = FirstDeclMatcher<FunctionDecl>().match(FromTu, Pattern);
  2020. auto *FromF1 = LastDeclMatcher<FunctionDecl>().match(FromTu, Pattern);
  2021. auto *ToF0 = Import(FromF0, Lang_CXX);
  2022. auto *ToF1 = Import(FromF1, Lang_CXX);
  2023. EXPECT_TRUE(ToF0);
  2024. ASSERT_TRUE(ToF1);
  2025. EXPECT_NE(ToF0, ToF1);
  2026. EXPECT_FALSE(ToF1->getPreviousDecl());
  2027. }
  2028. TEST_P(ImportFunctions, ImportFunctionFromUnnamedNamespace) {
  2029. {
  2030. Decl *FromTU = getTuDecl("namespace { void f() {} } void g0() { f(); }",
  2031. Lang_CXX, "input0.cc");
  2032. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  2033. FromTU, functionDecl(hasName("g0")));
  2034. Import(FromD, Lang_CXX);
  2035. }
  2036. {
  2037. Decl *FromTU =
  2038. getTuDecl("namespace { void f() { int a; } } void g1() { f(); }",
  2039. Lang_CXX, "input1.cc");
  2040. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  2041. FromTU, functionDecl(hasName("g1")));
  2042. Import(FromD, Lang_CXX);
  2043. }
  2044. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2045. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, functionDecl(hasName("f"))),
  2046. 2u);
  2047. }
  2048. TEST_P(ImportFunctions, ImportImplicitFunctionsInLambda) {
  2049. Decl *FromTU = getTuDecl(
  2050. R"(
  2051. void foo() {
  2052. (void)[]() { ; };
  2053. }
  2054. )",
  2055. Lang_CXX11);
  2056. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  2057. FromTU, functionDecl(hasName("foo")));
  2058. auto *ToD = Import(FromD, Lang_CXX);
  2059. EXPECT_TRUE(ToD);
  2060. CXXRecordDecl *LambdaRec =
  2061. cast<LambdaExpr>(cast<CStyleCastExpr>(
  2062. *cast<CompoundStmt>(ToD->getBody())->body_begin())
  2063. ->getSubExpr())
  2064. ->getLambdaClass();
  2065. EXPECT_TRUE(LambdaRec->getDestructor());
  2066. }
  2067. TEST_P(ImportFunctions,
  2068. CallExprOfMemberFunctionTemplateWithExplicitTemplateArgs) {
  2069. Decl *FromTU = getTuDecl(
  2070. R"(
  2071. struct X {
  2072. template <typename T>
  2073. void foo(){}
  2074. };
  2075. void f() {
  2076. X x;
  2077. x.foo<int>();
  2078. }
  2079. )",
  2080. Lang_CXX);
  2081. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  2082. FromTU, functionDecl(hasName("f")));
  2083. auto *ToD = Import(FromD, Lang_CXX);
  2084. EXPECT_TRUE(ToD);
  2085. EXPECT_TRUE(MatchVerifier<FunctionDecl>().match(
  2086. ToD, functionDecl(hasName("f"), hasDescendant(declRefExpr()))));
  2087. }
  2088. TEST_P(ImportFunctions,
  2089. DependentCallExprOfMemberFunctionTemplateWithExplicitTemplateArgs) {
  2090. Decl *FromTU = getTuDecl(
  2091. R"(
  2092. struct X {
  2093. template <typename T>
  2094. void foo(){}
  2095. };
  2096. template <typename T>
  2097. void f() {
  2098. X x;
  2099. x.foo<T>();
  2100. }
  2101. void g() {
  2102. f<int>();
  2103. }
  2104. )",
  2105. Lang_CXX);
  2106. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  2107. FromTU, functionDecl(hasName("g")));
  2108. auto *ToD = Import(FromD, Lang_CXX);
  2109. EXPECT_TRUE(ToD);
  2110. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2111. EXPECT_TRUE(MatchVerifier<TranslationUnitDecl>().match(
  2112. ToTU, translationUnitDecl(hasDescendant(
  2113. functionDecl(hasName("f"), hasDescendant(declRefExpr()))))));
  2114. }
  2115. struct ImportFunctionTemplates : ASTImporterOptionSpecificTestBase {};
  2116. TEST_P(ImportFunctionTemplates, ImportFunctionTemplateInRecordDeclTwice) {
  2117. auto Code =
  2118. R"(
  2119. class X {
  2120. template <class T>
  2121. void f(T t);
  2122. };
  2123. )";
  2124. Decl *FromTU1 = getTuDecl(Code, Lang_CXX, "input1.cc");
  2125. auto *FromD1 = FirstDeclMatcher<FunctionTemplateDecl>().match(
  2126. FromTU1, functionTemplateDecl(hasName("f")));
  2127. auto *ToD1 = Import(FromD1, Lang_CXX);
  2128. Decl *FromTU2 = getTuDecl(Code, Lang_CXX, "input2.cc");
  2129. auto *FromD2 = FirstDeclMatcher<FunctionTemplateDecl>().match(
  2130. FromTU2, functionTemplateDecl(hasName("f")));
  2131. auto *ToD2 = Import(FromD2, Lang_CXX);
  2132. EXPECT_EQ(ToD1, ToD2);
  2133. }
  2134. TEST_P(ImportFunctionTemplates,
  2135. ImportFunctionTemplateWithDefInRecordDeclTwice) {
  2136. auto Code =
  2137. R"(
  2138. class X {
  2139. template <class T>
  2140. void f(T t);
  2141. };
  2142. template <class T>
  2143. void X::f(T t) {};
  2144. )";
  2145. Decl *FromTU1 = getTuDecl(Code, Lang_CXX, "input1.cc");
  2146. auto *FromD1 = FirstDeclMatcher<FunctionTemplateDecl>().match(
  2147. FromTU1, functionTemplateDecl(hasName("f")));
  2148. auto *ToD1 = Import(FromD1, Lang_CXX);
  2149. Decl *FromTU2 = getTuDecl(Code, Lang_CXX, "input2.cc");
  2150. auto *FromD2 = FirstDeclMatcher<FunctionTemplateDecl>().match(
  2151. FromTU2, functionTemplateDecl(hasName("f")));
  2152. auto *ToD2 = Import(FromD2, Lang_CXX);
  2153. EXPECT_EQ(ToD1, ToD2);
  2154. }
  2155. struct ImportFriendFunctions : ImportFunctions {};
  2156. TEST_P(ImportFriendFunctions, ImportFriendFunctionRedeclChainProto) {
  2157. auto Pattern = functionDecl(hasName("f"));
  2158. Decl *FromTU = getTuDecl("struct X { friend void f(); };"
  2159. "void f();",
  2160. Lang_CXX,
  2161. "input0.cc");
  2162. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2163. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2164. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2165. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2166. EXPECT_FALSE(ImportedD->doesThisDeclarationHaveABody());
  2167. auto *ToFD = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2168. EXPECT_FALSE(ToFD->doesThisDeclarationHaveABody());
  2169. EXPECT_EQ(ToFD->getPreviousDecl(), ImportedD);
  2170. }
  2171. TEST_P(ImportFriendFunctions,
  2172. ImportFriendFunctionRedeclChainProto_OutOfClassProtoFirst) {
  2173. auto Pattern = functionDecl(hasName("f"));
  2174. Decl *FromTU = getTuDecl("void f();"
  2175. "struct X { friend void f(); };",
  2176. Lang_CXX, "input0.cc");
  2177. auto FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2178. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2179. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2180. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2181. EXPECT_FALSE(ImportedD->doesThisDeclarationHaveABody());
  2182. auto *ToFD = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2183. EXPECT_FALSE(ToFD->doesThisDeclarationHaveABody());
  2184. EXPECT_EQ(ToFD->getPreviousDecl(), ImportedD);
  2185. }
  2186. TEST_P(ImportFriendFunctions, ImportFriendFunctionRedeclChainDef) {
  2187. auto Pattern = functionDecl(hasName("f"));
  2188. Decl *FromTU = getTuDecl("struct X { friend void f(){} };"
  2189. "void f();",
  2190. Lang_CXX,
  2191. "input0.cc");
  2192. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2193. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2194. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2195. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2196. EXPECT_TRUE(ImportedD->doesThisDeclarationHaveABody());
  2197. auto *ToFD = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2198. EXPECT_FALSE(ToFD->doesThisDeclarationHaveABody());
  2199. EXPECT_EQ(ToFD->getPreviousDecl(), ImportedD);
  2200. }
  2201. TEST_P(ImportFriendFunctions,
  2202. ImportFriendFunctionRedeclChainDef_OutOfClassDef) {
  2203. auto Pattern = functionDecl(hasName("f"));
  2204. Decl *FromTU = getTuDecl("struct X { friend void f(); };"
  2205. "void f(){}",
  2206. Lang_CXX, "input0.cc");
  2207. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2208. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2209. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2210. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2211. EXPECT_FALSE(ImportedD->doesThisDeclarationHaveABody());
  2212. auto *ToFD = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2213. EXPECT_TRUE(ToFD->doesThisDeclarationHaveABody());
  2214. EXPECT_EQ(ToFD->getPreviousDecl(), ImportedD);
  2215. }
  2216. TEST_P(ImportFriendFunctions, ImportFriendFunctionRedeclChainDefWithClass) {
  2217. auto Pattern = functionDecl(hasName("f"));
  2218. Decl *FromTU = getTuDecl(
  2219. R"(
  2220. class X;
  2221. void f(X *x){}
  2222. class X{
  2223. friend void f(X *x);
  2224. };
  2225. )",
  2226. Lang_CXX, "input0.cc");
  2227. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2228. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2229. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2230. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2231. EXPECT_TRUE(ImportedD->doesThisDeclarationHaveABody());
  2232. auto *InClassFD = cast<FunctionDecl>(FirstDeclMatcher<FriendDecl>()
  2233. .match(ToTU, friendDecl())
  2234. ->getFriendDecl());
  2235. EXPECT_FALSE(InClassFD->doesThisDeclarationHaveABody());
  2236. EXPECT_EQ(InClassFD->getPreviousDecl(), ImportedD);
  2237. // The parameters must refer the same type
  2238. EXPECT_EQ((*InClassFD->param_begin())->getOriginalType(),
  2239. (*ImportedD->param_begin())->getOriginalType());
  2240. }
  2241. TEST_P(ImportFriendFunctions,
  2242. ImportFriendFunctionRedeclChainDefWithClass_ImportTheProto) {
  2243. auto Pattern = functionDecl(hasName("f"));
  2244. Decl *FromTU = getTuDecl(
  2245. R"(
  2246. class X;
  2247. void f(X *x){}
  2248. class X{
  2249. friend void f(X *x);
  2250. };
  2251. )",
  2252. Lang_CXX, "input0.cc");
  2253. auto *FromD = LastDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2254. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2255. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2256. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2257. EXPECT_FALSE(ImportedD->doesThisDeclarationHaveABody());
  2258. auto *OutOfClassFD = FirstDeclMatcher<FunctionDecl>().match(
  2259. ToTU, functionDecl(unless(hasParent(friendDecl()))));
  2260. EXPECT_TRUE(OutOfClassFD->doesThisDeclarationHaveABody());
  2261. EXPECT_EQ(ImportedD->getPreviousDecl(), OutOfClassFD);
  2262. // The parameters must refer the same type
  2263. EXPECT_EQ((*OutOfClassFD->param_begin())->getOriginalType(),
  2264. (*ImportedD->param_begin())->getOriginalType());
  2265. }
  2266. TEST_P(ImportFriendFunctions, ImportFriendFunctionFromMultipleTU) {
  2267. auto Pattern = functionDecl(hasName("f"));
  2268. FunctionDecl *ImportedD;
  2269. {
  2270. Decl *FromTU =
  2271. getTuDecl("struct X { friend void f(){} };", Lang_CXX, "input0.cc");
  2272. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2273. ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2274. }
  2275. FunctionDecl *ImportedD1;
  2276. {
  2277. Decl *FromTU = getTuDecl("void f();", Lang_CXX, "input1.cc");
  2278. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2279. ImportedD1 = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2280. }
  2281. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2282. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2283. EXPECT_TRUE(ImportedD->doesThisDeclarationHaveABody());
  2284. EXPECT_FALSE(ImportedD1->doesThisDeclarationHaveABody());
  2285. EXPECT_EQ(ImportedD1->getPreviousDecl(), ImportedD);
  2286. }
  2287. TEST_P(ImportFriendFunctions, Lookup) {
  2288. auto FunctionPattern = functionDecl(hasName("f"));
  2289. auto ClassPattern = cxxRecordDecl(hasName("X"));
  2290. TranslationUnitDecl *FromTU =
  2291. getTuDecl("struct X { friend void f(); };", Lang_CXX, "input0.cc");
  2292. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, FunctionPattern);
  2293. ASSERT_TRUE(FromD->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2294. ASSERT_FALSE(FromD->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2295. {
  2296. auto FromName = FromD->getDeclName();
  2297. auto *Class = FirstDeclMatcher<CXXRecordDecl>().match(FromTU, ClassPattern);
  2298. auto LookupRes = Class->noload_lookup(FromName);
  2299. ASSERT_EQ(LookupRes.size(), 0u);
  2300. LookupRes = FromTU->noload_lookup(FromName);
  2301. ASSERT_EQ(LookupRes.size(), 1u);
  2302. }
  2303. auto *ToD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2304. auto ToName = ToD->getDeclName();
  2305. TranslationUnitDecl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2306. auto *Class = FirstDeclMatcher<CXXRecordDecl>().match(ToTU, ClassPattern);
  2307. auto LookupRes = Class->noload_lookup(ToName);
  2308. EXPECT_EQ(LookupRes.size(), 0u);
  2309. LookupRes = ToTU->noload_lookup(ToName);
  2310. EXPECT_EQ(LookupRes.size(), 1u);
  2311. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, FunctionPattern), 1u);
  2312. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, FunctionPattern);
  2313. EXPECT_TRUE(To0->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2314. EXPECT_FALSE(To0->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2315. }
  2316. TEST_P(ImportFriendFunctions, DISABLED_LookupWithProtoAfter) {
  2317. auto FunctionPattern = functionDecl(hasName("f"));
  2318. auto ClassPattern = cxxRecordDecl(hasName("X"));
  2319. TranslationUnitDecl *FromTU = getTuDecl(
  2320. "struct X { friend void f(); };"
  2321. // This proto decl makes f available to normal
  2322. // lookup, otherwise it is hidden.
  2323. // Normal C++ lookup (implemented in
  2324. // `clang::Sema::CppLookupName()` and in `LookupDirect()`)
  2325. // returns the found `NamedDecl` only if the set IDNS is matched
  2326. "void f();",
  2327. Lang_CXX, "input0.cc");
  2328. auto *FromFriend =
  2329. FirstDeclMatcher<FunctionDecl>().match(FromTU, FunctionPattern);
  2330. auto *FromNormal =
  2331. LastDeclMatcher<FunctionDecl>().match(FromTU, FunctionPattern);
  2332. ASSERT_TRUE(FromFriend->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2333. ASSERT_FALSE(FromFriend->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2334. ASSERT_FALSE(FromNormal->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2335. ASSERT_TRUE(FromNormal->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2336. auto FromName = FromFriend->getDeclName();
  2337. auto *FromClass =
  2338. FirstDeclMatcher<CXXRecordDecl>().match(FromTU, ClassPattern);
  2339. auto LookupRes = FromClass->noload_lookup(FromName);
  2340. ASSERT_EQ(LookupRes.size(), 0u);
  2341. LookupRes = FromTU->noload_lookup(FromName);
  2342. ASSERT_EQ(LookupRes.size(), 1u);
  2343. auto *ToFriend = cast<FunctionDecl>(Import(FromFriend, Lang_CXX));
  2344. auto ToName = ToFriend->getDeclName();
  2345. TranslationUnitDecl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2346. auto *ToClass = FirstDeclMatcher<CXXRecordDecl>().match(ToTU, ClassPattern);
  2347. LookupRes = ToClass->noload_lookup(ToName);
  2348. EXPECT_EQ(LookupRes.size(), 0u);
  2349. LookupRes = ToTU->noload_lookup(ToName);
  2350. // Test is disabled because this result is 2.
  2351. EXPECT_EQ(LookupRes.size(), 1u);
  2352. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, FunctionPattern), 2u);
  2353. ToFriend = FirstDeclMatcher<FunctionDecl>().match(ToTU, FunctionPattern);
  2354. auto *ToNormal = LastDeclMatcher<FunctionDecl>().match(ToTU, FunctionPattern);
  2355. EXPECT_TRUE(ToFriend->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2356. EXPECT_FALSE(ToFriend->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2357. EXPECT_FALSE(ToNormal->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2358. EXPECT_TRUE(ToNormal->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2359. }
  2360. TEST_P(ImportFriendFunctions, LookupWithProtoBefore) {
  2361. auto FunctionPattern = functionDecl(hasName("f"));
  2362. auto ClassPattern = cxxRecordDecl(hasName("X"));
  2363. TranslationUnitDecl *FromTU = getTuDecl(
  2364. "void f();"
  2365. "struct X { friend void f(); };",
  2366. Lang_CXX, "input0.cc");
  2367. auto *FromNormal =
  2368. FirstDeclMatcher<FunctionDecl>().match(FromTU, FunctionPattern);
  2369. auto *FromFriend =
  2370. LastDeclMatcher<FunctionDecl>().match(FromTU, FunctionPattern);
  2371. ASSERT_FALSE(FromNormal->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2372. ASSERT_TRUE(FromNormal->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2373. ASSERT_TRUE(FromFriend->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2374. ASSERT_TRUE(FromFriend->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2375. auto FromName = FromNormal->getDeclName();
  2376. auto *FromClass =
  2377. FirstDeclMatcher<CXXRecordDecl>().match(FromTU, ClassPattern);
  2378. auto LookupRes = FromClass->noload_lookup(FromName);
  2379. ASSERT_EQ(LookupRes.size(), 0u);
  2380. LookupRes = FromTU->noload_lookup(FromName);
  2381. ASSERT_EQ(LookupRes.size(), 1u);
  2382. auto *ToNormal = cast<FunctionDecl>(Import(FromNormal, Lang_CXX));
  2383. auto ToName = ToNormal->getDeclName();
  2384. TranslationUnitDecl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2385. auto *ToClass = FirstDeclMatcher<CXXRecordDecl>().match(ToTU, ClassPattern);
  2386. LookupRes = ToClass->noload_lookup(ToName);
  2387. EXPECT_EQ(LookupRes.size(), 0u);
  2388. LookupRes = ToTU->noload_lookup(ToName);
  2389. EXPECT_EQ(LookupRes.size(), 1u);
  2390. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, FunctionPattern), 2u);
  2391. ToNormal = FirstDeclMatcher<FunctionDecl>().match(ToTU, FunctionPattern);
  2392. auto *ToFriend = LastDeclMatcher<FunctionDecl>().match(ToTU, FunctionPattern);
  2393. EXPECT_FALSE(ToNormal->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2394. EXPECT_TRUE(ToNormal->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2395. EXPECT_TRUE(ToFriend->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2396. EXPECT_TRUE(ToFriend->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2397. }
  2398. TEST_P(ImportFriendFunctions, ImportFriendChangesLookup) {
  2399. auto Pattern = functionDecl(hasName("f"));
  2400. TranslationUnitDecl *FromNormalTU =
  2401. getTuDecl("void f();", Lang_CXX, "input0.cc");
  2402. auto *FromNormalF =
  2403. FirstDeclMatcher<FunctionDecl>().match(FromNormalTU, Pattern);
  2404. TranslationUnitDecl *FromFriendTU =
  2405. getTuDecl("class X { friend void f(); };", Lang_CXX, "input1.cc");
  2406. auto *FromFriendF =
  2407. FirstDeclMatcher<FunctionDecl>().match(FromFriendTU, Pattern);
  2408. auto FromNormalName = FromNormalF->getDeclName();
  2409. auto FromFriendName = FromFriendF->getDeclName();
  2410. ASSERT_TRUE(FromNormalF->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2411. ASSERT_FALSE(FromNormalF->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2412. ASSERT_FALSE(FromFriendF->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2413. ASSERT_TRUE(FromFriendF->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2414. auto LookupRes = FromNormalTU->noload_lookup(FromNormalName);
  2415. ASSERT_EQ(LookupRes.size(), 1u);
  2416. LookupRes = FromFriendTU->noload_lookup(FromFriendName);
  2417. ASSERT_EQ(LookupRes.size(), 1u);
  2418. auto *ToNormalF = cast<FunctionDecl>(Import(FromNormalF, Lang_CXX));
  2419. TranslationUnitDecl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2420. auto ToName = ToNormalF->getDeclName();
  2421. EXPECT_TRUE(ToNormalF->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2422. EXPECT_FALSE(ToNormalF->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2423. LookupRes = ToTU->noload_lookup(ToName);
  2424. EXPECT_EQ(LookupRes.size(), 1u);
  2425. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 1u);
  2426. auto *ToFriendF = cast<FunctionDecl>(Import(FromFriendF, Lang_CXX));
  2427. LookupRes = ToTU->noload_lookup(ToName);
  2428. EXPECT_EQ(LookupRes.size(), 1u);
  2429. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2430. EXPECT_TRUE(ToNormalF->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2431. EXPECT_FALSE(ToNormalF->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2432. EXPECT_TRUE(ToFriendF->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2433. EXPECT_TRUE(ToFriendF->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2434. }
  2435. TEST_P(ImportFriendFunctions, ImportFriendList) {
  2436. TranslationUnitDecl *FromTU = getTuDecl(
  2437. "struct X { friend void f(); };"
  2438. "void f();",
  2439. Lang_CXX, "input0.cc");
  2440. auto *FromFriendF = FirstDeclMatcher<FunctionDecl>().match(
  2441. FromTU, functionDecl(hasName("f")));
  2442. auto *FromClass = FirstDeclMatcher<CXXRecordDecl>().match(
  2443. FromTU, cxxRecordDecl(hasName("X")));
  2444. auto *FromFriend = FirstDeclMatcher<FriendDecl>().match(FromTU, friendDecl());
  2445. auto FromFriends = FromClass->friends();
  2446. unsigned int FrN = 0;
  2447. for (auto Fr : FromFriends) {
  2448. ASSERT_EQ(Fr, FromFriend);
  2449. ++FrN;
  2450. }
  2451. ASSERT_EQ(FrN, 1u);
  2452. Import(FromFriendF, Lang_CXX);
  2453. TranslationUnitDecl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2454. auto *ToClass = FirstDeclMatcher<CXXRecordDecl>().match(
  2455. ToTU, cxxRecordDecl(hasName("X")));
  2456. auto *ToFriend = FirstDeclMatcher<FriendDecl>().match(ToTU, friendDecl());
  2457. auto ToFriends = ToClass->friends();
  2458. FrN = 0;
  2459. for (auto Fr : ToFriends) {
  2460. EXPECT_EQ(Fr, ToFriend);
  2461. ++FrN;
  2462. }
  2463. EXPECT_EQ(FrN, 1u);
  2464. }
  2465. AST_MATCHER_P(TagDecl, hasTypedefForAnonDecl, Matcher<TypedefNameDecl>,
  2466. InnerMatcher) {
  2467. if (auto *Typedef = Node.getTypedefNameForAnonDecl())
  2468. return InnerMatcher.matches(*Typedef, Finder, Builder);
  2469. return false;
  2470. }
  2471. TEST_P(ImportDecl, ImportEnumSequential) {
  2472. CodeFiles Samples{{"main.c",
  2473. {"void foo();"
  2474. "void moo();"
  2475. "int main() { foo(); moo(); }",
  2476. Lang_C}},
  2477. {"foo.c",
  2478. {"typedef enum { THING_VALUE } thing_t;"
  2479. "void conflict(thing_t type);"
  2480. "void foo() { (void)THING_VALUE; }"
  2481. "void conflict(thing_t type) {}",
  2482. Lang_C}},
  2483. {"moo.c",
  2484. {"typedef enum { THING_VALUE } thing_t;"
  2485. "void conflict(thing_t type);"
  2486. "void moo() { conflict(THING_VALUE); }",
  2487. Lang_C}}};
  2488. auto VerificationMatcher =
  2489. enumDecl(has(enumConstantDecl(hasName("THING_VALUE"))),
  2490. hasTypedefForAnonDecl(hasName("thing_t")));
  2491. ImportAction ImportFoo{"foo.c", "main.c", functionDecl(hasName("foo"))},
  2492. ImportMoo{"moo.c", "main.c", functionDecl(hasName("moo"))};
  2493. testImportSequence(
  2494. Samples, {ImportFoo, ImportMoo}, // "foo", them "moo".
  2495. // Just check that there is only one enum decl in the result AST.
  2496. "main.c", enumDecl(), VerificationMatcher);
  2497. // For different import order, result should be the same.
  2498. testImportSequence(
  2499. Samples, {ImportMoo, ImportFoo}, // "moo", them "foo".
  2500. // Check that there is only one enum decl in the result AST.
  2501. "main.c", enumDecl(), VerificationMatcher);
  2502. }
  2503. TEST_P(ImportDecl, ImportFieldOrder) {
  2504. MatchVerifier<Decl> Verifier;
  2505. testImport("struct declToImport {"
  2506. " int b = a + 2;"
  2507. " int a = 5;"
  2508. "};",
  2509. Lang_CXX11, "", Lang_CXX11, Verifier,
  2510. recordDecl(hasFieldOrder({"b", "a"})));
  2511. }
  2512. const internal::VariadicDynCastAllOfMatcher<Expr, DependentScopeDeclRefExpr>
  2513. dependentScopeDeclRefExpr;
  2514. TEST_P(ImportExpr, DependentScopeDeclRefExpr) {
  2515. MatchVerifier<Decl> Verifier;
  2516. testImport("template <typename T> struct S { static T foo; };"
  2517. "template <typename T> void declToImport() {"
  2518. " (void) S<T>::foo;"
  2519. "}"
  2520. "void instantiate() { declToImport<int>(); }"
  2521. "template <typename T> T S<T>::foo;",
  2522. Lang_CXX11, "", Lang_CXX11, Verifier,
  2523. functionTemplateDecl(has(functionDecl(has(compoundStmt(
  2524. has(cStyleCastExpr(has(dependentScopeDeclRefExpr())))))))));
  2525. testImport("template <typename T> struct S {"
  2526. "template<typename S> static void foo(){};"
  2527. "};"
  2528. "template <typename T> void declToImport() {"
  2529. " S<T>::template foo<T>();"
  2530. "}"
  2531. "void instantiate() { declToImport<int>(); }",
  2532. Lang_CXX11, "", Lang_CXX11, Verifier,
  2533. functionTemplateDecl(has(functionDecl(has(compoundStmt(
  2534. has(callExpr(has(dependentScopeDeclRefExpr())))))))));
  2535. }
  2536. const internal::VariadicDynCastAllOfMatcher<Type, DependentNameType>
  2537. dependentNameType;
  2538. TEST_P(ImportExpr, DependentNameType) {
  2539. MatchVerifier<Decl> Verifier;
  2540. testImport("template <typename T> struct declToImport {"
  2541. " typedef typename T::type dependent_name;"
  2542. "};",
  2543. Lang_CXX11, "", Lang_CXX11, Verifier,
  2544. classTemplateDecl(has(
  2545. cxxRecordDecl(has(typedefDecl(has(dependentNameType())))))));
  2546. }
  2547. TEST_P(ImportExpr, UnresolvedMemberExpr) {
  2548. MatchVerifier<Decl> Verifier;
  2549. testImport("struct S { template <typename T> void mem(); };"
  2550. "template <typename U> void declToImport() {"
  2551. " S s;"
  2552. " s.mem<U>();"
  2553. "}"
  2554. "void instantiate() { declToImport<int>(); }",
  2555. Lang_CXX11, "", Lang_CXX11, Verifier,
  2556. functionTemplateDecl(has(functionDecl(has(
  2557. compoundStmt(has(callExpr(has(unresolvedMemberExpr())))))))));
  2558. }
  2559. class ImportImplicitMethods : public ASTImporterOptionSpecificTestBase {
  2560. public:
  2561. static constexpr auto DefaultCode = R"(
  2562. struct A { int x; };
  2563. void f() {
  2564. A a;
  2565. A a1(a);
  2566. A a2(A{});
  2567. a = a1;
  2568. a = A{};
  2569. a.~A();
  2570. })";
  2571. template <typename MatcherType>
  2572. void testImportOf(
  2573. const MatcherType &MethodMatcher, const char *Code = DefaultCode) {
  2574. test(MethodMatcher, Code, /*ExpectedCount=*/1u);
  2575. }
  2576. template <typename MatcherType>
  2577. void testNoImportOf(
  2578. const MatcherType &MethodMatcher, const char *Code = DefaultCode) {
  2579. test(MethodMatcher, Code, /*ExpectedCount=*/0u);
  2580. }
  2581. private:
  2582. template <typename MatcherType>
  2583. void test(const MatcherType &MethodMatcher,
  2584. const char *Code, unsigned int ExpectedCount) {
  2585. auto ClassMatcher = cxxRecordDecl(unless(isImplicit()));
  2586. Decl *ToTU = getToTuDecl(Code, Lang_CXX11);
  2587. auto *ToClass = FirstDeclMatcher<CXXRecordDecl>().match(
  2588. ToTU, ClassMatcher);
  2589. ASSERT_EQ(DeclCounter<CXXMethodDecl>().match(ToClass, MethodMatcher), 1u);
  2590. {
  2591. CXXMethodDecl *Method =
  2592. FirstDeclMatcher<CXXMethodDecl>().match(ToClass, MethodMatcher);
  2593. ToClass->removeDecl(Method);
  2594. SharedStatePtr->getLookupTable()->remove(Method);
  2595. }
  2596. ASSERT_EQ(DeclCounter<CXXMethodDecl>().match(ToClass, MethodMatcher), 0u);
  2597. Decl *ImportedClass = nullptr;
  2598. {
  2599. Decl *FromTU = getTuDecl(Code, Lang_CXX11, "input1.cc");
  2600. auto *FromClass = FirstDeclMatcher<CXXRecordDecl>().match(
  2601. FromTU, ClassMatcher);
  2602. ImportedClass = Import(FromClass, Lang_CXX11);
  2603. }
  2604. EXPECT_EQ(ToClass, ImportedClass);
  2605. EXPECT_EQ(DeclCounter<CXXMethodDecl>().match(ToClass, MethodMatcher),
  2606. ExpectedCount);
  2607. }
  2608. };
  2609. TEST_P(ImportImplicitMethods, DefaultConstructor) {
  2610. testImportOf(cxxConstructorDecl(isDefaultConstructor()));
  2611. }
  2612. TEST_P(ImportImplicitMethods, CopyConstructor) {
  2613. testImportOf(cxxConstructorDecl(isCopyConstructor()));
  2614. }
  2615. TEST_P(ImportImplicitMethods, MoveConstructor) {
  2616. testImportOf(cxxConstructorDecl(isMoveConstructor()));
  2617. }
  2618. TEST_P(ImportImplicitMethods, Destructor) {
  2619. testImportOf(cxxDestructorDecl());
  2620. }
  2621. TEST_P(ImportImplicitMethods, CopyAssignment) {
  2622. testImportOf(cxxMethodDecl(isCopyAssignmentOperator()));
  2623. }
  2624. TEST_P(ImportImplicitMethods, MoveAssignment) {
  2625. testImportOf(cxxMethodDecl(isMoveAssignmentOperator()));
  2626. }
  2627. TEST_P(ImportImplicitMethods, DoNotImportUserProvided) {
  2628. auto Code = R"(
  2629. struct A { A() { int x; } };
  2630. )";
  2631. testNoImportOf(cxxConstructorDecl(isDefaultConstructor()), Code);
  2632. }
  2633. TEST_P(ImportImplicitMethods, DoNotImportDefault) {
  2634. auto Code = R"(
  2635. struct A { A() = default; };
  2636. )";
  2637. testNoImportOf(cxxConstructorDecl(isDefaultConstructor()), Code);
  2638. }
  2639. TEST_P(ImportImplicitMethods, DoNotImportDeleted) {
  2640. auto Code = R"(
  2641. struct A { A() = delete; };
  2642. )";
  2643. testNoImportOf(cxxConstructorDecl(isDefaultConstructor()), Code);
  2644. }
  2645. TEST_P(ImportImplicitMethods, DoNotImportOtherMethod) {
  2646. auto Code = R"(
  2647. struct A { void f() { } };
  2648. )";
  2649. testNoImportOf(cxxMethodDecl(hasName("f")), Code);
  2650. }
  2651. TEST_P(ASTImporterOptionSpecificTestBase, ImportOfEquivalentRecord) {
  2652. Decl *ToR1;
  2653. {
  2654. Decl *FromTU = getTuDecl(
  2655. "struct A { };", Lang_CXX, "input0.cc");
  2656. auto *FromR = FirstDeclMatcher<CXXRecordDecl>().match(
  2657. FromTU, cxxRecordDecl(hasName("A")));
  2658. ToR1 = Import(FromR, Lang_CXX);
  2659. }
  2660. Decl *ToR2;
  2661. {
  2662. Decl *FromTU = getTuDecl(
  2663. "struct A { };", Lang_CXX, "input1.cc");
  2664. auto *FromR = FirstDeclMatcher<CXXRecordDecl>().match(
  2665. FromTU, cxxRecordDecl(hasName("A")));
  2666. ToR2 = Import(FromR, Lang_CXX);
  2667. }
  2668. EXPECT_EQ(ToR1, ToR2);
  2669. }
  2670. TEST_P(ASTImporterOptionSpecificTestBase, ImportOfNonEquivalentRecord) {
  2671. Decl *ToR1;
  2672. {
  2673. Decl *FromTU = getTuDecl(
  2674. "struct A { int x; };", Lang_CXX, "input0.cc");
  2675. auto *FromR = FirstDeclMatcher<CXXRecordDecl>().match(
  2676. FromTU, cxxRecordDecl(hasName("A")));
  2677. ToR1 = Import(FromR, Lang_CXX);
  2678. }
  2679. Decl *ToR2;
  2680. {
  2681. Decl *FromTU = getTuDecl(
  2682. "struct A { unsigned x; };", Lang_CXX, "input1.cc");
  2683. auto *FromR = FirstDeclMatcher<CXXRecordDecl>().match(
  2684. FromTU, cxxRecordDecl(hasName("A")));
  2685. ToR2 = Import(FromR, Lang_CXX);
  2686. }
  2687. EXPECT_NE(ToR1, ToR2);
  2688. }
  2689. TEST_P(ASTImporterOptionSpecificTestBase, ImportOfEquivalentField) {
  2690. Decl *ToF1;
  2691. {
  2692. Decl *FromTU = getTuDecl(
  2693. "struct A { int x; };", Lang_CXX, "input0.cc");
  2694. auto *FromF = FirstDeclMatcher<FieldDecl>().match(
  2695. FromTU, fieldDecl(hasName("x")));
  2696. ToF1 = Import(FromF, Lang_CXX);
  2697. }
  2698. Decl *ToF2;
  2699. {
  2700. Decl *FromTU = getTuDecl(
  2701. "struct A { int x; };", Lang_CXX, "input1.cc");
  2702. auto *FromF = FirstDeclMatcher<FieldDecl>().match(
  2703. FromTU, fieldDecl(hasName("x")));
  2704. ToF2 = Import(FromF, Lang_CXX);
  2705. }
  2706. EXPECT_EQ(ToF1, ToF2);
  2707. }
  2708. TEST_P(ASTImporterOptionSpecificTestBase, ImportOfNonEquivalentField) {
  2709. Decl *ToF1;
  2710. {
  2711. Decl *FromTU = getTuDecl(
  2712. "struct A { int x; };", Lang_CXX, "input0.cc");
  2713. auto *FromF = FirstDeclMatcher<FieldDecl>().match(
  2714. FromTU, fieldDecl(hasName("x")));
  2715. ToF1 = Import(FromF, Lang_CXX);
  2716. }
  2717. Decl *ToF2;
  2718. {
  2719. Decl *FromTU = getTuDecl(
  2720. "struct A { unsigned x; };", Lang_CXX, "input1.cc");
  2721. auto *FromF = FirstDeclMatcher<FieldDecl>().match(
  2722. FromTU, fieldDecl(hasName("x")));
  2723. ToF2 = Import(FromF, Lang_CXX);
  2724. }
  2725. EXPECT_NE(ToF1, ToF2);
  2726. }
  2727. TEST_P(ASTImporterOptionSpecificTestBase, ImportOfEquivalentMethod) {
  2728. Decl *ToM1;
  2729. {
  2730. Decl *FromTU = getTuDecl(
  2731. "struct A { void x(); }; void A::x() { }", Lang_CXX, "input0.cc");
  2732. auto *FromM = FirstDeclMatcher<FunctionDecl>().match(
  2733. FromTU, functionDecl(hasName("x"), isDefinition()));
  2734. ToM1 = Import(FromM, Lang_CXX);
  2735. }
  2736. Decl *ToM2;
  2737. {
  2738. Decl *FromTU = getTuDecl(
  2739. "struct A { void x(); }; void A::x() { }", Lang_CXX, "input1.cc");
  2740. auto *FromM = FirstDeclMatcher<FunctionDecl>().match(
  2741. FromTU, functionDecl(hasName("x"), isDefinition()));
  2742. ToM2 = Import(FromM, Lang_CXX);
  2743. }
  2744. EXPECT_EQ(ToM1, ToM2);
  2745. }
  2746. TEST_P(ASTImporterOptionSpecificTestBase, ImportOfNonEquivalentMethod) {
  2747. Decl *ToM1;
  2748. {
  2749. Decl *FromTU = getTuDecl(
  2750. "struct A { void x(); }; void A::x() { }",
  2751. Lang_CXX, "input0.cc");
  2752. auto *FromM = FirstDeclMatcher<FunctionDecl>().match(
  2753. FromTU, functionDecl(hasName("x"), isDefinition()));
  2754. ToM1 = Import(FromM, Lang_CXX);
  2755. }
  2756. Decl *ToM2;
  2757. {
  2758. Decl *FromTU = getTuDecl(
  2759. "struct A { void x() const; }; void A::x() const { }",
  2760. Lang_CXX, "input1.cc");
  2761. auto *FromM = FirstDeclMatcher<FunctionDecl>().match(
  2762. FromTU, functionDecl(hasName("x"), isDefinition()));
  2763. ToM2 = Import(FromM, Lang_CXX);
  2764. }
  2765. EXPECT_NE(ToM1, ToM2);
  2766. }
  2767. TEST_P(ASTImporterOptionSpecificTestBase,
  2768. ImportUnnamedStructsWithRecursingField) {
  2769. Decl *FromTU = getTuDecl(
  2770. R"(
  2771. struct A {
  2772. struct {
  2773. struct A *next;
  2774. } entry0;
  2775. struct {
  2776. struct A *next;
  2777. } entry1;
  2778. };
  2779. )",
  2780. Lang_C, "input0.cc");
  2781. auto *From =
  2782. FirstDeclMatcher<RecordDecl>().match(FromTU, recordDecl(hasName("A")));
  2783. Import(From, Lang_C);
  2784. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2785. auto *Entry0 =
  2786. FirstDeclMatcher<FieldDecl>().match(ToTU, fieldDecl(hasName("entry0")));
  2787. auto *Entry1 =
  2788. FirstDeclMatcher<FieldDecl>().match(ToTU, fieldDecl(hasName("entry1")));
  2789. auto *R0 = getRecordDecl(Entry0);
  2790. auto *R1 = getRecordDecl(Entry1);
  2791. EXPECT_NE(R0, R1);
  2792. EXPECT_TRUE(MatchVerifier<RecordDecl>().match(
  2793. R0, recordDecl(has(fieldDecl(hasName("next"))))));
  2794. EXPECT_TRUE(MatchVerifier<RecordDecl>().match(
  2795. R1, recordDecl(has(fieldDecl(hasName("next"))))));
  2796. }
  2797. TEST_P(ASTImporterOptionSpecificTestBase, ImportUnnamedFieldsInCorrectOrder) {
  2798. Decl *FromTU = getTuDecl(
  2799. R"(
  2800. void f(int X, int Y, bool Z) {
  2801. (void)[X, Y, Z] { (void)Z; };
  2802. }
  2803. )",
  2804. Lang_CXX11, "input0.cc");
  2805. auto *FromF = FirstDeclMatcher<FunctionDecl>().match(
  2806. FromTU, functionDecl(hasName("f")));
  2807. auto *ToF = cast_or_null<FunctionDecl>(Import(FromF, Lang_CXX11));
  2808. EXPECT_TRUE(ToF);
  2809. CXXRecordDecl *FromLambda =
  2810. cast<LambdaExpr>(cast<CStyleCastExpr>(cast<CompoundStmt>(
  2811. FromF->getBody())->body_front())->getSubExpr())->getLambdaClass();
  2812. auto *ToLambda = cast_or_null<CXXRecordDecl>(Import(FromLambda, Lang_CXX11));
  2813. EXPECT_TRUE(ToLambda);
  2814. // Check if the fields of the lambda class are imported in correct order.
  2815. unsigned FromIndex = 0u;
  2816. for (auto *FromField : FromLambda->fields()) {
  2817. ASSERT_FALSE(FromField->getDeclName());
  2818. auto *ToField = cast_or_null<FieldDecl>(Import(FromField, Lang_CXX11));
  2819. EXPECT_TRUE(ToField);
  2820. Optional<unsigned> ToIndex = ASTImporter::getFieldIndex(ToField);
  2821. EXPECT_TRUE(ToIndex);
  2822. EXPECT_EQ(*ToIndex, FromIndex);
  2823. ++FromIndex;
  2824. }
  2825. EXPECT_EQ(FromIndex, 3u);
  2826. }
  2827. TEST_P(ASTImporterOptionSpecificTestBase,
  2828. MergeFieldDeclsOfClassTemplateSpecialization) {
  2829. std::string ClassTemplate =
  2830. R"(
  2831. template <typename T>
  2832. struct X {
  2833. int a{0}; // FieldDecl with InitListExpr
  2834. X(char) : a(3) {} // (1)
  2835. X(int) {} // (2)
  2836. };
  2837. )";
  2838. Decl *ToTU = getToTuDecl(ClassTemplate +
  2839. R"(
  2840. void foo() {
  2841. // ClassTemplateSpec with ctor (1): FieldDecl without InitlistExpr
  2842. X<char> xc('c');
  2843. }
  2844. )", Lang_CXX11);
  2845. auto *ToSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2846. ToTU, classTemplateSpecializationDecl(hasName("X")));
  2847. // FieldDecl without InitlistExpr:
  2848. auto *ToField = *ToSpec->field_begin();
  2849. ASSERT_TRUE(ToField);
  2850. ASSERT_FALSE(ToField->getInClassInitializer());
  2851. Decl *FromTU = getTuDecl(ClassTemplate +
  2852. R"(
  2853. void bar() {
  2854. // ClassTemplateSpec with ctor (2): FieldDecl WITH InitlistExpr
  2855. X<char> xc(1);
  2856. }
  2857. )", Lang_CXX11);
  2858. auto *FromSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2859. FromTU, classTemplateSpecializationDecl(hasName("X")));
  2860. // FieldDecl with InitlistExpr:
  2861. auto *FromField = *FromSpec->field_begin();
  2862. ASSERT_TRUE(FromField);
  2863. ASSERT_TRUE(FromField->getInClassInitializer());
  2864. auto *ImportedSpec = Import(FromSpec, Lang_CXX11);
  2865. ASSERT_TRUE(ImportedSpec);
  2866. EXPECT_EQ(ImportedSpec, ToSpec);
  2867. // After the import, the FieldDecl has to be merged, thus it should have the
  2868. // InitListExpr.
  2869. EXPECT_TRUE(ToField->getInClassInitializer());
  2870. }
  2871. TEST_P(ASTImporterOptionSpecificTestBase,
  2872. MergeFunctionOfClassTemplateSpecialization) {
  2873. std::string ClassTemplate =
  2874. R"(
  2875. template <typename T>
  2876. struct X {
  2877. void f() {}
  2878. void g() {}
  2879. };
  2880. )";
  2881. Decl *ToTU = getToTuDecl(ClassTemplate +
  2882. R"(
  2883. void foo() {
  2884. X<char> x;
  2885. x.f();
  2886. }
  2887. )", Lang_CXX11);
  2888. Decl *FromTU = getTuDecl(ClassTemplate +
  2889. R"(
  2890. void bar() {
  2891. X<char> x;
  2892. x.g();
  2893. }
  2894. )", Lang_CXX11);
  2895. auto *FromSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2896. FromTU, classTemplateSpecializationDecl(hasName("X")));
  2897. auto FunPattern = functionDecl(hasName("g"),
  2898. hasParent(classTemplateSpecializationDecl()));
  2899. auto *FromFun =
  2900. FirstDeclMatcher<FunctionDecl>().match(FromTU, FunPattern);
  2901. auto *ToFun =
  2902. FirstDeclMatcher<FunctionDecl>().match(ToTU, FunPattern);
  2903. ASSERT_TRUE(FromFun->hasBody());
  2904. ASSERT_FALSE(ToFun->hasBody());
  2905. auto *ImportedSpec = Import(FromSpec, Lang_CXX11);
  2906. ASSERT_TRUE(ImportedSpec);
  2907. auto *ToSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2908. ToTU, classTemplateSpecializationDecl(hasName("X")));
  2909. EXPECT_EQ(ImportedSpec, ToSpec);
  2910. EXPECT_TRUE(ToFun->hasBody());
  2911. }
  2912. TEST_P(ASTImporterOptionSpecificTestBase,
  2913. ODRViolationOfClassTemplateSpecializationsShouldBeReported) {
  2914. std::string ClassTemplate =
  2915. R"(
  2916. template <typename T>
  2917. struct X {};
  2918. )";
  2919. Decl *ToTU = getToTuDecl(ClassTemplate +
  2920. R"(
  2921. template <>
  2922. struct X<char> {
  2923. int a;
  2924. };
  2925. void foo() {
  2926. X<char> x;
  2927. }
  2928. )",
  2929. Lang_CXX11);
  2930. Decl *FromTU = getTuDecl(ClassTemplate +
  2931. R"(
  2932. template <>
  2933. struct X<char> {
  2934. int b;
  2935. };
  2936. void foo() {
  2937. X<char> x;
  2938. }
  2939. )",
  2940. Lang_CXX11);
  2941. auto *FromSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2942. FromTU, classTemplateSpecializationDecl(hasName("X")));
  2943. auto *ImportedSpec = Import(FromSpec, Lang_CXX11);
  2944. // We expect one (ODR) warning during the import.
  2945. EXPECT_EQ(1u, ToTU->getASTContext().getDiagnostics().getNumWarnings());
  2946. // The second specialization is different from the first, thus it violates
  2947. // ODR, consequently we expect to keep the first specialization only, which is
  2948. // already in the "To" context.
  2949. EXPECT_FALSE(ImportedSpec);
  2950. EXPECT_EQ(1u,
  2951. DeclCounter<ClassTemplateSpecializationDecl>().match(
  2952. ToTU, classTemplateSpecializationDecl(hasName("X"))));
  2953. }
  2954. TEST_P(ASTImporterOptionSpecificTestBase,
  2955. MergeCtorOfClassTemplateSpecialization) {
  2956. std::string ClassTemplate =
  2957. R"(
  2958. template <typename T>
  2959. struct X {
  2960. X(char) {}
  2961. X(int) {}
  2962. };
  2963. )";
  2964. Decl *ToTU = getToTuDecl(ClassTemplate +
  2965. R"(
  2966. void foo() {
  2967. X<char> x('c');
  2968. }
  2969. )", Lang_CXX11);
  2970. Decl *FromTU = getTuDecl(ClassTemplate +
  2971. R"(
  2972. void bar() {
  2973. X<char> x(1);
  2974. }
  2975. )", Lang_CXX11);
  2976. auto *FromSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2977. FromTU, classTemplateSpecializationDecl(hasName("X")));
  2978. // Match the void(int) ctor.
  2979. auto CtorPattern =
  2980. cxxConstructorDecl(hasParameter(0, varDecl(hasType(asString("int")))),
  2981. hasParent(classTemplateSpecializationDecl()));
  2982. auto *FromCtor =
  2983. FirstDeclMatcher<CXXConstructorDecl>().match(FromTU, CtorPattern);
  2984. auto *ToCtor =
  2985. FirstDeclMatcher<CXXConstructorDecl>().match(ToTU, CtorPattern);
  2986. ASSERT_TRUE(FromCtor->hasBody());
  2987. ASSERT_FALSE(ToCtor->hasBody());
  2988. auto *ImportedSpec = Import(FromSpec, Lang_CXX11);
  2989. ASSERT_TRUE(ImportedSpec);
  2990. auto *ToSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2991. ToTU, classTemplateSpecializationDecl(hasName("X")));
  2992. EXPECT_EQ(ImportedSpec, ToSpec);
  2993. EXPECT_TRUE(ToCtor->hasBody());
  2994. }
  2995. TEST_P(ASTImporterOptionSpecificTestBase,
  2996. ClassTemplatePartialSpecializationsShouldNotBeDuplicated) {
  2997. auto Code =
  2998. R"(
  2999. // primary template
  3000. template<class T1, class T2, int I>
  3001. class A {};
  3002. // partial specialization
  3003. template<class T, int I>
  3004. class A<T, T*, I> {};
  3005. )";
  3006. Decl *ToTU = getToTuDecl(Code, Lang_CXX11);
  3007. Decl *FromTU = getTuDecl(Code, Lang_CXX11);
  3008. auto *FromSpec =
  3009. FirstDeclMatcher<ClassTemplatePartialSpecializationDecl>().match(
  3010. FromTU, classTemplatePartialSpecializationDecl());
  3011. auto *ToSpec =
  3012. FirstDeclMatcher<ClassTemplatePartialSpecializationDecl>().match(
  3013. ToTU, classTemplatePartialSpecializationDecl());
  3014. auto *ImportedSpec = Import(FromSpec, Lang_CXX11);
  3015. EXPECT_EQ(ImportedSpec, ToSpec);
  3016. EXPECT_EQ(1u, DeclCounter<ClassTemplatePartialSpecializationDecl>().match(
  3017. ToTU, classTemplatePartialSpecializationDecl()));
  3018. }
  3019. TEST_P(ASTImporterOptionSpecificTestBase,
  3020. ClassTemplateSpecializationsShouldNotBeDuplicated) {
  3021. auto Code =
  3022. R"(
  3023. // primary template
  3024. template<class T1, class T2, int I>
  3025. class A {};
  3026. // full specialization
  3027. template<>
  3028. class A<int, int, 1> {};
  3029. )";
  3030. Decl *ToTU = getToTuDecl(Code, Lang_CXX11);
  3031. Decl *FromTU = getTuDecl(Code, Lang_CXX11);
  3032. auto *FromSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  3033. FromTU, classTemplateSpecializationDecl());
  3034. auto *ToSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  3035. ToTU, classTemplateSpecializationDecl());
  3036. auto *ImportedSpec = Import(FromSpec, Lang_CXX11);
  3037. EXPECT_EQ(ImportedSpec, ToSpec);
  3038. EXPECT_EQ(1u, DeclCounter<ClassTemplateSpecializationDecl>().match(
  3039. ToTU, classTemplateSpecializationDecl()));
  3040. }
  3041. TEST_P(ASTImporterOptionSpecificTestBase,
  3042. ClassTemplateFullAndPartialSpecsShouldNotBeMixed) {
  3043. std::string PrimaryTemplate =
  3044. R"(
  3045. template<class T1, class T2, int I>
  3046. class A {};
  3047. )";
  3048. auto PartialSpec =
  3049. R"(
  3050. template<class T, int I>
  3051. class A<T, T*, I> {};
  3052. )";
  3053. auto FullSpec =
  3054. R"(
  3055. template<>
  3056. class A<int, int, 1> {};
  3057. )";
  3058. Decl *ToTU = getToTuDecl(PrimaryTemplate + FullSpec, Lang_CXX11);
  3059. Decl *FromTU = getTuDecl(PrimaryTemplate + PartialSpec, Lang_CXX11);
  3060. auto *FromSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  3061. FromTU, classTemplateSpecializationDecl());
  3062. auto *ImportedSpec = Import(FromSpec, Lang_CXX11);
  3063. EXPECT_TRUE(ImportedSpec);
  3064. // Check the number of partial specializations.
  3065. EXPECT_EQ(1u, DeclCounter<ClassTemplatePartialSpecializationDecl>().match(
  3066. ToTU, classTemplatePartialSpecializationDecl()));
  3067. // Check the number of full specializations.
  3068. EXPECT_EQ(1u, DeclCounter<ClassTemplateSpecializationDecl>().match(
  3069. ToTU, classTemplateSpecializationDecl(
  3070. unless(classTemplatePartialSpecializationDecl()))));
  3071. }
  3072. TEST_P(ASTImporterOptionSpecificTestBase,
  3073. InitListExprValueKindShouldBeImported) {
  3074. Decl *TU = getTuDecl(
  3075. R"(
  3076. const int &init();
  3077. void foo() { const int &a{init()}; }
  3078. )", Lang_CXX11, "input0.cc");
  3079. auto *FromD = FirstDeclMatcher<VarDecl>().match(TU, varDecl(hasName("a")));
  3080. ASSERT_TRUE(FromD->getAnyInitializer());
  3081. auto *InitExpr = FromD->getAnyInitializer();
  3082. ASSERT_TRUE(InitExpr);
  3083. ASSERT_TRUE(InitExpr->isGLValue());
  3084. auto *ToD = Import(FromD, Lang_CXX11);
  3085. EXPECT_TRUE(ToD);
  3086. auto *ToInitExpr = cast<VarDecl>(ToD)->getAnyInitializer();
  3087. EXPECT_TRUE(ToInitExpr);
  3088. EXPECT_TRUE(ToInitExpr->isGLValue());
  3089. }
  3090. struct ImportVariables : ASTImporterOptionSpecificTestBase {};
  3091. TEST_P(ImportVariables, ImportOfOneDeclBringsInTheWholeChain) {
  3092. Decl *FromTU = getTuDecl(
  3093. R"(
  3094. struct A {
  3095. static const int a = 1 + 2;
  3096. };
  3097. const int A::a;
  3098. )", Lang_CXX, "input1.cc");
  3099. auto *FromDWithInit = FirstDeclMatcher<VarDecl>().match(
  3100. FromTU, varDecl(hasName("a"))); // Decl with init
  3101. auto *FromDWithDef = LastDeclMatcher<VarDecl>().match(
  3102. FromTU, varDecl(hasName("a"))); // Decl with definition
  3103. ASSERT_NE(FromDWithInit, FromDWithDef);
  3104. ASSERT_EQ(FromDWithDef->getPreviousDecl(), FromDWithInit);
  3105. auto *ToD0 = cast<VarDecl>(Import(FromDWithInit, Lang_CXX11));
  3106. auto *ToD1 = cast<VarDecl>(Import(FromDWithDef, Lang_CXX11));
  3107. ASSERT_TRUE(ToD0);
  3108. ASSERT_TRUE(ToD1);
  3109. EXPECT_NE(ToD0, ToD1);
  3110. EXPECT_EQ(ToD1->getPreviousDecl(), ToD0);
  3111. }
  3112. TEST_P(ImportVariables, InitAndDefinitionAreInDifferentTUs) {
  3113. auto StructA =
  3114. R"(
  3115. struct A {
  3116. static const int a = 1 + 2;
  3117. };
  3118. )";
  3119. Decl *ToTU = getToTuDecl(StructA, Lang_CXX);
  3120. Decl *FromTU = getTuDecl(std::string(StructA) + "const int A::a;", Lang_CXX,
  3121. "input1.cc");
  3122. auto *FromDWithInit = FirstDeclMatcher<VarDecl>().match(
  3123. FromTU, varDecl(hasName("a"))); // Decl with init
  3124. auto *FromDWithDef = LastDeclMatcher<VarDecl>().match(
  3125. FromTU, varDecl(hasName("a"))); // Decl with definition
  3126. ASSERT_EQ(FromDWithInit, FromDWithDef->getPreviousDecl());
  3127. ASSERT_TRUE(FromDWithInit->getInit());
  3128. ASSERT_FALSE(FromDWithInit->isThisDeclarationADefinition());
  3129. ASSERT_TRUE(FromDWithDef->isThisDeclarationADefinition());
  3130. ASSERT_FALSE(FromDWithDef->getInit());
  3131. auto *ToD = FirstDeclMatcher<VarDecl>().match(
  3132. ToTU, varDecl(hasName("a"))); // Decl with init
  3133. ASSERT_TRUE(ToD->getInit());
  3134. ASSERT_FALSE(ToD->getDefinition());
  3135. auto *ImportedD = cast<VarDecl>(Import(FromDWithDef, Lang_CXX11));
  3136. EXPECT_TRUE(ImportedD->getAnyInitializer());
  3137. EXPECT_TRUE(ImportedD->getDefinition());
  3138. }
  3139. TEST_P(ImportVariables, InitAndDefinitionAreInTheFromContext) {
  3140. auto StructA =
  3141. R"(
  3142. struct A {
  3143. static const int a;
  3144. };
  3145. )";
  3146. Decl *ToTU = getToTuDecl(StructA, Lang_CXX);
  3147. Decl *FromTU = getTuDecl(std::string(StructA) + "const int A::a = 1 + 2;",
  3148. Lang_CXX, "input1.cc");
  3149. auto *FromDDeclarationOnly = FirstDeclMatcher<VarDecl>().match(
  3150. FromTU, varDecl(hasName("a")));
  3151. auto *FromDWithDef = LastDeclMatcher<VarDecl>().match(
  3152. FromTU, varDecl(hasName("a"))); // Decl with definition and with init.
  3153. ASSERT_EQ(FromDDeclarationOnly, FromDWithDef->getPreviousDecl());
  3154. ASSERT_FALSE(FromDDeclarationOnly->getInit());
  3155. ASSERT_FALSE(FromDDeclarationOnly->isThisDeclarationADefinition());
  3156. ASSERT_TRUE(FromDWithDef->isThisDeclarationADefinition());
  3157. ASSERT_TRUE(FromDWithDef->getInit());
  3158. auto *ToD = FirstDeclMatcher<VarDecl>().match(
  3159. ToTU, varDecl(hasName("a")));
  3160. ASSERT_FALSE(ToD->getInit());
  3161. ASSERT_FALSE(ToD->getDefinition());
  3162. auto *ImportedD = cast<VarDecl>(Import(FromDWithDef, Lang_CXX11));
  3163. EXPECT_TRUE(ImportedD->getAnyInitializer());
  3164. EXPECT_TRUE(ImportedD->getDefinition());
  3165. }
  3166. struct ImportClasses : ASTImporterOptionSpecificTestBase {};
  3167. TEST_P(ImportClasses, ImportDefinitionWhenProtoIsInNestedToContext) {
  3168. Decl *ToTU = getToTuDecl("struct A { struct X *Xp; };", Lang_C);
  3169. Decl *FromTU1 = getTuDecl("struct X {};", Lang_C, "input1.cc");
  3170. auto Pattern = recordDecl(hasName("X"), unless(isImplicit()));
  3171. auto ToProto = FirstDeclMatcher<RecordDecl>().match(ToTU, Pattern);
  3172. auto FromDef = FirstDeclMatcher<RecordDecl>().match(FromTU1, Pattern);
  3173. Decl *ImportedDef = Import(FromDef, Lang_C);
  3174. EXPECT_NE(ImportedDef, ToProto);
  3175. EXPECT_EQ(DeclCounter<RecordDecl>().match(ToTU, Pattern), 2u);
  3176. auto ToDef = LastDeclMatcher<RecordDecl>().match(ToTU, Pattern);
  3177. EXPECT_TRUE(ImportedDef == ToDef);
  3178. EXPECT_TRUE(ToDef->isThisDeclarationADefinition());
  3179. EXPECT_FALSE(ToProto->isThisDeclarationADefinition());
  3180. EXPECT_EQ(ToDef->getPreviousDecl(), ToProto);
  3181. }
  3182. TEST_P(ImportClasses, ImportDefinitionWhenProtoIsInNestedToContextCXX) {
  3183. Decl *ToTU = getToTuDecl("struct A { struct X *Xp; };", Lang_CXX);
  3184. Decl *FromTU1 = getTuDecl("struct X {};", Lang_CXX, "input1.cc");
  3185. auto Pattern = recordDecl(hasName("X"), unless(isImplicit()));
  3186. auto ToProto = FirstDeclMatcher<RecordDecl>().match(ToTU, Pattern);
  3187. auto FromDef = FirstDeclMatcher<RecordDecl>().match(FromTU1, Pattern);
  3188. Decl *ImportedDef = Import(FromDef, Lang_CXX);
  3189. EXPECT_NE(ImportedDef, ToProto);
  3190. EXPECT_EQ(DeclCounter<RecordDecl>().match(ToTU, Pattern), 2u);
  3191. auto ToDef = LastDeclMatcher<RecordDecl>().match(ToTU, Pattern);
  3192. EXPECT_TRUE(ImportedDef == ToDef);
  3193. EXPECT_TRUE(ToDef->isThisDeclarationADefinition());
  3194. EXPECT_FALSE(ToProto->isThisDeclarationADefinition());
  3195. EXPECT_EQ(ToDef->getPreviousDecl(), ToProto);
  3196. }
  3197. TEST_P(ImportClasses, ImportNestedPrototypeThenDefinition) {
  3198. Decl *FromTU0 = getTuDecl("struct A { struct X *Xp; };", Lang_C, "input0.cc");
  3199. Decl *FromTU1 = getTuDecl("struct X {};", Lang_C, "input1.cc");
  3200. auto Pattern = recordDecl(hasName("X"), unless(isImplicit()));
  3201. auto FromProto = FirstDeclMatcher<RecordDecl>().match(FromTU0, Pattern);
  3202. auto FromDef = FirstDeclMatcher<RecordDecl>().match(FromTU1, Pattern);
  3203. Decl *ImportedProto = Import(FromProto, Lang_C);
  3204. Decl *ImportedDef = Import(FromDef, Lang_C);
  3205. Decl *ToTU = ImportedDef->getTranslationUnitDecl();
  3206. EXPECT_NE(ImportedDef, ImportedProto);
  3207. EXPECT_EQ(DeclCounter<RecordDecl>().match(ToTU, Pattern), 2u);
  3208. auto ToProto = FirstDeclMatcher<RecordDecl>().match(ToTU, Pattern);
  3209. auto ToDef = LastDeclMatcher<RecordDecl>().match(ToTU, Pattern);
  3210. EXPECT_TRUE(ImportedDef == ToDef);
  3211. EXPECT_TRUE(ImportedProto == ToProto);
  3212. EXPECT_TRUE(ToDef->isThisDeclarationADefinition());
  3213. EXPECT_FALSE(ToProto->isThisDeclarationADefinition());
  3214. EXPECT_EQ(ToDef->getPreviousDecl(), ToProto);
  3215. }
  3216. struct ImportFriendClasses : ASTImporterOptionSpecificTestBase {};
  3217. TEST_P(ImportFriendClasses, ImportOfFriendRecordDoesNotMergeDefinition) {
  3218. Decl *FromTU = getTuDecl(
  3219. R"(
  3220. class A {
  3221. template <int I> class F {};
  3222. class X {
  3223. template <int I> friend class F;
  3224. };
  3225. };
  3226. )",
  3227. Lang_CXX, "input0.cc");
  3228. auto *FromClass = FirstDeclMatcher<CXXRecordDecl>().match(
  3229. FromTU, cxxRecordDecl(hasName("F"), isDefinition()));
  3230. auto *FromFriendClass = LastDeclMatcher<CXXRecordDecl>().match(
  3231. FromTU, cxxRecordDecl(hasName("F")));
  3232. ASSERT_TRUE(FromClass);
  3233. ASSERT_TRUE(FromFriendClass);
  3234. ASSERT_NE(FromClass, FromFriendClass);
  3235. ASSERT_EQ(FromFriendClass->getDefinition(), FromClass);
  3236. ASSERT_EQ(FromFriendClass->getPreviousDecl(), FromClass);
  3237. ASSERT_EQ(FromFriendClass->getDescribedClassTemplate()->getPreviousDecl(),
  3238. FromClass->getDescribedClassTemplate());
  3239. auto *ToClass = cast<CXXRecordDecl>(Import(FromClass, Lang_CXX));
  3240. auto *ToFriendClass = cast<CXXRecordDecl>(Import(FromFriendClass, Lang_CXX));
  3241. EXPECT_TRUE(ToClass);
  3242. EXPECT_TRUE(ToFriendClass);
  3243. EXPECT_NE(ToClass, ToFriendClass);
  3244. EXPECT_EQ(ToFriendClass->getDefinition(), ToClass);
  3245. EXPECT_EQ(ToFriendClass->getPreviousDecl(), ToClass);
  3246. EXPECT_EQ(ToFriendClass->getDescribedClassTemplate()->getPreviousDecl(),
  3247. ToClass->getDescribedClassTemplate());
  3248. }
  3249. TEST_P(ImportFriendClasses, ImportOfRecursiveFriendClass) {
  3250. Decl *FromTu = getTuDecl(
  3251. R"(
  3252. class declToImport {
  3253. friend class declToImport;
  3254. };
  3255. )",
  3256. Lang_CXX, "input.cc");
  3257. auto *FromD = FirstDeclMatcher<CXXRecordDecl>().match(
  3258. FromTu, cxxRecordDecl(hasName("declToImport")));
  3259. auto *ToD = Import(FromD, Lang_CXX);
  3260. auto Pattern = cxxRecordDecl(has(friendDecl()));
  3261. ASSERT_TRUE(MatchVerifier<Decl>{}.match(FromD, Pattern));
  3262. EXPECT_TRUE(MatchVerifier<Decl>{}.match(ToD, Pattern));
  3263. }
  3264. TEST_P(ImportFriendClasses, ImportOfRecursiveFriendClassTemplate) {
  3265. Decl *FromTu = getTuDecl(
  3266. R"(
  3267. template<class A> class declToImport {
  3268. template<class A1> friend class declToImport;
  3269. };
  3270. )",
  3271. Lang_CXX, "input.cc");
  3272. auto *FromD =
  3273. FirstDeclMatcher<ClassTemplateDecl>().match(FromTu, classTemplateDecl());
  3274. auto *ToD = Import(FromD, Lang_CXX);
  3275. auto Pattern = classTemplateDecl(
  3276. has(cxxRecordDecl(has(friendDecl(has(classTemplateDecl()))))));
  3277. ASSERT_TRUE(MatchVerifier<Decl>{}.match(FromD, Pattern));
  3278. EXPECT_TRUE(MatchVerifier<Decl>{}.match(ToD, Pattern));
  3279. auto *Class =
  3280. FirstDeclMatcher<ClassTemplateDecl>().match(ToD, classTemplateDecl());
  3281. auto *Friend = FirstDeclMatcher<FriendDecl>().match(ToD, friendDecl());
  3282. EXPECT_NE(Friend->getFriendDecl(), Class);
  3283. EXPECT_EQ(Friend->getFriendDecl()->getPreviousDecl(), Class);
  3284. }
  3285. TEST_P(ImportFriendClasses, ProperPrevDeclForClassTemplateDecls) {
  3286. auto Pattern = classTemplateSpecializationDecl(hasName("X"));
  3287. ClassTemplateSpecializationDecl *Imported1;
  3288. {
  3289. Decl *FromTU = getTuDecl("template<class T> class X;"
  3290. "struct Y { friend class X<int>; };",
  3291. Lang_CXX, "input0.cc");
  3292. auto *FromD = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  3293. FromTU, Pattern);
  3294. Imported1 = cast<ClassTemplateSpecializationDecl>(Import(FromD, Lang_CXX));
  3295. }
  3296. ClassTemplateSpecializationDecl *Imported2;
  3297. {
  3298. Decl *FromTU = getTuDecl("template<class T> class X;"
  3299. "template<> class X<int>{};"
  3300. "struct Z { friend class X<int>; };",
  3301. Lang_CXX, "input1.cc");
  3302. auto *FromD = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  3303. FromTU, Pattern);
  3304. Imported2 = cast<ClassTemplateSpecializationDecl>(Import(FromD, Lang_CXX));
  3305. }
  3306. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  3307. EXPECT_EQ(DeclCounter<ClassTemplateSpecializationDecl>().match(ToTU, Pattern),
  3308. 2u);
  3309. ASSERT_TRUE(Imported2->getPreviousDecl());
  3310. EXPECT_EQ(Imported2->getPreviousDecl(), Imported1);
  3311. }
  3312. TEST_P(ImportFriendClasses, TypeForDeclShouldBeSetInTemplated) {
  3313. Decl *FromTU0 = getTuDecl(
  3314. R"(
  3315. class X {
  3316. class Y;
  3317. };
  3318. class X::Y {
  3319. template <typename T>
  3320. friend class F; // The decl context of F is the global namespace.
  3321. };
  3322. )",
  3323. Lang_CXX, "input0.cc");
  3324. auto *Fwd = FirstDeclMatcher<ClassTemplateDecl>().match(
  3325. FromTU0, classTemplateDecl(hasName("F")));
  3326. auto *Imported0 = cast<ClassTemplateDecl>(Import(Fwd, Lang_CXX));
  3327. Decl *FromTU1 = getTuDecl(
  3328. R"(
  3329. template <typename T>
  3330. class F {};
  3331. )",
  3332. Lang_CXX, "input1.cc");
  3333. auto *Definition = FirstDeclMatcher<ClassTemplateDecl>().match(
  3334. FromTU1, classTemplateDecl(hasName("F")));
  3335. auto *Imported1 = cast<ClassTemplateDecl>(Import(Definition, Lang_CXX));
  3336. EXPECT_EQ(Imported0->getTemplatedDecl()->getTypeForDecl(),
  3337. Imported1->getTemplatedDecl()->getTypeForDecl());
  3338. }
  3339. TEST_P(ImportFriendClasses, DeclsFromFriendsShouldBeInRedeclChains) {
  3340. Decl *From, *To;
  3341. std::tie(From, To) =
  3342. getImportedDecl("class declToImport {};", Lang_CXX,
  3343. "class Y { friend class declToImport; };", Lang_CXX);
  3344. auto *Imported = cast<CXXRecordDecl>(To);
  3345. EXPECT_TRUE(Imported->getPreviousDecl());
  3346. }
  3347. TEST_P(ImportFriendClasses,
  3348. ImportOfClassTemplateDefinitionShouldConnectToFwdFriend) {
  3349. Decl *ToTU = getToTuDecl(
  3350. R"(
  3351. class X {
  3352. class Y;
  3353. };
  3354. class X::Y {
  3355. template <typename T>
  3356. friend class F; // The decl context of F is the global namespace.
  3357. };
  3358. )",
  3359. Lang_CXX);
  3360. auto *ToDecl = FirstDeclMatcher<ClassTemplateDecl>().match(
  3361. ToTU, classTemplateDecl(hasName("F")));
  3362. Decl *FromTU = getTuDecl(
  3363. R"(
  3364. template <typename T>
  3365. class F {};
  3366. )",
  3367. Lang_CXX, "input0.cc");
  3368. auto *Definition = FirstDeclMatcher<ClassTemplateDecl>().match(
  3369. FromTU, classTemplateDecl(hasName("F")));
  3370. auto *ImportedDef = cast<ClassTemplateDecl>(Import(Definition, Lang_CXX));
  3371. EXPECT_TRUE(ImportedDef->getPreviousDecl());
  3372. EXPECT_EQ(ToDecl, ImportedDef->getPreviousDecl());
  3373. EXPECT_EQ(ToDecl->getTemplatedDecl(),
  3374. ImportedDef->getTemplatedDecl()->getPreviousDecl());
  3375. }
  3376. TEST_P(ImportFriendClasses,
  3377. ImportOfClassTemplateDefinitionAndFwdFriendShouldBeLinked) {
  3378. Decl *FromTU0 = getTuDecl(
  3379. R"(
  3380. class X {
  3381. class Y;
  3382. };
  3383. class X::Y {
  3384. template <typename T>
  3385. friend class F; // The decl context of F is the global namespace.
  3386. };
  3387. )",
  3388. Lang_CXX, "input0.cc");
  3389. auto *Fwd = FirstDeclMatcher<ClassTemplateDecl>().match(
  3390. FromTU0, classTemplateDecl(hasName("F")));
  3391. auto *ImportedFwd = cast<ClassTemplateDecl>(Import(Fwd, Lang_CXX));
  3392. Decl *FromTU1 = getTuDecl(
  3393. R"(
  3394. template <typename T>
  3395. class F {};
  3396. )",
  3397. Lang_CXX, "input1.cc");
  3398. auto *Definition = FirstDeclMatcher<ClassTemplateDecl>().match(
  3399. FromTU1, classTemplateDecl(hasName("F")));
  3400. auto *ImportedDef = cast<ClassTemplateDecl>(Import(Definition, Lang_CXX));
  3401. EXPECT_TRUE(ImportedDef->getPreviousDecl());
  3402. EXPECT_EQ(ImportedFwd, ImportedDef->getPreviousDecl());
  3403. EXPECT_EQ(ImportedFwd->getTemplatedDecl(),
  3404. ImportedDef->getTemplatedDecl()->getPreviousDecl());
  3405. }
  3406. TEST_P(ImportFriendClasses, ImportOfClassDefinitionAndFwdFriendShouldBeLinked) {
  3407. Decl *FromTU0 = getTuDecl(
  3408. R"(
  3409. class X {
  3410. class Y;
  3411. };
  3412. class X::Y {
  3413. friend class F; // The decl context of F is the global namespace.
  3414. };
  3415. )",
  3416. Lang_CXX, "input0.cc");
  3417. auto *Friend = FirstDeclMatcher<FriendDecl>().match(FromTU0, friendDecl());
  3418. QualType FT = Friend->getFriendType()->getType();
  3419. FT = FromTU0->getASTContext().getCanonicalType(FT);
  3420. auto *Fwd = cast<TagType>(FT)->getDecl();
  3421. auto *ImportedFwd = Import(Fwd, Lang_CXX);
  3422. Decl *FromTU1 = getTuDecl(
  3423. R"(
  3424. class F {};
  3425. )",
  3426. Lang_CXX, "input1.cc");
  3427. auto *Definition = FirstDeclMatcher<CXXRecordDecl>().match(
  3428. FromTU1, cxxRecordDecl(hasName("F")));
  3429. auto *ImportedDef = Import(Definition, Lang_CXX);
  3430. EXPECT_TRUE(ImportedDef->getPreviousDecl());
  3431. EXPECT_EQ(ImportedFwd, ImportedDef->getPreviousDecl());
  3432. }
  3433. TEST_P(ASTImporterOptionSpecificTestBase, FriendFunInClassTemplate) {
  3434. auto *Code = R"(
  3435. template <class T>
  3436. struct X {
  3437. friend void foo(){}
  3438. };
  3439. )";
  3440. TranslationUnitDecl *ToTU = getToTuDecl(Code, Lang_CXX);
  3441. auto *ToFoo = FirstDeclMatcher<FunctionDecl>().match(
  3442. ToTU, functionDecl(hasName("foo")));
  3443. TranslationUnitDecl *FromTU = getTuDecl(Code, Lang_CXX, "input.cc");
  3444. auto *FromFoo = FirstDeclMatcher<FunctionDecl>().match(
  3445. FromTU, functionDecl(hasName("foo")));
  3446. auto *ImportedFoo = Import(FromFoo, Lang_CXX);
  3447. EXPECT_EQ(ImportedFoo, ToFoo);
  3448. }
  3449. struct DeclContextTest : ASTImporterOptionSpecificTestBase {};
  3450. TEST_P(DeclContextTest, removeDeclOfClassTemplateSpecialization) {
  3451. Decl *TU = getTuDecl(
  3452. R"(
  3453. namespace NS {
  3454. template <typename T>
  3455. struct S {};
  3456. template struct S<int>;
  3457. inline namespace INS {
  3458. template <typename T>
  3459. struct S {};
  3460. template struct S<int>;
  3461. }
  3462. }
  3463. )", Lang_CXX11, "input0.cc");
  3464. auto *NS = FirstDeclMatcher<NamespaceDecl>().match(
  3465. TU, namespaceDecl());
  3466. auto *Spec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  3467. TU, classTemplateSpecializationDecl());
  3468. ASSERT_TRUE(NS->containsDecl(Spec));
  3469. NS->removeDecl(Spec);
  3470. EXPECT_FALSE(NS->containsDecl(Spec));
  3471. }
  3472. TEST_P(DeclContextTest,
  3473. removeDeclShouldNotFailEvenIfWeHaveExternalVisibleStorage) {
  3474. Decl *TU = getTuDecl("extern int A; int A;", Lang_CXX);
  3475. auto *A0 = FirstDeclMatcher<VarDecl>().match(TU, varDecl(hasName("A")));
  3476. auto *A1 = LastDeclMatcher<VarDecl>().match(TU, varDecl(hasName("A")));
  3477. // Investigate the list.
  3478. auto *DC = A0->getDeclContext();
  3479. ASSERT_TRUE(DC->containsDecl(A0));
  3480. ASSERT_TRUE(DC->containsDecl(A1));
  3481. // Investigate the lookup table.
  3482. auto *Map = DC->getLookupPtr();
  3483. ASSERT_TRUE(Map);
  3484. auto I = Map->find(A0->getDeclName());
  3485. ASSERT_NE(I, Map->end());
  3486. StoredDeclsList &L = I->second;
  3487. // The lookup table contains the most recent decl of A.
  3488. ASSERT_NE(L.getAsDecl(), A0);
  3489. ASSERT_EQ(L.getAsDecl(), A1);
  3490. ASSERT_TRUE(L.getAsDecl());
  3491. // Simulate the private function DeclContext::reconcileExternalVisibleStorage.
  3492. // The point here is to have a Vec with only one element, which is not the
  3493. // one we are going to delete from the DC later.
  3494. L.setHasExternalDecls();
  3495. ASSERT_TRUE(L.getAsVector());
  3496. ASSERT_EQ(1u, L.getAsVector()->size());
  3497. // This asserts in the old implementation.
  3498. DC->removeDecl(A0);
  3499. EXPECT_FALSE(DC->containsDecl(A0));
  3500. }
  3501. struct ImportFunctionTemplateSpecializations
  3502. : ASTImporterOptionSpecificTestBase {};
  3503. TEST_P(ImportFunctionTemplateSpecializations,
  3504. TUshouldNotContainFunctionTemplateImplicitInstantiation) {
  3505. Decl *FromTU = getTuDecl(
  3506. R"(
  3507. template<class T>
  3508. int f() { return 0; }
  3509. void foo() { f<int>(); }
  3510. )",
  3511. Lang_CXX, "input0.cc");
  3512. // Check that the function template instantiation is NOT the child of the TU.
  3513. auto Pattern = translationUnitDecl(
  3514. unless(has(functionDecl(hasName("f"), isTemplateInstantiation()))));
  3515. ASSERT_TRUE(MatchVerifier<Decl>{}.match(FromTU, Pattern));
  3516. auto *Foo = FirstDeclMatcher<FunctionDecl>().match(
  3517. FromTU, functionDecl(hasName("foo")));
  3518. ASSERT_TRUE(Import(Foo, Lang_CXX));
  3519. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  3520. EXPECT_TRUE(MatchVerifier<Decl>{}.match(ToTU, Pattern));
  3521. }
  3522. TEST_P(ImportFunctionTemplateSpecializations,
  3523. TUshouldNotContainFunctionTemplateExplicitInstantiation) {
  3524. Decl *FromTU = getTuDecl(
  3525. R"(
  3526. template<class T>
  3527. int f() { return 0; }
  3528. template int f<int>();
  3529. )",
  3530. Lang_CXX, "input0.cc");
  3531. // Check that the function template instantiation is NOT the child of the TU.
  3532. auto Instantiation = functionDecl(hasName("f"), isTemplateInstantiation());
  3533. auto Pattern = translationUnitDecl(unless(has(Instantiation)));
  3534. ASSERT_TRUE(MatchVerifier<Decl>{}.match(FromTU, Pattern));
  3535. ASSERT_TRUE(
  3536. Import(FirstDeclMatcher<Decl>().match(FromTU, Instantiation), Lang_CXX));
  3537. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  3538. EXPECT_TRUE(MatchVerifier<Decl>{}.match(ToTU, Pattern));
  3539. }
  3540. TEST_P(ImportFunctionTemplateSpecializations,
  3541. TUshouldContainFunctionTemplateSpecialization) {
  3542. Decl *FromTU = getTuDecl(
  3543. R"(
  3544. template<class T>
  3545. int f() { return 0; }
  3546. template <> int f<int>() { return 4; }
  3547. )",
  3548. Lang_CXX, "input0.cc");
  3549. // Check that the function template specialization is the child of the TU.
  3550. auto Specialization =
  3551. functionDecl(hasName("f"), isExplicitTemplateSpecialization());
  3552. auto Pattern = translationUnitDecl(has(Specialization));
  3553. ASSERT_TRUE(MatchVerifier<Decl>{}.match(FromTU, Pattern));
  3554. ASSERT_TRUE(
  3555. Import(FirstDeclMatcher<Decl>().match(FromTU, Specialization), Lang_CXX));
  3556. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  3557. EXPECT_TRUE(MatchVerifier<Decl>{}.match(ToTU, Pattern));
  3558. }
  3559. TEST_P(ImportFunctionTemplateSpecializations,
  3560. FunctionTemplateSpecializationRedeclChain) {
  3561. Decl *FromTU = getTuDecl(
  3562. R"(
  3563. template<class T>
  3564. int f() { return 0; }
  3565. template <> int f<int>() { return 4; }
  3566. )",
  3567. Lang_CXX, "input0.cc");
  3568. auto Spec = functionDecl(hasName("f"), isExplicitTemplateSpecialization(),
  3569. hasParent(translationUnitDecl()));
  3570. auto *FromSpecD = FirstDeclMatcher<Decl>().match(FromTU, Spec);
  3571. {
  3572. auto *TU = FromTU;
  3573. auto *SpecD = FromSpecD;
  3574. auto *TemplateD = FirstDeclMatcher<FunctionTemplateDecl>().match(
  3575. TU, functionTemplateDecl());
  3576. auto *FirstSpecD = *(TemplateD->spec_begin());
  3577. ASSERT_EQ(SpecD, FirstSpecD);
  3578. ASSERT_TRUE(SpecD->getPreviousDecl());
  3579. ASSERT_FALSE(cast<FunctionDecl>(SpecD->getPreviousDecl())
  3580. ->doesThisDeclarationHaveABody());
  3581. }
  3582. ASSERT_TRUE(Import(FromSpecD, Lang_CXX));
  3583. {
  3584. auto *TU = ToAST->getASTContext().getTranslationUnitDecl();
  3585. auto *SpecD = FirstDeclMatcher<Decl>().match(TU, Spec);
  3586. auto *TemplateD = FirstDeclMatcher<FunctionTemplateDecl>().match(
  3587. TU, functionTemplateDecl());
  3588. auto *FirstSpecD = *(TemplateD->spec_begin());
  3589. EXPECT_EQ(SpecD, FirstSpecD);
  3590. ASSERT_TRUE(SpecD->getPreviousDecl());
  3591. EXPECT_FALSE(cast<FunctionDecl>(SpecD->getPreviousDecl())
  3592. ->doesThisDeclarationHaveABody());
  3593. }
  3594. }
  3595. TEST_P(ImportFunctionTemplateSpecializations,
  3596. MatchNumberOfFunctionTemplateSpecializations) {
  3597. Decl *FromTU = getTuDecl(
  3598. R"(
  3599. template <typename T> constexpr int f() { return 0; }
  3600. template <> constexpr int f<int>() { return 4; }
  3601. void foo() {
  3602. static_assert(f<char>() == 0, "");
  3603. static_assert(f<int>() == 4, "");
  3604. }
  3605. )",
  3606. Lang_CXX11, "input0.cc");
  3607. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  3608. FromTU, functionDecl(hasName("foo")));
  3609. Import(FromD, Lang_CXX11);
  3610. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  3611. EXPECT_EQ(
  3612. DeclCounter<FunctionDecl>().match(FromTU, functionDecl(hasName("f"))),
  3613. DeclCounter<FunctionDecl>().match(ToTU, functionDecl(hasName("f"))));
  3614. }
  3615. TEST_P(ASTImporterOptionSpecificTestBase,
  3616. ImportShouldNotReportFalseODRErrorWhenRecordIsBeingDefined) {
  3617. {
  3618. Decl *FromTU = getTuDecl(
  3619. R"(
  3620. template <typename T>
  3621. struct B;
  3622. )",
  3623. Lang_CXX, "input0.cc");
  3624. auto *FromD = FirstDeclMatcher<ClassTemplateDecl>().match(
  3625. FromTU, classTemplateDecl(hasName("B")));
  3626. Import(FromD, Lang_CXX);
  3627. }
  3628. {
  3629. Decl *FromTU = getTuDecl(
  3630. R"(
  3631. template <typename T>
  3632. struct B {
  3633. void f();
  3634. B* b;
  3635. };
  3636. )",
  3637. Lang_CXX, "input1.cc");
  3638. FunctionDecl *FromD = FirstDeclMatcher<FunctionDecl>().match(
  3639. FromTU, functionDecl(hasName("f")));
  3640. Import(FromD, Lang_CXX);
  3641. auto *FromCTD = FirstDeclMatcher<ClassTemplateDecl>().match(
  3642. FromTU, classTemplateDecl(hasName("B")));
  3643. auto *ToCTD = cast<ClassTemplateDecl>(Import(FromCTD, Lang_CXX));
  3644. EXPECT_TRUE(ToCTD->isThisDeclarationADefinition());
  3645. // We expect no (ODR) warning during the import.
  3646. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  3647. EXPECT_EQ(0u, ToTU->getASTContext().getDiagnostics().getNumWarnings());
  3648. }
  3649. }
  3650. TEST_P(ASTImporterOptionSpecificTestBase,
  3651. ImportingTypedefShouldImportTheCompleteType) {
  3652. // We already have an incomplete underlying type in the "To" context.
  3653. auto Code =
  3654. R"(
  3655. template <typename T>
  3656. struct S {
  3657. void foo();
  3658. };
  3659. using U = S<int>;
  3660. )";
  3661. Decl *ToTU = getToTuDecl(Code, Lang_CXX11);
  3662. auto *ToD = FirstDeclMatcher<TypedefNameDecl>().match(ToTU,
  3663. typedefNameDecl(hasName("U")));
  3664. ASSERT_TRUE(ToD->getUnderlyingType()->isIncompleteType());
  3665. // The "From" context has the same typedef, but the underlying type is
  3666. // complete this time.
  3667. Decl *FromTU = getTuDecl(std::string(Code) +
  3668. R"(
  3669. void foo(U* u) {
  3670. u->foo();
  3671. }
  3672. )", Lang_CXX11);
  3673. auto *FromD = FirstDeclMatcher<TypedefNameDecl>().match(FromTU,
  3674. typedefNameDecl(hasName("U")));
  3675. ASSERT_FALSE(FromD->getUnderlyingType()->isIncompleteType());
  3676. // The imported type should be complete.
  3677. auto *ImportedD = cast<TypedefNameDecl>(Import(FromD, Lang_CXX11));
  3678. EXPECT_FALSE(ImportedD->getUnderlyingType()->isIncompleteType());
  3679. }
  3680. TEST_P(ASTImporterOptionSpecificTestBase, ImportTemplateParameterLists) {
  3681. auto Code =
  3682. R"(
  3683. template<class T>
  3684. int f() { return 0; }
  3685. template <> int f<int>() { return 4; }
  3686. )";
  3687. Decl *FromTU = getTuDecl(Code, Lang_CXX);
  3688. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU,
  3689. functionDecl(hasName("f"), isExplicitTemplateSpecialization()));
  3690. ASSERT_EQ(FromD->getNumTemplateParameterLists(), 1u);
  3691. auto *ToD = Import(FromD, Lang_CXX);
  3692. // The template parameter list should exist.
  3693. EXPECT_EQ(ToD->getNumTemplateParameterLists(), 1u);
  3694. }
  3695. struct ASTImporterLookupTableTest : ASTImporterOptionSpecificTestBase {};
  3696. TEST_P(ASTImporterLookupTableTest, OneDecl) {
  3697. auto *ToTU = getToTuDecl("int a;", Lang_CXX);
  3698. auto *D = FirstDeclMatcher<VarDecl>().match(ToTU, varDecl(hasName("a")));
  3699. ASTImporterLookupTable LT(*ToTU);
  3700. auto Res = LT.lookup(ToTU, D->getDeclName());
  3701. ASSERT_EQ(Res.size(), 1u);
  3702. EXPECT_EQ(*Res.begin(), D);
  3703. }
  3704. static Decl *findInDeclListOfDC(DeclContext *DC, DeclarationName Name) {
  3705. for (Decl *D : DC->decls()) {
  3706. if (auto *ND = dyn_cast<NamedDecl>(D))
  3707. if (ND->getDeclName() == Name)
  3708. return ND;
  3709. }
  3710. return nullptr;
  3711. }
  3712. TEST_P(ASTImporterLookupTableTest,
  3713. FriendWhichIsnotFoundByNormalLookupShouldBeFoundByImporterSpecificLookup) {
  3714. auto *Code = R"(
  3715. template <class T>
  3716. struct X {
  3717. friend void foo(){}
  3718. };
  3719. )";
  3720. TranslationUnitDecl *ToTU = getToTuDecl(Code, Lang_CXX);
  3721. auto *X = FirstDeclMatcher<ClassTemplateDecl>().match(
  3722. ToTU, classTemplateDecl(hasName("X")));
  3723. auto *Foo = FirstDeclMatcher<FunctionDecl>().match(
  3724. ToTU, functionDecl(hasName("foo")));
  3725. DeclContext *FooDC = Foo->getDeclContext();
  3726. DeclContext *FooLexicalDC = Foo->getLexicalDeclContext();
  3727. ASSERT_EQ(cast<Decl>(FooLexicalDC), X->getTemplatedDecl());
  3728. ASSERT_EQ(cast<Decl>(FooDC), ToTU);
  3729. DeclarationName FooName = Foo->getDeclName();
  3730. // Cannot find in the LookupTable of its DC (TUDecl)
  3731. SmallVector<NamedDecl *, 2> FoundDecls;
  3732. FooDC->getRedeclContext()->localUncachedLookup(FooName, FoundDecls);
  3733. EXPECT_EQ(FoundDecls.size(), 0u);
  3734. // Cannot find in the LookupTable of its LexicalDC (X)
  3735. FooLexicalDC->getRedeclContext()->localUncachedLookup(FooName, FoundDecls);
  3736. EXPECT_EQ(FoundDecls.size(), 0u);
  3737. // Can't find in the list of Decls of the DC.
  3738. EXPECT_EQ(findInDeclListOfDC(FooDC, FooName), nullptr);
  3739. // Can't find in the list of Decls of the LexicalDC
  3740. EXPECT_EQ(findInDeclListOfDC(FooLexicalDC, FooName), nullptr);
  3741. // ASTImporter specific lookup finds it.
  3742. ASTImporterLookupTable LT(*ToTU);
  3743. auto Res = LT.lookup(FooDC, Foo->getDeclName());
  3744. ASSERT_EQ(Res.size(), 1u);
  3745. EXPECT_EQ(*Res.begin(), Foo);
  3746. }
  3747. TEST_P(ASTImporterLookupTableTest,
  3748. FwdDeclStructShouldBeFoundByImporterSpecificLookup) {
  3749. TranslationUnitDecl *ToTU =
  3750. getToTuDecl("struct A { struct Foo *p; };", Lang_C);
  3751. auto *Foo =
  3752. FirstDeclMatcher<RecordDecl>().match(ToTU, recordDecl(hasName("Foo")));
  3753. auto *A =
  3754. FirstDeclMatcher<RecordDecl>().match(ToTU, recordDecl(hasName("A")));
  3755. DeclContext *FooDC = Foo->getDeclContext();
  3756. DeclContext *FooLexicalDC = Foo->getLexicalDeclContext();
  3757. ASSERT_EQ(cast<Decl>(FooLexicalDC), A);
  3758. ASSERT_EQ(cast<Decl>(FooDC), ToTU);
  3759. DeclarationName FooName = Foo->getDeclName();
  3760. // Cannot find in the LookupTable of its DC (TUDecl).
  3761. SmallVector<NamedDecl *, 2> FoundDecls;
  3762. FooDC->getRedeclContext()->localUncachedLookup(FooName, FoundDecls);
  3763. EXPECT_EQ(FoundDecls.size(), 0u);
  3764. // Cannot find in the LookupTable of its LexicalDC (A).
  3765. FooLexicalDC->getRedeclContext()->localUncachedLookup(FooName, FoundDecls);
  3766. EXPECT_EQ(FoundDecls.size(), 0u);
  3767. // Can't find in the list of Decls of the DC.
  3768. EXPECT_EQ(findInDeclListOfDC(FooDC, FooName), nullptr);
  3769. // Can find in the list of Decls of the LexicalDC.
  3770. EXPECT_EQ(findInDeclListOfDC(FooLexicalDC, FooName), Foo);
  3771. // ASTImporter specific lookup finds it.
  3772. ASTImporterLookupTable LT(*ToTU);
  3773. auto Res = LT.lookup(FooDC, Foo->getDeclName());
  3774. ASSERT_EQ(Res.size(), 1u);
  3775. EXPECT_EQ(*Res.begin(), Foo);
  3776. }
  3777. TEST_P(ASTImporterLookupTableTest, LookupFindsNamesInDifferentDC) {
  3778. TranslationUnitDecl *ToTU =
  3779. getToTuDecl("int V; struct A { int V; }; struct B { int V; };", Lang_C);
  3780. DeclarationName VName = FirstDeclMatcher<VarDecl>()
  3781. .match(ToTU, varDecl(hasName("V")))
  3782. ->getDeclName();
  3783. auto *A =
  3784. FirstDeclMatcher<RecordDecl>().match(ToTU, recordDecl(hasName("A")));
  3785. auto *B =
  3786. FirstDeclMatcher<RecordDecl>().match(ToTU, recordDecl(hasName("B")));
  3787. ASTImporterLookupTable LT(*ToTU);
  3788. auto Res = LT.lookup(cast<DeclContext>(A), VName);
  3789. ASSERT_EQ(Res.size(), 1u);
  3790. EXPECT_EQ(*Res.begin(), FirstDeclMatcher<FieldDecl>().match(
  3791. ToTU, fieldDecl(hasName("V"),
  3792. hasParent(recordDecl(hasName("A"))))));
  3793. Res = LT.lookup(cast<DeclContext>(B), VName);
  3794. ASSERT_EQ(Res.size(), 1u);
  3795. EXPECT_EQ(*Res.begin(), FirstDeclMatcher<FieldDecl>().match(
  3796. ToTU, fieldDecl(hasName("V"),
  3797. hasParent(recordDecl(hasName("B"))))));
  3798. Res = LT.lookup(ToTU, VName);
  3799. ASSERT_EQ(Res.size(), 1u);
  3800. EXPECT_EQ(*Res.begin(), FirstDeclMatcher<VarDecl>().match(
  3801. ToTU, varDecl(hasName("V"),
  3802. hasParent(translationUnitDecl()))));
  3803. }
  3804. TEST_P(ASTImporterLookupTableTest, LookupFindsOverloadedNames) {
  3805. TranslationUnitDecl *ToTU = getToTuDecl(
  3806. R"(
  3807. void foo();
  3808. void foo(int);
  3809. void foo(int, int);
  3810. )",
  3811. Lang_CXX);
  3812. ASTImporterLookupTable LT(*ToTU);
  3813. auto *F0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, functionDecl());
  3814. auto *F2 = LastDeclMatcher<FunctionDecl>().match(ToTU, functionDecl());
  3815. DeclarationName Name = F0->getDeclName();
  3816. auto Res = LT.lookup(ToTU, Name);
  3817. EXPECT_EQ(Res.size(), 3u);
  3818. EXPECT_EQ(Res.count(F0), 1u);
  3819. EXPECT_EQ(Res.count(F2), 1u);
  3820. }
  3821. TEST_P(ASTImporterLookupTableTest,
  3822. DifferentOperatorsShouldHaveDifferentResultSet) {
  3823. TranslationUnitDecl *ToTU = getToTuDecl(
  3824. R"(
  3825. struct X{};
  3826. void operator+(X, X);
  3827. void operator-(X, X);
  3828. )",
  3829. Lang_CXX);
  3830. ASTImporterLookupTable LT(*ToTU);
  3831. auto *FPlus = FirstDeclMatcher<FunctionDecl>().match(
  3832. ToTU, functionDecl(hasOverloadedOperatorName("+")));
  3833. auto *FMinus = FirstDeclMatcher<FunctionDecl>().match(
  3834. ToTU, functionDecl(hasOverloadedOperatorName("-")));
  3835. DeclarationName NamePlus = FPlus->getDeclName();
  3836. auto ResPlus = LT.lookup(ToTU, NamePlus);
  3837. EXPECT_EQ(ResPlus.size(), 1u);
  3838. EXPECT_EQ(ResPlus.count(FPlus), 1u);
  3839. EXPECT_EQ(ResPlus.count(FMinus), 0u);
  3840. DeclarationName NameMinus = FMinus->getDeclName();
  3841. auto ResMinus = LT.lookup(ToTU, NameMinus);
  3842. EXPECT_EQ(ResMinus.size(), 1u);
  3843. EXPECT_EQ(ResMinus.count(FMinus), 1u);
  3844. EXPECT_EQ(ResMinus.count(FPlus), 0u);
  3845. EXPECT_NE(*ResMinus.begin(), *ResPlus.begin());
  3846. }
  3847. TEST_P(ASTImporterLookupTableTest, LookupDeclNamesFromDifferentTUs) {
  3848. TranslationUnitDecl *ToTU = getToTuDecl(
  3849. R"(
  3850. struct X {};
  3851. void operator+(X, X);
  3852. )",
  3853. Lang_CXX);
  3854. auto *ToPlus = FirstDeclMatcher<FunctionDecl>().match(
  3855. ToTU, functionDecl(hasOverloadedOperatorName("+")));
  3856. Decl *FromTU = getTuDecl(
  3857. R"(
  3858. struct X {};
  3859. void operator+(X, X);
  3860. )",
  3861. Lang_CXX);
  3862. auto *FromPlus = FirstDeclMatcher<FunctionDecl>().match(
  3863. FromTU, functionDecl(hasOverloadedOperatorName("+")));
  3864. // FromPlus have a different TU, thus its DeclarationName is different too.
  3865. ASSERT_NE(ToPlus->getDeclName(), FromPlus->getDeclName());
  3866. ASTImporterLookupTable LT(*ToTU);
  3867. auto Res = LT.lookup(ToTU, ToPlus->getDeclName());
  3868. ASSERT_EQ(Res.size(), 1u);
  3869. EXPECT_EQ(*Res.begin(), ToPlus);
  3870. // FromPlus have a different TU, thus its DeclarationName is different too.
  3871. Res = LT.lookup(ToTU, FromPlus->getDeclName());
  3872. ASSERT_EQ(Res.size(), 0u);
  3873. }
  3874. static const RecordDecl *getRecordDeclOfFriend(FriendDecl *FD) {
  3875. QualType Ty = FD->getFriendType()->getType().getCanonicalType();
  3876. return cast<RecordType>(Ty)->getDecl();
  3877. }
  3878. TEST_P(ASTImporterLookupTableTest,
  3879. LookupFindsFwdFriendClassDeclWithElaboratedType) {
  3880. TranslationUnitDecl *ToTU = getToTuDecl(
  3881. R"(
  3882. class Y { friend class F; };
  3883. )",
  3884. Lang_CXX);
  3885. // In this case, the CXXRecordDecl is hidden, the FriendDecl is not a parent.
  3886. // So we must dig up the underlying CXXRecordDecl.
  3887. ASTImporterLookupTable LT(*ToTU);
  3888. auto *FriendD = FirstDeclMatcher<FriendDecl>().match(ToTU, friendDecl());
  3889. const RecordDecl *RD = getRecordDeclOfFriend(FriendD);
  3890. auto *Y = FirstDeclMatcher<CXXRecordDecl>().match(
  3891. ToTU, cxxRecordDecl(hasName("Y")));
  3892. DeclarationName Name = RD->getDeclName();
  3893. auto Res = LT.lookup(ToTU, Name);
  3894. EXPECT_EQ(Res.size(), 1u);
  3895. EXPECT_EQ(*Res.begin(), RD);
  3896. Res = LT.lookup(Y, Name);
  3897. EXPECT_EQ(Res.size(), 0u);
  3898. }
  3899. TEST_P(ASTImporterLookupTableTest,
  3900. LookupFindsFwdFriendClassDeclWithUnelaboratedType) {
  3901. TranslationUnitDecl *ToTU = getToTuDecl(
  3902. R"(
  3903. class F;
  3904. class Y { friend F; };
  3905. )",
  3906. Lang_CXX11);
  3907. // In this case, the CXXRecordDecl is hidden, the FriendDecl is not a parent.
  3908. // So we must dig up the underlying CXXRecordDecl.
  3909. ASTImporterLookupTable LT(*ToTU);
  3910. auto *FriendD = FirstDeclMatcher<FriendDecl>().match(ToTU, friendDecl());
  3911. const RecordDecl *RD = getRecordDeclOfFriend(FriendD);
  3912. auto *Y = FirstDeclMatcher<CXXRecordDecl>().match(ToTU, cxxRecordDecl(hasName("Y")));
  3913. DeclarationName Name = RD->getDeclName();
  3914. auto Res = LT.lookup(ToTU, Name);
  3915. EXPECT_EQ(Res.size(), 1u);
  3916. EXPECT_EQ(*Res.begin(), RD);
  3917. Res = LT.lookup(Y, Name);
  3918. EXPECT_EQ(Res.size(), 0u);
  3919. }
  3920. TEST_P(ASTImporterLookupTableTest,
  3921. LookupFindsFriendClassDeclWithTypeAliasDoesNotAssert) {
  3922. TranslationUnitDecl *ToTU = getToTuDecl(
  3923. R"(
  3924. class F;
  3925. using alias_of_f = F;
  3926. class Y { friend alias_of_f; };
  3927. )",
  3928. Lang_CXX11);
  3929. // ASTImporterLookupTable constructor handles using declarations correctly,
  3930. // no assert is expected.
  3931. ASTImporterLookupTable LT(*ToTU);
  3932. auto *Alias = FirstDeclMatcher<TypeAliasDecl>().match(
  3933. ToTU, typeAliasDecl(hasName("alias_of_f")));
  3934. DeclarationName Name = Alias->getDeclName();
  3935. auto Res = LT.lookup(ToTU, Name);
  3936. EXPECT_EQ(Res.count(Alias), 1u);
  3937. }
  3938. TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendClassTemplateDecl) {
  3939. TranslationUnitDecl *ToTU = getToTuDecl(
  3940. R"(
  3941. class Y { template <class T> friend class F; };
  3942. )",
  3943. Lang_CXX);
  3944. ASTImporterLookupTable LT(*ToTU);
  3945. auto *F = FirstDeclMatcher<ClassTemplateDecl>().match(
  3946. ToTU, classTemplateDecl(hasName("F")));
  3947. DeclarationName Name = F->getDeclName();
  3948. auto Res = LT.lookup(ToTU, Name);
  3949. EXPECT_EQ(Res.size(), 2u);
  3950. EXPECT_EQ(Res.count(F), 1u);
  3951. EXPECT_EQ(Res.count(F->getTemplatedDecl()), 1u);
  3952. }
  3953. TEST_P(ASTImporterLookupTableTest, DependentFriendClass) {
  3954. TranslationUnitDecl *ToTU = getToTuDecl(
  3955. R"(
  3956. template <typename T>
  3957. class F;
  3958. template <typename T>
  3959. class Y {
  3960. friend class F<T>;
  3961. };
  3962. )",
  3963. Lang_CXX);
  3964. ASTImporterLookupTable LT(*ToTU);
  3965. auto *F = FirstDeclMatcher<ClassTemplateDecl>().match(
  3966. ToTU, classTemplateDecl(hasName("F")));
  3967. DeclarationName Name = F->getDeclName();
  3968. auto Res = LT.lookup(ToTU, Name);
  3969. EXPECT_EQ(Res.size(), 2u);
  3970. EXPECT_EQ(Res.count(F), 1u);
  3971. EXPECT_EQ(Res.count(F->getTemplatedDecl()), 1u);
  3972. }
  3973. TEST_P(ASTImporterLookupTableTest, FriendClassTemplateSpecialization) {
  3974. TranslationUnitDecl *ToTU = getToTuDecl(
  3975. R"(
  3976. template <typename T>
  3977. class F;
  3978. class Y {
  3979. friend class F<int>;
  3980. };
  3981. )",
  3982. Lang_CXX);
  3983. ASTImporterLookupTable LT(*ToTU);
  3984. auto *F = FirstDeclMatcher<ClassTemplateDecl>().match(
  3985. ToTU, classTemplateDecl(hasName("F")));
  3986. DeclarationName Name = F->getDeclName();
  3987. auto Res = LT.lookup(ToTU, Name);
  3988. ASSERT_EQ(Res.size(), 3u);
  3989. EXPECT_EQ(Res.count(F), 1u);
  3990. EXPECT_EQ(Res.count(F->getTemplatedDecl()), 1u);
  3991. EXPECT_EQ(Res.count(*F->spec_begin()), 1u);
  3992. }
  3993. TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendFunctionDecl) {
  3994. TranslationUnitDecl *ToTU = getToTuDecl(
  3995. R"(
  3996. class Y { friend void F(); };
  3997. )",
  3998. Lang_CXX);
  3999. ASTImporterLookupTable LT(*ToTU);
  4000. auto *F =
  4001. FirstDeclMatcher<FunctionDecl>().match(ToTU, functionDecl(hasName("F")));
  4002. DeclarationName Name = F->getDeclName();
  4003. auto Res = LT.lookup(ToTU, Name);
  4004. EXPECT_EQ(Res.size(), 1u);
  4005. EXPECT_EQ(*Res.begin(), F);
  4006. }
  4007. TEST_P(ASTImporterLookupTableTest,
  4008. LookupFindsDeclsInClassTemplateSpecialization) {
  4009. TranslationUnitDecl *ToTU = getToTuDecl(
  4010. R"(
  4011. template <typename T>
  4012. struct X {
  4013. int F;
  4014. };
  4015. void foo() {
  4016. X<char> xc;
  4017. }
  4018. )",
  4019. Lang_CXX);
  4020. ASTImporterLookupTable LT(*ToTU);
  4021. auto *Template = FirstDeclMatcher<ClassTemplateDecl>().match(
  4022. ToTU, classTemplateDecl(hasName("X")));
  4023. auto *FieldInTemplate = FirstDeclMatcher<FieldDecl>().match(
  4024. ToTU,
  4025. fieldDecl(hasParent(cxxRecordDecl(hasParent(classTemplateDecl())))));
  4026. auto *Spec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  4027. ToTU, classTemplateSpecializationDecl(hasName("X")));
  4028. FieldDecl *FieldInSpec = *Spec->field_begin();
  4029. ASSERT_TRUE(FieldInSpec);
  4030. DeclarationName Name = FieldInSpec->getDeclName();
  4031. auto TemplateDC = cast<DeclContext>(Template->getTemplatedDecl());
  4032. SmallVector<NamedDecl *, 2> FoundDecls;
  4033. TemplateDC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  4034. EXPECT_EQ(FoundDecls.size(), 1u);
  4035. EXPECT_EQ(FoundDecls[0], FieldInTemplate);
  4036. auto Res = LT.lookup(TemplateDC, Name);
  4037. ASSERT_EQ(Res.size(), 1u);
  4038. EXPECT_EQ(*Res.begin(), FieldInTemplate);
  4039. cast<DeclContext>(Spec)->getRedeclContext()->localUncachedLookup(Name,
  4040. FoundDecls);
  4041. EXPECT_EQ(FoundDecls.size(), 1u);
  4042. EXPECT_EQ(FoundDecls[0], FieldInSpec);
  4043. Res = LT.lookup(cast<DeclContext>(Spec), Name);
  4044. ASSERT_EQ(Res.size(), 1u);
  4045. EXPECT_EQ(*Res.begin(), FieldInSpec);
  4046. }
  4047. TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendFunctionTemplateDecl) {
  4048. TranslationUnitDecl *ToTU = getToTuDecl(
  4049. R"(
  4050. class Y { template <class T> friend void F(); };
  4051. )",
  4052. Lang_CXX);
  4053. ASTImporterLookupTable LT(*ToTU);
  4054. auto *F = FirstDeclMatcher<FunctionTemplateDecl>().match(
  4055. ToTU, functionTemplateDecl(hasName("F")));
  4056. DeclarationName Name = F->getDeclName();
  4057. auto Res = LT.lookup(ToTU, Name);
  4058. EXPECT_EQ(Res.size(), 2u);
  4059. EXPECT_EQ(Res.count(F), 1u);
  4060. EXPECT_EQ(Res.count(F->getTemplatedDecl()), 1u);
  4061. }
  4062. TEST_P(ASTImporterLookupTableTest, MultipleBefriendingClasses) {
  4063. TranslationUnitDecl *ToTU = getToTuDecl(
  4064. R"(
  4065. struct X;
  4066. struct A {
  4067. friend struct X;
  4068. };
  4069. struct B {
  4070. friend struct X;
  4071. };
  4072. )",
  4073. Lang_CXX);
  4074. ASTImporterLookupTable LT(*ToTU);
  4075. auto *X = FirstDeclMatcher<CXXRecordDecl>().match(
  4076. ToTU, cxxRecordDecl(hasName("X")));
  4077. auto *FriendD0 = FirstDeclMatcher<FriendDecl>().match(ToTU, friendDecl());
  4078. auto *FriendD1 = LastDeclMatcher<FriendDecl>().match(ToTU, friendDecl());
  4079. const RecordDecl *RD0 = getRecordDeclOfFriend(FriendD0);
  4080. const RecordDecl *RD1 = getRecordDeclOfFriend(FriendD1);
  4081. ASSERT_EQ(RD0, RD1);
  4082. ASSERT_EQ(RD1, X);
  4083. DeclarationName Name = X->getDeclName();
  4084. auto Res = LT.lookup(ToTU, Name);
  4085. EXPECT_EQ(Res.size(), 1u);
  4086. EXPECT_EQ(*Res.begin(), X);
  4087. }
  4088. TEST_P(ASTImporterLookupTableTest, EnumConstantDecl) {
  4089. TranslationUnitDecl *ToTU = getToTuDecl(
  4090. R"(
  4091. enum E {
  4092. A,
  4093. B
  4094. };
  4095. )",
  4096. Lang_C);
  4097. ASTImporterLookupTable LT(*ToTU);
  4098. auto *E = FirstDeclMatcher<EnumDecl>().match(ToTU, enumDecl(hasName("E")));
  4099. auto *A = FirstDeclMatcher<EnumConstantDecl>().match(
  4100. ToTU, enumConstantDecl(hasName("A")));
  4101. DeclarationName Name = A->getDeclName();
  4102. // Redecl context is the TU.
  4103. ASSERT_EQ(E->getRedeclContext(), ToTU);
  4104. SmallVector<NamedDecl *, 2> FoundDecls;
  4105. // Normal lookup finds in the DC.
  4106. E->localUncachedLookup(Name, FoundDecls);
  4107. EXPECT_EQ(FoundDecls.size(), 1u);
  4108. // Normal lookup finds in the Redecl context.
  4109. ToTU->localUncachedLookup(Name, FoundDecls);
  4110. EXPECT_EQ(FoundDecls.size(), 1u);
  4111. // Import specific lookup finds in the DC.
  4112. auto Res = LT.lookup(E, Name);
  4113. ASSERT_EQ(Res.size(), 1u);
  4114. EXPECT_EQ(*Res.begin(), A);
  4115. // Import specific lookup finds in the Redecl context.
  4116. Res = LT.lookup(ToTU, Name);
  4117. ASSERT_EQ(Res.size(), 1u);
  4118. EXPECT_EQ(*Res.begin(), A);
  4119. }
  4120. TEST_P(ASTImporterLookupTableTest, LookupSearchesInTheWholeRedeclChain) {
  4121. TranslationUnitDecl *ToTU = getToTuDecl(
  4122. R"(
  4123. namespace N {
  4124. int A;
  4125. }
  4126. namespace N {
  4127. }
  4128. )",
  4129. Lang_CXX);
  4130. auto *N1 =
  4131. LastDeclMatcher<NamespaceDecl>().match(ToTU, namespaceDecl(hasName("N")));
  4132. auto *A = FirstDeclMatcher<VarDecl>().match(ToTU, varDecl(hasName("A")));
  4133. DeclarationName Name = A->getDeclName();
  4134. ASTImporterLookupTable LT(*ToTU);
  4135. auto Res = LT.lookup(N1, Name);
  4136. ASSERT_EQ(Res.size(), 1u);
  4137. EXPECT_EQ(*Res.begin(), A);
  4138. }
  4139. // FIXME This test is disabled currently, upcoming patches will make it
  4140. // possible to enable.
  4141. TEST_P(ASTImporterOptionSpecificTestBase,
  4142. DISABLED_RedeclChainShouldBeCorrectAmongstNamespaces) {
  4143. Decl *FromTU = getTuDecl(
  4144. R"(
  4145. namespace NS {
  4146. struct X;
  4147. struct Y {
  4148. static const int I = 3;
  4149. };
  4150. }
  4151. namespace NS {
  4152. struct X { // <--- To be imported
  4153. void method(int i = Y::I) {}
  4154. int f;
  4155. };
  4156. }
  4157. )",
  4158. Lang_CXX);
  4159. auto *FromFwd = FirstDeclMatcher<CXXRecordDecl>().match(
  4160. FromTU, cxxRecordDecl(hasName("X"), unless(isImplicit())));
  4161. auto *FromDef = LastDeclMatcher<CXXRecordDecl>().match(
  4162. FromTU,
  4163. cxxRecordDecl(hasName("X"), isDefinition(), unless(isImplicit())));
  4164. ASSERT_NE(FromFwd, FromDef);
  4165. ASSERT_FALSE(FromFwd->isThisDeclarationADefinition());
  4166. ASSERT_TRUE(FromDef->isThisDeclarationADefinition());
  4167. ASSERT_EQ(FromFwd->getCanonicalDecl(), FromDef->getCanonicalDecl());
  4168. auto *ToDef = cast_or_null<CXXRecordDecl>(Import(FromDef, Lang_CXX));
  4169. auto *ToFwd = cast_or_null<CXXRecordDecl>(Import(FromFwd, Lang_CXX));
  4170. EXPECT_NE(ToFwd, ToDef);
  4171. EXPECT_FALSE(ToFwd->isThisDeclarationADefinition());
  4172. EXPECT_TRUE(ToDef->isThisDeclarationADefinition());
  4173. EXPECT_EQ(ToFwd->getCanonicalDecl(), ToDef->getCanonicalDecl());
  4174. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  4175. // We expect no (ODR) warning during the import.
  4176. EXPECT_EQ(0u, ToTU->getASTContext().getDiagnostics().getNumWarnings());
  4177. }
  4178. struct ImportFriendFunctionTemplates : ASTImporterOptionSpecificTestBase {};
  4179. TEST_P(ImportFriendFunctionTemplates, LookupShouldFindPreviousFriend) {
  4180. Decl *ToTU = getToTuDecl(
  4181. R"(
  4182. class X {
  4183. template <typename T> friend void foo();
  4184. };
  4185. )",
  4186. Lang_CXX);
  4187. auto *Friend = FirstDeclMatcher<FunctionTemplateDecl>().match(
  4188. ToTU, functionTemplateDecl(hasName("foo")));
  4189. Decl *FromTU = getTuDecl(
  4190. R"(
  4191. template <typename T> void foo();
  4192. )",
  4193. Lang_CXX);
  4194. auto *FromFoo = FirstDeclMatcher<FunctionTemplateDecl>().match(
  4195. FromTU, functionTemplateDecl(hasName("foo")));
  4196. auto *Imported = Import(FromFoo, Lang_CXX);
  4197. EXPECT_EQ(Imported->getPreviousDecl(), Friend);
  4198. }
  4199. struct ASTImporterWithFakeErrors : ASTImporter {
  4200. using ASTImporter::ASTImporter;
  4201. bool returnWithErrorInTest() override { return true; }
  4202. };
  4203. struct ErrorHandlingTest : ASTImporterOptionSpecificTestBase {
  4204. ErrorHandlingTest() {
  4205. Creator = [](ASTContext &ToContext, FileManager &ToFileManager,
  4206. ASTContext &FromContext, FileManager &FromFileManager,
  4207. bool MinimalImport,
  4208. const std::shared_ptr<ASTImporterSharedState> &SharedState) {
  4209. return new ASTImporterWithFakeErrors(ToContext, ToFileManager,
  4210. FromContext, FromFileManager,
  4211. MinimalImport, SharedState);
  4212. };
  4213. }
  4214. // In this test we purposely report an error (UnsupportedConstruct) when
  4215. // importing the below stmt.
  4216. static constexpr auto* ErroneousStmt = R"( asm(""); )";
  4217. };
  4218. // Check a case when no new AST node is created in the AST before encountering
  4219. // the error.
  4220. TEST_P(ErrorHandlingTest, ErrorHappensBeforeCreatingANewNode) {
  4221. TranslationUnitDecl *ToTU = getToTuDecl(
  4222. R"(
  4223. template <typename T>
  4224. class X {};
  4225. template <>
  4226. class X<int> { int a; };
  4227. )",
  4228. Lang_CXX);
  4229. TranslationUnitDecl *FromTU = getTuDecl(
  4230. R"(
  4231. template <typename T>
  4232. class X {};
  4233. template <>
  4234. class X<int> { double b; };
  4235. )",
  4236. Lang_CXX);
  4237. auto *FromSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  4238. FromTU, classTemplateSpecializationDecl(hasName("X")));
  4239. ClassTemplateSpecializationDecl *ImportedSpec = Import(FromSpec, Lang_CXX);
  4240. EXPECT_FALSE(ImportedSpec);
  4241. // The original Decl is kept, no new decl is created.
  4242. EXPECT_EQ(DeclCounter<ClassTemplateSpecializationDecl>().match(
  4243. ToTU, classTemplateSpecializationDecl(hasName("X"))),
  4244. 1u);
  4245. // But an error is set to the counterpart in the "from" context.
  4246. ASTImporter *Importer = findFromTU(FromSpec)->Importer.get();
  4247. Optional<ImportError> OptErr = Importer->getImportDeclErrorIfAny(FromSpec);
  4248. ASSERT_TRUE(OptErr);
  4249. EXPECT_EQ(OptErr->Error, ImportError::NameConflict);
  4250. }
  4251. // Check a case when a new AST node is created but not linked to the AST before
  4252. // encountering the error.
  4253. TEST_P(ErrorHandlingTest,
  4254. ErrorHappensAfterCreatingTheNodeButBeforeLinkingThatToTheAST) {
  4255. TranslationUnitDecl *FromTU = getTuDecl(
  4256. std::string("void foo() { ") + ErroneousStmt + " }",
  4257. Lang_CXX);
  4258. auto *FromFoo = FirstDeclMatcher<FunctionDecl>().match(
  4259. FromTU, functionDecl(hasName("foo")));
  4260. FunctionDecl *ImportedFoo = Import(FromFoo, Lang_CXX);
  4261. EXPECT_FALSE(ImportedFoo);
  4262. TranslationUnitDecl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  4263. // Created, but not linked.
  4264. EXPECT_EQ(
  4265. DeclCounter<FunctionDecl>().match(ToTU, functionDecl(hasName("foo"))),
  4266. 0u);
  4267. ASTImporter *Importer = findFromTU(FromFoo)->Importer.get();
  4268. Optional<ImportError> OptErr = Importer->getImportDeclErrorIfAny(FromFoo);
  4269. ASSERT_TRUE(OptErr);
  4270. EXPECT_EQ(OptErr->Error, ImportError::UnsupportedConstruct);
  4271. }
  4272. // Check a case when a new AST node is created and linked to the AST before
  4273. // encountering the error. The error is set for the counterpart of the nodes in
  4274. // the "from" context.
  4275. TEST_P(ErrorHandlingTest, ErrorHappensAfterNodeIsCreatedAndLinked) {
  4276. TranslationUnitDecl *FromTU = getTuDecl(
  4277. std::string(R"(
  4278. void f();
  4279. void f() { )") + ErroneousStmt + R"( }
  4280. )",
  4281. Lang_CXX);
  4282. auto *FromProto = FirstDeclMatcher<FunctionDecl>().match(
  4283. FromTU, functionDecl(hasName("f")));
  4284. auto *FromDef =
  4285. LastDeclMatcher<FunctionDecl>().match(FromTU, functionDecl(hasName("f")));
  4286. FunctionDecl *ImportedProto = Import(FromProto, Lang_CXX);
  4287. EXPECT_FALSE(ImportedProto); // Could not import.
  4288. // However, we created two nodes in the AST. 1) the fwd decl 2) the
  4289. // definition. The definition is not added to its DC, but the fwd decl is
  4290. // there.
  4291. TranslationUnitDecl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  4292. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, functionDecl(hasName("f"))),
  4293. 1u);
  4294. // Match the fwd decl.
  4295. auto *ToProto =
  4296. FirstDeclMatcher<FunctionDecl>().match(ToTU, functionDecl(hasName("f")));
  4297. EXPECT_TRUE(ToProto);
  4298. // An error is set to the counterpart in the "from" context both for the fwd
  4299. // decl and the definition.
  4300. ASTImporter *Importer = findFromTU(FromProto)->Importer.get();
  4301. Optional<ImportError> OptErr = Importer->getImportDeclErrorIfAny(FromProto);
  4302. ASSERT_TRUE(OptErr);
  4303. EXPECT_EQ(OptErr->Error, ImportError::UnsupportedConstruct);
  4304. OptErr = Importer->getImportDeclErrorIfAny(FromDef);
  4305. ASSERT_TRUE(OptErr);
  4306. EXPECT_EQ(OptErr->Error, ImportError::UnsupportedConstruct);
  4307. }
  4308. // An error should be set for a class if we cannot import one member.
  4309. TEST_P(ErrorHandlingTest, ErrorIsPropagatedFromMemberToClass) {
  4310. TranslationUnitDecl *FromTU = getTuDecl(
  4311. std::string(R"(
  4312. class X {
  4313. void f() { )") + ErroneousStmt + R"( } // This member has the error
  4314. // during import.
  4315. void ok(); // The error should not prevent importing this.
  4316. }; // An error will be set for X too.
  4317. )",
  4318. Lang_CXX);
  4319. auto *FromX = FirstDeclMatcher<CXXRecordDecl>().match(
  4320. FromTU, cxxRecordDecl(hasName("X")));
  4321. CXXRecordDecl *ImportedX = Import(FromX, Lang_CXX);
  4322. // An error is set for X.
  4323. EXPECT_FALSE(ImportedX);
  4324. ASTImporter *Importer = findFromTU(FromX)->Importer.get();
  4325. Optional<ImportError> OptErr = Importer->getImportDeclErrorIfAny(FromX);
  4326. ASSERT_TRUE(OptErr);
  4327. EXPECT_EQ(OptErr->Error, ImportError::UnsupportedConstruct);
  4328. // An error is set for f().
  4329. auto *FromF = FirstDeclMatcher<CXXMethodDecl>().match(
  4330. FromTU, cxxMethodDecl(hasName("f")));
  4331. OptErr = Importer->getImportDeclErrorIfAny(FromF);
  4332. ASSERT_TRUE(OptErr);
  4333. EXPECT_EQ(OptErr->Error, ImportError::UnsupportedConstruct);
  4334. // And any subsequent import should fail.
  4335. CXXMethodDecl *ImportedF = Import(FromF, Lang_CXX);
  4336. EXPECT_FALSE(ImportedF);
  4337. // There is an error set for the other member too.
  4338. auto *FromOK = FirstDeclMatcher<CXXMethodDecl>().match(
  4339. FromTU, cxxMethodDecl(hasName("ok")));
  4340. OptErr = Importer->getImportDeclErrorIfAny(FromOK);
  4341. EXPECT_TRUE(OptErr);
  4342. // Cannot import the other member.
  4343. CXXMethodDecl *ImportedOK = Import(FromOK, Lang_CXX);
  4344. EXPECT_FALSE(ImportedOK);
  4345. }
  4346. // Check that an error propagates to the dependent AST nodes.
  4347. // In the below code it means that an error in X should propagate to A.
  4348. // And even to F since the containing A is erroneous.
  4349. // And to all AST nodes which we visit during the import process which finally
  4350. // ends up in a failure (in the error() function).
  4351. TEST_P(ErrorHandlingTest, ErrorPropagatesThroughImportCycles) {
  4352. Decl *FromTU = getTuDecl(
  4353. std::string(R"(
  4354. namespace NS {
  4355. class A {
  4356. template <int I> class F {};
  4357. class X {
  4358. template <int I> friend class F;
  4359. void error() { )") + ErroneousStmt + R"( }
  4360. };
  4361. };
  4362. class B {};
  4363. } // NS
  4364. )",
  4365. Lang_CXX, "input0.cc");
  4366. auto *FromFRD = FirstDeclMatcher<CXXRecordDecl>().match(
  4367. FromTU, cxxRecordDecl(hasName("F"), isDefinition()));
  4368. auto *FromA = FirstDeclMatcher<CXXRecordDecl>().match(
  4369. FromTU, cxxRecordDecl(hasName("A"), isDefinition()));
  4370. auto *FromB = FirstDeclMatcher<CXXRecordDecl>().match(
  4371. FromTU, cxxRecordDecl(hasName("B"), isDefinition()));
  4372. auto *FromNS = FirstDeclMatcher<NamespaceDecl>().match(
  4373. FromTU, namespaceDecl(hasName("NS")));
  4374. // Start by importing the templated CXXRecordDecl of F.
  4375. // Import fails for that.
  4376. EXPECT_FALSE(Import(FromFRD, Lang_CXX));
  4377. // Import fails for A.
  4378. EXPECT_FALSE(Import(FromA, Lang_CXX));
  4379. // But we should be able to import the independent B.
  4380. EXPECT_TRUE(Import(FromB, Lang_CXX));
  4381. // And the namespace.
  4382. EXPECT_TRUE(Import(FromNS, Lang_CXX));
  4383. // An error is set to the templated CXXRecordDecl of F.
  4384. ASTImporter *Importer = findFromTU(FromFRD)->Importer.get();
  4385. Optional<ImportError> OptErr = Importer->getImportDeclErrorIfAny(FromFRD);
  4386. EXPECT_TRUE(OptErr);
  4387. // An error is set to A.
  4388. OptErr = Importer->getImportDeclErrorIfAny(FromA);
  4389. EXPECT_TRUE(OptErr);
  4390. // There is no error set to B.
  4391. OptErr = Importer->getImportDeclErrorIfAny(FromB);
  4392. EXPECT_FALSE(OptErr);
  4393. // There is no error set to NS.
  4394. OptErr = Importer->getImportDeclErrorIfAny(FromNS);
  4395. EXPECT_FALSE(OptErr);
  4396. // Check some of those decls whose ancestor is X, they all should have an
  4397. // error set if we visited them during an import process which finally failed.
  4398. // These decls are part of a cycle in an ImportPath.
  4399. // There would not be any error set for these decls if we hadn't follow the
  4400. // ImportPaths and the cycles.
  4401. OptErr = Importer->getImportDeclErrorIfAny(
  4402. FirstDeclMatcher<ClassTemplateDecl>().match(
  4403. FromTU, classTemplateDecl(hasName("F"))));
  4404. // An error is set to the 'F' ClassTemplateDecl.
  4405. EXPECT_TRUE(OptErr);
  4406. // An error is set to the FriendDecl.
  4407. OptErr = Importer->getImportDeclErrorIfAny(
  4408. FirstDeclMatcher<FriendDecl>().match(
  4409. FromTU, friendDecl()));
  4410. EXPECT_TRUE(OptErr);
  4411. // An error is set to the implicit class of A.
  4412. OptErr =
  4413. Importer->getImportDeclErrorIfAny(FirstDeclMatcher<CXXRecordDecl>().match(
  4414. FromTU, cxxRecordDecl(hasName("A"), isImplicit())));
  4415. EXPECT_TRUE(OptErr);
  4416. // An error is set to the implicit class of X.
  4417. OptErr =
  4418. Importer->getImportDeclErrorIfAny(FirstDeclMatcher<CXXRecordDecl>().match(
  4419. FromTU, cxxRecordDecl(hasName("X"), isImplicit())));
  4420. EXPECT_TRUE(OptErr);
  4421. }
  4422. TEST_P(ErrorHandlingTest, ErrorIsNotPropagatedFromMemberToNamespace) {
  4423. TranslationUnitDecl *FromTU = getTuDecl(
  4424. std::string(R"(
  4425. namespace X {
  4426. void f() { )") + ErroneousStmt + R"( } // This member has the error
  4427. // during import.
  4428. void ok(); // The error should not prevent importing this.
  4429. }; // An error will be set for X too.
  4430. )",
  4431. Lang_CXX);
  4432. auto *FromX = FirstDeclMatcher<NamespaceDecl>().match(
  4433. FromTU, namespaceDecl(hasName("X")));
  4434. NamespaceDecl *ImportedX = Import(FromX, Lang_CXX);
  4435. // There is no error set for X.
  4436. EXPECT_TRUE(ImportedX);
  4437. ASTImporter *Importer = findFromTU(FromX)->Importer.get();
  4438. Optional<ImportError> OptErr = Importer->getImportDeclErrorIfAny(FromX);
  4439. ASSERT_FALSE(OptErr);
  4440. // An error is set for f().
  4441. auto *FromF = FirstDeclMatcher<FunctionDecl>().match(
  4442. FromTU, functionDecl(hasName("f")));
  4443. OptErr = Importer->getImportDeclErrorIfAny(FromF);
  4444. ASSERT_TRUE(OptErr);
  4445. EXPECT_EQ(OptErr->Error, ImportError::UnsupportedConstruct);
  4446. // And any subsequent import should fail.
  4447. FunctionDecl *ImportedF = Import(FromF, Lang_CXX);
  4448. EXPECT_FALSE(ImportedF);
  4449. // There is no error set for ok().
  4450. auto *FromOK = FirstDeclMatcher<FunctionDecl>().match(
  4451. FromTU, functionDecl(hasName("ok")));
  4452. OptErr = Importer->getImportDeclErrorIfAny(FromOK);
  4453. EXPECT_FALSE(OptErr);
  4454. // And we should be able to import.
  4455. FunctionDecl *ImportedOK = Import(FromOK, Lang_CXX);
  4456. EXPECT_TRUE(ImportedOK);
  4457. }
  4458. // An error should be set for a class if it had a previous import with an error
  4459. // from another TU.
  4460. TEST_P(ErrorHandlingTest,
  4461. ImportedDeclWithErrorShouldFailTheImportOfDeclWhichMapToIt) {
  4462. // We already have a fwd decl.
  4463. TranslationUnitDecl *ToTU = getToTuDecl(
  4464. "class X;", Lang_CXX);
  4465. // Then we import a definition.
  4466. {
  4467. TranslationUnitDecl *FromTU = getTuDecl(std::string(R"(
  4468. class X {
  4469. void f() { )") + ErroneousStmt + R"( }
  4470. void ok();
  4471. };
  4472. )",
  4473. Lang_CXX);
  4474. auto *FromX = FirstDeclMatcher<CXXRecordDecl>().match(
  4475. FromTU, cxxRecordDecl(hasName("X")));
  4476. CXXRecordDecl *ImportedX = Import(FromX, Lang_CXX);
  4477. // An error is set for X ...
  4478. EXPECT_FALSE(ImportedX);
  4479. ASTImporter *Importer = findFromTU(FromX)->Importer.get();
  4480. Optional<ImportError> OptErr = Importer->getImportDeclErrorIfAny(FromX);
  4481. ASSERT_TRUE(OptErr);
  4482. EXPECT_EQ(OptErr->Error, ImportError::UnsupportedConstruct);
  4483. }
  4484. // ... but the node had been created.
  4485. auto *ToXDef = FirstDeclMatcher<CXXRecordDecl>().match(
  4486. ToTU, cxxRecordDecl(hasName("X"), isDefinition()));
  4487. // An error is set for "ToXDef" in the shared state.
  4488. Optional<ImportError> OptErr =
  4489. SharedStatePtr->getImportDeclErrorIfAny(ToXDef);
  4490. ASSERT_TRUE(OptErr);
  4491. EXPECT_EQ(OptErr->Error, ImportError::UnsupportedConstruct);
  4492. auto *ToXFwd = FirstDeclMatcher<CXXRecordDecl>().match(
  4493. ToTU, cxxRecordDecl(hasName("X"), unless(isDefinition())));
  4494. // An error is NOT set for the fwd Decl of X in the shared state.
  4495. OptErr = SharedStatePtr->getImportDeclErrorIfAny(ToXFwd);
  4496. ASSERT_FALSE(OptErr);
  4497. // Try to import X again but from another TU.
  4498. {
  4499. TranslationUnitDecl *FromTU = getTuDecl(std::string(R"(
  4500. class X {
  4501. void f() { )") + ErroneousStmt + R"( }
  4502. void ok();
  4503. };
  4504. )",
  4505. Lang_CXX, "input1.cc");
  4506. auto *FromX = FirstDeclMatcher<CXXRecordDecl>().match(
  4507. FromTU, cxxRecordDecl(hasName("X")));
  4508. CXXRecordDecl *ImportedX = Import(FromX, Lang_CXX);
  4509. // If we did not save the errors for the "to" context then the below checks
  4510. // would fail, because the lookup finds the fwd Decl of the existing
  4511. // definition in the "to" context. We can reach the existing definition via
  4512. // the found fwd Decl. That existing definition is structurally equivalent
  4513. // (we check only the fields) with this one we want to import, so we return
  4514. // with the existing definition, which is erroneous (one method is missing).
  4515. // The import should fail.
  4516. EXPECT_FALSE(ImportedX);
  4517. ASTImporter *Importer = findFromTU(FromX)->Importer.get();
  4518. Optional<ImportError> OptErr = Importer->getImportDeclErrorIfAny(FromX);
  4519. // And an error is set for this new X in the "from" ctx.
  4520. ASSERT_TRUE(OptErr);
  4521. EXPECT_EQ(OptErr->Error, ImportError::UnsupportedConstruct);
  4522. }
  4523. }
  4524. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ErrorHandlingTest,
  4525. DefaultTestValuesForRunOptions, );
  4526. INSTANTIATE_TEST_CASE_P(ParameterizedTests, DeclContextTest,
  4527. ::testing::Values(ArgVector()), );
  4528. INSTANTIATE_TEST_CASE_P(ParameterizedTests, CanonicalRedeclChain,
  4529. ::testing::Values(ArgVector()), );
  4530. TEST_P(ASTImporterOptionSpecificTestBase, LambdaInFunctionBody) {
  4531. Decl *FromTU = getTuDecl(
  4532. R"(
  4533. void f() {
  4534. auto L = [](){};
  4535. }
  4536. )",
  4537. Lang_CXX11, "input0.cc");
  4538. auto Pattern = lambdaExpr();
  4539. CXXRecordDecl *FromL =
  4540. FirstDeclMatcher<LambdaExpr>().match(FromTU, Pattern)->getLambdaClass();
  4541. auto ToL = Import(FromL, Lang_CXX11);
  4542. unsigned ToLSize = std::distance(ToL->decls().begin(), ToL->decls().end());
  4543. unsigned FromLSize =
  4544. std::distance(FromL->decls().begin(), FromL->decls().end());
  4545. EXPECT_NE(ToLSize, 0u);
  4546. EXPECT_EQ(ToLSize, FromLSize);
  4547. }
  4548. TEST_P(ASTImporterOptionSpecificTestBase, LambdaInFunctionParam) {
  4549. Decl *FromTU = getTuDecl(
  4550. R"(
  4551. template <typename F>
  4552. void f(F L = [](){}) {}
  4553. )",
  4554. Lang_CXX11, "input0.cc");
  4555. auto Pattern = lambdaExpr();
  4556. CXXRecordDecl *FromL =
  4557. FirstDeclMatcher<LambdaExpr>().match(FromTU, Pattern)->getLambdaClass();
  4558. auto ToL = Import(FromL, Lang_CXX11);
  4559. unsigned ToLSize = std::distance(ToL->decls().begin(), ToL->decls().end());
  4560. unsigned FromLSize =
  4561. std::distance(FromL->decls().begin(), FromL->decls().end());
  4562. EXPECT_NE(ToLSize, 0u);
  4563. EXPECT_EQ(ToLSize, FromLSize);
  4564. }
  4565. TEST_P(ASTImporterOptionSpecificTestBase, LambdaInGlobalScope) {
  4566. Decl *FromTU = getTuDecl(
  4567. R"(
  4568. auto l1 = [](unsigned lp) { return 1; };
  4569. auto l2 = [](int lp) { return 2; };
  4570. int f(int p) {
  4571. return l1(p) + l2(p);
  4572. }
  4573. )",
  4574. Lang_CXX11, "input0.cc");
  4575. FunctionDecl *FromF = FirstDeclMatcher<FunctionDecl>().match(
  4576. FromTU, functionDecl(hasName("f")));
  4577. FunctionDecl *ToF = Import(FromF, Lang_CXX11);
  4578. EXPECT_TRUE(ToF);
  4579. }
  4580. struct LLDBLookupTest : ASTImporterOptionSpecificTestBase {
  4581. LLDBLookupTest() {
  4582. Creator = [](ASTContext &ToContext, FileManager &ToFileManager,
  4583. ASTContext &FromContext, FileManager &FromFileManager,
  4584. bool MinimalImport,
  4585. const std::shared_ptr<ASTImporterSharedState> &SharedState) {
  4586. return new ASTImporter(ToContext, ToFileManager, FromContext,
  4587. FromFileManager, MinimalImport,
  4588. // We use the regular lookup.
  4589. /*SharedState=*/nullptr);
  4590. };
  4591. }
  4592. };
  4593. TEST_P(LLDBLookupTest, ImporterShouldFindInTransparentContext) {
  4594. TranslationUnitDecl *ToTU = getToTuDecl(
  4595. R"(
  4596. extern "C" {
  4597. class X{};
  4598. };
  4599. )",
  4600. Lang_CXX);
  4601. auto *ToX = FirstDeclMatcher<CXXRecordDecl>().match(
  4602. ToTU, cxxRecordDecl(hasName("X")));
  4603. // Set up a stub external storage.
  4604. ToTU->setHasExternalLexicalStorage(true);
  4605. // Set up DeclContextBits.HasLazyExternalLexicalLookups to true.
  4606. ToTU->setMustBuildLookupTable();
  4607. struct TestExternalASTSource : ExternalASTSource {};
  4608. ToTU->getASTContext().setExternalSource(new TestExternalASTSource());
  4609. Decl *FromTU = getTuDecl(
  4610. R"(
  4611. class X;
  4612. )",
  4613. Lang_CXX);
  4614. auto *FromX = FirstDeclMatcher<CXXRecordDecl>().match(
  4615. FromTU, cxxRecordDecl(hasName("X")));
  4616. auto *ImportedX = Import(FromX, Lang_CXX);
  4617. // The lookup must find the existing class definition in the LinkageSpecDecl.
  4618. // Then the importer renders the existing and the new decl into one chain.
  4619. EXPECT_EQ(ImportedX->getCanonicalDecl(), ToX->getCanonicalDecl());
  4620. }
  4621. struct SVEBuiltins : ASTImporterOptionSpecificTestBase {};
  4622. TEST_P(SVEBuiltins, ImportTypes) {
  4623. static const char *const TypeNames[] = {
  4624. "__SVInt8_t",
  4625. "__SVInt16_t",
  4626. "__SVInt32_t",
  4627. "__SVInt64_t",
  4628. "__SVUint8_t",
  4629. "__SVUint16_t",
  4630. "__SVUint32_t",
  4631. "__SVUint64_t",
  4632. "__SVFloat16_t",
  4633. "__SVFloat32_t",
  4634. "__SVFloat64_t",
  4635. "__SVBool_t"
  4636. };
  4637. TranslationUnitDecl *ToTU = getToTuDecl("", Lang_CXX);
  4638. TranslationUnitDecl *FromTU = getTuDecl("", Lang_CXX, "input.cc");
  4639. for (auto *TypeName : TypeNames) {
  4640. auto *ToTypedef = FirstDeclMatcher<TypedefDecl>().match(
  4641. ToTU, typedefDecl(hasName(TypeName)));
  4642. QualType ToType = ToTypedef->getUnderlyingType();
  4643. auto *FromTypedef = FirstDeclMatcher<TypedefDecl>().match(
  4644. FromTU, typedefDecl(hasName(TypeName)));
  4645. QualType FromType = FromTypedef->getUnderlyingType();
  4646. QualType ImportedType = ImportType(FromType, FromTypedef, Lang_CXX);
  4647. EXPECT_EQ(ImportedType, ToType);
  4648. }
  4649. }
  4650. INSTANTIATE_TEST_CASE_P(ParameterizedTests, SVEBuiltins,
  4651. ::testing::Values(ArgVector{"-target",
  4652. "aarch64-linux-gnu"}), );
  4653. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterLookupTableTest,
  4654. DefaultTestValuesForRunOptions, );
  4655. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportPath,
  4656. ::testing::Values(ArgVector()), );
  4657. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportExpr,
  4658. DefaultTestValuesForRunOptions, );
  4659. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportType,
  4660. DefaultTestValuesForRunOptions, );
  4661. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportDecl,
  4662. DefaultTestValuesForRunOptions, );
  4663. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterOptionSpecificTestBase,
  4664. DefaultTestValuesForRunOptions, );
  4665. INSTANTIATE_TEST_CASE_P(ParameterizedTests, RedirectingImporterTest,
  4666. DefaultTestValuesForRunOptions, );
  4667. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportFunctions,
  4668. DefaultTestValuesForRunOptions, );
  4669. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportFriendFunctionTemplates,
  4670. DefaultTestValuesForRunOptions, );
  4671. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportClasses,
  4672. DefaultTestValuesForRunOptions, );
  4673. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportFunctionTemplates,
  4674. DefaultTestValuesForRunOptions, );
  4675. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportFriendFunctions,
  4676. DefaultTestValuesForRunOptions, );
  4677. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportFriendClasses,
  4678. DefaultTestValuesForRunOptions, );
  4679. INSTANTIATE_TEST_CASE_P(ParameterizedTests,
  4680. ImportFunctionTemplateSpecializations,
  4681. DefaultTestValuesForRunOptions, );
  4682. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportImplicitMethods,
  4683. DefaultTestValuesForRunOptions, );
  4684. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportVariables,
  4685. DefaultTestValuesForRunOptions, );
  4686. INSTANTIATE_TEST_CASE_P(ParameterizedTests, LLDBLookupTest,
  4687. DefaultTestValuesForRunOptions, );
  4688. } // end namespace ast_matchers
  4689. } // end namespace clang