Revert "[Tooling] [0/1] Refactor FrontendActionFactory::create() to return std::unique_ptr<>"

This reverts commit rL326201

This broke gcc4.8 builds, compiler just segfaults:¬
http://lab.llvm.org:8011/builders/clang-atom-d525-fedora-rel/builds/14909¬
http://lab.llvm.org:8011/builders/clang-x86_64-linux-abi-test/builds/22673¬

llvm-svn: 326204
This commit is contained in:
Roman Lebedev 2018-02-27 15:54:55 +00:00
parent 12b40745ab
commit 497fd98af2
14 changed files with 108 additions and 120 deletions

View File

@ -34,7 +34,7 @@ looked for. Let me give you an example:
TEST(runToolOnCode, CanSyntaxCheckCode) { TEST(runToolOnCode, CanSyntaxCheckCode) {
// runToolOnCode returns whether the action was correctly run over the // runToolOnCode returns whether the action was correctly run over the
// given code. // given code.
EXPECT_TRUE(runToolOnCode(llvm::make_unique<clang::SyntaxOnlyAction>(), "class X {};")); EXPECT_TRUE(runToolOnCode(new clang::SyntaxOnlyAction, "class X {};"));
} }
Writing a standalone tool Writing a standalone tool

View File

@ -196,7 +196,7 @@ Now we can combine all of the above into a small example program:
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (argc > 1) { if (argc > 1) {
clang::tooling::runToolOnCode(llvm::make_unique<FindNamedClassAction>(), argv[1]); clang::tooling::runToolOnCode(new FindNamedClassAction, argv[1]);
} }
} }

View File

