summaryrefslogtreecommitdiff
path: root/unittests/Tooling
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2013-04-08 18:45:10 +0000
committerDimitry Andric <dim@FreeBSD.org>2013-04-08 18:45:10 +0000
commit809500fc2c13c8173a16b052304d983864e4a1e1 (patch)
tree4fc2f184c499d106f29a386c452b49e5197bf63d /unittests/Tooling
parentbe7c9ec198dcdb5bf73a35bfbb00b3333cb87909 (diff)
Notes
Diffstat (limited to 'unittests/Tooling')
-rw-r--r--unittests/Tooling/CMakeLists.txt1
-rw-r--r--unittests/Tooling/CompilationDatabaseTest.cpp67
-rw-r--r--unittests/Tooling/Makefile2
-rw-r--r--unittests/Tooling/RecursiveASTVisitorTest.cpp5
-rw-r--r--unittests/Tooling/RefactoringCallbacksTest.cpp6
-rw-r--r--unittests/Tooling/RefactoringTest.cpp8
-rw-r--r--unittests/Tooling/RewriterTestContext.h8
-rw-r--r--unittests/Tooling/TestVisitor.h7
-rw-r--r--unittests/Tooling/ToolingTest.cpp36
9 files changed, 110 insertions, 30 deletions
diff --git a/unittests/Tooling/CMakeLists.txt b/unittests/Tooling/CMakeLists.txt
index bd7317fe4ae88..245c0599d4270 100644
--- a/unittests/Tooling/CMakeLists.txt
+++ b/unittests/Tooling/CMakeLists.txt
@@ -1,6 +1,7 @@
set(LLVM_LINK_COMPONENTS
${LLVM_TARGETS_TO_BUILD}
asmparser
+ bitreader
support
mc
)
diff --git a/unittests/Tooling/CompilationDatabaseTest.cpp b/unittests/Tooling/CompilationDatabaseTest.cpp
index 5ed4240c1ee0f..c453b056d2570 100644
--- a/unittests/Tooling/CompilationDatabaseTest.cpp
+++ b/unittests/Tooling/CompilationDatabaseTest.cpp
@@ -42,7 +42,7 @@ TEST(JSONCompilationDatabase, ErrsOnInvalidFormat) {
static std::vector<std::string> getAllFiles(StringRef JSONDatabase,
std::string &ErrorMessage) {
- llvm::OwningPtr<CompilationDatabase> Database(
+ OwningPtr<CompilationDatabase> Database(
JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage));
if (!Database) {
ADD_FAILURE() << ErrorMessage;
@@ -51,6 +51,17 @@ static std::vector<std::string> getAllFiles(StringRef JSONDatabase,
return Database->getAllFiles();
}
+static std::vector<CompileCommand> getAllCompileCommands(StringRef JSONDatabase,
+ std::string &ErrorMessage) {
+ OwningPtr<CompilationDatabase> Database(
+ JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage));
+ if (!Database) {
+ ADD_FAILURE() << ErrorMessage;
+ return std::vector<CompileCommand>();
+ }
+ return Database->getAllCompileCommands();
+}
+
TEST(JSONCompilationDatabase, GetAllFiles) {
std::string ErrorMessage;
EXPECT_EQ(std::vector<std::string>(),
@@ -72,10 +83,39 @@ TEST(JSONCompilationDatabase, GetAllFiles) {
ErrorMessage)) << ErrorMessage;
}
+TEST(JSONCompilationDatabase, GetAllCompileCommands) {
+ std::string ErrorMessage;
+ EXPECT_EQ(0u,
+ getAllCompileCommands("[]", ErrorMessage).size()) << ErrorMessage;
+
+ StringRef Directory1("//net/dir1");
+ StringRef FileName1("file1");
+ StringRef Command1("command1");
+ StringRef Directory2("//net/dir2");
+ StringRef FileName2("file1");
+ StringRef Command2("command1");
+
+ std::vector<CompileCommand> Commands = getAllCompileCommands(
+ ("[{\"directory\":\"" + Directory1 + "\"," +
+ "\"command\":\"" + Command1 + "\","
+ "\"file\":\"" + FileName1 + "\"},"
+ " {\"directory\":\"" + Directory2 + "\"," +
+ "\"command\":\"" + Command2 + "\","
+ "\"file\":\"" + FileName2 + "\"}]").str(),
+ ErrorMessage);
+ EXPECT_EQ(2U, Commands.size()) << ErrorMessage;
+ EXPECT_EQ(Directory1, Commands[0].Directory) << ErrorMessage;
+ ASSERT_EQ(1u, Commands[0].CommandLine.size());
+ EXPECT_EQ(Command1, Commands[0].CommandLine[0]) << ErrorMessage;
+ EXPECT_EQ(Directory2, Commands[1].Directory) << ErrorMessage;
+ ASSERT_EQ(1u, Commands[1].CommandLine.size());
+ EXPECT_EQ(Command2, Commands[1].CommandLine[0]) << ErrorMessage;
+}
+
static CompileCommand findCompileArgsInJsonDatabase(StringRef FileName,
StringRef JSONDatabase,
std::string &ErrorMessage) {
- llvm::OwningPtr<CompilationDatabase> Database(
+ OwningPtr<CompilationDatabase> Database(
JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage));
if (!Database)
return CompileCommand();
@@ -351,6 +391,12 @@ TEST(unescapeJsonCommandLine, ParsesQuotedStringWithoutClosingQuote) {
EXPECT_EQ("", Empty[0]);
}
+TEST(unescapeJsonCommandLine, ParsesSingleQuotedString) {
+ std::vector<std::string> Args = unescapeJsonCommandLine("a'\\\\b \\\"c\\\"'");
+ ASSERT_EQ(1ul, Args.size());
+ EXPECT_EQ("a\\b \"c\"", Args[0]);
+}
+
TEST(FixedCompilationDatabase, ReturnsFixedCommandLine) {
std::vector<std::string> CommandLine;
CommandLine.push_back("one");
@@ -376,9 +422,18 @@ TEST(FixedCompilationDatabase, GetAllFiles) {
EXPECT_EQ(0ul, Database.getAllFiles().size());
}
+TEST(FixedCompilationDatabase, GetAllCompileCommands) {
+ std::vector<std::string> CommandLine;
+ CommandLine.push_back("one");
+ CommandLine.push_back("two");
+ FixedCompilationDatabase Database(".", CommandLine);
+
+ EXPECT_EQ(0ul, Database.getAllCompileCommands().size());
+}
+
TEST(ParseFixedCompilationDatabase, ReturnsNullOnEmptyArgumentList) {
int Argc = 0;
- llvm::OwningPtr<FixedCompilationDatabase> Database(
+ OwningPtr<FixedCompilationDatabase> Database(
FixedCompilationDatabase::loadFromCommandLine(Argc, NULL));
EXPECT_FALSE(Database);
EXPECT_EQ(0, Argc);
@@ -387,7 +442,7 @@ TEST(ParseFixedCompilationDatabase, ReturnsNullOnEmptyArgumentList) {
TEST(ParseFixedCompilationDatabase, ReturnsNullWithoutDoubleDash) {
int Argc = 2;
const char *Argv[] = { "1", "2" };
- llvm::OwningPtr<FixedCompilationDatabase> Database(
+ OwningPtr<FixedCompilationDatabase> Database(
FixedCompilationDatabase::loadFromCommandLine(Argc, Argv));
EXPECT_FALSE(Database);
EXPECT_EQ(2, Argc);
@@ -396,7 +451,7 @@ TEST(ParseFixedCompilationDatabase, ReturnsNullWithoutDoubleDash) {
TEST(ParseFixedCompilationDatabase, ReturnsArgumentsAfterDoubleDash) {
int Argc = 5;
const char *Argv[] = { "1", "2", "--\0no-constant-folding", "3", "4" };
- llvm::OwningPtr<FixedCompilationDatabase> Database(
+ OwningPtr<FixedCompilationDatabase> Database(
FixedCompilationDatabase::loadFromCommandLine(Argc, Argv));
ASSERT_TRUE(Database);
std::vector<CompileCommand> Result =
@@ -415,7 +470,7 @@ TEST(ParseFixedCompilationDatabase, ReturnsArgumentsAfterDoubleDash) {
TEST(ParseFixedCompilationDatabase, ReturnsEmptyCommandLine) {
int Argc = 3;
const char *Argv[] = { "1", "2", "--\0no-constant-folding" };
- llvm::OwningPtr<FixedCompilationDatabase> Database(
+ OwningPtr<FixedCompilationDatabase> Database(
FixedCompilationDatabase::loadFromCommandLine(Argc, Argv));
ASSERT_TRUE(Database);
std::vector<CompileCommand> Result =
diff --git a/unittests/Tooling/Makefile b/unittests/Tooling/Makefile
index 5ed99fcc430be..06fdf88a2279f 100644
--- a/unittests/Tooling/Makefile
+++ b/unittests/Tooling/Makefile
@@ -10,7 +10,7 @@
CLANG_LEVEL = ../..
TESTNAME = Tooling
include $(CLANG_LEVEL)/../../Makefile.config
-LINK_COMPONENTS := $(TARGETS_TO_BUILD) asmparser support mc
+LINK_COMPONENTS := $(TARGETS_TO_BUILD) asmparser bitreader support mc
USEDLIBS = clangTooling.a clangFrontend.a clangSerialization.a clangDriver.a \
clangParse.a clangRewriteCore.a clangRewriteFrontend.a \
clangSema.a clangAnalysis.a clangEdit.a \
diff --git a/unittests/Tooling/RecursiveASTVisitorTest.cpp b/unittests/Tooling/RecursiveASTVisitorTest.cpp
index a68a869bf55dd..81be19003b2ad 100644
--- a/unittests/Tooling/RecursiveASTVisitorTest.cpp
+++ b/unittests/Tooling/RecursiveASTVisitorTest.cpp
@@ -50,10 +50,11 @@ class NamedDeclVisitor
public:
bool VisitNamedDecl(NamedDecl *Decl) {
std::string NameWithTemplateArgs;
- Decl->getNameForDiagnostic(NameWithTemplateArgs,
+ llvm::raw_string_ostream OS(NameWithTemplateArgs);
+ Decl->getNameForDiagnostic(OS,
Decl->getASTContext().getPrintingPolicy(),
true);
- Match(NameWithTemplateArgs, Decl->getLocation());
+ Match(OS.str(), Decl->getLocation());
return true;
}
};
diff --git a/unittests/Tooling/RefactoringCallbacksTest.cpp b/unittests/Tooling/RefactoringCallbacksTest.cpp
index 4e30cfde26abc..9e086d869a688 100644
--- a/unittests/Tooling/RefactoringCallbacksTest.cpp
+++ b/unittests/Tooling/RefactoringCallbacksTest.cpp
@@ -1,4 +1,4 @@
-//===- unittest/ASTMatchers/RefactoringCallbacksTest.cpp ------------------===//
+//===- unittest/Tooling/RefactoringCallbacksTest.cpp ----------------------===//
//
// The LLVM Compiler Infrastructure
//
@@ -7,10 +7,10 @@
//
//===----------------------------------------------------------------------===//
-#include "clang/ASTMatchers/ASTMatchers.h"
-#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Tooling/RefactoringCallbacks.h"
#include "RewriterTestContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
#include "gtest/gtest.h"
namespace clang {
diff --git a/unittests/Tooling/RefactoringTest.cpp b/unittests/Tooling/RefactoringTest.cpp
index ff278bfd52d57..3e0d7280b1d3b 100644
--- a/unittests/Tooling/RefactoringTest.cpp
+++ b/unittests/Tooling/RefactoringTest.cpp
@@ -8,12 +8,11 @@
//===----------------------------------------------------------------------===//
#include "RewriterTestContext.h"
-#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTConsumer.h"
+#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclGroup.h"
#include "clang/AST/RecursiveASTVisitor.h"
-#include "clang/Tooling/Refactoring.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/FileManager.h"
@@ -23,6 +22,7 @@
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Rewrite/Core/Rewriter.h"
+#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Path.h"
@@ -166,7 +166,7 @@ class FlushRewrittenFilesTest : public ::testing::Test {
}
FileID createFile(llvm::StringRef Name, llvm::StringRef Content) {
- llvm::SmallString<1024> Path(TemporaryDirectory.str());
+ SmallString<1024> Path(TemporaryDirectory.str());
llvm::sys::path::append(Path, Name);
std::string ErrorInfo;
llvm::raw_fd_ostream OutStream(Path.c_str(),
@@ -180,7 +180,7 @@ class FlushRewrittenFilesTest : public ::testing::Test {
}
std::string getFileContentFromDisk(llvm::StringRef Name) {
- llvm::SmallString<1024> Path(TemporaryDirectory.str());
+ SmallString<1024> Path(TemporaryDirectory.str());
llvm::sys::path::append(Path, Name);
// We need to read directly from the FileManager without relaying through
// a FileEntry, as otherwise we'd read through an already opened file
diff --git a/unittests/Tooling/RewriterTestContext.h b/unittests/Tooling/RewriterTestContext.h
index d790ac1035146..13c42029fc7f3 100644
--- a/unittests/Tooling/RewriterTestContext.h
+++ b/unittests/Tooling/RewriterTestContext.h
@@ -36,7 +36,7 @@ class RewriterTestContext {
public:
RewriterTestContext()
: DiagOpts(new DiagnosticOptions()),
- Diagnostics(llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
+ Diagnostics(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
&*DiagOpts),
DiagnosticPrinter(llvm::outs(), &*DiagOpts),
Files((FileSystemOptions())),
@@ -72,7 +72,7 @@ class RewriterTestContext {
llvm::raw_fd_ostream Closer(FD, /*shouldClose=*/true);
TemporaryDirectory = llvm::sys::path::parent_path(TemporaryDirectory);
}
- llvm::SmallString<1024> Path(TemporaryDirectory);
+ SmallString<1024> Path(TemporaryDirectory);
llvm::sys::path::append(Path, Name);
std::string ErrorInfo;
llvm::raw_fd_ostream OutStream(Path.c_str(),
@@ -101,7 +101,7 @@ class RewriterTestContext {
}
std::string getFileContentFromDisk(StringRef Name) {
- llvm::SmallString<1024> Path(TemporaryDirectory.str());
+ SmallString<1024> Path(TemporaryDirectory.str());
llvm::sys::path::append(Path, Name);
// We need to read directly from the FileManager without relaying through
// a FileEntry, as otherwise we'd read through an already opened file
@@ -111,7 +111,7 @@ class RewriterTestContext {
return Files.getBufferForFile(Path, NULL)->getBuffer();
}
- llvm::IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
+ IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
DiagnosticsEngine Diagnostics;
TextDiagnosticPrinter DiagnosticPrinter;
FileManager Files;
diff --git a/unittests/Tooling/TestVisitor.h b/unittests/Tooling/TestVisitor.h
index 8333c24a68807..ce3246a902f13 100644
--- a/unittests/Tooling/TestVisitor.h
+++ b/unittests/Tooling/TestVisitor.h
@@ -15,15 +15,14 @@
#ifndef LLVM_CLANG_TEST_VISITOR_H
#define LLVM_CLANG_TEST_VISITOR_H
-#include <vector>
-
-#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTConsumer.h"
+#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
-#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/Tooling.h"
#include "gtest/gtest.h"
+#include <vector>
namespace clang {
diff --git a/unittests/Tooling/ToolingTest.cpp b/unittests/Tooling/ToolingTest.cpp
index d40c613dd0592..a9319f2961207 100644
--- a/unittests/Tooling/ToolingTest.cpp
+++ b/unittests/Tooling/ToolingTest.cpp
@@ -10,6 +10,7 @@
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclGroup.h"
+#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CompilationDatabase.h"
@@ -97,9 +98,9 @@ TEST(runToolOnCode, FindsClassDecl) {
}
TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) {
- llvm::OwningPtr<FrontendActionFactory> Factory(
- newFrontendActionFactory<SyntaxOnlyAction>());
- llvm::OwningPtr<FrontendAction> Action(Factory->create());
+ OwningPtr<FrontendActionFactory> Factory(
+ newFrontendActionFactory<SyntaxOnlyAction>());
+ OwningPtr<FrontendAction> Action(Factory->create());
EXPECT_TRUE(Action.get() != NULL);
}
@@ -111,9 +112,9 @@ struct IndependentFrontendActionCreator {
TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromFactoryType) {
IndependentFrontendActionCreator Creator;
- llvm::OwningPtr<FrontendActionFactory> Factory(
- newFrontendActionFactory(&Creator));
- llvm::OwningPtr<FrontendAction> Action(Factory->create());
+ OwningPtr<FrontendActionFactory> Factory(
+ newFrontendActionFactory(&Creator));
+ OwningPtr<FrontendAction> Action(Factory->create());
EXPECT_TRUE(Action.get() != NULL);
}
@@ -162,5 +163,28 @@ TEST(newFrontendActionFactory, InjectsEndOfSourceFileCallback) {
}
#endif
+struct SkipBodyConsumer : public clang::ASTConsumer {
+ /// Skip the 'skipMe' function.
+ virtual bool shouldSkipFunctionBody(Decl *D) {
+ FunctionDecl *F = dyn_cast<FunctionDecl>(D);
+ return F && F->getNameAsString() == "skipMe";
+ }
+};
+
+struct SkipBodyAction : public clang::ASTFrontendAction {
+ virtual ASTConsumer *CreateASTConsumer(CompilerInstance &Compiler,
+ StringRef) {
+ Compiler.getFrontendOpts().SkipFunctionBodies = true;
+ return new SkipBodyConsumer;
+ }
+};
+
+TEST(runToolOnCode, TestSkipFunctionBody) {
+ EXPECT_TRUE(runToolOnCode(new SkipBodyAction,
+ "int skipMe() { an_error_here }"));
+ EXPECT_FALSE(runToolOnCode(new SkipBodyAction,
+ "int skipMeNot() { an_error_here }"));
+}
+
} // end namespace tooling
} // end namespace clang