Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 1 | //===- unittests/AST/NamedDeclPrinterTest.cpp --- NamedDecl printer tests -===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains tests for NamedDecl::printQualifiedName(). |
| 10 | // |
| 11 | // These tests have a coding convention: |
| 12 | // * declaration to be printed is named 'A' unless it should have some special |
| 13 | // name (e.g., 'operator+'); |
| 14 | // * additional helper declarations are 'Z', 'Y', 'X' and so on. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "clang/AST/ASTContext.h" |
Ilya Biryukov | 6648223 | 2019-09-25 15:46:04 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/AST/PrettyPrinter.h" |
Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 21 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 22 | #include "clang/Tooling/Tooling.h" |
| 23 | #include "llvm/ADT/SmallString.h" |
Ilya Biryukov | 6648223 | 2019-09-25 15:46:04 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 25 | #include "gtest/gtest.h" |
| 26 | |
| 27 | using namespace clang; |
| 28 | using namespace ast_matchers; |
| 29 | using namespace tooling; |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | class PrintMatch : public MatchFinder::MatchCallback { |
| 34 | SmallString<1024> Printed; |
| 35 | unsigned NumFoundDecls; |
Ilya Biryukov | 6648223 | 2019-09-25 15:46:04 +0000 | [diff] [blame] | 36 | std::function<void(llvm::raw_ostream &OS, const NamedDecl *)> Printer; |
Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 37 | |
| 38 | public: |
Ilya Biryukov | 6648223 | 2019-09-25 15:46:04 +0000 | [diff] [blame] | 39 | explicit PrintMatch( |
| 40 | std::function<void(llvm::raw_ostream &OS, const NamedDecl *)> Printer) |
| 41 | : NumFoundDecls(0), Printer(std::move(Printer)) {} |
Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 42 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 43 | void run(const MatchFinder::MatchResult &Result) override { |
Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 44 | const NamedDecl *ND = Result.Nodes.getNodeAs<NamedDecl>("id"); |
| 45 | if (!ND) |
| 46 | return; |
| 47 | NumFoundDecls++; |
| 48 | if (NumFoundDecls > 1) |
| 49 | return; |
| 50 | |
| 51 | llvm::raw_svector_ostream Out(Printed); |
Ilya Biryukov | 6648223 | 2019-09-25 15:46:04 +0000 | [diff] [blame] | 52 | Printer(Out, ND); |
Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | StringRef getPrinted() const { |
| 56 | return Printed; |
| 57 | } |
| 58 | |
| 59 | unsigned getNumFoundDecls() const { |
| 60 | return NumFoundDecls; |
| 61 | } |
| 62 | }; |
| 63 | |
Ilya Biryukov | 6648223 | 2019-09-25 15:46:04 +0000 | [diff] [blame] | 64 | ::testing::AssertionResult PrintedDeclMatches( |
| 65 | StringRef Code, const std::vector<std::string> &Args, |
| 66 | const DeclarationMatcher &NodeMatch, StringRef ExpectedPrinted, |
| 67 | StringRef FileName, |
| 68 | std::function<void(llvm::raw_ostream &, const NamedDecl *)> Print) { |
| 69 | PrintMatch Printer(std::move(Print)); |
Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 70 | MatchFinder Finder; |
| 71 | Finder.addMatcher(NodeMatch, &Printer); |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 72 | std::unique_ptr<FrontendActionFactory> Factory = |
| 73 | newFrontendActionFactory(&Finder); |
Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 74 | |
| 75 | if (!runToolOnCodeWithArgs(Factory->create(), Code, Args, FileName)) |
| 76 | return testing::AssertionFailure() |
| 77 | << "Parsing error in \"" << Code.str() << "\""; |
| 78 | |
| 79 | if (Printer.getNumFoundDecls() == 0) |
| 80 | return testing::AssertionFailure() |
| 81 | << "Matcher didn't find any named declarations"; |
| 82 | |
| 83 | if (Printer.getNumFoundDecls() > 1) |
| 84 | return testing::AssertionFailure() |
| 85 | << "Matcher should match only one named declaration " |
| 86 | "(found " << Printer.getNumFoundDecls() << ")"; |
| 87 | |
| 88 | if (Printer.getPrinted() != ExpectedPrinted) |
| 89 | return ::testing::AssertionFailure() |
| 90 | << "Expected \"" << ExpectedPrinted.str() << "\", " |
| 91 | "got \"" << Printer.getPrinted().str() << "\""; |
| 92 | |
| 93 | return ::testing::AssertionSuccess(); |
| 94 | } |
| 95 | |
| 96 | ::testing::AssertionResult |
Ilya Biryukov | 6648223 | 2019-09-25 15:46:04 +0000 | [diff] [blame] | 97 | PrintedNamedDeclMatches(StringRef Code, const std::vector<std::string> &Args, |
| 98 | bool SuppressUnwrittenScope, |
| 99 | const DeclarationMatcher &NodeMatch, |
| 100 | StringRef ExpectedPrinted, StringRef FileName) { |
| 101 | return PrintedDeclMatches(Code, Args, NodeMatch, ExpectedPrinted, FileName, |
| 102 | [=](llvm::raw_ostream &Out, const NamedDecl *ND) { |
| 103 | auto Policy = |
| 104 | ND->getASTContext().getPrintingPolicy(); |
| 105 | Policy.SuppressUnwrittenScope = |
| 106 | SuppressUnwrittenScope; |
| 107 | ND->printQualifiedName(Out, Policy); |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | ::testing::AssertionResult |
Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 112 | PrintedNamedDeclCXX98Matches(StringRef Code, StringRef DeclName, |
| 113 | StringRef ExpectedPrinted) { |
| 114 | std::vector<std::string> Args(1, "-std=c++98"); |
Benjamin Kramer | 4e3f4f0 | 2020-01-29 10:52:25 +0100 | [diff] [blame] | 115 | return PrintedNamedDeclMatches(Code, Args, |
| 116 | /*SuppressUnwrittenScope*/ false, |
| 117 | namedDecl(hasName(DeclName)).bind("id"), |
| 118 | ExpectedPrinted, "input.cc"); |
Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | ::testing::AssertionResult |
| 122 | PrintedWrittenNamedDeclCXX11Matches(StringRef Code, StringRef DeclName, |
| 123 | StringRef ExpectedPrinted) { |
| 124 | std::vector<std::string> Args(1, "-std=c++11"); |
Benjamin Kramer | 4e3f4f0 | 2020-01-29 10:52:25 +0100 | [diff] [blame] | 125 | return PrintedNamedDeclMatches(Code, Args, |
| 126 | /*SuppressUnwrittenScope*/ true, |
| 127 | namedDecl(hasName(DeclName)).bind("id"), |
| 128 | ExpectedPrinted, "input.cc"); |
Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 129 | } |
| 130 | |
David Goldman | 19d2185 | 2019-04-04 20:13:22 +0000 | [diff] [blame] | 131 | ::testing::AssertionResult |
| 132 | PrintedWrittenPropertyDeclObjCMatches(StringRef Code, StringRef DeclName, |
| 133 | StringRef ExpectedPrinted) { |
| 134 | std::vector<std::string> Args{"-std=c++11", "-xobjective-c++"}; |
Benjamin Kramer | 4e3f4f0 | 2020-01-29 10:52:25 +0100 | [diff] [blame] | 135 | return PrintedNamedDeclMatches(Code, Args, |
| 136 | /*SuppressUnwrittenScope*/ true, |
| 137 | objcPropertyDecl(hasName(DeclName)).bind("id"), |
| 138 | ExpectedPrinted, "input.m"); |
David Goldman | 19d2185 | 2019-04-04 20:13:22 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Ilya Biryukov | 6648223 | 2019-09-25 15:46:04 +0000 | [diff] [blame] | 141 | ::testing::AssertionResult |
| 142 | PrintedNestedNameSpecifierMatches(StringRef Code, StringRef DeclName, |
| 143 | StringRef ExpectedPrinted) { |
| 144 | std::vector<std::string> Args{"-std=c++11"}; |
Benjamin Kramer | 4e3f4f0 | 2020-01-29 10:52:25 +0100 | [diff] [blame] | 145 | return PrintedDeclMatches(Code, Args, namedDecl(hasName(DeclName)).bind("id"), |
| 146 | ExpectedPrinted, "input.cc", |
| 147 | [](llvm::raw_ostream &Out, const NamedDecl *D) { |
| 148 | D->printNestedNameSpecifier(Out); |
| 149 | }); |
Ilya Biryukov | 6648223 | 2019-09-25 15:46:04 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 152 | } // unnamed namespace |
| 153 | |
| 154 | TEST(NamedDeclPrinter, TestNamespace1) { |
| 155 | ASSERT_TRUE(PrintedNamedDeclCXX98Matches( |
| 156 | "namespace { int A; }", |
| 157 | "A", |
| 158 | "(anonymous namespace)::A")); |
| 159 | } |
| 160 | |
| 161 | TEST(NamedDeclPrinter, TestNamespace2) { |
| 162 | ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches( |
| 163 | "inline namespace Z { namespace { int A; } }", |
| 164 | "A", |
| 165 | "A")); |
| 166 | } |
Alexander Kornienko | 4f35532 | 2015-11-09 16:45:17 +0000 | [diff] [blame] | 167 | |
| 168 | TEST(NamedDeclPrinter, TestUnscopedUnnamedEnum) { |
| 169 | ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches( |
| 170 | "enum { A };", |
| 171 | "A", |
| 172 | "A")); |
| 173 | } |
| 174 | |
| 175 | TEST(NamedDeclPrinter, TestNamedEnum) { |
| 176 | ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches( |
| 177 | "enum X { A };", |
| 178 | "A", |
Paul Robinson | f183848 | 2017-12-21 21:47:22 +0000 | [diff] [blame] | 179 | "A")); |
Alexander Kornienko | 4f35532 | 2015-11-09 16:45:17 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | TEST(NamedDeclPrinter, TestScopedNamedEnum) { |
| 183 | ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches( |
| 184 | "enum class X { A };", |
| 185 | "A", |
| 186 | "X::A")); |
| 187 | } |
| 188 | |
| 189 | TEST(NamedDeclPrinter, TestClassWithUnscopedUnnamedEnum) { |
| 190 | ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches( |
| 191 | "class X { enum { A }; };", |
| 192 | "A", |
| 193 | "X::A")); |
| 194 | } |
| 195 | |
| 196 | TEST(NamedDeclPrinter, TestClassWithUnscopedNamedEnum) { |
| 197 | ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches( |
| 198 | "class X { enum Y { A }; };", |
| 199 | "A", |
Paul Robinson | f183848 | 2017-12-21 21:47:22 +0000 | [diff] [blame] | 200 | "X::A")); |
Alexander Kornienko | 4f35532 | 2015-11-09 16:45:17 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | TEST(NamedDeclPrinter, TestClassWithScopedNamedEnum) { |
| 204 | ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches( |
| 205 | "class X { enum class Y { A }; };", |
| 206 | "A", |
| 207 | "X::Y::A")); |
| 208 | } |
Sam McCall | 34f9d3f | 2018-02-02 13:34:47 +0000 | [diff] [blame] | 209 | |
| 210 | TEST(NamedDeclPrinter, TestLinkageInNamespace) { |
| 211 | ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches( |
| 212 | "namespace X { extern \"C\" { int A; } }", |
| 213 | "A", |
| 214 | "X::A")); |
| 215 | } |
David Goldman | 19d2185 | 2019-04-04 20:13:22 +0000 | [diff] [blame] | 216 | |
| 217 | TEST(NamedDeclPrinter, TestObjCClassExtension) { |
David Goldman | 4af5d74 | 2019-04-05 19:17:24 +0000 | [diff] [blame] | 218 | const char *Code = |
| 219 | R"( |
| 220 | @interface Obj |
| 221 | @end |
David Goldman | 19d2185 | 2019-04-04 20:13:22 +0000 | [diff] [blame] | 222 | |
David Goldman | 4af5d74 | 2019-04-05 19:17:24 +0000 | [diff] [blame] | 223 | @interface Obj () |
| 224 | @property(nonatomic) int property; |
| 225 | @end |
| 226 | )"; |
| 227 | ASSERT_TRUE(PrintedWrittenPropertyDeclObjCMatches( |
| 228 | Code, |
David Goldman | 19d2185 | 2019-04-04 20:13:22 +0000 | [diff] [blame] | 229 | "property", |
| 230 | "Obj::property")); |
| 231 | } |
| 232 | |
Haojian Wu | 6f56599 | 2020-05-19 15:10:10 +0200 | [diff] [blame] | 233 | TEST(NamedDeclPrinter, TestInstanceObjCClassExtension) { |
| 234 | const char *Code = |
| 235 | R"( |
| 236 | @interface ObjC |
| 237 | @end |
| 238 | @interface ObjC () { |
| 239 | char data; // legal with non-fragile ABI. |
| 240 | } |
| 241 | @end |
| 242 | )"; |
| 243 | |
| 244 | std::vector<std::string> Args{ |
| 245 | "-std=c++11", "-xobjective-c++", |
| 246 | "-fobjc-runtime=macosx" /*force to use non-fragile ABI*/}; |
| 247 | ASSERT_TRUE(PrintedNamedDeclMatches(Code, Args, |
| 248 | /*SuppressUnwrittenScope*/ true, |
| 249 | namedDecl(hasName("data")).bind("id"), |
| 250 | // not "::data" |
| 251 | "ObjC::data", "input.mm")); |
| 252 | } |
| 253 | |
David Goldman | 19d2185 | 2019-04-04 20:13:22 +0000 | [diff] [blame] | 254 | TEST(NamedDeclPrinter, TestObjCClassExtensionWithGetter) { |
David Goldman | 4af5d74 | 2019-04-05 19:17:24 +0000 | [diff] [blame] | 255 | const char *Code = |
| 256 | R"( |
| 257 | @interface Obj |
| 258 | @end |
David Goldman | 19d2185 | 2019-04-04 20:13:22 +0000 | [diff] [blame] | 259 | |
David Goldman | 4af5d74 | 2019-04-05 19:17:24 +0000 | [diff] [blame] | 260 | @interface Obj () |
| 261 | @property(nonatomic, getter=myPropertyGetter) int property; |
| 262 | @end |
| 263 | )"; |
| 264 | ASSERT_TRUE(PrintedWrittenPropertyDeclObjCMatches( |
| 265 | Code, |
David Goldman | 19d2185 | 2019-04-04 20:13:22 +0000 | [diff] [blame] | 266 | "property", |
| 267 | "Obj::property")); |
| 268 | } |
Ilya Biryukov | 6648223 | 2019-09-25 15:46:04 +0000 | [diff] [blame] | 269 | |
| 270 | TEST(NamedDeclPrinter, NestedNameSpecifierSimple) { |
| 271 | const char *Code = |
| 272 | R"( |
| 273 | namespace foo { namespace bar { void func(); } } |
| 274 | )"; |
| 275 | ASSERT_TRUE(PrintedNestedNameSpecifierMatches(Code, "func", "foo::bar::")); |
| 276 | } |
| 277 | |
| 278 | TEST(NamedDeclPrinter, NestedNameSpecifierTemplateArgs) { |
| 279 | const char *Code = |
| 280 | R"( |
| 281 | template <class T> struct vector; |
| 282 | template <> struct vector<int> { int method(); }; |
| 283 | )"; |
| 284 | ASSERT_TRUE( |
| 285 | PrintedNestedNameSpecifierMatches(Code, "method", "vector<int>::")); |
| 286 | } |