Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 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 | |
| 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/ASTImporter.h" |
| 16 | #include "MatchVerifier.h" |
| 17 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 18 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 19 | #include "clang/Tooling/Tooling.h" |
| 20 | #include "gtest/gtest.h" |
| 21 | |
| 22 | namespace clang { |
| 23 | namespace ast_matchers { |
| 24 | |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 25 | typedef std::vector<std::string> ArgVector; |
| 26 | typedef std::vector<ArgVector> RunOptions; |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 27 | |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 28 | static bool isCXX(Language Lang) { |
| 29 | return Lang == Lang_CXX || Lang == Lang_CXX11; |
| 30 | } |
| 31 | |
| 32 | static RunOptions getRunOptionsForLanguage(Language Lang) { |
| 33 | ArgVector BasicArgs; |
| 34 | // Test with basic arguments. |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 35 | switch (Lang) { |
| 36 | case Lang_C: |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 37 | BasicArgs = {"-x", "c", "-std=c99"}; |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 38 | break; |
| 39 | case Lang_C89: |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 40 | BasicArgs = {"-x", "c", "-std=c89"}; |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 41 | break; |
| 42 | case Lang_CXX: |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 43 | BasicArgs = {"-std=c++98"}; |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 44 | break; |
| 45 | case Lang_CXX11: |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 46 | BasicArgs = {"-std=c++11"}; |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 47 | break; |
| 48 | case Lang_OpenCL: |
| 49 | case Lang_OBJCXX: |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 50 | llvm_unreachable("Not implemented yet!"); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 51 | } |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 52 | |
| 53 | // For C++, test with "-fdelayed-template-parsing" enabled to handle MSVC |
| 54 | // default behaviour. |
| 55 | if (isCXX(Lang)) { |
| 56 | ArgVector ArgsForDelayedTemplateParse = BasicArgs; |
| 57 | ArgsForDelayedTemplateParse.emplace_back("-fdelayed-template-parsing"); |
| 58 | return {BasicArgs, ArgsForDelayedTemplateParse}; |
| 59 | } |
| 60 | |
| 61 | return {BasicArgs}; |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | template<typename NodeType, typename MatcherType> |
| 65 | testing::AssertionResult |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 66 | testImport(const std::string &FromCode, const ArgVector &FromArgs, |
| 67 | const std::string &ToCode, const ArgVector &ToArgs, |
| 68 | MatchVerifier<NodeType> &Verifier, const MatcherType &AMatcher) { |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 69 | const char *const InputFileName = "input.cc"; |
| 70 | const char *const OutputFileName = "output.cc"; |
| 71 | |
| 72 | std::unique_ptr<ASTUnit> |
| 73 | FromAST = tooling::buildASTFromCodeWithArgs( |
| 74 | FromCode, FromArgs, InputFileName), |
| 75 | ToAST = tooling::buildASTFromCodeWithArgs(ToCode, ToArgs, OutputFileName); |
| 76 | |
| 77 | ASTContext &FromCtx = FromAST->getASTContext(), |
| 78 | &ToCtx = ToAST->getASTContext(); |
| 79 | |
| 80 | // Add input.cc to virtual file system so importer can 'find' it |
| 81 | // while importing SourceLocations. |
| 82 | vfs::OverlayFileSystem *OFS = static_cast<vfs::OverlayFileSystem *>( |
| 83 | ToCtx.getSourceManager().getFileManager().getVirtualFileSystem().get()); |
| 84 | vfs::InMemoryFileSystem *MFS = static_cast<vfs::InMemoryFileSystem *>( |
| 85 | OFS->overlays_begin()->get()); |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 86 | MFS->addFile(InputFileName, 0, llvm::MemoryBuffer::getMemBuffer(FromCode)); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 87 | |
| 88 | ASTImporter Importer(ToCtx, ToAST->getFileManager(), |
| 89 | FromCtx, FromAST->getFileManager(), false); |
| 90 | |
| 91 | IdentifierInfo *ImportedII = &FromCtx.Idents.get("declToImport"); |
| 92 | assert(ImportedII && "Declaration with 'declToImport' name" |
| 93 | "should be specified in test!"); |
| 94 | DeclarationName ImportDeclName(ImportedII); |
| 95 | SmallVector<NamedDecl *, 4> FoundDecls; |
| 96 | FromCtx.getTranslationUnitDecl()->localUncachedLookup( |
| 97 | ImportDeclName, FoundDecls); |
| 98 | |
| 99 | if (FoundDecls.size() != 1) |
| 100 | return testing::AssertionFailure() << "Multiple declarations were found!"; |
| 101 | |
| 102 | auto Imported = Importer.Import(*FoundDecls.begin()); |
| 103 | if (!Imported) |
| 104 | return testing::AssertionFailure() << "Import failed, nullptr returned!"; |
| 105 | |
| 106 | // This should dump source locations and assert if some source locations |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 107 | // were not imported. |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 108 | SmallString<1024> ImportChecker; |
| 109 | llvm::raw_svector_ostream ToNothing(ImportChecker); |
| 110 | ToCtx.getTranslationUnitDecl()->print(ToNothing); |
| 111 | |
Gabor Horvath | 480892b | 2017-10-18 09:25:18 +0000 | [diff] [blame] | 112 | // This traverses the AST to catch certain bugs like poorly or not |
| 113 | // implemented subtrees. |
| 114 | Imported->dump(ToNothing); |
| 115 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 116 | return Verifier.match(Imported, AMatcher); |
| 117 | } |
| 118 | |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 119 | template<typename NodeType, typename MatcherType> |
| 120 | void testImport(const std::string &FromCode, Language FromLang, |
| 121 | const std::string &ToCode, Language ToLang, |
| 122 | MatchVerifier<NodeType> &Verifier, |
| 123 | const MatcherType &AMatcher) { |
| 124 | auto RunOptsFrom = getRunOptionsForLanguage(FromLang); |
| 125 | auto RunOptsTo = getRunOptionsForLanguage(ToLang); |
| 126 | for (const auto &FromArgs : RunOptsFrom) |
| 127 | for (const auto &ToArgs : RunOptsTo) |
| 128 | EXPECT_TRUE(testImport(FromCode, FromArgs, ToCode, ToArgs, |
| 129 | Verifier, AMatcher)); |
| 130 | } |
| 131 | |
| 132 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 133 | TEST(ImportExpr, ImportStringLiteral) { |
| 134 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 135 | testImport("void declToImport() { \"foo\"; }", |
| 136 | Lang_CXX, "", Lang_CXX, Verifier, |
| 137 | functionDecl( |
| 138 | hasBody( |
| 139 | compoundStmt( |
| 140 | has( |
| 141 | stringLiteral( |
| 142 | hasType( |
| 143 | asString("const char [4]")))))))); |
| 144 | testImport("void declToImport() { L\"foo\"; }", |
| 145 | Lang_CXX, "", Lang_CXX, Verifier, |
| 146 | functionDecl( |
| 147 | hasBody( |
| 148 | compoundStmt( |
| 149 | has( |
| 150 | stringLiteral( |
| 151 | hasType( |
| 152 | asString("const wchar_t [4]")))))))); |
| 153 | testImport("void declToImport() { \"foo\" \"bar\"; }", |
| 154 | Lang_CXX, "", Lang_CXX, Verifier, |
| 155 | functionDecl( |
| 156 | hasBody( |
| 157 | compoundStmt( |
| 158 | has( |
| 159 | stringLiteral( |
| 160 | hasType( |
| 161 | asString("const char [7]")))))))); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | TEST(ImportExpr, ImportGNUNullExpr) { |
| 165 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 166 | testImport("void declToImport() { __null; }", |
| 167 | Lang_CXX, "", Lang_CXX, Verifier, |
| 168 | functionDecl( |
| 169 | hasBody( |
| 170 | compoundStmt( |
| 171 | has( |
| 172 | gnuNullExpr( |
| 173 | hasType(isInteger()))))))); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | TEST(ImportExpr, ImportCXXNullPtrLiteralExpr) { |
| 177 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 178 | testImport("void declToImport() { nullptr; }", |
| 179 | Lang_CXX11, "", Lang_CXX11, Verifier, |
| 180 | functionDecl( |
| 181 | hasBody( |
| 182 | compoundStmt( |
| 183 | has( |
| 184 | cxxNullPtrLiteralExpr()))))); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
| 188 | TEST(ImportExpr, ImportFloatinglLiteralExpr) { |
| 189 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 190 | testImport("void declToImport() { 1.0; }", |
| 191 | Lang_C, "", Lang_C, Verifier, |
| 192 | functionDecl( |
| 193 | hasBody( |
| 194 | compoundStmt( |
| 195 | has( |
| 196 | floatLiteral( |
| 197 | equals(1.0), |
| 198 | hasType(asString("double")))))))); |
| 199 | testImport("void declToImport() { 1.0e-5f; }", |
| 200 | Lang_C, "", Lang_C, Verifier, |
| 201 | functionDecl( |
| 202 | hasBody( |
| 203 | compoundStmt( |
| 204 | has( |
| 205 | floatLiteral( |
| 206 | equals(1.0e-5f), |
| 207 | hasType(asString("float")))))))); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | TEST(ImportExpr, ImportCompoundLiteralExpr) { |
| 211 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 212 | testImport("void declToImport() {" |
| 213 | " struct s { int x; long y; unsigned z; }; " |
| 214 | " (struct s){ 42, 0L, 1U }; }", |
| 215 | Lang_CXX, "", Lang_CXX, Verifier, |
| 216 | functionDecl( |
| 217 | hasBody( |
| 218 | compoundStmt( |
| 219 | has( |
| 220 | compoundLiteralExpr( |
| 221 | hasType(asString("struct s")), |
| 222 | has(initListExpr( |
| 223 | hasType(asString("struct s")), |
| 224 | has(integerLiteral( |
| 225 | equals(42), hasType(asString("int")))), |
| 226 | has(integerLiteral( |
| 227 | equals(0), hasType(asString("long")))), |
| 228 | has(integerLiteral( |
| 229 | equals(1), |
| 230 | hasType(asString("unsigned int")))) |
| 231 | )))))))); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | TEST(ImportExpr, ImportCXXThisExpr) { |
| 235 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 236 | testImport("class declToImport { void f() { this; } };", |
| 237 | Lang_CXX, "", Lang_CXX, Verifier, |
| 238 | cxxRecordDecl( |
| 239 | hasMethod( |
| 240 | hasBody( |
| 241 | compoundStmt( |
| 242 | has( |
| 243 | cxxThisExpr( |
| 244 | hasType( |
| 245 | asString("class declToImport *"))))))))); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | TEST(ImportExpr, ImportAtomicExpr) { |
| 249 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 250 | testImport("void declToImport() { int *ptr; __atomic_load_n(ptr, 1); }", |
| 251 | Lang_C, "", Lang_C, Verifier, |
| 252 | functionDecl(hasBody(compoundStmt(has(atomicExpr( |
| 253 | has(ignoringParenImpCasts( |
| 254 | declRefExpr(hasDeclaration(varDecl(hasName("ptr"))), |
| 255 | hasType(asString("int *"))))), |
| 256 | has(integerLiteral(equals(1), hasType(asString("int")))))))))); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | TEST(ImportExpr, ImportLabelDeclAndAddrLabelExpr) { |
| 260 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 261 | testImport( |
| 262 | "void declToImport() { loop: goto loop; &&loop; }", Lang_C, "", Lang_C, |
| 263 | Verifier, |
| 264 | functionDecl(hasBody(compoundStmt( |
| 265 | has(labelStmt(hasDeclaration(labelDecl(hasName("loop"))))), |
| 266 | has(addrLabelExpr(hasDeclaration(labelDecl(hasName("loop"))))))))); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | AST_MATCHER_P(TemplateDecl, hasTemplateDecl, |
| 270 | internal::Matcher<NamedDecl>, InnerMatcher) { |
| 271 | const NamedDecl *Template = Node.getTemplatedDecl(); |
| 272 | return Template && InnerMatcher.matches(*Template, Finder, Builder); |
| 273 | } |
| 274 | |
| 275 | TEST(ImportExpr, ImportParenListExpr) { |
| 276 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 277 | testImport( |
Manuel Klimek | 696e505 | 2017-08-02 13:04:44 +0000 | [diff] [blame] | 278 | "template<typename T> class dummy { void f() { dummy X(*this); } };" |
| 279 | "typedef dummy<int> declToImport;" |
| 280 | "template class dummy<int>;", |
| 281 | Lang_CXX, "", Lang_CXX, Verifier, |
| 282 | typedefDecl(hasType(templateSpecializationType( |
| 283 | hasDeclaration(classTemplateSpecializationDecl(hasSpecializedTemplate( |
| 284 | classTemplateDecl(hasTemplateDecl(cxxRecordDecl(hasMethod(allOf( |
| 285 | hasName("f"), |
| 286 | hasBody(compoundStmt(has(declStmt(hasSingleDecl( |
| 287 | varDecl(hasInitializer(parenListExpr(has(unaryOperator( |
| 288 | hasOperatorName("*"), |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 289 | hasUnaryOperand(cxxThisExpr()))))))))))))))))))))))); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Gabor Horvath | 480892b | 2017-10-18 09:25:18 +0000 | [diff] [blame] | 292 | TEST(ImportExpr, ImportSwitch) { |
| 293 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 294 | testImport("void declToImport() { int b; switch (b) { case 1: break; } }", |
| 295 | Lang_C, "", Lang_C, Verifier, |
| 296 | functionDecl(hasBody(compoundStmt( |
| 297 | has(switchStmt(has(compoundStmt(has(caseStmt()))))))))); |
Gabor Horvath | 480892b | 2017-10-18 09:25:18 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 300 | TEST(ImportExpr, ImportStmtExpr) { |
| 301 | MatchVerifier<Decl> Verifier; |
| 302 | // NOTE: has() ignores implicit casts, using hasDescendant() to match it |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 303 | testImport( |
| 304 | "void declToImport() { int b; int a = b ?: 1; int C = ({int X=4; X;}); }", |
| 305 | Lang_C, "", Lang_C, Verifier, |
| 306 | functionDecl( |
| 307 | hasBody( |
| 308 | compoundStmt( |
| 309 | has( |
| 310 | declStmt( |
| 311 | hasSingleDecl( |
| 312 | varDecl( |
| 313 | hasName("C"), |
| 314 | hasType(asString("int")), |
| 315 | hasInitializer( |
| 316 | stmtExpr( |
| 317 | hasAnySubstatement( |
| 318 | declStmt( |
| 319 | hasSingleDecl( |
| 320 | varDecl( |
| 321 | hasName("X"), |
| 322 | hasType(asString("int")), |
| 323 | hasInitializer( |
| 324 | integerLiteral(equals(4))))))), |
| 325 | hasDescendant( |
| 326 | implicitCastExpr() |
| 327 | ))))))))))); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | TEST(ImportExpr, ImportConditionalOperator) { |
| 331 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 332 | testImport( |
| 333 | "void declToImport() { true ? 1 : -5; }", |
| 334 | Lang_CXX, "", Lang_CXX, Verifier, |
| 335 | functionDecl( |
| 336 | hasBody( |
| 337 | compoundStmt( |
| 338 | has( |
| 339 | conditionalOperator( |
| 340 | hasCondition(cxxBoolLiteral(equals(true))), |
| 341 | hasTrueExpression(integerLiteral(equals(1))), |
| 342 | hasFalseExpression( |
| 343 | unaryOperator(hasUnaryOperand(integerLiteral(equals(5)))) |
| 344 | ))))))); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | TEST(ImportExpr, ImportBinaryConditionalOperator) { |
| 348 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 349 | testImport( |
| 350 | "void declToImport() { 1 ?: -5; }", Lang_CXX, "", Lang_CXX, Verifier, |
| 351 | functionDecl( |
| 352 | hasBody( |
| 353 | compoundStmt( |
| 354 | has( |
| 355 | binaryConditionalOperator( |
| 356 | hasCondition( |
| 357 | implicitCastExpr( |
| 358 | hasSourceExpression( |
| 359 | opaqueValueExpr( |
| 360 | hasSourceExpression(integerLiteral(equals(1))))), |
| 361 | hasType(booleanType()))), |
| 362 | hasTrueExpression( |
| 363 | opaqueValueExpr(hasSourceExpression( |
| 364 | integerLiteral(equals(1))))), |
| 365 | hasFalseExpression( |
| 366 | unaryOperator(hasOperatorName("-"), |
| 367 | hasUnaryOperand(integerLiteral(equals(5))))) |
| 368 | )))))); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | TEST(ImportExpr, ImportDesignatedInitExpr) { |
| 372 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 373 | testImport("void declToImport() {" |
| 374 | " struct point { double x; double y; };" |
| 375 | " struct point ptarray[10] = " |
| 376 | "{ [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; }", |
| 377 | Lang_C, "", Lang_C, Verifier, |
| 378 | functionDecl( |
| 379 | hasBody( |
| 380 | compoundStmt( |
| 381 | has( |
| 382 | declStmt( |
| 383 | hasSingleDecl( |
| 384 | varDecl( |
| 385 | hasInitializer( |
| 386 | initListExpr( |
| 387 | hasSyntacticForm( |
| 388 | initListExpr( |
| 389 | has( |
| 390 | designatedInitExpr( |
| 391 | designatorCountIs(2), |
| 392 | has(floatLiteral( |
| 393 | equals(1.0))), |
| 394 | has(integerLiteral( |
| 395 | equals(2))))), |
| 396 | has( |
| 397 | designatedInitExpr( |
| 398 | designatorCountIs(2), |
| 399 | has(floatLiteral( |
| 400 | equals(2.0))), |
| 401 | has(integerLiteral( |
| 402 | equals(2))))), |
| 403 | has( |
| 404 | designatedInitExpr( |
| 405 | designatorCountIs(2), |
| 406 | has(floatLiteral( |
| 407 | equals(1.0))), |
| 408 | has(integerLiteral( |
| 409 | equals(0))))) |
| 410 | )))))))))))); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | |
| 414 | TEST(ImportExpr, ImportPredefinedExpr) { |
| 415 | MatchVerifier<Decl> Verifier; |
| 416 | // __func__ expands as StringLiteral("declToImport") |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 417 | testImport("void declToImport() { __func__; }", |
| 418 | Lang_CXX, "", Lang_CXX, Verifier, |
| 419 | functionDecl( |
| 420 | hasBody( |
| 421 | compoundStmt( |
| 422 | has( |
| 423 | predefinedExpr( |
| 424 | hasType( |
| 425 | asString("const char [13]")), |
| 426 | has( |
| 427 | stringLiteral( |
| 428 | hasType( |
| 429 | asString("const char [13]")))))))))); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | TEST(ImportExpr, ImportInitListExpr) { |
| 433 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 434 | testImport( |
| 435 | "void declToImport() {" |
| 436 | " struct point { double x; double y; };" |
| 437 | " point ptarray[10] = { [2].y = 1.0, [2].x = 2.0," |
| 438 | " [0].x = 1.0 }; }", |
| 439 | Lang_CXX, "", Lang_CXX, Verifier, |
| 440 | functionDecl( |
| 441 | hasBody( |
| 442 | compoundStmt( |
| 443 | has( |
| 444 | declStmt( |
| 445 | hasSingleDecl( |
| 446 | varDecl( |
| 447 | hasInitializer( |
| 448 | initListExpr( |
| 449 | has( |
| 450 | cxxConstructExpr( |
| 451 | requiresZeroInitialization())), |
| 452 | has( |
| 453 | initListExpr( |
| 454 | hasType(asString("struct point")), |
| 455 | has(floatLiteral(equals(1.0))), |
| 456 | has(implicitValueInitExpr( |
| 457 | hasType(asString("double")))))), |
| 458 | has( |
| 459 | initListExpr( |
| 460 | hasType(asString("struct point")), |
| 461 | has(floatLiteral(equals(2.0))), |
| 462 | has(floatLiteral(equals(1.0))))) |
| 463 | )))))))))); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 467 | const internal::VariadicDynCastAllOfMatcher<Expr, VAArgExpr> vaArgExpr; |
| 468 | |
| 469 | TEST(ImportExpr, ImportVAArgExpr) { |
| 470 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 471 | testImport("void declToImport(__builtin_va_list list, ...) {" |
| 472 | " (void)__builtin_va_arg(list, int); }", |
| 473 | Lang_CXX, "", Lang_CXX, Verifier, |
| 474 | functionDecl( |
| 475 | hasBody( |
| 476 | compoundStmt( |
| 477 | has( |
| 478 | cStyleCastExpr( |
| 479 | hasSourceExpression( |
| 480 | vaArgExpr()))))))); |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | |
Gabor Horvath | 0866c2f | 2016-11-23 15:24:23 +0000 | [diff] [blame] | 484 | TEST(ImportType, ImportAtomicType) { |
| 485 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 486 | testImport("void declToImport() { typedef _Atomic(int) a_int; }", |
| 487 | Lang_CXX11, "", Lang_CXX11, Verifier, |
| 488 | functionDecl( |
| 489 | hasBody( |
| 490 | compoundStmt( |
| 491 | has( |
| 492 | declStmt( |
| 493 | has( |
| 494 | typedefDecl( |
| 495 | has(atomicType()))))))))); |
Gabor Horvath | 0866c2f | 2016-11-23 15:24:23 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Aleksei Sidorin | 7f758b6 | 2017-12-27 17:04:42 +0000 | [diff] [blame^] | 498 | TEST(ImportDecl, ImportFunctionTemplateDecl) { |
| 499 | MatchVerifier<Decl> Verifier; |
| 500 | testImport("template <typename T> void declToImport() { };", Lang_CXX, "", |
| 501 | Lang_CXX, Verifier, functionTemplateDecl()); |
| 502 | } |
| 503 | |
| 504 | const internal::VariadicDynCastAllOfMatcher<Expr, CXXDependentScopeMemberExpr> |
| 505 | cxxDependentScopeMemberExpr; |
| 506 | |
| 507 | TEST(ImportExpr, ImportCXXDependentScopeMemberExpr) { |
| 508 | MatchVerifier<Decl> Verifier; |
| 509 | testImport("template <typename T> struct C { T t; };" |
| 510 | "template <typename T> void declToImport() {" |
| 511 | " C<T> d;" |
| 512 | " d.t;" |
| 513 | "}" |
| 514 | "void instantiate() { declToImport<int>(); }", |
| 515 | Lang_CXX, "", Lang_CXX, Verifier, |
| 516 | functionTemplateDecl(has(functionDecl( |
| 517 | has(compoundStmt(has(cxxDependentScopeMemberExpr()))))))); |
| 518 | testImport("template <typename T> struct C { T t; };" |
| 519 | "template <typename T> void declToImport() {" |
| 520 | " C<T> d;" |
| 521 | " (&d)->t;" |
| 522 | "}" |
| 523 | "void instantiate() { declToImport<int>(); }", |
| 524 | Lang_CXX, "", Lang_CXX, Verifier, |
| 525 | functionTemplateDecl(has(functionDecl( |
| 526 | has(compoundStmt(has(cxxDependentScopeMemberExpr()))))))); |
| 527 | } |
Gabor Horvath | 0866c2f | 2016-11-23 15:24:23 +0000 | [diff] [blame] | 528 | |
Gabor Horvath | 7a91c08 | 2017-11-14 11:30:38 +0000 | [diff] [blame] | 529 | TEST(ImportType, ImportTypeAliasTemplate) { |
| 530 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 531 | testImport("template <int K>" |
| 532 | "struct dummy { static const int i = K; };" |
| 533 | "template <int K> using dummy2 = dummy<K>;" |
| 534 | "int declToImport() { return dummy2<3>::i; }", |
| 535 | Lang_CXX11, "", Lang_CXX11, Verifier, |
| 536 | functionDecl( |
| 537 | hasBody( |
| 538 | compoundStmt( |
| 539 | has( |
| 540 | returnStmt( |
| 541 | has( |
| 542 | implicitCastExpr( |
| 543 | has( |
| 544 | declRefExpr()))))))))); |
Gabor Horvath | 7a91c08 | 2017-11-14 11:30:38 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Gabor Horvath | 7a91c08 | 2017-11-14 11:30:38 +0000 | [diff] [blame] | 547 | TEST(ImportType, ImportPackExpansion) { |
| 548 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 549 | testImport("template <typename... Args>" |
| 550 | "struct dummy {" |
| 551 | " dummy(Args... args) {}" |
| 552 | " static const int i = 4;" |
| 553 | "};" |
| 554 | "int declToImport() { return dummy<int>::i; }", |
| 555 | Lang_CXX11, "", Lang_CXX11, Verifier, |
| 556 | functionDecl( |
| 557 | hasBody( |
| 558 | compoundStmt( |
| 559 | has( |
| 560 | returnStmt( |
| 561 | has( |
| 562 | implicitCastExpr( |
| 563 | has( |
| 564 | declRefExpr()))))))))); |
Gabor Horvath | 7a91c08 | 2017-11-14 11:30:38 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Aleksei Sidorin | b05f37a | 2017-11-26 17:04:06 +0000 | [diff] [blame] | 567 | /// \brief Matches __builtin_types_compatible_p: |
| 568 | /// GNU extension to check equivalent types |
| 569 | /// Given |
| 570 | /// \code |
| 571 | /// __builtin_types_compatible_p(int, int) |
| 572 | /// \endcode |
| 573 | // will generate TypeTraitExpr <...> 'int' |
| 574 | const internal::VariadicDynCastAllOfMatcher<Stmt, TypeTraitExpr> typeTraitExpr; |
| 575 | |
| 576 | TEST(ImportExpr, ImportTypeTraitExpr) { |
| 577 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 578 | testImport("void declToImport() { " |
| 579 | " __builtin_types_compatible_p(int, int);" |
| 580 | "}", |
| 581 | Lang_C, "", Lang_C, Verifier, |
| 582 | functionDecl( |
| 583 | hasBody( |
| 584 | compoundStmt( |
| 585 | has( |
| 586 | typeTraitExpr(hasType(asString("int")))))))); |
Aleksei Sidorin | b05f37a | 2017-11-26 17:04:06 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | TEST(ImportExpr, ImportTypeTraitExprValDep) { |
| 590 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 591 | testImport("template<typename T> struct declToImport {" |
| 592 | " void m() { __is_pod(T); }" |
| 593 | "};" |
| 594 | "void f() { declToImport<int>().m(); }", |
| 595 | Lang_CXX11, "", Lang_CXX11, Verifier, |
| 596 | classTemplateDecl( |
| 597 | has( |
| 598 | cxxRecordDecl( |
| 599 | has( |
| 600 | functionDecl( |
| 601 | hasBody( |
| 602 | compoundStmt( |
Aleksei Sidorin | b05f37a | 2017-11-26 17:04:06 +0000 | [diff] [blame] | 603 | has( |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 604 | typeTraitExpr( |
| 605 | hasType(booleanType()) |
| 606 | )))))))))); |
Aleksei Sidorin | b05f37a | 2017-11-26 17:04:06 +0000 | [diff] [blame] | 607 | } |
Gabor Horvath | 7a91c08 | 2017-11-14 11:30:38 +0000 | [diff] [blame] | 608 | |
Aleksei Sidorin | 60ccb7d | 2017-11-27 10:30:00 +0000 | [diff] [blame] | 609 | const internal::VariadicDynCastAllOfMatcher<Expr, CXXPseudoDestructorExpr> |
| 610 | cxxPseudoDestructorExpr; |
| 611 | |
| 612 | TEST(ImportExpr, ImportCXXPseudoDestructorExpr) { |
| 613 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 614 | testImport("typedef int T;" |
| 615 | "void declToImport(int *p) {" |
| 616 | " T t;" |
| 617 | " p->T::~T();" |
| 618 | "}", |
| 619 | Lang_CXX, "", Lang_CXX, Verifier, |
| 620 | functionDecl(has(compoundStmt(has( |
| 621 | callExpr(has(cxxPseudoDestructorExpr()))))))); |
Aleksei Sidorin | 60ccb7d | 2017-11-27 10:30:00 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Aleksei Sidorin | 9d8ba2e | 2017-12-03 16:04:07 +0000 | [diff] [blame] | 624 | TEST(ImportDecl, ImportUsingDecl) { |
| 625 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 626 | testImport("namespace foo { int bar; }" |
| 627 | "int declToImport(){ using foo::bar; }", |
| 628 | Lang_CXX, "", Lang_CXX, Verifier, |
| 629 | functionDecl( |
| 630 | has( |
| 631 | compoundStmt( |
| 632 | has( |
| 633 | declStmt( |
| 634 | has( |
| 635 | usingDecl()))))))); |
Aleksei Sidorin | 9d8ba2e | 2017-12-03 16:04:07 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /// \brief Matches shadow declarations introduced into a scope by a |
| 639 | /// (resolved) using declaration. |
| 640 | /// |
| 641 | /// Given |
| 642 | /// \code |
| 643 | /// namespace n { int f; } |
| 644 | /// namespace declToImport { using n::f; } |
| 645 | /// \endcode |
| 646 | /// usingShadowDecl() |
| 647 | /// matches \code f \endcode |
| 648 | const internal::VariadicDynCastAllOfMatcher<Decl, |
| 649 | UsingShadowDecl> usingShadowDecl; |
| 650 | |
| 651 | TEST(ImportDecl, ImportUsingShadowDecl) { |
| 652 | MatchVerifier<Decl> Verifier; |
Aleksei Sidorin | e45ab56 | 2017-12-21 17:41:06 +0000 | [diff] [blame] | 653 | testImport("namespace foo { int bar; }" |
| 654 | "namespace declToImport { using foo::bar; }", |
| 655 | Lang_CXX, "", Lang_CXX, Verifier, |
| 656 | namespaceDecl(has(usingShadowDecl()))); |
Aleksei Sidorin | 9d8ba2e | 2017-12-03 16:04:07 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 659 | } // end namespace ast_matchers |
| 660 | } // end namespace clang |