ASTImporterTest.cpp 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792
  1. //===- unittest/AST/ASTImporterTest.cpp - AST node import test ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Tests for the correct import of AST nodes from one AST context to another.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "MatchVerifier.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/ASTImporter.h"
  16. #include "clang/ASTMatchers/ASTMatchFinder.h"
  17. #include "clang/ASTMatchers/ASTMatchers.h"
  18. #include "clang/Tooling/Tooling.h"
  19. #include "DeclMatcher.h"
  20. #include "Language.h"
  21. #include "gmock/gmock.h"
  22. #include "llvm/ADT/StringMap.h"
  23. namespace clang {
  24. namespace ast_matchers {
  25. using internal::Matcher;
  26. using internal::BindableMatcher;
  27. using llvm::StringMap;
  28. // Creates a virtual file and assigns that to the context of given AST. If the
  29. // file already exists then the file will not be created again as a duplicate.
  30. static void
  31. createVirtualFileIfNeeded(ASTUnit *ToAST, StringRef FileName,
  32. std::unique_ptr<llvm::MemoryBuffer> &&Buffer) {
  33. assert(ToAST);
  34. ASTContext &ToCtx = ToAST->getASTContext();
  35. auto *OFS = static_cast<vfs::OverlayFileSystem *>(
  36. ToCtx.getSourceManager().getFileManager().getVirtualFileSystem().get());
  37. auto *MFS =
  38. static_cast<vfs::InMemoryFileSystem *>(OFS->overlays_begin()->get());
  39. MFS->addFile(FileName, 0, std::move(Buffer));
  40. }
  41. static void createVirtualFileIfNeeded(ASTUnit *ToAST, StringRef FileName,
  42. StringRef Code) {
  43. return createVirtualFileIfNeeded(ToAST, FileName,
  44. llvm::MemoryBuffer::getMemBuffer(Code));
  45. }
  46. const StringRef DeclToImportID = "declToImport";
  47. const StringRef DeclToVerifyID = "declToVerify";
  48. // Common base for the different families of ASTImporter tests that are
  49. // parameterized on the compiler options which may result a different AST. E.g.
  50. // -fms-compatibility or -fdelayed-template-parsing.
  51. struct ParameterizedTestsFixture : ::testing::TestWithParam<ArgVector> {
  52. // Returns the argument vector used for a specific language option, this set
  53. // can be tweaked by the test parameters.
  54. ArgVector getArgVectorForLanguage(Language Lang) const {
  55. ArgVector Args = getBasicRunOptionsForLanguage(Lang);
  56. ArgVector ExtraArgs = GetParam();
  57. for (const auto &Arg : ExtraArgs) {
  58. Args.push_back(Arg);
  59. }
  60. return Args;
  61. }
  62. };
  63. // Base class for those tests which use the family of `testImport` functions.
  64. class TestImportBase : public ParameterizedTestsFixture {
  65. template <typename NodeType>
  66. NodeType importNode(ASTUnit *From, ASTUnit *To, ASTImporter &Importer,
  67. NodeType Node) {
  68. ASTContext &ToCtx = To->getASTContext();
  69. // Add 'From' file to virtual file system so importer can 'find' it
  70. // while importing SourceLocations. It is safe to add same file multiple
  71. // times - it just isn't replaced.
  72. StringRef FromFileName = From->getMainFileName();
  73. createVirtualFileIfNeeded(To, FromFileName,
  74. From->getBufferForFile(FromFileName));
  75. auto Imported = Importer.Import(Node);
  76. // This should dump source locations and assert if some source locations
  77. // were not imported.
  78. SmallString<1024> ImportChecker;
  79. llvm::raw_svector_ostream ToNothing(ImportChecker);
  80. ToCtx.getTranslationUnitDecl()->print(ToNothing);
  81. // This traverses the AST to catch certain bugs like poorly or not
  82. // implemented subtrees.
  83. Imported->dump(ToNothing);
  84. return Imported;
  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> &SearchMatcher,
  92. const BindableMatcher<NodeType> &VerificationMatcher) {
  93. const char *const InputFileName = "input.cc";
  94. const char *const OutputFileName = "output.cc";
  95. std::unique_ptr<ASTUnit> FromAST = tooling::buildASTFromCodeWithArgs(
  96. FromCode, FromArgs, InputFileName),
  97. ToAST = tooling::buildASTFromCodeWithArgs(
  98. ToCode, ToArgs, OutputFileName);
  99. ASTContext &FromCtx = FromAST->getASTContext(),
  100. &ToCtx = ToAST->getASTContext();
  101. ASTImporter Importer(ToCtx, ToAST->getFileManager(), FromCtx,
  102. FromAST->getFileManager(), false);
  103. auto FoundNodes = match(SearchMatcher, FromCtx);
  104. if (FoundNodes.size() != 1)
  105. return testing::AssertionFailure()
  106. << "Multiple potential nodes were found!";
  107. auto ToImport = selectFirst<NodeType>(DeclToImportID, FoundNodes);
  108. if (!ToImport)
  109. return testing::AssertionFailure() << "Node type mismatch!";
  110. // Sanity check: the node being imported should match in the same way as
  111. // the result node.
  112. BindableMatcher<NodeType> WrapperMatcher(VerificationMatcher);
  113. EXPECT_TRUE(Verifier.match(ToImport, WrapperMatcher));
  114. auto Imported = importNode(FromAST.get(), ToAST.get(), Importer, ToImport);
  115. if (!Imported)
  116. return testing::AssertionFailure() << "Import failed, nullptr returned!";
  117. return Verifier.match(Imported, WrapperMatcher);
  118. }
  119. template <typename NodeType>
  120. testing::AssertionResult
  121. testImport(const std::string &FromCode, const ArgVector &FromArgs,
  122. const std::string &ToCode, const ArgVector &ToArgs,
  123. MatchVerifier<NodeType> &Verifier,
  124. const BindableMatcher<NodeType> &VerificationMatcher) {
  125. return testImport(
  126. FromCode, FromArgs, ToCode, ToArgs, Verifier,
  127. translationUnitDecl(
  128. has(namedDecl(hasName(DeclToImportID)).bind(DeclToImportID))),
  129. VerificationMatcher);
  130. }
  131. public:
  132. /// Test how AST node named "declToImport" located in the translation unit
  133. /// of "FromCode" virtual file is imported to "ToCode" virtual file.
  134. /// The verification is done by running AMatcher over the imported node.
  135. template <typename NodeType, typename MatcherType>
  136. void testImport(const std::string &FromCode, Language FromLang,
  137. const std::string &ToCode, Language ToLang,
  138. MatchVerifier<NodeType> &Verifier,
  139. const MatcherType &AMatcher) {
  140. ArgVector FromArgs = getArgVectorForLanguage(FromLang),
  141. ToArgs = getArgVectorForLanguage(ToLang);
  142. EXPECT_TRUE(
  143. testImport(FromCode, FromArgs, ToCode, ToArgs, Verifier, AMatcher));
  144. }
  145. struct ImportAction {
  146. StringRef FromFilename;
  147. StringRef ToFilename;
  148. // FIXME: Generalize this to support other node kinds.
  149. BindableMatcher<Decl> ImportPredicate;
  150. ImportAction(StringRef FromFilename, StringRef ToFilename,
  151. DeclarationMatcher ImportPredicate)
  152. : FromFilename(FromFilename), ToFilename(ToFilename),
  153. ImportPredicate(ImportPredicate) {}
  154. ImportAction(StringRef FromFilename, StringRef ToFilename,
  155. const std::string &DeclName)
  156. : FromFilename(FromFilename), ToFilename(ToFilename),
  157. ImportPredicate(namedDecl(hasName(DeclName))) {}
  158. };
  159. using SingleASTUnit = std::unique_ptr<ASTUnit>;
  160. using AllASTUnits = StringMap<SingleASTUnit>;
  161. struct CodeEntry {
  162. std::string CodeSample;
  163. Language Lang;
  164. };
  165. using CodeFiles = StringMap<CodeEntry>;
  166. /// Builds an ASTUnit for one potential compile options set.
  167. SingleASTUnit createASTUnit(StringRef FileName, const CodeEntry &CE) const {
  168. ArgVector Args = getArgVectorForLanguage(CE.Lang);
  169. auto AST = tooling::buildASTFromCodeWithArgs(CE.CodeSample, Args, FileName);
  170. EXPECT_TRUE(AST.get());
  171. return AST;
  172. }
  173. /// Test an arbitrary sequence of imports for a set of given in-memory files.
  174. /// The verification is done by running VerificationMatcher against a
  175. /// specified AST node inside of one of given files.
  176. /// \param CodeSamples Map whose key is the file name and the value is the
  177. /// file content.
  178. /// \param ImportActions Sequence of imports. Each import in sequence
  179. /// specifies "from file" and "to file" and a matcher that is used for
  180. /// searching a declaration for import in "from file".
  181. /// \param FileForFinalCheck Name of virtual file for which the final check is
  182. /// applied.
  183. /// \param FinalSelectPredicate Matcher that specifies the AST node in the
  184. /// FileForFinalCheck for which the verification will be done.
  185. /// \param VerificationMatcher Matcher that will be used for verification
  186. /// after all imports in sequence are done.
  187. void testImportSequence(const CodeFiles &CodeSamples,
  188. const std::vector<ImportAction> &ImportActions,
  189. StringRef FileForFinalCheck,
  190. BindableMatcher<Decl> FinalSelectPredicate,
  191. BindableMatcher<Decl> VerificationMatcher) {
  192. AllASTUnits AllASTs;
  193. using ImporterKey = std::pair<const ASTUnit *, const ASTUnit *>;
  194. llvm::DenseMap<ImporterKey, std::unique_ptr<ASTImporter>> Importers;
  195. auto GenASTsIfNeeded = [this, &AllASTs, &CodeSamples](StringRef Filename) {
  196. if (!AllASTs.count(Filename)) {
  197. auto Found = CodeSamples.find(Filename);
  198. assert(Found != CodeSamples.end() && "Wrong file for import!");
  199. AllASTs[Filename] = createASTUnit(Filename, Found->getValue());
  200. }
  201. };
  202. for (const ImportAction &Action : ImportActions) {
  203. StringRef FromFile = Action.FromFilename, ToFile = Action.ToFilename;
  204. GenASTsIfNeeded(FromFile);
  205. GenASTsIfNeeded(ToFile);
  206. ASTUnit *From = AllASTs[FromFile].get();
  207. ASTUnit *To = AllASTs[ToFile].get();
  208. // Create a new importer if needed.
  209. std::unique_ptr<ASTImporter> &ImporterRef = Importers[{From, To}];
  210. if (!ImporterRef)
  211. ImporterRef.reset(new ASTImporter(
  212. To->getASTContext(), To->getFileManager(), From->getASTContext(),
  213. From->getFileManager(), false));
  214. // Find the declaration and import it.
  215. auto FoundDecl = match(Action.ImportPredicate.bind(DeclToImportID),
  216. From->getASTContext());
  217. EXPECT_TRUE(FoundDecl.size() == 1);
  218. const Decl *ToImport = selectFirst<Decl>(DeclToImportID, FoundDecl);
  219. auto Imported = importNode(From, To, *ImporterRef, ToImport);
  220. EXPECT_TRUE(Imported);
  221. }
  222. // Find the declaration and import it.
  223. auto FoundDecl = match(FinalSelectPredicate.bind(DeclToVerifyID),
  224. AllASTs[FileForFinalCheck]->getASTContext());
  225. EXPECT_TRUE(FoundDecl.size() == 1);
  226. const Decl *ToVerify = selectFirst<Decl>(DeclToVerifyID, FoundDecl);
  227. MatchVerifier<Decl> Verifier;
  228. EXPECT_TRUE(
  229. Verifier.match(ToVerify, BindableMatcher<Decl>(VerificationMatcher)));
  230. }
  231. };
  232. // This class provides generic methods to write tests which can check internal
  233. // attributes of AST nodes like getPreviousDecl(), isVirtual(), etc. Also,
  234. // this fixture makes it possible to import from several "From" contexts.
  235. class ASTImporterTestBase : public ParameterizedTestsFixture {
  236. const char *const InputFileName = "input.cc";
  237. const char *const OutputFileName = "output.cc";
  238. // Buffer for the To context, must live in the test scope.
  239. std::string ToCode;
  240. // Represents a "From" translation unit and holds an importer object which we
  241. // use to import from this translation unit.
  242. struct TU {
  243. // Buffer for the context, must live in the test scope.
  244. std::string Code;
  245. std::string FileName;
  246. std::unique_ptr<ASTUnit> Unit;
  247. TranslationUnitDecl *TUDecl = nullptr;
  248. std::unique_ptr<ASTImporter> Importer;
  249. TU(StringRef Code, StringRef FileName, ArgVector Args)
  250. : Code(Code), FileName(FileName),
  251. Unit(tooling::buildASTFromCodeWithArgs(this->Code, Args,
  252. this->FileName)),
  253. TUDecl(Unit->getASTContext().getTranslationUnitDecl()) {
  254. Unit->enableSourceFileDiagnostics();
  255. }
  256. Decl *import(ASTUnit *ToAST, Decl *FromDecl) {
  257. assert(ToAST);
  258. if (!Importer) {
  259. Importer.reset(new ASTImporter(
  260. ToAST->getASTContext(), ToAST->getFileManager(),
  261. Unit->getASTContext(), Unit->getFileManager(), false));
  262. }
  263. return Importer->Import(FromDecl);
  264. }
  265. };
  266. // We may have several From contexts and related translation units. In each
  267. // AST, the buffers for the source are handled via references and are set
  268. // during the creation of the AST. These references must point to a valid
  269. // buffer until the AST is alive. Thus, we must use a list in order to avoid
  270. // moving of the stored objects because that would mean breaking the
  271. // references in the AST. By using a vector a move could happen when the
  272. // vector is expanding, with the list we won't have these issues.
  273. std::list<TU> FromTUs;
  274. public:
  275. // We may have several From context but only one To context.
  276. std::unique_ptr<ASTUnit> ToAST;
  277. // Creates an AST both for the From and To source code and imports the Decl
  278. // of the identifier into the To context.
  279. // Must not be called more than once within the same test.
  280. std::tuple<Decl *, Decl *>
  281. getImportedDecl(StringRef FromSrcCode, Language FromLang, StringRef ToSrcCode,
  282. Language ToLang, StringRef Identifier = DeclToImportID) {
  283. ArgVector FromArgs = getArgVectorForLanguage(FromLang),
  284. ToArgs = getArgVectorForLanguage(ToLang);
  285. FromTUs.emplace_back(FromSrcCode, InputFileName, FromArgs);
  286. TU &FromTU = FromTUs.back();
  287. ToCode = ToSrcCode;
  288. assert(!ToAST);
  289. ToAST = tooling::buildASTFromCodeWithArgs(ToCode, ToArgs, OutputFileName);
  290. ToAST->enableSourceFileDiagnostics();
  291. ASTContext &FromCtx = FromTU.Unit->getASTContext();
  292. createVirtualFileIfNeeded(ToAST.get(), InputFileName, FromTU.Code);
  293. IdentifierInfo *ImportedII = &FromCtx.Idents.get(Identifier);
  294. assert(ImportedII && "Declaration with the given identifier "
  295. "should be specified in test!");
  296. DeclarationName ImportDeclName(ImportedII);
  297. SmallVector<NamedDecl *, 4> FoundDecls;
  298. FromCtx.getTranslationUnitDecl()->localUncachedLookup(ImportDeclName,
  299. FoundDecls);
  300. assert(FoundDecls.size() == 1);
  301. Decl *Imported = FromTU.import(ToAST.get(), FoundDecls.front());
  302. assert(Imported);
  303. return std::make_tuple(*FoundDecls.begin(), Imported);
  304. }
  305. // Creates a TU decl for the given source code which can be used as a From
  306. // context. May be called several times in a given test (with different file
  307. // name).
  308. TranslationUnitDecl *getTuDecl(StringRef SrcCode, Language Lang,
  309. StringRef FileName = "input.cc") {
  310. assert(
  311. std::find_if(FromTUs.begin(), FromTUs.end(), [FileName](const TU &E) {
  312. return E.FileName == FileName;
  313. }) == FromTUs.end());
  314. ArgVector Args = getArgVectorForLanguage(Lang);
  315. FromTUs.emplace_back(SrcCode, FileName, Args);
  316. TU &Tu = FromTUs.back();
  317. return Tu.TUDecl;
  318. }
  319. // Creates the To context with the given source code and returns the TU decl.
  320. TranslationUnitDecl *getToTuDecl(StringRef ToSrcCode, Language ToLang) {
  321. ArgVector ToArgs = getArgVectorForLanguage(ToLang);
  322. ToCode = ToSrcCode;
  323. assert(!ToAST);
  324. ToAST = tooling::buildASTFromCodeWithArgs(ToCode, ToArgs, OutputFileName);
  325. ToAST->enableSourceFileDiagnostics();
  326. return ToAST->getASTContext().getTranslationUnitDecl();
  327. }
  328. // Import the given Decl into the ToCtx.
  329. // May be called several times in a given test.
  330. // The different instances of the param From may have different ASTContext.
  331. Decl *Import(Decl *From, Language ToLang) {
  332. if (!ToAST) {
  333. ArgVector ToArgs = getArgVectorForLanguage(ToLang);
  334. // Build the AST from an empty file.
  335. ToAST =
  336. tooling::buildASTFromCodeWithArgs(/*Code=*/"", ToArgs, "empty.cc");
  337. ToAST->enableSourceFileDiagnostics();
  338. }
  339. // Create a virtual file in the To Ctx which corresponds to the file from
  340. // which we want to import the `From` Decl. Without this source locations
  341. // will be invalid in the ToCtx.
  342. auto It = std::find_if(FromTUs.begin(), FromTUs.end(), [From](const TU &E) {
  343. return E.TUDecl == From->getTranslationUnitDecl();
  344. });
  345. assert(It != FromTUs.end());
  346. createVirtualFileIfNeeded(ToAST.get(), It->FileName, It->Code);
  347. return It->import(ToAST.get(), From);
  348. }
  349. ~ASTImporterTestBase() {
  350. if (!::testing::Test::HasFailure()) return;
  351. for (auto &Tu : FromTUs) {
  352. assert(Tu.Unit);
  353. llvm::errs() << "FromAST:\n";
  354. Tu.Unit->getASTContext().getTranslationUnitDecl()->dump();
  355. llvm::errs() << "\n";
  356. }
  357. if (ToAST) {
  358. llvm::errs() << "ToAST:\n";
  359. ToAST->getASTContext().getTranslationUnitDecl()->dump();
  360. }
  361. }
  362. };
  363. struct ImportExpr : TestImportBase {};
  364. struct ImportType : TestImportBase {};
  365. struct ImportDecl : TestImportBase {};
  366. struct CanonicalRedeclChain : ASTImporterTestBase {};
  367. TEST_P(CanonicalRedeclChain, ShouldBeConsequentWithMatchers) {
  368. Decl *FromTU = getTuDecl("void f();", Lang_CXX);
  369. auto Pattern = functionDecl(hasName("f"));
  370. auto *D0 = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  371. auto Redecls = getCanonicalForwardRedeclChain(D0);
  372. ASSERT_EQ(Redecls.size(), 1u);
  373. EXPECT_EQ(D0, Redecls[0]);
  374. }
  375. TEST_P(CanonicalRedeclChain, ShouldBeConsequentWithMatchers2) {
  376. Decl *FromTU = getTuDecl("void f(); void f(); void f();", Lang_CXX);
  377. auto Pattern = functionDecl(hasName("f"));
  378. auto *D0 = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  379. auto *D2 = LastDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  380. FunctionDecl *D1 = D2->getPreviousDecl();
  381. auto Redecls = getCanonicalForwardRedeclChain(D0);
  382. ASSERT_EQ(Redecls.size(), 3u);
  383. EXPECT_EQ(D0, Redecls[0]);
  384. EXPECT_EQ(D1, Redecls[1]);
  385. EXPECT_EQ(D2, Redecls[2]);
  386. }
  387. TEST_P(CanonicalRedeclChain, ShouldBeSameForAllDeclInTheChain) {
  388. Decl *FromTU = getTuDecl("void f(); void f(); void f();", Lang_CXX);
  389. auto Pattern = functionDecl(hasName("f"));
  390. auto *D0 = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  391. auto *D2 = LastDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  392. FunctionDecl *D1 = D2->getPreviousDecl();
  393. auto RedeclsD0 = getCanonicalForwardRedeclChain(D0);
  394. auto RedeclsD1 = getCanonicalForwardRedeclChain(D1);
  395. auto RedeclsD2 = getCanonicalForwardRedeclChain(D2);
  396. EXPECT_THAT(RedeclsD0, ::testing::ContainerEq(RedeclsD1));
  397. EXPECT_THAT(RedeclsD1, ::testing::ContainerEq(RedeclsD2));
  398. }
  399. TEST_P(ImportExpr, ImportStringLiteral) {
  400. MatchVerifier<Decl> Verifier;
  401. testImport(
  402. "void declToImport() { (void)\"foo\"; }",
  403. Lang_CXX, "", Lang_CXX, Verifier,
  404. functionDecl(hasDescendant(
  405. stringLiteral(hasType(asString("const char [4]"))))));
  406. testImport(
  407. "void declToImport() { (void)L\"foo\"; }",
  408. Lang_CXX, "", Lang_CXX, Verifier,
  409. functionDecl(hasDescendant(
  410. stringLiteral(hasType(asString("const wchar_t [4]"))))));
  411. testImport(
  412. "void declToImport() { (void) \"foo\" \"bar\"; }",
  413. Lang_CXX, "", Lang_CXX, Verifier,
  414. functionDecl(hasDescendant(
  415. stringLiteral(hasType(asString("const char [7]"))))));
  416. }
  417. TEST_P(ImportExpr, ImportGNUNullExpr) {
  418. MatchVerifier<Decl> Verifier;
  419. testImport(
  420. "void declToImport() { (void)__null; }",
  421. Lang_CXX, "", Lang_CXX, Verifier,
  422. functionDecl(hasDescendant(gnuNullExpr(hasType(isInteger())))));
  423. }
  424. TEST_P(ImportExpr, ImportCXXNullPtrLiteralExpr) {
  425. MatchVerifier<Decl> Verifier;
  426. testImport(
  427. "void declToImport() { (void)nullptr; }",
  428. Lang_CXX11, "", Lang_CXX11, Verifier,
  429. functionDecl(hasDescendant(cxxNullPtrLiteralExpr())));
  430. }
  431. TEST_P(ImportExpr, ImportFloatinglLiteralExpr) {
  432. MatchVerifier<Decl> Verifier;
  433. testImport(
  434. "void declToImport() { (void)1.0; }",
  435. Lang_C, "", Lang_C, Verifier,
  436. functionDecl(hasDescendant(
  437. floatLiteral(equals(1.0), hasType(asString("double"))))));
  438. testImport(
  439. "void declToImport() { (void)1.0e-5f; }",
  440. Lang_C, "", Lang_C, Verifier,
  441. functionDecl(hasDescendant(
  442. floatLiteral(equals(1.0e-5f), hasType(asString("float"))))));
  443. }
  444. TEST_P(ImportExpr, ImportCompoundLiteralExpr) {
  445. MatchVerifier<Decl> Verifier;
  446. testImport(
  447. "void declToImport() {"
  448. " struct s { int x; long y; unsigned z; }; "
  449. " (void)(struct s){ 42, 0L, 1U }; }",
  450. Lang_CXX, "", Lang_CXX, Verifier,
  451. functionDecl(hasDescendant(
  452. compoundLiteralExpr(
  453. hasType(asString("struct s")),
  454. has(initListExpr(
  455. hasType(asString("struct s")),
  456. has(integerLiteral(
  457. equals(42), hasType(asString("int")))),
  458. has(integerLiteral(
  459. equals(0), hasType(asString("long")))),
  460. has(integerLiteral(
  461. equals(1), hasType(asString("unsigned int"))))))))));
  462. }
  463. TEST_P(ImportExpr, ImportCXXThisExpr) {
  464. MatchVerifier<Decl> Verifier;
  465. testImport(
  466. "class declToImport { void f() { (void)this; } };",
  467. Lang_CXX, "", Lang_CXX, Verifier,
  468. cxxRecordDecl(
  469. hasMethod(
  470. hasDescendant(
  471. cxxThisExpr(
  472. hasType(
  473. asString("class declToImport *")))))));
  474. }
  475. TEST_P(ImportExpr, ImportAtomicExpr) {
  476. MatchVerifier<Decl> Verifier;
  477. testImport(
  478. "void declToImport() { int *ptr; __atomic_load_n(ptr, 1); }",
  479. Lang_C, "", Lang_C, Verifier,
  480. functionDecl(hasDescendant(
  481. atomicExpr(
  482. has(ignoringParenImpCasts(
  483. declRefExpr(hasDeclaration(varDecl(hasName("ptr"))),
  484. hasType(asString("int *"))))),
  485. has(integerLiteral(equals(1), hasType(asString("int"))))))));
  486. }
  487. TEST_P(ImportExpr, ImportLabelDeclAndAddrLabelExpr) {
  488. MatchVerifier<Decl> Verifier;
  489. testImport(
  490. "void declToImport() { loop: goto loop; (void)&&loop; }",
  491. Lang_C, "", Lang_C, Verifier,
  492. functionDecl(
  493. hasDescendant(
  494. labelStmt(hasDeclaration(labelDecl(hasName("loop"))))),
  495. hasDescendant(
  496. addrLabelExpr(hasDeclaration(labelDecl(hasName("loop")))))));
  497. }
  498. AST_MATCHER_P(TemplateDecl, hasTemplateDecl,
  499. internal::Matcher<NamedDecl>, InnerMatcher) {
  500. const NamedDecl *Template = Node.getTemplatedDecl();
  501. return Template && InnerMatcher.matches(*Template, Finder, Builder);
  502. }
  503. TEST_P(ImportExpr, ImportParenListExpr) {
  504. MatchVerifier<Decl> Verifier;
  505. testImport(
  506. "template<typename T> class dummy { void f() { dummy X(*this); } };"
  507. "typedef dummy<int> declToImport;"
  508. "template class dummy<int>;",
  509. Lang_CXX, "", Lang_CXX, Verifier,
  510. typedefDecl(hasType(templateSpecializationType(
  511. hasDeclaration(classTemplateSpecializationDecl(hasSpecializedTemplate(
  512. classTemplateDecl(hasTemplateDecl(cxxRecordDecl(hasMethod(allOf(
  513. hasName("f"),
  514. hasBody(compoundStmt(has(declStmt(hasSingleDecl(
  515. varDecl(hasInitializer(parenListExpr(has(unaryOperator(
  516. hasOperatorName("*"),
  517. hasUnaryOperand(cxxThisExpr())))))))))))))))))))))));
  518. }
  519. TEST_P(ImportExpr, ImportSwitch) {
  520. MatchVerifier<Decl> Verifier;
  521. testImport(
  522. "void declToImport() { int b; switch (b) { case 1: break; } }",
  523. Lang_C, "", Lang_C, Verifier,
  524. functionDecl(hasDescendant(
  525. switchStmt(has(compoundStmt(has(caseStmt())))))));
  526. }
  527. TEST_P(ImportExpr, ImportStmtExpr) {
  528. MatchVerifier<Decl> Verifier;
  529. // NOTE: has() ignores implicit casts, using hasDescendant() to match it
  530. testImport(
  531. "void declToImport() { int b; int a = b ?: 1; int C = ({int X=4; X;}); }",
  532. Lang_C, "", Lang_C, Verifier,
  533. functionDecl(hasDescendant(
  534. varDecl(
  535. hasName("C"),
  536. hasType(asString("int")),
  537. hasInitializer(
  538. stmtExpr(
  539. hasAnySubstatement(declStmt(hasSingleDecl(
  540. varDecl(
  541. hasName("X"),
  542. hasType(asString("int")),
  543. hasInitializer(
  544. integerLiteral(equals(4))))))),
  545. hasDescendant(
  546. implicitCastExpr())))))));
  547. }
  548. TEST_P(ImportExpr, ImportConditionalOperator) {
  549. MatchVerifier<Decl> Verifier;
  550. testImport(
  551. "void declToImport() { (void)(true ? 1 : -5); }",
  552. Lang_CXX, "", Lang_CXX, Verifier,
  553. functionDecl(hasDescendant(
  554. conditionalOperator(
  555. hasCondition(cxxBoolLiteral(equals(true))),
  556. hasTrueExpression(integerLiteral(equals(1))),
  557. hasFalseExpression(
  558. unaryOperator(hasUnaryOperand(integerLiteral(equals(5))))))
  559. )));
  560. }
  561. TEST_P(ImportExpr, ImportBinaryConditionalOperator) {
  562. MatchVerifier<Decl> Verifier;
  563. testImport(
  564. "void declToImport() { (void)(1 ?: -5); }",
  565. Lang_CXX, "", Lang_CXX, Verifier,
  566. functionDecl(hasDescendant(
  567. binaryConditionalOperator(
  568. hasCondition(
  569. implicitCastExpr(
  570. hasSourceExpression(opaqueValueExpr(
  571. hasSourceExpression(integerLiteral(equals(1))))),
  572. hasType(booleanType()))),
  573. hasTrueExpression(
  574. opaqueValueExpr(
  575. hasSourceExpression(integerLiteral(equals(1))))),
  576. hasFalseExpression(
  577. unaryOperator(
  578. hasOperatorName("-"),
  579. hasUnaryOperand(integerLiteral(equals(5)))))))));
  580. }
  581. TEST_P(ImportExpr, ImportDesignatedInitExpr) {
  582. MatchVerifier<Decl> Verifier;
  583. testImport(
  584. "void declToImport() {"
  585. " struct point { double x; double y; };"
  586. " struct point ptarray[10] = "
  587. "{ [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; }",
  588. Lang_C, "", Lang_C, Verifier,
  589. functionDecl(hasDescendant(
  590. initListExpr(
  591. has(designatedInitExpr(
  592. designatorCountIs(2),
  593. has(floatLiteral(equals(1.0))),
  594. has(integerLiteral(equals(2))))),
  595. has(designatedInitExpr(
  596. designatorCountIs(2),
  597. has(floatLiteral(equals(2.0))),
  598. has(integerLiteral(equals(2))))),
  599. has(designatedInitExpr(
  600. designatorCountIs(2),
  601. has(floatLiteral(equals(1.0))),
  602. has(integerLiteral(equals(0)))))))));
  603. }
  604. TEST_P(ImportExpr, ImportPredefinedExpr) {
  605. MatchVerifier<Decl> Verifier;
  606. // __func__ expands as StringLiteral("declToImport")
  607. testImport(
  608. "void declToImport() { (void)__func__; }",
  609. Lang_CXX, "", Lang_CXX, Verifier,
  610. functionDecl(hasDescendant(
  611. predefinedExpr(
  612. hasType(
  613. asString("const char [13]")),
  614. has(stringLiteral(hasType(
  615. asString("const char [13]"))))))));
  616. }
  617. TEST_P(ImportExpr, ImportInitListExpr) {
  618. MatchVerifier<Decl> Verifier;
  619. testImport(
  620. "void declToImport() {"
  621. " struct point { double x; double y; };"
  622. " point ptarray[10] = { [2].y = 1.0, [2].x = 2.0,"
  623. " [0].x = 1.0 }; }",
  624. Lang_CXX, "", Lang_CXX, Verifier,
  625. functionDecl(hasDescendant(
  626. initListExpr(
  627. has(
  628. cxxConstructExpr(
  629. requiresZeroInitialization())),
  630. has(
  631. initListExpr(
  632. hasType(asString("struct point")),
  633. has(floatLiteral(equals(1.0))),
  634. has(implicitValueInitExpr(
  635. hasType(asString("double")))))),
  636. has(
  637. initListExpr(
  638. hasType(asString("struct point")),
  639. has(floatLiteral(equals(2.0))),
  640. has(floatLiteral(equals(1.0)))))))));
  641. }
  642. const internal::VariadicDynCastAllOfMatcher<Expr, VAArgExpr> vaArgExpr;
  643. TEST_P(ImportExpr, ImportVAArgExpr) {
  644. MatchVerifier<Decl> Verifier;
  645. testImport(
  646. "void declToImport(__builtin_va_list list, ...) {"
  647. " (void)__builtin_va_arg(list, int); }",
  648. Lang_CXX, "", Lang_CXX, Verifier,
  649. functionDecl(hasDescendant(
  650. cStyleCastExpr(hasSourceExpression(vaArgExpr())))));
  651. }
  652. TEST_P(ImportExpr, CXXTemporaryObjectExpr) {
  653. MatchVerifier<Decl> Verifier;
  654. testImport(
  655. "struct C {};"
  656. "void declToImport() { C c = C(); }",
  657. Lang_CXX, "", Lang_CXX, Verifier,
  658. functionDecl(hasDescendant(
  659. exprWithCleanups(has(cxxConstructExpr(
  660. has(materializeTemporaryExpr(has(implicitCastExpr(
  661. has(cxxTemporaryObjectExpr())))))))))));
  662. }
  663. TEST_P(ImportType, ImportAtomicType) {
  664. MatchVerifier<Decl> Verifier;
  665. testImport(
  666. "void declToImport() { typedef _Atomic(int) a_int; }",
  667. Lang_CXX11, "", Lang_CXX11, Verifier,
  668. functionDecl(hasDescendant(typedefDecl(has(atomicType())))));
  669. }
  670. TEST_P(ImportDecl, ImportFunctionTemplateDecl) {
  671. MatchVerifier<Decl> Verifier;
  672. testImport(
  673. "template <typename T> void declToImport() { };",
  674. Lang_CXX, "", Lang_CXX, Verifier,
  675. functionTemplateDecl());
  676. }
  677. const internal::VariadicDynCastAllOfMatcher<Expr, CXXDependentScopeMemberExpr>
  678. cxxDependentScopeMemberExpr;
  679. TEST_P(ImportExpr, ImportCXXDependentScopeMemberExpr) {
  680. MatchVerifier<Decl> Verifier;
  681. testImport(
  682. "template <typename T> struct C { T t; };"
  683. "template <typename T> void declToImport() {"
  684. " C<T> d;"
  685. " (void)d.t;"
  686. "}"
  687. "void instantiate() { declToImport<int>(); }",
  688. Lang_CXX, "", Lang_CXX, Verifier,
  689. functionTemplateDecl(hasDescendant(
  690. cStyleCastExpr(has(cxxDependentScopeMemberExpr())))));
  691. testImport(
  692. "template <typename T> struct C { T t; };"
  693. "template <typename T> void declToImport() {"
  694. " C<T> d;"
  695. " (void)(&d)->t;"
  696. "}"
  697. "void instantiate() { declToImport<int>(); }",
  698. Lang_CXX, "", Lang_CXX, Verifier,
  699. functionTemplateDecl(hasDescendant(
  700. cStyleCastExpr(has(cxxDependentScopeMemberExpr())))));
  701. }
  702. TEST_P(ImportType, ImportTypeAliasTemplate) {
  703. MatchVerifier<Decl> Verifier;
  704. testImport(
  705. "template <int K>"
  706. "struct dummy { static const int i = K; };"
  707. "template <int K> using dummy2 = dummy<K>;"
  708. "int declToImport() { return dummy2<3>::i; }",
  709. Lang_CXX11, "", Lang_CXX11, Verifier,
  710. functionDecl(
  711. hasDescendant(implicitCastExpr(has(declRefExpr()))),
  712. unless(hasAncestor(translationUnitDecl(has(typeAliasDecl()))))));
  713. }
  714. const internal::VariadicDynCastAllOfMatcher<Decl, VarTemplateSpecializationDecl>
  715. varTemplateSpecializationDecl;
  716. TEST_P(ImportDecl, ImportVarTemplate) {
  717. MatchVerifier<Decl> Verifier;
  718. testImport(
  719. "template <typename T>"
  720. "T pi = T(3.1415926535897932385L);"
  721. "void declToImport() { (void)pi<int>; }",
  722. Lang_CXX14, "", Lang_CXX14, Verifier,
  723. functionDecl(
  724. hasDescendant(declRefExpr(to(varTemplateSpecializationDecl()))),
  725. unless(hasAncestor(translationUnitDecl(has(varDecl(
  726. hasName("pi"), unless(varTemplateSpecializationDecl()))))))));
  727. }
  728. TEST_P(ImportType, ImportPackExpansion) {
  729. MatchVerifier<Decl> Verifier;
  730. testImport(
  731. "template <typename... Args>"
  732. "struct dummy {"
  733. " dummy(Args... args) {}"
  734. " static const int i = 4;"
  735. "};"
  736. "int declToImport() { return dummy<int>::i; }",
  737. Lang_CXX11, "", Lang_CXX11, Verifier,
  738. functionDecl(hasDescendant(
  739. returnStmt(has(implicitCastExpr(has(declRefExpr())))))));
  740. }
  741. const internal::VariadicDynCastAllOfMatcher<Type,
  742. DependentTemplateSpecializationType>
  743. dependentTemplateSpecializationType;
  744. TEST_P(ImportType, ImportDependentTemplateSpecialization) {
  745. MatchVerifier<Decl> Verifier;
  746. testImport(
  747. "template<typename T>"
  748. "struct A;"
  749. "template<typename T>"
  750. "struct declToImport {"
  751. " typename A<T>::template B<T> a;"
  752. "};",
  753. Lang_CXX, "", Lang_CXX, Verifier,
  754. classTemplateDecl(has(cxxRecordDecl(has(
  755. fieldDecl(hasType(dependentTemplateSpecializationType())))))));
  756. }
  757. const internal::VariadicDynCastAllOfMatcher<Stmt, SizeOfPackExpr>
  758. sizeOfPackExpr;
  759. TEST_P(ImportExpr, ImportSizeOfPackExpr) {
  760. MatchVerifier<Decl> Verifier;
  761. testImport(
  762. "template <typename... Ts>"
  763. "void declToImport() {"
  764. " const int i = sizeof...(Ts);"
  765. "};"
  766. "void g() { declToImport<int>(); }",
  767. Lang_CXX11, "", Lang_CXX11, Verifier,
  768. functionTemplateDecl(hasDescendant(sizeOfPackExpr())));
  769. testImport(
  770. "template <typename... Ts>"
  771. "using X = int[sizeof...(Ts)];"
  772. "template <typename... Us>"
  773. "struct Y {"
  774. " X<Us..., int, double, int, Us...> f;"
  775. "};"
  776. "Y<float, int> declToImport;",
  777. Lang_CXX11, "", Lang_CXX11, Verifier,
  778. varDecl(hasType(classTemplateSpecializationDecl(has(fieldDecl(hasType(
  779. hasUnqualifiedDesugaredType(constantArrayType(hasSize(7))))))))));
  780. }
  781. /// \brief Matches __builtin_types_compatible_p:
  782. /// GNU extension to check equivalent types
  783. /// Given
  784. /// \code
  785. /// __builtin_types_compatible_p(int, int)
  786. /// \endcode
  787. // will generate TypeTraitExpr <...> 'int'
  788. const internal::VariadicDynCastAllOfMatcher<Stmt, TypeTraitExpr> typeTraitExpr;
  789. TEST_P(ImportExpr, ImportTypeTraitExpr) {
  790. MatchVerifier<Decl> Verifier;
  791. testImport(
  792. "void declToImport() { "
  793. " (void)__builtin_types_compatible_p(int, int);"
  794. "}",
  795. Lang_C, "", Lang_C, Verifier,
  796. functionDecl(hasDescendant(typeTraitExpr(hasType(asString("int"))))));
  797. }
  798. const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTypeidExpr> cxxTypeidExpr;
  799. TEST_P(ImportExpr, ImportCXXTypeidExpr) {
  800. MatchVerifier<Decl> Verifier;
  801. testImport(
  802. "namespace std { class type_info {}; }"
  803. "void declToImport() {"
  804. " int x;"
  805. " auto a = typeid(int); auto b = typeid(x);"
  806. "}",
  807. Lang_CXX11, "", Lang_CXX11, Verifier,
  808. functionDecl(
  809. hasDescendant(varDecl(
  810. hasName("a"), hasInitializer(hasDescendant(cxxTypeidExpr())))),
  811. hasDescendant(varDecl(
  812. hasName("b"), hasInitializer(hasDescendant(cxxTypeidExpr()))))));
  813. }
  814. TEST_P(ImportExpr, ImportTypeTraitExprValDep) {
  815. MatchVerifier<Decl> Verifier;
  816. testImport(
  817. "template<typename T> struct declToImport {"
  818. " void m() { (void)__is_pod(T); }"
  819. "};"
  820. "void f() { declToImport<int>().m(); }",
  821. Lang_CXX11, "", Lang_CXX11, Verifier,
  822. classTemplateDecl(has(cxxRecordDecl(has(
  823. functionDecl(hasDescendant(
  824. typeTraitExpr(hasType(booleanType())))))))));
  825. }
  826. const internal::VariadicDynCastAllOfMatcher<Expr, CXXPseudoDestructorExpr>
  827. cxxPseudoDestructorExpr;
  828. TEST_P(ImportExpr, ImportCXXPseudoDestructorExpr) {
  829. MatchVerifier<Decl> Verifier;
  830. testImport(
  831. "typedef int T;"
  832. "void declToImport(int *p) {"
  833. " T t;"
  834. " p->T::~T();"
  835. "}",
  836. Lang_CXX, "", Lang_CXX, Verifier,
  837. functionDecl(hasDescendant(
  838. callExpr(has(cxxPseudoDestructorExpr())))));
  839. }
  840. TEST_P(ImportDecl, ImportUsingDecl) {
  841. MatchVerifier<Decl> Verifier;
  842. testImport(
  843. "namespace foo { int bar; }"
  844. "void declToImport() { using foo::bar; }",
  845. Lang_CXX, "", Lang_CXX, Verifier,
  846. functionDecl(hasDescendant(usingDecl())));
  847. }
  848. /// \brief Matches shadow declarations introduced into a scope by a
  849. /// (resolved) using declaration.
  850. ///
  851. /// Given
  852. /// \code
  853. /// namespace n { int f; }
  854. /// namespace declToImport { using n::f; }
  855. /// \endcode
  856. /// usingShadowDecl()
  857. /// matches \code f \endcode
  858. const internal::VariadicDynCastAllOfMatcher<Decl,
  859. UsingShadowDecl> usingShadowDecl;
  860. TEST_P(ImportDecl, ImportUsingShadowDecl) {
  861. MatchVerifier<Decl> Verifier;
  862. testImport(
  863. "namespace foo { int bar; }"
  864. "namespace declToImport { using foo::bar; }",
  865. Lang_CXX, "", Lang_CXX, Verifier,
  866. namespaceDecl(has(usingShadowDecl())));
  867. }
  868. TEST_P(ImportExpr, ImportUnresolvedLookupExpr) {
  869. MatchVerifier<Decl> Verifier;
  870. testImport(
  871. "template<typename T> int foo();"
  872. "template <typename T> void declToImport() {"
  873. " (void)::foo<T>;"
  874. " (void)::template foo<T>;"
  875. "}"
  876. "void instantiate() { declToImport<int>(); }",
  877. Lang_CXX, "", Lang_CXX, Verifier,
  878. functionTemplateDecl(hasDescendant(unresolvedLookupExpr())));
  879. }
  880. TEST_P(ImportExpr, ImportCXXUnresolvedConstructExpr) {
  881. MatchVerifier<Decl> Verifier;
  882. testImport(
  883. "template <typename T> struct C { T t; };"
  884. "template <typename T> void declToImport() {"
  885. " C<T> d;"
  886. " d.t = T();"
  887. "}"
  888. "void instantiate() { declToImport<int>(); }",
  889. Lang_CXX, "", Lang_CXX, Verifier,
  890. functionTemplateDecl(hasDescendant(
  891. binaryOperator(has(cxxUnresolvedConstructExpr())))));
  892. testImport(
  893. "template <typename T> struct C { T t; };"
  894. "template <typename T> void declToImport() {"
  895. " C<T> d;"
  896. " (&d)->t = T();"
  897. "}"
  898. "void instantiate() { declToImport<int>(); }",
  899. Lang_CXX, "", Lang_CXX, Verifier,
  900. functionTemplateDecl(hasDescendant(
  901. binaryOperator(has(cxxUnresolvedConstructExpr())))));
  902. }
  903. /// Check that function "declToImport()" (which is the templated function
  904. /// for corresponding FunctionTemplateDecl) is not added into DeclContext.
  905. /// Same for class template declarations.
  906. TEST_P(ImportDecl, ImportTemplatedDeclForTemplate) {
  907. MatchVerifier<Decl> Verifier;
  908. testImport(
  909. "template <typename T> void declToImport() { T a = 1; }"
  910. "void instantiate() { declToImport<int>(); }",
  911. Lang_CXX, "", Lang_CXX, Verifier,
  912. functionTemplateDecl(hasAncestor(translationUnitDecl(
  913. unless(has(functionDecl(hasName("declToImport"))))))));
  914. testImport(
  915. "template <typename T> struct declToImport { T t; };"
  916. "void instantiate() { declToImport<int>(); }",
  917. Lang_CXX, "", Lang_CXX, Verifier,
  918. classTemplateDecl(hasAncestor(translationUnitDecl(
  919. unless(has(cxxRecordDecl(hasName("declToImport"))))))));
  920. }
  921. TEST_P(ImportDecl, ImportClassTemplatePartialSpecialization) {
  922. MatchVerifier<Decl> Verifier;
  923. auto Code =
  924. R"s(
  925. struct declToImport {
  926. template <typename T0> struct X;
  927. template <typename T0> struct X<T0 *> {};
  928. };
  929. )s";
  930. testImport(Code, Lang_CXX, "", Lang_CXX, Verifier,
  931. recordDecl(has(classTemplateDecl()),
  932. has(classTemplateSpecializationDecl())));
  933. }
  934. TEST_P(ImportExpr, CXXOperatorCallExpr) {
  935. MatchVerifier<Decl> Verifier;
  936. testImport(
  937. "class declToImport {"
  938. " void f() { *this = declToImport(); }"
  939. "};",
  940. Lang_CXX, "", Lang_CXX, Verifier,
  941. cxxRecordDecl(has(cxxMethodDecl(hasDescendant(
  942. cxxOperatorCallExpr())))));
  943. }
  944. TEST_P(ImportExpr, DependentSizedArrayType) {
  945. MatchVerifier<Decl> Verifier;
  946. testImport(
  947. "template<typename T, int Size> class declToImport {"
  948. " T data[Size];"
  949. "};",
  950. Lang_CXX, "", Lang_CXX, Verifier,
  951. classTemplateDecl(has(cxxRecordDecl(
  952. has(fieldDecl(hasType(dependentSizedArrayType())))))));
  953. }
  954. TEST_P(ASTImporterTestBase, 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(ASTImporterTestBase, ImportOfTemplatedDeclOfFunctionTemplateDecl) {
  967. Decl *FromTU = getTuDecl("template<class X> void f(){}", Lang_CXX);
  968. auto From = FirstDeclMatcher<FunctionTemplateDecl>().match(
  969. FromTU, functionTemplateDecl());
  970. ASSERT_TRUE(From);
  971. auto To = cast<FunctionTemplateDecl>(Import(From, Lang_CXX));
  972. ASSERT_TRUE(To);
  973. Decl *ToTemplated = To->getTemplatedDecl();
  974. Decl *ToTemplated1 = Import(From->getTemplatedDecl(), Lang_CXX);
  975. EXPECT_TRUE(ToTemplated1);
  976. EXPECT_EQ(ToTemplated1, ToTemplated);
  977. }
  978. TEST_P(ASTImporterTestBase,
  979. ImportOfTemplatedDeclShouldImportTheClassTemplateDecl) {
  980. Decl *FromTU = getTuDecl("template<class X> struct S{};", Lang_CXX);
  981. auto FromFT =
  982. FirstDeclMatcher<ClassTemplateDecl>().match(FromTU, classTemplateDecl());
  983. ASSERT_TRUE(FromFT);
  984. auto ToTemplated =
  985. cast<CXXRecordDecl>(Import(FromFT->getTemplatedDecl(), Lang_CXX));
  986. EXPECT_TRUE(ToTemplated);
  987. auto ToTU = ToTemplated->getTranslationUnitDecl();
  988. auto ToFT =
  989. FirstDeclMatcher<ClassTemplateDecl>().match(ToTU, classTemplateDecl());
  990. EXPECT_TRUE(ToFT);
  991. }
  992. TEST_P(ASTImporterTestBase,
  993. DISABLED_ImportOfTemplatedDeclShouldImportTheFunctionTemplateDecl) {
  994. Decl *FromTU = getTuDecl("template<class X> void f(){}", Lang_CXX);
  995. auto FromFT = FirstDeclMatcher<FunctionTemplateDecl>().match(
  996. FromTU, functionTemplateDecl());
  997. ASSERT_TRUE(FromFT);
  998. auto ToTemplated =
  999. cast<FunctionDecl>(Import(FromFT->getTemplatedDecl(), Lang_CXX));
  1000. EXPECT_TRUE(ToTemplated);
  1001. auto ToTU = ToTemplated->getTranslationUnitDecl();
  1002. auto ToFT = FirstDeclMatcher<FunctionTemplateDecl>().match(
  1003. ToTU, functionTemplateDecl());
  1004. EXPECT_TRUE(ToFT);
  1005. }
  1006. TEST_P(ASTImporterTestBase, ImportCorrectTemplatedDecl) {
  1007. auto Code =
  1008. R"(
  1009. namespace x {
  1010. template<class X> struct S1{};
  1011. template<class X> struct S2{};
  1012. template<class X> struct S3{};
  1013. }
  1014. )";
  1015. Decl *FromTU = getTuDecl(Code, Lang_CXX);
  1016. auto FromNs =
  1017. FirstDeclMatcher<NamespaceDecl>().match(FromTU, namespaceDecl());
  1018. auto ToNs = cast<NamespaceDecl>(Import(FromNs, Lang_CXX));
  1019. ASSERT_TRUE(ToNs);
  1020. auto From =
  1021. FirstDeclMatcher<ClassTemplateDecl>().match(FromTU,
  1022. classTemplateDecl(
  1023. hasName("S2")));
  1024. auto To =
  1025. FirstDeclMatcher<ClassTemplateDecl>().match(ToNs,
  1026. classTemplateDecl(
  1027. hasName("S2")));
  1028. ASSERT_TRUE(From);
  1029. ASSERT_TRUE(To);
  1030. auto ToTemplated = To->getTemplatedDecl();
  1031. auto ToTemplated1 =
  1032. cast<CXXRecordDecl>(Import(From->getTemplatedDecl(), Lang_CXX));
  1033. EXPECT_TRUE(ToTemplated1);
  1034. ASSERT_EQ(ToTemplated1, ToTemplated);
  1035. }
  1036. TEST_P(ASTImporterTestBase, ImportFunctionWithBackReferringParameter) {
  1037. Decl *From, *To;
  1038. std::tie(From, To) = getImportedDecl(
  1039. R"(
  1040. template <typename T> struct X {};
  1041. void declToImport(int y, X<int> &x) {}
  1042. template <> struct X<int> {
  1043. void g() {
  1044. X<int> x;
  1045. declToImport(0, x);
  1046. }
  1047. };
  1048. )",
  1049. Lang_CXX, "", Lang_CXX);
  1050. MatchVerifier<Decl> Verifier;
  1051. auto Matcher = functionDecl(hasName("declToImport"),
  1052. parameterCountIs(2),
  1053. hasParameter(0, hasName("y")),
  1054. hasParameter(1, hasName("x")),
  1055. hasParameter(1, hasType(asString("X<int> &"))));
  1056. ASSERT_TRUE(Verifier.match(From, Matcher));
  1057. EXPECT_TRUE(Verifier.match(To, Matcher));
  1058. }
  1059. TEST_P(ASTImporterTestBase,
  1060. TUshouldNotContainTemplatedDeclOfFunctionTemplates) {
  1061. Decl *From, *To;
  1062. std::tie(From, To) =
  1063. getImportedDecl("template <typename T> void declToImport() { T a = 1; }"
  1064. "void instantiate() { declToImport<int>(); }",
  1065. Lang_CXX, "", Lang_CXX);
  1066. auto Check = [](Decl *D) -> bool {
  1067. auto TU = D->getTranslationUnitDecl();
  1068. for (auto Child : TU->decls()) {
  1069. if (auto *FD = dyn_cast<FunctionDecl>(Child)) {
  1070. if (FD->getNameAsString() == "declToImport") {
  1071. GTEST_NONFATAL_FAILURE_(
  1072. "TU should not contain any FunctionDecl with name declToImport");
  1073. return false;
  1074. }
  1075. }
  1076. }
  1077. return true;
  1078. };
  1079. ASSERT_TRUE(Check(From));
  1080. EXPECT_TRUE(Check(To));
  1081. }
  1082. TEST_P(ASTImporterTestBase, TUshouldNotContainTemplatedDeclOfClassTemplates) {
  1083. Decl *From, *To;
  1084. std::tie(From, To) =
  1085. getImportedDecl("template <typename T> struct declToImport { T t; };"
  1086. "void instantiate() { declToImport<int>(); }",
  1087. Lang_CXX, "", Lang_CXX);
  1088. auto Check = [](Decl *D) -> bool {
  1089. auto TU = D->getTranslationUnitDecl();
  1090. for (auto Child : TU->decls()) {
  1091. if (auto *RD = dyn_cast<CXXRecordDecl>(Child)) {
  1092. if (RD->getNameAsString() == "declToImport") {
  1093. GTEST_NONFATAL_FAILURE_(
  1094. "TU should not contain any CXXRecordDecl with name declToImport");
  1095. return false;
  1096. }
  1097. }
  1098. }
  1099. return true;
  1100. };
  1101. ASSERT_TRUE(Check(From));
  1102. EXPECT_TRUE(Check(To));
  1103. }
  1104. TEST_P(ASTImporterTestBase, TUshouldNotContainTemplatedDeclOfTypeAlias) {
  1105. Decl *From, *To;
  1106. std::tie(From, To) =
  1107. getImportedDecl(
  1108. "template <typename T> struct X {};"
  1109. "template <typename T> using declToImport = X<T>;"
  1110. "void instantiate() { declToImport<int> a; }",
  1111. Lang_CXX11, "", Lang_CXX11);
  1112. auto Check = [](Decl *D) -> bool {
  1113. auto TU = D->getTranslationUnitDecl();
  1114. for (auto Child : TU->decls()) {
  1115. if (auto *AD = dyn_cast<TypeAliasDecl>(Child)) {
  1116. if (AD->getNameAsString() == "declToImport") {
  1117. GTEST_NONFATAL_FAILURE_(
  1118. "TU should not contain any TypeAliasDecl with name declToImport");
  1119. return false;
  1120. }
  1121. }
  1122. }
  1123. return true;
  1124. };
  1125. ASSERT_TRUE(Check(From));
  1126. EXPECT_TRUE(Check(To));
  1127. }
  1128. TEST_P(
  1129. ASTImporterTestBase,
  1130. TUshouldNotContainClassTemplateSpecializationOfImplicitInstantiation) {
  1131. Decl *From, *To;
  1132. std::tie(From, To) = getImportedDecl(
  1133. R"(
  1134. template<class T>
  1135. class Base {};
  1136. class declToImport : public Base<declToImport> {};
  1137. )",
  1138. Lang_CXX, "", Lang_CXX);
  1139. // Check that the ClassTemplateSpecializationDecl is NOT the child of the TU.
  1140. auto Pattern =
  1141. translationUnitDecl(unless(has(classTemplateSpecializationDecl())));
  1142. ASSERT_TRUE(
  1143. MatchVerifier<Decl>{}.match(From->getTranslationUnitDecl(), Pattern));
  1144. EXPECT_TRUE(
  1145. MatchVerifier<Decl>{}.match(To->getTranslationUnitDecl(), Pattern));
  1146. // Check that the ClassTemplateSpecializationDecl is the child of the
  1147. // ClassTemplateDecl.
  1148. Pattern = translationUnitDecl(has(classTemplateDecl(
  1149. hasName("Base"), has(classTemplateSpecializationDecl()))));
  1150. ASSERT_TRUE(
  1151. MatchVerifier<Decl>{}.match(From->getTranslationUnitDecl(), Pattern));
  1152. EXPECT_TRUE(
  1153. MatchVerifier<Decl>{}.match(To->getTranslationUnitDecl(), Pattern));
  1154. }
  1155. AST_MATCHER_P(RecordDecl, hasFieldOrder, std::vector<StringRef>, Order) {
  1156. size_t Index = 0;
  1157. for (FieldDecl *Field : Node.fields()) {
  1158. if (Index == Order.size())
  1159. return false;
  1160. if (Field->getName() != Order[Index])
  1161. return false;
  1162. ++Index;
  1163. }
  1164. return Index == Order.size();
  1165. }
  1166. TEST_P(ASTImporterTestBase,
  1167. TUshouldContainClassTemplateSpecializationOfExplicitInstantiation) {
  1168. Decl *From, *To;
  1169. std::tie(From, To) = getImportedDecl(
  1170. R"(
  1171. namespace NS {
  1172. template<class T>
  1173. class X {};
  1174. template class X<int>;
  1175. }
  1176. )",
  1177. Lang_CXX, "", Lang_CXX, "NS");
  1178. // Check that the ClassTemplateSpecializationDecl is NOT the child of the
  1179. // ClassTemplateDecl.
  1180. auto Pattern = namespaceDecl(has(classTemplateDecl(
  1181. hasName("X"), unless(has(classTemplateSpecializationDecl())))));
  1182. ASSERT_TRUE(MatchVerifier<Decl>{}.match(From, Pattern));
  1183. EXPECT_TRUE(MatchVerifier<Decl>{}.match(To, Pattern));
  1184. // Check that the ClassTemplateSpecializationDecl is the child of the
  1185. // NamespaceDecl.
  1186. Pattern = namespaceDecl(has(classTemplateSpecializationDecl(hasName("X"))));
  1187. ASSERT_TRUE(MatchVerifier<Decl>{}.match(From, Pattern));
  1188. EXPECT_TRUE(MatchVerifier<Decl>{}.match(To, Pattern));
  1189. }
  1190. TEST_P(ASTImporterTestBase, CXXRecordDeclFieldsShouldBeInCorrectOrder) {
  1191. Decl *From, *To;
  1192. std::tie(From, To) =
  1193. getImportedDecl(
  1194. "struct declToImport { int a; int b; };",
  1195. Lang_CXX11, "", Lang_CXX11);
  1196. MatchVerifier<Decl> Verifier;
  1197. ASSERT_TRUE(Verifier.match(From, cxxRecordDecl(hasFieldOrder({"a", "b"}))));
  1198. EXPECT_TRUE(Verifier.match(To, cxxRecordDecl(hasFieldOrder({"a", "b"}))));
  1199. }
  1200. TEST_P(ASTImporterTestBase,
  1201. DISABLED_CXXRecordDeclFieldOrderShouldNotDependOnImportOrder) {
  1202. Decl *From, *To;
  1203. std::tie(From, To) = getImportedDecl(
  1204. // The original recursive algorithm of ASTImporter first imports 'c' then
  1205. // 'b' and lastly 'a'. Therefore we must restore the order somehow.
  1206. R"s(
  1207. struct declToImport {
  1208. int a = c + b;
  1209. int b = 1;
  1210. int c = 2;
  1211. };
  1212. )s",
  1213. Lang_CXX11, "", Lang_CXX11);
  1214. MatchVerifier<Decl> Verifier;
  1215. ASSERT_TRUE(
  1216. Verifier.match(From, cxxRecordDecl(hasFieldOrder({"a", "b", "c"}))));
  1217. EXPECT_TRUE(
  1218. Verifier.match(To, cxxRecordDecl(hasFieldOrder({"a", "b", "c"}))));
  1219. }
  1220. TEST_P(ASTImporterTestBase, ShouldImportImplicitCXXRecordDecl) {
  1221. Decl *From, *To;
  1222. std::tie(From, To) = getImportedDecl(
  1223. R"(
  1224. struct declToImport {
  1225. };
  1226. )",
  1227. Lang_CXX, "", Lang_CXX);
  1228. MatchVerifier<Decl> Verifier;
  1229. // Match the implicit Decl.
  1230. auto Matcher = cxxRecordDecl(has(cxxRecordDecl()));
  1231. ASSERT_TRUE(Verifier.match(From, Matcher));
  1232. EXPECT_TRUE(Verifier.match(To, Matcher));
  1233. }
  1234. TEST_P(ASTImporterTestBase, ShouldImportImplicitCXXRecordDeclOfClassTemplate) {
  1235. Decl *From, *To;
  1236. std::tie(From, To) = getImportedDecl(
  1237. R"(
  1238. template <typename U>
  1239. struct declToImport {
  1240. };
  1241. )",
  1242. Lang_CXX, "", Lang_CXX);
  1243. MatchVerifier<Decl> Verifier;
  1244. // Match the implicit Decl.
  1245. auto Matcher = classTemplateDecl(has(cxxRecordDecl(has(cxxRecordDecl()))));
  1246. ASSERT_TRUE(Verifier.match(From, Matcher));
  1247. EXPECT_TRUE(Verifier.match(To, Matcher));
  1248. }
  1249. TEST_P(
  1250. ASTImporterTestBase,
  1251. ShouldImportImplicitCXXRecordDeclOfClassTemplateSpecializationDecl) {
  1252. Decl *From, *To;
  1253. std::tie(From, To) = getImportedDecl(
  1254. R"(
  1255. template<class T>
  1256. class Base {};
  1257. class declToImport : public Base<declToImport> {};
  1258. )",
  1259. Lang_CXX, "", Lang_CXX);
  1260. auto hasImplicitClass = has(cxxRecordDecl());
  1261. auto Pattern = translationUnitDecl(has(classTemplateDecl(
  1262. hasName("Base"),
  1263. has(classTemplateSpecializationDecl(hasImplicitClass)))));
  1264. ASSERT_TRUE(
  1265. MatchVerifier<Decl>{}.match(From->getTranslationUnitDecl(), Pattern));
  1266. EXPECT_TRUE(
  1267. MatchVerifier<Decl>{}.match(To->getTranslationUnitDecl(), Pattern));
  1268. }
  1269. TEST_P(ASTImporterTestBase, IDNSOrdinary) {
  1270. Decl *From, *To;
  1271. std::tie(From, To) =
  1272. getImportedDecl("void declToImport() {}", Lang_CXX, "", Lang_CXX);
  1273. MatchVerifier<Decl> Verifier;
  1274. auto Matcher = functionDecl();
  1275. ASSERT_TRUE(Verifier.match(From, Matcher));
  1276. EXPECT_TRUE(Verifier.match(To, Matcher));
  1277. EXPECT_EQ(From->getIdentifierNamespace(), To->getIdentifierNamespace());
  1278. }
  1279. TEST_P(ASTImporterTestBase, IDNSOfNonmemberOperator) {
  1280. Decl *FromTU = getTuDecl(
  1281. R"(
  1282. struct X {};
  1283. void operator<<(int, X);
  1284. )",
  1285. Lang_CXX);
  1286. Decl *From = LastDeclMatcher<Decl>{}.match(FromTU, functionDecl());
  1287. const Decl *To = Import(From, Lang_CXX);
  1288. EXPECT_EQ(From->getIdentifierNamespace(), To->getIdentifierNamespace());
  1289. }
  1290. TEST_P(ASTImporterTestBase,
  1291. ShouldImportMembersOfClassTemplateSpecializationDecl) {
  1292. Decl *From, *To;
  1293. std::tie(From, To) = getImportedDecl(
  1294. R"(
  1295. template<class T>
  1296. class Base { int a; };
  1297. class declToImport : Base<declToImport> {};
  1298. )",
  1299. Lang_CXX, "", Lang_CXX);
  1300. auto Pattern = translationUnitDecl(has(classTemplateDecl(
  1301. hasName("Base"),
  1302. has(classTemplateSpecializationDecl(has(fieldDecl(hasName("a"))))))));
  1303. ASSERT_TRUE(
  1304. MatchVerifier<Decl>{}.match(From->getTranslationUnitDecl(), Pattern));
  1305. EXPECT_TRUE(
  1306. MatchVerifier<Decl>{}.match(To->getTranslationUnitDecl(), Pattern));
  1307. }
  1308. TEST_P(ASTImporterTestBase, ImportDefinitionOfClassTemplateAfterFwdDecl) {
  1309. {
  1310. Decl *FromTU = getTuDecl(
  1311. R"(
  1312. template <typename T>
  1313. struct B;
  1314. )",
  1315. Lang_CXX, "input0.cc");
  1316. auto *FromD = FirstDeclMatcher<ClassTemplateDecl>().match(
  1317. FromTU, classTemplateDecl(hasName("B")));
  1318. Import(FromD, Lang_CXX);
  1319. }
  1320. {
  1321. Decl *FromTU = getTuDecl(
  1322. R"(
  1323. template <typename T>
  1324. struct B {
  1325. void f();
  1326. };
  1327. )",
  1328. Lang_CXX, "input1.cc");
  1329. FunctionDecl *FromD = FirstDeclMatcher<FunctionDecl>().match(
  1330. FromTU, functionDecl(hasName("f")));
  1331. Import(FromD, Lang_CXX);
  1332. auto *FromCTD = FirstDeclMatcher<ClassTemplateDecl>().match(
  1333. FromTU, classTemplateDecl(hasName("B")));
  1334. auto *ToCTD = cast<ClassTemplateDecl>(Import(FromCTD, Lang_CXX));
  1335. EXPECT_TRUE(ToCTD->isThisDeclarationADefinition());
  1336. }
  1337. }
  1338. TEST_P(ASTImporterTestBase,
  1339. ImportDefinitionOfClassTemplateIfThereIsAnExistingFwdDeclAndDefinition) {
  1340. Decl *ToTU = getToTuDecl(
  1341. R"(
  1342. template <typename T>
  1343. struct B {
  1344. void f();
  1345. };
  1346. template <typename T>
  1347. struct B;
  1348. )",
  1349. Lang_CXX);
  1350. ASSERT_EQ(1u, DeclCounterWithPredicate<ClassTemplateDecl>(
  1351. [](const ClassTemplateDecl *T) {
  1352. return T->isThisDeclarationADefinition();
  1353. })
  1354. .match(ToTU, classTemplateDecl()));
  1355. Decl *FromTU = getTuDecl(
  1356. R"(
  1357. template <typename T>
  1358. struct B {
  1359. void f();
  1360. };
  1361. )",
  1362. Lang_CXX, "input1.cc");
  1363. ClassTemplateDecl *FromD = FirstDeclMatcher<ClassTemplateDecl>().match(
  1364. FromTU, classTemplateDecl(hasName("B")));
  1365. Import(FromD, Lang_CXX);
  1366. // We should have only one definition.
  1367. EXPECT_EQ(1u, DeclCounterWithPredicate<ClassTemplateDecl>(
  1368. [](const ClassTemplateDecl *T) {
  1369. return T->isThisDeclarationADefinition();
  1370. })
  1371. .match(ToTU, classTemplateDecl()));
  1372. }
  1373. TEST_P(ASTImporterTestBase,
  1374. ImportDefinitionOfClassIfThereIsAnExistingFwdDeclAndDefinition) {
  1375. Decl *ToTU = getToTuDecl(
  1376. R"(
  1377. struct B {
  1378. void f();
  1379. };
  1380. struct B;
  1381. )",
  1382. Lang_CXX);
  1383. ASSERT_EQ(2u, DeclCounter<CXXRecordDecl>().match(
  1384. ToTU, cxxRecordDecl(unless(isImplicit()))));
  1385. Decl *FromTU = getTuDecl(
  1386. R"(
  1387. struct B {
  1388. void f();
  1389. };
  1390. )",
  1391. Lang_CXX, "input1.cc");
  1392. auto *FromD = FirstDeclMatcher<CXXRecordDecl>().match(
  1393. FromTU, cxxRecordDecl(hasName("B")));
  1394. Import(FromD, Lang_CXX);
  1395. EXPECT_EQ(2u, DeclCounter<CXXRecordDecl>().match(
  1396. ToTU, cxxRecordDecl(unless(isImplicit()))));
  1397. }
  1398. static void CompareSourceLocs(FullSourceLoc Loc1, FullSourceLoc Loc2) {
  1399. EXPECT_EQ(Loc1.getExpansionLineNumber(), Loc2.getExpansionLineNumber());
  1400. EXPECT_EQ(Loc1.getExpansionColumnNumber(), Loc2.getExpansionColumnNumber());
  1401. EXPECT_EQ(Loc1.getSpellingLineNumber(), Loc2.getSpellingLineNumber());
  1402. EXPECT_EQ(Loc1.getSpellingColumnNumber(), Loc2.getSpellingColumnNumber());
  1403. }
  1404. static void CompareSourceRanges(SourceRange Range1, SourceRange Range2,
  1405. SourceManager &SM1, SourceManager &SM2) {
  1406. CompareSourceLocs(FullSourceLoc{ Range1.getBegin(), SM1 },
  1407. FullSourceLoc{ Range2.getBegin(), SM2 });
  1408. CompareSourceLocs(FullSourceLoc{ Range1.getEnd(), SM1 },
  1409. FullSourceLoc{ Range2.getEnd(), SM2 });
  1410. }
  1411. TEST_P(ASTImporterTestBase, ImportSourceLocs) {
  1412. Decl *FromTU = getTuDecl(
  1413. R"(
  1414. #define MFOO(arg) arg = arg + 1
  1415. void foo() {
  1416. int a = 5;
  1417. MFOO(a);
  1418. }
  1419. )",
  1420. Lang_CXX);
  1421. auto FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, functionDecl());
  1422. auto ToD = Import(FromD, Lang_CXX);
  1423. auto ToLHS = LastDeclMatcher<DeclRefExpr>().match(ToD, declRefExpr());
  1424. auto FromLHS = LastDeclMatcher<DeclRefExpr>().match(FromTU, declRefExpr());
  1425. auto ToRHS = LastDeclMatcher<IntegerLiteral>().match(ToD, integerLiteral());
  1426. auto FromRHS =
  1427. LastDeclMatcher<IntegerLiteral>().match(FromTU, integerLiteral());
  1428. SourceManager &ToSM = ToAST->getASTContext().getSourceManager();
  1429. SourceManager &FromSM = FromD->getASTContext().getSourceManager();
  1430. CompareSourceRanges(ToD->getSourceRange(), FromD->getSourceRange(), ToSM,
  1431. FromSM);
  1432. CompareSourceRanges(ToLHS->getSourceRange(), FromLHS->getSourceRange(), ToSM,
  1433. FromSM);
  1434. CompareSourceRanges(ToRHS->getSourceRange(), FromRHS->getSourceRange(), ToSM,
  1435. FromSM);
  1436. }
  1437. TEST_P(ASTImporterTestBase, ImportNestedMacro) {
  1438. Decl *FromTU = getTuDecl(
  1439. R"(
  1440. #define FUNC_INT void declToImport
  1441. #define FUNC FUNC_INT
  1442. FUNC(int a);
  1443. )",
  1444. Lang_CXX);
  1445. auto FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, functionDecl());
  1446. auto ToD = Import(FromD, Lang_CXX);
  1447. SourceManager &ToSM = ToAST->getASTContext().getSourceManager();
  1448. SourceManager &FromSM = FromD->getASTContext().getSourceManager();
  1449. CompareSourceRanges(ToD->getSourceRange(), FromD->getSourceRange(), ToSM,
  1450. FromSM);
  1451. }
  1452. TEST_P(
  1453. ASTImporterTestBase,
  1454. ImportDefinitionOfClassTemplateSpecIfThereIsAnExistingFwdDeclAndDefinition)
  1455. {
  1456. Decl *ToTU = getToTuDecl(
  1457. R"(
  1458. template <typename T>
  1459. struct B;
  1460. template <>
  1461. struct B<int> {};
  1462. template <>
  1463. struct B<int>;
  1464. )",
  1465. Lang_CXX);
  1466. // We should have only one definition.
  1467. ASSERT_EQ(1u, DeclCounterWithPredicate<ClassTemplateSpecializationDecl>(
  1468. [](const ClassTemplateSpecializationDecl *T) {
  1469. return T->isThisDeclarationADefinition();
  1470. })
  1471. .match(ToTU, classTemplateSpecializationDecl()));
  1472. Decl *FromTU = getTuDecl(
  1473. R"(
  1474. template <typename T>
  1475. struct B;
  1476. template <>
  1477. struct B<int> {};
  1478. )",
  1479. Lang_CXX, "input1.cc");
  1480. auto *FromD = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  1481. FromTU, classTemplateSpecializationDecl(hasName("B")));
  1482. Import(FromD, Lang_CXX);
  1483. // We should have only one definition.
  1484. EXPECT_EQ(1u, DeclCounterWithPredicate<ClassTemplateSpecializationDecl>(
  1485. [](const ClassTemplateSpecializationDecl *T) {
  1486. return T->isThisDeclarationADefinition();
  1487. })
  1488. .match(ToTU, classTemplateSpecializationDecl()));
  1489. }
  1490. TEST_P(ASTImporterTestBase, ObjectsWithUnnamedStructType) {
  1491. Decl *FromTU = getTuDecl(
  1492. R"(
  1493. struct { int a; int b; } object0 = { 2, 3 };
  1494. struct { int x; int y; int z; } object1;
  1495. )",
  1496. Lang_CXX, "input0.cc");
  1497. auto getRecordDecl = [](VarDecl *VD) {
  1498. auto *ET = cast<ElaboratedType>(VD->getType().getTypePtr());
  1499. return cast<RecordType>(ET->getNamedType().getTypePtr())->getDecl();
  1500. };
  1501. auto *Obj0 =
  1502. FirstDeclMatcher<VarDecl>().match(FromTU, varDecl(hasName("object0")));
  1503. auto *From0 = getRecordDecl(Obj0);
  1504. auto *Obj1 =
  1505. FirstDeclMatcher<VarDecl>().match(FromTU, varDecl(hasName("object1")));
  1506. auto *From1 = getRecordDecl(Obj1);
  1507. auto *To0 = Import(From0, Lang_CXX);
  1508. auto *To1 = Import(From1, Lang_CXX);
  1509. EXPECT_TRUE(To0);
  1510. EXPECT_TRUE(To1);
  1511. EXPECT_NE(To0, To1);
  1512. EXPECT_NE(To0->getCanonicalDecl(), To1->getCanonicalDecl());
  1513. }
  1514. TEST_P(ASTImporterTestBase, ImportDoesUpdateUsedFlag) {
  1515. auto Pattern = varDecl(hasName("x"));
  1516. VarDecl *Imported1;
  1517. {
  1518. Decl *FromTU = getTuDecl("extern int x;", Lang_CXX, "input0.cc");
  1519. auto *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern);
  1520. Imported1 = cast<VarDecl>(Import(FromD, Lang_CXX));
  1521. }
  1522. VarDecl *Imported2;
  1523. {
  1524. Decl *FromTU = getTuDecl("int x;", Lang_CXX, "input1.cc");
  1525. auto *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern);
  1526. Imported2 = cast<VarDecl>(Import(FromD, Lang_CXX));
  1527. }
  1528. EXPECT_EQ(Imported1->getCanonicalDecl(), Imported2->getCanonicalDecl());
  1529. EXPECT_FALSE(Imported2->isUsed(false));
  1530. {
  1531. Decl *FromTU =
  1532. getTuDecl("extern int x; int f() { return x; }", Lang_CXX, "input2.cc");
  1533. auto *FromD =
  1534. FirstDeclMatcher<FunctionDecl>().match(FromTU, functionDecl());
  1535. Import(FromD, Lang_CXX);
  1536. }
  1537. EXPECT_TRUE(Imported2->isUsed(false));
  1538. }
  1539. TEST_P(ASTImporterTestBase, ReimportWithUsedFlag) {
  1540. auto Pattern = varDecl(hasName("x"));
  1541. Decl *FromTU = getTuDecl("int x;", Lang_CXX, "input0.cc");
  1542. auto *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern);
  1543. auto *Imported1 = cast<VarDecl>(Import(FromD, Lang_CXX));
  1544. ASSERT_FALSE(Imported1->isUsed(false));
  1545. FromD->setIsUsed();
  1546. auto *Imported2 = cast<VarDecl>(Import(FromD, Lang_CXX));
  1547. EXPECT_EQ(Imported1, Imported2);
  1548. EXPECT_TRUE(Imported2->isUsed(false));
  1549. }
  1550. struct ImportFunctions : ASTImporterTestBase {};
  1551. TEST_P(ImportFunctions,
  1552. DefinitionShouldBeImportedAsDefintionWhenThereIsAPrototype) {
  1553. Decl *FromTU = getTuDecl("void f(); void f() {}", Lang_CXX);
  1554. auto Pattern = functionDecl(hasName("f"));
  1555. FunctionDecl *FromD = // Definition
  1556. LastDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1557. Decl *ImportedD = Import(FromD, Lang_CXX);
  1558. Decl *ToTU = ImportedD->getTranslationUnitDecl();
  1559. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1560. EXPECT_TRUE(cast<FunctionDecl>(ImportedD)->doesThisDeclarationHaveABody());
  1561. }
  1562. TEST_P(ImportFunctions, DefinitionShouldBeImportedAsADefinition) {
  1563. Decl *FromTU = getTuDecl("void f() {}", Lang_CXX);
  1564. auto Pattern = functionDecl(hasName("f"));
  1565. FunctionDecl *FromD =
  1566. FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1567. Decl *ImportedD = Import(FromD, Lang_CXX);
  1568. Decl *ToTU = ImportedD->getTranslationUnitDecl();
  1569. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 1u);
  1570. EXPECT_TRUE(cast<FunctionDecl>(ImportedD)->doesThisDeclarationHaveABody());
  1571. }
  1572. TEST_P(ImportFunctions, ImportPrototypeOfRecursiveFunction) {
  1573. Decl *FromTU = getTuDecl("void f(); void f() { f(); }", Lang_CXX);
  1574. auto Pattern = functionDecl(hasName("f"));
  1575. auto *From =
  1576. FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern); // Proto
  1577. Decl *ImportedD = Import(From, Lang_CXX);
  1578. Decl *ToTU = ImportedD->getTranslationUnitDecl();
  1579. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1580. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1581. auto *To1 = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1582. EXPECT_TRUE(ImportedD == To0);
  1583. EXPECT_FALSE(To0->doesThisDeclarationHaveABody());
  1584. EXPECT_TRUE(To1->doesThisDeclarationHaveABody());
  1585. EXPECT_EQ(To1->getPreviousDecl(), To0);
  1586. }
  1587. TEST_P(ImportFunctions, ImportDefinitionOfRecursiveFunction) {
  1588. Decl *FromTU = getTuDecl("void f(); void f() { f(); }", Lang_CXX);
  1589. auto Pattern = functionDecl(hasName("f"));
  1590. auto *From =
  1591. LastDeclMatcher<FunctionDecl>().match(FromTU, Pattern); // Def
  1592. Decl *ImportedD = Import(From, Lang_CXX);
  1593. Decl *ToTU = ImportedD->getTranslationUnitDecl();
  1594. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1595. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1596. auto *To1 = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1597. EXPECT_TRUE(ImportedD == To1);
  1598. EXPECT_FALSE(To0->doesThisDeclarationHaveABody());
  1599. EXPECT_TRUE(To1->doesThisDeclarationHaveABody());
  1600. EXPECT_EQ(To1->getPreviousDecl(), To0);
  1601. }
  1602. TEST_P(ImportFunctions, ImportPrototypes) {
  1603. auto Pattern = functionDecl(hasName("f"));
  1604. Decl *ImportedD;
  1605. {
  1606. Decl *FromTU = getTuDecl("void f();", Lang_CXX, "input0.cc");
  1607. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1608. ImportedD = Import(FromD, Lang_CXX);
  1609. }
  1610. {
  1611. Decl *FromTU = getTuDecl("void f();", Lang_CXX, "input1.cc");
  1612. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1613. Import(FromD, Lang_CXX);
  1614. }
  1615. Decl *ToTU = ImportedD->getTranslationUnitDecl();
  1616. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1617. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1618. auto *To1 = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1619. EXPECT_TRUE(ImportedD == To0);
  1620. EXPECT_FALSE(To0->doesThisDeclarationHaveABody());
  1621. EXPECT_FALSE(To1->doesThisDeclarationHaveABody());
  1622. EXPECT_EQ(To1->getPreviousDecl(), To0);
  1623. }
  1624. TEST_P(ImportFunctions, ImportDefinitions) {
  1625. auto Pattern = functionDecl(hasName("f"));
  1626. Decl *ImportedD;
  1627. {
  1628. Decl *FromTU = getTuDecl("void f(){}", Lang_CXX, "input0.cc");
  1629. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1630. ImportedD = Import(FromD, Lang_CXX);
  1631. }
  1632. {
  1633. Decl *FromTU = getTuDecl("void f(){};", Lang_CXX, "input1.cc");
  1634. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1635. Import(FromD, Lang_CXX);
  1636. }
  1637. Decl *ToTU = ImportedD->getTranslationUnitDecl();
  1638. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 1u);
  1639. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1640. EXPECT_TRUE(ImportedD == To0);
  1641. EXPECT_TRUE(To0->doesThisDeclarationHaveABody());
  1642. }
  1643. TEST_P(ImportFunctions, ImportDefinitionThenPrototype) {
  1644. auto Pattern = functionDecl(hasName("f"));
  1645. Decl *ImportedD;
  1646. {
  1647. Decl *FromTU = getTuDecl("void f(){}", Lang_CXX, "input0.cc");
  1648. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1649. ImportedD = Import(FromD, Lang_CXX);
  1650. }
  1651. {
  1652. Decl *FromTU = getTuDecl("void f();", Lang_CXX, "input1.cc");
  1653. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1654. Import(FromD, Lang_CXX);
  1655. }
  1656. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1657. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1658. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1659. auto *To1 = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1660. EXPECT_TRUE(ImportedD == To0);
  1661. EXPECT_TRUE(To0->doesThisDeclarationHaveABody());
  1662. EXPECT_FALSE(To1->doesThisDeclarationHaveABody());
  1663. EXPECT_EQ(To1->getPreviousDecl(), To0);
  1664. }
  1665. TEST_P(ImportFunctions, ImportPrototypeThenDefinition) {
  1666. auto Pattern = functionDecl(hasName("f"));
  1667. {
  1668. Decl *FromTU = getTuDecl("void f();", Lang_CXX, "input0.cc");
  1669. FunctionDecl *FromD =
  1670. FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1671. Import(FromD, Lang_CXX);
  1672. }
  1673. {
  1674. Decl *FromTU = getTuDecl("void f(){}", Lang_CXX, "input1.cc");
  1675. FunctionDecl *FromD =
  1676. FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1677. Import(FromD, Lang_CXX);
  1678. }
  1679. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1680. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1681. FunctionDecl *ProtoD = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1682. EXPECT_FALSE(ProtoD->doesThisDeclarationHaveABody());
  1683. FunctionDecl *DefinitionD =
  1684. LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1685. EXPECT_TRUE(DefinitionD->doesThisDeclarationHaveABody());
  1686. EXPECT_EQ(DefinitionD->getPreviousDecl(), ProtoD);
  1687. }
  1688. TEST_P(ImportFunctions, ImportPrototypeThenProtoAndDefinition) {
  1689. auto Pattern = functionDecl(hasName("f"));
  1690. {
  1691. Decl *FromTU = getTuDecl("void f();", Lang_CXX, "input0.cc");
  1692. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1693. Import(FromD, Lang_CXX);
  1694. }
  1695. {
  1696. Decl *FromTU = getTuDecl("void f(); void f(){}", Lang_CXX, "input1.cc");
  1697. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1698. Import(FromD, Lang_CXX);
  1699. }
  1700. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1701. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 3u);
  1702. FunctionDecl *ProtoD = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1703. EXPECT_FALSE(ProtoD->doesThisDeclarationHaveABody());
  1704. FunctionDecl *DefinitionD =
  1705. LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1706. EXPECT_TRUE(DefinitionD->doesThisDeclarationHaveABody());
  1707. EXPECT_TRUE(DefinitionD->getPreviousDecl());
  1708. EXPECT_FALSE(DefinitionD->getPreviousDecl()->doesThisDeclarationHaveABody());
  1709. EXPECT_EQ(DefinitionD->getPreviousDecl()->getPreviousDecl(), ProtoD);
  1710. }
  1711. TEST_P(ImportFunctions, OverriddenMethodsShouldBeImported) {
  1712. auto Code =
  1713. R"(
  1714. struct B { virtual void f(); };
  1715. void B::f() {}
  1716. struct D : B { void f(); };
  1717. )";
  1718. auto Pattern =
  1719. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("D"))));
  1720. Decl *FromTU = getTuDecl(Code, Lang_CXX);
  1721. CXXMethodDecl *Proto =
  1722. FirstDeclMatcher<CXXMethodDecl>().match(FromTU, Pattern);
  1723. ASSERT_EQ(Proto->size_overridden_methods(), 1u);
  1724. CXXMethodDecl *To = cast<CXXMethodDecl>(Import(Proto, Lang_CXX));
  1725. EXPECT_EQ(To->size_overridden_methods(), 1u);
  1726. }
  1727. TEST_P(ImportFunctions, VirtualFlagShouldBePreservedWhenImportingPrototype) {
  1728. auto Code =
  1729. R"(
  1730. struct B { virtual void f(); };
  1731. void B::f() {}
  1732. )";
  1733. auto Pattern =
  1734. cxxMethodDecl(hasName("f"), hasParent(cxxRecordDecl(hasName("B"))));
  1735. Decl *FromTU = getTuDecl(Code, Lang_CXX);
  1736. CXXMethodDecl *Proto =
  1737. FirstDeclMatcher<CXXMethodDecl>().match(FromTU, Pattern);
  1738. CXXMethodDecl *Def = LastDeclMatcher<CXXMethodDecl>().match(FromTU, Pattern);
  1739. ASSERT_TRUE(Proto->isVirtual());
  1740. ASSERT_TRUE(Def->isVirtual());
  1741. CXXMethodDecl *To = cast<CXXMethodDecl>(Import(Proto, Lang_CXX));
  1742. EXPECT_TRUE(To->isVirtual());
  1743. }
  1744. TEST_P(ImportFunctions,
  1745. ImportDefinitionIfThereIsAnExistingDefinitionAndFwdDecl) {
  1746. Decl *ToTU = getToTuDecl(
  1747. R"(
  1748. void f() {}
  1749. void f();
  1750. )",
  1751. Lang_CXX);
  1752. ASSERT_EQ(1u,
  1753. DeclCounterWithPredicate<FunctionDecl>([](const FunctionDecl *FD) {
  1754. return FD->doesThisDeclarationHaveABody();
  1755. }).match(ToTU, functionDecl()));
  1756. Decl *FromTU = getTuDecl("void f() {}", Lang_CXX, "input0.cc");
  1757. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, functionDecl());
  1758. Import(FromD, Lang_CXX);
  1759. EXPECT_EQ(1u,
  1760. DeclCounterWithPredicate<FunctionDecl>([](const FunctionDecl *FD) {
  1761. return FD->doesThisDeclarationHaveABody();
  1762. }).match(ToTU, functionDecl()));
  1763. }
  1764. struct ImportFriendFunctions : ImportFunctions {};
  1765. TEST_P(ImportFriendFunctions, ImportFriendFunctionRedeclChainProto) {
  1766. auto Pattern = functionDecl(hasName("f"));
  1767. Decl *FromTU = getTuDecl("struct X { friend void f(); };"
  1768. "void f();",
  1769. Lang_CXX,
  1770. "input0.cc");
  1771. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1772. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  1773. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1774. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1775. EXPECT_FALSE(ImportedD->doesThisDeclarationHaveABody());
  1776. auto *ToFD = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1777. EXPECT_FALSE(ToFD->doesThisDeclarationHaveABody());
  1778. EXPECT_EQ(ToFD->getPreviousDecl(), ImportedD);
  1779. }
  1780. TEST_P(ImportFriendFunctions,
  1781. ImportFriendFunctionRedeclChainProto_OutOfClassProtoFirst) {
  1782. auto Pattern = functionDecl(hasName("f"));
  1783. Decl *FromTU = getTuDecl("void f();"
  1784. "struct X { friend void f(); };",
  1785. Lang_CXX, "input0.cc");
  1786. auto FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1787. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  1788. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1789. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1790. EXPECT_FALSE(ImportedD->doesThisDeclarationHaveABody());
  1791. auto *ToFD = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1792. EXPECT_FALSE(ToFD->doesThisDeclarationHaveABody());
  1793. EXPECT_EQ(ToFD->getPreviousDecl(), ImportedD);
  1794. }
  1795. TEST_P(ImportFriendFunctions, ImportFriendFunctionRedeclChainDef) {
  1796. auto Pattern = functionDecl(hasName("f"));
  1797. Decl *FromTU = getTuDecl("struct X { friend void f(){} };"
  1798. "void f();",
  1799. Lang_CXX,
  1800. "input0.cc");
  1801. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1802. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  1803. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1804. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1805. EXPECT_TRUE(ImportedD->doesThisDeclarationHaveABody());
  1806. auto *ToFD = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1807. EXPECT_FALSE(ToFD->doesThisDeclarationHaveABody());
  1808. EXPECT_EQ(ToFD->getPreviousDecl(), ImportedD);
  1809. }
  1810. TEST_P(ImportFriendFunctions,
  1811. ImportFriendFunctionRedeclChainDef_OutOfClassDef) {
  1812. auto Pattern = functionDecl(hasName("f"));
  1813. Decl *FromTU = getTuDecl("struct X { friend void f(); };"
  1814. "void f(){}",
  1815. Lang_CXX, "input0.cc");
  1816. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1817. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  1818. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1819. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1820. EXPECT_FALSE(ImportedD->doesThisDeclarationHaveABody());
  1821. auto *ToFD = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  1822. EXPECT_TRUE(ToFD->doesThisDeclarationHaveABody());
  1823. EXPECT_EQ(ToFD->getPreviousDecl(), ImportedD);
  1824. }
  1825. // Disabled temporarily, because the new structural equivalence check
  1826. // (https://reviews.llvm.org/D48628) breaks it.
  1827. // PreviousDecl is not set because there is no structural match.
  1828. // FIXME Enable!
  1829. TEST_P(ImportFriendFunctions,
  1830. DISABLED_ImportFriendFunctionRedeclChainDefWithClass) {
  1831. auto Pattern = functionDecl(hasName("f"));
  1832. Decl *FromTU = getTuDecl(
  1833. R"(
  1834. class X;
  1835. void f(X *x){}
  1836. class X{
  1837. friend void f(X *x);
  1838. };
  1839. )",
  1840. Lang_CXX, "input0.cc");
  1841. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1842. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  1843. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1844. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1845. EXPECT_TRUE(ImportedD->doesThisDeclarationHaveABody());
  1846. auto *InClassFD = cast<FunctionDecl>(FirstDeclMatcher<FriendDecl>()
  1847. .match(ToTU, friendDecl())
  1848. ->getFriendDecl());
  1849. EXPECT_FALSE(InClassFD->doesThisDeclarationHaveABody());
  1850. EXPECT_EQ(InClassFD->getPreviousDecl(), ImportedD);
  1851. // The parameters must refer the same type
  1852. EXPECT_EQ((*InClassFD->param_begin())->getOriginalType(),
  1853. (*ImportedD->param_begin())->getOriginalType());
  1854. }
  1855. // Disabled temporarily, because the new structural equivalence check
  1856. // (https://reviews.llvm.org/D48628) breaks it.
  1857. // PreviousDecl is not set because there is no structural match.
  1858. // FIXME Enable!
  1859. TEST_P(ImportFriendFunctions,
  1860. DISABLED_ImportFriendFunctionRedeclChainDefWithClass_ImportTheProto) {
  1861. auto Pattern = functionDecl(hasName("f"));
  1862. Decl *FromTU = getTuDecl(
  1863. R"(
  1864. class X;
  1865. void f(X *x){}
  1866. class X{
  1867. friend void f(X *x);
  1868. };
  1869. )",
  1870. Lang_CXX, "input0.cc");
  1871. auto *FromD = LastDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1872. auto *ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  1873. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1874. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1875. EXPECT_FALSE(ImportedD->doesThisDeclarationHaveABody());
  1876. auto *OutOfClassFD = FirstDeclMatcher<FunctionDecl>().match(
  1877. ToTU, functionDecl(unless(hasParent(friendDecl()))));
  1878. EXPECT_TRUE(OutOfClassFD->doesThisDeclarationHaveABody());
  1879. EXPECT_EQ(ImportedD->getPreviousDecl(), OutOfClassFD);
  1880. // The parameters must refer the same type
  1881. EXPECT_EQ((*OutOfClassFD->param_begin())->getOriginalType(),
  1882. (*ImportedD->param_begin())->getOriginalType());
  1883. }
  1884. TEST_P(ImportFriendFunctions, ImportFriendFunctionFromMultipleTU) {
  1885. auto Pattern = functionDecl(hasName("f"));
  1886. FunctionDecl *ImportedD;
  1887. {
  1888. Decl *FromTU =
  1889. getTuDecl("struct X { friend void f(){} };", Lang_CXX, "input0.cc");
  1890. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1891. ImportedD = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  1892. }
  1893. FunctionDecl *ImportedD1;
  1894. {
  1895. Decl *FromTU = getTuDecl("void f();", Lang_CXX, "input1.cc");
  1896. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  1897. ImportedD1 = cast<FunctionDecl>(Import(FromD, Lang_CXX));
  1898. }
  1899. Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  1900. ASSERT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  1901. EXPECT_TRUE(ImportedD->doesThisDeclarationHaveABody());
  1902. EXPECT_FALSE(ImportedD1->doesThisDeclarationHaveABody());
  1903. EXPECT_EQ(ImportedD1->getPreviousDecl(), ImportedD);
  1904. }
  1905. AST_MATCHER_P(TagDecl, hasTypedefForAnonDecl, Matcher<TypedefNameDecl>,
  1906. InnerMatcher) {
  1907. if (auto *Typedef = Node.getTypedefNameForAnonDecl())
  1908. return InnerMatcher.matches(*Typedef, Finder, Builder);
  1909. return false;
  1910. }
  1911. TEST_P(ImportDecl, ImportEnumSequential) {
  1912. CodeFiles Samples{{"main.c",
  1913. {"void foo();"
  1914. "void moo();"
  1915. "int main() { foo(); moo(); }",
  1916. Lang_C}},
  1917. {"foo.c",
  1918. {"typedef enum { THING_VALUE } thing_t;"
  1919. "void conflict(thing_t type);"
  1920. "void foo() { (void)THING_VALUE; }"
  1921. "void conflict(thing_t type) {}",
  1922. Lang_C}},
  1923. {"moo.c",
  1924. {"typedef enum { THING_VALUE } thing_t;"
  1925. "void conflict(thing_t type);"
  1926. "void moo() { conflict(THING_VALUE); }",
  1927. Lang_C}}};
  1928. auto VerificationMatcher =
  1929. enumDecl(has(enumConstantDecl(hasName("THING_VALUE"))),
  1930. hasTypedefForAnonDecl(hasName("thing_t")));
  1931. ImportAction ImportFoo{"foo.c", "main.c", functionDecl(hasName("foo"))},
  1932. ImportMoo{"moo.c", "main.c", functionDecl(hasName("moo"))};
  1933. testImportSequence(
  1934. Samples, {ImportFoo, ImportMoo}, // "foo", them "moo".
  1935. // Just check that there is only one enum decl in the result AST.
  1936. "main.c", enumDecl(), VerificationMatcher);
  1937. // For different import order, result should be the same.
  1938. testImportSequence(
  1939. Samples, {ImportMoo, ImportFoo}, // "moo", them "foo".
  1940. // Check that there is only one enum decl in the result AST.
  1941. "main.c", enumDecl(), VerificationMatcher);
  1942. }
  1943. const internal::VariadicDynCastAllOfMatcher<Expr, DependentScopeDeclRefExpr>
  1944. dependentScopeDeclRefExpr;
  1945. TEST_P(ImportExpr, DependentScopeDeclRefExpr) {
  1946. MatchVerifier<Decl> Verifier;
  1947. testImport("template <typename T> struct S { static T foo; };"
  1948. "template <typename T> void declToImport() {"
  1949. " (void) S<T>::foo;"
  1950. "}"
  1951. "void instantiate() { declToImport<int>(); }"
  1952. "template <typename T> T S<T>::foo;",
  1953. Lang_CXX11, "", Lang_CXX11, Verifier,
  1954. functionTemplateDecl(has(functionDecl(has(compoundStmt(
  1955. has(cStyleCastExpr(has(dependentScopeDeclRefExpr())))))))));
  1956. testImport("template <typename T> struct S {"
  1957. "template<typename S> static void foo(){};"
  1958. "};"
  1959. "template <typename T> void declToImport() {"
  1960. " S<T>::template foo<T>();"
  1961. "}"
  1962. "void instantiate() { declToImport<int>(); }",
  1963. Lang_CXX11, "", Lang_CXX11, Verifier,
  1964. functionTemplateDecl(has(functionDecl(has(compoundStmt(
  1965. has(callExpr(has(dependentScopeDeclRefExpr())))))))));
  1966. }
  1967. const internal::VariadicDynCastAllOfMatcher<Type, DependentNameType>
  1968. dependentNameType;
  1969. TEST_P(ImportExpr, DependentNameType) {
  1970. MatchVerifier<Decl> Verifier;
  1971. testImport("template <typename T> struct declToImport {"
  1972. " typedef typename T::type dependent_name;"
  1973. "};",
  1974. Lang_CXX11, "", Lang_CXX11, Verifier,
  1975. classTemplateDecl(has(
  1976. cxxRecordDecl(has(typedefDecl(has(dependentNameType())))))));
  1977. }
  1978. const internal::VariadicDynCastAllOfMatcher<Expr, UnresolvedMemberExpr>
  1979. unresolvedMemberExpr;
  1980. TEST_P(ImportExpr, UnresolvedMemberExpr) {
  1981. MatchVerifier<Decl> Verifier;
  1982. testImport("struct S { template <typename T> void mem(); };"
  1983. "template <typename U> void declToImport() {"
  1984. " S s;"
  1985. " s.mem<U>();"
  1986. "}"
  1987. "void instantiate() { declToImport<int>(); }",
  1988. Lang_CXX11, "", Lang_CXX11, Verifier,
  1989. functionTemplateDecl(has(functionDecl(has(
  1990. compoundStmt(has(callExpr(has(unresolvedMemberExpr())))))))));
  1991. }
  1992. TEST_P(ASTImporterTestBase, ImportOfEquivalentRecord) {
  1993. Decl *ToR1;
  1994. {
  1995. Decl *FromTU = getTuDecl(
  1996. "struct A { };", Lang_CXX, "input0.cc");
  1997. auto *FromR = FirstDeclMatcher<CXXRecordDecl>().match(
  1998. FromTU, cxxRecordDecl(hasName("A")));
  1999. ToR1 = Import(FromR, Lang_CXX);
  2000. }
  2001. Decl *ToR2;
  2002. {
  2003. Decl *FromTU = getTuDecl(
  2004. "struct A { };", Lang_CXX, "input1.cc");
  2005. auto *FromR = FirstDeclMatcher<CXXRecordDecl>().match(
  2006. FromTU, cxxRecordDecl(hasName("A")));
  2007. ToR2 = Import(FromR, Lang_CXX);
  2008. }
  2009. EXPECT_EQ(ToR1, ToR2);
  2010. }
  2011. TEST_P(ASTImporterTestBase, ImportOfNonEquivalentRecord) {
  2012. Decl *ToR1;
  2013. {
  2014. Decl *FromTU = getTuDecl(
  2015. "struct A { int x; };", Lang_CXX, "input0.cc");
  2016. auto *FromR = FirstDeclMatcher<CXXRecordDecl>().match(
  2017. FromTU, cxxRecordDecl(hasName("A")));
  2018. ToR1 = Import(FromR, Lang_CXX);
  2019. }
  2020. Decl *ToR2;
  2021. {
  2022. Decl *FromTU = getTuDecl(
  2023. "struct A { unsigned x; };", Lang_CXX, "input1.cc");
  2024. auto *FromR = FirstDeclMatcher<CXXRecordDecl>().match(
  2025. FromTU, cxxRecordDecl(hasName("A")));
  2026. ToR2 = Import(FromR, Lang_CXX);
  2027. }
  2028. EXPECT_NE(ToR1, ToR2);
  2029. }
  2030. TEST_P(ASTImporterTestBase, ImportOfEquivalentField) {
  2031. Decl *ToF1;
  2032. {
  2033. Decl *FromTU = getTuDecl(
  2034. "struct A { int x; };", Lang_CXX, "input0.cc");
  2035. auto *FromF = FirstDeclMatcher<FieldDecl>().match(
  2036. FromTU, fieldDecl(hasName("x")));
  2037. ToF1 = Import(FromF, Lang_CXX);
  2038. }
  2039. Decl *ToF2;
  2040. {
  2041. Decl *FromTU = getTuDecl(
  2042. "struct A { int x; };", Lang_CXX, "input1.cc");
  2043. auto *FromF = FirstDeclMatcher<FieldDecl>().match(
  2044. FromTU, fieldDecl(hasName("x")));
  2045. ToF2 = Import(FromF, Lang_CXX);
  2046. }
  2047. EXPECT_EQ(ToF1, ToF2);
  2048. }
  2049. TEST_P(ASTImporterTestBase, ImportOfNonEquivalentField) {
  2050. Decl *ToF1;
  2051. {
  2052. Decl *FromTU = getTuDecl(
  2053. "struct A { int x; };", Lang_CXX, "input0.cc");
  2054. auto *FromF = FirstDeclMatcher<FieldDecl>().match(
  2055. FromTU, fieldDecl(hasName("x")));
  2056. ToF1 = Import(FromF, Lang_CXX);
  2057. }
  2058. Decl *ToF2;
  2059. {
  2060. Decl *FromTU = getTuDecl(
  2061. "struct A { unsigned x; };", Lang_CXX, "input1.cc");
  2062. auto *FromF = FirstDeclMatcher<FieldDecl>().match(
  2063. FromTU, fieldDecl(hasName("x")));
  2064. ToF2 = Import(FromF, Lang_CXX);
  2065. }
  2066. EXPECT_NE(ToF1, ToF2);
  2067. }
  2068. TEST_P(ASTImporterTestBase, ImportOfEquivalentMethod) {
  2069. Decl *ToM1;
  2070. {
  2071. Decl *FromTU = getTuDecl(
  2072. "struct A { void x(); }; void A::x() { }", Lang_CXX, "input0.cc");
  2073. auto *FromM = FirstDeclMatcher<FunctionDecl>().match(
  2074. FromTU, functionDecl(hasName("x"), isDefinition()));
  2075. ToM1 = Import(FromM, Lang_CXX);
  2076. }
  2077. Decl *ToM2;
  2078. {
  2079. Decl *FromTU = getTuDecl(
  2080. "struct A { void x(); }; void A::x() { }", Lang_CXX, "input1.cc");
  2081. auto *FromM = FirstDeclMatcher<FunctionDecl>().match(
  2082. FromTU, functionDecl(hasName("x"), isDefinition()));
  2083. ToM2 = Import(FromM, Lang_CXX);
  2084. }
  2085. EXPECT_EQ(ToM1, ToM2);
  2086. }
  2087. TEST_P(ASTImporterTestBase, ImportOfNonEquivalentMethod) {
  2088. Decl *ToM1;
  2089. {
  2090. Decl *FromTU = getTuDecl(
  2091. "struct A { void x(); }; void A::x() { }",
  2092. Lang_CXX, "input0.cc");
  2093. auto *FromM = FirstDeclMatcher<FunctionDecl>().match(
  2094. FromTU, functionDecl(hasName("x"), isDefinition()));
  2095. ToM1 = Import(FromM, Lang_CXX);
  2096. }
  2097. Decl *ToM2;
  2098. {
  2099. Decl *FromTU = getTuDecl(
  2100. "struct A { void x() const; }; void A::x() const { }",
  2101. Lang_CXX, "input1.cc");
  2102. auto *FromM = FirstDeclMatcher<FunctionDecl>().match(
  2103. FromTU, functionDecl(hasName("x"), isDefinition()));
  2104. ToM2 = Import(FromM, Lang_CXX);
  2105. }
  2106. EXPECT_NE(ToM1, ToM2);
  2107. }
  2108. struct DeclContextTest : ASTImporterTestBase {};
  2109. TEST_P(DeclContextTest, removeDeclOfClassTemplateSpecialization) {
  2110. Decl *TU = getTuDecl(
  2111. R"(
  2112. namespace NS {
  2113. template <typename T>
  2114. struct S {};
  2115. template struct S<int>;
  2116. inline namespace INS {
  2117. template <typename T>
  2118. struct S {};
  2119. template struct S<int>;
  2120. }
  2121. }
  2122. )", Lang_CXX11, "input0.cc");
  2123. auto *NS = FirstDeclMatcher<NamespaceDecl>().match(
  2124. TU, namespaceDecl());
  2125. auto *Spec = FirstDeclMatcher<ClassTemplateSpecializationDecl>().match(
  2126. TU, classTemplateSpecializationDecl());
  2127. ASSERT_TRUE(NS->containsDecl(Spec));
  2128. NS->removeDecl(Spec);
  2129. EXPECT_FALSE(NS->containsDecl(Spec));
  2130. }
  2131. struct ImportFunctionTemplateSpecializations : ASTImporterTestBase {};
  2132. TEST_P(ImportFunctionTemplateSpecializations,
  2133. TUshouldNotContainFunctionTemplateImplicitInstantiation) {
  2134. Decl *FromTU = getTuDecl(
  2135. R"(
  2136. template<class T>
  2137. int f() { return 0; }
  2138. void foo() { f<int>(); }
  2139. )",
  2140. Lang_CXX, "input0.cc");
  2141. // Check that the function template instantiation is NOT the child of the TU.
  2142. auto Pattern = translationUnitDecl(
  2143. unless(has(functionDecl(hasName("f"), isTemplateInstantiation()))));
  2144. ASSERT_TRUE(MatchVerifier<Decl>{}.match(FromTU, Pattern));
  2145. auto *Foo = FirstDeclMatcher<FunctionDecl>().match(
  2146. FromTU, functionDecl(hasName("foo")));
  2147. ASSERT_TRUE(Import(Foo, Lang_CXX));
  2148. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2149. EXPECT_TRUE(MatchVerifier<Decl>{}.match(ToTU, Pattern));
  2150. }
  2151. TEST_P(ImportFunctionTemplateSpecializations,
  2152. TUshouldNotContainFunctionTemplateExplicitInstantiation) {
  2153. Decl *FromTU = getTuDecl(
  2154. R"(
  2155. template<class T>
  2156. int f() { return 0; }
  2157. template int f<int>();
  2158. )",
  2159. Lang_CXX, "input0.cc");
  2160. // Check that the function template instantiation is NOT the child of the TU.
  2161. auto Instantiation = functionDecl(hasName("f"), isTemplateInstantiation());
  2162. auto Pattern = translationUnitDecl(unless(has(Instantiation)));
  2163. ASSERT_TRUE(MatchVerifier<Decl>{}.match(FromTU, Pattern));
  2164. ASSERT_TRUE(
  2165. Import(FirstDeclMatcher<Decl>().match(FromTU, Instantiation), Lang_CXX));
  2166. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2167. EXPECT_TRUE(MatchVerifier<Decl>{}.match(ToTU, Pattern));
  2168. }
  2169. TEST_P(ImportFunctionTemplateSpecializations,
  2170. TUshouldContainFunctionTemplateSpecialization) {
  2171. Decl *FromTU = getTuDecl(
  2172. R"(
  2173. template<class T>
  2174. int f() { return 0; }
  2175. template <> int f<int>() { return 4; }
  2176. )",
  2177. Lang_CXX, "input0.cc");
  2178. // Check that the function template specialization is the child of the TU.
  2179. auto Specialization =
  2180. functionDecl(hasName("f"), isExplicitTemplateSpecialization());
  2181. auto Pattern = translationUnitDecl(has(Specialization));
  2182. ASSERT_TRUE(MatchVerifier<Decl>{}.match(FromTU, Pattern));
  2183. ASSERT_TRUE(
  2184. Import(FirstDeclMatcher<Decl>().match(FromTU, Specialization), Lang_CXX));
  2185. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2186. EXPECT_TRUE(MatchVerifier<Decl>{}.match(ToTU, Pattern));
  2187. }
  2188. TEST_P(ImportFunctionTemplateSpecializations,
  2189. FunctionTemplateSpecializationRedeclChain) {
  2190. Decl *FromTU = getTuDecl(
  2191. R"(
  2192. template<class T>
  2193. int f() { return 0; }
  2194. template <> int f<int>() { return 4; }
  2195. )",
  2196. Lang_CXX, "input0.cc");
  2197. auto Spec = functionDecl(hasName("f"), isExplicitTemplateSpecialization(),
  2198. hasParent(translationUnitDecl()));
  2199. auto *FromSpecD = FirstDeclMatcher<Decl>().match(FromTU, Spec);
  2200. {
  2201. auto *TU = FromTU;
  2202. auto *SpecD = FromSpecD;
  2203. auto *TemplateD = FirstDeclMatcher<FunctionTemplateDecl>().match(
  2204. TU, functionTemplateDecl());
  2205. auto *FirstSpecD = *(TemplateD->spec_begin());
  2206. ASSERT_EQ(SpecD, FirstSpecD);
  2207. ASSERT_TRUE(SpecD->getPreviousDecl());
  2208. ASSERT_FALSE(cast<FunctionDecl>(SpecD->getPreviousDecl())
  2209. ->doesThisDeclarationHaveABody());
  2210. }
  2211. ASSERT_TRUE(Import(FromSpecD, Lang_CXX));
  2212. {
  2213. auto *TU = ToAST->getASTContext().getTranslationUnitDecl();
  2214. auto *SpecD = FirstDeclMatcher<Decl>().match(TU, Spec);
  2215. auto *TemplateD = FirstDeclMatcher<FunctionTemplateDecl>().match(
  2216. TU, functionTemplateDecl());
  2217. auto *FirstSpecD = *(TemplateD->spec_begin());
  2218. EXPECT_EQ(SpecD, FirstSpecD);
  2219. ASSERT_TRUE(SpecD->getPreviousDecl());
  2220. EXPECT_FALSE(cast<FunctionDecl>(SpecD->getPreviousDecl())
  2221. ->doesThisDeclarationHaveABody());
  2222. }
  2223. }
  2224. TEST_P(ImportFunctionTemplateSpecializations,
  2225. MatchNumberOfFunctionTemplateSpecializations) {
  2226. Decl *FromTU = getTuDecl(
  2227. R"(
  2228. template <typename T> constexpr int f() { return 0; }
  2229. template <> constexpr int f<int>() { return 4; }
  2230. void foo() {
  2231. static_assert(f<char>() == 0, "");
  2232. static_assert(f<int>() == 4, "");
  2233. }
  2234. )",
  2235. Lang_CXX11, "input0.cc");
  2236. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
  2237. FromTU, functionDecl(hasName("foo")));
  2238. Import(FromD, Lang_CXX11);
  2239. auto *ToTU = ToAST->getASTContext().getTranslationUnitDecl();
  2240. EXPECT_EQ(
  2241. DeclCounter<FunctionDecl>().match(FromTU, functionDecl(hasName("f"))),
  2242. DeclCounter<FunctionDecl>().match(ToTU, functionDecl(hasName("f"))));
  2243. }
  2244. TEST_P(ImportFunctionTemplateSpecializations,
  2245. ImportPrototypes) {
  2246. auto Pattern = functionDecl(hasName("f"), isExplicitTemplateSpecialization());
  2247. auto Code =
  2248. R"(
  2249. // Proto of the primary template.
  2250. template <class T>
  2251. void f();
  2252. // Proto of the specialization.
  2253. template <>
  2254. void f<int>();
  2255. )";
  2256. Decl *ImportedD;
  2257. {
  2258. Decl *FromTU = getTuDecl(Code, Lang_CXX, "input0.cc");
  2259. auto *FromD = LastDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2260. ImportedD = Import(FromD, Lang_CXX);
  2261. }
  2262. {
  2263. Decl *FromTU = getTuDecl(Code, Lang_CXX, "input1.cc");
  2264. auto *FromD = LastDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2265. Import(FromD, Lang_CXX);
  2266. }
  2267. Decl *ToTU = ImportedD->getTranslationUnitDecl();
  2268. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2269. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2270. auto *To1 = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2271. EXPECT_TRUE(ImportedD == To0);
  2272. EXPECT_TRUE(ImportedD != To1);
  2273. EXPECT_FALSE(To0->doesThisDeclarationHaveABody());
  2274. EXPECT_FALSE(To1->doesThisDeclarationHaveABody());
  2275. // Check that they are part of the same redecl chain.
  2276. EXPECT_EQ(To1->getCanonicalDecl(), To0->getCanonicalDecl());
  2277. }
  2278. TEST_P(ImportFunctionTemplateSpecializations, ImportDefinitions) {
  2279. auto Pattern = functionDecl(hasName("f"), isExplicitTemplateSpecialization());
  2280. auto Code =
  2281. R"(
  2282. // Proto of the primary template.
  2283. template <class T>
  2284. void f();
  2285. // Specialization and definition.
  2286. template <>
  2287. void f<int>() {}
  2288. )";
  2289. Decl *ImportedD;
  2290. {
  2291. Decl *FromTU = getTuDecl(Code, Lang_CXX, "input0.cc");
  2292. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2293. ImportedD = Import(FromD, Lang_CXX);
  2294. }
  2295. {
  2296. Decl *FromTU = getTuDecl(Code, Lang_CXX, "input1.cc");
  2297. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2298. Import(FromD, Lang_CXX);
  2299. }
  2300. Decl *ToTU = ImportedD->getTranslationUnitDecl();
  2301. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 1u);
  2302. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2303. EXPECT_TRUE(ImportedD == To0);
  2304. EXPECT_TRUE(To0->doesThisDeclarationHaveABody());
  2305. auto *TemplateD = FirstDeclMatcher<FunctionTemplateDecl>().match(
  2306. ToTU, functionTemplateDecl());
  2307. auto *FirstSpecD = *(TemplateD->spec_begin());
  2308. EXPECT_EQ(FirstSpecD->getCanonicalDecl(), To0->getCanonicalDecl());
  2309. }
  2310. TEST_P(ImportFunctionTemplateSpecializations, PrototypeThenPrototype) {
  2311. auto Pattern = functionDecl(hasName("f"), isExplicitTemplateSpecialization());
  2312. auto Code =
  2313. R"(
  2314. // Proto of the primary template.
  2315. template <class T>
  2316. void f();
  2317. // Specialization proto.
  2318. template <>
  2319. void f<int>();
  2320. // Specialization proto.
  2321. template <>
  2322. void f<int>();
  2323. )";
  2324. Decl *ImportedD;
  2325. {
  2326. Decl *FromTU = getTuDecl(Code, Lang_CXX, "input0.cc");
  2327. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2328. ImportedD = Import(FromD, Lang_CXX);
  2329. }
  2330. Decl *ToTU = ImportedD->getTranslationUnitDecl();
  2331. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2332. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2333. auto *To1 = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2334. EXPECT_TRUE(ImportedD == To0);
  2335. EXPECT_TRUE(ImportedD != To1);
  2336. EXPECT_FALSE(To0->doesThisDeclarationHaveABody());
  2337. EXPECT_FALSE(To1->doesThisDeclarationHaveABody());
  2338. EXPECT_EQ(To1->getPreviousDecl(), To0);
  2339. }
  2340. TEST_P(ImportFunctionTemplateSpecializations, PrototypeThenDefinition) {
  2341. auto Pattern = functionDecl(hasName("f"), isExplicitTemplateSpecialization());
  2342. auto Code =
  2343. R"(
  2344. // Proto of the primary template.
  2345. template <class T>
  2346. void f();
  2347. // Specialization proto.
  2348. template <>
  2349. void f<int>();
  2350. // Specialization definition.
  2351. template <>
  2352. void f<int>() {}
  2353. )";
  2354. Decl *ImportedD;
  2355. {
  2356. Decl *FromTU = getTuDecl(Code, Lang_CXX, "input0.cc");
  2357. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2358. ImportedD = Import(FromD, Lang_CXX);
  2359. }
  2360. Decl *ToTU = ImportedD->getTranslationUnitDecl();
  2361. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2362. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2363. auto *To1 = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2364. EXPECT_TRUE(ImportedD == To0);
  2365. EXPECT_TRUE(ImportedD != To1);
  2366. EXPECT_FALSE(To0->doesThisDeclarationHaveABody());
  2367. EXPECT_TRUE(To1->doesThisDeclarationHaveABody());
  2368. EXPECT_EQ(To1->getPreviousDecl(), To0);
  2369. }
  2370. TEST_P(ImportFunctionTemplateSpecializations, DefinitionThenPrototype) {
  2371. auto Pattern = functionDecl(hasName("f"), isExplicitTemplateSpecialization());
  2372. auto Code =
  2373. R"(
  2374. // Proto of the primary template.
  2375. template <class T>
  2376. void f();
  2377. // Specialization definition.
  2378. template <>
  2379. void f<int>() {}
  2380. // Specialization proto.
  2381. template <>
  2382. void f<int>();
  2383. )";
  2384. Decl *ImportedD;
  2385. {
  2386. Decl *FromTU = getTuDecl(Code, Lang_CXX, "input0.cc");
  2387. auto *FromD = FirstDeclMatcher<FunctionDecl>().match(FromTU, Pattern);
  2388. ImportedD = Import(FromD, Lang_CXX);
  2389. }
  2390. Decl *ToTU = ImportedD->getTranslationUnitDecl();
  2391. EXPECT_EQ(DeclCounter<FunctionDecl>().match(ToTU, Pattern), 2u);
  2392. auto *To0 = FirstDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2393. auto *To1 = LastDeclMatcher<FunctionDecl>().match(ToTU, Pattern);
  2394. EXPECT_TRUE(ImportedD == To0);
  2395. EXPECT_TRUE(ImportedD != To1);
  2396. EXPECT_TRUE(To0->doesThisDeclarationHaveABody());
  2397. EXPECT_FALSE(To1->doesThisDeclarationHaveABody());
  2398. EXPECT_EQ(To1->getPreviousDecl(), To0);
  2399. }
  2400. INSTANTIATE_TEST_CASE_P(ParameterizedTests, DeclContextTest,
  2401. ::testing::Values(ArgVector()), );
  2402. INSTANTIATE_TEST_CASE_P(
  2403. ParameterizedTests, CanonicalRedeclChain,
  2404. ::testing::Values(ArgVector()),);
  2405. auto DefaultTestValuesForRunOptions = ::testing::Values(
  2406. ArgVector(),
  2407. ArgVector{"-fdelayed-template-parsing"},
  2408. ArgVector{"-fms-compatibility"},
  2409. ArgVector{"-fdelayed-template-parsing", "-fms-compatibility"});
  2410. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportExpr,
  2411. DefaultTestValuesForRunOptions, );
  2412. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportType,
  2413. DefaultTestValuesForRunOptions, );
  2414. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportDecl,
  2415. DefaultTestValuesForRunOptions, );
  2416. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterTestBase,
  2417. DefaultTestValuesForRunOptions, );
  2418. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportFunctions,
  2419. DefaultTestValuesForRunOptions, );
  2420. INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportFriendFunctions,
  2421. DefaultTestValuesForRunOptions, );
  2422. INSTANTIATE_TEST_CASE_P(ParameterizedTests,
  2423. ImportFunctionTemplateSpecializations,
  2424. DefaultTestValuesForRunOptions, );
  2425. } // end namespace ast_matchers
  2426. } // end namespace clang