From aa558eb893a580e7a08aa77cf55081fb3c30ccb6 Mon Sep 17 00:00:00 2001 From: Gabor Marton Date: Thu, 9 Aug 2018 12:18:07 +0000 Subject: [PATCH] Add support for importing imaginary literals Reviewers: a_sidorin, r.stahl, xazax.hun Subscribers: rnkovacs, dkrupp, cfe-commits Differential Revision: https://reviews.llvm.org/D50428 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@339334 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/ASTMatchers/ASTMatchers.h | 5 +++++ lib/AST/ASTImporter.cpp | 13 +++++++++++++ lib/ASTMatchers/ASTMatchersInternal.cpp | 1 + unittests/AST/ASTImporterTest.cpp | 8 ++++++++ 4 files changed, 27 insertions(+) diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index 51f2ce2af7..c8fe660d5d 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -1975,6 +1975,11 @@ extern const internal::VariadicDynCastAllOfMatcher extern const internal::VariadicDynCastAllOfMatcher floatLiteral; +/// Matches imaginary literals, which are based on integer and floating +/// point literals e.g.: 1i, 1.0i +extern const internal::VariadicDynCastAllOfMatcher + imaginaryLiteral; + /// Matches user defined literal operator call. /// /// Example match: "foo"_suffix diff --git a/lib/AST/ASTImporter.cpp b/lib/AST/ASTImporter.cpp index 791dcc747c..a8f8dcf622 100644 --- a/lib/AST/ASTImporter.cpp +++ b/lib/AST/ASTImporter.cpp @@ -434,6 +434,7 @@ namespace clang { Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E); Expr *VisitIntegerLiteral(IntegerLiteral *E); Expr *VisitFloatingLiteral(FloatingLiteral *E); + Expr *VisitImaginaryLiteral(ImaginaryLiteral *E); Expr *VisitCharacterLiteral(CharacterLiteral *E); Expr *VisitStringLiteral(StringLiteral *E); Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E); @@ -5613,6 +5614,18 @@ Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) { Importer.Import(E->getLocation())); } +Expr *ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) { + QualType T = Importer.Import(E->getType()); + if (T.isNull()) + return nullptr; + + Expr *SubE = Importer.Import(E->getSubExpr()); + if (!SubE) + return nullptr; + + return new (Importer.getToContext()) ImaginaryLiteral(SubE, T); +} + Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { QualType T = Importer.Import(E->getType()); if (T.isNull()) diff --git a/lib/ASTMatchers/ASTMatchersInternal.cpp b/lib/ASTMatchers/ASTMatchersInternal.cpp index 9cea2f5efc..97b0ab8fd3 100644 --- a/lib/ASTMatchers/ASTMatchersInternal.cpp +++ b/lib/ASTMatchers/ASTMatchersInternal.cpp @@ -710,6 +710,7 @@ const internal::VariadicDynCastAllOfMatcher const internal::VariadicDynCastAllOfMatcher integerLiteral; const internal::VariadicDynCastAllOfMatcher floatLiteral; +const internal::VariadicDynCastAllOfMatcher imaginaryLiteral; const internal::VariadicDynCastAllOfMatcher userDefinedLiteral; const internal::VariadicDynCastAllOfMatcher diff --git a/unittests/AST/ASTImporterTest.cpp b/unittests/AST/ASTImporterTest.cpp index 2690f4e5f1..70edd15046 100644 --- a/unittests/AST/ASTImporterTest.cpp +++ b/unittests/AST/ASTImporterTest.cpp @@ -553,6 +553,14 @@ TEST_P(ImportExpr, ImportFloatinglLiteralExpr) { floatLiteral(equals(1.0e-5f), hasType(asString("float")))))); } +TEST_P(ImportExpr, ImportImaginaryLiteralExpr) { + MatchVerifier Verifier; + testImport( + "void declToImport() { (void)1.0i; }", + Lang_CXX14, "", Lang_CXX14, Verifier, + functionDecl(hasDescendant(imaginaryLiteral()))); +} + TEST_P(ImportExpr, ImportCompoundLiteralExpr) { MatchVerifier Verifier; testImport( -- 2.40.0