2011-02-03 12:51:52 +08:00
|
|
|
//===- unittests/Frontend/FrontendActionTest.cpp - FrontendAction tests ---===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2012-12-04 17:45:34 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2012-12-04 17:53:37 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2011-02-03 12:51:52 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Frontend/FrontendAction.h"
|
2016-11-17 02:15:26 +08:00
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
2013-11-24 10:12:18 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Lex/PreprocessorOptions.h"
|
2014-10-23 01:50:19 +08:00
|
|
|
#include "clang/Sema/Sema.h"
|
2011-02-03 12:51:52 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class TestASTFrontendAction : public ASTFrontendAction {
|
|
|
|
public:
|
2014-10-23 01:50:19 +08:00
|
|
|
TestASTFrontendAction(bool enableIncrementalProcessing = false,
|
|
|
|
bool actOnEndOfTranslationUnit = false)
|
|
|
|
: EnableIncrementalProcessing(enableIncrementalProcessing),
|
|
|
|
ActOnEndOfTranslationUnit(actOnEndOfTranslationUnit) { }
|
2013-11-24 10:12:18 +08:00
|
|
|
|
|
|
|
bool EnableIncrementalProcessing;
|
2014-10-23 01:50:19 +08:00
|
|
|
bool ActOnEndOfTranslationUnit;
|
2011-02-03 12:51:52 +08:00
|
|
|
std::vector<std::string> decl_names;
|
|
|
|
|
2017-06-09 09:36:10 +08:00
|
|
|
bool BeginSourceFileAction(CompilerInstance &ci) override {
|
2013-11-24 10:12:18 +08:00
|
|
|
if (EnableIncrementalProcessing)
|
|
|
|
ci.getPreprocessor().enableIncrementalProcessing();
|
|
|
|
|
2017-06-09 09:36:10 +08:00
|
|
|
return ASTFrontendAction::BeginSourceFileAction(ci);
|
2013-11-24 10:12:18 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
StringRef InFile) override {
|
2014-10-23 01:50:19 +08:00
|
|
|
return llvm::make_unique<Visitor>(CI, ActOnEndOfTranslationUnit,
|
|
|
|
decl_names);
|
2011-02-03 12:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
class Visitor : public ASTConsumer, public RecursiveASTVisitor<Visitor> {
|
|
|
|
public:
|
2014-10-23 01:50:19 +08:00
|
|
|
Visitor(CompilerInstance &CI, bool ActOnEndOfTranslationUnit,
|
|
|
|
std::vector<std::string> &decl_names) :
|
|
|
|
CI(CI), ActOnEndOfTranslationUnit(ActOnEndOfTranslationUnit),
|
|
|
|
decl_names_(decl_names) {}
|
2011-02-03 12:51:52 +08:00
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
void HandleTranslationUnit(ASTContext &context) override {
|
2014-10-23 01:50:19 +08:00
|
|
|
if (ActOnEndOfTranslationUnit) {
|
|
|
|
CI.getSema().ActOnEndOfTranslationUnit();
|
|
|
|
}
|
2011-02-03 12:51:52 +08:00
|
|
|
TraverseDecl(context.getTranslationUnitDecl());
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool VisitNamedDecl(NamedDecl *Decl) {
|
|
|
|
decl_names_.push_back(Decl->getQualifiedNameAsString());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-10-23 01:50:19 +08:00
|
|
|
CompilerInstance &CI;
|
|
|
|
bool ActOnEndOfTranslationUnit;
|
2011-02-03 12:51:52 +08:00
|
|
|
std::vector<std::string> &decl_names_;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ASTFrontendAction, Sanity) {
|
2017-01-07 03:49:01 +08:00
|
|
|
auto invocation = std::make_shared<CompilerInvocation>();
|
2011-02-03 12:51:52 +08:00
|
|
|
invocation->getPreprocessorOpts().addRemappedFile(
|
2014-08-28 04:03:29 +08:00
|
|
|
"test.cc",
|
|
|
|
MemoryBuffer::getMemBuffer("int main() { float x; }").release());
|
2017-04-27 02:57:40 +08:00
|
|
|
invocation->getFrontendOpts().Inputs.push_back(
|
|
|
|
FrontendInputFile("test.cc", InputKind::CXX));
|
2011-02-03 12:51:52 +08:00
|
|
|
invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
|
|
|
|
invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
|
|
|
|
CompilerInstance compiler;
|
2017-01-07 03:49:01 +08:00
|
|
|
compiler.setInvocation(std::move(invocation));
|
2013-01-20 09:58:28 +08:00
|
|
|
compiler.createDiagnostics();
|
2011-02-03 12:51:52 +08:00
|
|
|
|
|
|
|
TestASTFrontendAction test_action;
|
|
|
|
ASSERT_TRUE(compiler.ExecuteAction(test_action));
|
2013-12-15 18:36:26 +08:00
|
|
|
ASSERT_EQ(2U, test_action.decl_names.size());
|
|
|
|
EXPECT_EQ("main", test_action.decl_names[0]);
|
|
|
|
EXPECT_EQ("x", test_action.decl_names[1]);
|
2011-02-03 12:51:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-24 10:12:18 +08:00
|
|
|
TEST(ASTFrontendAction, IncrementalParsing) {
|
2017-01-07 03:49:01 +08:00
|
|
|
auto invocation = std::make_shared<CompilerInvocation>();
|
2013-11-24 10:12:18 +08:00
|
|
|
invocation->getPreprocessorOpts().addRemappedFile(
|
2014-08-28 04:03:29 +08:00
|
|
|
"test.cc",
|
|
|
|
MemoryBuffer::getMemBuffer("int main() { float x; }").release());
|
2017-04-27 02:57:40 +08:00
|
|
|
invocation->getFrontendOpts().Inputs.push_back(
|
|
|
|
FrontendInputFile("test.cc", InputKind::CXX));
|
2013-11-24 10:12:18 +08:00
|
|
|
invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
|
|
|
|
invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
|
|
|
|
CompilerInstance compiler;
|
2017-01-07 03:49:01 +08:00
|
|
|
compiler.setInvocation(std::move(invocation));
|
2013-11-24 10:12:18 +08:00
|
|
|
compiler.createDiagnostics();
|
|
|
|
|
|
|
|
TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true);
|
|
|
|
ASSERT_TRUE(compiler.ExecuteAction(test_action));
|
2013-12-15 18:36:26 +08:00
|
|
|
ASSERT_EQ(2U, test_action.decl_names.size());
|
|
|
|
EXPECT_EQ("main", test_action.decl_names[0]);
|
|
|
|
EXPECT_EQ("x", test_action.decl_names[1]);
|
2013-11-24 10:12:18 +08:00
|
|
|
}
|
|
|
|
|
2014-10-23 01:50:19 +08:00
|
|
|
TEST(ASTFrontendAction, LateTemplateIncrementalParsing) {
|
2017-01-07 03:49:01 +08:00
|
|
|
auto invocation = std::make_shared<CompilerInvocation>();
|
2014-10-23 01:50:19 +08:00
|
|
|
invocation->getLangOpts()->CPlusPlus = true;
|
|
|
|
invocation->getLangOpts()->DelayedTemplateParsing = true;
|
|
|
|
invocation->getPreprocessorOpts().addRemappedFile(
|
|
|
|
"test.cc", MemoryBuffer::getMemBuffer(
|
|
|
|
"template<typename T> struct A { A(T); T data; };\n"
|
|
|
|
"template<typename T> struct B: public A<T> {\n"
|
|
|
|
" B();\n"
|
|
|
|
" B(B const& b): A<T>(b.data) {}\n"
|
|
|
|
"};\n"
|
|
|
|
"B<char> c() { return B<char>(); }\n").release());
|
2017-04-27 02:57:40 +08:00
|
|
|
invocation->getFrontendOpts().Inputs.push_back(
|
|
|
|
FrontendInputFile("test.cc", InputKind::CXX));
|
2014-10-23 01:50:19 +08:00
|
|
|
invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
|
|
|
|
invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
|
|
|
|
CompilerInstance compiler;
|
2017-01-07 03:49:01 +08:00
|
|
|
compiler.setInvocation(std::move(invocation));
|
2014-10-23 01:50:19 +08:00
|
|
|
compiler.createDiagnostics();
|
|
|
|
|
|
|
|
TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true,
|
|
|
|
/*actOnEndOfTranslationUnit=*/true);
|
|
|
|
ASSERT_TRUE(compiler.ExecuteAction(test_action));
|
|
|
|
ASSERT_EQ(13U, test_action.decl_names.size());
|
|
|
|
EXPECT_EQ("A", test_action.decl_names[0]);
|
|
|
|
EXPECT_EQ("c", test_action.decl_names[12]);
|
|
|
|
}
|
|
|
|
|
2014-08-08 04:51:16 +08:00
|
|
|
struct TestPPCallbacks : public PPCallbacks {
|
|
|
|
TestPPCallbacks() : SeenEnd(false) {}
|
|
|
|
|
|
|
|
void EndOfMainFile() override { SeenEnd = true; }
|
|
|
|
|
|
|
|
bool SeenEnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestPPCallbacksFrontendAction : public PreprocessorFrontendAction {
|
2014-09-10 17:35:49 +08:00
|
|
|
TestPPCallbacks *Callbacks;
|
2014-08-08 04:51:16 +08:00
|
|
|
|
|
|
|
public:
|
2014-09-10 17:35:49 +08:00
|
|
|
TestPPCallbacksFrontendAction(TestPPCallbacks *C)
|
|
|
|
: Callbacks(C), SeenEnd(false) {}
|
2014-08-08 04:51:16 +08:00
|
|
|
|
|
|
|
void ExecuteAction() override {
|
|
|
|
Preprocessor &PP = getCompilerInstance().getPreprocessor();
|
2014-09-10 17:35:49 +08:00
|
|
|
PP.addPPCallbacks(std::unique_ptr<TestPPCallbacks>(Callbacks));
|
2014-08-08 04:51:16 +08:00
|
|
|
PP.EnterMainSourceFile();
|
|
|
|
}
|
|
|
|
void EndSourceFileAction() override { SeenEnd = Callbacks->SeenEnd; }
|
|
|
|
|
|
|
|
bool SeenEnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(PreprocessorFrontendAction, EndSourceFile) {
|
2017-01-07 03:49:01 +08:00
|
|
|
auto Invocation = std::make_shared<CompilerInvocation>();
|
2014-08-08 04:51:16 +08:00
|
|
|
Invocation->getPreprocessorOpts().addRemappedFile(
|
2014-08-28 04:03:29 +08:00
|
|
|
"test.cc",
|
|
|
|
MemoryBuffer::getMemBuffer("int main() { float x; }").release());
|
2014-08-08 04:51:16 +08:00
|
|
|
Invocation->getFrontendOpts().Inputs.push_back(
|
2017-04-27 02:57:40 +08:00
|
|
|
FrontendInputFile("test.cc", InputKind::CXX));
|
2014-08-08 04:51:16 +08:00
|
|
|
Invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
|
|
|
|
Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
|
|
|
|
CompilerInstance Compiler;
|
2017-01-07 03:49:01 +08:00
|
|
|
Compiler.setInvocation(std::move(Invocation));
|
2014-08-08 04:51:16 +08:00
|
|
|
Compiler.createDiagnostics();
|
|
|
|
|
2014-09-10 17:35:49 +08:00
|
|
|
TestPPCallbacks *Callbacks = new TestPPCallbacks;
|
|
|
|
TestPPCallbacksFrontendAction TestAction(Callbacks);
|
2014-08-08 04:51:16 +08:00
|
|
|
ASSERT_FALSE(Callbacks->SeenEnd);
|
|
|
|
ASSERT_FALSE(TestAction.SeenEnd);
|
|
|
|
ASSERT_TRUE(Compiler.ExecuteAction(TestAction));
|
|
|
|
// Check that EndOfMainFile was called before EndSourceFileAction.
|
|
|
|
ASSERT_TRUE(TestAction.SeenEnd);
|
|
|
|
}
|
|
|
|
|
2016-11-17 02:15:26 +08:00
|
|
|
class TypoExternalSemaSource : public ExternalSemaSource {
|
|
|
|
CompilerInstance &CI;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TypoExternalSemaSource(CompilerInstance &CI) : CI(CI) {}
|
|
|
|
|
|
|
|
TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo, int LookupKind,
|
|
|
|
Scope *S, CXXScopeSpec *SS,
|
|
|
|
CorrectionCandidateCallback &CCC,
|
|
|
|
DeclContext *MemberContext, bool EnteringContext,
|
|
|
|
const ObjCObjectPointerType *OPT) override {
|
|
|
|
// Generate a fake typo correction with one attached note.
|
|
|
|
ASTContext &Ctx = CI.getASTContext();
|
|
|
|
TypoCorrection TC(DeclarationName(&Ctx.Idents.get("moo")));
|
|
|
|
unsigned DiagID = Ctx.getDiagnostics().getCustomDiagID(
|
|
|
|
DiagnosticsEngine::Note, "This is a note");
|
|
|
|
TC.addExtraDiagnostic(PartialDiagnostic(DiagID, Ctx.getDiagAllocator()));
|
|
|
|
return TC;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TypoDiagnosticConsumer : public DiagnosticConsumer {
|
|
|
|
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
const Diagnostic &Info) override {
|
|
|
|
// Capture errors and notes. There should be one of each.
|
|
|
|
if (DiagLevel == DiagnosticsEngine::Error) {
|
|
|
|
assert(Error.empty());
|
|
|
|
Info.FormatDiagnostic(Error);
|
|
|
|
} else {
|
|
|
|
assert(Note.empty());
|
|
|
|
Info.FormatDiagnostic(Note);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SmallString<32> Error;
|
|
|
|
SmallString<32> Note;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ASTFrontendAction, ExternalSemaSource) {
|
2017-01-07 03:49:01 +08:00
|
|
|
auto Invocation = std::make_shared<CompilerInvocation>();
|
2016-11-17 02:15:26 +08:00
|
|
|
Invocation->getLangOpts()->CPlusPlus = true;
|
|
|
|
Invocation->getPreprocessorOpts().addRemappedFile(
|
|
|
|
"test.cc", MemoryBuffer::getMemBuffer("void fooo();\n"
|
|
|
|
"int main() { foo(); }")
|
|
|
|
.release());
|
|
|
|
Invocation->getFrontendOpts().Inputs.push_back(
|
2017-04-27 02:57:40 +08:00
|
|
|
FrontendInputFile("test.cc", InputKind::CXX));
|
2016-11-17 02:15:26 +08:00
|
|
|
Invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
|
|
|
|
Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
|
|
|
|
CompilerInstance Compiler;
|
2017-01-07 03:49:01 +08:00
|
|
|
Compiler.setInvocation(std::move(Invocation));
|
2016-11-17 02:15:26 +08:00
|
|
|
auto *TDC = new TypoDiagnosticConsumer;
|
|
|
|
Compiler.createDiagnostics(TDC, /*ShouldOwnClient=*/true);
|
|
|
|
Compiler.setExternalSemaSource(new TypoExternalSemaSource(Compiler));
|
|
|
|
|
|
|
|
SyntaxOnlyAction TestAction;
|
|
|
|
ASSERT_TRUE(Compiler.ExecuteAction(TestAction));
|
|
|
|
// There should be one error correcting to 'moo' and a note attached to it.
|
|
|
|
EXPECT_EQ("use of undeclared identifier 'foo'; did you mean 'moo'?",
|
|
|
|
TDC->Error.str().str());
|
|
|
|
EXPECT_EQ("This is a note", TDC->Note.str().str());
|
|
|
|
}
|
|
|
|
|
2011-02-03 12:51:52 +08:00
|
|
|
} // anonymous namespace
|