TEST(runToolOnCode, CanSyntaxCheckCode) {
// runToolOnCode returns whether the action was correctly run over the
// given code.
- EXPECT_TRUE(runToolOnCode(llvm::make_unique<clang::SyntaxOnlyAction>(), "class X {};"));
+ EXPECT_TRUE(runToolOnCode(new clang::SyntaxOnlyAction, "class X {};"));
}
Writing a standalone tool
int main(int argc, char **argv) {
if (argc > 1) {
- clang::tooling::runToolOnCode(llvm::make_unique<FindNamedClassAction>(), argv[1]);
+ clang::tooling::runToolOnCode(new FindNamedClassAction, argv[1]);
}
}
/// \brief Returns a new clang::FrontendAction.
///
/// 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.
/// clang modules.
///
/// \return - True if 'ToolAction' was successfully executed.
-bool runToolOnCode(std::unique_ptr<FrontendAction> ToolAction,
- const Twine &Code, const Twine &FileName = "input.cc",
+bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code,
+ const Twine &FileName = "input.cc",
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>());
///
/// \return - True if 'ToolAction' was successfully executed.
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 Twine &ToolName = "clang-tool",
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
/// ownership.
/// \param PCHContainerOps The PCHContainerOperations for loading and creating
/// clang modules.
- ToolInvocation(std::vector<std::string> CommandLine,
- std::unique_ptr<FrontendAction> FAction, FileManager *Files,
+ ToolInvocation(std::vector<std::string> CommandLine, FrontendAction *FAction,
+ FileManager *Files,
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>());
std::unique_ptr<FrontendActionFactory> newFrontendActionFactory() {
class SimpleFrontendActionFactory : public FrontendActionFactory {
public:
- std::unique_ptr<clang::FrontendAction> create() override {
- return llvm::make_unique<T>();
- }
+ clang::FrontendAction *create() override { return new T; }
};
return std::unique_ptr<FrontendActionFactory>(
SourceFileCallbacks *Callbacks)
: ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {}
- std::unique_ptr<clang::FrontendAction> create() override {
- return llvm::make_unique<ConsumerFactoryAdaptor>(ConsumerFactory,
- Callbacks);
+ clang::FrontendAction *create() override {
+ return new ConsumerFactoryAdaptor(ConsumerFactory, Callbacks);
}
private:
return Invocation;
}
-bool runToolOnCode(std::unique_ptr<FrontendAction> ToolAction,
- const Twine &Code, const Twine &FileName,
+bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code,
+ const Twine &FileName,
std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
- return runToolOnCodeWithArgs(std::move(ToolAction), Code,
- std::vector<std::string>(), FileName,
- "clang-tool", std::move(PCHContainerOps));
+ return runToolOnCodeWithArgs(ToolAction, Code, std::vector<std::string>(),
+ FileName, "clang-tool",
+ std::move(PCHContainerOps));
}
static std::vector<std::string>
}
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 Twine &ToolName,
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster();
ToolInvocation Invocation(
getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef),
- std::move(ToolAction), Files.get(), std::move(PCHContainerOps));
+ ToolAction, Files.get(),
+ std::move(PCHContainerOps));
SmallString<1024> CodeStorage;
InMemoryFileSystem->addFile(FileNameRef, 0,
namespace {
class SingleFrontendActionFactory : public FrontendActionFactory {
- std::unique_ptr<clang::FrontendAction> Action;
+ FrontendAction *Action;
public:
- SingleFrontendActionFactory(std::unique_ptr<clang::FrontendAction> Action)
- : Action(std::move(Action)) {}
+ SingleFrontendActionFactory(FrontendAction *Action) : Action(Action) {}
- std::unique_ptr<clang::FrontendAction> create() override {
- return std::move(Action);
- }
+ FrontendAction *create() override { return Action; }
};
-} // namespace
+}
ToolInvocation::ToolInvocation(
std::vector<std::string> CommandLine, ToolAction *Action,
DiagConsumer(nullptr) {}
ToolInvocation::ToolInvocation(
- std::vector<std::string> CommandLine,
- std::unique_ptr<FrontendAction> FAction, FileManager *Files,
- std::shared_ptr<PCHContainerOperations> PCHContainerOps)
+ std::vector<std::string> CommandLine, FrontendAction *FAction,
+ FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps)
: CommandLine(std::move(CommandLine)),
- Action(new SingleFrontendActionFactory(std::move(FAction))),
- OwnsAction(true), Files(Files),
- PCHContainerOps(std::move(PCHContainerOps)), DiagConsumer(nullptr) {}
+ Action(new SingleFrontendActionFactory(FAction)), OwnsAction(true),
+ Files(Files), PCHContainerOps(std::move(PCHContainerOps)),
+ DiagConsumer(nullptr) {}
ToolInvocation::~ToolInvocation() {
if (OwnsAction)
ToolActionFactory(TUCallbackType Callback)
: Callback(std::move(Callback)) {}
- std::unique_ptr<FrontendAction> create() override {
- return llvm::make_unique<ToolASTAction>(Callback);
- }
+ FrontendAction *create() override { return new ToolASTAction(Callback); }
private:
TUCallbackType Callback;
std::vector<std::string> Args(1, Mode);
Args.push_back("-fno-delayed-template-parsing");
ASSERT_TRUE(runToolOnCodeWithArgs(
- llvm::make_unique<EvaluateConstantInitializersAction>(),
+ new EvaluateConstantInitializersAction(),
"template <typename T>"
"struct vector {"
" explicit vector(int size);"
TEST(CrossTranslationUnit, CanLoadFunctionDefinition) {
bool Success = false;
- EXPECT_TRUE(tooling::runToolOnCode(llvm::make_unique<CTUAction>(&Success),
- "int f(int);"));
+ EXPECT_TRUE(tooling::runToolOnCode(new CTUAction(&Success), "int f(int);"));
EXPECT_TRUE(Success);
}
auto Action = llvm::make_unique<CodeCompleteAction>(
offsetToPosition(WithoutToken, TokenOffset), Results);
- clang::tooling::runToolOnCodeWithArgs(std::move(Action), Code, {"-std=c++11"},
+ clang::tooling::runToolOnCodeWithArgs(Action.release(), Code, {"-std=c++11"},
TestCCName);
return Results;
}
// Make sure that the DiagnosticWatcher is not miscounting.
TEST(ExternalSemaSource, SanityCheck) {
- auto Installer = llvm::make_unique<ExternalSemaSourceInstaller>();
+ std::unique_ptr<ExternalSemaSourceInstaller> Installer(
+ new ExternalSemaSourceInstaller);
DiagnosticWatcher Watcher("AAB", "BBB");
Installer->PushWatcher(&Watcher);
std::vector<std::string> Args(1, "-std=c++11");
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);
}
// Check that when we add a NamespaceTypeProvider, we use that suggestion
// instead of the usual suggestion we would use above.
TEST(ExternalSemaSource, ExternalTypoCorrectionPrioritized) {
- auto Installer = llvm::make_unique<ExternalSemaSourceInstaller>();
+ std::unique_ptr<ExternalSemaSourceInstaller> Installer(
+ new ExternalSemaSourceInstaller);
NamespaceTypoProvider Provider("AAB", "BBB");
DiagnosticWatcher Watcher("AAB", "BBB");
Installer->PushSource(&Provider);
Installer->PushWatcher(&Watcher);
std::vector<std::string> Args(1, "-std=c++11");
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_EQ(1, Watcher.SeenCount);
}
// Check that we use the first successful TypoCorrection returned from an
// ExternalSemaSource.
TEST(ExternalSemaSource, ExternalTypoCorrectionOrdering) {
- auto Installer = llvm::make_unique<ExternalSemaSourceInstaller>();
+ std::unique_ptr<ExternalSemaSourceInstaller> Installer(
+ new ExternalSemaSourceInstaller);
NamespaceTypoProvider First("XXX", "BBB");
NamespaceTypoProvider Second("AAB", "CCC");
NamespaceTypoProvider Third("AAB", "DDD");
Installer->PushWatcher(&Watcher);
std::vector<std::string> Args(1, "-std=c++11");
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, Second.CallCount);
ASSERT_EQ(0, Third.CallCount);
}
TEST(ExternalSemaSource, ExternalDelayedTypoCorrection) {
- auto Installer = llvm::make_unique<ExternalSemaSourceInstaller>();
+ std::unique_ptr<ExternalSemaSourceInstaller> Installer(
+ new ExternalSemaSourceInstaller);
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(
- std::move(Installer), "namespace AAA { } void foo() { AAA::aaa(); }",
+ Installer.release(), "namespace AAA { } void foo() { AAA::aaa(); }",
Args));
ASSERT_LE(0, Provider.CallCount);
ASSERT_EQ(1, Watcher.SeenCount);
// We should only try MaybeDiagnoseMissingCompleteType if we can't otherwise
// solve the problem.
TEST(ExternalSemaSource, TryOtherTacticsBeforeDiagnosing) {
- auto Installer = llvm::make_unique<ExternalSemaSourceInstaller>();
+ std::unique_ptr<ExternalSemaSourceInstaller> Installer(
+ new ExternalSemaSourceInstaller);
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(
- std::move(Installer),
+ Installer.release(),
"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) {
- auto Installer = llvm::make_unique<ExternalSemaSourceInstaller>();
+ std::unique_ptr<ExternalSemaSourceInstaller> Installer(
+ new ExternalSemaSourceInstaller);
CompleteTypeDiagnoser First(false);
CompleteTypeDiagnoser Second(true);
CompleteTypeDiagnoser Third(true);
Installer->PushSource(&Third);
std::vector<std::string> Args(1, "-std=c++11");
ASSERT_FALSE(clang::tooling::runToolOnCodeWithArgs(
- std::move(Installer), "class Incomplete; Incomplete IncompleteInstance;",
+ Installer.release(), "class Incomplete; Incomplete IncompleteInstance;",
Args));
ASSERT_EQ(1, First.CallCount);
ASSERT_EQ(1, Second.CallCount);
CommentVerifier GetVerifier();
protected:
- std::unique_ptr<FrontendAction> CreateTestAction() override {
- return llvm::make_unique<CommentHandlerAction>(this);
+ ASTFrontendAction *CreateTestAction() override {
+ return new CommentHandlerAction(this);
}
private:
class ReportResultActionFactory : public FrontendActionFactory {
public:
ReportResultActionFactory(ExecutionContext *Context) : Context(Context) {}
- std::unique_ptr<FrontendAction> create() override {
- return llvm::make_unique<ReportResultAction>(Context);
- }
+ FrontendAction *create() override { return new ReportResultAction(Context); }
private:
ExecutionContext *const Context;
class TestVisitor : public clang::RecursiveASTVisitor<T> {
public:
bool runOver(StringRef Code) {
- return runToolOnCode(llvm::make_unique<TestAction>(this), Code);
+ return runToolOnCode(new TestAction(this), Code);
}
protected:
}
protected:
- virtual std::unique_ptr<FrontendAction> CreateTestAction() {
- return llvm::make_unique<TestAction>(this);
+ virtual ASTFrontendAction* CreateTestAction() {
+ return new TestAction(this);
}
class FindConsumer : public ASTConsumer {
TEST(runToolOnCode, FindsNoTopLevelDeclOnEmptyCode) {
bool FoundTopLevelDecl = false;
- EXPECT_TRUE(runToolOnCode(
- llvm::make_unique<TestAction>(
- llvm::make_unique<FindTopLevelDeclConsumer>(&FoundTopLevelDecl)),
- ""));
+ EXPECT_TRUE(
+ runToolOnCode(new TestAction(llvm::make_unique<FindTopLevelDeclConsumer>(
+ &FoundTopLevelDecl)),
+ ""));
EXPECT_FALSE(FoundTopLevelDecl);
}
TEST(runToolOnCode, FindsClassDecl) {
bool FoundClassDeclX = false;
- EXPECT_TRUE(runToolOnCode(
- llvm::make_unique<TestAction>(
- llvm::make_unique<FindClassDeclXConsumer>(&FoundClassDeclX)),
- "class X;"));
+ EXPECT_TRUE(
+ runToolOnCode(new TestAction(llvm::make_unique<FindClassDeclXConsumer>(
+ &FoundClassDeclX)),
+ "class X;"));
EXPECT_TRUE(FoundClassDeclX);
FoundClassDeclX = false;
- EXPECT_TRUE(runToolOnCode(
- llvm::make_unique<TestAction>(
- llvm::make_unique<FindClassDeclXConsumer>(&FoundClassDeclX)),
- "class Y;"));
+ EXPECT_TRUE(
+ runToolOnCode(new TestAction(llvm::make_unique<FindClassDeclXConsumer>(
+ &FoundClassDeclX)),
+ "class Y;"));
EXPECT_FALSE(FoundClassDeclX);
}
Args.push_back("-Idef");
Args.push_back("-fsyntax-only");
Args.push_back("test.cpp");
- clang::tooling::ToolInvocation Invocation(
- Args, llvm::make_unique<SyntaxOnlyAction>(), Files.get());
+ clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction,
+ Files.get());
InMemoryFileSystem->addFile(
"test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("#include <abc>\n"));
InMemoryFileSystem->addFile("def/abc", 0,
Args.push_back("-Idef");
Args.push_back("-fsyntax-only");
Args.push_back("test.cpp");
- clang::tooling::ToolInvocation Invocation(
- Args, llvm::make_unique<SyntaxOnlyAction>(), Files.get());
+ clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction,
+ Files.get());
InMemoryFileSystem->addFile(
"test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("#include <abc>\n"));
InMemoryFileSystem->addFile("def/abc", 0,
std::vector<std::string> Args = {"-std=c++11"};
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 }"));
- EXPECT_FALSE(runToolOnCode(llvm::make_unique<SkipBodyAction>(),
+ EXPECT_FALSE(runToolOnCode(new SkipBodyAction,
"int skipMeNot() { an_error_here }"));
// Test constructors with initializers
EXPECT_TRUE(runToolOnCodeWithArgs(
- llvm::make_unique<SkipBodyAction>(),
+ new SkipBodyAction,
"struct skipMe { skipMe() : an_error() { more error } };", Args));
EXPECT_TRUE(runToolOnCodeWithArgs(
- llvm::make_unique<SkipBodyAction>(),
- "struct skipMe { skipMe(); };"
- "skipMe::skipMe() : an_error([](){;}) { more error }",
+ new SkipBodyAction, "struct skipMe { skipMe(); };"
+ "skipMe::skipMe() : an_error([](){;}) { more error }",
Args));
EXPECT_TRUE(runToolOnCodeWithArgs(
- llvm::make_unique<SkipBodyAction>(),
- "struct skipMe { skipMe(); };"
- "skipMe::skipMe() : an_error{[](){;}} { more error }",
+ new SkipBodyAction, "struct skipMe { skipMe(); };"
+ "skipMe::skipMe() : an_error{[](){;}} { more error }",
Args));
EXPECT_TRUE(runToolOnCodeWithArgs(
- llvm::make_unique<SkipBodyAction>(),
+ new SkipBodyAction,
"struct skipMe { skipMe(); };"
"skipMe::skipMe() : a<b<c>(e)>>(), f{}, g() { error }",
Args));
EXPECT_TRUE(runToolOnCodeWithArgs(
- llvm::make_unique<SkipBodyAction>(),
- "struct skipMe { skipMe() : bases()... { error } };", Args));
+ new SkipBodyAction, "struct skipMe { skipMe() : bases()... { error } };",
+ Args));
EXPECT_FALSE(runToolOnCodeWithArgs(
- llvm::make_unique<SkipBodyAction>(),
- "struct skipMeNot { skipMeNot() : an_error() { } };", Args));
- EXPECT_FALSE(runToolOnCodeWithArgs(llvm::make_unique<SkipBodyAction>(),
+ new SkipBodyAction, "struct skipMeNot { skipMeNot() : an_error() { } };",
+ Args));
+ EXPECT_FALSE(runToolOnCodeWithArgs(new SkipBodyAction,
"struct skipMeNot { skipMeNot(); };"
"skipMeNot::skipMeNot() : an_error() { }",
Args));
// Try/catch
EXPECT_TRUE(runToolOnCode(
- llvm::make_unique<SkipBodyAction>(),
+ new SkipBodyAction,
"void skipMe() try { an_error() } catch(error) { error };"));
EXPECT_TRUE(runToolOnCode(
- llvm::make_unique<SkipBodyAction>(),
+ new SkipBodyAction,
"struct S { void skipMe() try { an_error() } catch(error) { error } };"));
EXPECT_TRUE(
- runToolOnCode(llvm::make_unique<SkipBodyAction>(),
+ runToolOnCode(new SkipBodyAction,
"void skipMe() try { an_error() } catch(error) { error; }"
"catch(error) { error } catch (error) { }"));
EXPECT_FALSE(runToolOnCode(
- llvm::make_unique<SkipBodyAction>(),
+ new SkipBodyAction,
"void skipMe() try something;")); // don't crash while parsing
// Template
- EXPECT_TRUE(
- runToolOnCode(llvm::make_unique<SkipBodyAction>(),
- "template<typename T> int skipMe() { an_error_here }"
- "int x = skipMe<int>();"));
+ EXPECT_TRUE(runToolOnCode(
+ new SkipBodyAction, "template<typename T> int skipMe() { an_error_here }"
+ "int x = skipMe<int>();"));
EXPECT_FALSE(runToolOnCodeWithArgs(
- llvm::make_unique<SkipBodyAction>(),
+ new SkipBodyAction,
"template<typename T> int skipMeNot() { an_error_here }", Args2));
}
Args.push_back(DepFilePath.str());
Args.push_back("-MF");
Args.push_back(DepFilePath.str());
- EXPECT_TRUE(
- runToolOnCodeWithArgs(llvm::make_unique<SkipBodyAction>(), "", Args));
+ EXPECT_TRUE(runToolOnCodeWithArgs(new SkipBodyAction, "", Args));
EXPECT_FALSE(llvm::sys::fs::exists(DepFilePath.str()));
EXPECT_FALSE(llvm::sys::fs::remove(DepFilePath.str()));
}
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(
- llvm::make_unique<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), "",
+ new CheckColoredDiagnosticsAction(true), "",
{"-fno-color-diagnostics", "-fdiagnostics-color=always"}));
// Check that this test would fail if ShowColors is not what it should.
- EXPECT_FALSE(runToolOnCodeWithArgs(
- llvm::make_unique<CheckColoredDiagnosticsAction>(false), "",
- {"-fcolor-diagnostics"}));
+ EXPECT_FALSE(runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(false),
+ "", {"-fcolor-diagnostics"}));
}
TEST(ClangToolTest, ArgumentAdjusters) {
// Should not crash
EXPECT_FALSE(
- runToolOnCode(llvm::make_unique<ResetDiagnosticAction>(),
+ runToolOnCode(new ResetDiagnosticAction,
"struct Foo { Foo(int); ~Foo(); struct Fwd _fwd; };"
"void func() { long x; Foo f(x); }"));
}