ExternalSemaSourceTest.cpp 12 KB

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