Author: Nathan James Date: 2020-06-02T21:20:58+01:00 New Revision: 26cb70683bd4ffa49d94a8dad5ecfda549a673b0
URL: https://github.com/llvm/llvm-project/commit/26cb70683bd4ffa49d94a8dad5ecfda549a673b0 DIFF: https://github.com/llvm/llvm-project/commit/26cb70683bd4ffa49d94a8dad5ecfda549a673b0.diff LOG: [NFC][ASTMatchers] StringRef-ify and Twine-ify ASTMatchers tests. Added: Modified: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp clang/unittests/ASTMatchers/ASTMatchersTest.h clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp Removed: ################################################################################ diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp index 9f8538edb35b..80eebf227a31 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -19,7 +19,7 @@ namespace clang { namespace ast_matchers { TEST(IsExpandedFromMacro, ShouldMatchInFile) { - std::string input = R"cc( + StringRef input = R"cc( #define MY_MACRO(a) (4 + (a)) void Test() { MY_MACRO(4); } )cc"; @@ -27,7 +27,7 @@ TEST(IsExpandedFromMacro, ShouldMatchInFile) { } TEST(IsExpandedFromMacro, ShouldMatchNested) { - std::string input = R"cc( + StringRef input = R"cc( #define MY_MACRO(a) (4 + (a)) #define WRAPPER(a) MY_MACRO(a) void Test() { WRAPPER(4); } @@ -36,7 +36,7 @@ TEST(IsExpandedFromMacro, ShouldMatchNested) { } TEST(IsExpandedFromMacro, ShouldMatchIntermediate) { - std::string input = R"cc( + StringRef input = R"cc( #define IMPL(a) (4 + (a)) #define MY_MACRO(a) IMPL(a) #define WRAPPER(a) MY_MACRO(a) @@ -46,7 +46,7 @@ TEST(IsExpandedFromMacro, ShouldMatchIntermediate) { } TEST(IsExpandedFromMacro, ShouldMatchTransitive) { - std::string input = R"cc( + StringRef input = R"cc( #define MY_MACRO(a) (4 + (a)) #define WRAPPER(a) MY_MACRO(a) void Test() { WRAPPER(4); } @@ -55,7 +55,7 @@ TEST(IsExpandedFromMacro, ShouldMatchTransitive) { } TEST(IsExpandedFromMacro, ShouldMatchArgument) { - std::string input = R"cc( + StringRef input = R"cc( #define MY_MACRO(a) (4 + (a)) void Test() { int x = 5; @@ -68,7 +68,7 @@ TEST(IsExpandedFromMacro, ShouldMatchArgument) { // Like IsExpandedFromMacroShouldMatchArgumentMacro, but the argument is itself // a macro. TEST(IsExpandedFromMacro, ShouldMatchArgumentMacroExpansion) { - std::string input = R"cc( + StringRef input = R"cc( #define MY_MACRO(a) (4 + (a)) #define IDENTITY(a) (a) void Test() { @@ -79,7 +79,7 @@ TEST(IsExpandedFromMacro, ShouldMatchArgumentMacroExpansion) { } TEST(IsExpandedFromMacro, ShouldMatchWhenInArgument) { - std::string input = R"cc( + StringRef input = R"cc( #define MY_MACRO(a) (4 + (a)) #define IDENTITY(a) (a) void Test() { @@ -90,7 +90,7 @@ TEST(IsExpandedFromMacro, ShouldMatchWhenInArgument) { } TEST(IsExpandedFromMacro, ShouldMatchObjectMacro) { - std::string input = R"cc( + StringRef input = R"cc( #define PLUS (2 + 2) void Test() { PLUS; @@ -100,7 +100,7 @@ TEST(IsExpandedFromMacro, ShouldMatchObjectMacro) { } TEST(IsExpandedFromMacro, ShouldMatchFromCommandLine) { - std::string input = R"cc( + StringRef input = R"cc( void Test() { FOUR_PLUS_FOUR; } )cc"; EXPECT_TRUE(matchesConditionally(input, @@ -109,7 +109,7 @@ TEST(IsExpandedFromMacro, ShouldMatchFromCommandLine) { } TEST(IsExpandedFromMacro, ShouldNotMatchBeginOnly) { - std::string input = R"cc( + StringRef input = R"cc( #define ONE_PLUS 1+ void Test() { ONE_PLUS 4; } )cc"; @@ -118,7 +118,7 @@ TEST(IsExpandedFromMacro, ShouldNotMatchBeginOnly) { } TEST(IsExpandedFromMacro, ShouldNotMatchEndOnly) { - std::string input = R"cc( + StringRef input = R"cc( #define PLUS_ONE +1 void Test() { 4 PLUS_ONE; } )cc"; @@ -127,7 +127,7 @@ TEST(IsExpandedFromMacro, ShouldNotMatchEndOnly) { } TEST(IsExpandedFromMacro, ShouldNotMatchDifferentMacro) { - std::string input = R"cc( + StringRef input = R"cc( #define MY_MACRO(a) (4 + (a)) void Test() { MY_MACRO(4); } )cc"; @@ -135,7 +135,7 @@ TEST(IsExpandedFromMacro, ShouldNotMatchDifferentMacro) { } TEST(IsExpandedFromMacro, ShouldNotMatchDifferentInstances) { - std::string input = R"cc( + StringRef input = R"cc( #define FOUR 4 void Test() { FOUR + FOUR; } )cc"; @@ -966,8 +966,8 @@ TEST(Matcher, VarDecl_IsStaticLocal) { } TEST(Matcher, VarDecl_StorageDuration) { - std::string T = - "void f() { int x; static int y; } int a;static int b;extern int c;"; + StringRef T = + "void f() { int x; static int y; } int a;static int b;extern int c;"; EXPECT_TRUE(matches(T, varDecl(hasName("x"), hasAutomaticStorageDuration()))); EXPECT_TRUE( @@ -1629,7 +1629,7 @@ TEST(Matcher, HasNameSupportsOuterClasses) { } TEST(Matcher, HasNameSupportsInlinedNamespaces) { - std::string code = "namespace a { inline namespace b { class C; } }"; + StringRef code = "namespace a { inline namespace b { class C; } }"; EXPECT_TRUE(matches(code, recordDecl(hasName("a::b::C")))); EXPECT_TRUE(matches(code, recordDecl(hasName("a::C")))); EXPECT_TRUE(matches(code, recordDecl(hasName("::a::b::C")))); @@ -1637,7 +1637,7 @@ TEST(Matcher, HasNameSupportsInlinedNamespaces) { } TEST(Matcher, HasNameSupportsAnonymousNamespaces) { - std::string code = "namespace a { namespace { class C; } }"; + StringRef code = "namespace a { namespace { class C; } }"; EXPECT_TRUE( matches(code, recordDecl(hasName("a::(anonymous namespace)::C")))); EXPECT_TRUE(matches(code, recordDecl(hasName("a::C")))); @@ -1662,8 +1662,8 @@ TEST(Matcher, HasNameSupportsAnonymousOuterClasses) { } TEST(Matcher, HasNameSupportsFunctionScope) { - std::string code = - "namespace a { void F(int a) { struct S { int m; }; int i; } }"; + StringRef code = + "namespace a { void F(int a) { struct S { int m; }; int i; } }"; EXPECT_TRUE(matches(code, varDecl(hasName("i")))); EXPECT_FALSE(matches(code, varDecl(hasName("F()::i")))); @@ -1676,7 +1676,7 @@ TEST(Matcher, HasNameSupportsFunctionScope) { TEST(Matcher, HasNameQualifiedSupportsLinkage) { // https://bugs.llvm.org/show_bug.cgi?id=42193 - std::string code = R"cpp(namespace foo { extern "C" void test(); })cpp"; + StringRef code = R"cpp(namespace foo { extern "C" void test(); })cpp"; EXPECT_TRUE(matches(code, functionDecl(hasName("test")))); EXPECT_TRUE(matches(code, functionDecl(hasName("foo::test")))); EXPECT_TRUE(matches(code, functionDecl(hasName("::foo::test")))); @@ -1690,7 +1690,7 @@ TEST(Matcher, HasNameQualifiedSupportsLinkage) { } TEST(Matcher, HasAnyName) { - const std::string Code = "namespace a { namespace b { class C; } }"; + StringRef Code = "namespace a { namespace b { class C; } }"; EXPECT_TRUE(matches(Code, recordDecl(hasAnyName("XX", "a::b::C")))); EXPECT_TRUE(matches(Code, recordDecl(hasAnyName("a::b::C", "XX")))); @@ -1756,9 +1756,9 @@ TEST(Matcher, HandlesNullQualTypes) { } TEST(ObjCIvarRefExprMatcher, IvarExpr) { - std::string ObjCString = - "@interface A @end " - "@implementation A { A *x; } - (void) func { x = 0; } @end"; + StringRef ObjCString = + "@interface A @end " + "@implementation A { A *x; } - (void) func { x = 0; } @end"; EXPECT_TRUE(matchesObjC(ObjCString, objcIvarRefExpr())); EXPECT_TRUE(matchesObjC(ObjCString, objcIvarRefExpr( hasDeclaration(namedDecl(hasName("x")))))); @@ -2044,7 +2044,7 @@ TEST(Optionally, SubmatchersDoNotMatch) { // Regression test. TEST(Optionally, SubmatchersDoNotMatchButPreserveBindings) { - std::string Code = "class A { int a; int b; };"; + StringRef Code = "class A { int a; int b; };"; auto Matcher = recordDecl(decl().bind("decl"), optionally(has(fieldDecl(hasName("c")).bind("v")))); // "decl" is still bound. @@ -2796,14 +2796,14 @@ TEST(Matcher, isMain) { TEST(OMPExecutableDirective, isStandaloneDirective) { auto Matcher = ompExecutableDirective(isStandaloneDirective()); - const std::string Source0 = R"( + StringRef Source0 = R"( void x() { #pragma omp parallel ; })"; EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); - const std::string Source1 = R"( + StringRef Source1 = R"( void x() { #pragma omp taskyield })"; @@ -2811,7 +2811,7 @@ void x() { } TEST(OMPExecutableDirective, hasStructuredBlock) { - const std::string Source0 = R"( + StringRef Source0 = R"( void x() { #pragma omp parallel ; @@ -2819,7 +2819,7 @@ void x() { EXPECT_TRUE(matchesWithOpenMP( Source0, ompExecutableDirective(hasStructuredBlock(nullStmt())))); - const std::string Source1 = R"( + StringRef Source1 = R"( void x() { #pragma omp parallel {;} @@ -2829,7 +2829,7 @@ void x() { EXPECT_TRUE(matchesWithOpenMP( Source1, ompExecutableDirective(hasStructuredBlock(compoundStmt())))); - const std::string Source2 = R"( + StringRef Source2 = R"( void x() { #pragma omp taskyield {;} @@ -2841,34 +2841,34 @@ void x() { TEST(OMPExecutableDirective, hasClause) { auto Matcher = ompExecutableDirective(hasAnyClause(anything())); - const std::string Source0 = R"( + StringRef Source0 = R"( void x() { ; })"; EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); - const std::string Source1 = R"( + StringRef Source1 = R"( void x() { #pragma omp parallel ; })"; EXPECT_TRUE(notMatchesWithOpenMP(Source1, Matcher)); - const std::string Source2 = R"( + StringRef Source2 = R"( void x() { #pragma omp parallel default(none) ; })"; EXPECT_TRUE(matchesWithOpenMP(Source2, Matcher)); - const std::string Source3 = R"( + StringRef Source3 = R"( void x() { #pragma omp parallel default(shared) ; })"; EXPECT_TRUE(matchesWithOpenMP(Source3, Matcher)); - const std::string Source4 = R"( + StringRef Source4 = R"( void x(int x) { #pragma omp parallel num_threads(x) ; @@ -2880,34 +2880,34 @@ TEST(OMPDefaultClause, isNoneKind) { auto Matcher = ompExecutableDirective(hasAnyClause(ompDefaultClause(isNoneKind()))); - const std::string Source0 = R"( + StringRef Source0 = R"( void x() { ; })"; EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); - const std::string Source1 = R"( + StringRef Source1 = R"( void x() { #pragma omp parallel ; })"; EXPECT_TRUE(notMatchesWithOpenMP(Source1, Matcher)); - const std::string Source2 = R"( + StringRef Source2 = R"( void x() { #pragma omp parallel default(none) ; })"; EXPECT_TRUE(matchesWithOpenMP(Source2, Matcher)); - const std::string Source3 = R"( + StringRef Source3 = R"( void x() { #pragma omp parallel default(shared) ; })"; EXPECT_TRUE(notMatchesWithOpenMP(Source3, Matcher)); - const std::string Source4 = R"( + StringRef Source4 = R"( void x(int x) { #pragma omp parallel num_threads(x) ; @@ -2919,34 +2919,34 @@ TEST(OMPDefaultClause, isSharedKind) { auto Matcher = ompExecutableDirective(hasAnyClause(ompDefaultClause(isSharedKind()))); - const std::string Source0 = R"( + StringRef Source0 = R"( void x() { ; })"; EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); - const std::string Source1 = R"( + StringRef Source1 = R"( void x() { #pragma omp parallel ; })"; EXPECT_TRUE(notMatchesWithOpenMP(Source1, Matcher)); - const std::string Source2 = R"( + StringRef Source2 = R"( void x() { #pragma omp parallel default(shared) ; })"; EXPECT_TRUE(matchesWithOpenMP(Source2, Matcher)); - const std::string Source3 = R"( + StringRef Source3 = R"( void x() { #pragma omp parallel default(none) ; })"; EXPECT_TRUE(notMatchesWithOpenMP(Source3, Matcher)); - const std::string Source4 = R"( + StringRef Source4 = R"( void x(int x) { #pragma omp parallel num_threads(x) ; @@ -2958,47 +2958,47 @@ TEST(OMPExecutableDirective, isAllowedToContainClauseKind) { auto Matcher = ompExecutableDirective( isAllowedToContainClauseKind(llvm::omp::OMPC_default)); - const std::string Source0 = R"( + StringRef Source0 = R"( void x() { ; })"; EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); - const std::string Source1 = R"( + StringRef Source1 = R"( void x() { #pragma omp parallel ; })"; EXPECT_TRUE(matchesWithOpenMP(Source1, Matcher)); - const std::string Source2 = R"( + StringRef Source2 = R"( void x() { #pragma omp parallel default(none) ; })"; EXPECT_TRUE(matchesWithOpenMP(Source2, Matcher)); - const std::string Source3 = R"( + StringRef Source3 = R"( void x() { #pragma omp parallel default(shared) ; })"; EXPECT_TRUE(matchesWithOpenMP(Source3, Matcher)); - const std::string Source4 = R"( + StringRef Source4 = R"( void x(int x) { #pragma omp parallel num_threads(x) ; })"; EXPECT_TRUE(matchesWithOpenMP(Source4, Matcher)); - const std::string Source5 = R"( + StringRef Source5 = R"( void x() { #pragma omp taskyield })"; EXPECT_TRUE(notMatchesWithOpenMP(Source5, Matcher)); - const std::string Source6 = R"( + StringRef Source6 = R"( void x() { #pragma omp task ; diff --git a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp index 82a28d00cebf..c6f40955e2a0 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp @@ -95,10 +95,10 @@ TEST(DeclarationMatcher, MatchClass) { } TEST(DeclarationMatcher, translationUnitDecl) { - const std::string Code = "int MyVar1;\n" - "namespace NameSpace {\n" - "int MyVar2;\n" - "} // namespace NameSpace\n"; + StringRef Code = "int MyVar1;\n" + "namespace NameSpace {\n" + "int MyVar2;\n" + "} // namespace NameSpace\n"; EXPECT_TRUE(matches( Code, varDecl(hasName("MyVar1"), hasDeclContext(translationUnitDecl())))); EXPECT_FALSE(matches( @@ -208,7 +208,7 @@ TEST(Matcher, UnresolvedLookupExpr) { TEST(Matcher, ADLCall) { StatementMatcher ADLMatch = callExpr(usesADL()); StatementMatcher ADLMatchOper = cxxOperatorCallExpr(usesADL()); - auto NS_Str = R"cpp( + StringRef NS_Str = R"cpp( namespace NS { struct X {}; void f(X); @@ -219,10 +219,8 @@ TEST(Matcher, ADLCall) { void operator+(MyX, MyX); )cpp"; - auto MkStr = [&](std::string Body) -> std::string { - std::string S = NS_Str; - S += "void test_fn() { " + Body + " }"; - return S; + auto MkStr = [&](StringRef Body) { + return (NS_Str + "void test_fn() { " + Body + " }").str(); }; EXPECT_TRUE(matches(MkStr("NS::X x; f(x);"), ADLMatch)); @@ -612,7 +610,7 @@ TEST(Matcher, BindTemporaryExpression) { StatementMatcher TempExpression = traverse(ast_type_traits::TK_AsIs, cxxBindTemporaryExpr()); - std::string ClassString = "class string { public: string(); ~string(); }; "; + StringRef ClassString = "class string { public: string(); ~string(); }; "; EXPECT_TRUE( matches(ClassString + @@ -641,8 +639,7 @@ TEST(Matcher, BindTemporaryExpression) { } TEST(MaterializeTemporaryExpr, MatchesTemporary) { - std::string ClassString = - "class string { public: string(); int length(); }; "; + StringRef ClassString = "class string { public: string(); int length(); }; "; StatementMatcher TempExpression = traverse(ast_type_traits::TK_AsIs, materializeTemporaryExpr()); @@ -987,13 +984,13 @@ TEST(ReinterpretCast, DoesNotMatchOtherCasts) { } TEST(FunctionalCast, MatchesSimpleCase) { - std::string foo_class = "class Foo { public: Foo(const char*); };"; + StringRef foo_class = "class Foo { public: Foo(const char*); };"; EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }", cxxFunctionalCastExpr())); } TEST(FunctionalCast, DoesNotMatchOtherCasts) { - std::string FooClass = "class Foo { public: Foo(const char*); };"; + StringRef FooClass = "class Foo { public: Foo(const char*); };"; EXPECT_TRUE( notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }", cxxFunctionalCastExpr())); @@ -1115,14 +1112,14 @@ TEST(InitListExpression, MatchesInitListExpression) { } TEST(CXXStdInitializerListExpression, MatchesCXXStdInitializerListExpression) { - const std::string code = "namespace std {" - "template <typename> class initializer_list {" - " public: initializer_list() noexcept {}" - "};" - "}" - "struct A {" - " A(std::initializer_list<int>) {}" - "};"; + StringRef code = "namespace std {" + "template <typename> class initializer_list {" + " public: initializer_list() noexcept {}" + "};" + "}" + "struct A {" + " A(std::initializer_list<int>) {}" + "};"; EXPECT_TRUE( matches(code + "A a{0};", traverse(ast_type_traits::TK_AsIs, @@ -1364,7 +1361,7 @@ TEST(TypeMatching, PointerTypes) { "int* b; int* * const a = &b;", loc(qualType(isConstQualified(), pointerType())))); - std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;"; + StringRef Fragment = "struct A { int i; }; int A::* ptr = &A::i;"; EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), hasType(blockPointerType())))); EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"), @@ -1418,11 +1415,11 @@ TEST(TypeMatching, PointerTypes) { } TEST(TypeMatching, AutoRefTypes) { - std::string Fragment = "auto a = 1;" - "auto b = a;" - "auto &c = a;" - "auto &&d = c;" - "auto &&e = 2;"; + StringRef Fragment = "auto a = 1;" + "auto b = a;" + "auto &c = a;" + "auto &&d = c;" + "auto &&e = 2;"; EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"), hasType(referenceType())))); EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"), @@ -1504,11 +1501,11 @@ TEST(TypeMatching, MatchesElaboratedType) { } TEST(TypeMatching, MatchesSubstTemplateTypeParmType) { - const std::string code = "template <typename T>" - "int F() {" - " return 1 + T();" - "}" - "int i = F<int>();"; + StringRef code = "template <typename T>" + "int F() {" + " return 1 + T();" + "}" + "int i = F<int>();"; EXPECT_FALSE(matches(code, binaryOperator(hasLHS( expr(hasType(substTemplateTypeParmType())))))); EXPECT_TRUE(matches(code, binaryOperator(hasRHS( @@ -1650,7 +1647,7 @@ TEST(TypedefNameDeclMatcher, Match) { } TEST(TypeAliasTemplateDeclMatcher, Match) { - std::string Code = R"( + StringRef Code = R"( template <typename T> class X { T t; }; @@ -1669,21 +1666,20 @@ TEST(ObjCMessageExprMatcher, SimpleExprs) { // don't find ObjCMessageExpr where none are present EXPECT_TRUE(notMatchesObjC("", objcMessageExpr(anything()))); - std::string Objc1String = - "@interface Str " - " - (Str *)uppercaseString;" - "@end " - "@interface foo " - "- (void)contents;" - "- (void)meth:(Str *)text;" - "@end " - " " - "@implementation foo " - "- (void) meth:(Str *)text { " - " [self contents];" - " Str *up = [text uppercaseString];" - "} " - "@end "; + StringRef Objc1String = "@interface Str " + " - (Str *)uppercaseString;" + "@end " + "@interface foo " + "- (void)contents;" + "- (void)meth:(Str *)text;" + "@end " + " " + "@implementation foo " + "- (void) meth:(Str *)text { " + " [self contents];" + " Str *up = [text uppercaseString];" + "} " + "@end "; EXPECT_TRUE(matchesObjC( Objc1String, objcMessageExpr(anything()))); @@ -1724,24 +1720,22 @@ TEST(ObjCMessageExprMatcher, SimpleExprs) { } TEST(ObjCDeclMatcher, CoreDecls) { - std::string ObjCString = - "@protocol Proto " - "- (void)protoDidThing; " - "@end " - "@interface Thing " - "@property int enabled; " - "@end " - "@interface Thing (ABC) " - "- (void)abc_doThing; " - "@end " - "@implementation Thing " - "{ id _ivar; } " - "- (void)anything {} " - "@end " - "@implementation Thing (ABC) " - "- (void)abc_doThing {} " - "@end " - ; + StringRef ObjCString = "@protocol Proto " + "- (void)protoDidThing; " + "@end " + "@interface Thing " + "@property int enabled; " + "@end " + "@interface Thing (ABC) " + "- (void)abc_doThing; " + "@end " + "@implementation Thing " + "{ id _ivar; } " + "- (void)anything {} " + "@end " + "@implementation Thing (ABC) " + "- (void)abc_doThing {} " + "@end "; EXPECT_TRUE(matchesObjC( ObjCString, @@ -1773,13 +1767,12 @@ TEST(ObjCDeclMatcher, CoreDecls) { } TEST(ObjCStmtMatcher, ExceptionStmts) { - std::string ObjCString = - "void f(id obj) {" - " @try {" - " @throw obj;" - " } @catch (...) {" - " } @finally {}" - "}"; + StringRef ObjCString = "void f(id obj) {" + " @try {" + " @throw obj;" + " } @catch (...) {" + " } @finally {}" + "}"; EXPECT_TRUE(matchesObjC( ObjCString, @@ -1796,35 +1789,34 @@ TEST(ObjCStmtMatcher, ExceptionStmts) { } TEST(ObjCAutoreleaseMatcher, AutoreleasePool) { - std::string ObjCString = - "void f() {" - "@autoreleasepool {" - " int x = 1;" - "}" - "}"; + StringRef ObjCString = "void f() {" + "@autoreleasepool {" + " int x = 1;" + "}" + "}"; EXPECT_TRUE(matchesObjC(ObjCString, autoreleasePoolStmt())); - std::string ObjCStringNoPool = "void f() { int x = 1; }"; + StringRef ObjCStringNoPool = "void f() { int x = 1; }"; EXPECT_FALSE(matchesObjC(ObjCStringNoPool, autoreleasePoolStmt())); } TEST(OMPExecutableDirective, Matches) { auto Matcher = stmt(ompExecutableDirective()); - const std::string Source0 = R"( + StringRef Source0 = R"( void x() { #pragma omp parallel ; })"; EXPECT_TRUE(matchesWithOpenMP(Source0, Matcher)); - const std::string Source1 = R"( + StringRef Source1 = R"( void x() { #pragma omp taskyield ; })"; EXPECT_TRUE(matchesWithOpenMP(Source1, Matcher)); - const std::string Source2 = R"( + StringRef Source2 = R"( void x() { ; })"; @@ -1834,34 +1826,34 @@ void x() { TEST(OMPDefaultClause, Matches) { auto Matcher = ompExecutableDirective(hasAnyClause(ompDefaultClause())); - const std::string Source0 = R"( + StringRef Source0 = R"( void x() { ; })"; EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); - const std::string Source1 = R"( + StringRef Source1 = R"( void x() { #pragma omp parallel ; })"; EXPECT_TRUE(notMatchesWithOpenMP(Source1, Matcher)); - const std::string Source2 = R"( + StringRef Source2 = R"( void x() { #pragma omp parallel default(none) ; })"; EXPECT_TRUE(matchesWithOpenMP(Source2, Matcher)); - const std::string Source3 = R"( + StringRef Source3 = R"( void x() { #pragma omp parallel default(shared) ; })"; EXPECT_TRUE(matchesWithOpenMP(Source3, Matcher)); - const std::string Source4 = R"( + StringRef Source4 = R"( void x(int x) { #pragma omp parallel num_threads(x) ; @@ -1871,7 +1863,7 @@ void x(int x) { TEST(MatchFinderAPI, matchesDynamic) { - std::string SourceCode = "struct A { void f() {} };"; + StringRef SourceCode = "struct A { void f() {} };"; auto Matcher = functionDecl(isDefinition()).bind("method"); auto astUnit = tooling::buildASTFromCode(SourceCode); diff --git a/clang/unittests/ASTMatchers/ASTMatchersTest.h b/clang/unittests/ASTMatchers/ASTMatchersTest.h index 8bf23a5aca19..65cb6b07af70 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTest.h +++ b/clang/unittests/ASTMatchers/ASTMatchersTest.h @@ -70,10 +70,10 @@ enum class LanguageMode { template <typename T> testing::AssertionResult matchesConditionally( - const std::string &Code, const T &AMatcher, bool ExpectMatch, + const Twine &Code, const T &AMatcher, bool ExpectMatch, llvm::ArrayRef<llvm::StringRef> CompileArgs, const FileContentMappings &VirtualMappedFiles = FileContentMappings(), - const std::string &Filename = "input.cc") { + StringRef Filename = "input.cc") { bool Found = false, DynamicFound = false; MatchFinder Finder; VerifyMatch VerifyFound(nullptr, &Found); @@ -117,10 +117,10 @@ testing::AssertionResult matchesConditionally( template <typename T> testing::AssertionResult matchesConditionally( - const std::string &Code, const T &AMatcher, bool ExpectMatch, + const Twine &Code, const T &AMatcher, bool ExpectMatch, llvm::StringRef CompileArg, const FileContentMappings &VirtualMappedFiles = FileContentMappings(), - const std::string &Filename = "input.cc") { + StringRef Filename = "input.cc") { return matchesConditionally(Code, AMatcher, ExpectMatch, llvm::makeArrayRef(CompileArg), VirtualMappedFiles, Filename); @@ -128,8 +128,8 @@ testing::AssertionResult matchesConditionally( template <typename T> testing::AssertionResult -matchesConditionally(const std::string &Code, const T &AMatcher, - bool ExpectMatch, const LanguageMode &Mode) { +matchesConditionally(const Twine &Code, const T &AMatcher, bool ExpectMatch, + const LanguageMode &Mode) { std::vector<LanguageMode> LangModes; switch (Mode) { case LanguageMode::Cxx11: @@ -184,20 +184,20 @@ matchesConditionally(const std::string &Code, const T &AMatcher, template <typename T> testing::AssertionResult -matches(const std::string &Code, const T &AMatcher, +matches(const Twine &Code, const T &AMatcher, const LanguageMode &Mode = LanguageMode::Cxx11) { return matchesConditionally(Code, AMatcher, true, Mode); } template <typename T> testing::AssertionResult -notMatches(const std::string &Code, const T &AMatcher, +notMatches(const Twine &Code, const T &AMatcher, const LanguageMode &Mode = LanguageMode::Cxx11) { return matchesConditionally(Code, AMatcher, false, Mode); } template <typename T> -testing::AssertionResult matchesObjC(const std::string &Code, const T &AMatcher, +testing::AssertionResult matchesObjC(const Twine &Code, const T &AMatcher, bool ExpectMatch = true) { return matchesConditionally(Code, AMatcher, ExpectMatch, {"-fobjc-nonfragile-abi", "-Wno-objc-root-class", @@ -206,38 +206,34 @@ testing::AssertionResult matchesObjC(const std::string &Code, const T &AMatcher, } template <typename T> -testing::AssertionResult matchesC(const std::string &Code, const T &AMatcher) { +testing::AssertionResult matchesC(const Twine &Code, const T &AMatcher) { return matchesConditionally(Code, AMatcher, true, "", FileContentMappings(), "input.c"); } template <typename T> -testing::AssertionResult matchesC99(const std::string &Code, - const T &AMatcher) { +testing::AssertionResult matchesC99(const Twine &Code, const T &AMatcher) { return matchesConditionally(Code, AMatcher, true, "-std=c99", FileContentMappings(), "input.c"); } template <typename T> -testing::AssertionResult notMatchesC(const std::string &Code, - const T &AMatcher) { +testing::AssertionResult notMatchesC(const Twine &Code, const T &AMatcher) { return matchesConditionally(Code, AMatcher, false, "", FileContentMappings(), "input.c"); } template <typename T> -testing::AssertionResult notMatchesObjC(const std::string &Code, - const T &AMatcher) { +testing::AssertionResult notMatchesObjC(const Twine &Code, const T &AMatcher) { return matchesObjC(Code, AMatcher, false); } - // Function based on matchesConditionally with "-x cuda" argument added and // small CUDA header prepended to the code string. template <typename T> -testing::AssertionResult matchesConditionallyWithCuda( - const std::string &Code, const T &AMatcher, bool ExpectMatch, - llvm::StringRef CompileArg) { +testing::AssertionResult +matchesConditionallyWithCuda(const Twine &Code, const T &AMatcher, + bool ExpectMatch, llvm::StringRef CompileArg) { const std::string CudaHeader = "typedef unsigned int size_t;\n" "#define __constant__ __attribute__((constant))\n" @@ -293,34 +289,32 @@ testing::AssertionResult matchesConditionallyWithCuda( } template <typename T> -testing::AssertionResult matchesWithCuda(const std::string &Code, - const T &AMatcher) { +testing::AssertionResult matchesWithCuda(const Twine &Code, const T &AMatcher) { return matchesConditionallyWithCuda(Code, AMatcher, true, "-std=c++11"); } template <typename T> -testing::AssertionResult notMatchesWithCuda(const std::string &Code, - const T &AMatcher) { +testing::AssertionResult notMatchesWithCuda(const Twine &Code, + const T &AMatcher) { return matchesConditionallyWithCuda(Code, AMatcher, false, "-std=c++11"); } template <typename T> -testing::AssertionResult matchesWithOpenMP(const std::string &Code, +testing::AssertionResult matchesWithOpenMP(const Twine &Code, const T &AMatcher) { return matchesConditionally(Code, AMatcher, true, "-fopenmp=libomp"); } template <typename T> -testing::AssertionResult notMatchesWithOpenMP(const std::string &Code, +testing::AssertionResult notMatchesWithOpenMP(const Twine &Code, const T &AMatcher) { return matchesConditionally(Code, AMatcher, false, "-fopenmp=libomp"); } template <typename T> -testing::AssertionResult -matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher, - std::unique_ptr<BoundNodesCallback> FindResultVerifier, - bool ExpectResult) { +testing::AssertionResult matchAndVerifyResultConditionally( + const Twine &Code, const T &AMatcher, + std::unique_ptr<BoundNodesCallback> FindResultVerifier, bool ExpectResult) { bool VerifiedResult = false; MatchFinder Finder; VerifyMatch VerifyVerifiedResult(std::move(FindResultVerifier), &VerifiedResult); @@ -344,7 +338,9 @@ matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher, } VerifiedResult = false; - std::unique_ptr<ASTUnit> AST(buildASTFromCodeWithArgs(Code, Args)); + SmallString<256> Buffer; + std::unique_ptr<ASTUnit> AST( + buildASTFromCodeWithArgs(Code.toStringRef(Buffer), Args)); if (!AST.get()) return testing::AssertionFailure() << "Parsing error in \"" << Code << "\" while building AST"; @@ -363,17 +359,17 @@ matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher, // FIXME: Find better names for these functions (or document what they // do more precisely). template <typename T> -testing::AssertionResult -matchAndVerifyResultTrue(const std::string &Code, const T &AMatcher, - std::unique_ptr<BoundNodesCallback> FindResultVerifier) { +testing::AssertionResult matchAndVerifyResultTrue( + const Twine &Code, const T &AMatcher, + std::unique_ptr<BoundNodesCallback> FindResultVerifier) { return matchAndVerifyResultConditionally( Code, AMatcher, std::move(FindResultVerifier), true); } template <typename T> -testing::AssertionResult -matchAndVerifyResultFalse(const std::string &Code, const T &AMatcher, - std::unique_ptr<BoundNodesCallback> FindResultVerifier) { +testing::AssertionResult matchAndVerifyResultFalse( + const Twine &Code, const T &AMatcher, + std::unique_ptr<BoundNodesCallback> FindResultVerifier) { return matchAndVerifyResultConditionally( Code, AMatcher, std::move(FindResultVerifier), false); } diff --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp index 93b0eefa676b..445bfd0b9f15 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp @@ -545,13 +545,13 @@ TEST(Matcher, isInstanceMethod) { } TEST(MatcherCXXMemberCallExpr, On) { - auto Snippet1 = R"cc( + StringRef Snippet1 = R"cc( struct Y { void m(); }; void z(Y y) { y.m(); } )cc"; - auto Snippet2 = R"cc( + StringRef Snippet2 = R"cc( struct Y { void m(); }; @@ -566,7 +566,7 @@ TEST(MatcherCXXMemberCallExpr, On) { EXPECT_TRUE(matches(Snippet2, MatchesX)); // Parens are ignored. - auto Snippet3 = R"cc( + StringRef Snippet3 = R"cc( struct Y { void m(); }; @@ -578,13 +578,13 @@ TEST(MatcherCXXMemberCallExpr, On) { } TEST(MatcherCXXMemberCallExpr, OnImplicitObjectArgument) { - auto Snippet1 = R"cc( + StringRef Snippet1 = R"cc( struct Y { void m(); }; void z(Y y) { y.m(); } )cc"; - auto Snippet2 = R"cc( + StringRef Snippet2 = R"cc( struct Y { void m(); }; @@ -603,7 +603,7 @@ TEST(MatcherCXXMemberCallExpr, OnImplicitObjectArgument) { EXPECT_TRUE(notMatches(Snippet2, MatchesX)); // Parens are not ignored. - auto Snippet3 = R"cc( + StringRef Snippet3 = R"cc( struct Y { void m(); }; @@ -617,13 +617,13 @@ TEST(MatcherCXXMemberCallExpr, OnImplicitObjectArgument) { } TEST(Matcher, HasObjectExpr) { - auto Snippet1 = R"cc( + StringRef Snippet1 = R"cc( struct X { int m; int f(X x) { return x.m; } }; )cc"; - auto Snippet2 = R"cc( + StringRef Snippet2 = R"cc( struct X { int m; int f(X x) { return m; } @@ -1622,7 +1622,7 @@ TEST(IgnoringImplicit, MatchesImplicit) { } TEST(IgnoringImplicit, MatchesNestedImplicit) { - const char *Code = R"( + StringRef Code = R"( struct OtherType; @@ -1666,7 +1666,7 @@ TEST(IgnoringImplicit, DoesNotMatchIncorrectly) { TEST(Traversal, traverseMatcher) { - const char *VarDeclCode = R"cpp( + StringRef VarDeclCode = R"cpp( void foo() { int i = 3.0; @@ -1727,7 +1727,7 @@ void foo() matches(VarDeclCode, functionDecl(traverse(TK_IgnoreUnlessSpelledInSource, hasAnyName("foo", "bar"))))); - const char *Code = R"cpp( + StringRef Code = R"cpp( void foo(int a) { int i = 3.0 + a; @@ -1896,7 +1896,7 @@ void actual_template_test() { template <typename MatcherT> bool matcherTemplateWithBinding(StringRef Code, const MatcherT &M) { return matchAndVerifyResultTrue( - std::string(Code), M.bind("matchedStmt"), + Code, M.bind("matchedStmt"), std::make_unique<VerifyIdIsBoundTo<ReturnStmt>>("matchedStmt", 1)); } @@ -1917,7 +1917,7 @@ int foo() TEST(Traversal, traverseMatcherNesting) { - const char *Code = R"cpp( + StringRef Code = R"cpp( float bar(int i) { return i; @@ -1937,7 +1937,7 @@ void foo() } TEST(Traversal, traverseMatcherThroughImplicit) { - const char *Code = R"cpp( + StringRef Code = R"cpp( struct S { S(int x); }; @@ -1957,7 +1957,7 @@ void constructImplicit() { TEST(Traversal, traverseMatcherThroughMemoization) { - const char *Code = R"cpp( + StringRef Code = R"cpp( void foo() { int i = 3.0; @@ -1982,7 +1982,7 @@ void foo() TEST(Traversal, traverseUnlessSpelledInSource) { - const char *Code = R"cpp( + StringRef Code = R"cpp( struct A { @@ -3049,8 +3049,8 @@ TEST(NNS, BindsNestedNameSpecifierLocs) { } TEST(NNS, DescendantsOfNestedNameSpecifiers) { - std::string Fragment = - "namespace a { struct A { struct B { struct C {}; }; }; };" + StringRef Fragment = + "namespace a { struct A { struct B { struct C {}; }; }; };" "void f() { a::A::B::C c; }"; EXPECT_TRUE(matches( Fragment, @@ -3078,8 +3078,8 @@ TEST(NNS, DescendantsOfNestedNameSpecifiers) { } TEST(NNS, NestedNameSpecifiersAsDescendants) { - std::string Fragment = - "namespace a { struct A { struct B { struct C {}; }; }; };" + StringRef Fragment = + "namespace a { struct A { struct B { struct C {}; }; }; };" "void f() { a::A::B::C c; }"; EXPECT_TRUE(matches( Fragment, @@ -3094,8 +3094,8 @@ TEST(NNS, NestedNameSpecifiersAsDescendants) { } TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) { - std::string Fragment = - "namespace a { struct A { struct B { struct C {}; }; }; };" + StringRef Fragment = + "namespace a { struct A { struct B { struct C {}; }; }; };" "void f() { a::A::B::C c; }"; EXPECT_TRUE(matches( Fragment, @@ -3121,8 +3121,8 @@ TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) { } TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) { - std::string Fragment = - "namespace a { struct A { struct B { struct C {}; }; }; };" + StringRef Fragment = + "namespace a { struct A { struct B { struct C {}; }; }; };" "void f() { a::A::B::C c; }"; EXPECT_TRUE(matches( Fragment, @@ -3198,21 +3198,19 @@ TEST(StatementMatcher, HasReturnValue) { } TEST(StatementMatcher, ForFunction) { - const auto CppString1 = - "struct PosVec {" - " PosVec& operator=(const PosVec&) {" - " auto x = [] { return 1; };" - " return *this;" - " }" - "};"; - const auto CppString2 = - "void F() {" - " struct S {" - " void F2() {" - " return;" - " }" - " };" - "}"; + StringRef CppString1 = "struct PosVec {" + " PosVec& operator=(const PosVec&) {" + " auto x = [] { return 1; };" + " return *this;" + " }" + "};"; + StringRef CppString2 = "void F() {" + " struct S {" + " void F2() {" + " return;" + " }" + " };" + "}"; EXPECT_TRUE( matches( CppString1, @@ -3277,10 +3275,10 @@ TEST(Matcher, ForEachOverriden) { } TEST(Matcher, HasAnyDeclaration) { - std::string Fragment = "void foo(int p1);" - "void foo(int *p2);" - "void bar(int p3);" - "template <typename T> void baz(T t) { foo(t); }"; + StringRef Fragment = "void foo(int p1);" + "void foo(int *p2);" + "void bar(int p3);" + "template <typename T> void baz(T t) { foo(t); }"; EXPECT_TRUE( matches(Fragment, unresolvedLookupExpr(hasAnyDeclaration(functionDecl( @@ -3296,10 +3294,10 @@ TEST(Matcher, HasAnyDeclaration) { } TEST(SubstTemplateTypeParmType, HasReplacementType) { - std::string Fragment = "template<typename T>" - "double F(T t);" - "int i;" - "double j = F(i);"; + StringRef Fragment = "template<typename T>" + "double F(T t);" + "int i;" + "double j = F(i);"; EXPECT_TRUE(matches(Fragment, substTemplateTypeParmType(hasReplacementType( qualType(asString("int")))))); EXPECT_TRUE(notMatches(Fragment, substTemplateTypeParmType(hasReplacementType( _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits