ASTImporterTest.cpp 159 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667
  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, ASTImporterLookupTable *LookupTable) {
  267. return new RedirectingImporter(ToContext, ToFileManager, FromContext,
  268. FromFileManager, MinimalImport,
  269. LookupTable);
  270. };
  271. }
  272. };
  273. // Test that an ASTImporter subclass can intercept an import call.
  274. TEST_P(RedirectingImporterTest, InterceptImport) {
  275. Decl *From, *To;
  276. std::tie(From, To) =
  277. getImportedDecl("class shouldNotBeImported {};", Lang_CXX,
  278. "class realDecl {};", Lang_CXX, "shouldNotBeImported");
  279. auto *Imported = cast<CXXRecordDecl>(To);
  280. EXPECT_EQ(Imported->getQualifiedNameAsString(), "realDecl");
  281. // Make sure our importer prevented the importing of the decl.
  282. auto *ToTU = Imported->getTranslationUnitDecl();
  283. auto Pattern = functionDecl(hasName("shouldNotBeImported"));
  284. unsigned count =
  285. DeclCounterWithPredicate<CXXRecordDecl>().match(ToTU, Pattern);
  286. EXPECT_EQ(0U, count);
  287. }
  288. // Test that when we indirectly import a declaration the custom ASTImporter
  289. // is still intercepting the import.
  290. TEST_P(RedirectingImporterTest, InterceptIndirectImport) {
  291. Decl *From, *To;
  292. std::tie(From, To) =
  293. getImportedDecl("class shouldNotBeImported {};"
  294. "class F { shouldNotBeImported f; };",
  295. Lang_CXX, "class realDecl {};", Lang_CXX, "F");
  296. // Make sure our ASTImporter prevented the importing of the decl.
  297. auto *ToTU = To->getTranslationUnitDecl();
  298. auto Pattern = functionDecl(hasName("shouldNotBeImported"));
  299. unsigned count =
  300. DeclCounterWithPredicate<CXXRecordDecl>().match(ToTU, Pattern);
  301. EXPECT_EQ(0U, count);
  302. }
  303. TEST_P(ImportExpr, ImportStringLiteral) {
  304. MatchVerifier<Decl> Verifier;
  305. testImport(
  306. "void declToImport() { (void)\"foo\"; }",
  307. Lang_CXX, "", Lang_CXX, Verifier,
  308. functionDecl(hasDescendant(
  309. stringLiteral(hasType(asString("const char [4]"))))));
  310. testImport(
  311. "void declToImport() { (void)L\"foo\"; }",
  312. Lang_CXX, "", Lang_CXX, Verifier,
  313. functionDecl(hasDescendant(
  314. stringLiteral(hasType(asString("const wchar_t [4]"))))));
  315. testImport(
  316. "void declToImport() { (void) \"foo\" \"bar\"; }",
  317. Lang_CXX, "", Lang_CXX, Verifier,
  318. functionDecl(hasDescendant(
  319. stringLiteral(hasType(asString("const char [7]"))))));
  320. }
  321. TEST_P(ImportExpr, ImportChooseExpr) {
  322. MatchVerifier<Decl> Verifier;
  323. // This case tests C code that is not condition-dependent and has a true
  324. // condition.
  325. testImport(
  326. "void declToImport() { (void)__builtin_choose_expr(1, 2, 3); }",
  327. Lang_C, "", Lang_C, Verifier,
  328. functionDecl(hasDescendant(chooseExpr())));
  329. }
  330. TEST_P(ImportExpr, ImportGNUNullExpr) {
  331. MatchVerifier<Decl> Verifier;
  332. testImport(
  333. "void declToImport() { (void)__null; }",
  334. Lang_CXX, "", Lang_CXX, Verifier,
  335. functionDecl(hasDescendant(gnuNullExpr(hasType(isInteger())))));
  336. }
  337. TEST_P(ImportExpr, ImportCXXNullPtrLiteralExpr) {
  338. MatchVerifier<Decl> Verifier;
  339. testImport(
  340. "void declToImport() { (void)nullptr; }",
  341. Lang_CXX11, "", Lang_CXX11, Verifier,
  342. functionDecl(hasDescendant(cxxNullPtrLiteralExpr())));
  343. }
  344. TEST_P(ImportExpr, ImportFloatinglLiteralExpr) {
  345. MatchVerifier<Decl> Verifier;
  346. testImport(
  347. "void declToImport() { (void)1.0; }",
  348. Lang_C, "", Lang_C, Verifier,
  349. functionDecl(hasDescendant(
  350. floatLiteral(equals(1.0), hasType(asString("double"))))));
  351. testImport(
  352. "void declToImport() { (void)1.0e-5f; }",
  353. Lang_C, "", Lang_C, Verifier,
  354. functionDecl(hasDescendant(
  355. floatLiteral(equals(1.0e-5f), hasType(asString("float"))))));
  356. }
  357. TEST_P(ImportExpr, ImportImaginaryLiteralExpr) {
  358. MatchVerifier<Decl> Verifier;
  359. testImport(
  360. "void declToImport() { (void)1.0i; }",
  361. Lang_CXX14, "", Lang_CXX14, Verifier,
  362. functionDecl(hasDescendant(imaginaryLiteral())));
  363. }
  364. TEST_P(ImportExpr, ImportCompoundLiteralExpr) {
  365. MatchVerifier<Decl> Verifier;
  366. testImport(
  367. "void declToImport() {"
  368. " struct s { int x; long y; unsigned z; }; "
  369. " (void)(struct s){ 42, 0L, 1U }; }",
  370. Lang_CXX, "", Lang_CXX, Verifier,
  371. functionDecl(hasDescendant(
  372. compoundLiteralExpr(
  373. hasType(asString("struct s")),
  374. has(initListExpr(
  375. hasType(asString("struct s")),
  376. has(integerLiteral(
  377. equals(42), hasType(asString("int")))),
  378. has(integerLiteral(
  379. equals(0), hasType(asString("long")))),
  380. has(integerLiteral(
  381. equals(1), hasType(asString("unsigned int"))))))))));
  382. }
  383. TEST_P(ImportExpr, ImportCXXThisExpr) {
  384. MatchVerifier<Decl> Verifier;
  385. testImport(
  386. "class declToImport { void f() { (void)this; } };",
  387. Lang_CXX, "", Lang_CXX, Verifier,
  388. cxxRecordDecl(
  389. hasMethod(
  390. hasDescendant(
  391. cxxThisExpr(
  392. hasType(
  393. asString("class declToImport *")))))));
  394. }
  395. TEST_P(ImportExpr, ImportAtomicExpr) {
  396. MatchVerifier<Decl> Verifier;
  397. testImport(
  398. "void declToImport() { int *ptr; __atomic_load_n(ptr, 1); }",
  399. Lang_C, "", Lang_C, Verifier,
  400. functionDecl(hasDescendant(
  401. atomicExpr(
  402. has(ignoringParenImpCasts(
  403. declRefExpr(hasDeclaration(varDecl(hasName("ptr"))),
  404. hasType(asString("int *"))))),
  405. has(integerLiteral(equals(1), hasType(asString("int"))))))));
  406. }
  407. TEST_P(ImportExpr, ImportLabelDeclAndAddrLabelExpr) {
  408. MatchVerifier<Decl> Verifier;
  409. testImport(
  410. "void declToImport() { loop: goto loop; (void)&&loop; }",
  411. Lang_C, "", Lang_C, Verifier,
  412. functionDecl(
  413. hasDescendant(
  414. labelStmt(hasDeclaration(labelDecl(hasName("loop"))))),
  415. hasDescendant(
  416. addrLabelExpr(hasDeclaration(labelDecl(hasName("loop")))))));
  417. }
  418. AST_MATCHER_P(TemplateDecl, hasTemplateDecl,
  419. internal::Matcher<NamedDecl>, InnerMatcher) {
  420. const NamedDecl *Template = Node.getTemplatedDecl();
  421. return Template && InnerMatcher.matches(*Template, Finder, Builder);
  422. }
  423. TEST_P(ImportExpr, ImportParenListExpr) {
  424. MatchVerifier<Decl> Verifier;
  425. testImport(
  426. "template<typename T> class dummy { void f() { dummy X(*this); } };"
  427. "typedef dummy<int> declToImport;"
  428. "template class dummy<int>;",
  429. Lang_CXX, "", Lang_CXX, Verifier,
  430. typedefDecl(hasType(templateSpecializationType(
  431. hasDeclaration(classTemplateSpecializationDecl(hasSpecializedTemplate(
  432. classTemplateDecl(hasTemplateDecl(cxxRecordDecl(hasMethod(allOf(
  433. hasName("f"),
  434. hasBody(compoundStmt(has(declStmt(hasSingleDecl(
  435. varDecl(hasInitializer(parenListExpr(has(unaryOperator(
  436. hasOperatorName("*"),
  437. hasUnaryOperand(cxxThisExpr())))))))))))))))))))))));
  438. }
  439. TEST_P(ImportExpr, ImportSwitch) {
  440. MatchVerifier<Decl> Verifier;
  441. testImport(
  442. "void declToImport() { int b; switch (b) { case 1: break; } }",
  443. Lang_C, "", Lang_C, Verifier,
  444. functionDecl(hasDescendant(
  445. switchStmt(has(compoundStmt(has(caseStmt())))))));
  446. }
  447. TEST_P(ImportExpr, ImportStmtExpr) {
  448. MatchVerifier<Decl> Verifier;
  449. testImport(
  450. "void declToImport() { int b; int a = b ?: 1; int C = ({int X=4; X;}); }",
  451. Lang_C, "", Lang_C, Verifier,
  452. functionDecl(hasDescendant(
  453. varDecl(
  454. hasName("C"),
  455. hasType(asString("int")),
  456. hasInitializer(
  457. stmtExpr(
  458. hasAnySubstatement(declStmt(hasSingleDecl(
  459. varDecl(
  460. hasName("X"),
  461. hasType(asString("int")),
  462. hasInitializer(
  463. integerLiteral(equals(4))))))),
  464. hasDescendant(
  465. implicitCastExpr())))))));
  466. }
  467. TEST_P(ImportExpr, ImportConditionalOperator) {
  468. MatchVerifier<Decl> Verifier;
  469. testImport(
  470. "void declToImport() { (void)(true ? 1 : -5); }",
  471. Lang_CXX, "", Lang_CXX, Verifier,
  472. functionDecl(hasDescendant(
  473. conditionalOperator(
  474. hasCondition(cxxBoolLiteral(equals(true))),
  475. hasTrueExpression(integerLiteral(equals(1))),
  476. hasFalseExpression(
  477. unaryOperator(hasUnaryOperand(integerLiteral(equals(5))))))
  478. )));
  479. }
  480. TEST_P(ImportExpr, ImportBinaryConditionalOperator) {
  481. MatchVerifier<Decl> Verifier;
  482. testImport(
  483. "void declToImport() { (void)(1 ?: -5); }",
  484. Lang_CXX, "", Lang_CXX, Verifier,
  485. functionDecl(hasDescendant(
  486. binaryConditionalOperator(
  487. hasCondition(
  488. implicitCastExpr(
  489. hasSourceExpression(opaqueValueExpr(
  490. hasSourceExpression(integerLiteral(equals(1))))),
  491. hasType(booleanType()))),
  492. hasTrueExpression(
  493. opaqueValueExpr(
  494. hasSourceExpression(integerLiteral(equals(1))))),
  495. hasFalseExpression(
  496. unaryOperator(
  497. hasOperatorName("-"),
  498. hasUnaryOperand(integerLiteral(equals(5)))))))));
  499. }
  500. TEST_P(ImportExpr, ImportDesignatedInitExpr) {
  501. MatchVerifier<Decl> Verifier;
  502. testImport(
  503. "void declToImport() {"
  504. " struct point { double x; double y; };"
  505. " struct point ptarray[10] = "
  506. "{ [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; }",
  507. Lang_C, "", Lang_C, Verifier,
  508. functionDecl(hasDescendant(
  509. initListExpr(
  510. has(designatedInitExpr(
  511. designatorCountIs(2),
  512. hasDescendant(floatLiteral(equals(1.0))),
  513. hasDescendant(integerLiteral(equals(2))))),
  514. has(designatedInitExpr(
  515. designatorCountIs(2),
  516. hasDescendant(floatLiteral(equals(2.0))),
  517. hasDescendant(integerLiteral(equals(2))))),
  518. has(designatedInitExpr(
  519. designatorCountIs(2),
  520. hasDescendant(floatLiteral(equals(1.0))),
  521. hasDescendant(integerLiteral(equals(0)))))))));
  522. }
  523. TEST_P(ImportExpr, ImportPredefinedExpr) {
  524. MatchVerifier<Decl> Verifier;
  525. // __func__ expands as StringLiteral("declToImport")
  526. testImport(
  527. "void declToImport() { (void)__func__; }",
  528. Lang_CXX, "", Lang_CXX, Verifier,
  529. functionDecl(hasDescendant(
  530. predefinedExpr(
  531. hasType(
  532. asString("const char [13]")),
  533. has(stringLiteral(hasType(
  534. asString("const char [13]"))))))));
  535. }
  536. TEST_P(ImportExpr, ImportInitListExpr) {
  537. MatchVerifier<Decl> Verifier;
  538. testImport(
  539. "void declToImport() {"
  540. " struct point { double x; double y; };"
  541. " point ptarray[10] = { [2].y = 1.0, [2].x = 2.0,"
  542. " [0].x = 1.0 }; }",
  543. Lang_CXX, "", Lang_CXX, Verifier,
  544. functionDecl(hasDescendant(
  545. initListExpr(
  546. has(
  547. cxxConstructExpr(
  548. requiresZeroInitialization())),
  549. has(
  550. initListExpr(
  551. hasType(asString("struct point")),
  552. has(floatLiteral(equals(1.0))),
  553. has(implicitValueInitExpr(
  554. hasType(asString("double")))))),
  555. has(
  556. initListExpr(
  557. hasType(asString("struct point")),
  558. has(floatLiteral(equals(2.0))),
  559. has(floatLiteral(equals(1.0)))))))));
  560. }
  561. const internal::VariadicDynCastAllOfMatcher<Expr, VAArgExpr> vaArgExpr;
  562. TEST_P(ImportExpr, ImportVAArgExpr) {
  563. MatchVerifier<Decl> Verifier;
  564. testImport(
  565. "void declToImport(__builtin_va_list list, ...) {"
  566. " (void)__builtin_va_arg(list, int); }",
  567. Lang_CXX, "", Lang_CXX, Verifier,
  568. functionDecl(hasDescendant(
  569. cStyleCastExpr(hasSourceExpression(vaArgExpr())))));
  570. }
  571. TEST_P(ImportExpr, CXXTemporaryObjectExpr) {
  572. MatchVerifier<Decl> Verifier;
  573. testImport(
  574. "struct C {};"
  575. "void declToImport() { C c = C(); }",
  576. Lang_CXX, "", Lang_CXX, Verifier,
  577. functionDecl(hasDescendant(
  578. exprWithCleanups(has(cxxConstructExpr(
  579. has(materializeTemporaryExpr(has(implicitCastExpr(
  580. has(cxxTemporaryObjectExpr())))))))))));
  581. }
  582. TEST_P(ImportType, ImportAtomicType) {
  583. MatchVerifier<Decl> Verifier;
  584. testImport(
  585. "void declToImport() { typedef _Atomic(int) a_int; }",
  586. Lang_CXX11, "", Lang_CXX11, Verifier,
  587. functionDecl(hasDescendant(typedefDecl(has(atomicType())))));
  588. }
  589. TEST_P(ImportDecl, ImportFunctionTemplateDecl) {
  590. MatchVerifier<Decl> Verifier;
  591. testImport(
  592. "template <typename T> void declToImport() { };",
  593. Lang_CXX, "", Lang_CXX, Verifier,
  594. functionTemplateDecl());
  595. }
  596. TEST_P(ImportExpr, ImportCXXDependentScopeMemberExpr) {
  597. MatchVerifier<Decl> Verifier;
  598. testImport(
  599. "template <typename T> struct C { T t; };"
  600. "template <typename T> void declToImport() {"
  601. " C<T> d;"
  602. " (void)d.t;"
  603. "}"
  604. "void instantiate() { declToImport<int>(); }",
  605. Lang_CXX, "", Lang_CXX, Verifier,
  606. functionTemplateDecl(hasDescendant(
  607. cStyleCastExpr(has(cxxDependentScopeMemberExpr())))));
  608. testImport(
  609. "template <typename T> struct C { T t; };"
  610. "template <typename T> void declToImport() {"
  611. " C<T> d;"
  612. " (void)(&d)->t;"
  613. "}"
  614. "void instantiate() { declToImport<int>(); }",
  615. Lang_CXX, "", Lang_CXX, Verifier,
  616. functionTemplateDecl(hasDescendant(
  617. cStyleCastExpr(has(cxxDependentScopeMemberExpr())))));
  618. }
  619. TEST_P(ImportType, ImportTypeAliasTemplate) {
  620. MatchVerifier<Decl> Verifier;
  621. testImport(
  622. "template <int K>"
  623. "struct dummy { static const int i = K; };"
  624. "template <int K> using dummy2 = dummy<K>;"
  625. "int declToImport() { return dummy2<3>::i; }",
  626. Lang_CXX11, "", Lang_CXX11, Verifier,
  627. functionDecl(
  628. hasDescendant(implicitCastExpr(has(declRefExpr()))),
  629. unless(hasAncestor(translationUnitDecl(has(typeAliasDecl()))))));
  630. }
  631. const internal::VariadicDynCastAllOfMatcher<Decl, VarTemplateSpecializationDecl>
  632. varTemplateSpecializationDecl;
  633. TEST_P(ImportDecl, ImportVarTemplate) {
  634. MatchVerifier<Decl> Verifier;
  635. testImport(
  636. "template <typename T>"
  637. "T pi = T(3.1415926535897932385L);"
  638. "void declToImport() { (void)pi<int>; }",
  639. Lang_CXX14, "", Lang_CXX14, Verifier,
  640. functionDecl(
  641. hasDescendant(declRefExpr(to(varTemplateSpecializationDecl()))),
  642. unless(hasAncestor(translationUnitDecl(has(varDecl(
  643. hasName("pi"), unless(varTemplateSpecializationDecl()))))))));
  644. }
  645. TEST_P(ImportType, ImportPackExpansion) {
  646. MatchVerifier<Decl> Verifier;
  647. testImport(
  648. "template <typename... Args>"
  649. "struct dummy {"
  650. " dummy(Args... args) {}"
  651. " static const int i = 4;"
  652. "};"
  653. "int declToImport() { return dummy<int>::i; }",
  654. Lang_CXX11, "", Lang_CXX11, Verifier,
  655. functionDecl(hasDescendant(
  656. returnStmt(has(implicitCastExpr(has(declRefExpr())))))));
  657. }
  658. const internal::VariadicDynCastAllOfMatcher<Type,
  659. DependentTemplateSpecializationType>
  660. dependentTemplateSpecializationType;
  661. TEST_P(ImportType, ImportDependentTemplateSpecialization) {
  662. MatchVerifier<Decl> Verifier;
  663. testImport(
  664. "template<typename T>"
  665. "struct A;"
  666. "template<typename T>"
  667. "struct declToImport {"
  668. " typename A<T>::template B<T> a;"
  669. "};",
  670. Lang_CXX, "", Lang_CXX, Verifier,
  671. classTemplateDecl(has(cxxRecordDecl(has(
  672. fieldDecl(hasType(dependentTemplateSpecializationType())))))));
  673. }
  674. const internal::VariadicDynCastAllOfMatcher<Stmt, SizeOfPackExpr>
  675. sizeOfPackExpr;
  676. TEST_P(ImportExpr, ImportSizeOfPackExpr) {
  677. MatchVerifier<Decl> Verifier;
  678. testImport(
  679. "template <typename... Ts>"
  680. "void declToImport() {"
  681. " const int i = sizeof...(Ts);"
  682. "};"
  683. "void g() { declToImport<int>(); }",
  684. Lang_CXX11, "", Lang_CXX11, Verifier,
  685. functionTemplateDecl(hasDescendant(sizeOfPackExpr())));
  686. testImport(
  687. "template <typename... Ts>"
  688. "using X = int[sizeof...(Ts)];"
  689. "template <typename... Us>"
  690. "struct Y {"
  691. " X<Us..., int, double, int, Us...> f;"
  692. "};"
  693. "Y<float, int> declToImport;",
  694. Lang_CXX11, "", Lang_CXX11, Verifier,
  695. varDecl(hasType(classTemplateSpecializationDecl(has(fieldDecl(hasType(
  696. hasUnqualifiedDesugaredType(constantArrayType(hasSize(7))))))))));
  697. }
  698. /// \brief Matches __builtin_types_compatible_p:
  699. /// GNU extension to check equivalent types
  700. /// Given
  701. /// \code
  702. /// __builtin_types_compatible_p(int, int)
  703. /// \endcode
  704. // will generate TypeTraitExpr <...> 'int'
  705. const internal::VariadicDynCastAllOfMatcher<Stmt, TypeTraitExpr> typeTraitExpr;
  706. TEST_P(ImportExpr, ImportTypeTraitExpr) {
  707. MatchVerifier<Decl> Verifier;
  708. testImport(
  709. "void declToImport() { "
  710. " (void)__builtin_types_compatible_p(int, int);"
  711. "}",
  712. Lang_C, "", Lang_C, Verifier,
  713. functionDecl(hasDescendant(typeTraitExpr(hasType(asString("int"))))));
  714. }
  715. const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTypeidExpr> cxxTypeidExpr;
  716. TEST_P(ImportExpr, ImportCXXTypeidExpr) {
  717. MatchVerifier<Decl> Verifier;
  718. testImport(
  719. "namespace std { class type_info {}; }"
  720. "void declToImport() {"
  721. " int x;"
  722. " auto a = typeid(int); auto b = typeid(x);"
  723. "}",
  724. Lang_CXX11, "", Lang_CXX11, Verifier,
  725. functionDecl(
  726. hasDescendant(varDecl(
  727. hasName("a"), hasInitializer(hasDescendant(cxxTypeidExpr())))),
  728. hasDescendant(varDecl(
  729. hasName("b"), hasInitializer(hasDescendant(cxxTypeidExpr()))))));
  730. }
  731. TEST_P(ImportExpr, ImportTypeTraitExprValDep) {
  732. MatchVerifier<Decl> Verifier;
  733. testImport(
  734. "template<typename T> struct declToImport {"
  735. " void m() { (void)__is_pod(T); }"
  736. "};"
  737. "void f() { declToImport<int>().m(); }",
  738. Lang_CXX11, "", Lang_CXX11, Verifier,
  739. classTemplateDecl(has(cxxRecordDecl(has(
  740. functionDecl(hasDescendant(
  741. typeTraitExpr(hasType(booleanType())))))))));
  742. }
  743. TEST_P(ImportDecl, ImportRecordDeclInFunc) {
  744. MatchVerifier<Decl> Verifier;
  745. testImport("int declToImport() { "
  746. " struct data_t {int a;int b;};"
  747. " struct data_t d;"
  748. " return 0;"
  749. "}",
  750. Lang_C, "", Lang_C, Verifier,
  751. functionDecl(hasBody(compoundStmt(
  752. has(declStmt(hasSingleDecl(varDecl(hasName("d")))))))));
  753. }
  754. TEST_P(ASTImporterOptionSpecificTestBase, ImportRecordTypeInFunc) {
  755. Decl *FromTU = getTuDecl("int declToImport() { "
  756. " struct data_t {int a;int b;};"
  757. " struct data_t d;"
  758. " return 0;"
  759. "}",
  760. Lang_C, "input.c");
  761. auto *FromVar =
  762. FirstDeclMatcher<VarDecl>().match(FromTU, varDecl(hasName("d")));
  763. ASSERT_TRUE(FromVar);
  764. auto ToType =
  765. ImportType(FromVar->getType().getCanonicalType(), FromVar, Lang_C);
  766. EXPECT_FALSE(ToType.isNull());
  767. }
  768. TEST_P(ASTImporterOptionSpecificTestBase, ImportRecordDeclInFuncParams) {
  769. // This construct is not supported by ASTImporter.
  770. Decl *FromTU = getTuDecl(
  771. "int declToImport(struct data_t{int a;int b;} ***d){ return 0; }",
  772. Lang_C, "input.c");
  773. auto *From = FirstDeclMatcher<FunctionDecl>().match(
  774. FromTU, functionDecl(hasName("declToImport")));
  775. ASSERT_TRUE(From);
  776. auto *To = Import(From, Lang_C);
  777. EXPECT_EQ(To, nullptr);
  778. }
  779. TEST_P(ASTImporterOptionSpecificTestBase, ImportRecordDeclInFuncFromMacro) {
  780. Decl *FromTU = getTuDecl(
  781. "#define NONAME_SIZEOF(type) sizeof(struct{type *dummy;}) \n"
  782. "int declToImport(){ return NONAME_SIZEOF(int); }",
  783. Lang_C, "input.c");
  784. auto *From = FirstDeclMatcher<FunctionDecl>().match(
  785. FromTU, functionDecl(hasName("declToImport")));
  786. ASSERT_TRUE(From);
  787. auto *To = Import(From, Lang_C);
  788. ASSERT_TRUE(To);
  789. EXPECT_TRUE(MatchVerifier<FunctionDecl>().match(
  790. To, functionDecl(hasName("declToImport"),
  791. hasDescendant(unaryExprOrTypeTraitExpr()))));
  792. }
  793. TEST_P(ASTImporterOptionSpecificTestBase,
  794. ImportRecordDeclInFuncParamsFromMacro) {
  795. // This construct is not supported by ASTImporter.
  796. Decl *FromTU = getTuDecl(
  797. "#define PAIR_STRUCT(type) struct data_t{type a;type b;} \n"
  798. "int declToImport(PAIR_STRUCT(int) ***d){ return 0; }",
  799. Lang_C, "input.c");
  800. auto *From = FirstDeclMatcher<FunctionDecl>().match(
  801. FromTU, functionDecl(hasName("declToImport")));
  802. ASSERT_TRUE(From);
  803. auto *To = Import(From, Lang_C);
  804. EXPECT_EQ(To, nullptr);
  805. }
  806. const internal::VariadicDynCastAllOfMatcher<Expr, CXXPseudoDestructorExpr>
  807. cxxPseudoDestructorExpr;
  808. TEST_P(ImportExpr, ImportCXXPseudoDestructorExpr) {
  809. MatchVerifier<Decl> Verifier;
  810. testImport(
  811. "typedef int T;"
  812. "void declToImport(int *p) {"
  813. " T t;"
  814. " p->T::~T();"
  815. "}",
  816. Lang_CXX, "", Lang_CXX, Verifier,
  817. functionDecl(hasDescendant(
  818. callExpr(has(cxxPseudoDestructorExpr())))));
  819. }
  820. TEST_P(ImportDecl, ImportUsingDecl) {
  821. MatchVerifier<Decl> Verifier;
  822. testImport(
  823. "namespace foo { int bar; }"
  824. "void declToImport() { using foo::bar; }",
  825. Lang_CXX, "", Lang_CXX, Verifier,
  826. functionDecl(hasDescendant(usingDecl())));
  827. }
  828. /// \brief Matches shadow declarations introduced into a scope by a
  829. /// (resolved) using declaration.
  830. ///
  831. /// Given
  832. /// \code
  833. /// namespace n { int f; }
  834. /// namespace declToImport { using n::f; }
  835. /// \endcode
  836. /// usingShadowDecl()
  837. /// matches \code f \endcode
  838. const internal::VariadicDynCastAllOfMatcher<Decl,
  839. UsingShadowDecl> usingShadowDecl;
  840. TEST_P(ImportDecl, ImportUsingShadowDecl) {
  841. MatchVerifier<Decl> Verifier;
  842. testImport(
  843. "namespace foo { int bar; }"
  844. "namespace declToImport { using foo::bar; }",
  845. Lang_CXX, "", Lang_CXX, Verifier,
  846. namespaceDecl(has(usingShadowDecl())));
  847. }
  848. TEST_P(ImportExpr, ImportUnresolvedLookupExpr) {
  849. MatchVerifier<Decl> Verifier;
  850. testImport(
  851. "template<typename T> int foo();"
  852. "template <typename T> void declToImport() {"
  853. " (void)::foo<T>;"
  854. " (void)::template foo<T>;"
  855. "}"
  856. "void instantiate() { declToImport<int>(); }",
  857. Lang_CXX, "", Lang_CXX, Verifier,
  858. functionTemplateDecl(hasDescendant(unresolvedLookupExpr())));
  859. }
  860. TEST_P(ImportExpr, ImportCXXUnresolvedConstructExpr) {
  861. MatchVerifier<Decl> Verifier;
  862. testImport(
  863. "template <typename T> struct C { T t; };"
  864. "template <typename T> void declToImport() {"
  865. " C<T> d;"
  866. " d.t = T();"
  867. "}"
  868. "void instantiate() { declToImport<int>(); }",
  869. Lang_CXX, "", Lang_CXX, Verifier,
  870. functionTemplateDecl(hasDescendant(
  871. binaryOperator(has(cxxUnresolvedConstructExpr())))));
  872. testImport(
  873. "template <typename T> struct C { T t; };"
  874. "template <typename T> void declToImport() {"
  875. " C<T> d;"
  876. " (&d)->t = T();"
  877. "}"
  878. "void instantiate() { declToImport<int>(); }",
  879. Lang_CXX, "", Lang_CXX, Verifier,
  880. functionTemplateDecl(hasDescendant(
  881. binaryOperator(has(cxxUnresolvedConstructExpr())))));
  882. }
  883. /// Check that function "declToImport()" (which is the templated function
  884. /// for corresponding FunctionTemplateDecl) is not added into DeclContext.
  885. /// Same for class template declarations.
  886. TEST_P(ImportDecl, ImportTemplatedDeclForTemplate) {
  887. MatchVerifier<Decl> Verifier;
  888. testImport(
  889. "template <typename T> void declToImport() { T a = 1; }"
  890. "void instantiate() { declToImport<int>(); }",
  891. Lang_CXX, "", Lang_CXX, Verifier,
  892. functionTemplateDecl(hasAncestor(translationUnitDecl(
  893. unless(has(functionDecl(hasName("declToImport"))))))));
  894. testImport(
  895. "template <typename T> struct declToImport { T t; };"
  896. "void instantiate() { declToImport<int>(); }",
  897. Lang_CXX, "", Lang_CXX, Verifier,
  898. classTemplateDecl(hasAncestor(translationUnitDecl(
  899. unless(has(cxxRecordDecl(hasName("declToImport"))))))));
  900. }
  901. TEST_P(ImportDecl, ImportClassTemplatePartialSpecialization) {
  902. MatchVerifier<Decl> Verifier;
  903. auto Code =
  904. R"s(
  905. struct declToImport {
  906. template <typename T0> struct X;
  907. template <typename T0> struct X<T0 *> {};
  908. };
  909. )s";
  910. testImport(Code, Lang_CXX, "", Lang_CXX, Verifier,
  911. recordDecl(has(classTemplateDecl()),
  912. has(classTemplateSpecializationDecl())));
  913. }
  914. TEST_P(ImportExpr, CXXOperatorCallExpr) {
  915. MatchVerifier<Decl> Verifier;
  916. testImport(
  917. "class declToImport {"
  918. " void f() { *this = declToImport(); }"
  919. "};",
  920. Lang_CXX, "", Lang_CXX, Verifier,
  921. cxxRecordDecl(has(cxxMethodDecl(hasDescendant(
  922. cxxOperatorCallExpr())))));
  923. }
  924. TEST_P(ImportExpr, DependentSizedArrayType) {
  925. MatchVerifier<Decl> Verifier;
  926. testImport(
  927. "template<typename T, int Size> class declToImport {"
  928. " T data[Size];"
  929. "};",
  930. Lang_CXX, "", Lang_CXX, Verifier,
  931. classTemplateDecl(has(cxxRecordDecl(
  932. has(fieldDecl(hasType(dependentSizedArrayType())))))));
  933. }
  934. TEST_P(ASTImporterOptionSpecificTestBase, ImportBeginLocOfDeclRefExpr) {
  935. Decl *FromTU = getTuDecl(
  936. "class A { public: static int X; }; void f() { (void)A::X; }", Lang_CXX);
  937. auto From = FirstDeclMatcher<FunctionDecl>().match(
  938. FromTU, functionDecl(hasName("f")));
  939. ASSERT_TRUE(From);
  940. ASSERT_TRUE(
  941. cast<CStyleCastExpr>(cast<CompoundStmt>(From->getBody())->body_front())
  942. ->getSubExpr()
  943. ->getBeginLoc()
  944. .isValid());
  945. FunctionDecl *To = Import(From, Lang_CXX);
  946. ASSERT_TRUE(To);
  947. ASSERT_TRUE(
  948. cast<CStyleCastExpr>(cast<CompoundStmt>(To->getBody())->body_front())
  949. ->getSubExpr()
  950. ->getBeginLoc()
  951. .isValid());
  952. }
  953. TEST_P(ASTImporterOptionSpecificTestBase,
  954. ImportOfTemplatedDeclOfClassTemplateDecl) {
  955. Decl *FromTU = getTuDecl("template<class X> struct S{};", Lang_CXX);
  956. auto From =
  957. FirstDeclMatcher<ClassTemplateDecl>().match(FromTU, classTemplateDecl());
  958. ASSERT_TRUE(From);
  959. auto To = cast<ClassTemplateDecl>(Import(From, Lang_CXX));
  960. ASSERT_TRUE(To);
  961. Decl *ToTemplated = To->getTemplatedDecl();
  962. Decl *ToTemplated1 = Import(From->getTemplatedDecl(), Lang_CXX);
  963. EXPECT_TRUE(ToTemplated1);
  964. EXPECT_EQ(ToTemplated1, ToTemplated);
  965. }
  966. TEST_P(ASTImporterOptionSpecificTestBase,
  967. ImportOfTemplatedDeclOfFunctionTemplateDecl) {
  968. Decl *FromTU = getTuDecl("template<class X> void f(){}", Lang_CXX);
  969. auto From = FirstDeclMatcher<FunctionTemplateDecl>().match(
  970. FromTU, functionTemplateDecl());
  971. ASSERT_TRUE(From);
  972. auto To = cast<FunctionTemplateDecl>(Import(From, Lang_CXX));
  973. ASSERT_TRUE(To);
  974. Decl *ToTemplated = To->getTemplatedDecl();
  975. Decl *ToTemplated1 = Import(From->getTemplatedDecl(), Lang_CXX);
  976. EXPECT_TRUE(ToTemplated1);
  977. EXPECT_EQ(ToTemplated1, ToTemplated);
  978. }
  979. TEST_P(ASTImporterOptionSpecificTestBase,
  980. ImportOfTemplatedDeclShouldImportTheClassTemplateDecl) {
  981. Decl *FromTU = getTuDecl("template<class X> struct S{};", Lang_CXX);
  982. auto FromFT =
  983. FirstDeclMatcher<ClassTemplateDecl>().match(FromTU, classTemplateDecl());
  984. ASSERT_TRUE(FromFT);
  985. auto ToTemplated =
  986. cast<CXXRecordDecl>(Import(FromFT->getTemplatedDecl(), Lang_CXX));
  987. EXPECT_TRUE(ToTemplated);
  988. auto ToTU = ToTemplated->getTranslationUnitDecl();
  989. auto ToFT =
  990. FirstDeclMatcher<ClassTemplateDecl>().match(ToTU, classTemplateDecl());
  991. EXPECT_TRUE(ToFT);
  992. }
  993. TEST_P(ASTImporterOptionSpecificTestBase,
  994. ImportOfTemplatedDeclShouldImportTheFunctionTemplateDecl) {
  995. Decl *FromTU = getTuDecl("template<class X> void f(){}", Lang_CXX);
  996. auto FromFT = FirstDeclMatcher<FunctionTemplateDecl>().match(
  997. FromTU, functionTemplateDecl());
  998. ASSERT_TRUE(FromFT);
  999. auto ToTemplated =
  1000. cast<FunctionDecl>(Import(FromFT->getTemplatedDecl(), Lang_CXX));
  1001. EXPECT_TRUE(ToTemplated);
  1002. auto ToTU = ToTemplated->getTranslationUnitDecl();
  1003. auto ToFT = FirstDeclMatcher<FunctionTemplateDecl>().match(
  1004. ToTU, functionTemplateDecl());
  1005. EXPECT_TRUE(ToFT);
  1006. }
  1007. TEST_P(ASTImporterOptionSpecificTestBase, ImportCorrectTemplatedDecl) {
  1008. auto Code =
  1009. R"(
  1010. namespace x {
  1011. template<class X> struct S1{};
  1012. template<class X> struct S2{};
  1013. template<class X> struct S3{};
  1014. }
  1015. )";
  1016. Decl *FromTU = getTuDecl(Code, Lang_CXX);
  1017. auto FromNs =
  1018. FirstDeclMatcher<NamespaceDecl>().match(FromTU, namespaceDecl());
  1019. auto ToNs = cast<NamespaceDecl>(Import(FromNs, Lang_CXX));
  1020. ASSERT_TRUE(ToNs);
  1021. auto From =
  1022. FirstDeclMatcher<ClassTemplateDecl>().match(FromTU,
  1023. classTemplateDecl(
  1024. hasName("S2")));
  1025. auto To =
  1026. FirstDeclMatcher<ClassTemplateDecl>().match(ToNs,
  1027. classTemplateDecl(
  1028. hasName("S2")));
  1029. ASSERT_TRUE(From);
  1030. ASSERT_TRUE(To);
  1031. auto ToTemplated = To->getTemplatedDecl();
  1032. auto ToTemplated1 =
  1033. cast<CXXRecordDecl>(Import(From->getTemplatedDecl(), Lang_CXX));
  1034. EXPECT_TRUE(ToTemplated1);
  1035. ASSERT_EQ(ToTemplated1, ToTemplated);
  1036. }
  1037. TEST_P(ASTImporterOptionSpecificTestBase, ImportChooseExpr) {
  1038. // This tests the import of isConditionTrue directly to make sure the importer
  1039. // gets it right.
  1040. Decl *From, *To;
  1041. std::tie(From, To) = getImportedDecl(
  1042. "void declToImport() { (void)__builtin_choose_expr(1, 0, 1); }",
  1043. Lang_C, "", Lang_C);
  1044. auto ToResults = match(chooseExpr().bind("choose"), To->getASTContext());
  1045. auto FromResults = match(chooseExpr().bind("choose"), From->getASTContext());
  1046. const ChooseExpr *FromChooseExpr =
  1047. selectFirst<ChooseExpr>("choose", FromResults);
  1048. ASSERT_TRUE(FromChooseExpr);
  1049. const ChooseExpr *ToChooseExpr = selectFirst<ChooseExpr>("choose", ToResults);
  1050. ASSERT_TRUE(ToChooseExpr);
  1051. EXPECT_EQ(FromChooseExpr->isConditionTrue(), ToChooseExpr->isConditionTrue());
  1052. EXPECT_EQ(FromChooseExpr->isConditionDependent(),
  1053. ToChooseExpr->isConditionDependent());
  1054. }
  1055. TEST_P(ASTImporterOptionSpecificTestBase,
  1056. ImportFunctionWithBackReferringParameter) {
  1057. Decl *From, *To;
  1058. std::tie(From, To) = getImportedDecl(
  1059. R"(
  1060. template <typename T> struct X {};
  1061. void declToImport(int y, X<int> &x) {}
  1062. template <> struct X<int> {
  1063. void g() {
  1064. X<int> x;
  1065. declToImport(0, x);
  1066. }
  1067. };
  1068. )",
  1069. Lang_CXX, "", Lang_CXX);
  1070. MatchVerifier<Decl> Verifier;
  1071. auto Matcher = functionDecl(hasName("declToImport"),
  1072. parameterCountIs(2),
  1073. hasParameter(0, hasName("y")),
  1074. hasParameter(1, hasName("x")),
  1075. hasParameter(1, hasType(asString("X<int> &"))));
  1076. ASSERT_TRUE(Verifier.match(From, Matcher));
  1077. EXPECT_TRUE(Verifier.match(To, Matcher));
  1078. }
  1079. TEST_P(ASTImporterOptionSpecificTestBase,
  1080. TUshouldNotContainTemplatedDeclOfFunctionTemplates) {
  1081. Decl *From, *To;
  1082. std::tie(From, To) =
  1083. getImportedDecl("template <typename T> void declToImport() { T a = 1; }"
  1084. "void instantiate() { declToImport<int>(); }",
  1085. Lang_CXX, "", Lang_CXX);
  1086. auto Check = [](Decl *D) -> bool {
  1087. auto TU = D->getTranslationUnitDecl();
  1088. for (auto Child : TU->decls()) {
  1089. if (auto *FD = dyn_cast<FunctionDecl>(Child)) {
  1090. if (FD->getNameAsString() == "declToImport") {
  1091. GTEST_NONFATAL_FAILURE_(
  1092. "TU should not contain any FunctionDecl with name declToImport");
  1093. return false;
  1094. }
  1095. }
  1096. }
  1097. return true;
  1098. };
  1099. ASSERT_TRUE(Check(From));
  1100. EXPECT_TRUE(Check(To));
  1101. }
  1102. TEST_P(ASTImporterOptionSpecificTestBase,
  1103. TUshouldNotContainTemplatedDeclOfClassTemplates) {
  1104. Decl *From, *To;
  1105. std::tie(From, To) =
  1106. getImportedDecl("template <typename T> struct declToImport { T t; };"
  1107. "void instantiate() { declToImport<int>(); }",
  1108. Lang_CXX, "", Lang_CXX);
  1109. auto Check = [](Decl *D) -> bool {
  1110. auto TU = D->getTranslationUnitDecl();
  1111. for (auto Child : TU->decls()) {
  1112. if (auto *RD = dyn_cast<CXXRecordDecl>(Child)) {
  1113. if (RD->getNameAsString() == "declToImport") {
  1114. GTEST_NONFATAL_FAILURE_(
  1115. "TU should not contain any CXXRecordDecl with name declToImport");
  1116. return false;
  1117. }
  1118. }
  1119. }
  1120. return true;
  1121. };
  1122. ASSERT_TRUE(Check(From));
  1123. EXPECT_TRUE(Check(To));
  1124. }
  1125. TEST_P(ASTImporterOptionSpecificTestBase,
  1126. TUshouldNotContainTemplatedDeclOfTypeAlias) {
  1127. Decl *From, *To;
  1128. std::tie(From, To) =
  1129. getImportedDecl(
  1130. "template <typename T> struct X {};"
  1131. "template <typename T> using declToImport = X<T>;"
  1132. "void instantiate() { declToImport<int> a; }",
  1133. Lang_CXX11, "", Lang_CXX11);
  1134. auto Check = [](Decl *D) -> bool {
  1135. auto TU = D->getTranslationUnitDecl();
  1136. for (auto Child : TU->decls()) {
  1137. if (auto *AD = dyn_cast<TypeAliasDecl>(Child)) {
  1138. if (AD->getNameAsString() == "declToImport") {
  1139. GTEST_NONFATAL_FAILURE_(
  1140. "TU should not contain any TypeAliasDecl with name declToImport");
  1141. return false;
  1142. }
  1143. }
  1144. }
  1145. return true;
  1146. };
  1147. ASSERT_TRUE(Check(From));
  1148. EXPECT_TRUE(Check(To));
  1149. }
  1150. TEST_P(ASTImporterOptionSpecificTestBase,
  1151. TUshouldNotContainClassTemplateSpecializationOfImplicitInstantiation) {
  1152. Decl *From, *To;
  1153. std::tie(From, To) = getImportedDecl(
  1154. R"(
  1155. template<class T>
  1156. class Base {};
  1157. class declToImport : public Base<declToImport> {};
  1158. )",
  1159. Lang_CXX, "", Lang_CXX);
  1160. // Check that the ClassTemplateSpecializationDecl is NOT the child of the TU.
  1161. auto Pattern =
  1162. translationUnitDecl(unless(has(classTemplateSpecializationDecl())));
  1163. ASSERT_TRUE(
  1164. MatchVerifier<Decl>{}.match(From->getTranslationUnitDecl(), Pattern));
  1165. EXPECT_TRUE(
  1166. MatchVerifier<Decl>{}.match(To->getTranslationUnitDecl(), Pattern));
  1167. // Check that the ClassTemplateSpecializationDecl is the child of the
  1168. // ClassTemplateDecl.
  1169. Pattern = translationUnitDecl(has(classTemplateDecl(
  1170. hasName("Base"), has(classTemplateSpecializationDecl()))));
  1171. ASSERT_TRUE(
  1172. MatchVerifier<Decl>{}.match(From->getTranslationUnitDecl(), Pattern));
  1173. EXPECT_TRUE(
  1174. MatchVerifier<Decl>{}.match(To->getTranslationUnitDecl(), Pattern));
  1175. }
  1176. AST_MATCHER_P(RecordDecl, hasFieldOrder, std::vector<StringRef>, Order) {
  1177. size_t Index = 0;
  1178. for (FieldDecl *Field : Node.fields()) {
  1179. if (Index == Order.size())
  1180. return false;
  1181. if (Field->getName() != Order[Index])
  1182. return false;
  1183. ++Index;
  1184. }
  1185. return Index == Order.size();
  1186. }
  1187. TEST_P(ASTImporterOptionSpecificTestBase,
  1188. TUshouldContainClassTemplateSpecializationOfExplicitInstantiation) {
  1189. Decl *From, *To;
  1190. std::tie(From, To) = getImportedDecl(
  1191. R"(
  1192. namespace NS {
  1193. template<class T>
  1194. class X {};
  1195. template class X<int>;
  1196. }
  1197. )",
  1198. Lang_CXX, "", Lang_CXX, "NS");
  1199. // Check that the ClassTemplateSpecializationDecl is NOT the child of the
  1200. // ClassTemplateDecl.
  1201. auto Pattern = namespaceDecl(has(classTemplateDecl(
  1202. hasName("X"), unless(has(classTemplateSpecializationDecl())))));
  1203. ASSERT_TRUE(MatchVerifier<Decl>{}.match(From, Pattern));
  1204. EXPECT_TRUE(MatchVerifier<Decl>{}.match(To, Pattern));
  1205. // Check that the ClassTemplateSpecializationDecl is the child of the
  1206. // NamespaceDecl.
  1207. Pattern = namespaceDecl(has(classTemplateSpecializationDecl(hasName("X"))));
  1208. ASSERT_TRUE(MatchVerifier<Decl>{}.match(From, Pattern));
  1209. EXPECT_TRUE(MatchVerifier<Decl>{}.match(To, Pattern));
  1210. }
  1211. TEST_P(ASTImporterOptionSpecificTestBase,
  1212. CXXRecordDeclFieldsShouldBeInCorrectOrder) {
  1213. Decl *From, *To;
  1214. std::tie(From, To) =
  1215. getImportedDecl(
  1216. "struct declToImport { int a; int b; };",
  1217. Lang_CXX11, "", Lang_CXX11);
  1218. MatchVerifier<Decl> Verifier;
  1219. ASSERT_TRUE(Verifier.match(From, cxxRecordDecl(hasFieldOrder({"a", "b"}))));
  1220. EXPECT_TRUE(Verifier.match(To, cxxRecordDecl(hasFieldOrder({"a", "b"}))));
  1221. }
  1222. TEST_P(ASTImporterOptionSpecificTestBase,
  1223. DISABLED_CXXRecordDeclFieldOrderShouldNotDependOnImportOrder) {
  1224. Decl *From, *To;
  1225. std::tie(From, To) = getImportedDecl(
  1226. // The original recursive algorithm of ASTImporter first imports 'c' then
  1227. // 'b' and lastly 'a'. Therefore we must restore the order somehow.
  1228. R"s(
  1229. struct declToImport {
  1230. int a = c + b;
  1231. int b = 1;
  1232. int c = 2;
  1233. };
  1234. )s",
  1235. Lang_CXX11, "", Lang_CXX11);
  1236. MatchVerifier<Decl> Verifier;
  1237. ASSERT_TRUE(
  1238. Verifier.match(From, cxxRecordDecl(hasFieldOrder({"a", "b", "c"}))));
  1239. EXPECT_TRUE(
  1240. Verifier.match(To, cxxRecordDecl(hasFieldOrder({"a", "b", "c"}))));
  1241. }
  1242. TEST_P(ASTImporterOptionSpecificTestBase, ShouldImportImplicitCXXRecordDecl) {
  1243. Decl *From, *To;
  1244. std::tie(From, To) = getImportedDecl(
  1245. R"(
  1246. struct declToImport {
  1247. };
  1248. )",
  1249. Lang_CXX, "", Lang_CXX);
  1250. MatchVerifier<Decl> Verifier;
  1251. // Match the implicit Decl.
  1252. auto Matcher = cxxRecordDecl(has(cxxRecordDecl()));
  1253. ASSERT_TRUE(Verifier.match(From, Matcher));
  1254. EXPECT_TRUE(Verifier.match(To, Matcher));
  1255. }
  1256. TEST_P(ASTImporterOptionSpecificTestBase,
  1257. ShouldImportImplicitCXXRecordDeclOfClassTemplate) {
  1258. Decl *From, *To;
  1259. std::tie(From, To) = getImportedDecl(
  1260. R"(
  1261. template <typename U>
  1262. struct declToImport {
  1263. };
  1264. )",
  1265. Lang_CXX, "", Lang_CXX);
  1266. MatchVerifier<Decl> Verifier;
  1267. // Match the implicit Decl.
  1268. auto Matcher = classTemplateDecl(has(cxxRecordDecl(has(cxxRecordDecl()))));
  1269. ASSERT_TRUE(Verifier.match(From, Matcher));
  1270. EXPECT_TRUE(Verifier.match(To, Matcher));
  1271. }
  1272. TEST_P(ASTImporterOptionSpecificTestBase,
  1273. ShouldImportImplicitCXXRecordDeclOfClassTemplateSpecializationDecl) {
  1274. Decl *From, *To;
  1275. std::tie(From, To) = getImportedDecl(
  1276. R"(
  1277. template<class T>
  1278. class Base {};
  1279. class declToImport : public Base<declToImport> {};
  1280. )",
  1281. Lang_CXX, "", Lang_CXX);
  1282. auto hasImplicitClass = has(cxxRecordDecl());
  1283. auto Pattern = translationUnitDecl(has(classTemplateDecl(
  1284. hasName("Base"),
  1285. has(classTemplateSpecializationDecl(hasImplicitClass)))));
  1286. ASSERT_TRUE(
  1287. MatchVerifier<Decl>{}.match(From->getTranslationUnitDecl(), Pattern));
  1288. EXPECT_TRUE(
  1289. MatchVerifier<Decl>{}.match(To->getTranslationUnitDecl(), Pattern));
  1290. }
  1291. TEST_P(ASTImporterOptionSpecificTestBase, IDNSOrdinary) {
  1292. Decl *From, *To;
  1293. std::tie(From, To) =
  1294. getImportedDecl("void declToImport() {}", Lang_CXX, "", Lang_CXX);
  1295. MatchVerifier<Decl> Verifier;
  1296. auto Matcher = functionDecl();
  1297. ASSERT_TRUE(Verifier.match(From, Matcher));
  1298. EXPECT_TRUE(Verifier.match(To, Matcher));
  1299. EXPECT_EQ(From->getIdentifierNamespace(), To->getIdentifierNamespace());
  1300. }
  1301. TEST_P(ASTImporterOptionSpecificTestBase, IDNSOfNonmemberOperator) {
  1302. Decl *FromTU = getTuDecl(
  1303. R"(
  1304. struct X {};
  1305. void operator<<(int, X);
  1306. )",
  1307. Lang_CXX);
  1308. Decl *From = LastDeclMatcher<Decl>{}.match(FromTU, functionDecl());
  1309. const Decl *To = Import(From, Lang_CXX);
  1310. EXPECT_EQ(From->getIdentifierNamespace(), To->getIdentifierNamespace());
  1311. }
  1312. TEST_P(ASTImporterOptionSpecificTestBase,
  1313. ShouldImportMembersOfClassTemplateSpecializationDecl) {
  1314. Decl *From, *To;
  1315. std::tie(From, To) = getImportedDecl(
  1316. R"(
  1317. template<class T>
  1318. class Base { int a; };
  1319. class declToImport : Base<declToImport> {};
  1320. )",
  1321. Lang_CXX, "", Lang_CXX);
  1322. auto Pattern = translationUnitDecl(has(classTemplateDecl(
  1323. hasName("Base"),
  1324. has(classTemplateSpecializationDecl(has(fieldDecl(hasName("a"))))))));
  1325. ASSERT_TRUE(
  1326. MatchVerifier<Decl>{}.match(From->getTranslationUnitDecl(), Pattern));
  1327. EXPECT_TRUE(
  1328. MatchVerifier<Decl>{}.match(To->getTranslationUnitDecl(), Pattern));
  1329. }
  1330. TEST_P(ASTImporterOptionSpecificTestBase,
  1331. ImportDefinitionOfClassTemplateAfterFwdDecl) {
  1332. {
  1333. Decl *FromTU = getTuDecl(
  1334. R"(
  1335. template <typename T>
  1336. struct B;
  1337. )",
  1338. Lang_CXX, "input0.cc");
  1339. auto *FromD = FirstDeclMatcher<ClassTemplateDecl>().match(
  1340. FromTU, classTemplateDecl(hasName("B")));
  1341. Import(FromD, Lang_CXX);
  1342. }
  1343. {
  1344. Decl *FromTU = getTuDecl(
  1345. R"(
  1346. template <typename T>
  1347. struct B {
  1348. void f();
  1349. };
  1350. )",
  1351. Lang_CXX, "input1.cc");
  1352. FunctionDecl *FromD = FirstDeclMatcher<FunctionDecl>().match(
  1353. FromTU, functionDecl(hasName("f")));
  1354. Import(FromD, Lang_CXX);
  1355. auto *FromCTD = FirstDeclMatcher<ClassTemplateDecl>().match(
  1356. FromTU, classTemplateDecl(hasName("B")));
  1357. auto *ToCTD = cast<ClassTemplateDecl>(Import(FromCTD, Lang_CXX));
  1358. EXPECT_TRUE(ToCTD->isThisDeclarationADefinition());
  1359. }
  1360. }
  1361. TEST_P(ASTImporterOptionSpecificTestBase,
  1362. ImportDefinitionOfClassTemplateIfThereIsAnExistingFwdDeclAndDefinition) {
  1363. Decl *ToTU = getToTuDecl(
  1364. R"(
  1365. template <typename T>
  1366. struct B {
  1367. void f();
  1368. };
  1369. template <typename T>
  1370. struct B;
  1371. )",
  1372. Lang_CXX);
  1373. ASSERT_EQ(1u, DeclCounterWithPredicate<ClassTemplateDecl>(
  1374. [](const ClassTemplateDecl *T) {
  1375. return T->isThisDeclarationADefinition();
  1376. })
  1377. .match(ToTU, classTemplateDecl()));
  1378. Decl *FromTU = getTuDecl(
  1379. R"(
  1380. template <typename T>
  1381. struct B {
  1382. void f();
  1383. };
  1384. )",
  1385. Lang_CXX, "input1.cc");
  1386. ClassTemplateDecl *FromD = FirstDeclMatcher<ClassTemplateDecl>().match(
  1387. FromTU, classTemplateDecl(hasName("B")));
  1388. Import(FromD, Lang_CXX);
  1389. // We should have only one definition.
  1390. EXPECT_EQ(1u, DeclCounterWithPredicate<ClassTemplateDecl>(
  1391. [](const ClassTemplateDecl *T) {
  1392. return T->isThisDeclarationADefinition();
  1393. })
  1394. .match(ToTU, classTemplateDecl()));
  1395. }
  1396. TEST_P(ASTImporterOptionSpecificTestBase,
  1397. ImportDefinitionOfClassIfThereIsAnExistingFwdDeclAndDefinition) {
  1398. Decl *ToTU = getToTuDecl(
  1399. R"(
  1400. struct B {
  1401. void f();
  1402. };
  1403. struct B;
  1404. )",
  1405. Lang_CXX);
  1406. ASSERT_EQ(2u, DeclCounter<CXXRecordDecl>().match(
  1407. ToTU, cxxRecordDecl(unless(isImplicit()))));
  1408. Decl *FromTU = getTuDecl(
  1409. R"(
  1410. struct B {
  1411. void f();
  1412. };
  1413. )",
  1414. Lang_CXX, "input1.cc");
  1415. auto *FromD = FirstDeclMatcher<CXXRecordDecl>().match(
  1416. FromTU, cxxRecordDecl(hasName("B")));
  1417. Import(FromD, Lang_CXX);
  1418. EXPECT_EQ(2u, DeclCounter<CXXRecordDecl>().match(
  1419. ToTU, cxxRecordDecl(unless(isImplicit()))));
  1420. }
  1421. static void CompareSourceLocs(FullSourceLoc Loc1, FullSourceLoc Loc2) {
  1422. EXPECT_EQ(Loc1.getExpansionLineNumber(), Loc2.getExpansionLineNumber());
  1423. EXPECT_EQ(Loc1.getExpansionColumnNumber(), Loc2.getExpansionColumnNumber());
  1424. EXPECT_EQ(Loc1.getSpellingLineNumber(), Loc2.getSpellingLineNumber());
  1425. EXPECT_EQ(Loc1.getSpellingColumnNumber(), Loc2.getSpellingColumnNumber());
  1426. }
  1427. static void CompareSourceRanges(SourceRange Range1, SourceRange Range2,
  1428. SourceManager &SM1, SourceManager &SM2) {
  1429. CompareSourceLocs(FullSourceLoc{ Range1.getBegin(), SM1 },
  1430. FullSourceLoc{ Range2.getBegin(), SM2 });
  1431. CompareSourceLocs(FullSourceLoc{ Range1.getEnd(), SM1 },
  1432. FullSourceLoc{ Range2.getEnd(), SM2 });
  1433. }
  1434. TEST_P(ASTImporterOptionSpecificTestBase, ImportSourceLocs) {
  1435. Decl *FromTU = getTuDecl(
  1436. R"(
  1437. #define MFOO(arg) arg = arg + 1
  1438. void foo() {
  1439. int a = 5;
  1440. MFOO(a);
  1441. }
  1442. )",
  1443. Lang_CXX);
  1444. auto FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, functionDecl());
  1445. auto ToD = Import(FromD, Lang_CXX);
  1446. auto ToLHS = LastDeclMatcher<DeclRefExpr>().match(ToD, declRefExpr());
  1447. auto FromLHS = LastDeclMatcher<DeclRefExpr>().match(FromTU, declRefExpr());
  1448. auto ToRHS = LastDeclMatcher<IntegerLiteral>().match(ToD, integerLiteral());
  1449. auto FromRHS =
  1450. LastDeclMatcher<IntegerLiteral>().match(FromTU, integerLiteral());
  1451. SourceManager &ToSM = ToAST->getASTContext().getSourceManager();
  1452. SourceManager &FromSM = FromD->getASTContext().getSourceManager();
  1453. CompareSourceRanges(ToD->getSourceRange(), FromD->getSourceRange(), ToSM,
  1454. FromSM);
  1455. CompareSourceRanges(ToLHS->getSourceRange(), FromLHS->getSourceRange(), ToSM,
  1456. FromSM);
  1457. CompareSourceRanges(ToRHS->getSourceRange(), FromRHS->getSourceRange(), ToSM,
  1458. FromSM);
  1459. }
  1460. TEST_P(ASTImporterOptionSpecificTestBase, ImportNestedMacro) {
  1461. Decl *FromTU = getTuDecl(
  1462. R"(
  1463. #define FUNC_INT void declToImport
  1464. #define FUNC FUNC_INT
  1465. FUNC(int a);
  1466. )",
  1467. Lang_CXX);
  1468. auto FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, functionDecl());
  1469. auto ToD = Import(FromD, Lang_CXX);
  1470. SourceManager &ToSM = ToAST->getASTContext().getSourceManager();
  1471. SourceManager &FromSM = FromD->getASTContext().getSourceManager();
  1472. CompareSourceRanges(ToD->getSourceRange(), FromD->getSourceRange(), ToSM,
  1473. FromSM);
  1474. }
  1475. TEST_P(
  1476. ASTImporterOptionSpecificTestBase,
  1477. ImportDefinitionOfClassTemplateSpecIfThereIsAnExistingFwdDeclAndDefinition) {
  1478. Decl *ToTU = getToTuDecl(
  1479. R"(
  1480. template <typename T>
  1481. struct B;
  1482. template <>
  1483. struct B<int> {};
  1484. template <>
  1485. struct B<int>;
  1486. )",
  1487. Lang_CXX);
  1488. // We should have only one definition.
  1489. ASSERT_EQ(1u, DeclCounterWithPredicate<ClassTemplateSpecializationDecl>(
  1490. [](const ClassTemplateSpecializationDecl *T) {
  1491. return T->isThisDeclarationADefinition();
  1492. })
  1493. .match(ToTU, classTemplateSpecializationDecl()));
  1494. Decl *FromTU = getTuDecl(
  1495. R"(
  1496. template <typename T>
  1497. struct B;
  1498. template <>
  1499. struct B<int> {};
  1500. )",
  1501. Lang_CXX, "input1.cc");
  1502. auto *FromD = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  1503. FromTU, classTemplateSpecializationDecl(hasName("B")));
  1504. Import(FromD, Lang_CXX);
  1505. // We should have only one definition.
  1506. EXPECT_EQ(1u, DeclCounterWithPredicate<ClassTemplateSpecializationDecl>(
  1507. [](const ClassTemplateSpecializationDecl *T) {
  1508. return T->isThisDeclarationADefinition();
  1509. })
  1510. .match(ToTU, classTemplateSpecializationDecl()));
  1511. }
  1512. TEST_P(ASTImporterOptionSpecificTestBase, ObjectsWithUnnamedStructType) {
  1513. Decl *FromTU = getTuDecl(
  1514. R"(
  1515. struct { int a; int b; } object0 = { 2, 3 };
  1516. struct { int x; int y; int z; } object1;
  1517. )",
  1518. Lang_CXX, "input0.cc");
  1519. auto *Obj0 =
  1520. FirstDeclMatcher<VarDecl>().match(FromTU, varDecl(hasName("object0")));
  1521. auto *From0 = getRecordDecl(Obj0);
  1522. auto *Obj1 =
  1523. FirstDeclMatcher<VarDecl>().match(FromTU, varDecl(hasName("object1")));
  1524. auto *From1 = getRecordDecl(Obj1);
  1525. auto *To0 = Import(From0, Lang_CXX);
  1526. auto *To1 = Import(From1, Lang_CXX);
  1527. EXPECT_TRUE(To0);
  1528. EXPECT_TRUE(To1);
  1529. EXPECT_NE(To0, To1);
  1530. EXPECT_NE(To0->getCanonicalDecl(), To1->getCanonicalDecl());
  1531. }
  1532. TEST_P(ASTImporterOptionSpecificTestBase, AnonymousRecords) {
  1533. auto *Code =
  1534. R"(
  1535. struct X {
  1536. struct { int a; };
  1537. struct { int b; };
  1538. };
  1539. )";
  1540. Decl *FromTU0 = getTuDecl(Code, Lang_C, "input0.c");
  1541. Decl *FromTU1 = getTuDecl(Code, Lang_C, "input1.c");
  1542. auto *X0 =
  1543. FirstDeclMatcher<RecordDecl>().match(FromTU0, recordDecl(hasName("X")));
  1544. auto *X1 =
  1545. FirstDeclMatcher<RecordDecl>().match(FromTU1, recordDecl(hasName("X")));
  1546. Import(X0, Lang_C);
  1547. Import(X1, Lang_C);
  1548. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1549. // We expect no (ODR) warning during the import.
  1550. EXPECT_EQ(0u, ToTU->getASTContext().getDiagnostics().getNumWarnings());
  1551. EXPECT_EQ(1u,
  1552. DeclCounter<RecordDecl>().match(ToTU, recordDecl(hasName("X"))));
  1553. }
  1554. TEST_P(ASTImporterOptionSpecificTestBase, AnonymousRecordsReversed) {
  1555. Decl *FromTU0 = getTuDecl(
  1556. R"(
  1557. struct X {
  1558. struct { int a; };
  1559. struct { int b; };
  1560. };
  1561. )",
  1562. Lang_C, "input0.c");
  1563. Decl *FromTU1 = getTuDecl(
  1564. R"(
  1565. struct X { // reversed order
  1566. struct { int b; };
  1567. struct { int a; };
  1568. };
  1569. )",
  1570. Lang_C, "input1.c");
  1571. auto *X0 =
  1572. FirstDeclMatcher<RecordDecl>().match(FromTU0, recordDecl(hasName("X")));
  1573. auto *X1 =
  1574. FirstDeclMatcher<RecordDecl>().match(FromTU1, recordDecl(hasName("X")));
  1575. Import(X0, Lang_C);
  1576. Import(X1, Lang_C);
  1577. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1578. // We expect one (ODR) warning during the import.
  1579. EXPECT_EQ(1u, ToTU->getASTContext().getDiagnostics().getNumWarnings());
  1580. EXPECT_EQ(2u,
  1581. DeclCounter<RecordDecl>().match(ToTU, recordDecl(hasName("X"))));
  1582. }
  1583. TEST_P(ASTImporterOptionSpecificTestBase, ImportDoesUpdateUsedFlag) {
  1584. auto Pattern = varDecl(hasName("x"));
  1585. VarDecl *Imported1;
  1586. {
  1587. Decl *FromTU = getTuDecl("extern int x;", Lang_CXX, "input0.cc");
  1588. auto *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern);
  1589. Imported1 = cast<VarDecl>(Import(FromD, Lang_CXX));
  1590. }
  1591. VarDecl *Imported2;
  1592. {
  1593. Decl *FromTU = getTuDecl("int x;", Lang_CXX, "input1.cc");
  1594. auto *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern);
  1595. Imported2 = cast<VarDecl>(Import(FromD, Lang_CXX));
  1596. }
  1597. EXPECT_EQ(Imported1->getCanonicalDecl(), Imported2->getCanonicalDecl());
  1598. EXPECT_FALSE(Imported2->isUsed(false));
  1599. {
  1600. Decl *FromTU =
  1601. getTuDecl("extern int x; int f() { return x; }", Lang_CXX, "input2.cc");
  1602. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  1603. FromTU, functionDecl(hasName("f")));
  1604. Import(FromD, Lang_CXX);
  1605. }
  1606. EXPECT_TRUE(Imported2->isUsed(false));
  1607. }
  1608. TEST_P(ASTImporterOptionSpecificTestBase, ImportDoesUpdateUsedFlag2) {
  1609. auto Pattern = varDecl(hasName("x"));
  1610. VarDecl *ExistingD;
  1611. {
  1612. Decl *ToTU = getToTuDecl("int x = 1;", Lang_CXX);
  1613. ExistingD = FirstDeclMatcher<VarDecl>().match(ToTU, Pattern);
  1614. }
  1615. EXPECT_FALSE(ExistingD->isUsed(false));
  1616. {
  1617. Decl *FromTU = getTuDecl(
  1618. "int x = 1; int f() { return x; }", Lang_CXX, "input1.cc");
  1619. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  1620. FromTU, functionDecl(hasName("f")));
  1621. Import(FromD, Lang_CXX);
  1622. }
  1623. EXPECT_TRUE(ExistingD->isUsed(false));
  1624. }
  1625. TEST_P(ASTImporterOptionSpecificTestBase, ImportDoesUpdateUsedFlag3) {
  1626. auto Pattern = varDecl(hasName("a"));
  1627. VarDecl *ExistingD;
  1628. {
  1629. Decl *ToTU = getToTuDecl(
  1630. R"(
  1631. struct A {
  1632. static const int a = 1;
  1633. };
  1634. )", Lang_CXX);
  1635. ExistingD = FirstDeclMatcher<VarDecl>().match(ToTU, Pattern);
  1636. }
  1637. EXPECT_FALSE(ExistingD->isUsed(false));
  1638. {
  1639. Decl *FromTU = getTuDecl(
  1640. R"(
  1641. struct A {
  1642. static const int a = 1;
  1643. };
  1644. const int *f() { return &A::a; } // requires storage,
  1645. // thus used flag will be set
  1646. )", Lang_CXX, "input1.cc");
  1647. auto *FromFunD = FirstDeclMatcher<FunctionDecl>().match(
  1648. FromTU, functionDecl(hasName("f")));
  1649. auto *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern);
  1650. ASSERT_TRUE(FromD->isUsed(false));
  1651. Import(FromFunD, Lang_CXX);
  1652. }
  1653. EXPECT_TRUE(ExistingD->isUsed(false));
  1654. }
  1655. TEST_P(ASTImporterOptionSpecificTestBase, ReimportWithUsedFlag) {
  1656. auto Pattern = varDecl(hasName("x"));
  1657. Decl *FromTU = getTuDecl("int x;", Lang_CXX, "input0.cc");
  1658. auto *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern);
  1659. auto *Imported1 = cast<VarDecl>(Import(FromD, Lang_CXX));
  1660. ASSERT_FALSE(Imported1->isUsed(false));
  1661. FromD->setIsUsed();
  1662. auto *Imported2 = cast<VarDecl>(Import(FromD, Lang_CXX));
  1663. EXPECT_EQ(Imported1, Imported2);
  1664. EXPECT_TRUE(Imported2->isUsed(false));
  1665. }
  1666. struct ImportFunctions : ASTImporterOptionSpecificTestBase {};
  1667. TEST_P(ImportFunctions, ImportPrototypeOfRecursiveFunction) {
  1668. Decl *FromTU = getTuDecl("void f(); void f() { f(); }", Lang_CXX);
  1669. auto Pattern = functionDecl(hasName("f"));
  1670. auto *From =
  1671. FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern); // Proto
  1672. Decl *ImportedD = Import(From, Lang_CXX);
  1673. Decl *ToTU = ImportedD->getTranslationUnitDecl();
  1674. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1675. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1676. auto *To1 = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1677. EXPECT_TRUE(ImportedD == To0);
  1678. EXPECT_FALSE(To0->doesThisDeclarationHaveABody());
  1679. EXPECT_TRUE(To1->doesThisDeclarationHaveABody());
  1680. EXPECT_EQ(To1->getPreviousDecl(), To0);
  1681. }
  1682. TEST_P(ImportFunctions, ImportDefinitionOfRecursiveFunction) {
  1683. Decl *FromTU = getTuDecl("void f(); void f() { f(); }", Lang_CXX);
  1684. auto Pattern = functionDecl(hasName("f"));
  1685. auto *From =
  1686. LastDeclMatcher<FunctionDecl>().match(FromTU, Pattern); // Def
  1687. Decl *ImportedD = Import(From, Lang_CXX);
  1688. Decl *ToTU = ImportedD->getTranslationUnitDecl();
  1689. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1690. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1691. auto *To1 = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1692. EXPECT_TRUE(ImportedD == To1);
  1693. EXPECT_FALSE(To0->doesThisDeclarationHaveABody());
  1694. EXPECT_TRUE(To1->doesThisDeclarationHaveABody());
  1695. EXPECT_EQ(To1->getPreviousDecl(), To0);
  1696. }
  1697. TEST_P(ImportFunctions, OverriddenMethodsShouldBeImported) {
  1698. auto Code =
  1699. R"(
  1700. struct B { virtual void f(); };
  1701. void B::f() {}
  1702. struct D : B { void f(); };
  1703. )";
  1704. auto Pattern =
  1705. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("D"))));
  1706. Decl *FromTU = getTuDecl(Code, Lang_CXX);
  1707. CXXMethodDecl *Proto =
  1708. FirstDeclMatcher<CXXMethodDecl>().match(FromTU, Pattern);
  1709. ASSERT_EQ(Proto->size_overridden_methods(), 1u);
  1710. CXXMethodDecl *To = cast<CXXMethodDecl>(Import(Proto, Lang_CXX));
  1711. EXPECT_EQ(To->size_overridden_methods(), 1u);
  1712. }
  1713. TEST_P(ImportFunctions, VirtualFlagShouldBePreservedWhenImportingPrototype) {
  1714. auto Code =
  1715. R"(
  1716. struct B { virtual void f(); };
  1717. void B::f() {}
  1718. )";
  1719. auto Pattern =
  1720. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("B"))));
  1721. Decl *FromTU = getTuDecl(Code, Lang_CXX);
  1722. CXXMethodDecl *Proto =
  1723. FirstDeclMatcher<CXXMethodDecl>().match(FromTU, Pattern);
  1724. CXXMethodDecl *Def = LastDeclMatcher<CXXMethodDecl>().match(FromTU, Pattern);
  1725. ASSERT_TRUE(Proto->isVirtual());
  1726. ASSERT_TRUE(Def->isVirtual());
  1727. CXXMethodDecl *To = cast<CXXMethodDecl>(Import(Proto, Lang_CXX));
  1728. EXPECT_TRUE(To->isVirtual());
  1729. }
  1730. TEST_P(ImportFunctions,
  1731. ImportDefinitionIfThereIsAnExistingDefinitionAndFwdDecl) {
  1732. Decl *ToTU = getToTuDecl(
  1733. R"(
  1734. void f() {}
  1735. void f();
  1736. )",
  1737. Lang_CXX);
  1738. ASSERT_EQ(1u,
  1739. DeclCounterWithPredicate<FunctionDecl>([](const FunctionDecl *FD) {
  1740. return FD->doesThisDeclarationHaveABody();
  1741. }).match(ToTU, functionDecl()));
  1742. Decl *FromTU = getTuDecl("void f() {}", Lang_CXX, "input0.cc");
  1743. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, functionDecl());
  1744. Import(FromD, Lang_CXX);
  1745. EXPECT_EQ(1u,
  1746. DeclCounterWithPredicate<FunctionDecl>([](const FunctionDecl *FD) {
  1747. return FD->doesThisDeclarationHaveABody();
  1748. }).match(ToTU, functionDecl()));
  1749. }
  1750. TEST_P(ImportFunctions, ImportOverriddenMethodTwice) {
  1751. auto Code =
  1752. R"(
  1753. struct B { virtual void f(); };
  1754. struct D:B { void f(); };
  1755. )";
  1756. auto BFP =
  1757. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("B"))));
  1758. auto DFP =
  1759. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("D"))));
  1760. Decl *FromTU0 = getTuDecl(Code, Lang_CXX);
  1761. auto *DF = FirstDeclMatcher<CXXMethodDecl>().match(FromTU0, DFP);
  1762. Import(DF, Lang_CXX);
  1763. Decl *FromTU1 = getTuDecl(Code, Lang_CXX, "input1.cc");
  1764. auto *BF = FirstDeclMatcher<CXXMethodDecl>().match(FromTU1, BFP);
  1765. Import(BF, Lang_CXX);
  1766. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1767. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, BFP), 1u);
  1768. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, DFP), 1u);
  1769. }
  1770. TEST_P(ImportFunctions, ImportOverriddenMethodTwiceDefinitionFirst) {
  1771. auto CodeWithoutDef =
  1772. R"(
  1773. struct B { virtual void f(); };
  1774. struct D:B { void f(); };
  1775. )";
  1776. auto CodeWithDef =
  1777. R"(
  1778. struct B { virtual void f(){}; };
  1779. struct D:B { void f(){}; };
  1780. )";
  1781. auto BFP =
  1782. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("B"))));
  1783. auto DFP =
  1784. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("D"))));
  1785. auto BFDefP = cxxMethodDecl(
  1786. hasName("f"), hasParent(cxxRecordDecl(hasName("B"))), isDefinition());
  1787. auto DFDefP = cxxMethodDecl(
  1788. hasName("f"), hasParent(cxxRecordDecl(hasName("D"))), isDefinition());
  1789. auto FDefAllP = cxxMethodDecl(hasName("f"), isDefinition());
  1790. {
  1791. Decl *FromTU = getTuDecl(CodeWithDef, Lang_CXX, "input0.cc");
  1792. auto *FromD = FirstDeclMatcher<CXXMethodDecl>().match(FromTU, DFP);
  1793. Import(FromD, Lang_CXX);
  1794. }
  1795. {
  1796. Decl *FromTU = getTuDecl(CodeWithoutDef, Lang_CXX, "input1.cc");
  1797. auto *FromB = FirstDeclMatcher<CXXMethodDecl>().match(FromTU, BFP);
  1798. Import(FromB, Lang_CXX);
  1799. }
  1800. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1801. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, BFP), 1u);
  1802. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, DFP), 1u);
  1803. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, BFDefP), 1u);
  1804. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, DFDefP), 1u);
  1805. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, FDefAllP), 2u);
  1806. }
  1807. TEST_P(ImportFunctions, ImportOverriddenMethodTwiceOutOfClassDef) {
  1808. auto Code =
  1809. R"(
  1810. struct B { virtual void f(); };
  1811. struct D:B { void f(); };
  1812. void B::f(){};
  1813. )";
  1814. auto BFP =
  1815. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("B"))));
  1816. auto BFDefP = cxxMethodDecl(
  1817. hasName("f"), hasParent(cxxRecordDecl(hasName("B"))), isDefinition());
  1818. auto DFP = cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("D"))),
  1819. unless(isDefinition()));
  1820. Decl *FromTU0 = getTuDecl(Code, Lang_CXX);
  1821. auto *D = FirstDeclMatcher<CXXMethodDecl>().match(FromTU0, DFP);
  1822. Import(D, Lang_CXX);
  1823. Decl *FromTU1 = getTuDecl(Code, Lang_CXX, "input1.cc");
  1824. auto *B = FirstDeclMatcher<CXXMethodDecl>().match(FromTU1, BFP);
  1825. Import(B, Lang_CXX);
  1826. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1827. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, BFP), 1u);
  1828. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, BFDefP), 0u);
  1829. auto *ToB = FirstDeclMatcher<CXXRecordDecl>().match(
  1830. ToTU, cxxRecordDecl(hasName("B")));
  1831. auto *ToBFInClass = FirstDeclMatcher<CXXMethodDecl>().match(ToTU, BFP);
  1832. auto *ToBFOutOfClass = FirstDeclMatcher<CXXMethodDecl>().match(
  1833. ToTU, cxxMethodDecl(hasName("f"), isDefinition()));
  1834. // The definition should be out-of-class.
  1835. EXPECT_NE(ToBFInClass, ToBFOutOfClass);
  1836. EXPECT_NE(ToBFInClass->getLexicalDeclContext(),
  1837. ToBFOutOfClass->getLexicalDeclContext());
  1838. EXPECT_EQ(ToBFOutOfClass->getDeclContext(), ToB);
  1839. EXPECT_EQ(ToBFOutOfClass->getLexicalDeclContext(), ToTU);
  1840. // Check that the redecl chain is intact.
  1841. EXPECT_EQ(ToBFOutOfClass->getPreviousDecl(), ToBFInClass);
  1842. }
  1843. TEST_P(ImportFunctions,
  1844. ImportOverriddenMethodTwiceOutOfClassDefInSeparateCode) {
  1845. auto CodeTU0 =
  1846. R"(
  1847. struct B { virtual void f(); };
  1848. struct D:B { void f(); };
  1849. )";
  1850. auto CodeTU1 =
  1851. R"(
  1852. struct B { virtual void f(); };
  1853. struct D:B { void f(); };
  1854. void B::f(){}
  1855. void D::f(){}
  1856. void foo(B &b, D &d) { b.f(); d.f(); }
  1857. )";
  1858. auto BFP =
  1859. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("B"))));
  1860. auto BFDefP = cxxMethodDecl(
  1861. hasName("f"), hasParent(cxxRecordDecl(hasName("B"))), isDefinition());
  1862. auto DFP =
  1863. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("D"))));
  1864. auto DFDefP = cxxMethodDecl(
  1865. hasName("f"), hasParent(cxxRecordDecl(hasName("D"))), isDefinition());
  1866. auto FooDef = functionDecl(hasName("foo"));
  1867. {
  1868. Decl *FromTU0 = getTuDecl(CodeTU0, Lang_CXX, "input0.cc");
  1869. auto *D = FirstDeclMatcher<CXXMethodDecl>().match(FromTU0, DFP);
  1870. Import(D, Lang_CXX);
  1871. }
  1872. {
  1873. Decl *FromTU1 = getTuDecl(CodeTU1, Lang_CXX, "input1.cc");
  1874. auto *Foo = FirstDeclMatcher<FunctionDecl>().match(FromTU1, FooDef);
  1875. Import(Foo, Lang_CXX);
  1876. }
  1877. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1878. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, BFP), 1u);
  1879. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, DFP), 1u);
  1880. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, BFDefP), 0u);
  1881. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, DFDefP), 0u);
  1882. auto *ToB = FirstDeclMatcher<CXXRecordDecl>().match(
  1883. ToTU, cxxRecordDecl(hasName("B")));
  1884. auto *ToD = FirstDeclMatcher<CXXRecordDecl>().match(
  1885. ToTU, cxxRecordDecl(hasName("D")));
  1886. auto *ToBFInClass = FirstDeclMatcher<CXXMethodDecl>().match(ToTU, BFP);
  1887. auto *ToBFOutOfClass = FirstDeclMatcher<CXXMethodDecl>().match(
  1888. ToTU, cxxMethodDecl(hasName("f"), isDefinition()));
  1889. auto *ToDFInClass = FirstDeclMatcher<CXXMethodDecl>().match(ToTU, DFP);
  1890. auto *ToDFOutOfClass = LastDeclMatcher<CXXMethodDecl>().match(
  1891. ToTU, cxxMethodDecl(hasName("f"), isDefinition()));
  1892. // The definition should be out-of-class.
  1893. EXPECT_NE(ToBFInClass, ToBFOutOfClass);
  1894. EXPECT_NE(ToBFInClass->getLexicalDeclContext(),
  1895. ToBFOutOfClass->getLexicalDeclContext());
  1896. EXPECT_EQ(ToBFOutOfClass->getDeclContext(), ToB);
  1897. EXPECT_EQ(ToBFOutOfClass->getLexicalDeclContext(), ToTU);
  1898. EXPECT_NE(ToDFInClass, ToDFOutOfClass);
  1899. EXPECT_NE(ToDFInClass->getLexicalDeclContext(),
  1900. ToDFOutOfClass->getLexicalDeclContext());
  1901. EXPECT_EQ(ToDFOutOfClass->getDeclContext(), ToD);
  1902. EXPECT_EQ(ToDFOutOfClass->getLexicalDeclContext(), ToTU);
  1903. // Check that the redecl chain is intact.
  1904. EXPECT_EQ(ToBFOutOfClass->getPreviousDecl(), ToBFInClass);
  1905. EXPECT_EQ(ToDFOutOfClass->getPreviousDecl(), ToDFInClass);
  1906. }
  1907. TEST_P(ASTImporterOptionSpecificTestBase, ImportVariableChainInC) {
  1908. std::string Code = "static int v; static int v = 0;";
  1909. auto Pattern = varDecl(hasName("v"));
  1910. TranslationUnitDecl *FromTu = getTuDecl(Code, Lang_C, "input0.c");
  1911. auto *From0 = FirstDeclMatcher<VarDecl>().match(FromTu, Pattern);
  1912. auto *From1 = LastDeclMatcher<VarDecl>().match(FromTu, Pattern);
  1913. auto *To0 = Import(From0, Lang_C);
  1914. auto *To1 = Import(From1, Lang_C);
  1915. EXPECT_TRUE(To0);
  1916. ASSERT_TRUE(To1);
  1917. EXPECT_NE(To0, To1);
  1918. EXPECT_EQ(To1->getPreviousDecl(), To0);
  1919. }
  1920. TEST_P(ImportFunctions, ImportFromDifferentScopedAnonNamespace) {
  1921. TranslationUnitDecl *FromTu = getTuDecl(
  1922. "namespace NS0 { namespace { void f(); } }"
  1923. "namespace NS1 { namespace { void f(); } }",
  1924. Lang_CXX, "input0.cc");
  1925. auto Pattern = functionDecl(hasName("f"));
  1926. auto *FromF0 = FirstDeclMatcher<FunctionDecl>().match(FromTu, Pattern);
  1927. auto *FromF1 = LastDeclMatcher<FunctionDecl>().match(FromTu, Pattern);
  1928. auto *ToF0 = Import(FromF0, Lang_CXX);
  1929. auto *ToF1 = Import(FromF1, Lang_CXX);
  1930. EXPECT_TRUE(ToF0);
  1931. ASSERT_TRUE(ToF1);
  1932. EXPECT_NE(ToF0, ToF1);
  1933. EXPECT_FALSE(ToF1->getPreviousDecl());
  1934. }
  1935. TEST_P(ImportFunctions, ImportFunctionFromUnnamedNamespace) {
  1936. {
  1937. Decl *FromTU = getTuDecl("namespace { void f() {} } void g0() { f(); }",
  1938. Lang_CXX, "input0.cc");
  1939. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  1940. FromTU, functionDecl(hasName("g0")));
  1941. Import(FromD, Lang_CXX);
  1942. }
  1943. {
  1944. Decl *FromTU =
  1945. getTuDecl("namespace { void f() { int a; } } void g1() { f(); }",
  1946. Lang_CXX, "input1.cc");
  1947. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  1948. FromTU, functionDecl(hasName("g1")));
  1949. Import(FromD, Lang_CXX);
  1950. }
  1951. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1952. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, functionDecl(hasName("f"))),
  1953. 2u);
  1954. }
  1955. TEST_P(ImportFunctions, ImportImplicitFunctionsInLambda) {
  1956. Decl *FromTU = getTuDecl(
  1957. R"(
  1958. void foo() {
  1959. (void)[]() { ; };
  1960. }
  1961. )",
  1962. Lang_CXX11);
  1963. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  1964. FromTU, functionDecl(hasName("foo")));
  1965. auto *ToD = Import(FromD, Lang_CXX);
  1966. EXPECT_TRUE(ToD);
  1967. CXXRecordDecl *LambdaRec =
  1968. cast<LambdaExpr>(cast<CStyleCastExpr>(
  1969. *cast<CompoundStmt>(ToD->getBody())->body_begin())
  1970. ->getSubExpr())
  1971. ->getLambdaClass();
  1972. EXPECT_TRUE(LambdaRec->getDestructor());
  1973. }
  1974. TEST_P(ImportFunctions,
  1975. CallExprOfMemberFunctionTemplateWithExplicitTemplateArgs) {
  1976. Decl *FromTU = getTuDecl(
  1977. R"(
  1978. struct X {
  1979. template <typename T>
  1980. void foo(){}
  1981. };
  1982. void f() {
  1983. X x;
  1984. x.foo<int>();
  1985. }
  1986. )",
  1987. Lang_CXX);
  1988. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  1989. FromTU, functionDecl(hasName("f")));
  1990. auto *ToD = Import(FromD, Lang_CXX);
  1991. EXPECT_TRUE(ToD);
  1992. EXPECT_TRUE(MatchVerifier<FunctionDecl>().match(
  1993. ToD, functionDecl(hasName("f"), hasDescendant(declRefExpr()))));
  1994. }
  1995. TEST_P(ImportFunctions,
  1996. DependentCallExprOfMemberFunctionTemplateWithExplicitTemplateArgs) {
  1997. Decl *FromTU = getTuDecl(
  1998. R"(
  1999. struct X {
  2000. template <typename T>
  2001. void foo(){}
  2002. };
  2003. template <typename T>
  2004. void f() {
  2005. X x;
  2006. x.foo<T>();
  2007. }
  2008. void g() {
  2009. f<int>();
  2010. }
  2011. )",
  2012. Lang_CXX);
  2013. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  2014. FromTU, functionDecl(hasName("g")));
  2015. auto *ToD = Import(FromD, Lang_CXX);
  2016. EXPECT_TRUE(ToD);
  2017. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2018. EXPECT_TRUE(MatchVerifier<TranslationUnitDecl>().match(
  2019. ToTU, translationUnitDecl(hasDescendant(
  2020. functionDecl(hasName("f"), hasDescendant(declRefExpr()))))));
  2021. }
  2022. struct ImportFriendFunctions : ImportFunctions {};
  2023. TEST_P(ImportFriendFunctions, ImportFriendFunctionRedeclChainProto) {
  2024. auto Pattern = functionDecl(hasName("f"));
  2025. Decl *FromTU = getTuDecl("struct X { friend void f(); };"
  2026. "void f();",
  2027. Lang_CXX,
  2028. "input0.cc");
  2029. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2030. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2031. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2032. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2033. EXPECT_FALSE(ImportedD->doesThisDeclarationHaveABody());
  2034. auto *ToFD = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2035. EXPECT_FALSE(ToFD->doesThisDeclarationHaveABody());
  2036. EXPECT_EQ(ToFD->getPreviousDecl(), ImportedD);
  2037. }
  2038. TEST_P(ImportFriendFunctions,
  2039. ImportFriendFunctionRedeclChainProto_OutOfClassProtoFirst) {
  2040. auto Pattern = functionDecl(hasName("f"));
  2041. Decl *FromTU = getTuDecl("void f();"
  2042. "struct X { friend void f(); };",
  2043. Lang_CXX, "input0.cc");
  2044. auto FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2045. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2046. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2047. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2048. EXPECT_FALSE(ImportedD->doesThisDeclarationHaveABody());
  2049. auto *ToFD = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2050. EXPECT_FALSE(ToFD->doesThisDeclarationHaveABody());
  2051. EXPECT_EQ(ToFD->getPreviousDecl(), ImportedD);
  2052. }
  2053. TEST_P(ImportFriendFunctions, ImportFriendFunctionRedeclChainDef) {
  2054. auto Pattern = functionDecl(hasName("f"));
  2055. Decl *FromTU = getTuDecl("struct X { friend void f(){} };"
  2056. "void f();",
  2057. Lang_CXX,
  2058. "input0.cc");
  2059. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2060. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2061. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2062. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2063. EXPECT_TRUE(ImportedD->doesThisDeclarationHaveABody());
  2064. auto *ToFD = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2065. EXPECT_FALSE(ToFD->doesThisDeclarationHaveABody());
  2066. EXPECT_EQ(ToFD->getPreviousDecl(), ImportedD);
  2067. }
  2068. TEST_P(ImportFriendFunctions,
  2069. ImportFriendFunctionRedeclChainDef_OutOfClassDef) {
  2070. auto Pattern = functionDecl(hasName("f"));
  2071. Decl *FromTU = getTuDecl("struct X { friend void f(); };"
  2072. "void f(){}",
  2073. Lang_CXX, "input0.cc");
  2074. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2075. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2076. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2077. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2078. EXPECT_FALSE(ImportedD->doesThisDeclarationHaveABody());
  2079. auto *ToFD = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2080. EXPECT_TRUE(ToFD->doesThisDeclarationHaveABody());
  2081. EXPECT_EQ(ToFD->getPreviousDecl(), ImportedD);
  2082. }
  2083. TEST_P(ImportFriendFunctions, ImportFriendFunctionRedeclChainDefWithClass) {
  2084. auto Pattern = functionDecl(hasName("f"));
  2085. Decl *FromTU = getTuDecl(
  2086. R"(
  2087. class X;
  2088. void f(X *x){}
  2089. class X{
  2090. friend void f(X *x);
  2091. };
  2092. )",
  2093. Lang_CXX, "input0.cc");
  2094. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2095. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2096. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2097. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2098. EXPECT_TRUE(ImportedD->doesThisDeclarationHaveABody());
  2099. auto *InClassFD = cast<FunctionDecl>(FirstDeclMatcher<FriendDecl>()
  2100. .match(ToTU, friendDecl())
  2101. ->getFriendDecl());
  2102. EXPECT_FALSE(InClassFD->doesThisDeclarationHaveABody());
  2103. EXPECT_EQ(InClassFD->getPreviousDecl(), ImportedD);
  2104. // The parameters must refer the same type
  2105. EXPECT_EQ((*InClassFD->param_begin())->getOriginalType(),
  2106. (*ImportedD->param_begin())->getOriginalType());
  2107. }
  2108. TEST_P(ImportFriendFunctions,
  2109. ImportFriendFunctionRedeclChainDefWithClass_ImportTheProto) {
  2110. auto Pattern = functionDecl(hasName("f"));
  2111. Decl *FromTU = getTuDecl(
  2112. R"(
  2113. class X;
  2114. void f(X *x){}
  2115. class X{
  2116. friend void f(X *x);
  2117. };
  2118. )",
  2119. Lang_CXX, "input0.cc");
  2120. auto *FromD = LastDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2121. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2122. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2123. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2124. EXPECT_FALSE(ImportedD->doesThisDeclarationHaveABody());
  2125. auto *OutOfClassFD = FirstDeclMatcher<FunctionDecl>().match(
  2126. ToTU, functionDecl(unless(hasParent(friendDecl()))));
  2127. EXPECT_TRUE(OutOfClassFD->doesThisDeclarationHaveABody());
  2128. EXPECT_EQ(ImportedD->getPreviousDecl(), OutOfClassFD);
  2129. // The parameters must refer the same type
  2130. EXPECT_EQ((*OutOfClassFD->param_begin())->getOriginalType(),
  2131. (*ImportedD->param_begin())->getOriginalType());
  2132. }
  2133. TEST_P(ImportFriendFunctions, ImportFriendFunctionFromMultipleTU) {
  2134. auto Pattern = functionDecl(hasName("f"));
  2135. FunctionDecl *ImportedD;
  2136. {
  2137. Decl *FromTU =
  2138. getTuDecl("struct X { friend void f(){} };", Lang_CXX, "input0.cc");
  2139. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2140. ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2141. }
  2142. FunctionDecl *ImportedD1;
  2143. {
  2144. Decl *FromTU = getTuDecl("void f();", Lang_CXX, "input1.cc");
  2145. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2146. ImportedD1 = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2147. }
  2148. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2149. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2150. EXPECT_TRUE(ImportedD->doesThisDeclarationHaveABody());
  2151. EXPECT_FALSE(ImportedD1->doesThisDeclarationHaveABody());
  2152. EXPECT_EQ(ImportedD1->getPreviousDecl(), ImportedD);
  2153. }
  2154. TEST_P(ImportFriendFunctions, Lookup) {
  2155. auto FunctionPattern = functionDecl(hasName("f"));
  2156. auto ClassPattern = cxxRecordDecl(hasName("X"));
  2157. TranslationUnitDecl *FromTU =
  2158. getTuDecl("struct X { friend void f(); };", Lang_CXX, "input0.cc");
  2159. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, FunctionPattern);
  2160. ASSERT_TRUE(FromD->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2161. ASSERT_FALSE(FromD->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2162. {
  2163. auto FromName = FromD->getDeclName();
  2164. auto *Class = FirstDeclMatcher<CXXRecordDecl>().match(FromTU, ClassPattern);
  2165. auto LookupRes = Class->noload_lookup(FromName);
  2166. ASSERT_EQ(LookupRes.size(), 0u);
  2167. LookupRes = FromTU->noload_lookup(FromName);
  2168. ASSERT_EQ(LookupRes.size(), 1u);
  2169. }
  2170. auto *ToD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  2171. auto ToName = ToD->getDeclName();
  2172. TranslationUnitDecl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2173. auto *Class = FirstDeclMatcher<CXXRecordDecl>().match(ToTU, ClassPattern);
  2174. auto LookupRes = Class->noload_lookup(ToName);
  2175. EXPECT_EQ(LookupRes.size(), 0u);
  2176. LookupRes = ToTU->noload_lookup(ToName);
  2177. EXPECT_EQ(LookupRes.size(), 1u);
  2178. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, FunctionPattern), 1u);
  2179. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, FunctionPattern);
  2180. EXPECT_TRUE(To0->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2181. EXPECT_FALSE(To0->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2182. }
  2183. TEST_P(ImportFriendFunctions, DISABLED_LookupWithProtoAfter) {
  2184. auto FunctionPattern = functionDecl(hasName("f"));
  2185. auto ClassPattern = cxxRecordDecl(hasName("X"));
  2186. TranslationUnitDecl *FromTU = getTuDecl(
  2187. "struct X { friend void f(); };"
  2188. // This proto decl makes f available to normal
  2189. // lookup, otherwise it is hidden.
  2190. // Normal C++ lookup (implemented in
  2191. // `clang::Sema::CppLookupName()` and in `LookupDirect()`)
  2192. // returns the found `NamedDecl` only if the set IDNS is matched
  2193. "void f();",
  2194. Lang_CXX, "input0.cc");
  2195. auto *FromFriend =
  2196. FirstDeclMatcher<FunctionDecl>().match(FromTU, FunctionPattern);
  2197. auto *FromNormal =
  2198. LastDeclMatcher<FunctionDecl>().match(FromTU, FunctionPattern);
  2199. ASSERT_TRUE(FromFriend->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2200. ASSERT_FALSE(FromFriend->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2201. ASSERT_FALSE(FromNormal->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2202. ASSERT_TRUE(FromNormal->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2203. auto FromName = FromFriend->getDeclName();
  2204. auto *FromClass =
  2205. FirstDeclMatcher<CXXRecordDecl>().match(FromTU, ClassPattern);
  2206. auto LookupRes = FromClass->noload_lookup(FromName);
  2207. ASSERT_EQ(LookupRes.size(), 0u);
  2208. LookupRes = FromTU->noload_lookup(FromName);
  2209. ASSERT_EQ(LookupRes.size(), 1u);
  2210. auto *ToFriend = cast<FunctionDecl>(Import(FromFriend, Lang_CXX));
  2211. auto ToName = ToFriend->getDeclName();
  2212. TranslationUnitDecl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2213. auto *ToClass = FirstDeclMatcher<CXXRecordDecl>().match(ToTU, ClassPattern);
  2214. LookupRes = ToClass->noload_lookup(ToName);
  2215. EXPECT_EQ(LookupRes.size(), 0u);
  2216. LookupRes = ToTU->noload_lookup(ToName);
  2217. // Test is disabled because this result is 2.
  2218. EXPECT_EQ(LookupRes.size(), 1u);
  2219. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, FunctionPattern), 2u);
  2220. ToFriend = FirstDeclMatcher<FunctionDecl>().match(ToTU, FunctionPattern);
  2221. auto *ToNormal = LastDeclMatcher<FunctionDecl>().match(ToTU, FunctionPattern);
  2222. EXPECT_TRUE(ToFriend->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2223. EXPECT_FALSE(ToFriend->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2224. EXPECT_FALSE(ToNormal->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2225. EXPECT_TRUE(ToNormal->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2226. }
  2227. TEST_P(ImportFriendFunctions, LookupWithProtoBefore) {
  2228. auto FunctionPattern = functionDecl(hasName("f"));
  2229. auto ClassPattern = cxxRecordDecl(hasName("X"));
  2230. TranslationUnitDecl *FromTU = getTuDecl(
  2231. "void f();"
  2232. "struct X { friend void f(); };",
  2233. Lang_CXX, "input0.cc");
  2234. auto *FromNormal =
  2235. FirstDeclMatcher<FunctionDecl>().match(FromTU, FunctionPattern);
  2236. auto *FromFriend =
  2237. LastDeclMatcher<FunctionDecl>().match(FromTU, FunctionPattern);
  2238. ASSERT_FALSE(FromNormal->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2239. ASSERT_TRUE(FromNormal->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2240. ASSERT_TRUE(FromFriend->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2241. ASSERT_TRUE(FromFriend->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2242. auto FromName = FromNormal->getDeclName();
  2243. auto *FromClass =
  2244. FirstDeclMatcher<CXXRecordDecl>().match(FromTU, ClassPattern);
  2245. auto LookupRes = FromClass->noload_lookup(FromName);
  2246. ASSERT_EQ(LookupRes.size(), 0u);
  2247. LookupRes = FromTU->noload_lookup(FromName);
  2248. ASSERT_EQ(LookupRes.size(), 1u);
  2249. auto *ToNormal = cast<FunctionDecl>(Import(FromNormal, Lang_CXX));
  2250. auto ToName = ToNormal->getDeclName();
  2251. TranslationUnitDecl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2252. auto *ToClass = FirstDeclMatcher<CXXRecordDecl>().match(ToTU, ClassPattern);
  2253. LookupRes = ToClass->noload_lookup(ToName);
  2254. EXPECT_EQ(LookupRes.size(), 0u);
  2255. LookupRes = ToTU->noload_lookup(ToName);
  2256. EXPECT_EQ(LookupRes.size(), 1u);
  2257. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, FunctionPattern), 2u);
  2258. ToNormal = FirstDeclMatcher<FunctionDecl>().match(ToTU, FunctionPattern);
  2259. auto *ToFriend = LastDeclMatcher<FunctionDecl>().match(ToTU, FunctionPattern);
  2260. EXPECT_FALSE(ToNormal->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2261. EXPECT_TRUE(ToNormal->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2262. EXPECT_TRUE(ToFriend->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2263. EXPECT_TRUE(ToFriend->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2264. }
  2265. TEST_P(ImportFriendFunctions, ImportFriendChangesLookup) {
  2266. auto Pattern = functionDecl(hasName("f"));
  2267. TranslationUnitDecl *FromNormalTU =
  2268. getTuDecl("void f();", Lang_CXX, "input0.cc");
  2269. auto *FromNormalF =
  2270. FirstDeclMatcher<FunctionDecl>().match(FromNormalTU, Pattern);
  2271. TranslationUnitDecl *FromFriendTU =
  2272. getTuDecl("class X { friend void f(); };", Lang_CXX, "input1.cc");
  2273. auto *FromFriendF =
  2274. FirstDeclMatcher<FunctionDecl>().match(FromFriendTU, Pattern);
  2275. auto FromNormalName = FromNormalF->getDeclName();
  2276. auto FromFriendName = FromFriendF->getDeclName();
  2277. ASSERT_TRUE(FromNormalF->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2278. ASSERT_FALSE(FromNormalF->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2279. ASSERT_FALSE(FromFriendF->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2280. ASSERT_TRUE(FromFriendF->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2281. auto LookupRes = FromNormalTU->noload_lookup(FromNormalName);
  2282. ASSERT_EQ(LookupRes.size(), 1u);
  2283. LookupRes = FromFriendTU->noload_lookup(FromFriendName);
  2284. ASSERT_EQ(LookupRes.size(), 1u);
  2285. auto *ToNormalF = cast<FunctionDecl>(Import(FromNormalF, Lang_CXX));
  2286. TranslationUnitDecl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2287. auto ToName = ToNormalF->getDeclName();
  2288. EXPECT_TRUE(ToNormalF->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2289. EXPECT_FALSE(ToNormalF->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2290. LookupRes = ToTU->noload_lookup(ToName);
  2291. EXPECT_EQ(LookupRes.size(), 1u);
  2292. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 1u);
  2293. auto *ToFriendF = cast<FunctionDecl>(Import(FromFriendF, Lang_CXX));
  2294. LookupRes = ToTU->noload_lookup(ToName);
  2295. EXPECT_EQ(LookupRes.size(), 1u);
  2296. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2297. EXPECT_TRUE(ToNormalF->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2298. EXPECT_FALSE(ToNormalF->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2299. EXPECT_TRUE(ToFriendF->isInIdentifierNamespace(Decl::IDNS_Ordinary));
  2300. EXPECT_TRUE(ToFriendF->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend));
  2301. }
  2302. TEST_P(ImportFriendFunctions, ImportFriendList) {
  2303. TranslationUnitDecl *FromTU = getTuDecl(
  2304. "struct X { friend void f(); };"
  2305. "void f();",
  2306. Lang_CXX, "input0.cc");
  2307. auto *FromFriendF = FirstDeclMatcher<FunctionDecl>().match(
  2308. FromTU, functionDecl(hasName("f")));
  2309. auto *FromClass = FirstDeclMatcher<CXXRecordDecl>().match(
  2310. FromTU, cxxRecordDecl(hasName("X")));
  2311. auto *FromFriend = FirstDeclMatcher<FriendDecl>().match(FromTU, friendDecl());
  2312. auto FromFriends = FromClass->friends();
  2313. unsigned int FrN = 0;
  2314. for (auto Fr : FromFriends) {
  2315. ASSERT_EQ(Fr, FromFriend);
  2316. ++FrN;
  2317. }
  2318. ASSERT_EQ(FrN, 1u);
  2319. Import(FromFriendF, Lang_CXX);
  2320. TranslationUnitDecl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2321. auto *ToClass = FirstDeclMatcher<CXXRecordDecl>().match(
  2322. ToTU, cxxRecordDecl(hasName("X")));
  2323. auto *ToFriend = FirstDeclMatcher<FriendDecl>().match(ToTU, friendDecl());
  2324. auto ToFriends = ToClass->friends();
  2325. FrN = 0;
  2326. for (auto Fr : ToFriends) {
  2327. EXPECT_EQ(Fr, ToFriend);
  2328. ++FrN;
  2329. }
  2330. EXPECT_EQ(FrN, 1u);
  2331. }
  2332. AST_MATCHER_P(TagDecl, hasTypedefForAnonDecl, Matcher<TypedefNameDecl>,
  2333. InnerMatcher) {
  2334. if (auto *Typedef = Node.getTypedefNameForAnonDecl())
  2335. return InnerMatcher.matches(*Typedef, Finder, Builder);
  2336. return false;
  2337. }
  2338. TEST_P(ImportDecl, ImportEnumSequential) {
  2339. CodeFiles Samples{{"main.c",
  2340. {"void foo();"
  2341. "void moo();"
  2342. "int main() { foo(); moo(); }",
  2343. Lang_C}},
  2344. {"foo.c",
  2345. {"typedef enum { THING_VALUE } thing_t;"
  2346. "void conflict(thing_t type);"
  2347. "void foo() { (void)THING_VALUE; }"
  2348. "void conflict(thing_t type) {}",
  2349. Lang_C}},
  2350. {"moo.c",
  2351. {"typedef enum { THING_VALUE } thing_t;"
  2352. "void conflict(thing_t type);"
  2353. "void moo() { conflict(THING_VALUE); }",
  2354. Lang_C}}};
  2355. auto VerificationMatcher =
  2356. enumDecl(has(enumConstantDecl(hasName("THING_VALUE"))),
  2357. hasTypedefForAnonDecl(hasName("thing_t")));
  2358. ImportAction ImportFoo{"foo.c", "main.c", functionDecl(hasName("foo"))},
  2359. ImportMoo{"moo.c", "main.c", functionDecl(hasName("moo"))};
  2360. testImportSequence(
  2361. Samples, {ImportFoo, ImportMoo}, // "foo", them "moo".
  2362. // Just check that there is only one enum decl in the result AST.
  2363. "main.c", enumDecl(), VerificationMatcher);
  2364. // For different import order, result should be the same.
  2365. testImportSequence(
  2366. Samples, {ImportMoo, ImportFoo}, // "moo", them "foo".
  2367. // Check that there is only one enum decl in the result AST.
  2368. "main.c", enumDecl(), VerificationMatcher);
  2369. }
  2370. const internal::VariadicDynCastAllOfMatcher<Expr, DependentScopeDeclRefExpr>
  2371. dependentScopeDeclRefExpr;
  2372. TEST_P(ImportExpr, DependentScopeDeclRefExpr) {
  2373. MatchVerifier<Decl> Verifier;
  2374. testImport("template <typename T> struct S { static T foo; };"
  2375. "template <typename T> void declToImport() {"
  2376. " (void) S<T>::foo;"
  2377. "}"
  2378. "void instantiate() { declToImport<int>(); }"
  2379. "template <typename T> T S<T>::foo;",
  2380. Lang_CXX11, "", Lang_CXX11, Verifier,
  2381. functionTemplateDecl(has(functionDecl(has(compoundStmt(
  2382. has(cStyleCastExpr(has(dependentScopeDeclRefExpr())))))))));
  2383. testImport("template <typename T> struct S {"
  2384. "template<typename S> static void foo(){};"
  2385. "};"
  2386. "template <typename T> void declToImport() {"
  2387. " S<T>::template foo<T>();"
  2388. "}"
  2389. "void instantiate() { declToImport<int>(); }",
  2390. Lang_CXX11, "", Lang_CXX11, Verifier,
  2391. functionTemplateDecl(has(functionDecl(has(compoundStmt(
  2392. has(callExpr(has(dependentScopeDeclRefExpr())))))))));
  2393. }
  2394. const internal::VariadicDynCastAllOfMatcher<Type, DependentNameType>
  2395. dependentNameType;
  2396. TEST_P(ImportExpr, DependentNameType) {
  2397. MatchVerifier<Decl> Verifier;
  2398. testImport("template <typename T> struct declToImport {"
  2399. " typedef typename T::type dependent_name;"
  2400. "};",
  2401. Lang_CXX11, "", Lang_CXX11, Verifier,
  2402. classTemplateDecl(has(
  2403. cxxRecordDecl(has(typedefDecl(has(dependentNameType())))))));
  2404. }
  2405. TEST_P(ImportExpr, UnresolvedMemberExpr) {
  2406. MatchVerifier<Decl> Verifier;
  2407. testImport("struct S { template <typename T> void mem(); };"
  2408. "template <typename U> void declToImport() {"
  2409. " S s;"
  2410. " s.mem<U>();"
  2411. "}"
  2412. "void instantiate() { declToImport<int>(); }",
  2413. Lang_CXX11, "", Lang_CXX11, Verifier,
  2414. functionTemplateDecl(has(functionDecl(has(
  2415. compoundStmt(has(callExpr(has(unresolvedMemberExpr())))))))));
  2416. }
  2417. class ImportImplicitMethods : public ASTImporterOptionSpecificTestBase {
  2418. public:
  2419. static constexpr auto DefaultCode = R"(
  2420. struct A { int x; };
  2421. void f() {
  2422. A a;
  2423. A a1(a);
  2424. A a2(A{});
  2425. a = a1;
  2426. a = A{};
  2427. a.~A();
  2428. })";
  2429. template <typename MatcherType>
  2430. void testImportOf(
  2431. const MatcherType &MethodMatcher, const char *Code = DefaultCode) {
  2432. test(MethodMatcher, Code, /*ExpectedCount=*/1u);
  2433. }
  2434. template <typename MatcherType>
  2435. void testNoImportOf(
  2436. const MatcherType &MethodMatcher, const char *Code = DefaultCode) {
  2437. test(MethodMatcher, Code, /*ExpectedCount=*/0u);
  2438. }
  2439. private:
  2440. template <typename MatcherType>
  2441. void test(const MatcherType &MethodMatcher,
  2442. const char *Code, unsigned int ExpectedCount) {
  2443. auto ClassMatcher = cxxRecordDecl(unless(isImplicit()));
  2444. Decl *ToTU = getToTuDecl(Code, Lang_CXX11);
  2445. auto *ToClass = FirstDeclMatcher<CXXRecordDecl>().match(
  2446. ToTU, ClassMatcher);
  2447. ASSERT_EQ(DeclCounter<CXXMethodDecl>().match(ToClass, MethodMatcher), 1u);
  2448. {
  2449. CXXMethodDecl *Method =
  2450. FirstDeclMatcher<CXXMethodDecl>().match(ToClass, MethodMatcher);
  2451. ToClass->removeDecl(Method);
  2452. LookupTablePtr->remove(Method);
  2453. }
  2454. ASSERT_EQ(DeclCounter<CXXMethodDecl>().match(ToClass, MethodMatcher), 0u);
  2455. Decl *ImportedClass = nullptr;
  2456. {
  2457. Decl *FromTU = getTuDecl(Code, Lang_CXX11, "input1.cc");
  2458. auto *FromClass = FirstDeclMatcher<CXXRecordDecl>().match(
  2459. FromTU, ClassMatcher);
  2460. ImportedClass = Import(FromClass, Lang_CXX11);
  2461. }
  2462. EXPECT_EQ(ToClass, ImportedClass);
  2463. EXPECT_EQ(DeclCounter<CXXMethodDecl>().match(ToClass, MethodMatcher),
  2464. ExpectedCount);
  2465. }
  2466. };
  2467. TEST_P(ImportImplicitMethods, DefaultConstructor) {
  2468. testImportOf(cxxConstructorDecl(isDefaultConstructor()));
  2469. }
  2470. TEST_P(ImportImplicitMethods, CopyConstructor) {
  2471. testImportOf(cxxConstructorDecl(isCopyConstructor()));
  2472. }
  2473. TEST_P(ImportImplicitMethods, MoveConstructor) {
  2474. testImportOf(cxxConstructorDecl(isMoveConstructor()));
  2475. }
  2476. TEST_P(ImportImplicitMethods, Destructor) {
  2477. testImportOf(cxxDestructorDecl());
  2478. }
  2479. TEST_P(ImportImplicitMethods, CopyAssignment) {
  2480. testImportOf(cxxMethodDecl(isCopyAssignmentOperator()));
  2481. }
  2482. TEST_P(ImportImplicitMethods, MoveAssignment) {
  2483. testImportOf(cxxMethodDecl(isMoveAssignmentOperator()));
  2484. }
  2485. TEST_P(ImportImplicitMethods, DoNotImportUserProvided) {
  2486. auto Code = R"(
  2487. struct A { A() { int x; } };
  2488. )";
  2489. testNoImportOf(cxxConstructorDecl(isDefaultConstructor()), Code);
  2490. }
  2491. TEST_P(ImportImplicitMethods, DoNotImportDefault) {
  2492. auto Code = R"(
  2493. struct A { A() = default; };
  2494. )";
  2495. testNoImportOf(cxxConstructorDecl(isDefaultConstructor()), Code);
  2496. }
  2497. TEST_P(ImportImplicitMethods, DoNotImportDeleted) {
  2498. auto Code = R"(
  2499. struct A { A() = delete; };
  2500. )";
  2501. testNoImportOf(cxxConstructorDecl(isDefaultConstructor()), Code);
  2502. }
  2503. TEST_P(ImportImplicitMethods, DoNotImportOtherMethod) {
  2504. auto Code = R"(
  2505. struct A { void f() { } };
  2506. )";
  2507. testNoImportOf(cxxMethodDecl(hasName("f")), Code);
  2508. }
  2509. TEST_P(ASTImporterOptionSpecificTestBase, ImportOfEquivalentRecord) {
  2510. Decl *ToR1;
  2511. {
  2512. Decl *FromTU = getTuDecl(
  2513. "struct A { };", Lang_CXX, "input0.cc");
  2514. auto *FromR = FirstDeclMatcher<CXXRecordDecl>().match(
  2515. FromTU, cxxRecordDecl(hasName("A")));
  2516. ToR1 = Import(FromR, Lang_CXX);
  2517. }
  2518. Decl *ToR2;
  2519. {
  2520. Decl *FromTU = getTuDecl(
  2521. "struct A { };", Lang_CXX, "input1.cc");
  2522. auto *FromR = FirstDeclMatcher<CXXRecordDecl>().match(
  2523. FromTU, cxxRecordDecl(hasName("A")));
  2524. ToR2 = Import(FromR, Lang_CXX);
  2525. }
  2526. EXPECT_EQ(ToR1, ToR2);
  2527. }
  2528. TEST_P(ASTImporterOptionSpecificTestBase, ImportOfNonEquivalentRecord) {
  2529. Decl *ToR1;
  2530. {
  2531. Decl *FromTU = getTuDecl(
  2532. "struct A { int x; };", Lang_CXX, "input0.cc");
  2533. auto *FromR = FirstDeclMatcher<CXXRecordDecl>().match(
  2534. FromTU, cxxRecordDecl(hasName("A")));
  2535. ToR1 = Import(FromR, Lang_CXX);
  2536. }
  2537. Decl *ToR2;
  2538. {
  2539. Decl *FromTU = getTuDecl(
  2540. "struct A { unsigned x; };", Lang_CXX, "input1.cc");
  2541. auto *FromR = FirstDeclMatcher<CXXRecordDecl>().match(
  2542. FromTU, cxxRecordDecl(hasName("A")));
  2543. ToR2 = Import(FromR, Lang_CXX);
  2544. }
  2545. EXPECT_NE(ToR1, ToR2);
  2546. }
  2547. TEST_P(ASTImporterOptionSpecificTestBase, ImportOfEquivalentField) {
  2548. Decl *ToF1;
  2549. {
  2550. Decl *FromTU = getTuDecl(
  2551. "struct A { int x; };", Lang_CXX, "input0.cc");
  2552. auto *FromF = FirstDeclMatcher<FieldDecl>().match(
  2553. FromTU, fieldDecl(hasName("x")));
  2554. ToF1 = Import(FromF, Lang_CXX);
  2555. }
  2556. Decl *ToF2;
  2557. {
  2558. Decl *FromTU = getTuDecl(
  2559. "struct A { int x; };", Lang_CXX, "input1.cc");
  2560. auto *FromF = FirstDeclMatcher<FieldDecl>().match(
  2561. FromTU, fieldDecl(hasName("x")));
  2562. ToF2 = Import(FromF, Lang_CXX);
  2563. }
  2564. EXPECT_EQ(ToF1, ToF2);
  2565. }
  2566. TEST_P(ASTImporterOptionSpecificTestBase, ImportOfNonEquivalentField) {
  2567. Decl *ToF1;
  2568. {
  2569. Decl *FromTU = getTuDecl(
  2570. "struct A { int x; };", Lang_CXX, "input0.cc");
  2571. auto *FromF = FirstDeclMatcher<FieldDecl>().match(
  2572. FromTU, fieldDecl(hasName("x")));
  2573. ToF1 = Import(FromF, Lang_CXX);
  2574. }
  2575. Decl *ToF2;
  2576. {
  2577. Decl *FromTU = getTuDecl(
  2578. "struct A { unsigned x; };", Lang_CXX, "input1.cc");
  2579. auto *FromF = FirstDeclMatcher<FieldDecl>().match(
  2580. FromTU, fieldDecl(hasName("x")));
  2581. ToF2 = Import(FromF, Lang_CXX);
  2582. }
  2583. EXPECT_NE(ToF1, ToF2);
  2584. }
  2585. TEST_P(ASTImporterOptionSpecificTestBase, ImportOfEquivalentMethod) {
  2586. Decl *ToM1;
  2587. {
  2588. Decl *FromTU = getTuDecl(
  2589. "struct A { void x(); }; void A::x() { }", Lang_CXX, "input0.cc");
  2590. auto *FromM = FirstDeclMatcher<FunctionDecl>().match(
  2591. FromTU, functionDecl(hasName("x"), isDefinition()));
  2592. ToM1 = Import(FromM, Lang_CXX);
  2593. }
  2594. Decl *ToM2;
  2595. {
  2596. Decl *FromTU = getTuDecl(
  2597. "struct A { void x(); }; void A::x() { }", Lang_CXX, "input1.cc");
  2598. auto *FromM = FirstDeclMatcher<FunctionDecl>().match(
  2599. FromTU, functionDecl(hasName("x"), isDefinition()));
  2600. ToM2 = Import(FromM, Lang_CXX);
  2601. }
  2602. EXPECT_EQ(ToM1, ToM2);
  2603. }
  2604. TEST_P(ASTImporterOptionSpecificTestBase, ImportOfNonEquivalentMethod) {
  2605. Decl *ToM1;
  2606. {
  2607. Decl *FromTU = getTuDecl(
  2608. "struct A { void x(); }; void A::x() { }",
  2609. Lang_CXX, "input0.cc");
  2610. auto *FromM = FirstDeclMatcher<FunctionDecl>().match(
  2611. FromTU, functionDecl(hasName("x"), isDefinition()));
  2612. ToM1 = Import(FromM, Lang_CXX);
  2613. }
  2614. Decl *ToM2;
  2615. {
  2616. Decl *FromTU = getTuDecl(
  2617. "struct A { void x() const; }; void A::x() const { }",
  2618. Lang_CXX, "input1.cc");
  2619. auto *FromM = FirstDeclMatcher<FunctionDecl>().match(
  2620. FromTU, functionDecl(hasName("x"), isDefinition()));
  2621. ToM2 = Import(FromM, Lang_CXX);
  2622. }
  2623. EXPECT_NE(ToM1, ToM2);
  2624. }
  2625. TEST_P(ASTImporterOptionSpecificTestBase,
  2626. ImportUnnamedStructsWithRecursingField) {
  2627. Decl *FromTU = getTuDecl(
  2628. R"(
  2629. struct A {
  2630. struct {
  2631. struct A *next;
  2632. } entry0;
  2633. struct {
  2634. struct A *next;
  2635. } entry1;
  2636. };
  2637. )",
  2638. Lang_C, "input0.cc");
  2639. auto *From =
  2640. FirstDeclMatcher<RecordDecl>().match(FromTU, recordDecl(hasName("A")));
  2641. Import(From, Lang_C);
  2642. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2643. auto *Entry0 =
  2644. FirstDeclMatcher<FieldDecl>().match(ToTU, fieldDecl(hasName("entry0")));
  2645. auto *Entry1 =
  2646. FirstDeclMatcher<FieldDecl>().match(ToTU, fieldDecl(hasName("entry1")));
  2647. auto *R0 = getRecordDecl(Entry0);
  2648. auto *R1 = getRecordDecl(Entry1);
  2649. EXPECT_NE(R0, R1);
  2650. EXPECT_TRUE(MatchVerifier<RecordDecl>().match(
  2651. R0, recordDecl(has(fieldDecl(hasName("next"))))));
  2652. EXPECT_TRUE(MatchVerifier<RecordDecl>().match(
  2653. R1, recordDecl(has(fieldDecl(hasName("next"))))));
  2654. }
  2655. TEST_P(ASTImporterOptionSpecificTestBase, ImportUnnamedFieldsInCorrectOrder) {
  2656. Decl *FromTU = getTuDecl(
  2657. R"(
  2658. void f(int X, int Y, bool Z) {
  2659. (void)[X, Y, Z] { (void)Z; };
  2660. }
  2661. )",
  2662. Lang_CXX11, "input0.cc");
  2663. auto *FromF = FirstDeclMatcher<FunctionDecl>().match(
  2664. FromTU, functionDecl(hasName("f")));
  2665. auto *ToF = cast_or_null<FunctionDecl>(Import(FromF, Lang_CXX11));
  2666. EXPECT_TRUE(ToF);
  2667. CXXRecordDecl *FromLambda =
  2668. cast<LambdaExpr>(cast<CStyleCastExpr>(cast<CompoundStmt>(
  2669. FromF->getBody())->body_front())->getSubExpr())->getLambdaClass();
  2670. auto *ToLambda = cast_or_null<CXXRecordDecl>(Import(FromLambda, Lang_CXX11));
  2671. EXPECT_TRUE(ToLambda);
  2672. // Check if the fields of the lambda class are imported in correct order.
  2673. unsigned FromIndex = 0u;
  2674. for (auto *FromField : FromLambda->fields()) {
  2675. ASSERT_FALSE(FromField->getDeclName());
  2676. auto *ToField = cast_or_null<FieldDecl>(Import(FromField, Lang_CXX11));
  2677. EXPECT_TRUE(ToField);
  2678. Optional<unsigned> ToIndex = ASTImporter::getFieldIndex(ToField);
  2679. EXPECT_TRUE(ToIndex);
  2680. EXPECT_EQ(*ToIndex, FromIndex);
  2681. ++FromIndex;
  2682. }
  2683. EXPECT_EQ(FromIndex, 3u);
  2684. }
  2685. TEST_P(ASTImporterOptionSpecificTestBase,
  2686. MergeFieldDeclsOfClassTemplateSpecialization) {
  2687. std::string ClassTemplate =
  2688. R"(
  2689. template <typename T>
  2690. struct X {
  2691. int a{0}; // FieldDecl with InitListExpr
  2692. X(char) : a(3) {} // (1)
  2693. X(int) {} // (2)
  2694. };
  2695. )";
  2696. Decl *ToTU = getToTuDecl(ClassTemplate +
  2697. R"(
  2698. void foo() {
  2699. // ClassTemplateSpec with ctor (1): FieldDecl without InitlistExpr
  2700. X<char> xc('c');
  2701. }
  2702. )", Lang_CXX11);
  2703. auto *ToSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2704. ToTU, classTemplateSpecializationDecl(hasName("X")));
  2705. // FieldDecl without InitlistExpr:
  2706. auto *ToField = *ToSpec->field_begin();
  2707. ASSERT_TRUE(ToField);
  2708. ASSERT_FALSE(ToField->getInClassInitializer());
  2709. Decl *FromTU = getTuDecl(ClassTemplate +
  2710. R"(
  2711. void bar() {
  2712. // ClassTemplateSpec with ctor (2): FieldDecl WITH InitlistExpr
  2713. X<char> xc(1);
  2714. }
  2715. )", Lang_CXX11);
  2716. auto *FromSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2717. FromTU, classTemplateSpecializationDecl(hasName("X")));
  2718. // FieldDecl with InitlistExpr:
  2719. auto *FromField = *FromSpec->field_begin();
  2720. ASSERT_TRUE(FromField);
  2721. ASSERT_TRUE(FromField->getInClassInitializer());
  2722. auto *ImportedSpec = Import(FromSpec, Lang_CXX11);
  2723. ASSERT_TRUE(ImportedSpec);
  2724. EXPECT_EQ(ImportedSpec, ToSpec);
  2725. // After the import, the FieldDecl has to be merged, thus it should have the
  2726. // InitListExpr.
  2727. EXPECT_TRUE(ToField->getInClassInitializer());
  2728. }
  2729. TEST_P(ASTImporterOptionSpecificTestBase,
  2730. MergeFunctionOfClassTemplateSpecialization) {
  2731. std::string ClassTemplate =
  2732. R"(
  2733. template <typename T>
  2734. struct X {
  2735. void f() {}
  2736. void g() {}
  2737. };
  2738. )";
  2739. Decl *ToTU = getToTuDecl(ClassTemplate +
  2740. R"(
  2741. void foo() {
  2742. X<char> x;
  2743. x.f();
  2744. }
  2745. )", Lang_CXX11);
  2746. Decl *FromTU = getTuDecl(ClassTemplate +
  2747. R"(
  2748. void bar() {
  2749. X<char> x;
  2750. x.g();
  2751. }
  2752. )", Lang_CXX11);
  2753. auto *FromSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2754. FromTU, classTemplateSpecializationDecl(hasName("X")));
  2755. auto FunPattern = functionDecl(hasName("g"),
  2756. hasParent(classTemplateSpecializationDecl()));
  2757. auto *FromFun =
  2758. FirstDeclMatcher<FunctionDecl>().match(FromTU, FunPattern);
  2759. auto *ToFun =
  2760. FirstDeclMatcher<FunctionDecl>().match(ToTU, FunPattern);
  2761. ASSERT_TRUE(FromFun->hasBody());
  2762. ASSERT_FALSE(ToFun->hasBody());
  2763. auto *ImportedSpec = Import(FromSpec, Lang_CXX11);
  2764. ASSERT_TRUE(ImportedSpec);
  2765. auto *ToSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2766. ToTU, classTemplateSpecializationDecl(hasName("X")));
  2767. EXPECT_EQ(ImportedSpec, ToSpec);
  2768. EXPECT_TRUE(ToFun->hasBody());
  2769. }
  2770. TEST_P(ASTImporterOptionSpecificTestBase,
  2771. ODRViolationOfClassTemplateSpecializationsShouldBeReported) {
  2772. std::string ClassTemplate =
  2773. R"(
  2774. template <typename T>
  2775. struct X {};
  2776. )";
  2777. Decl *ToTU = getToTuDecl(ClassTemplate +
  2778. R"(
  2779. template <>
  2780. struct X<char> {
  2781. int a;
  2782. };
  2783. void foo() {
  2784. X<char> x;
  2785. }
  2786. )",
  2787. Lang_CXX11);
  2788. Decl *FromTU = getTuDecl(ClassTemplate +
  2789. R"(
  2790. template <>
  2791. struct X<char> {
  2792. int b;
  2793. };
  2794. void foo() {
  2795. X<char> x;
  2796. }
  2797. )",
  2798. Lang_CXX11);
  2799. auto *FromSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2800. FromTU, classTemplateSpecializationDecl(hasName("X")));
  2801. auto *ImportedSpec = Import(FromSpec, Lang_CXX11);
  2802. // We expect one (ODR) warning during the import.
  2803. EXPECT_EQ(1u, ToTU->getASTContext().getDiagnostics().getNumWarnings());
  2804. // The second specialization is different from the first, thus it violates
  2805. // ODR, consequently we expect to keep the first specialization only, which is
  2806. // already in the "To" context.
  2807. EXPECT_FALSE(ImportedSpec);
  2808. EXPECT_EQ(1u,
  2809. DeclCounter<ClassTemplateSpecializationDecl>().match(
  2810. ToTU, classTemplateSpecializationDecl(hasName("X"))));
  2811. }
  2812. TEST_P(ASTImporterOptionSpecificTestBase,
  2813. MergeCtorOfClassTemplateSpecialization) {
  2814. std::string ClassTemplate =
  2815. R"(
  2816. template <typename T>
  2817. struct X {
  2818. X(char) {}
  2819. X(int) {}
  2820. };
  2821. )";
  2822. Decl *ToTU = getToTuDecl(ClassTemplate +
  2823. R"(
  2824. void foo() {
  2825. X<char> x('c');
  2826. }
  2827. )", Lang_CXX11);
  2828. Decl *FromTU = getTuDecl(ClassTemplate +
  2829. R"(
  2830. void bar() {
  2831. X<char> x(1);
  2832. }
  2833. )", Lang_CXX11);
  2834. auto *FromSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2835. FromTU, classTemplateSpecializationDecl(hasName("X")));
  2836. // Match the void(int) ctor.
  2837. auto CtorPattern =
  2838. cxxConstructorDecl(hasParameter(0, varDecl(hasType(asString("int")))),
  2839. hasParent(classTemplateSpecializationDecl()));
  2840. auto *FromCtor =
  2841. FirstDeclMatcher<CXXConstructorDecl>().match(FromTU, CtorPattern);
  2842. auto *ToCtor =
  2843. FirstDeclMatcher<CXXConstructorDecl>().match(ToTU, CtorPattern);
  2844. ASSERT_TRUE(FromCtor->hasBody());
  2845. ASSERT_FALSE(ToCtor->hasBody());
  2846. auto *ImportedSpec = Import(FromSpec, Lang_CXX11);
  2847. ASSERT_TRUE(ImportedSpec);
  2848. auto *ToSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2849. ToTU, classTemplateSpecializationDecl(hasName("X")));
  2850. EXPECT_EQ(ImportedSpec, ToSpec);
  2851. EXPECT_TRUE(ToCtor->hasBody());
  2852. }
  2853. TEST_P(ASTImporterOptionSpecificTestBase,
  2854. ClassTemplatePartialSpecializationsShouldNotBeDuplicated) {
  2855. auto Code =
  2856. R"(
  2857. // primary template
  2858. template<class T1, class T2, int I>
  2859. class A {};
  2860. // partial specialization
  2861. template<class T, int I>
  2862. class A<T, T*, I> {};
  2863. )";
  2864. Decl *ToTU = getToTuDecl(Code, Lang_CXX11);
  2865. Decl *FromTU = getTuDecl(Code, Lang_CXX11);
  2866. auto *FromSpec =
  2867. FirstDeclMatcher<ClassTemplatePartialSpecializationDecl>().match(
  2868. FromTU, classTemplatePartialSpecializationDecl());
  2869. auto *ToSpec =
  2870. FirstDeclMatcher<ClassTemplatePartialSpecializationDecl>().match(
  2871. ToTU, classTemplatePartialSpecializationDecl());
  2872. auto *ImportedSpec = Import(FromSpec, Lang_CXX11);
  2873. EXPECT_EQ(ImportedSpec, ToSpec);
  2874. EXPECT_EQ(1u, DeclCounter<ClassTemplatePartialSpecializationDecl>().match(
  2875. ToTU, classTemplatePartialSpecializationDecl()));
  2876. }
  2877. TEST_P(ASTImporterOptionSpecificTestBase,
  2878. ClassTemplateSpecializationsShouldNotBeDuplicated) {
  2879. auto Code =
  2880. R"(
  2881. // primary template
  2882. template<class T1, class T2, int I>
  2883. class A {};
  2884. // full specialization
  2885. template<>
  2886. class A<int, int, 1> {};
  2887. )";
  2888. Decl *ToTU = getToTuDecl(Code, Lang_CXX11);
  2889. Decl *FromTU = getTuDecl(Code, Lang_CXX11);
  2890. auto *FromSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2891. FromTU, classTemplateSpecializationDecl());
  2892. auto *ToSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2893. ToTU, classTemplateSpecializationDecl());
  2894. auto *ImportedSpec = Import(FromSpec, Lang_CXX11);
  2895. EXPECT_EQ(ImportedSpec, ToSpec);
  2896. EXPECT_EQ(1u, DeclCounter<ClassTemplateSpecializationDecl>().match(
  2897. ToTU, classTemplateSpecializationDecl()));
  2898. }
  2899. TEST_P(ASTImporterOptionSpecificTestBase,
  2900. ClassTemplateFullAndPartialSpecsShouldNotBeMixed) {
  2901. std::string PrimaryTemplate =
  2902. R"(
  2903. template<class T1, class T2, int I>
  2904. class A {};
  2905. )";
  2906. auto PartialSpec =
  2907. R"(
  2908. template<class T, int I>
  2909. class A<T, T*, I> {};
  2910. )";
  2911. auto FullSpec =
  2912. R"(
  2913. template<>
  2914. class A<int, int, 1> {};
  2915. )";
  2916. Decl *ToTU = getToTuDecl(PrimaryTemplate + FullSpec, Lang_CXX11);
  2917. Decl *FromTU = getTuDecl(PrimaryTemplate + PartialSpec, Lang_CXX11);
  2918. auto *FromSpec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2919. FromTU, classTemplateSpecializationDecl());
  2920. auto *ImportedSpec = Import(FromSpec, Lang_CXX11);
  2921. EXPECT_TRUE(ImportedSpec);
  2922. // Check the number of partial specializations.
  2923. EXPECT_EQ(1u, DeclCounter<ClassTemplatePartialSpecializationDecl>().match(
  2924. ToTU, classTemplatePartialSpecializationDecl()));
  2925. // Check the number of full specializations.
  2926. EXPECT_EQ(1u, DeclCounter<ClassTemplateSpecializationDecl>().match(
  2927. ToTU, classTemplateSpecializationDecl(
  2928. unless(classTemplatePartialSpecializationDecl()))));
  2929. }
  2930. TEST_P(ASTImporterOptionSpecificTestBase,
  2931. InitListExprValueKindShouldBeImported) {
  2932. Decl *TU = getTuDecl(
  2933. R"(
  2934. const int &init();
  2935. void foo() { const int &a{init()}; }
  2936. )", Lang_CXX11, "input0.cc");
  2937. auto *FromD = FirstDeclMatcher<VarDecl>().match(TU, varDecl(hasName("a")));
  2938. ASSERT_TRUE(FromD->getAnyInitializer());
  2939. auto *InitExpr = FromD->getAnyInitializer();
  2940. ASSERT_TRUE(InitExpr);
  2941. ASSERT_TRUE(InitExpr->isGLValue());
  2942. auto *ToD = Import(FromD, Lang_CXX11);
  2943. EXPECT_TRUE(ToD);
  2944. auto *ToInitExpr = cast<VarDecl>(ToD)->getAnyInitializer();
  2945. EXPECT_TRUE(ToInitExpr);
  2946. EXPECT_TRUE(ToInitExpr->isGLValue());
  2947. }
  2948. struct ImportVariables : ASTImporterOptionSpecificTestBase {};
  2949. TEST_P(ImportVariables, ImportOfOneDeclBringsInTheWholeChain) {
  2950. Decl *FromTU = getTuDecl(
  2951. R"(
  2952. struct A {
  2953. static const int a = 1 + 2;
  2954. };
  2955. const int A::a;
  2956. )", Lang_CXX, "input1.cc");
  2957. auto *FromDWithInit = FirstDeclMatcher<VarDecl>().match(
  2958. FromTU, varDecl(hasName("a"))); // Decl with init
  2959. auto *FromDWithDef = LastDeclMatcher<VarDecl>().match(
  2960. FromTU, varDecl(hasName("a"))); // Decl with definition
  2961. ASSERT_NE(FromDWithInit, FromDWithDef);
  2962. ASSERT_EQ(FromDWithDef->getPreviousDecl(), FromDWithInit);
  2963. auto *ToD0 = cast<VarDecl>(Import(FromDWithInit, Lang_CXX11));
  2964. auto *ToD1 = cast<VarDecl>(Import(FromDWithDef, Lang_CXX11));
  2965. ASSERT_TRUE(ToD0);
  2966. ASSERT_TRUE(ToD1);
  2967. EXPECT_NE(ToD0, ToD1);
  2968. EXPECT_EQ(ToD1->getPreviousDecl(), ToD0);
  2969. }
  2970. TEST_P(ImportVariables, InitAndDefinitionAreInDifferentTUs) {
  2971. auto StructA =
  2972. R"(
  2973. struct A {
  2974. static const int a = 1 + 2;
  2975. };
  2976. )";
  2977. Decl *ToTU = getToTuDecl(StructA, Lang_CXX);
  2978. Decl *FromTU = getTuDecl(std::string(StructA) + "const int A::a;", Lang_CXX,
  2979. "input1.cc");
  2980. auto *FromDWithInit = FirstDeclMatcher<VarDecl>().match(
  2981. FromTU, varDecl(hasName("a"))); // Decl with init
  2982. auto *FromDWithDef = LastDeclMatcher<VarDecl>().match(
  2983. FromTU, varDecl(hasName("a"))); // Decl with definition
  2984. ASSERT_EQ(FromDWithInit, FromDWithDef->getPreviousDecl());
  2985. ASSERT_TRUE(FromDWithInit->getInit());
  2986. ASSERT_FALSE(FromDWithInit->isThisDeclarationADefinition());
  2987. ASSERT_TRUE(FromDWithDef->isThisDeclarationADefinition());
  2988. ASSERT_FALSE(FromDWithDef->getInit());
  2989. auto *ToD = FirstDeclMatcher<VarDecl>().match(
  2990. ToTU, varDecl(hasName("a"))); // Decl with init
  2991. ASSERT_TRUE(ToD->getInit());
  2992. ASSERT_FALSE(ToD->getDefinition());
  2993. auto *ImportedD = cast<VarDecl>(Import(FromDWithDef, Lang_CXX11));
  2994. EXPECT_TRUE(ImportedD->getAnyInitializer());
  2995. EXPECT_TRUE(ImportedD->getDefinition());
  2996. }
  2997. TEST_P(ImportVariables, InitAndDefinitionAreInTheFromContext) {
  2998. auto StructA =
  2999. R"(
  3000. struct A {
  3001. static const int a;
  3002. };
  3003. )";
  3004. Decl *ToTU = getToTuDecl(StructA, Lang_CXX);
  3005. Decl *FromTU = getTuDecl(std::string(StructA) + "const int A::a = 1 + 2;",
  3006. Lang_CXX, "input1.cc");
  3007. auto *FromDDeclarationOnly = FirstDeclMatcher<VarDecl>().match(
  3008. FromTU, varDecl(hasName("a")));
  3009. auto *FromDWithDef = LastDeclMatcher<VarDecl>().match(
  3010. FromTU, varDecl(hasName("a"))); // Decl with definition and with init.
  3011. ASSERT_EQ(FromDDeclarationOnly, FromDWithDef->getPreviousDecl());
  3012. ASSERT_FALSE(FromDDeclarationOnly->getInit());
  3013. ASSERT_FALSE(FromDDeclarationOnly->isThisDeclarationADefinition());
  3014. ASSERT_TRUE(FromDWithDef->isThisDeclarationADefinition());
  3015. ASSERT_TRUE(FromDWithDef->getInit());
  3016. auto *ToD = FirstDeclMatcher<VarDecl>().match(
  3017. ToTU, varDecl(hasName("a")));
  3018. ASSERT_FALSE(ToD->getInit());
  3019. ASSERT_FALSE(ToD->getDefinition());
  3020. auto *ImportedD = cast<VarDecl>(Import(FromDWithDef, Lang_CXX11));
  3021. EXPECT_TRUE(ImportedD->getAnyInitializer());
  3022. EXPECT_TRUE(ImportedD->getDefinition());
  3023. }
  3024. struct ImportClasses : ASTImporterOptionSpecificTestBase {};
  3025. TEST_P(ImportClasses, ImportDefinitionWhenProtoIsInNestedToContext) {
  3026. Decl *ToTU = getToTuDecl("struct A { struct X *Xp; };", Lang_C);
  3027. Decl *FromTU1 = getTuDecl("struct X {};", Lang_C, "input1.cc");
  3028. auto Pattern = recordDecl(hasName("X"), unless(isImplicit()));
  3029. auto ToProto = FirstDeclMatcher<RecordDecl>().match(ToTU, Pattern);
  3030. auto FromDef = FirstDeclMatcher<RecordDecl>().match(FromTU1, Pattern);
  3031. Decl *ImportedDef = Import(FromDef, Lang_C);
  3032. EXPECT_NE(ImportedDef, ToProto);
  3033. EXPECT_EQ(DeclCounter<RecordDecl>().match(ToTU, Pattern), 2u);
  3034. auto ToDef = LastDeclMatcher<RecordDecl>().match(ToTU, Pattern);
  3035. EXPECT_TRUE(ImportedDef == ToDef);
  3036. EXPECT_TRUE(ToDef->isThisDeclarationADefinition());
  3037. EXPECT_FALSE(ToProto->isThisDeclarationADefinition());
  3038. EXPECT_EQ(ToDef->getPreviousDecl(), ToProto);
  3039. }
  3040. TEST_P(ImportClasses, ImportDefinitionWhenProtoIsInNestedToContextCXX) {
  3041. Decl *ToTU = getToTuDecl("struct A { struct X *Xp; };", Lang_CXX);
  3042. Decl *FromTU1 = getTuDecl("struct X {};", Lang_CXX, "input1.cc");
  3043. auto Pattern = recordDecl(hasName("X"), unless(isImplicit()));
  3044. auto ToProto = FirstDeclMatcher<RecordDecl>().match(ToTU, Pattern);
  3045. auto FromDef = FirstDeclMatcher<RecordDecl>().match(FromTU1, Pattern);
  3046. Decl *ImportedDef = Import(FromDef, Lang_CXX);
  3047. EXPECT_NE(ImportedDef, ToProto);
  3048. EXPECT_EQ(DeclCounter<RecordDecl>().match(ToTU, Pattern), 2u);
  3049. auto ToDef = LastDeclMatcher<RecordDecl>().match(ToTU, Pattern);
  3050. EXPECT_TRUE(ImportedDef == ToDef);
  3051. EXPECT_TRUE(ToDef->isThisDeclarationADefinition());
  3052. EXPECT_FALSE(ToProto->isThisDeclarationADefinition());
  3053. EXPECT_EQ(ToDef->getPreviousDecl(), ToProto);
  3054. }
  3055. TEST_P(ImportClasses, ImportNestedPrototypeThenDefinition) {
  3056. Decl *FromTU0 = getTuDecl("struct A { struct X *Xp; };", Lang_C, "input0.cc");
  3057. Decl *FromTU1 = getTuDecl("struct X {};", Lang_C, "input1.cc");
  3058. auto Pattern = recordDecl(hasName("X"), unless(isImplicit()));
  3059. auto FromProto = FirstDeclMatcher<RecordDecl>().match(FromTU0, Pattern);
  3060. auto FromDef = FirstDeclMatcher<RecordDecl>().match(FromTU1, Pattern);
  3061. Decl *ImportedProto = Import(FromProto, Lang_C);
  3062. Decl *ImportedDef = Import(FromDef, Lang_C);
  3063. Decl *ToTU = ImportedDef->getTranslationUnitDecl();
  3064. EXPECT_NE(ImportedDef, ImportedProto);
  3065. EXPECT_EQ(DeclCounter<RecordDecl>().match(ToTU, Pattern), 2u);
  3066. auto ToProto = FirstDeclMatcher<RecordDecl>().match(ToTU, Pattern);
  3067. auto ToDef = LastDeclMatcher<RecordDecl>().match(ToTU, Pattern);
  3068. EXPECT_TRUE(ImportedDef == ToDef);
  3069. EXPECT_TRUE(ImportedProto == ToProto);
  3070. EXPECT_TRUE(ToDef->isThisDeclarationADefinition());
  3071. EXPECT_FALSE(ToProto->isThisDeclarationADefinition());
  3072. EXPECT_EQ(ToDef->getPreviousDecl(), ToProto);
  3073. }
  3074. struct ImportFriendClasses : ASTImporterOptionSpecificTestBase {};
  3075. TEST_P(ImportFriendClasses, ImportOfFriendRecordDoesNotMergeDefinition) {
  3076. Decl *FromTU = getTuDecl(
  3077. R"(
  3078. class A {
  3079. template <int I> class F {};
  3080. class X {
  3081. template <int I> friend class F;
  3082. };
  3083. };
  3084. )",
  3085. Lang_CXX, "input0.cc");
  3086. auto *FromClass = FirstDeclMatcher<CXXRecordDecl>().match(
  3087. FromTU, cxxRecordDecl(hasName("F"), isDefinition()));
  3088. auto *FromFriendClass = LastDeclMatcher<CXXRecordDecl>().match(
  3089. FromTU, cxxRecordDecl(hasName("F")));
  3090. ASSERT_TRUE(FromClass);
  3091. ASSERT_TRUE(FromFriendClass);
  3092. ASSERT_NE(FromClass, FromFriendClass);
  3093. ASSERT_EQ(FromFriendClass->getDefinition(), FromClass);
  3094. ASSERT_EQ(FromFriendClass->getPreviousDecl(), FromClass);
  3095. ASSERT_EQ(FromFriendClass->getDescribedClassTemplate()->getPreviousDecl(),
  3096. FromClass->getDescribedClassTemplate());
  3097. auto *ToClass = cast<CXXRecordDecl>(Import(FromClass, Lang_CXX));
  3098. auto *ToFriendClass = cast<CXXRecordDecl>(Import(FromFriendClass, Lang_CXX));
  3099. EXPECT_TRUE(ToClass);
  3100. EXPECT_TRUE(ToFriendClass);
  3101. EXPECT_NE(ToClass, ToFriendClass);
  3102. EXPECT_EQ(ToFriendClass->getDefinition(), ToClass);
  3103. EXPECT_EQ(ToFriendClass->getPreviousDecl(), ToClass);
  3104. EXPECT_EQ(ToFriendClass->getDescribedClassTemplate()->getPreviousDecl(),
  3105. ToClass->getDescribedClassTemplate());
  3106. }
  3107. TEST_P(ImportFriendClasses, ImportOfRecursiveFriendClass) {
  3108. Decl *FromTu = getTuDecl(
  3109. R"(
  3110. class declToImport {
  3111. friend class declToImport;
  3112. };
  3113. )",
  3114. Lang_CXX, "input.cc");
  3115. auto *FromD = FirstDeclMatcher<CXXRecordDecl>().match(
  3116. FromTu, cxxRecordDecl(hasName("declToImport")));
  3117. auto *ToD = Import(FromD, Lang_CXX);
  3118. auto Pattern = cxxRecordDecl(has(friendDecl()));
  3119. ASSERT_TRUE(MatchVerifier<Decl>{}.match(FromD, Pattern));
  3120. EXPECT_TRUE(MatchVerifier<Decl>{}.match(ToD, Pattern));
  3121. }
  3122. TEST_P(ImportFriendClasses, ImportOfRecursiveFriendClassTemplate) {
  3123. Decl *FromTu = getTuDecl(
  3124. R"(
  3125. template<class A> class declToImport {
  3126. template<class A1> friend class declToImport;
  3127. };
  3128. )",
  3129. Lang_CXX, "input.cc");
  3130. auto *FromD =
  3131. FirstDeclMatcher<ClassTemplateDecl>().match(FromTu, classTemplateDecl());
  3132. auto *ToD = Import(FromD, Lang_CXX);
  3133. auto Pattern = classTemplateDecl(
  3134. has(cxxRecordDecl(has(friendDecl(has(classTemplateDecl()))))));
  3135. ASSERT_TRUE(MatchVerifier<Decl>{}.match(FromD, Pattern));
  3136. EXPECT_TRUE(MatchVerifier<Decl>{}.match(ToD, Pattern));
  3137. auto *Class =
  3138. FirstDeclMatcher<ClassTemplateDecl>().match(ToD, classTemplateDecl());
  3139. auto *Friend = FirstDeclMatcher<FriendDecl>().match(ToD, friendDecl());
  3140. EXPECT_NE(Friend->getFriendDecl(), Class);
  3141. EXPECT_EQ(Friend->getFriendDecl()->getPreviousDecl(), Class);
  3142. }
  3143. TEST_P(ImportFriendClasses, ProperPrevDeclForClassTemplateDecls) {
  3144. auto Pattern = classTemplateSpecializationDecl(hasName("X"));
  3145. ClassTemplateSpecializationDecl *Imported1;
  3146. {
  3147. Decl *FromTU = getTuDecl("template<class T> class X;"
  3148. "struct Y { friend class X<int>; };",
  3149. Lang_CXX, "input0.cc");
  3150. auto *FromD = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  3151. FromTU, Pattern);
  3152. Imported1 = cast<ClassTemplateSpecializationDecl>(Import(FromD, Lang_CXX));
  3153. }
  3154. ClassTemplateSpecializationDecl *Imported2;
  3155. {
  3156. Decl *FromTU = getTuDecl("template<class T> class X;"
  3157. "template<> class X<int>{};"
  3158. "struct Z { friend class X<int>; };",
  3159. Lang_CXX, "input1.cc");
  3160. auto *FromD = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  3161. FromTU, Pattern);
  3162. Imported2 = cast<ClassTemplateSpecializationDecl>(Import(FromD, Lang_CXX));
  3163. }
  3164. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  3165. EXPECT_EQ(DeclCounter<ClassTemplateSpecializationDecl>().match(ToTU, Pattern),
  3166. 2u);
  3167. ASSERT_TRUE(Imported2->getPreviousDecl());
  3168. EXPECT_EQ(Imported2->getPreviousDecl(), Imported1);
  3169. }
  3170. TEST_P(ImportFriendClasses, TypeForDeclShouldBeSetInTemplated) {
  3171. Decl *FromTU0 = getTuDecl(
  3172. R"(
  3173. class X {
  3174. class Y;
  3175. };
  3176. class X::Y {
  3177. template <typename T>
  3178. friend class F; // The decl context of F is the global namespace.
  3179. };
  3180. )",
  3181. Lang_CXX, "input0.cc");
  3182. auto *Fwd = FirstDeclMatcher<ClassTemplateDecl>().match(
  3183. FromTU0, classTemplateDecl(hasName("F")));
  3184. auto *Imported0 = cast<ClassTemplateDecl>(Import(Fwd, Lang_CXX));
  3185. Decl *FromTU1 = getTuDecl(
  3186. R"(
  3187. template <typename T>
  3188. class F {};
  3189. )",
  3190. Lang_CXX, "input1.cc");
  3191. auto *Definition = FirstDeclMatcher<ClassTemplateDecl>().match(
  3192. FromTU1, classTemplateDecl(hasName("F")));
  3193. auto *Imported1 = cast<ClassTemplateDecl>(Import(Definition, Lang_CXX));
  3194. EXPECT_EQ(Imported0->getTemplatedDecl()->getTypeForDecl(),
  3195. Imported1->getTemplatedDecl()->getTypeForDecl());
  3196. }
  3197. TEST_P(ImportFriendClasses, DeclsFromFriendsShouldBeInRedeclChains) {
  3198. Decl *From, *To;
  3199. std::tie(From, To) =
  3200. getImportedDecl("class declToImport {};", Lang_CXX,
  3201. "class Y { friend class declToImport; };", Lang_CXX);
  3202. auto *Imported = cast<CXXRecordDecl>(To);
  3203. EXPECT_TRUE(Imported->getPreviousDecl());
  3204. }
  3205. TEST_P(ImportFriendClasses,
  3206. ImportOfClassTemplateDefinitionShouldConnectToFwdFriend) {
  3207. Decl *ToTU = getToTuDecl(
  3208. R"(
  3209. class X {
  3210. class Y;
  3211. };
  3212. class X::Y {
  3213. template <typename T>
  3214. friend class F; // The decl context of F is the global namespace.
  3215. };
  3216. )",
  3217. Lang_CXX);
  3218. auto *ToDecl = FirstDeclMatcher<ClassTemplateDecl>().match(
  3219. ToTU, classTemplateDecl(hasName("F")));
  3220. Decl *FromTU = getTuDecl(
  3221. R"(
  3222. template <typename T>
  3223. class F {};
  3224. )",
  3225. Lang_CXX, "input0.cc");
  3226. auto *Definition = FirstDeclMatcher<ClassTemplateDecl>().match(
  3227. FromTU, classTemplateDecl(hasName("F")));
  3228. auto *ImportedDef = cast<ClassTemplateDecl>(Import(Definition, Lang_CXX));
  3229. EXPECT_TRUE(ImportedDef->getPreviousDecl());
  3230. EXPECT_EQ(ToDecl, ImportedDef->getPreviousDecl());
  3231. EXPECT_EQ(ToDecl->getTemplatedDecl(),
  3232. ImportedDef->getTemplatedDecl()->getPreviousDecl());
  3233. }
  3234. TEST_P(ImportFriendClasses,
  3235. ImportOfClassTemplateDefinitionAndFwdFriendShouldBeLinked) {
  3236. Decl *FromTU0 = getTuDecl(
  3237. R"(
  3238. class X {
  3239. class Y;
  3240. };
  3241. class X::Y {
  3242. template <typename T>
  3243. friend class F; // The decl context of F is the global namespace.
  3244. };
  3245. )",
  3246. Lang_CXX, "input0.cc");
  3247. auto *Fwd = FirstDeclMatcher<ClassTemplateDecl>().match(
  3248. FromTU0, classTemplateDecl(hasName("F")));
  3249. auto *ImportedFwd = cast<ClassTemplateDecl>(Import(Fwd, Lang_CXX));
  3250. Decl *FromTU1 = getTuDecl(
  3251. R"(
  3252. template <typename T>
  3253. class F {};
  3254. )",
  3255. Lang_CXX, "input1.cc");
  3256. auto *Definition = FirstDeclMatcher<ClassTemplateDecl>().match(
  3257. FromTU1, classTemplateDecl(hasName("F")));
  3258. auto *ImportedDef = cast<ClassTemplateDecl>(Import(Definition, Lang_CXX));
  3259. EXPECT_TRUE(ImportedDef->getPreviousDecl());
  3260. EXPECT_EQ(ImportedFwd, ImportedDef->getPreviousDecl());
  3261. EXPECT_EQ(ImportedFwd->getTemplatedDecl(),
  3262. ImportedDef->getTemplatedDecl()->getPreviousDecl());
  3263. }
  3264. TEST_P(ImportFriendClasses, ImportOfClassDefinitionAndFwdFriendShouldBeLinked) {
  3265. Decl *FromTU0 = getTuDecl(
  3266. R"(
  3267. class X {
  3268. class Y;
  3269. };
  3270. class X::Y {
  3271. friend class F; // The decl context of F is the global namespace.
  3272. };
  3273. )",
  3274. Lang_CXX, "input0.cc");
  3275. auto *Friend = FirstDeclMatcher<FriendDecl>().match(FromTU0, friendDecl());
  3276. QualType FT = Friend->getFriendType()->getType();
  3277. FT = FromTU0->getASTContext().getCanonicalType(FT);
  3278. auto *Fwd = cast<TagType>(FT)->getDecl();
  3279. auto *ImportedFwd = Import(Fwd, Lang_CXX);
  3280. Decl *FromTU1 = getTuDecl(
  3281. R"(
  3282. class F {};
  3283. )",
  3284. Lang_CXX, "input1.cc");
  3285. auto *Definition = FirstDeclMatcher<CXXRecordDecl>().match(
  3286. FromTU1, cxxRecordDecl(hasName("F")));
  3287. auto *ImportedDef = Import(Definition, Lang_CXX);
  3288. EXPECT_TRUE(ImportedDef->getPreviousDecl());
  3289. EXPECT_EQ(ImportedFwd, ImportedDef->getPreviousDecl());
  3290. }
  3291. TEST_P(ASTImporterOptionSpecificTestBase, FriendFunInClassTemplate) {
  3292. auto *Code = R"(
  3293. template <class T>
  3294. struct X {
  3295. friend void foo(){}
  3296. };
  3297. )";
  3298. TranslationUnitDecl *ToTU = getToTuDecl(Code, Lang_CXX);
  3299. auto *ToFoo = FirstDeclMatcher<FunctionDecl>().match(
  3300. ToTU, functionDecl(hasName("foo")));
  3301. TranslationUnitDecl *FromTU = getTuDecl(Code, Lang_CXX, "input.cc");
  3302. auto *FromFoo = FirstDeclMatcher<FunctionDecl>().match(
  3303. FromTU, functionDecl(hasName("foo")));
  3304. auto *ImportedFoo = Import(FromFoo, Lang_CXX);
  3305. EXPECT_EQ(ImportedFoo, ToFoo);
  3306. }
  3307. struct DeclContextTest : ASTImporterOptionSpecificTestBase {};
  3308. TEST_P(DeclContextTest, removeDeclOfClassTemplateSpecialization) {
  3309. Decl *TU = getTuDecl(
  3310. R"(
  3311. namespace NS {
  3312. template <typename T>
  3313. struct S {};
  3314. template struct S<int>;
  3315. inline namespace INS {
  3316. template <typename T>
  3317. struct S {};
  3318. template struct S<int>;
  3319. }
  3320. }
  3321. )", Lang_CXX11, "input0.cc");
  3322. auto *NS = FirstDeclMatcher<NamespaceDecl>().match(
  3323. TU, namespaceDecl());
  3324. auto *Spec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  3325. TU, classTemplateSpecializationDecl());
  3326. ASSERT_TRUE(NS->containsDecl(Spec));
  3327. NS->removeDecl(Spec);
  3328. EXPECT_FALSE(NS->containsDecl(Spec));
  3329. }
  3330. TEST_P(DeclContextTest,
  3331. removeDeclShouldNotFailEvenIfWeHaveExternalVisibleStorage) {
  3332. Decl *TU = getTuDecl("extern int A; int A;", Lang_CXX);
  3333. auto *A0 = FirstDeclMatcher<VarDecl>().match(TU, varDecl(hasName("A")));
  3334. auto *A1 = LastDeclMatcher<VarDecl>().match(TU, varDecl(hasName("A")));
  3335. // Investigate the list.
  3336. auto *DC = A0->getDeclContext();
  3337. ASSERT_TRUE(DC->containsDecl(A0));
  3338. ASSERT_TRUE(DC->containsDecl(A1));
  3339. // Investigate the lookup table.
  3340. auto *Map = DC->getLookupPtr();
  3341. ASSERT_TRUE(Map);
  3342. auto I = Map->find(A0->getDeclName());
  3343. ASSERT_NE(I, Map->end());
  3344. StoredDeclsList &L = I->second;
  3345. // The lookup table contains the most recent decl of A.
  3346. ASSERT_NE(L.getAsDecl(), A0);
  3347. ASSERT_EQ(L.getAsDecl(), A1);
  3348. ASSERT_TRUE(L.getAsDecl());
  3349. // Simulate the private function DeclContext::reconcileExternalVisibleStorage.
  3350. // The point here is to have a Vec with only one element, which is not the
  3351. // one we are going to delete from the DC later.
  3352. L.setHasExternalDecls();
  3353. ASSERT_TRUE(L.getAsVector());
  3354. ASSERT_EQ(1u, L.getAsVector()->size());
  3355. // This asserts in the old implementation.
  3356. DC->removeDecl(A0);
  3357. EXPECT_FALSE(DC->containsDecl(A0));
  3358. }
  3359. struct ImportFunctionTemplateSpecializations
  3360. : ASTImporterOptionSpecificTestBase {};
  3361. TEST_P(ImportFunctionTemplateSpecializations,
  3362. TUshouldNotContainFunctionTemplateImplicitInstantiation) {
  3363. Decl *FromTU = getTuDecl(
  3364. R"(
  3365. template<class T>
  3366. int f() { return 0; }
  3367. void foo() { f<int>(); }
  3368. )",
  3369. Lang_CXX, "input0.cc");
  3370. // Check that the function template instantiation is NOT the child of the TU.
  3371. auto Pattern = translationUnitDecl(
  3372. unless(has(functionDecl(hasName("f"), isTemplateInstantiation()))));
  3373. ASSERT_TRUE(MatchVerifier<Decl>{}.match(FromTU, Pattern));
  3374. auto *Foo = FirstDeclMatcher<FunctionDecl>().match(
  3375. FromTU, functionDecl(hasName("foo")));
  3376. ASSERT_TRUE(Import(Foo, Lang_CXX));
  3377. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  3378. EXPECT_TRUE(MatchVerifier<Decl>{}.match(ToTU, Pattern));
  3379. }
  3380. TEST_P(ImportFunctionTemplateSpecializations,
  3381. TUshouldNotContainFunctionTemplateExplicitInstantiation) {
  3382. Decl *FromTU = getTuDecl(
  3383. R"(
  3384. template<class T>
  3385. int f() { return 0; }
  3386. template int f<int>();
  3387. )",
  3388. Lang_CXX, "input0.cc");
  3389. // Check that the function template instantiation is NOT the child of the TU.
  3390. auto Instantiation = functionDecl(hasName("f"), isTemplateInstantiation());
  3391. auto Pattern = translationUnitDecl(unless(has(Instantiation)));
  3392. ASSERT_TRUE(MatchVerifier<Decl>{}.match(FromTU, Pattern));
  3393. ASSERT_TRUE(
  3394. Import(FirstDeclMatcher<Decl>().match(FromTU, Instantiation), Lang_CXX));
  3395. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  3396. EXPECT_TRUE(MatchVerifier<Decl>{}.match(ToTU, Pattern));
  3397. }
  3398. TEST_P(ImportFunctionTemplateSpecializations,
  3399. TUshouldContainFunctionTemplateSpecialization) {
  3400. Decl *FromTU = getTuDecl(
  3401. R"(
  3402. template<class T>
  3403. int f() { return 0; }
  3404. template <> int f<int>() { return 4; }
  3405. )",
  3406. Lang_CXX, "input0.cc");
  3407. // Check that the function template specialization is the child of the TU.
  3408. auto Specialization =
  3409. functionDecl(hasName("f"), isExplicitTemplateSpecialization());
  3410. auto Pattern = translationUnitDecl(has(Specialization));
  3411. ASSERT_TRUE(MatchVerifier<Decl>{}.match(FromTU, Pattern));
  3412. ASSERT_TRUE(
  3413. Import(FirstDeclMatcher<Decl>().match(FromTU, Specialization), Lang_CXX));
  3414. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  3415. EXPECT_TRUE(MatchVerifier<Decl>{}.match(ToTU, Pattern));
  3416. }
  3417. TEST_P(ImportFunctionTemplateSpecializations,
  3418. FunctionTemplateSpecializationRedeclChain) {
  3419. Decl *FromTU = getTuDecl(
  3420. R"(
  3421. template<class T>
  3422. int f() { return 0; }
  3423. template <> int f<int>() { return 4; }
  3424. )",
  3425. Lang_CXX, "input0.cc");
  3426. auto Spec = functionDecl(hasName("f"), isExplicitTemplateSpecialization(),
  3427. hasParent(translationUnitDecl()));
  3428. auto *FromSpecD = FirstDeclMatcher<Decl>().match(FromTU, Spec);
  3429. {
  3430. auto *TU = FromTU;
  3431. auto *SpecD = FromSpecD;
  3432. auto *TemplateD = FirstDeclMatcher<FunctionTemplateDecl>().match(
  3433. TU, functionTemplateDecl());
  3434. auto *FirstSpecD = *(TemplateD->spec_begin());
  3435. ASSERT_EQ(SpecD, FirstSpecD);
  3436. ASSERT_TRUE(SpecD->getPreviousDecl());
  3437. ASSERT_FALSE(cast<FunctionDecl>(SpecD->getPreviousDecl())
  3438. ->doesThisDeclarationHaveABody());
  3439. }
  3440. ASSERT_TRUE(Import(FromSpecD, Lang_CXX));
  3441. {
  3442. auto *TU = ToAST->getASTContext().getTranslationUnitDecl();
  3443. auto *SpecD = FirstDeclMatcher<Decl>().match(TU, Spec);
  3444. auto *TemplateD = FirstDeclMatcher<FunctionTemplateDecl>().match(
  3445. TU, functionTemplateDecl());
  3446. auto *FirstSpecD = *(TemplateD->spec_begin());
  3447. EXPECT_EQ(SpecD, FirstSpecD);
  3448. ASSERT_TRUE(SpecD->getPreviousDecl());
  3449. EXPECT_FALSE(cast<FunctionDecl>(SpecD->getPreviousDecl())
  3450. ->doesThisDeclarationHaveABody());
  3451. }
  3452. }
  3453. TEST_P(ImportFunctionTemplateSpecializations,
  3454. MatchNumberOfFunctionTemplateSpecializations) {
  3455. Decl *FromTU = getTuDecl(
  3456. R"(
  3457. template <typename T> constexpr int f() { return 0; }
  3458. template <> constexpr int f<int>() { return 4; }
  3459. void foo() {
  3460. static_assert(f<char>() == 0, "");
  3461. static_assert(f<int>() == 4, "");
  3462. }
  3463. )",
  3464. Lang_CXX11, "input0.cc");
  3465. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  3466. FromTU, functionDecl(hasName("foo")));
  3467. Import(FromD, Lang_CXX11);
  3468. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  3469. EXPECT_EQ(
  3470. DeclCounter<FunctionDecl>().match(FromTU, functionDecl(hasName("f"))),
  3471. DeclCounter<FunctionDecl>().match(ToTU, functionDecl(hasName("f"))));
  3472. }
  3473. TEST_P(ASTImporterOptionSpecificTestBase,
  3474. ImportShouldNotReportFalseODRErrorWhenRecordIsBeingDefined) {
  3475. {
  3476. Decl *FromTU = getTuDecl(
  3477. R"(
  3478. template <typename T>
  3479. struct B;
  3480. )",
  3481. Lang_CXX, "input0.cc");
  3482. auto *FromD = FirstDeclMatcher<ClassTemplateDecl>().match(
  3483. FromTU, classTemplateDecl(hasName("B")));
  3484. Import(FromD, Lang_CXX);
  3485. }
  3486. {
  3487. Decl *FromTU = getTuDecl(
  3488. R"(
  3489. template <typename T>
  3490. struct B {
  3491. void f();
  3492. B* b;
  3493. };
  3494. )",
  3495. Lang_CXX, "input1.cc");
  3496. FunctionDecl *FromD = FirstDeclMatcher<FunctionDecl>().match(
  3497. FromTU, functionDecl(hasName("f")));
  3498. Import(FromD, Lang_CXX);
  3499. auto *FromCTD = FirstDeclMatcher<ClassTemplateDecl>().match(
  3500. FromTU, classTemplateDecl(hasName("B")));
  3501. auto *ToCTD = cast<ClassTemplateDecl>(Import(FromCTD, Lang_CXX));
  3502. EXPECT_TRUE(ToCTD->isThisDeclarationADefinition());
  3503. // We expect no (ODR) warning during the import.
  3504. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  3505. EXPECT_EQ(0u, ToTU->getASTContext().getDiagnostics().getNumWarnings());
  3506. }
  3507. }
  3508. TEST_P(ASTImporterOptionSpecificTestBase,
  3509. ImportingTypedefShouldImportTheCompleteType) {
  3510. // We already have an incomplete underlying type in the "To" context.
  3511. auto Code =
  3512. R"(
  3513. template <typename T>
  3514. struct S {
  3515. void foo();
  3516. };
  3517. using U = S<int>;
  3518. )";
  3519. Decl *ToTU = getToTuDecl(Code, Lang_CXX11);
  3520. auto *ToD = FirstDeclMatcher<TypedefNameDecl>().match(ToTU,
  3521. typedefNameDecl(hasName("U")));
  3522. ASSERT_TRUE(ToD->getUnderlyingType()->isIncompleteType());
  3523. // The "From" context has the same typedef, but the underlying type is
  3524. // complete this time.
  3525. Decl *FromTU = getTuDecl(std::string(Code) +
  3526. R"(
  3527. void foo(U* u) {
  3528. u->foo();
  3529. }
  3530. )", Lang_CXX11);
  3531. auto *FromD = FirstDeclMatcher<TypedefNameDecl>().match(FromTU,
  3532. typedefNameDecl(hasName("U")));
  3533. ASSERT_FALSE(FromD->getUnderlyingType()->isIncompleteType());
  3534. // The imported type should be complete.
  3535. auto *ImportedD = cast<TypedefNameDecl>(Import(FromD, Lang_CXX11));
  3536. EXPECT_FALSE(ImportedD->getUnderlyingType()->isIncompleteType());
  3537. }
  3538. TEST_P(ASTImporterOptionSpecificTestBase, ImportTemplateParameterLists) {
  3539. auto Code =
  3540. R"(
  3541. template<class T>
  3542. int f() { return 0; }
  3543. template <> int f<int>() { return 4; }
  3544. )";
  3545. Decl *FromTU = getTuDecl(Code, Lang_CXX);
  3546. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU,
  3547. functionDecl(hasName("f"), isExplicitTemplateSpecialization()));
  3548. ASSERT_EQ(FromD->getNumTemplateParameterLists(), 1u);
  3549. auto *ToD = Import(FromD, Lang_CXX);
  3550. // The template parameter list should exist.
  3551. EXPECT_EQ(ToD->getNumTemplateParameterLists(), 1u);
  3552. }
  3553. struct ASTImporterLookupTableTest : ASTImporterOptionSpecificTestBase {};
  3554. TEST_P(ASTImporterLookupTableTest, OneDecl) {
  3555. auto *ToTU = getToTuDecl("int a;", Lang_CXX);
  3556. auto *D = FirstDeclMatcher<VarDecl>().match(ToTU, varDecl(hasName("a")));
  3557. ASTImporterLookupTable LT(*ToTU);
  3558. auto Res = LT.lookup(ToTU, D->getDeclName());
  3559. ASSERT_EQ(Res.size(), 1u);
  3560. EXPECT_EQ(*Res.begin(), D);
  3561. }
  3562. static Decl *findInDeclListOfDC(DeclContext *DC, DeclarationName Name) {
  3563. for (Decl *D : DC->decls()) {
  3564. if (auto *ND = dyn_cast<NamedDecl>(D))
  3565. if (ND->getDeclName() == Name)
  3566. return ND;
  3567. }
  3568. return nullptr;
  3569. }
  3570. TEST_P(ASTImporterLookupTableTest,
  3571. FriendWhichIsnotFoundByNormalLookupShouldBeFoundByImporterSpecificLookup) {
  3572. auto *Code = R"(
  3573. template <class T>
  3574. struct X {
  3575. friend void foo(){}
  3576. };
  3577. )";
  3578. TranslationUnitDecl *ToTU = getToTuDecl(Code, Lang_CXX);
  3579. auto *X = FirstDeclMatcher<ClassTemplateDecl>().match(
  3580. ToTU, classTemplateDecl(hasName("X")));
  3581. auto *Foo = FirstDeclMatcher<FunctionDecl>().match(
  3582. ToTU, functionDecl(hasName("foo")));
  3583. DeclContext *FooDC = Foo->getDeclContext();
  3584. DeclContext *FooLexicalDC = Foo->getLexicalDeclContext();
  3585. ASSERT_EQ(cast<Decl>(FooLexicalDC), X->getTemplatedDecl());
  3586. ASSERT_EQ(cast<Decl>(FooDC), ToTU);
  3587. DeclarationName FooName = Foo->getDeclName();
  3588. // Cannot find in the LookupTable of its DC (TUDecl)
  3589. SmallVector<NamedDecl *, 2> FoundDecls;
  3590. FooDC->getRedeclContext()->localUncachedLookup(FooName, FoundDecls);
  3591. EXPECT_EQ(FoundDecls.size(), 0u);
  3592. // Cannot find in the LookupTable of its LexicalDC (X)
  3593. FooLexicalDC->getRedeclContext()->localUncachedLookup(FooName, FoundDecls);
  3594. EXPECT_EQ(FoundDecls.size(), 0u);
  3595. // Can't find in the list of Decls of the DC.
  3596. EXPECT_EQ(findInDeclListOfDC(FooDC, FooName), nullptr);
  3597. // Can't find in the list of Decls of the LexicalDC
  3598. EXPECT_EQ(findInDeclListOfDC(FooLexicalDC, FooName), nullptr);
  3599. // ASTImporter specific lookup finds it.
  3600. ASTImporterLookupTable LT(*ToTU);
  3601. auto Res = LT.lookup(FooDC, Foo->getDeclName());
  3602. ASSERT_EQ(Res.size(), 1u);
  3603. EXPECT_EQ(*Res.begin(), Foo);
  3604. }
  3605. TEST_P(ASTImporterLookupTableTest,
  3606. FwdDeclStructShouldBeFoundByImporterSpecificLookup) {
  3607. TranslationUnitDecl *ToTU =
  3608. getToTuDecl("struct A { struct Foo *p; };", Lang_C);
  3609. auto *Foo =
  3610. FirstDeclMatcher<RecordDecl>().match(ToTU, recordDecl(hasName("Foo")));
  3611. auto *A =
  3612. FirstDeclMatcher<RecordDecl>().match(ToTU, recordDecl(hasName("A")));
  3613. DeclContext *FooDC = Foo->getDeclContext();
  3614. DeclContext *FooLexicalDC = Foo->getLexicalDeclContext();
  3615. ASSERT_EQ(cast<Decl>(FooLexicalDC), A);
  3616. ASSERT_EQ(cast<Decl>(FooDC), ToTU);
  3617. DeclarationName FooName = Foo->getDeclName();
  3618. // Cannot find in the LookupTable of its DC (TUDecl).
  3619. SmallVector<NamedDecl *, 2> FoundDecls;
  3620. FooDC->getRedeclContext()->localUncachedLookup(FooName, FoundDecls);
  3621. EXPECT_EQ(FoundDecls.size(), 0u);
  3622. // Cannot find in the LookupTable of its LexicalDC (A).
  3623. FooLexicalDC->getRedeclContext()->localUncachedLookup(FooName, FoundDecls);
  3624. EXPECT_EQ(FoundDecls.size(), 0u);
  3625. // Can't find in the list of Decls of the DC.
  3626. EXPECT_EQ(findInDeclListOfDC(FooDC, FooName), nullptr);
  3627. // Can find in the list of Decls of the LexicalDC.
  3628. EXPECT_EQ(findInDeclListOfDC(FooLexicalDC, FooName), Foo);
  3629. // ASTImporter specific lookup finds it.
  3630. ASTImporterLookupTable LT(*ToTU);
  3631. auto Res = LT.lookup(FooDC, Foo->getDeclName());
  3632. ASSERT_EQ(Res.size(), 1u);
  3633. EXPECT_EQ(*Res.begin(), Foo);
  3634. }
  3635. TEST_P(ASTImporterLookupTableTest, LookupFindsNamesInDifferentDC) {
  3636. TranslationUnitDecl *ToTU =
  3637. getToTuDecl("int V; struct A { int V; }; struct B { int V; };", Lang_C);
  3638. DeclarationName VName = FirstDeclMatcher<VarDecl>()
  3639. .match(ToTU, varDecl(hasName("V")))
  3640. ->getDeclName();
  3641. auto *A =
  3642. FirstDeclMatcher<RecordDecl>().match(ToTU, recordDecl(hasName("A")));
  3643. auto *B =
  3644. FirstDeclMatcher<RecordDecl>().match(ToTU, recordDecl(hasName("B")));
  3645. ASTImporterLookupTable LT(*ToTU);
  3646. auto Res = LT.lookup(cast<DeclContext>(A), VName);
  3647. ASSERT_EQ(Res.size(), 1u);
  3648. EXPECT_EQ(*Res.begin(), FirstDeclMatcher<FieldDecl>().match(
  3649. ToTU, fieldDecl(hasName("V"),
  3650. hasParent(recordDecl(hasName("A"))))));
  3651. Res = LT.lookup(cast<DeclContext>(B), VName);
  3652. ASSERT_EQ(Res.size(), 1u);
  3653. EXPECT_EQ(*Res.begin(), FirstDeclMatcher<FieldDecl>().match(
  3654. ToTU, fieldDecl(hasName("V"),
  3655. hasParent(recordDecl(hasName("B"))))));
  3656. Res = LT.lookup(ToTU, VName);
  3657. ASSERT_EQ(Res.size(), 1u);
  3658. EXPECT_EQ(*Res.begin(), FirstDeclMatcher<VarDecl>().match(
  3659. ToTU, varDecl(hasName("V"),
  3660. hasParent(translationUnitDecl()))));
  3661. }
  3662. TEST_P(ASTImporterLookupTableTest, LookupFindsOverloadedNames) {
  3663. TranslationUnitDecl *ToTU = getToTuDecl(
  3664. R"(
  3665. void foo();
  3666. void foo(int);
  3667. void foo(int, int);
  3668. )",
  3669. Lang_CXX);
  3670. ASTImporterLookupTable LT(*ToTU);
  3671. auto *F0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, functionDecl());
  3672. auto *F2 = LastDeclMatcher<FunctionDecl>().match(ToTU, functionDecl());
  3673. DeclarationName Name = F0->getDeclName();
  3674. auto Res = LT.lookup(ToTU, Name);
  3675. EXPECT_EQ(Res.size(), 3u);
  3676. EXPECT_EQ(Res.count(F0), 1u);
  3677. EXPECT_EQ(Res.count(F2), 1u);
  3678. }
  3679. TEST_P(ASTImporterLookupTableTest,
  3680. DifferentOperatorsShouldHaveDifferentResultSet) {
  3681. TranslationUnitDecl *ToTU = getToTuDecl(
  3682. R"(
  3683. struct X{};
  3684. void operator+(X, X);
  3685. void operator-(X, X);
  3686. )",
  3687. Lang_CXX);
  3688. ASTImporterLookupTable LT(*ToTU);
  3689. auto *FPlus = FirstDeclMatcher<FunctionDecl>().match(
  3690. ToTU, functionDecl(hasOverloadedOperatorName("+")));
  3691. auto *FMinus = FirstDeclMatcher<FunctionDecl>().match(
  3692. ToTU, functionDecl(hasOverloadedOperatorName("-")));
  3693. DeclarationName NamePlus = FPlus->getDeclName();
  3694. auto ResPlus = LT.lookup(ToTU, NamePlus);
  3695. EXPECT_EQ(ResPlus.size(), 1u);
  3696. EXPECT_EQ(ResPlus.count(FPlus), 1u);
  3697. EXPECT_EQ(ResPlus.count(FMinus), 0u);
  3698. DeclarationName NameMinus = FMinus->getDeclName();
  3699. auto ResMinus = LT.lookup(ToTU, NameMinus);
  3700. EXPECT_EQ(ResMinus.size(), 1u);
  3701. EXPECT_EQ(ResMinus.count(FMinus), 1u);
  3702. EXPECT_EQ(ResMinus.count(FPlus), 0u);
  3703. EXPECT_NE(*ResMinus.begin(), *ResPlus.begin());
  3704. }
  3705. TEST_P(ASTImporterLookupTableTest, LookupDeclNamesFromDifferentTUs) {
  3706. TranslationUnitDecl *ToTU = getToTuDecl(
  3707. R"(
  3708. struct X {};
  3709. void operator+(X, X);
  3710. )",
  3711. Lang_CXX);
  3712. auto *ToPlus = FirstDeclMatcher<FunctionDecl>().match(
  3713. ToTU, functionDecl(hasOverloadedOperatorName("+")));
  3714. Decl *FromTU = getTuDecl(
  3715. R"(
  3716. struct X {};
  3717. void operator+(X, X);
  3718. )",
  3719. Lang_CXX);
  3720. auto *FromPlus = FirstDeclMatcher<FunctionDecl>().match(
  3721. FromTU, functionDecl(hasOverloadedOperatorName("+")));
  3722. // FromPlus have a different TU, thus its DeclarationName is different too.
  3723. ASSERT_NE(ToPlus->getDeclName(), FromPlus->getDeclName());
  3724. ASTImporterLookupTable LT(*ToTU);
  3725. auto Res = LT.lookup(ToTU, ToPlus->getDeclName());
  3726. ASSERT_EQ(Res.size(), 1u);
  3727. EXPECT_EQ(*Res.begin(), ToPlus);
  3728. // FromPlus have a different TU, thus its DeclarationName is different too.
  3729. Res = LT.lookup(ToTU, FromPlus->getDeclName());
  3730. ASSERT_EQ(Res.size(), 0u);
  3731. }
  3732. static const RecordDecl *getRecordDeclOfFriend(FriendDecl *FD) {
  3733. QualType Ty = FD->getFriendType()->getType().getCanonicalType();
  3734. return cast<RecordType>(Ty)->getDecl();
  3735. }
  3736. TEST_P(ASTImporterLookupTableTest,
  3737. LookupFindsFwdFriendClassDeclWithElaboratedType) {
  3738. TranslationUnitDecl *ToTU = getToTuDecl(
  3739. R"(
  3740. class Y { friend class F; };
  3741. )",
  3742. Lang_CXX);
  3743. // In this case, the CXXRecordDecl is hidden, the FriendDecl is not a parent.
  3744. // So we must dig up the underlying CXXRecordDecl.
  3745. ASTImporterLookupTable LT(*ToTU);
  3746. auto *FriendD = FirstDeclMatcher<FriendDecl>().match(ToTU, friendDecl());
  3747. const RecordDecl *RD = getRecordDeclOfFriend(FriendD);
  3748. auto *Y = FirstDeclMatcher<CXXRecordDecl>().match(
  3749. ToTU, cxxRecordDecl(hasName("Y")));
  3750. DeclarationName Name = RD->getDeclName();
  3751. auto Res = LT.lookup(ToTU, Name);
  3752. EXPECT_EQ(Res.size(), 1u);
  3753. EXPECT_EQ(*Res.begin(), RD);
  3754. Res = LT.lookup(Y, Name);
  3755. EXPECT_EQ(Res.size(), 0u);
  3756. }
  3757. TEST_P(ASTImporterLookupTableTest,
  3758. LookupFindsFwdFriendClassDeclWithUnelaboratedType) {
  3759. TranslationUnitDecl *ToTU = getToTuDecl(
  3760. R"(
  3761. class F;
  3762. class Y { friend F; };
  3763. )",
  3764. Lang_CXX11);
  3765. // In this case, the CXXRecordDecl is hidden, the FriendDecl is not a parent.
  3766. // So we must dig up the underlying CXXRecordDecl.
  3767. ASTImporterLookupTable LT(*ToTU);
  3768. auto *FriendD = FirstDeclMatcher<FriendDecl>().match(ToTU, friendDecl());
  3769. const RecordDecl *RD = getRecordDeclOfFriend(FriendD);
  3770. auto *Y = FirstDeclMatcher<CXXRecordDecl>().match(ToTU, cxxRecordDecl(hasName("Y")));
  3771. DeclarationName Name = RD->getDeclName();
  3772. auto Res = LT.lookup(ToTU, Name);
  3773. EXPECT_EQ(Res.size(), 1u);
  3774. EXPECT_EQ(*Res.begin(), RD);
  3775. Res = LT.lookup(Y, Name);
  3776. EXPECT_EQ(Res.size(), 0u);
  3777. }
  3778. TEST_P(ASTImporterLookupTableTest,
  3779. LookupFindsFriendClassDeclWithTypeAliasDoesNotAssert) {
  3780. TranslationUnitDecl *ToTU = getToTuDecl(
  3781. R"(
  3782. class F;
  3783. using alias_of_f = F;
  3784. class Y { friend alias_of_f; };
  3785. )",
  3786. Lang_CXX11);
  3787. // ASTImporterLookupTable constructor handles using declarations correctly,
  3788. // no assert is expected.
  3789. ASTImporterLookupTable LT(*ToTU);
  3790. auto *Alias = FirstDeclMatcher<TypeAliasDecl>().match(
  3791. ToTU, typeAliasDecl(hasName("alias_of_f")));
  3792. DeclarationName Name = Alias->getDeclName();
  3793. auto Res = LT.lookup(ToTU, Name);
  3794. EXPECT_EQ(Res.count(Alias), 1u);
  3795. }
  3796. TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendClassTemplateDecl) {
  3797. TranslationUnitDecl *ToTU = getToTuDecl(
  3798. R"(
  3799. class Y { template <class T> friend class F; };
  3800. )",
  3801. Lang_CXX);
  3802. ASTImporterLookupTable LT(*ToTU);
  3803. auto *F = FirstDeclMatcher<ClassTemplateDecl>().match(
  3804. ToTU, classTemplateDecl(hasName("F")));
  3805. DeclarationName Name = F->getDeclName();
  3806. auto Res = LT.lookup(ToTU, Name);
  3807. EXPECT_EQ(Res.size(), 2u);
  3808. EXPECT_EQ(Res.count(F), 1u);
  3809. EXPECT_EQ(Res.count(F->getTemplatedDecl()), 1u);
  3810. }
  3811. TEST_P(ASTImporterLookupTableTest, DependentFriendClass) {
  3812. TranslationUnitDecl *ToTU = getToTuDecl(
  3813. R"(
  3814. template <typename T>
  3815. class F;
  3816. template <typename T>
  3817. class Y {
  3818. friend class F<T>;
  3819. };
  3820. )",
  3821. Lang_CXX);
  3822. ASTImporterLookupTable LT(*ToTU);
  3823. auto *F = FirstDeclMatcher<ClassTemplateDecl>().match(
  3824. ToTU, classTemplateDecl(hasName("F")));
  3825. DeclarationName Name = F->getDeclName();
  3826. auto Res = LT.lookup(ToTU, Name);
  3827. EXPECT_EQ(Res.size(), 2u);
  3828. EXPECT_EQ(Res.count(F), 1u);
  3829. EXPECT_EQ(Res.count(F->getTemplatedDecl()), 1u);
  3830. }
  3831. TEST_P(ASTImporterLookupTableTest, FriendClassTemplateSpecialization) {
  3832. TranslationUnitDecl *ToTU = getToTuDecl(
  3833. R"(
  3834. template <typename T>
  3835. class F;
  3836. class Y {
  3837. friend class F<int>;
  3838. };
  3839. )",
  3840. Lang_CXX);
  3841. ASTImporterLookupTable LT(*ToTU);
  3842. auto *F = FirstDeclMatcher<ClassTemplateDecl>().match(
  3843. ToTU, classTemplateDecl(hasName("F")));
  3844. DeclarationName Name = F->getDeclName();
  3845. auto Res = LT.lookup(ToTU, Name);
  3846. ASSERT_EQ(Res.size(), 3u);
  3847. EXPECT_EQ(Res.count(F), 1u);
  3848. EXPECT_EQ(Res.count(F->getTemplatedDecl()), 1u);
  3849. EXPECT_EQ(Res.count(*F->spec_begin()), 1u);
  3850. }
  3851. TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendFunctionDecl) {
  3852. TranslationUnitDecl *ToTU = getToTuDecl(
  3853. R"(
  3854. class Y { friend void F(); };
  3855. )",
  3856. Lang_CXX);
  3857. ASTImporterLookupTable LT(*ToTU);
  3858. auto *F =
  3859. FirstDeclMatcher<FunctionDecl>().match(ToTU, functionDecl(hasName("F")));
  3860. DeclarationName Name = F->getDeclName();
  3861. auto Res = LT.lookup(ToTU, Name);
  3862. EXPECT_EQ(Res.size(), 1u);
  3863. EXPECT_EQ(*Res.begin(), F);
  3864. }
  3865. TEST_P(ASTImporterLookupTableTest,
  3866. LookupFindsDeclsInClassTemplateSpecialization) {
  3867. TranslationUnitDecl *ToTU = getToTuDecl(
  3868. R"(
  3869. template <typename T>
  3870. struct X {
  3871. int F;
  3872. };
  3873. void foo() {
  3874. X<char> xc;
  3875. }
  3876. )",
  3877. Lang_CXX);
  3878. ASTImporterLookupTable LT(*ToTU);
  3879. auto *Template = FirstDeclMatcher<ClassTemplateDecl>().match(
  3880. ToTU, classTemplateDecl(hasName("X")));
  3881. auto *FieldInTemplate = FirstDeclMatcher<FieldDecl>().match(
  3882. ToTU,
  3883. fieldDecl(hasParent(cxxRecordDecl(hasParent(classTemplateDecl())))));
  3884. auto *Spec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  3885. ToTU, classTemplateSpecializationDecl(hasName("X")));
  3886. FieldDecl *FieldInSpec = *Spec->field_begin();
  3887. ASSERT_TRUE(FieldInSpec);
  3888. DeclarationName Name = FieldInSpec->getDeclName();
  3889. auto TemplateDC = cast<DeclContext>(Template->getTemplatedDecl());
  3890. SmallVector<NamedDecl *, 2> FoundDecls;
  3891. TemplateDC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
  3892. EXPECT_EQ(FoundDecls.size(), 1u);
  3893. EXPECT_EQ(FoundDecls[0], FieldInTemplate);
  3894. auto Res = LT.lookup(TemplateDC, Name);
  3895. ASSERT_EQ(Res.size(), 1u);
  3896. EXPECT_EQ(*Res.begin(), FieldInTemplate);
  3897. cast<DeclContext>(Spec)->getRedeclContext()->localUncachedLookup(Name,
  3898. FoundDecls);
  3899. EXPECT_EQ(FoundDecls.size(), 1u);
  3900. EXPECT_EQ(FoundDecls[0], FieldInSpec);
  3901. Res = LT.lookup(cast<DeclContext>(Spec), Name);
  3902. ASSERT_EQ(Res.size(), 1u);
  3903. EXPECT_EQ(*Res.begin(), FieldInSpec);
  3904. }
  3905. TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendFunctionTemplateDecl) {
  3906. TranslationUnitDecl *ToTU = getToTuDecl(
  3907. R"(
  3908. class Y { template <class T> friend void F(); };
  3909. )",
  3910. Lang_CXX);
  3911. ASTImporterLookupTable LT(*ToTU);
  3912. auto *F = FirstDeclMatcher<FunctionTemplateDecl>().match(
  3913. ToTU, functionTemplateDecl(hasName("F")));
  3914. DeclarationName Name = F->getDeclName();
  3915. auto Res = LT.lookup(ToTU, Name);
  3916. EXPECT_EQ(Res.size(), 2u);
  3917. EXPECT_EQ(Res.count(F), 1u);
  3918. EXPECT_EQ(Res.count(F->getTemplatedDecl()), 1u);
  3919. }
  3920. TEST_P(ASTImporterLookupTableTest, MultipleBefriendingClasses) {
  3921. TranslationUnitDecl *ToTU = getToTuDecl(
  3922. R"(
  3923. struct X;
  3924. struct A {
  3925. friend struct X;
  3926. };
  3927. struct B {
  3928. friend struct X;
  3929. };
  3930. )",
  3931. Lang_CXX);
  3932. ASTImporterLookupTable LT(*ToTU);
  3933. auto *X = FirstDeclMatcher<CXXRecordDecl>().match(
  3934. ToTU, cxxRecordDecl(hasName("X")));
  3935. auto *FriendD0 = FirstDeclMatcher<FriendDecl>().match(ToTU, friendDecl());
  3936. auto *FriendD1 = LastDeclMatcher<FriendDecl>().match(ToTU, friendDecl());
  3937. const RecordDecl *RD0 = getRecordDeclOfFriend(FriendD0);
  3938. const RecordDecl *RD1 = getRecordDeclOfFriend(FriendD1);
  3939. ASSERT_EQ(RD0, RD1);
  3940. ASSERT_EQ(RD1, X);
  3941. DeclarationName Name = X->getDeclName();
  3942. auto Res = LT.lookup(ToTU, Name);
  3943. EXPECT_EQ(Res.size(), 1u);
  3944. EXPECT_EQ(*Res.begin(), X);
  3945. }
  3946. TEST_P(ASTImporterLookupTableTest, EnumConstantDecl) {
  3947. TranslationUnitDecl *ToTU = getToTuDecl(
  3948. R"(
  3949. enum E {
  3950. A,
  3951. B
  3952. };
  3953. )",
  3954. Lang_C);
  3955. ASTImporterLookupTable LT(*ToTU);
  3956. auto *E = FirstDeclMatcher<EnumDecl>().match(ToTU, enumDecl(hasName("E")));
  3957. auto *A = FirstDeclMatcher<EnumConstantDecl>().match(
  3958. ToTU, enumConstantDecl(hasName("A")));
  3959. DeclarationName Name = A->getDeclName();
  3960. // Redecl context is the TU.
  3961. ASSERT_EQ(E->getRedeclContext(), ToTU);
  3962. SmallVector<NamedDecl *, 2> FoundDecls;
  3963. // Normal lookup finds in the DC.
  3964. E->localUncachedLookup(Name, FoundDecls);
  3965. EXPECT_EQ(FoundDecls.size(), 1u);
  3966. // Normal lookup finds in the Redecl context.
  3967. ToTU->localUncachedLookup(Name, FoundDecls);
  3968. EXPECT_EQ(FoundDecls.size(), 1u);
  3969. // Import specific lookup finds in the DC.
  3970. auto Res = LT.lookup(E, Name);
  3971. ASSERT_EQ(Res.size(), 1u);
  3972. EXPECT_EQ(*Res.begin(), A);
  3973. // Import specific lookup finds in the Redecl context.
  3974. Res = LT.lookup(ToTU, Name);
  3975. ASSERT_EQ(Res.size(), 1u);
  3976. EXPECT_EQ(*Res.begin(), A);
  3977. }
  3978. TEST_P(ASTImporterLookupTableTest, LookupSearchesInTheWholeRedeclChain) {
  3979. TranslationUnitDecl *ToTU = getToTuDecl(
  3980. R"(
  3981. namespace N {
  3982. int A;
  3983. }
  3984. namespace N {
  3985. }
  3986. )",
  3987. Lang_CXX);
  3988. auto *N1 =
  3989. LastDeclMatcher<NamespaceDecl>().match(ToTU, namespaceDecl(hasName("N")));
  3990. auto *A = FirstDeclMatcher<VarDecl>().match(ToTU, varDecl(hasName("A")));
  3991. DeclarationName Name = A->getDeclName();
  3992. ASTImporterLookupTable LT(*ToTU);
  3993. auto Res = LT.lookup(N1, Name);
  3994. ASSERT_EQ(Res.size(), 1u);
  3995. EXPECT_EQ(*Res.begin(), A);
  3996. }
  3997. INSTANTIATE_TEST_CASE_P(ParameterizedTests, DeclContextTest,
  3998. ::testing::Values(ArgVector()), );
  3999. INSTANTIATE_TEST_CASE_P(
  4000. ParameterizedTests, CanonicalRedeclChain,
  4001. ::testing::Values(ArgVector()),);
  4002. // FIXME This test is disabled currently, upcoming patches will make it
  4003. // possible to enable.
  4004. TEST_P(ASTImporterOptionSpecificTestBase,
  4005. DISABLED_RedeclChainShouldBeCorrectAmongstNamespaces) {
  4006. Decl *FromTU = getTuDecl(
  4007. R"(
  4008. namespace NS {
  4009. struct X;
  4010. struct Y {
  4011. static const int I = 3;
  4012. };
  4013. }
  4014. namespace NS {
  4015. struct X { // <--- To be imported
  4016. void method(int i = Y::I) {}
  4017. int f;
  4018. };
  4019. }
  4020. )",
  4021. Lang_CXX);
  4022. auto *FromFwd = FirstDeclMatcher<CXXRecordDecl>().match(
  4023. FromTU, cxxRecordDecl(hasName("X"), unless(isImplicit())));
  4024. auto *FromDef = LastDeclMatcher<CXXRecordDecl>().match(
  4025. FromTU,
  4026. cxxRecordDecl(hasName("X"), isDefinition(), unless(isImplicit())));
  4027. ASSERT_NE(FromFwd, FromDef);
  4028. ASSERT_FALSE(FromFwd->isThisDeclarationADefinition());
  4029. ASSERT_TRUE(FromDef->isThisDeclarationADefinition());
  4030. ASSERT_EQ(FromFwd->getCanonicalDecl(), FromDef->getCanonicalDecl());
  4031. auto *ToDef = cast_or_null<CXXRecordDecl>(Import(FromDef, Lang_CXX));
  4032. auto *ToFwd = cast_or_null<CXXRecordDecl>(Import(FromFwd, Lang_CXX));
  4033. EXPECT_NE(ToFwd, ToDef);
  4034. EXPECT_FALSE(ToFwd->isThisDeclarationADefinition());
  4035. EXPECT_TRUE(ToDef->isThisDeclarationADefinition());
  4036. EXPECT_EQ(ToFwd->getCanonicalDecl(), ToDef->getCanonicalDecl());
  4037. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  4038. // We expect no (ODR) warning during the import.
  4039. EXPECT_EQ(0u, ToTU->getASTContext().getDiagnostics().getNumWarnings());
  4040. }
  4041. struct ImportFriendFunctionTemplates : ASTImporterOptionSpecificTestBase {};
  4042. TEST_P(ImportFriendFunctionTemplates, LookupShouldFindPreviousFriend) {
  4043. Decl *ToTU = getToTuDecl(
  4044. R"(
  4045. class X {
  4046. template <typename T> friend void foo();
  4047. };
  4048. )",
  4049. Lang_CXX);
  4050. auto *Friend = FirstDeclMatcher<FunctionTemplateDecl>().match(
  4051. ToTU, functionTemplateDecl(hasName("foo")));
  4052. Decl *FromTU = getTuDecl(
  4053. R"(
  4054. template <typename T> void foo();
  4055. )",
  4056. Lang_CXX);
  4057. auto *FromFoo = FirstDeclMatcher<FunctionTemplateDecl>().match(
  4058. FromTU, functionTemplateDecl(hasName("foo")));
  4059. auto *Imported = Import(FromFoo, Lang_CXX);
  4060. EXPECT_EQ(Imported->getPreviousDecl(), Friend);
  4061. }
  4062. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterLookupTableTest,
  4063. DefaultTestValuesForRunOptions, );
  4064. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportExpr,
  4065. DefaultTestValuesForRunOptions, );
  4066. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportType,
  4067. DefaultTestValuesForRunOptions, );
  4068. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportDecl,
  4069. DefaultTestValuesForRunOptions, );
  4070. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterOptionSpecificTestBase,
  4071. DefaultTestValuesForRunOptions, );
  4072. INSTANTIATE_TEST_CASE_P(ParameterizedTests, RedirectingImporterTest,
  4073. DefaultTestValuesForRunOptions, );
  4074. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportFunctions,
  4075. DefaultTestValuesForRunOptions, );
  4076. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportFriendFunctionTemplates,
  4077. DefaultTestValuesForRunOptions, );
  4078. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportClasses,
  4079. DefaultTestValuesForRunOptions, );
  4080. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportFriendFunctions,
  4081. DefaultTestValuesForRunOptions, );
  4082. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportFriendClasses,
  4083. DefaultTestValuesForRunOptions, );
  4084. INSTANTIATE_TEST_CASE_P(ParameterizedTests,
  4085. ImportFunctionTemplateSpecializations,
  4086. DefaultTestValuesForRunOptions, );
  4087. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportImplicitMethods,
  4088. DefaultTestValuesForRunOptions, );
  4089. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportVariables,
  4090. DefaultTestValuesForRunOptions, );
  4091. } // end namespace ast_matchers
  4092. } // end namespace clang