Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 1 | //=== unittests/Sema/ExternalSemaSourceTest.cpp - ExternalSemaSource 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 |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "clang/AST/ASTConsumer.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/Frontend/CompilerInstance.h" |
| 12 | #include "clang/Lex/Preprocessor.h" |
| 13 | #include "clang/Parse/ParseAST.h" |
| 14 | #include "clang/Sema/ExternalSemaSource.h" |
| 15 | #include "clang/Sema/Sema.h" |
| 16 | #include "clang/Sema/SemaDiagnostic.h" |
| 17 | #include "clang/Sema/TypoCorrection.h" |
| 18 | #include "clang/Tooling/Tooling.h" |
| 19 | #include "gtest/gtest.h" |
| 20 | |
| 21 | using namespace clang; |
| 22 | using namespace clang::tooling; |
| 23 | |
| 24 | namespace { |
| 25 | |
Kaelyn Uhrain | 2c351bb | 2013-08-12 22:11:14 +0000 | [diff] [blame] | 26 | // \brief Counts the number of times MaybeDiagnoseMissingCompleteType |
| 27 | // is called. Returns the result it was provided on creation. |
| 28 | class CompleteTypeDiagnoser : public clang::ExternalSemaSource { |
| 29 | public: |
| 30 | CompleteTypeDiagnoser(bool MockResult) : CallCount(0), Result(MockResult) {} |
| 31 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 32 | bool MaybeDiagnoseMissingCompleteType(SourceLocation L, QualType T) override { |
Kaelyn Uhrain | 2c351bb | 2013-08-12 22:11:14 +0000 | [diff] [blame] | 33 | ++CallCount; |
| 34 | return Result; |
| 35 | } |
| 36 | |
| 37 | int CallCount; |
| 38 | bool Result; |
| 39 | }; |
| 40 | |
Benjamin Kramer | b872733 | 2016-05-19 10:46:10 +0000 | [diff] [blame] | 41 | /// Counts the number of typo-correcting diagnostics correcting from one name to |
| 42 | /// another while still passing all diagnostics along a chain of consumers. |
| 43 | class DiagnosticWatcher : public clang::DiagnosticConsumer { |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 44 | DiagnosticConsumer *Chained; |
Benjamin Kramer | b872733 | 2016-05-19 10:46:10 +0000 | [diff] [blame] | 45 | std::string FromName; |
| 46 | std::string ToName; |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 47 | |
| 48 | public: |
Benjamin Kramer | b872733 | 2016-05-19 10:46:10 +0000 | [diff] [blame] | 49 | DiagnosticWatcher(StringRef From, StringRef To) |
| 50 | : Chained(nullptr), FromName(From), ToName("'"), SeenCount(0) { |
| 51 | ToName.append(To); |
| 52 | ToName.append("'"); |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 55 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 56 | const Diagnostic &Info) override { |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 57 | if (Chained) |
| 58 | Chained->HandleDiagnostic(DiagLevel, Info); |
| 59 | if (Info.getID() - 1 == diag::err_using_directive_member_suggest) { |
| 60 | const IdentifierInfo *Ident = Info.getArgIdentifier(0); |
| 61 | const std::string &CorrectedQuotedStr = Info.getArgStdStr(1); |
Benjamin Kramer | b872733 | 2016-05-19 10:46:10 +0000 | [diff] [blame] | 62 | if (Ident->getName() == FromName && CorrectedQuotedStr == ToName) |
| 63 | ++SeenCount; |
| 64 | } else if (Info.getID() == diag::err_no_member_suggest) { |
| 65 | auto Ident = DeclarationName::getFromOpaqueInteger(Info.getRawArg(0)); |
| 66 | const std::string &CorrectedQuotedStr = Info.getArgStdStr(3); |
| 67 | if (Ident.getAsString() == FromName && CorrectedQuotedStr == ToName) |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 68 | ++SeenCount; |
| 69 | } |
| 70 | } |
| 71 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 72 | void clear() override { |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 73 | DiagnosticConsumer::clear(); |
| 74 | if (Chained) |
| 75 | Chained->clear(); |
| 76 | } |
| 77 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 78 | bool IncludeInDiagnosticCounts() const override { |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 79 | if (Chained) |
| 80 | return Chained->IncludeInDiagnosticCounts(); |
| 81 | return false; |
| 82 | } |
| 83 | |
Benjamin Kramer | b872733 | 2016-05-19 10:46:10 +0000 | [diff] [blame] | 84 | DiagnosticWatcher *Chain(DiagnosticConsumer *ToChain) { |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 85 | Chained = ToChain; |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | int SeenCount; |
| 90 | }; |
| 91 | |
| 92 | // \brief Always corrects a typo matching CorrectFrom with a new namespace |
| 93 | // with the name CorrectTo. |
| 94 | class NamespaceTypoProvider : public clang::ExternalSemaSource { |
| 95 | std::string CorrectFrom; |
| 96 | std::string CorrectTo; |
| 97 | Sema *CurrentSema; |
| 98 | |
| 99 | public: |
| 100 | NamespaceTypoProvider(StringRef From, StringRef To) |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 101 | : CorrectFrom(From), CorrectTo(To), CurrentSema(nullptr), CallCount(0) {} |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 102 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 103 | void InitializeSema(Sema &S) override { CurrentSema = &S; } |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 104 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 105 | void ForgetSema() override { CurrentSema = nullptr; } |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 106 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 107 | TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo, int LookupKind, |
| 108 | Scope *S, CXXScopeSpec *SS, |
| 109 | CorrectionCandidateCallback &CCC, |
| 110 | DeclContext *MemberContext, bool EnteringContext, |
| 111 | const ObjCObjectPointerType *OPT) override { |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 112 | ++CallCount; |
| 113 | if (CurrentSema && Typo.getName().getAsString() == CorrectFrom) { |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 114 | DeclContext *DestContext = nullptr; |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 115 | ASTContext &Context = CurrentSema->getASTContext(); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 116 | if (SS) |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 117 | DestContext = CurrentSema->computeDeclContext(*SS, EnteringContext); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 118 | if (!DestContext) |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 119 | DestContext = Context.getTranslationUnitDecl(); |
| 120 | IdentifierInfo *ToIdent = |
| 121 | CurrentSema->getPreprocessor().getIdentifierInfo(CorrectTo); |
| 122 | NamespaceDecl *NewNamespace = |
| 123 | NamespaceDecl::Create(Context, DestContext, false, Typo.getBeginLoc(), |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 124 | Typo.getLoc(), ToIdent, nullptr); |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 125 | DestContext->addDecl(NewNamespace); |
| 126 | TypoCorrection Correction(ToIdent); |
| 127 | Correction.addCorrectionDecl(NewNamespace); |
| 128 | return Correction; |
| 129 | } |
| 130 | return TypoCorrection(); |
| 131 | } |
| 132 | |
| 133 | int CallCount; |
| 134 | }; |
| 135 | |
Benjamin Kramer | b872733 | 2016-05-19 10:46:10 +0000 | [diff] [blame] | 136 | class FunctionTypoProvider : public clang::ExternalSemaSource { |
| 137 | std::string CorrectFrom; |
| 138 | std::string CorrectTo; |
| 139 | Sema *CurrentSema; |
| 140 | |
| 141 | public: |
| 142 | FunctionTypoProvider(StringRef From, StringRef To) |
| 143 | : CorrectFrom(From), CorrectTo(To), CurrentSema(nullptr), CallCount(0) {} |
| 144 | |
| 145 | void InitializeSema(Sema &S) override { CurrentSema = &S; } |
| 146 | |
| 147 | void ForgetSema() override { CurrentSema = nullptr; } |
| 148 | |
| 149 | TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo, int LookupKind, |
| 150 | Scope *S, CXXScopeSpec *SS, |
| 151 | CorrectionCandidateCallback &CCC, |
| 152 | DeclContext *MemberContext, bool EnteringContext, |
| 153 | const ObjCObjectPointerType *OPT) override { |
| 154 | ++CallCount; |
| 155 | if (CurrentSema && Typo.getName().getAsString() == CorrectFrom) { |
| 156 | DeclContext *DestContext = nullptr; |
| 157 | ASTContext &Context = CurrentSema->getASTContext(); |
| 158 | if (SS) |
| 159 | DestContext = CurrentSema->computeDeclContext(*SS, EnteringContext); |
| 160 | if (!DestContext) |
| 161 | DestContext = Context.getTranslationUnitDecl(); |
| 162 | IdentifierInfo *ToIdent = |
| 163 | CurrentSema->getPreprocessor().getIdentifierInfo(CorrectTo); |
| 164 | auto *NewFunction = FunctionDecl::Create( |
| 165 | Context, DestContext, SourceLocation(), SourceLocation(), ToIdent, |
| 166 | Context.getFunctionType(Context.VoidTy, {}, {}), nullptr, SC_Static); |
| 167 | DestContext->addDecl(NewFunction); |
| 168 | TypoCorrection Correction(ToIdent); |
| 169 | Correction.addCorrectionDecl(NewFunction); |
| 170 | return Correction; |
| 171 | } |
| 172 | return TypoCorrection(); |
| 173 | } |
| 174 | |
| 175 | int CallCount; |
| 176 | }; |
| 177 | |
| 178 | // \brief Chains together a vector of DiagnosticWatchers and |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 179 | // adds a vector of ExternalSemaSources to the CompilerInstance before |
| 180 | // performing semantic analysis. |
| 181 | class ExternalSemaSourceInstaller : public clang::ASTFrontendAction { |
Benjamin Kramer | b872733 | 2016-05-19 10:46:10 +0000 | [diff] [blame] | 182 | std::vector<DiagnosticWatcher *> Watchers; |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 183 | std::vector<clang::ExternalSemaSource *> Sources; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 184 | std::unique_ptr<DiagnosticConsumer> OwnedClient; |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 185 | |
| 186 | protected: |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 187 | std::unique_ptr<clang::ASTConsumer> |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 188 | CreateASTConsumer(clang::CompilerInstance &Compiler, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 189 | llvm::StringRef /* dummy */) override { |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 190 | return std::make_unique<clang::ASTConsumer>(); |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 193 | void ExecuteAction() override { |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 194 | CompilerInstance &CI = getCompilerInstance(); |
| 195 | ASSERT_FALSE(CI.hasSema()); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 196 | CI.createSema(getTranslationUnitKind(), nullptr); |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 197 | ASSERT_TRUE(CI.hasDiagnostics()); |
| 198 | DiagnosticsEngine &Diagnostics = CI.getDiagnostics(); |
| 199 | DiagnosticConsumer *Client = Diagnostics.getClient(); |
| 200 | if (Diagnostics.ownsClient()) |
Alexander Kornienko | 41c247a | 2014-11-17 23:46:02 +0000 | [diff] [blame] | 201 | OwnedClient = Diagnostics.takeClient(); |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 202 | for (size_t I = 0, E = Watchers.size(); I < E; ++I) |
| 203 | Client = Watchers[I]->Chain(Client); |
| 204 | Diagnostics.setClient(Client, false); |
| 205 | for (size_t I = 0, E = Sources.size(); I < E; ++I) { |
| 206 | Sources[I]->InitializeSema(CI.getSema()); |
| 207 | CI.getSema().addExternalSource(Sources[I]); |
| 208 | } |
| 209 | ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats, |
| 210 | CI.getFrontendOpts().SkipFunctionBodies); |
| 211 | } |
| 212 | |
| 213 | public: |
| 214 | void PushSource(clang::ExternalSemaSource *Source) { |
| 215 | Sources.push_back(Source); |
| 216 | } |
| 217 | |
Benjamin Kramer | b872733 | 2016-05-19 10:46:10 +0000 | [diff] [blame] | 218 | void PushWatcher(DiagnosticWatcher *Watcher) { Watchers.push_back(Watcher); } |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
Benjamin Kramer | b872733 | 2016-05-19 10:46:10 +0000 | [diff] [blame] | 221 | // Make sure that the DiagnosticWatcher is not miscounting. |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 222 | TEST(ExternalSemaSource, SanityCheck) { |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 223 | std::unique_ptr<ExternalSemaSourceInstaller> Installer( |
| 224 | new ExternalSemaSourceInstaller); |
Benjamin Kramer | b872733 | 2016-05-19 10:46:10 +0000 | [diff] [blame] | 225 | DiagnosticWatcher Watcher("AAB", "BBB"); |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 226 | Installer->PushWatcher(&Watcher); |
| 227 | std::vector<std::string> Args(1, "-std=c++11"); |
| 228 | ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs( |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 229 | Installer.release(), "namespace AAA { } using namespace AAB;", Args)); |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 230 | ASSERT_EQ(0, Watcher.SeenCount); |
| 231 | } |
| 232 | |
| 233 | // Check that when we add a NamespaceTypeProvider, we use that suggestion |
| 234 | // instead of the usual suggestion we would use above. |
| 235 | TEST(ExternalSemaSource, ExternalTypoCorrectionPrioritized) { |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 236 | std::unique_ptr<ExternalSemaSourceInstaller> Installer( |
| 237 | new ExternalSemaSourceInstaller); |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 238 | NamespaceTypoProvider Provider("AAB", "BBB"); |
Benjamin Kramer | b872733 | 2016-05-19 10:46:10 +0000 | [diff] [blame] | 239 | DiagnosticWatcher Watcher("AAB", "BBB"); |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 240 | Installer->PushSource(&Provider); |
| 241 | Installer->PushWatcher(&Watcher); |
| 242 | std::vector<std::string> Args(1, "-std=c++11"); |
| 243 | ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs( |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 244 | Installer.release(), "namespace AAA { } using namespace AAB;", Args)); |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 245 | ASSERT_LE(0, Provider.CallCount); |
| 246 | ASSERT_EQ(1, Watcher.SeenCount); |
| 247 | } |
| 248 | |
| 249 | // Check that we use the first successful TypoCorrection returned from an |
| 250 | // ExternalSemaSource. |
| 251 | TEST(ExternalSemaSource, ExternalTypoCorrectionOrdering) { |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 252 | std::unique_ptr<ExternalSemaSourceInstaller> Installer( |
| 253 | new ExternalSemaSourceInstaller); |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 254 | NamespaceTypoProvider First("XXX", "BBB"); |
| 255 | NamespaceTypoProvider Second("AAB", "CCC"); |
| 256 | NamespaceTypoProvider Third("AAB", "DDD"); |
Benjamin Kramer | b872733 | 2016-05-19 10:46:10 +0000 | [diff] [blame] | 257 | DiagnosticWatcher Watcher("AAB", "CCC"); |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 258 | Installer->PushSource(&First); |
| 259 | Installer->PushSource(&Second); |
| 260 | Installer->PushSource(&Third); |
| 261 | Installer->PushWatcher(&Watcher); |
| 262 | std::vector<std::string> Args(1, "-std=c++11"); |
| 263 | ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs( |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 264 | Installer.release(), "namespace AAA { } using namespace AAB;", Args)); |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 265 | ASSERT_LE(1, First.CallCount); |
| 266 | ASSERT_LE(1, Second.CallCount); |
| 267 | ASSERT_EQ(0, Third.CallCount); |
| 268 | ASSERT_EQ(1, Watcher.SeenCount); |
| 269 | } |
| 270 | |
Benjamin Kramer | b872733 | 2016-05-19 10:46:10 +0000 | [diff] [blame] | 271 | TEST(ExternalSemaSource, ExternalDelayedTypoCorrection) { |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 272 | std::unique_ptr<ExternalSemaSourceInstaller> Installer( |
| 273 | new ExternalSemaSourceInstaller); |
Benjamin Kramer | b872733 | 2016-05-19 10:46:10 +0000 | [diff] [blame] | 274 | FunctionTypoProvider Provider("aaa", "bbb"); |
| 275 | DiagnosticWatcher Watcher("aaa", "bbb"); |
| 276 | Installer->PushSource(&Provider); |
| 277 | Installer->PushWatcher(&Watcher); |
| 278 | std::vector<std::string> Args(1, "-std=c++11"); |
| 279 | ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs( |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 280 | Installer.release(), "namespace AAA { } void foo() { AAA::aaa(); }", |
Benjamin Kramer | b872733 | 2016-05-19 10:46:10 +0000 | [diff] [blame] | 281 | Args)); |
| 282 | ASSERT_LE(0, Provider.CallCount); |
| 283 | ASSERT_EQ(1, Watcher.SeenCount); |
| 284 | } |
| 285 | |
Kaelyn Uhrain | 2c351bb | 2013-08-12 22:11:14 +0000 | [diff] [blame] | 286 | // We should only try MaybeDiagnoseMissingCompleteType if we can't otherwise |
| 287 | // solve the problem. |
| 288 | TEST(ExternalSemaSource, TryOtherTacticsBeforeDiagnosing) { |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 289 | std::unique_ptr<ExternalSemaSourceInstaller> Installer( |
| 290 | new ExternalSemaSourceInstaller); |
Kaelyn Uhrain | 2c351bb | 2013-08-12 22:11:14 +0000 | [diff] [blame] | 291 | CompleteTypeDiagnoser Diagnoser(false); |
| 292 | Installer->PushSource(&Diagnoser); |
| 293 | std::vector<std::string> Args(1, "-std=c++11"); |
| 294 | // This code hits the class template specialization/class member of a class |
| 295 | // template specialization checks in Sema::RequireCompleteTypeImpl. |
| 296 | ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs( |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 297 | Installer.release(), |
Kaelyn Uhrain | 2c351bb | 2013-08-12 22:11:14 +0000 | [diff] [blame] | 298 | "template <typename T> struct S { class C { }; }; S<char>::C SCInst;", |
| 299 | Args)); |
| 300 | ASSERT_EQ(0, Diagnoser.CallCount); |
| 301 | } |
| 302 | |
| 303 | // The first ExternalSemaSource where MaybeDiagnoseMissingCompleteType returns |
| 304 | // true should be the last one called. |
| 305 | TEST(ExternalSemaSource, FirstDiagnoserTaken) { |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 306 | std::unique_ptr<ExternalSemaSourceInstaller> Installer( |
| 307 | new ExternalSemaSourceInstaller); |
Kaelyn Uhrain | 2c351bb | 2013-08-12 22:11:14 +0000 | [diff] [blame] | 308 | CompleteTypeDiagnoser First(false); |
| 309 | CompleteTypeDiagnoser Second(true); |
| 310 | CompleteTypeDiagnoser Third(true); |
| 311 | Installer->PushSource(&First); |
| 312 | Installer->PushSource(&Second); |
| 313 | Installer->PushSource(&Third); |
| 314 | std::vector<std::string> Args(1, "-std=c++11"); |
| 315 | ASSERT_FALSE(clang::tooling::runToolOnCodeWithArgs( |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 316 | Installer.release(), "class Incomplete; Incomplete IncompleteInstance;", |
Kaelyn Uhrain | 2c351bb | 2013-08-12 22:11:14 +0000 | [diff] [blame] | 317 | Args)); |
| 318 | ASSERT_EQ(1, First.CallCount); |
| 319 | ASSERT_EQ(1, Second.CallCount); |
| 320 | ASSERT_EQ(0, Third.CallCount); |
| 321 | } |
| 322 | |
Kaelyn Uhrain | 4ebf576 | 2013-08-12 19:57:06 +0000 | [diff] [blame] | 323 | } // anonymous namespace |