@ -94,7 +94,7 @@ public:
/// \brief Returns a new clang::FrontendAction. /// \brief Returns a new clang::FrontendAction.
/// ///
/// The caller takes ownership of the returned action. /// The caller takes ownership of the returned action.
virtual std::unique_ptr<clang::FrontendAction> create() = 0; virtual clang::FrontendAction *create() = 0;
}; };
/// \brief Returns a new FrontendActionFactory for a given type. /// \brief Returns a new FrontendActionFactory for a given type.
@ -149,8 +149,8 @@ inline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory(
/// clang modules. /// clang modules.
/// ///
/// \return - True if 'ToolAction' was successfully executed. /// \return - True if 'ToolAction' was successfully executed.
bool runToolOnCode(std::unique_ptr<FrontendAction> ToolAction, bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code,
const Twine &Code, const Twine &FileName = "input.cc", const Twine &FileName = "input.cc",
std::shared_ptr<PCHContainerOperations> PCHContainerOps = std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>()); std::make_shared<PCHContainerOperations>());
@ -172,7 +172,7 @@ typedef std::vector<std::pair<std::string, std::string>> FileContentMappings;
/// ///
/// \return - True if 'ToolAction' was successfully executed. /// \return - True if 'ToolAction' was successfully executed.
bool runToolOnCodeWithArgs( bool runToolOnCodeWithArgs(
std::unique_ptr<FrontendAction> ToolAction, const Twine &Code, clang::FrontendAction *ToolAction, const Twine &Code,
const std::vector<std::string> &Args, const Twine &FileName = "input.cc", const std::vector<std::string> &Args, const Twine &FileName = "input.cc",
const Twine &ToolName = "clang-tool", const Twine &ToolName = "clang-tool",
std::shared_ptr<PCHContainerOperations> PCHContainerOps = std::shared_ptr<PCHContainerOperations> PCHContainerOps =
@ -226,8 +226,8 @@ public:
/// ownership. /// ownership.
/// \param PCHContainerOps The PCHContainerOperations for loading and creating /// \param PCHContainerOps The PCHContainerOperations for loading and creating
/// clang modules. /// clang modules.
ToolInvocation(std::vector<std::string> CommandLine, ToolInvocation(std::vector<std::string> CommandLine, FrontendAction *FAction,
std::unique_ptr<FrontendAction> FAction, FileManager *Files, FileManager *Files,
std::shared_ptr<PCHContainerOperations> PCHContainerOps = std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>()); std::make_shared<PCHContainerOperations>());
@ -367,9 +367,7 @@ template <typename T>
std::unique_ptr<FrontendActionFactory> newFrontendActionFactory() { std::unique_ptr<FrontendActionFactory> newFrontendActionFactory() {
class SimpleFrontendActionFactory : public FrontendActionFactory { class SimpleFrontendActionFactory : public FrontendActionFactory {
public: public:
std::unique_ptr<clang::FrontendAction> create() override { clang::FrontendAction *create() override { return new T; }
return llvm::make_unique<T>();
}
}; };
return std::unique_ptr<FrontendActionFactory>( return std::unique_ptr<FrontendActionFactory>(
@ -385,9 +383,8 @@ inline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory(
SourceFileCallbacks *Callbacks) SourceFileCallbacks *Callbacks)
: ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {} : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {}
std::unique_ptr<clang::FrontendAction> create() override { clang::FrontendAction *create() override {
return llvm::make_unique<ConsumerFactoryAdaptor>(ConsumerFactory, return new ConsumerFactoryAdaptor(ConsumerFactory, Callbacks);
Callbacks);
} }
private: private:

View File

@ -104,12 +104,12 @@ clang::CompilerInvocation *newInvocation(
return Invocation; return Invocation;
} }
bool runToolOnCode(std::unique_ptr<FrontendAction> ToolAction, bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code,
const Twine &Code, const Twine &FileName, const Twine &FileName,
std::shared_ptr<PCHContainerOperations> PCHContainerOps) { std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
return runToolOnCodeWithArgs(std::move(ToolAction), Code, return runToolOnCodeWithArgs(ToolAction, Code, std::vector<std::string>(),
std::vector<std::string>(), FileName, FileName, "clang-tool",
"clang-tool", std::move(PCHContainerOps)); std::move(PCHContainerOps));
} }
static std::vector<std::string> static std::vector<std::string>
@ -125,7 +125,7 @@ getSyntaxOnlyToolArgs(const Twine &ToolName,
} }
bool runToolOnCodeWithArgs( bool runToolOnCodeWithArgs(
std::unique_ptr<FrontendAction> ToolAction, const Twine &Code, clang::FrontendAction *ToolAction, const Twine &Code,
const std::vector<std::string> &Args, const Twine &FileName, const std::vector<std::string> &Args, const Twine &FileName,
const Twine &ToolName, const Twine &ToolName,
std::shared_ptr<PCHContainerOperations> PCHContainerOps, std::shared_ptr<PCHContainerOperations> PCHContainerOps,
@ -143,7 +143,8 @@ bool runToolOnCodeWithArgs(
ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster(); ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster();
ToolInvocation Invocation( ToolInvocation Invocation(
getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef), getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef),
std::move(ToolAction), Files.get(), std::move(PCHContainerOps)); ToolAction, Files.get(),
std::move(PCHContainerOps));
SmallString<1024> CodeStorage; SmallString<1024> CodeStorage;
InMemoryFileSystem->addFile(FileNameRef, 0, InMemoryFileSystem->addFile(FileNameRef, 0,
@ -203,18 +204,15 @@ void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine,
namespace { namespace {
class SingleFrontendActionFactory : public FrontendActionFactory { class SingleFrontendActionFactory : public FrontendActionFactory {
std::unique_ptr<clang::FrontendAction> Action; FrontendAction *Action;
public: public:
SingleFrontendActionFactory(std::unique_ptr<clang::FrontendAction> Action) SingleFrontendActionFactory(FrontendAction *Action) : Action(Action) {}
: Action(std::move(Action)) {}
std::unique_ptr<clang::FrontendAction> create() override { FrontendAction *create() override { return Action; }
return std::move(Action);
}
}; };
} // namespace }
ToolInvocation::ToolInvocation( ToolInvocation::ToolInvocation(
std::vector<std::string> CommandLine, ToolAction *Action, std::vector<std::string> CommandLine, ToolAction *Action,
@ -224,13 +222,12 @@ ToolInvocation::ToolInvocation(
DiagConsumer(nullptr) {} DiagConsumer(nullptr) {}
ToolInvocation::ToolInvocation( ToolInvocation::ToolInvocation(
std::vector<std::string> CommandLine, std::vector<std::string> CommandLine, FrontendAction *FAction,
std::unique_ptr<FrontendAction> FAction, FileManager *Files, FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps)
std::shared_ptr<PCHContainerOperations> PCHContainerOps)
: CommandLine(std::move(CommandLine)), : CommandLine(std::move(CommandLine)),
Action(new SingleFrontendActionFactory(std::move(FAction))), Action(new SingleFrontendActionFactory(FAction)), OwnsAction(true),
OwnsAction(true), Files(Files), Files(Files), PCHContainerOps(std::move(PCHContainerOps)),
PCHContainerOps(std::move(PCHContainerOps)), DiagConsumer(nullptr) {} DiagConsumer(nullptr) {}
ToolInvocation::~ToolInvocation() { ToolInvocation::~ToolInvocation() {
if (OwnsAction) if (OwnsAction)

View File

@ -462,9 +462,7 @@ public:
ToolActionFactory(TUCallbackType Callback) ToolActionFactory(TUCallbackType Callback)
: Callback(std::move(Callback)) {} : Callback(std::move(Callback)) {}
std::unique_ptr<FrontendAction> create() override { FrontendAction *create() override { return new ToolASTAction(Callback); }
return llvm::make_unique<ToolASTAction>(Callback);
}
private: private:
TUCallbackType Callback; TUCallbackType Callback;

View File

@ -90,7 +90,7 @@ TEST(EvaluateAsRValue, FailsGracefullyForUnknownTypes) {
std::vector<std::string> Args(1, Mode); std::vector<std::string> Args(1, Mode);
Args.push_back("-fno-delayed-template-parsing"); Args.push_back("-fno-delayed-template-parsing");
ASSERT_TRUE(runToolOnCodeWithArgs( ASSERT_TRUE(runToolOnCodeWithArgs(
llvm::make_unique<EvaluateConstantInitializersAction>(), new EvaluateConstantInitializersAction(),
"template <typename T>" "template <typename T>"
"struct vector {" "struct vector {"
" explicit vector(int size);" " explicit vector(int size);"

View File

@ -103,8 +103,7 @@ private:
TEST(CrossTranslationUnit, CanLoadFunctionDefinition) { TEST(CrossTranslationUnit, CanLoadFunctionDefinition) {
bool Success = false; bool Success = false;
EXPECT_TRUE(tooling::runToolOnCode(llvm::make_unique<CTUAction>(&Success), EXPECT_TRUE(tooling::runToolOnCode(new CTUAction(&Success), "int f(int);"));
"int f(int);"));
EXPECT_TRUE(Success); EXPECT_TRUE(Success);
} }

View File

@ -100,7 +100,7 @@ VisitedContextResults runCodeCompleteOnCode(StringRef Code) {
auto Action = llvm::make_unique<CodeCompleteAction>( auto Action = llvm::make_unique<CodeCompleteAction>(
offsetToPosition(WithoutToken, TokenOffset), Results); offsetToPosition(WithoutToken, TokenOffset), Results);
clang::tooling::runToolOnCodeWithArgs(std::move(Action), Code, {"-std=c++11"}, clang::tooling::runToolOnCodeWithArgs(Action.release(), Code, {"-std=c++11"},
TestCCName); TestCCName);
return Results; return Results;
} }

View File

@ -221,26 +221,28 @@ public:
// Make sure that the DiagnosticWatcher is not miscounting. // Make sure that the DiagnosticWatcher is not miscounting.
TEST(ExternalSemaSource, SanityCheck) { TEST(ExternalSemaSource, SanityCheck) {
auto Installer = llvm::make_unique<ExternalSemaSourceInstaller>(); std::unique_ptr<ExternalSemaSourceInstaller> Installer(
new ExternalSemaSourceInstaller);
DiagnosticWatcher Watcher("AAB", "BBB"); DiagnosticWatcher Watcher("AAB", "BBB");
Installer->PushWatcher(&Watcher); Installer->PushWatcher(&Watcher);
std::vector<std::string> Args(1, "-std=c++11"); std::vector<std::string> Args(1, "-std=c++11");
ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs( ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs(
std::move(Installer), "namespace AAA { } using namespace AAB;", Args)); Installer.release(), "namespace AAA { } using namespace AAB;", Args));
ASSERT_EQ(0, Watcher.SeenCount); ASSERT_EQ(0, Watcher.SeenCount);
} }
// Check that when we add a NamespaceTypeProvider, we use that suggestion // Check that when we add a NamespaceTypeProvider, we use that suggestion
// instead of the usual suggestion we would use above. // instead of the usual suggestion we would use above.
TEST(ExternalSemaSource, ExternalTypoCorrectionPrioritized) { TEST(ExternalSemaSource, ExternalTypoCorrectionPrioritized) {
auto Installer = llvm::make_unique<ExternalSemaSourceInstaller>(); std::unique_ptr<ExternalSemaSourceInstaller> Installer(
new ExternalSemaSourceInstaller);
NamespaceTypoProvider Provider("AAB", "BBB"); NamespaceTypoProvider Provider("AAB", "BBB");
DiagnosticWatcher Watcher("AAB", "BBB"); DiagnosticWatcher Watcher("AAB", "BBB");
Installer->PushSource(&Provider); Installer->PushSource(&Provider);
Installer->PushWatcher(&Watcher); Installer->PushWatcher(&Watcher);
std::vector<std::string> Args(1, "-std=c++11"); std::vector<std::string> Args(1, "-std=c++11");
ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs( ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs(
std::move(Installer), "namespace AAA { } using namespace AAB;", Args)); Installer.release(), "namespace AAA { } using namespace AAB;", Args));
ASSERT_LE(0, Provider.CallCount); ASSERT_LE(0, Provider.CallCount);
ASSERT_EQ(1, Watcher.SeenCount); ASSERT_EQ(1, Watcher.SeenCount);
} }
@ -248,7 +250,8 @@ TEST(ExternalSemaSource, ExternalTypoCorrectionPrioritized) {
// Check that we use the first successful TypoCorrection returned from an // Check that we use the first successful TypoCorrection returned from an
// ExternalSemaSource. // ExternalSemaSource.
TEST(ExternalSemaSource, ExternalTypoCorrectionOrdering) { TEST(ExternalSemaSource, ExternalTypoCorrectionOrdering) {
auto Installer = llvm::make_unique<ExternalSemaSourceInstaller>(); std::unique_ptr<ExternalSemaSourceInstaller> Installer(
new ExternalSemaSourceInstaller);
NamespaceTypoProvider First("XXX", "BBB"); NamespaceTypoProvider First("XXX", "BBB");
NamespaceTypoProvider Second("AAB", "CCC"); NamespaceTypoProvider Second("AAB", "CCC");
NamespaceTypoProvider Third("AAB", "DDD"); NamespaceTypoProvider Third("AAB", "DDD");
@ -259,7 +262,7 @@ TEST(ExternalSemaSource, ExternalTypoCorrectionOrdering) {
Installer->PushWatcher(&Watcher); Installer->PushWatcher(&Watcher);
std::vector<std::string> Args(1, "-std=c++11"); std::vector<std::string> Args(1, "-std=c++11");
ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs( ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs(
std::move(Installer), "namespace AAA { } using namespace AAB;", Args)); Installer.release(), "namespace AAA { } using namespace AAB;", Args));
ASSERT_LE(1, First.CallCount); ASSERT_LE(1, First.CallCount);
ASSERT_LE(1, Second.CallCount); ASSERT_LE(1, Second.CallCount);
ASSERT_EQ(0, Third.CallCount); ASSERT_EQ(0, Third.CallCount);
@ -267,14 +270,15 @@ TEST(ExternalSemaSource, ExternalTypoCorrectionOrdering) {
} }
TEST(ExternalSemaSource, ExternalDelayedTypoCorrection) { TEST(ExternalSemaSource, ExternalDelayedTypoCorrection) {
auto Installer = llvm::make_unique<ExternalSemaSourceInstaller>(); std::unique_ptr<ExternalSemaSourceInstaller> Installer(
new ExternalSemaSourceInstaller);
FunctionTypoProvider Provider("aaa", "bbb"); FunctionTypoProvider Provider("aaa", "bbb");
DiagnosticWatcher Watcher("aaa", "bbb"); DiagnosticWatcher Watcher("aaa", "bbb");
Installer->PushSource(&Provider); Installer->PushSource(&Provider);
Installer->PushWatcher(&Watcher); Installer->PushWatcher(&Watcher);
std::vector<std::string> Args(1, "-std=c++11"); std::vector<std::string> Args(1, "-std=c++11");
ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs( ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs(
std::move(Installer), "namespace AAA { } void foo() { AAA::aaa(); }", Installer.release(), "namespace AAA { } void foo() { AAA::aaa(); }",
Args)); Args));
ASSERT_LE(0, Provider.CallCount); ASSERT_LE(0, Provider.CallCount);
ASSERT_EQ(1, Watcher.SeenCount); ASSERT_EQ(1, Watcher.SeenCount);
@ -283,14 +287,15 @@ TEST(ExternalSemaSource, ExternalDelayedTypoCorrection) {
// We should only try MaybeDiagnoseMissingCompleteType if we can't otherwise // We should only try MaybeDiagnoseMissingCompleteType if we can't otherwise
// solve the problem. // solve the problem.
TEST(ExternalSemaSource, TryOtherTacticsBeforeDiagnosing) { TEST(ExternalSemaSource, TryOtherTacticsBeforeDiagnosing) {
auto Installer = llvm::make_unique<ExternalSemaSourceInstaller>(); std::unique_ptr<ExternalSemaSourceInstaller> Installer(
new ExternalSemaSourceInstaller);
CompleteTypeDiagnoser Diagnoser(false); CompleteTypeDiagnoser Diagnoser(false);
Installer->PushSource(&Diagnoser); Installer->PushSource(&Diagnoser);
std::vector<std::string> Args(1, "-std=c++11"); std::vector<std::string> Args(1, "-std=c++11");
// This code hits the class template specialization/class member of a class // This code hits the class template specialization/class member of a class
// template specialization checks in Sema::RequireCompleteTypeImpl. // template specialization checks in Sema::RequireCompleteTypeImpl.
ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs( ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs(
std::move(Installer), Installer.release(),
"template <typename T> struct S { class C { }; }; S<char>::C SCInst;", "template <typename T> struct S { class C { }; }; S<char>::C SCInst;",
Args)); Args));
ASSERT_EQ(0, Diagnoser.CallCount); ASSERT_EQ(0, Diagnoser.CallCount);
@ -299,7 +304,8 @@ TEST(ExternalSemaSource, TryOtherTacticsBeforeDiagnosing) {
// The first ExternalSemaSource where MaybeDiagnoseMissingCompleteType returns // The first ExternalSemaSource where MaybeDiagnoseMissingCompleteType returns
// true should be the last one called. // true should be the last one called.
TEST(ExternalSemaSource, FirstDiagnoserTaken) { TEST(ExternalSemaSource, FirstDiagnoserTaken) {
auto Installer = llvm::make_unique<ExternalSemaSourceInstaller>(); std::unique_ptr<ExternalSemaSourceInstaller> Installer(
new ExternalSemaSourceInstaller);
CompleteTypeDiagnoser First(false); CompleteTypeDiagnoser First(false);
CompleteTypeDiagnoser Second(true); CompleteTypeDiagnoser Second(true);
CompleteTypeDiagnoser Third(true); CompleteTypeDiagnoser Third(true);
@ -308,7 +314,7 @@ TEST(ExternalSemaSource, FirstDiagnoserTaken) {
Installer->PushSource(&Third); Installer->PushSource(&Third);
std::vector<std::string> Args(1, "-std=c++11"); std::vector<std::string> Args(1, "-std=c++11");
ASSERT_FALSE(clang::tooling::runToolOnCodeWithArgs( ASSERT_FALSE(clang::tooling::runToolOnCodeWithArgs(
std::move(Installer), "class Incomplete; Incomplete IncompleteInstance;", Installer.release(), "class Incomplete; Incomplete IncompleteInstance;",
Args)); Args));
ASSERT_EQ(1, First.CallCount); ASSERT_EQ(1, First.CallCount);
ASSERT_EQ(1, Second.CallCount); ASSERT_EQ(1, Second.CallCount);

View File

@ -56,8 +56,8 @@ public:
CommentVerifier GetVerifier(); CommentVerifier GetVerifier();
protected: protected:
std::unique_ptr<FrontendAction> CreateTestAction() override { ASTFrontendAction *CreateTestAction() override {
return llvm::make_unique<CommentHandlerAction>(this); return new CommentHandlerAction(this);
} }
private: private:

View File

@ -79,9 +79,7 @@ private:
class ReportResultActionFactory : public FrontendActionFactory { class ReportResultActionFactory : public FrontendActionFactory {
public: public:
ReportResultActionFactory(ExecutionContext *Context) : Context(Context) {} ReportResultActionFactory(ExecutionContext *Context) : Context(Context) {}
std::unique_ptr<FrontendAction> create() override { FrontendAction *create() override { return new ReportResultAction(Context); }
return llvm::make_unique<ReportResultAction>(Context);
}
private: private:
ExecutionContext *const Context; ExecutionContext *const Context;

View File

@ -650,7 +650,7 @@ template <typename T>
class TestVisitor : public clang::RecursiveASTVisitor<T> { class TestVisitor : public clang::RecursiveASTVisitor<T> {
public: public:
bool runOver(StringRef Code) { bool runOver(StringRef Code) {
return runToolOnCode(llvm::make_unique<TestAction>(this), Code); return runToolOnCode(new TestAction(this), Code);
} }
protected: protected:

View File

@ -82,8 +82,8 @@ public:
} }
protected: protected:
virtual std::unique_ptr<FrontendAction> CreateTestAction() { virtual ASTFrontendAction* CreateTestAction() {
return llvm::make_unique<TestAction>(this); return new TestAction(this);
} }
class FindConsumer : public ASTConsumer { class FindConsumer : public ASTConsumer {

View File

@ -64,10 +64,10 @@ class FindTopLevelDeclConsumer : public clang::ASTConsumer {
TEST(runToolOnCode, FindsNoTopLevelDeclOnEmptyCode) { TEST(runToolOnCode, FindsNoTopLevelDeclOnEmptyCode) {
bool FoundTopLevelDecl = false; bool FoundTopLevelDecl = false;
EXPECT_TRUE(runToolOnCode( EXPECT_TRUE(
llvm::make_unique<TestAction>( runToolOnCode(new TestAction(llvm::make_unique<FindTopLevelDeclConsumer>(
llvm::make_unique<FindTopLevelDeclConsumer>(&FoundTopLevelDecl)), &FoundTopLevelDecl)),
"")); ""));
EXPECT_FALSE(FoundTopLevelDecl); EXPECT_FALSE(FoundTopLevelDecl);
} }
@ -104,17 +104,17 @@ bool FindClassDeclX(ASTUnit *AST) {
TEST(runToolOnCode, FindsClassDecl) { TEST(runToolOnCode, FindsClassDecl) {
bool FoundClassDeclX = false; bool FoundClassDeclX = false;
EXPECT_TRUE(runToolOnCode( EXPECT_TRUE(
llvm::make_unique<TestAction>( runToolOnCode(new TestAction(llvm::make_unique<FindClassDeclXConsumer>(
llvm::make_unique<FindClassDeclXConsumer>(&FoundClassDeclX)), &FoundClassDeclX)),
"class X;")); "class X;"));
EXPECT_TRUE(FoundClassDeclX); EXPECT_TRUE(FoundClassDeclX);
FoundClassDeclX = false; FoundClassDeclX = false;
EXPECT_TRUE(runToolOnCode( EXPECT_TRUE(
llvm::make_unique<TestAction>( runToolOnCode(new TestAction(llvm::make_unique<FindClassDeclXConsumer>(
llvm::make_unique<FindClassDeclXConsumer>(&FoundClassDeclX)), &FoundClassDeclX)),
"class Y;")); "class Y;"));
EXPECT_FALSE(FoundClassDeclX); EXPECT_FALSE(FoundClassDeclX);
} }
@ -162,8 +162,8 @@ TEST(ToolInvocation, TestMapVirtualFile) {
Args.push_back("-Idef"); Args.push_back("-Idef");
Args.push_back("-fsyntax-only"); Args.push_back("-fsyntax-only");
Args.push_back("test.cpp"); Args.push_back("test.cpp");
clang::tooling::ToolInvocation Invocation( clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction,
Args, llvm::make_unique<SyntaxOnlyAction>(), Files.get()); Files.get());
InMemoryFileSystem->addFile( InMemoryFileSystem->addFile(
"test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("#include <abc>\n")); "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("#include <abc>\n"));
InMemoryFileSystem->addFile("def/abc", 0, InMemoryFileSystem->addFile("def/abc", 0,
@ -188,8 +188,8 @@ TEST(ToolInvocation, TestVirtualModulesCompilation) {
Args.push_back("-Idef"); Args.push_back("-Idef");
Args.push_back("-fsyntax-only"); Args.push_back("-fsyntax-only");
Args.push_back("test.cpp"); Args.push_back("test.cpp");
clang::tooling::ToolInvocation Invocation( clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction,
Args, llvm::make_unique<SyntaxOnlyAction>(), Files.get()); Files.get());
InMemoryFileSystem->addFile( InMemoryFileSystem->addFile(
"test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("#include <abc>\n")); "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("#include <abc>\n"));
InMemoryFileSystem->addFile("def/abc", 0, InMemoryFileSystem->addFile("def/abc", 0,
@ -259,64 +259,61 @@ TEST(runToolOnCode, TestSkipFunctionBody) {
std::vector<std::string> Args = {"-std=c++11"}; std::vector<std::string> Args = {"-std=c++11"};
std::vector<std::string> Args2 = {"-fno-delayed-template-parsing"}; std::vector<std::string> Args2 = {"-fno-delayed-template-parsing"};
EXPECT_TRUE(runToolOnCode(llvm::make_unique<SkipBodyAction>(), EXPECT_TRUE(runToolOnCode(new SkipBodyAction,
"int skipMe() { an_error_here }")); "int skipMe() { an_error_here }"));
EXPECT_FALSE(runToolOnCode(llvm::make_unique<SkipBodyAction>(), EXPECT_FALSE(runToolOnCode(new SkipBodyAction,
"int skipMeNot() { an_error_here }")); "int skipMeNot() { an_error_here }"));
// Test constructors with initializers // Test constructors with initializers
EXPECT_TRUE(runToolOnCodeWithArgs( EXPECT_TRUE(runToolOnCodeWithArgs(
llvm::make_unique<SkipBodyAction>(), new SkipBodyAction,
"struct skipMe { skipMe() : an_error() { more error } };", Args)); "struct skipMe { skipMe() : an_error() { more error } };", Args));
EXPECT_TRUE(runToolOnCodeWithArgs( EXPECT_TRUE(runToolOnCodeWithArgs(
llvm::make_unique<SkipBodyAction>(), new SkipBodyAction, "struct skipMe { skipMe(); };"
"struct skipMe { skipMe(); };" "skipMe::skipMe() : an_error([](){;}) { more error }",
"skipMe::skipMe() : an_error([](){;}) { more error }",
Args)); Args));
EXPECT_TRUE(runToolOnCodeWithArgs( EXPECT_TRUE(runToolOnCodeWithArgs(
llvm::make_unique<SkipBodyAction>(), new SkipBodyAction, "struct skipMe { skipMe(); };"
"struct skipMe { skipMe(); };" "skipMe::skipMe() : an_error{[](){;}} { more error }",
"skipMe::skipMe() : an_error{[](){;}} { more error }",
Args)); Args));
EXPECT_TRUE(runToolOnCodeWithArgs( EXPECT_TRUE(runToolOnCodeWithArgs(
llvm::make_unique<SkipBodyAction>(), new SkipBodyAction,
"struct skipMe { skipMe(); };" "struct skipMe { skipMe(); };"
"skipMe::skipMe() : a<b<c>(e)>>(), f{}, g() { error }", "skipMe::skipMe() : a<b<c>(e)>>(), f{}, g() { error }",
Args)); Args));
EXPECT_TRUE(runToolOnCodeWithArgs( EXPECT_TRUE(runToolOnCodeWithArgs(
llvm::make_unique<SkipBodyAction>(), new SkipBodyAction, "struct skipMe { skipMe() : bases()... { error } };",
"struct skipMe { skipMe() : bases()... { error } };", Args)); Args));
EXPECT_FALSE(runToolOnCodeWithArgs( EXPECT_FALSE(runToolOnCodeWithArgs(
llvm::make_unique<SkipBodyAction>(), new SkipBodyAction, "struct skipMeNot { skipMeNot() : an_error() { } };",
"struct skipMeNot { skipMeNot() : an_error() { } };", Args)); Args));
EXPECT_FALSE(runToolOnCodeWithArgs(llvm::make_unique<SkipBodyAction>(), EXPECT_FALSE(runToolOnCodeWithArgs(new SkipBodyAction,
"struct skipMeNot { skipMeNot(); };" "struct skipMeNot { skipMeNot(); };"
"skipMeNot::skipMeNot() : an_error() { }", "skipMeNot::skipMeNot() : an_error() { }",
Args)); Args));
// Try/catch // Try/catch
EXPECT_TRUE(runToolOnCode( EXPECT_TRUE(runToolOnCode(
llvm::make_unique<SkipBodyAction>(), new SkipBodyAction,
"void skipMe() try { an_error() } catch(error) { error };")); "void skipMe() try { an_error() } catch(error) { error };"));
EXPECT_TRUE(runToolOnCode( EXPECT_TRUE(runToolOnCode(
llvm::make_unique<SkipBodyAction>(), new SkipBodyAction,
"struct S { void skipMe() try { an_error() } catch(error) { error } };")); "struct S { void skipMe() try { an_error() } catch(error) { error } };"));
EXPECT_TRUE( EXPECT_TRUE(
runToolOnCode(llvm::make_unique<SkipBodyAction>(), runToolOnCode(new SkipBodyAction,
"void skipMe() try { an_error() } catch(error) { error; }" "void skipMe() try { an_error() } catch(error) { error; }"
"catch(error) { error } catch (error) { }")); "catch(error) { error } catch (error) { }"));
EXPECT_FALSE(runToolOnCode( EXPECT_FALSE(runToolOnCode(
llvm::make_unique<SkipBodyAction>(), new SkipBodyAction,
"void skipMe() try something;")); // don't crash while parsing "void skipMe() try something;")); // don't crash while parsing
// Template // Template
EXPECT_TRUE( EXPECT_TRUE(runToolOnCode(
runToolOnCode(llvm::make_unique<SkipBodyAction>(), new SkipBodyAction, "template<typename T> int skipMe() { an_error_here }"
"template<typename T> int skipMe() { an_error_here }" "int x = skipMe<int>();"));
"int x = skipMe<int>();"));
EXPECT_FALSE(runToolOnCodeWithArgs( EXPECT_FALSE(runToolOnCodeWithArgs(
llvm::make_unique<SkipBodyAction>(), new SkipBodyAction,
"template<typename T> int skipMeNot() { an_error_here }", Args2)); "template<typename T> int skipMeNot() { an_error_here }", Args2));
} }
@ -330,8 +327,7 @@ TEST(runToolOnCodeWithArgs, TestNoDepFile) {
Args.push_back(DepFilePath.str()); Args.push_back(DepFilePath.str());
Args.push_back("-MF"); Args.push_back("-MF");
Args.push_back(DepFilePath.str()); Args.push_back(DepFilePath.str());
EXPECT_TRUE( EXPECT_TRUE(runToolOnCodeWithArgs(new SkipBodyAction, "", Args));
runToolOnCodeWithArgs(llvm::make_unique<SkipBodyAction>(), "", Args));
EXPECT_FALSE(llvm::sys::fs::exists(DepFilePath.str())); EXPECT_FALSE(llvm::sys::fs::exists(DepFilePath.str()));
EXPECT_FALSE(llvm::sys::fs::remove(DepFilePath.str())); EXPECT_FALSE(llvm::sys::fs::remove(DepFilePath.str()));
} }
@ -355,26 +351,23 @@ private:
TEST(runToolOnCodeWithArgs, DiagnosticsColor) { TEST(runToolOnCodeWithArgs, DiagnosticsColor) {
EXPECT_TRUE(runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(true), "",
{"-fcolor-diagnostics"}));
EXPECT_TRUE(runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(false),
"", {"-fno-color-diagnostics"}));
EXPECT_TRUE(
runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(true), "",
{"-fno-color-diagnostics", "-fcolor-diagnostics"}));
EXPECT_TRUE(
runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(false), "",
{"-fcolor-diagnostics", "-fno-color-diagnostics"}));
EXPECT_TRUE(runToolOnCodeWithArgs( EXPECT_TRUE(runToolOnCodeWithArgs(
llvm::make_unique<CheckColoredDiagnosticsAction>(true), "", new CheckColoredDiagnosticsAction(true), "",
{"-fcolor-diagnostics"}));
EXPECT_TRUE(runToolOnCodeWithArgs(
llvm::make_unique<CheckColoredDiagnosticsAction>(false), "",
{"-fno-color-diagnostics"}));
EXPECT_TRUE(runToolOnCodeWithArgs(
llvm::make_unique<CheckColoredDiagnosticsAction>(true), "",
{"-fno-color-diagnostics", "-fcolor-diagnostics"}));
EXPECT_TRUE(runToolOnCodeWithArgs(
llvm::make_unique<CheckColoredDiagnosticsAction>(false), "",
{"-fcolor-diagnostics", "-fno-color-diagnostics"}));
EXPECT_TRUE(runToolOnCodeWithArgs(
llvm::make_unique<CheckColoredDiagnosticsAction>(true), "",
{"-fno-color-diagnostics", "-fdiagnostics-color=always"})); {"-fno-color-diagnostics", "-fdiagnostics-color=always"}));
// Check that this test would fail if ShowColors is not what it should. // Check that this test would fail if ShowColors is not what it should.
EXPECT_FALSE(runToolOnCodeWithArgs( EXPECT_FALSE(runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(false),
llvm::make_unique<CheckColoredDiagnosticsAction>(false), "", "", {"-fcolor-diagnostics"}));
{"-fcolor-diagnostics"}));
} }
TEST(ClangToolTest, ArgumentAdjusters) { TEST(ClangToolTest, ArgumentAdjusters) {
@ -610,7 +603,7 @@ TEST(runToolOnCode, TestResetDiagnostics) {
// Should not crash // Should not crash
EXPECT_FALSE( EXPECT_FALSE(
runToolOnCode(llvm::make_unique<ResetDiagnosticAction>(), runToolOnCode(new ResetDiagnosticAction,
"struct Foo { Foo(int); ~Foo(); struct Fwd _fwd; };" "struct Foo { Foo(int); ~Foo(); struct Fwd _fwd; };"
"void func() { long x; Foo f(x); }")); "void func() { long x; Foo f(x); }"));
} }