2013-08-13 03:57:06 +08:00
|
|
|
//=== unittests/Sema/ExternalSemaSourceTest.cpp - ExternalSemaSource tests ===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-08-13 03:57:06 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Parse/ParseAST.h"
|
|
|
|
#include "clang/Sema/ExternalSemaSource.h"
|
|
|
|
#include "clang/Sema/Sema.h"
|
|
|
|
#include "clang/Sema/SemaDiagnostic.h"
|
|
|
|
#include "clang/Sema/TypoCorrection.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::tooling;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-08-13 06:11:14 +08:00
|
|
|
// \brief Counts the number of times MaybeDiagnoseMissingCompleteType
|
|
|
|
// is called. Returns the result it was provided on creation.
|
|
|
|
class CompleteTypeDiagnoser : public clang::ExternalSemaSource {
|
|
|
|
public:
|
|
|
|
CompleteTypeDiagnoser(bool MockResult) : CallCount(0), Result(MockResult) {}
|
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
bool MaybeDiagnoseMissingCompleteType(SourceLocation L, QualType T) override {
|
2013-08-13 06:11:14 +08:00
|
|
|
++CallCount;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CallCount;
|
|
|
|
bool Result;
|
|
|
|
};
|
|
|
|
|
2016-05-19 18:46:10 +08:00
|
|
|
/// Counts the number of typo-correcting diagnostics correcting from one name to
|
|
|
|
/// another while still passing all diagnostics along a chain of consumers.
|
|
|
|
class DiagnosticWatcher : public clang::DiagnosticConsumer {
|
2013-08-13 03:57:06 +08:00
|
|
|
DiagnosticConsumer *Chained;
|
2016-05-19 18:46:10 +08:00
|
|
|
std::string FromName;
|
|
|
|
std::string ToName;
|
2013-08-13 03:57:06 +08:00
|
|
|
|
|
|
|
public:
|
2016-05-19 18:46:10 +08:00
|
|
|
DiagnosticWatcher(StringRef From, StringRef To)
|
|
|
|
: Chained(nullptr), FromName(From), ToName("'"), SeenCount(0) {
|
2020-01-29 03:23:46 +08:00
|
|
|
ToName.append(std::string(To));
|
2016-05-19 18:46:10 +08:00
|
|
|
ToName.append("'");
|
2013-08-13 03:57:06 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
const Diagnostic &Info) override {
|
2013-08-13 03:57:06 +08:00
|
|
|
if (Chained)
|
|
|
|
Chained->HandleDiagnostic(DiagLevel, Info);
|
|
|
|
if (Info.getID() - 1 == diag::err_using_directive_member_suggest) {
|
|
|
|
const IdentifierInfo *Ident = Info.getArgIdentifier(0);
|
|
|
|
const std::string &CorrectedQuotedStr = Info.getArgStdStr(1);
|
2016-05-19 18:46:10 +08:00
|
|
|
if (Ident->getName() == FromName && CorrectedQuotedStr == ToName)
|
|
|
|
++SeenCount;
|
|
|
|
} else if (Info.getID() == diag::err_no_member_suggest) {
|
|
|
|
auto Ident = DeclarationName::getFromOpaqueInteger(Info.getRawArg(0));
|
|
|
|
const std::string &CorrectedQuotedStr = Info.getArgStdStr(3);
|
|
|
|
if (Ident.getAsString() == FromName && CorrectedQuotedStr == ToName)
|
2013-08-13 03:57:06 +08:00
|
|
|
++SeenCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
void clear() override {
|
2013-08-13 03:57:06 +08:00
|
|
|
DiagnosticConsumer::clear();
|
|
|
|
if (Chained)
|
|
|
|
Chained->clear();
|
|
|
|
}
|
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
bool IncludeInDiagnosticCounts() const override {
|
2013-08-13 03:57:06 +08:00
|
|
|
if (Chained)
|
|
|
|
return Chained->IncludeInDiagnosticCounts();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-19 18:46:10 +08:00
|
|
|
DiagnosticWatcher *Chain(DiagnosticConsumer *ToChain) {
|
2013-08-13 03:57:06 +08:00
|
|
|
Chained = ToChain;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SeenCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
// \brief Always corrects a typo matching CorrectFrom with a new namespace
|
|
|
|
// with the name CorrectTo.
|
|
|
|
class NamespaceTypoProvider : public clang::ExternalSemaSource {
|
|
|
|
std::string CorrectFrom;
|
|
|
|
std::string CorrectTo;
|
|
|
|
Sema *CurrentSema;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NamespaceTypoProvider(StringRef From, StringRef To)
|
2014-06-08 16:38:12 +08:00
|
|
|
: CorrectFrom(From), CorrectTo(To), CurrentSema(nullptr), CallCount(0) {}
|
2013-08-13 03:57:06 +08:00
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
void InitializeSema(Sema &S) override { CurrentSema = &S; }
|
2013-08-13 03:57:06 +08:00
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
void ForgetSema() override { CurrentSema = nullptr; }
|
2013-08-13 03:57:06 +08:00
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo, int LookupKind,
|
|
|
|
Scope *S, CXXScopeSpec *SS,
|
|
|
|
CorrectionCandidateCallback &CCC,
|
|
|
|
DeclContext *MemberContext, bool EnteringContext,
|
|
|
|
const ObjCObjectPointerType *OPT) override {
|
2013-08-13 03:57:06 +08:00
|
|
|
++CallCount;
|
|
|
|
if (CurrentSema && Typo.getName().getAsString() == CorrectFrom) {
|
2014-06-08 16:38:12 +08:00
|
|
|
DeclContext *DestContext = nullptr;
|
2013-08-13 03:57:06 +08:00
|
|
|
ASTContext &Context = CurrentSema->getASTContext();
|
2014-06-08 16:38:12 +08:00
|
|
|
if (SS)
|
2013-08-13 03:57:06 +08:00
|
|
|
DestContext = CurrentSema->computeDeclContext(*SS, EnteringContext);
|
2014-06-08 16:38:12 +08:00
|
|
|
if (!DestContext)
|
2013-08-13 03:57:06 +08:00
|
|
|
DestContext = Context.getTranslationUnitDecl();
|
|
|
|
IdentifierInfo *ToIdent =
|
|
|
|
CurrentSema->getPreprocessor().getIdentifierInfo(CorrectTo);
|
|
|
|
NamespaceDecl *NewNamespace =
|
|
|
|
NamespaceDecl::Create(Context, DestContext, false, Typo.getBeginLoc(),
|
2014-06-08 16:38:12 +08:00
|
|
|
Typo.getLoc(), ToIdent, nullptr);
|
2013-08-13 03:57:06 +08:00
|
|
|
DestContext->addDecl(NewNamespace);
|
|
|
|
TypoCorrection Correction(ToIdent);
|
|
|
|
Correction.addCorrectionDecl(NewNamespace);
|
|
|
|
return Correction;
|
|
|
|
}
|
|
|
|
return TypoCorrection();
|
|
|
|
}
|
|
|
|
|
|
|
|
int CallCount;
|
|
|
|
};
|
|
|
|
|
2016-05-19 18:46:10 +08:00
|
|
|
class FunctionTypoProvider : public clang::ExternalSemaSource {
|
|
|
|
std::string CorrectFrom;
|
|
|
|
std::string CorrectTo;
|
|
|
|
Sema *CurrentSema;
|
|
|
|
|
|
|
|
public:
|
|
|
|
FunctionTypoProvider(StringRef From, StringRef To)
|
|
|
|
: CorrectFrom(From), CorrectTo(To), CurrentSema(nullptr), CallCount(0) {}
|
|
|
|
|
|
|
|
void InitializeSema(Sema &S) override { CurrentSema = &S; }
|
|
|
|
|
|
|
|
void ForgetSema() override { CurrentSema = nullptr; }
|
|
|
|
|
|
|
|
TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo, int LookupKind,
|
|
|
|
Scope *S, CXXScopeSpec *SS,
|
|
|
|
CorrectionCandidateCallback &CCC,
|
|
|
|
DeclContext *MemberContext, bool EnteringContext,
|
|
|
|
const ObjCObjectPointerType *OPT) override {
|
|
|
|
++CallCount;
|
|
|
|
if (CurrentSema && Typo.getName().getAsString() == CorrectFrom) {
|
|
|
|
DeclContext *DestContext = nullptr;
|
|
|
|
ASTContext &Context = CurrentSema->getASTContext();
|
|
|
|
if (SS)
|
|
|
|
DestContext = CurrentSema->computeDeclContext(*SS, EnteringContext);
|
|
|
|
if (!DestContext)
|
|
|
|
DestContext = Context.getTranslationUnitDecl();
|
|
|
|
IdentifierInfo *ToIdent =
|
|
|
|
CurrentSema->getPreprocessor().getIdentifierInfo(CorrectTo);
|
|
|
|
auto *NewFunction = FunctionDecl::Create(
|
|
|
|
Context, DestContext, SourceLocation(), SourceLocation(), ToIdent,
|
|
|
|
Context.getFunctionType(Context.VoidTy, {}, {}), nullptr, SC_Static);
|
|
|
|
DestContext->addDecl(NewFunction);
|
|
|
|
TypoCorrection Correction(ToIdent);
|
|
|
|
Correction.addCorrectionDecl(NewFunction);
|
|
|
|
return Correction;
|
|
|
|
}
|
|
|
|
return TypoCorrection();
|
|
|
|
}
|
|
|
|
|
|
|
|
int CallCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
// \brief Chains together a vector of DiagnosticWatchers and
|
2013-08-13 03:57:06 +08:00
|
|
|
// adds a vector of ExternalSemaSources to the CompilerInstance before
|
|
|
|
// performing semantic analysis.
|
|
|
|
class ExternalSemaSourceInstaller : public clang::ASTFrontendAction {
|
2016-05-19 18:46:10 +08:00
|
|
|
std::vector<DiagnosticWatcher *> Watchers;
|
2013-08-13 03:57:06 +08:00
|
|
|
std::vector<clang::ExternalSemaSource *> Sources;
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<DiagnosticConsumer> OwnedClient;
|
2013-08-13 03:57:06 +08:00
|
|
|
|
|
|
|
protected:
|
2015-04-11 10:00:23 +08:00
|
|
|
std::unique_ptr<clang::ASTConsumer>
|
2013-08-13 03:57:06 +08:00
|
|
|
CreateASTConsumer(clang::CompilerInstance &Compiler,
|
2015-04-11 10:00:23 +08:00
|
|
|
llvm::StringRef /* dummy */) override {
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<clang::ASTConsumer>();
|
2013-08-13 03:57:06 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
void ExecuteAction() override {
|
2013-08-13 03:57:06 +08:00
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
ASSERT_FALSE(CI.hasSema());
|
2014-06-08 16:38:12 +08:00
|
|
|
CI.createSema(getTranslationUnitKind(), nullptr);
|
2013-08-13 03:57:06 +08:00
|
|
|
ASSERT_TRUE(CI.hasDiagnostics());
|
|
|
|
DiagnosticsEngine &Diagnostics = CI.getDiagnostics();
|
|
|
|
DiagnosticConsumer *Client = Diagnostics.getClient();
|
|
|
|
if (Diagnostics.ownsClient())
|
2014-11-18 07:46:02 +08:00
|
|
|
OwnedClient = Diagnostics.takeClient();
|
2013-08-13 03:57:06 +08:00
|
|
|
for (size_t I = 0, E = Watchers.size(); I < E; ++I)
|
|
|
|
Client = Watchers[I]->Chain(Client);
|
|
|
|
Diagnostics.setClient(Client, false);
|
|
|
|
for (size_t I = 0, E = Sources.size(); I < E; ++I) {
|
|
|
|
Sources[I]->InitializeSema(CI.getSema());
|
|
|
|
CI.getSema().addExternalSource(Sources[I]);
|
|
|
|
}
|
|
|
|
ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
|
|
|
|
CI.getFrontendOpts().SkipFunctionBodies);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
void PushSource(clang::ExternalSemaSource *Source) {
|
|
|
|
Sources.push_back(Source);
|
|
|
|
}
|
|
|
|
|
2016-05-19 18:46:10 +08:00
|
|
|
void PushWatcher(DiagnosticWatcher *Watcher) { Watchers.push_back(Watcher); }
|
2013-08-13 03:57:06 +08:00
|
|
|
};
|
|
|
|
|
2016-05-19 18:46:10 +08:00
|
|
|
// Make sure that the DiagnosticWatcher is not miscounting.
|
2013-08-13 03:57:06 +08:00
|
|
|
TEST(ExternalSemaSource, SanityCheck) {
|
2019-08-30 17:29:34 +08:00
|
|
|
auto Installer = std::make_unique<ExternalSemaSourceInstaller>();
|
2016-05-19 18:46:10 +08:00
|
|
|
DiagnosticWatcher Watcher("AAB", "BBB");
|
2013-08-13 03:57:06 +08:00
|
|
|
Installer->PushWatcher(&Watcher);
|
|
|
|
std::vector<std::string> Args(1, "-std=c++11");
|
|
|
|
ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs(
|
2019-08-30 17:29:34 +08:00
|
|
|
std::move(Installer), "namespace AAA { } using namespace AAB;", Args));
|
2013-08-13 03:57:06 +08:00
|
|
|
ASSERT_EQ(0, Watcher.SeenCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that when we add a NamespaceTypeProvider, we use that suggestion
|
|
|
|
// instead of the usual suggestion we would use above.
|
|
|
|
TEST(ExternalSemaSource, ExternalTypoCorrectionPrioritized) {
|
2019-08-30 17:29:34 +08:00
|
|
|
auto Installer = std::make_unique<ExternalSemaSourceInstaller>();
|
2013-08-13 03:57:06 +08:00
|
|
|
NamespaceTypoProvider Provider("AAB", "BBB");
|
2016-05-19 18:46:10 +08:00
|
|
|
DiagnosticWatcher Watcher("AAB", "BBB");
|
2013-08-13 03:57:06 +08:00
|
|
|
Installer->PushSource(&Provider);
|
|
|
|
Installer->PushWatcher(&Watcher);
|
|
|
|
std::vector<std::string> Args(1, "-std=c++11");
|
|
|
|
ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs(
|
2019-08-30 17:29:34 +08:00
|
|
|
std::move(Installer), "namespace AAA { } using namespace AAB;", Args));
|
2013-08-13 03:57:06 +08:00
|
|
|
ASSERT_LE(0, Provider.CallCount);
|
|
|
|
ASSERT_EQ(1, Watcher.SeenCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we use the first successful TypoCorrection returned from an
|
|
|
|
// ExternalSemaSource.
|
|
|
|
TEST(ExternalSemaSource, ExternalTypoCorrectionOrdering) {
|
2019-08-30 17:29:34 +08:00
|
|
|
auto Installer = std::make_unique<ExternalSemaSourceInstaller>();
|
2013-08-13 03:57:06 +08:00
|
|
|
NamespaceTypoProvider First("XXX", "BBB");
|
|
|
|
NamespaceTypoProvider Second("AAB", "CCC");
|
|
|
|
NamespaceTypoProvider Third("AAB", "DDD");
|
2016-05-19 18:46:10 +08:00
|
|
|
DiagnosticWatcher Watcher("AAB", "CCC");
|
2013-08-13 03:57:06 +08:00
|
|
|
Installer->PushSource(&First);
|
|
|
|
Installer->PushSource(&Second);
|
|
|
|
Installer->PushSource(&Third);
|
|
|
|
Installer->PushWatcher(&Watcher);
|
|
|
|
std::vector<std::string> Args(1, "-std=c++11");
|
|
|
|
ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs(
|
2019-08-30 17:29:34 +08:00
|
|
|
std::move(Installer), "namespace AAA { } using namespace AAB;", Args));
|
2013-08-13 03:57:06 +08:00
|
|
|
ASSERT_LE(1, First.CallCount);
|
|
|
|
ASSERT_LE(1, Second.CallCount);
|
|
|
|
ASSERT_EQ(0, Third.CallCount);
|
|
|
|
ASSERT_EQ(1, Watcher.SeenCount);
|
|
|
|
}
|
|
|
|
|
2016-05-19 18:46:10 +08:00
|
|
|
TEST(ExternalSemaSource, ExternalDelayedTypoCorrection) {
|
2019-08-30 17:29:34 +08:00
|
|
|
auto Installer = std::make_unique<ExternalSemaSourceInstaller>();
|
2016-05-19 18:46:10 +08:00
|
|
|
FunctionTypoProvider Provider("aaa", "bbb");
|
|
|
|
DiagnosticWatcher Watcher("aaa", "bbb");
|
|
|
|
Installer->PushSource(&Provider);
|
|
|
|
Installer->PushWatcher(&Watcher);
|
|
|
|
std::vector<std::string> Args(1, "-std=c++11");
|
|
|
|
ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs(
|
2019-08-30 17:29:34 +08:00
|
|
|
std::move(Installer), "namespace AAA { } void foo() { AAA::aaa(); }",
|
2016-05-19 18:46:10 +08:00
|
|
|
Args));
|
|
|
|
ASSERT_LE(0, Provider.CallCount);
|
|
|
|
ASSERT_EQ(1, Watcher.SeenCount);
|
|
|
|
}
|
|
|
|
|
2013-08-13 06:11:14 +08:00
|
|
|
// We should only try MaybeDiagnoseMissingCompleteType if we can't otherwise
|
|
|
|
// solve the problem.
|
|
|
|
TEST(ExternalSemaSource, TryOtherTacticsBeforeDiagnosing) {
|
2019-08-30 17:29:34 +08:00
|
|
|
auto Installer = std::make_unique<ExternalSemaSourceInstaller>();
|
2013-08-13 06:11:14 +08:00
|
|
|
CompleteTypeDiagnoser Diagnoser(false);
|
|
|
|
Installer->PushSource(&Diagnoser);
|
|
|
|
std::vector<std::string> Args(1, "-std=c++11");
|
|
|
|
// This code hits the class template specialization/class member of a class
|
|
|
|
// template specialization checks in Sema::RequireCompleteTypeImpl.
|
|
|
|
ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs(
|
2019-08-30 17:29:34 +08:00
|
|
|
std::move(Installer),
|
2013-08-13 06:11:14 +08:00
|
|
|
"template <typename T> struct S { class C { }; }; S<char>::C SCInst;",
|
|
|
|
Args));
|
|
|
|
ASSERT_EQ(0, Diagnoser.CallCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first ExternalSemaSource where MaybeDiagnoseMissingCompleteType returns
|
|
|
|
// true should be the last one called.
|
|
|
|
TEST(ExternalSemaSource, FirstDiagnoserTaken) {
|
2019-08-30 17:29:34 +08:00
|
|
|
auto Installer = std::make_unique<ExternalSemaSourceInstaller>();
|
2013-08-13 06:11:14 +08:00
|
|
|
CompleteTypeDiagnoser First(false);
|
|
|
|
CompleteTypeDiagnoser Second(true);
|
|
|
|
CompleteTypeDiagnoser Third(true);
|
|
|
|
Installer->PushSource(&First);
|
|
|
|
Installer->PushSource(&Second);
|
|
|
|
Installer->PushSource(&Third);
|
|
|
|
std::vector<std::string> Args(1, "-std=c++11");
|
|
|
|
ASSERT_FALSE(clang::tooling::runToolOnCodeWithArgs(
|
2019-08-30 17:29:34 +08:00
|
|
|
std::move(Installer), "class Incomplete; Incomplete IncompleteInstance;",
|
2013-08-13 06:11:14 +08:00
|
|
|
Args));
|
|
|
|
ASSERT_EQ(1, First.CallCount);
|
|
|
|
ASSERT_EQ(1, Second.CallCount);
|
|
|
|
ASSERT_EQ(0, Third.CallCount);
|
|
|
|
}
|
|
|
|
|
2013-08-13 03:57:06 +08:00
|
|
|
} // anonymous namespace
|