TEST(runToolOnCode, CanSyntaxCheckCode) {
// runToolOnCode returns whether the action was correctly run over the
// given code.
- EXPECT_TRUE(runToolOnCode(new clang::SyntaxOnlyAction, "class X {};"));
+ EXPECT_TRUE(runToolOnCode(std::make_unique<clang::SyntaxOnlyAction>(), "class X {};"));
}
Writing a standalone tool
int main(int argc, char **argv) {
if (argc > 1) {
- clang::tooling::runToolOnCode(new FindNamedClassAction, argv[1]);
+ clang::tooling::runToolOnCode(std::make_unique<FindNamedClassAction>(), argv[1]);
}
}
Clang. If upgrading an external codebase that uses Clang as a library,
this section should help get you past the largest hurdles of upgrading.
+- libTooling APIs that transfer ownership of `FrontendAction` objects now pass
+ them by `unique_ptr`, making the ownership transfer obvious in the type
+ system. `FrontendActionFactory::create()` now returns a
+ `unique_ptr<FrontendAction>`. `runToolOnCode`, `runToolOnCodeWithArgs`,
+ `ToolInvocation::ToolInvocation()` now take a `unique_ptr<FrontendAction>`.
+
Build System Changes
--------------------
/// clang modules.
///
/// \return - True if 'ToolAction' was successfully executed.
-bool runToolOnCode(FrontendAction *ToolAction, const Twine &Code,
+bool runToolOnCode(std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
const Twine &FileName = "input.cc",
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>());
-inline bool
-runToolOnCode(std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
- const Twine &FileName = "input.cc",
- std::shared_ptr<PCHContainerOperations> PCHContainerOps =
- std::make_shared<PCHContainerOperations>()) {
- return runToolOnCode(ToolAction.release(), Code, FileName, PCHContainerOps);
-}
-
/// The first part of the pair is the filename, the second part the
/// file-content.
using FileContentMappings = std::vector<std::pair<std::string, std::string>>;
///
/// \return - True if 'ToolAction' was successfully executed.
bool runToolOnCodeWithArgs(
- 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 =
- std::make_shared<PCHContainerOperations>(),
- const FileContentMappings &VirtualMappedFiles = FileContentMappings());
-
-inline bool runToolOnCodeWithArgs(
std::unique_ptr<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 =
std::make_shared<PCHContainerOperations>(),
- const FileContentMappings &VirtualMappedFiles = FileContentMappings()) {
- return runToolOnCodeWithArgs(ToolAction.release(), Code, Args, FileName,
- ToolName, PCHContainerOps, VirtualMappedFiles);
-}
+ const FileContentMappings &VirtualMappedFiles = FileContentMappings());
// Similar to the overload except this takes a VFS.
bool runToolOnCodeWithArgs(
- FrontendAction *ToolAction, const Twine &Code,
- llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
- const std::vector<std::string> &Args, const Twine &FileName = "input.cc",
- const Twine &ToolName = "clang-tool",
- std::shared_ptr<PCHContainerOperations> PCHContainerOps =
- std::make_shared<PCHContainerOperations>());
-
-inline bool runToolOnCodeWithArgs(
std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
const std::vector<std::string> &Args, const Twine &FileName = "input.cc",
const Twine &ToolName = "clang-tool",
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
- std::make_shared<PCHContainerOperations>()) {
- return runToolOnCodeWithArgs(ToolAction.release(), Code, VFS, Args, FileName,
- ToolName, PCHContainerOps);
-}
+ std::make_shared<PCHContainerOperations>());
/// Builds an AST for 'Code'.
///
/// uses its binary name (CommandLine[0]) to locate its builtin headers.
/// Callers have to ensure that they are installed in a compatible location
/// (see clang driver implementation) or mapped in via mapVirtualFile.
- /// \param FAction The action to be executed. Class takes ownership.
+ /// \param FAction The action to be executed.
/// \param Files The FileManager used for the execution. Class does not take
/// ownership.
/// \param PCHContainerOps The PCHContainerOperations for loading and creating
/// clang modules.
- ToolInvocation(std::vector<std::string> CommandLine, FrontendAction *FAction,
- FileManager *Files,
- std::shared_ptr<PCHContainerOperations> PCHContainerOps =
- std::make_shared<PCHContainerOperations>());
-
ToolInvocation(std::vector<std::string> CommandLine,
std::unique_ptr<FrontendAction> FAction, FileManager *Files,
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
- std::make_shared<PCHContainerOperations>())
- : ToolInvocation(std::move(CommandLine), FAction.release(), Files,
- PCHContainerOps) {}
+ std::make_shared<PCHContainerOperations>());
/// Create a tool invocation.
///
return Invocation;
}
-bool runToolOnCode(FrontendAction *ToolAction, const Twine &Code,
- const Twine &FileName,
+bool runToolOnCode(std::unique_ptr<FrontendAction> ToolAction,
+ const Twine &Code, const Twine &FileName,
std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
- return runToolOnCodeWithArgs(ToolAction, Code, std::vector<std::string>(),
- FileName, "clang-tool",
- std::move(PCHContainerOps));
+ return runToolOnCodeWithArgs(std::move(ToolAction), Code,
+ std::vector<std::string>(), FileName,
+ "clang-tool", std::move(PCHContainerOps));
}
} // namespace tooling
namespace tooling {
bool runToolOnCodeWithArgs(
- FrontendAction *ToolAction, const Twine &Code,
+ std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
const std::vector<std::string> &Args, const Twine &FileName,
const Twine &ToolName,
ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster();
ToolInvocation Invocation(
getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef),
- ToolAction, Files.get(),
- std::move(PCHContainerOps));
+ std::move(ToolAction), Files.get(), std::move(PCHContainerOps));
return Invocation.run();
}
bool runToolOnCodeWithArgs(
- FrontendAction *ToolAction, const Twine &Code,
+ std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
const std::vector<std::string> &Args, const Twine &FileName,
const Twine &ToolName,
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
}
- return runToolOnCodeWithArgs(ToolAction, Code, OverlayFileSystem, Args,
- FileName, ToolName);
+ return runToolOnCodeWithArgs(std::move(ToolAction), Code, OverlayFileSystem,
+ Args, FileName, ToolName);
}
llvm::Expected<std::string> getAbsolutePath(llvm::vfs::FileSystem &FS,
Files(Files), PCHContainerOps(std::move(PCHContainerOps)) {}
ToolInvocation::ToolInvocation(
- std::vector<std::string> CommandLine, FrontendAction *FAction,
- FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps)
+ std::vector<std::string> CommandLine,
+ std::unique_ptr<FrontendAction> FAction, FileManager *Files,
+ std::shared_ptr<PCHContainerOperations> PCHContainerOps)
: CommandLine(std::move(CommandLine)),
- Action(new SingleFrontendActionFactory(
- std::unique_ptr<FrontendAction>(FAction))),
+ Action(new SingleFrontendActionFactory(std::move(FAction))),
OwnsAction(true), Files(Files),
PCHContainerOps(std::move(PCHContainerOps)) {}
std::vector<std::string> Args(1, Mode);
Args.push_back("-fno-delayed-template-parsing");
ASSERT_TRUE(runToolOnCodeWithArgs(
- new EvaluateConstantInitializersAction(),
- "template <typename T>"
- "struct vector {"
- " explicit vector(int size);"
- "};"
- "template <typename R>"
- "struct S {"
- " vector<R> intervals() const {"
- " vector<R> Dependent(2);"
- " return Dependent;"
- " }"
- "};"
- "void doSomething() {"
- " int Constant = 2 + 2;"
- " (void) Constant;"
- "}",
- Args));
+ std::make_unique<EvaluateConstantInitializersAction>(),
+ "template <typename T>"
+ "struct vector {"
+ " explicit vector(int size);"
+ "};"
+ "template <typename R>"
+ "struct S {"
+ " vector<R> intervals() const {"
+ " vector<R> Dependent(2);"
+ " return Dependent;"
+ " }"
+ "};"
+ "void doSomething() {"
+ " int Constant = 2 + 2;"
+ " (void) Constant;"
+ "}",
+ Args));
}
}
std::vector<VisitEvent> collectEvents(llvm::StringRef Code) {
CollectInterestingEvents Visitor;
clang::tooling::runToolOnCode(
- new ProcessASTAction(
+ std::make_unique<ProcessASTAction>(
[&](clang::ASTContext &Ctx) { Visitor.TraverseAST(Ctx); }),
Code);
return std::move(Visitor).takeEvents();
TEST(CrossTranslationUnit, CanLoadFunctionDefinition) {
bool Success = false;
- EXPECT_TRUE(
- tooling::runToolOnCode(new CTUAction(&Success, 1u), "int f(int);"));
+ EXPECT_TRUE(tooling::runToolOnCode(std::make_unique<CTUAction>(&Success, 1u),
+ "int f(int);"));
EXPECT_TRUE(Success);
}
TEST(CrossTranslationUnit, RespectsLoadThreshold) {
bool Success = false;
- EXPECT_TRUE(
- tooling::runToolOnCode(new CTUAction(&Success, 0u), "int f(int);"));
+ EXPECT_TRUE(tooling::runToolOnCode(std::make_unique<CTUAction>(&Success, 0u),
+ "int f(int);"));
EXPECT_FALSE(Success);
}
TEST(IndexTest, Simple) {
auto Index = std::make_shared<Indexer>();
- tooling::runToolOnCode(new IndexAction(Index), "class X {}; void f() {}");
+ tooling::runToolOnCode(std::make_unique<IndexAction>(Index),
+ "class X {}; void f() {}");
EXPECT_THAT(Index->Symbols, UnorderedElementsAre(QName("X"), QName("f")));
}
auto Index = std::make_shared<Indexer>();
IndexingOptions Opts;
Opts.IndexMacrosInPreprocessor = true;
- tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+ tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
EXPECT_THAT(Index->Symbols, Contains(QName("INDEX_MAC")));
Opts.IndexMacrosInPreprocessor = false;
Index->Symbols.clear();
- tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+ tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
EXPECT_THAT(Index->Symbols, UnorderedElementsAre());
}
IndexingOptions Opts;
Opts.IndexFunctionLocals = true;
Opts.IndexParametersInDeclarations = true;
- tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+ tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
EXPECT_THAT(Index->Symbols, Contains(QName("bar")));
Opts.IndexParametersInDeclarations = false;
Index->Symbols.clear();
- tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+ tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
EXPECT_THAT(Index->Symbols, Not(Contains(QName("bar"))));
}
)cpp";
auto Index = std::make_shared<Indexer>();
IndexingOptions Opts;
- tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+ tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
EXPECT_THAT(Index->Symbols,
AllOf(Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)),
DeclAt(Position(5, 12)))),
)cpp";
auto Index = std::make_shared<Indexer>();
IndexingOptions Opts;
- tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+ tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
EXPECT_THAT(Index->Symbols,
Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)),
DeclAt(Position(5, 12)))));
)cpp";
auto Index = std::make_shared<Indexer>();
IndexingOptions Opts;
- tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+ tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
EXPECT_THAT(Index->Symbols, AllOf(Not(Contains(QName("Foo::T"))),
Not(Contains(QName("Foo::I"))),
Not(Contains(QName("Foo::C"))),
Opts.IndexTemplateParameters = true;
Index->Symbols.clear();
- tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+ tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
EXPECT_THAT(Index->Symbols,
AllOf(Contains(QName("Foo::T")), Contains(QName("Foo::I")),
Contains(QName("Foo::C")), Contains(QName("Foo::NoRef"))));
)cpp";
auto Index = std::make_shared<Indexer>();
IndexingOptions Opts;
- tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+ tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
EXPECT_THAT(Index->Symbols,
Contains(AllOf(QName("std::foo"), Kind(SymbolKind::Using))));
}
)cpp";
auto Index = std::make_shared<Indexer>();
IndexingOptions Opts;
- tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+ tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
EXPECT_THAT(
Index->Symbols,
UnorderedElementsAre(
CompletionContext runCompletion(StringRef Code, size_t Offset) {
CompletionContext ResultCtx;
- auto Action = std::make_unique<CodeCompleteAction>(
- offsetToPosition(Code, Offset), ResultCtx);
- clang::tooling::runToolOnCodeWithArgs(Action.release(), Code, {"-std=c++11"},
- TestCCName);
+ clang::tooling::runToolOnCodeWithArgs(
+ std::make_unique<CodeCompleteAction>(offsetToPosition(Code, Offset),
+ ResultCtx),
+ Code, {"-std=c++11"}, TestCCName);
return ResultCtx;
}
// Make sure that the DiagnosticWatcher is not miscounting.
TEST(ExternalSemaSource, SanityCheck) {
- std::unique_ptr<ExternalSemaSourceInstaller> Installer(
- new ExternalSemaSourceInstaller);
+ auto Installer = std::make_unique<ExternalSemaSourceInstaller>();
DiagnosticWatcher Watcher("AAB", "BBB");
Installer->PushWatcher(&Watcher);
std::vector<std::string> Args(1, "-std=c++11");
ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs(
- Installer.release(), "namespace AAA { } using namespace AAB;", Args));
+ std::move(Installer), "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) {
- std::unique_ptr<ExternalSemaSourceInstaller> Installer(
- new ExternalSemaSourceInstaller);
+ auto Installer = std::make_unique<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(
- Installer.release(), "namespace AAA { } using namespace AAB;", Args));
+ std::move(Installer), "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) {
- std::unique_ptr<ExternalSemaSourceInstaller> Installer(
- new ExternalSemaSourceInstaller);
+ auto Installer = std::make_unique<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(
- Installer.release(), "namespace AAA { } using namespace AAB;", Args));
+ std::move(Installer), "namespace AAA { } using namespace AAB;", Args));
ASSERT_LE(1, First.CallCount);
ASSERT_LE(1, Second.CallCount);
ASSERT_EQ(0, Third.CallCount);
}
TEST(ExternalSemaSource, ExternalDelayedTypoCorrection) {
- std::unique_ptr<ExternalSemaSourceInstaller> Installer(
- new ExternalSemaSourceInstaller);
+ auto Installer = std::make_unique<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(
- Installer.release(), "namespace AAA { } void foo() { AAA::aaa(); }",
+ std::move(Installer), "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) {
- std::unique_ptr<ExternalSemaSourceInstaller> Installer(
- new ExternalSemaSourceInstaller);
+ auto Installer = std::make_unique<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(
- Installer.release(),
+ std::move(Installer),
"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) {
- std::unique_ptr<ExternalSemaSourceInstaller> Installer(
- new ExternalSemaSourceInstaller);
+ auto Installer = std::make_unique<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(
- Installer.release(), "class Incomplete; Incomplete IncompleteInstance;",
+ std::move(Installer), "class Incomplete; Incomplete IncompleteInstance;",
Args));
ASSERT_EQ(1, First.CallCount);
ASSERT_EQ(1, Second.CallCount);
TEST(CallEvent, CallDescription) {
// Test simple name matching.
EXPECT_TRUE(tooling::runToolOnCode(
- new CallDescriptionAction({
+ std::unique_ptr<CallDescriptionAction>(new CallDescriptionAction({
{{"bar"}, false}, // false: there's no call to 'bar' in this code.
{{"foo"}, true}, // true: there's a call to 'foo' in this code.
- }), "void foo(); void bar() { foo(); }"));
+ })), "void foo(); void bar() { foo(); }"));
// Test arguments check.
EXPECT_TRUE(tooling::runToolOnCode(
- new CallDescriptionAction({
+ std::unique_ptr<CallDescriptionAction>(new CallDescriptionAction({
{{"foo", 1}, true},
{{"foo", 2}, false},
- }), "void foo(int); void foo(int, int); void bar() { foo(1); }"));
+ })), "void foo(int); void foo(int, int); void bar() { foo(1); }"));
// Test lack of arguments check.
EXPECT_TRUE(tooling::runToolOnCode(
- new CallDescriptionAction({
+ std::unique_ptr<CallDescriptionAction>(new CallDescriptionAction({
{{"foo", None}, true},
{{"foo", 2}, false},
- }), "void foo(int); void foo(int, int); void bar() { foo(1); }"));
+ })), "void foo(int); void foo(int, int); void bar() { foo(1); }"));
// Test qualified names.
EXPECT_TRUE(tooling::runToolOnCode(
- new CallDescriptionAction({
+ std::unique_ptr<CallDescriptionAction>(new CallDescriptionAction({
{{{"std", "basic_string", "c_str"}}, true},
- }),
+ })),
"namespace std { inline namespace __1 {"
" template<typename T> class basic_string {"
" public:"
// A negative test for qualified names.
EXPECT_TRUE(tooling::runToolOnCode(
- new CallDescriptionAction({
+ std::unique_ptr<CallDescriptionAction>(new CallDescriptionAction({
{{{"foo", "bar"}}, false},
{{{"bar", "foo"}}, false},
{{"foo"}, true},
- }), "void foo(); struct bar { void foo(); }; void test() { foo(); }"));
+ })), "void foo(); struct bar { void foo(); }; void test() { foo(); }"));
// Test CDF_MaybeBuiltin - a flag that allows matching weird builtins.
EXPECT_TRUE(tooling::runToolOnCode(
- new CallDescriptionAction({
+ std::unique_ptr<CallDescriptionAction>(new CallDescriptionAction({
{{"memset", 3}, false},
{{CDF_MaybeBuiltin, "memset", 3}, true}
- }),
+ })),
"void foo() {"
" int x;"
" __builtin___memset_chk(&x, 0, sizeof(x),"
template <typename CheckerT>
bool runCheckerOnCode(const std::string &Code, std::string &Diags) {
llvm::raw_string_ostream OS(Diags);
- return tooling::runToolOnCode(new TestAction<CheckerT>(OS), Code);
+ return tooling::runToolOnCode(std::make_unique<TestAction<CheckerT>>(OS),
+ Code);
}
template <typename CheckerT>
bool runCheckerOnCode(const std::string &Code) {
};
TEST(Store, VariableBind) {
- EXPECT_TRUE(tooling::runToolOnCode(
- new VariableBindAction, "void foo() { int x0, y0, z0, x1, y1; }"));
+ EXPECT_TRUE(tooling::runToolOnCode(std::make_unique<VariableBindAction>(),
+ "void foo() { int x0, y0, z0, x1, y1; }"));
}
} // namespace
// Test that marking s.x as live would also make s live.
TEST(SymbolReaper, SuperRegionLiveness) {
- EXPECT_TRUE(tooling::runToolOnCode(new SuperRegionLivenessAction,
- "void foo() { struct S { int x; } s; }"));
+ EXPECT_TRUE(
+ tooling::runToolOnCode(std::make_unique<SuperRegionLivenessAction>(),
+ "void foo() { struct S { int x; } s; }"));
}
} // namespace
CommentVerifier GetVerifier();
protected:
- ASTFrontendAction *CreateTestAction() override {
- return new CommentHandlerAction(this);
+ std::unique_ptr<ASTFrontendAction> CreateTestAction() override {
+ return std::make_unique<CommentHandlerAction>(this);
}
private:
class TestVisitor : public clang::RecursiveASTVisitor<T> {
public:
bool runOver(StringRef Code) {
- return runToolOnCode(new TestAction(this), Code);
+ return runToolOnCode(std::make_unique<TestAction>(this), Code);
}
protected:
}
protected:
- virtual ASTFrontendAction* CreateTestAction() {
- return new TestAction(this);
+ virtual std::unique_ptr<ASTFrontendAction> CreateTestAction() {
+ return std::make_unique<TestAction>(this);
}
class FindConsumer : public ASTConsumer {
TEST(runToolOnCode, FindsNoTopLevelDeclOnEmptyCode) {
bool FoundTopLevelDecl = false;
- EXPECT_TRUE(
- runToolOnCode(new TestAction(std::make_unique<FindTopLevelDeclConsumer>(
- &FoundTopLevelDecl)),
- ""));
+ EXPECT_TRUE(runToolOnCode(
+ std::make_unique<TestAction>(
+ std::make_unique<FindTopLevelDeclConsumer>(&FoundTopLevelDecl)),
+ ""));
EXPECT_FALSE(FoundTopLevelDecl);
}
TEST(runToolOnCode, FindsClassDecl) {
bool FoundClassDeclX = false;
- EXPECT_TRUE(
- runToolOnCode(new TestAction(std::make_unique<FindClassDeclXConsumer>(
- &FoundClassDeclX)),
- "class X;"));
+ EXPECT_TRUE(runToolOnCode(
+ std::make_unique<TestAction>(
+ std::make_unique<FindClassDeclXConsumer>(&FoundClassDeclX)),
+ "class X;"));
EXPECT_TRUE(FoundClassDeclX);
FoundClassDeclX = false;
- EXPECT_TRUE(
- runToolOnCode(new TestAction(std::make_unique<FindClassDeclXConsumer>(
- &FoundClassDeclX)),
- "class Y;"));
+ EXPECT_TRUE(runToolOnCode(
+ std::make_unique<TestAction>(
+ std::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, new SyntaxOnlyAction,
- Files.get());
+ clang::tooling::ToolInvocation Invocation(
+ Args, std::make_unique<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, new SyntaxOnlyAction,
- Files.get());
+ clang::tooling::ToolInvocation Invocation(
+ Args, std::make_unique<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(new SkipBodyAction,
+ EXPECT_TRUE(runToolOnCode(std::make_unique<SkipBodyAction>(),
"int skipMe() { an_error_here }"));
- EXPECT_FALSE(runToolOnCode(new SkipBodyAction,
+ EXPECT_FALSE(runToolOnCode(std::make_unique<SkipBodyAction>(),
"int skipMeNot() { an_error_here }"));
// Test constructors with initializers
EXPECT_TRUE(runToolOnCodeWithArgs(
- new SkipBodyAction,
+ std::make_unique<SkipBodyAction>(),
"struct skipMe { skipMe() : an_error() { more error } };", Args));
EXPECT_TRUE(runToolOnCodeWithArgs(
- new SkipBodyAction, "struct skipMe { skipMe(); };"
+ std::make_unique<SkipBodyAction>(), "struct skipMe { skipMe(); };"
"skipMe::skipMe() : an_error([](){;}) { more error }",
Args));
EXPECT_TRUE(runToolOnCodeWithArgs(
- new SkipBodyAction, "struct skipMe { skipMe(); };"
+ std::make_unique<SkipBodyAction>(), "struct skipMe { skipMe(); };"
"skipMe::skipMe() : an_error{[](){;}} { more error }",
Args));
EXPECT_TRUE(runToolOnCodeWithArgs(
- new SkipBodyAction,
+ std::make_unique<SkipBodyAction>(),
"struct skipMe { skipMe(); };"
"skipMe::skipMe() : a<b<c>(e)>>(), f{}, g() { error }",
Args));
EXPECT_TRUE(runToolOnCodeWithArgs(
- new SkipBodyAction, "struct skipMe { skipMe() : bases()... { error } };",
+ std::make_unique<SkipBodyAction>(), "struct skipMe { skipMe() : bases()... { error } };",
Args));
EXPECT_FALSE(runToolOnCodeWithArgs(
- new SkipBodyAction, "struct skipMeNot { skipMeNot() : an_error() { } };",
+ std::make_unique<SkipBodyAction>(), "struct skipMeNot { skipMeNot() : an_error() { } };",
Args));
- EXPECT_FALSE(runToolOnCodeWithArgs(new SkipBodyAction,
+ EXPECT_FALSE(runToolOnCodeWithArgs(std::make_unique<SkipBodyAction>(),
"struct skipMeNot { skipMeNot(); };"
"skipMeNot::skipMeNot() : an_error() { }",
Args));
// Try/catch
EXPECT_TRUE(runToolOnCode(
- new SkipBodyAction,
+ std::make_unique<SkipBodyAction>(),
"void skipMe() try { an_error() } catch(error) { error };"));
EXPECT_TRUE(runToolOnCode(
- new SkipBodyAction,
+ std::make_unique<SkipBodyAction>(),
"struct S { void skipMe() try { an_error() } catch(error) { error } };"));
EXPECT_TRUE(
- runToolOnCode(new SkipBodyAction,
+ runToolOnCode(std::make_unique<SkipBodyAction>(),
"void skipMe() try { an_error() } catch(error) { error; }"
"catch(error) { error } catch (error) { }"));
EXPECT_FALSE(runToolOnCode(
- new SkipBodyAction,
+ std::make_unique<SkipBodyAction>(),
"void skipMe() try something;")); // don't crash while parsing
// Template
EXPECT_TRUE(runToolOnCode(
- new SkipBodyAction, "template<typename T> int skipMe() { an_error_here }"
+ std::make_unique<SkipBodyAction>(), "template<typename T> int skipMe() { an_error_here }"
"int x = skipMe<int>();"));
EXPECT_FALSE(runToolOnCodeWithArgs(
- new SkipBodyAction,
+ std::make_unique<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(new SkipBodyAction, "", Args));
+ EXPECT_TRUE(runToolOnCodeWithArgs(std::make_unique<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(
- new CheckColoredDiagnosticsAction(true), "",
+ std::make_unique<CheckColoredDiagnosticsAction>(true), "",
+ {"-fcolor-diagnostics"}));
+ EXPECT_TRUE(runToolOnCodeWithArgs(
+ std::make_unique<CheckColoredDiagnosticsAction>(false), "",
+ {"-fno-color-diagnostics"}));
+ EXPECT_TRUE(runToolOnCodeWithArgs(
+ std::make_unique<CheckColoredDiagnosticsAction>(true), "",
+ {"-fno-color-diagnostics", "-fcolor-diagnostics"}));
+ EXPECT_TRUE(runToolOnCodeWithArgs(
+ std::make_unique<CheckColoredDiagnosticsAction>(false), "",
+ {"-fcolor-diagnostics", "-fno-color-diagnostics"}));
+ EXPECT_TRUE(runToolOnCodeWithArgs(
+ std::make_unique<CheckColoredDiagnosticsAction>(true), "",
{"-fno-color-diagnostics", "-fdiagnostics-color=always"}));
// Check that this test would fail if ShowColors is not what it should.
- EXPECT_FALSE(runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(false),
- "", {"-fcolor-diagnostics"}));
+ EXPECT_FALSE(runToolOnCodeWithArgs(
+ std::make_unique<CheckColoredDiagnosticsAction>(false), "",
+ {"-fcolor-diagnostics"}));
}
TEST(ClangToolTest, ArgumentAdjusters) {
// Should not crash
EXPECT_FALSE(
- runToolOnCode(new ResetDiagnosticAction,
+ runToolOnCode(std::make_unique<ResetDiagnosticAction>(),
"struct Foo { Foo(int); ~Foo(); struct Fwd _fwd; };"
"void func() { long x; Foo f(x); }"));
}