summaryrefslogtreecommitdiffstats
path: root/unittests/Tooling
diff options
context:
space:
mode:
Diffstat (limited to 'unittests/Tooling')
-rw-r--r--unittests/Tooling/CMakeLists.txt2
-rw-r--r--unittests/Tooling/DiagnosticsYamlTest.cpp58
-rw-r--r--unittests/Tooling/ExecutionTest.cpp7
-rw-r--r--unittests/Tooling/RecursiveASTVisitorTests/TraversalScope.cpp51
-rw-r--r--unittests/Tooling/RefactoringActionRulesTest.cpp4
-rw-r--r--unittests/Tooling/RefactoringTest.cpp16
-rw-r--r--unittests/Tooling/ReplacementsYamlTest.cpp6
-rw-r--r--unittests/Tooling/RewriterTestContext.h16
-rw-r--r--unittests/Tooling/TestVisitor.h4
-rw-r--r--unittests/Tooling/ToolingTest.cpp24
10 files changed, 139 insertions, 49 deletions
diff --git a/unittests/Tooling/CMakeLists.txt b/unittests/Tooling/CMakeLists.txt
index e9910fb87a..7619c7fb23 100644
--- a/unittests/Tooling/CMakeLists.txt
+++ b/unittests/Tooling/CMakeLists.txt
@@ -40,6 +40,7 @@ add_clang_unittest(ToolingTests
RecursiveASTVisitorTests/NestedNameSpecifiers.cpp
RecursiveASTVisitorTests/ParenExpr.cpp
RecursiveASTVisitorTests/TemplateArgumentLocTraverser.cpp
+ RecursiveASTVisitorTests/TraversalScope.cpp
RecursiveASTVisitorTestDeclVisitor.cpp
RecursiveASTVisitorTestPostOrderVisitor.cpp
RecursiveASTVisitorTestTypeLocVisitor.cpp
@@ -60,6 +61,7 @@ target_link_libraries(ToolingTests
clangFrontend
clangLex
clangRewrite
+ clangSerialization
clangTooling
clangToolingCore
clangToolingInclusions
diff --git a/unittests/Tooling/DiagnosticsYamlTest.cpp b/unittests/Tooling/DiagnosticsYamlTest.cpp
index 83e09eaeef..f4de53fad2 100644
--- a/unittests/Tooling/DiagnosticsYamlTest.cpp
+++ b/unittests/Tooling/DiagnosticsYamlTest.cpp
@@ -20,16 +20,21 @@ using namespace llvm;
using namespace clang::tooling;
using clang::tooling::Diagnostic;
-static Diagnostic makeDiagnostic(StringRef DiagnosticName,
- const std::string &Message, int FileOffset,
- const std::string &FilePath,
- const StringMap<Replacements> &Fix) {
+static DiagnosticMessage makeMessage(const std::string &Message, int FileOffset,
+ const std::string &FilePath) {
DiagnosticMessage DiagMessage;
DiagMessage.Message = Message;
DiagMessage.FileOffset = FileOffset;
DiagMessage.FilePath = FilePath;
- return Diagnostic(DiagnosticName, DiagMessage, Fix, {}, Diagnostic::Warning,
- "path/to/build/directory");
+ return DiagMessage;
+}
+
+static Diagnostic makeDiagnostic(StringRef DiagnosticName,
+ const std::string &Message, int FileOffset,
+ const std::string &FilePath,
+ const StringMap<Replacements> &Fix) {
+ return Diagnostic(DiagnosticName, makeMessage(Message, FileOffset, FilePath),
+ Fix, {}, Diagnostic::Warning, "path/to/build/directory");
}
TEST(DiagnosticsYamlTest, serializesDiagnostics) {
@@ -50,6 +55,10 @@ TEST(DiagnosticsYamlTest, serializesDiagnostics) {
TUD.Diagnostics.push_back(makeDiagnostic("diagnostic#3", "message #3", 72,
"path/to/source2.cpp", {}));
+ TUD.Diagnostics.back().Notes.push_back(
+ makeMessage("Note1", 88, "path/to/note1.cpp"));
+ TUD.Diagnostics.back().Notes.push_back(
+ makeMessage("Note2", 99, "path/to/note2.cpp"));
std::string YamlContent;
raw_string_ostream YamlContentStream(YamlContent);
@@ -58,31 +67,38 @@ TEST(DiagnosticsYamlTest, serializesDiagnostics) {
YAML << TUD;
EXPECT_EQ("---\n"
- "MainSourceFile: path/to/source.cpp\n"
+ "MainSourceFile: 'path/to/source.cpp'\n"
"Diagnostics: \n"
" - DiagnosticName: 'diagnostic#1\'\n"
" Message: 'message #1'\n"
" FileOffset: 55\n"
- " FilePath: path/to/source.cpp\n"
+ " FilePath: 'path/to/source.cpp'\n"
" Replacements: \n"
- " - FilePath: path/to/source.cpp\n"
+ " - FilePath: 'path/to/source.cpp'\n"
" Offset: 100\n"
" Length: 12\n"
" ReplacementText: 'replacement #1'\n"
" - DiagnosticName: 'diagnostic#2'\n"
" Message: 'message #2'\n"
" FileOffset: 60\n"
- " FilePath: path/to/header.h\n"
+ " FilePath: 'path/to/header.h'\n"
" Replacements: \n"
- " - FilePath: path/to/header.h\n"
+ " - FilePath: 'path/to/header.h'\n"
" Offset: 62\n"
" Length: 2\n"
" ReplacementText: 'replacement #2'\n"
" - DiagnosticName: 'diagnostic#3'\n"
" Message: 'message #3'\n"
" FileOffset: 72\n"
- " FilePath: path/to/source2.cpp\n"
- " Replacements: \n"
+ " FilePath: 'path/to/source2.cpp'\n"
+ " Notes: \n"
+ " - Message: Note1\n"
+ " FilePath: 'path/to/note1.cpp'\n"
+ " FileOffset: 88\n"
+ " - Message: Note2\n"
+ " FilePath: 'path/to/note2.cpp'\n"
+ " FileOffset: 99\n"
+ " Replacements: []\n"
"...\n",
YamlContentStream.str());
}
@@ -113,7 +129,14 @@ TEST(DiagnosticsYamlTest, deserializesDiagnostics) {
" Message: 'message #3'\n"
" FileOffset: 98\n"
" FilePath: path/to/source.cpp\n"
- " Replacements: \n"
+ " Notes:\n"
+ " - Message: Note1\n"
+ " FilePath: 'path/to/note1.cpp'\n"
+ " FileOffset: 66\n"
+ " - Message: Note2\n"
+ " FilePath: 'path/to/note2.cpp'\n"
+ " FileOffset: 77\n"
+ " Replacements: []\n"
"...\n";
TranslationUnitDiagnostics TUDActual;
yaml::Input YAML(YamlContent);
@@ -162,6 +185,13 @@ TEST(DiagnosticsYamlTest, deserializesDiagnostics) {
EXPECT_EQ("message #3", D3.Message.Message);
EXPECT_EQ(98u, D3.Message.FileOffset);
EXPECT_EQ("path/to/source.cpp", D3.Message.FilePath);
+ EXPECT_EQ(2u, D3.Notes.size());
+ EXPECT_EQ("Note1", D3.Notes[0].Message);
+ EXPECT_EQ(66u, D3.Notes[0].FileOffset);
+ EXPECT_EQ("path/to/note1.cpp", D3.Notes[0].FilePath);
+ EXPECT_EQ("Note2", D3.Notes[1].Message);
+ EXPECT_EQ(77u, D3.Notes[1].FileOffset);
+ EXPECT_EQ("path/to/note2.cpp", D3.Notes[1].FilePath);
std::vector<Replacement> Fixes3 = getFixes(D3.Fix);
EXPECT_TRUE(Fixes3.empty());
}
diff --git a/unittests/Tooling/ExecutionTest.cpp b/unittests/Tooling/ExecutionTest.cpp
index e5dc98d228..785ec7c2bc 100644
--- a/unittests/Tooling/ExecutionTest.cpp
+++ b/unittests/Tooling/ExecutionTest.cpp
@@ -248,12 +248,14 @@ private:
MATCHER_P(Named, Name, "") { return arg.first == Name; }
TEST(AllTUsToolTest, AFewFiles) {
- FixedCompilationDatabaseWithFiles Compilations(".", {"a.cc", "b.cc", "c.cc"},
- std::vector<std::string>());
+ FixedCompilationDatabaseWithFiles Compilations(
+ ".", {"a.cc", "b.cc", "c.cc", "ignore.cc"}, std::vector<std::string>());
AllTUsToolExecutor Executor(Compilations, /*ThreadCount=*/0);
+ Filter.setValue("[a-c].cc");
Executor.mapVirtualFile("a.cc", "void x() {}");
Executor.mapVirtualFile("b.cc", "void y() {}");
Executor.mapVirtualFile("c.cc", "void z() {}");
+ Executor.mapVirtualFile("ignore.cc", "void d() {}");
auto Err = Executor.execute(std::unique_ptr<FrontendActionFactory>(
new ReportResultActionFactory(Executor.getExecutionContext())));
@@ -261,6 +263,7 @@ TEST(AllTUsToolTest, AFewFiles) {
EXPECT_THAT(
Executor.getToolResults()->AllKVResults(),
::testing::UnorderedElementsAre(Named("x"), Named("y"), Named("z")));
+ Filter.setValue(".*"); // reset to default value.
}
TEST(AllTUsToolTest, ManyFiles) {
diff --git a/unittests/Tooling/RecursiveASTVisitorTests/TraversalScope.cpp b/unittests/Tooling/RecursiveASTVisitorTests/TraversalScope.cpp
new file mode 100644
index 0000000000..72f6c644b3
--- /dev/null
+++ b/unittests/Tooling/RecursiveASTVisitorTests/TraversalScope.cpp
@@ -0,0 +1,51 @@
+//===- unittest/Tooling/RecursiveASTVisitorTests/TraversalScope.cpp -------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "TestVisitor.h"
+
+using namespace clang;
+
+namespace {
+
+class Visitor : public ExpectedLocationVisitor<Visitor, clang::TestVisitor> {
+public:
+ Visitor(ASTContext *Context) { this->Context = Context; }
+
+ bool VisitNamedDecl(NamedDecl *D) {
+ if (!D->isImplicit())
+ Match(D->getName(), D->getLocation());
+ return true;
+ }
+};
+
+TEST(RecursiveASTVisitor, RespectsTraversalScope) {
+ auto AST = tooling::buildASTFromCode(
+ R"cpp(
+struct foo {
+ struct bar {
+ struct baz {};
+ };
+};
+ )cpp",
+ "foo.cpp", std::make_shared<PCHContainerOperations>());
+ auto &Ctx = AST->getASTContext();
+ auto &TU = *Ctx.getTranslationUnitDecl();
+ auto &Foo = *TU.lookup(&Ctx.Idents.get("foo")).front();
+ auto &Bar = *cast<DeclContext>(Foo).lookup(&Ctx.Idents.get("bar")).front();
+
+ Ctx.setTraversalScope({&Bar});
+
+ Visitor V(&Ctx);
+ V.DisallowMatch("foo", 2, 8);
+ V.ExpectMatch("bar", 3, 10);
+ V.ExpectMatch("baz", 4, 12);
+ V.TraverseAST(Ctx);
+}
+
+} // end anonymous namespace
diff --git a/unittests/Tooling/RefactoringActionRulesTest.cpp b/unittests/Tooling/RefactoringActionRulesTest.cpp
index e9a12deb3f..acacfa05b4 100644
--- a/unittests/Tooling/RefactoringActionRulesTest.cpp
+++ b/unittests/Tooling/RefactoringActionRulesTest.cpp
@@ -117,8 +117,8 @@ TEST_F(RefactoringActionRulesTest, MyFirstRefactoringRule) {
"Key: 'input.cpp:30'\n"
"FilePath: input.cpp\n"
"Error: ''\n"
- "InsertedHeaders: \n"
- "RemovedHeaders: \n"
+ "InsertedHeaders: []\n"
+ "RemovedHeaders: []\n"
"Replacements: \n" // Extra whitespace here!
" - FilePath: input.cpp\n"
" Offset: 30\n"
diff --git a/unittests/Tooling/RefactoringTest.cpp b/unittests/Tooling/RefactoringTest.cpp
index fcd2aa5937..d618c0fb07 100644
--- a/unittests/Tooling/RefactoringTest.cpp
+++ b/unittests/Tooling/RefactoringTest.cpp
@@ -7,6 +7,7 @@
//
//===----------------------------------------------------------------------===//
+#include "clang/Tooling/Refactoring.h"
#include "ReplacementTest.h"
#include "RewriterTestContext.h"
#include "clang/AST/ASTConsumer.h"
@@ -19,16 +20,15 @@
#include "clang/Basic/FileManager.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceManager.h"
-#include "clang/Basic/VirtualFileSystem.h"
#include "clang/Format/Format.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Rewrite/Core/Rewriter.h"
-#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/Refactoring/AtomicChange.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/VirtualFileSystem.h"
#include "gtest/gtest.h"
namespace clang {
@@ -1032,8 +1032,8 @@ TEST_F(MergeReplacementsTest, OverlappingRanges) {
TEST(DeduplicateByFileTest, PathsWithDots) {
std::map<std::string, Replacements> FileToReplaces;
- llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> VFS(
- new vfs::InMemoryFileSystem());
+ llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS(
+ new llvm::vfs::InMemoryFileSystem());
FileManager FileMgr(FileSystemOptions(), VFS);
#if !defined(_WIN32)
StringRef Path1 = "a/b/.././c.h";
@@ -1053,8 +1053,8 @@ TEST(DeduplicateByFileTest, PathsWithDots) {
TEST(DeduplicateByFileTest, PathWithDotSlash) {
std::map<std::string, Replacements> FileToReplaces;
- llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> VFS(
- new vfs::InMemoryFileSystem());
+ llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS(
+ new llvm::vfs::InMemoryFileSystem());
FileManager FileMgr(FileSystemOptions(), VFS);
#if !defined(_WIN32)
StringRef Path1 = "./a/b/c.h";
@@ -1074,8 +1074,8 @@ TEST(DeduplicateByFileTest, PathWithDotSlash) {
TEST(DeduplicateByFileTest, NonExistingFilePath) {
std::map<std::string, Replacements> FileToReplaces;
- llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> VFS(
- new vfs::InMemoryFileSystem());
+ llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS(
+ new llvm::vfs::InMemoryFileSystem());
FileManager FileMgr(FileSystemOptions(), VFS);
#if !defined(_WIN32)
StringRef Path1 = "./a/b/c.h";
diff --git a/unittests/Tooling/ReplacementsYamlTest.cpp b/unittests/Tooling/ReplacementsYamlTest.cpp
index 3e4193d2ad..2e5a87a931 100644
--- a/unittests/Tooling/ReplacementsYamlTest.cpp
+++ b/unittests/Tooling/ReplacementsYamlTest.cpp
@@ -33,13 +33,13 @@ TEST(ReplacementsYamlTest, serializesReplacements) {
// NOTE: If this test starts to fail for no obvious reason, check whitespace.
ASSERT_STREQ("---\n"
- "MainSourceFile: /path/to/source.cpp\n"
+ "MainSourceFile: '/path/to/source.cpp'\n"
"Replacements: \n" // Extra whitespace here!
- " - FilePath: /path/to/file1.h\n"
+ " - FilePath: '/path/to/file1.h'\n"
" Offset: 232\n"
" Length: 56\n"
" ReplacementText: 'replacement #1'\n"
- " - FilePath: /path/to/file2.h\n"
+ " - FilePath: '/path/to/file2.h'\n"
" Offset: 301\n"
" Length: 2\n"
" ReplacementText: 'replacement #2'\n"
diff --git a/unittests/Tooling/RewriterTestContext.h b/unittests/Tooling/RewriterTestContext.h
index eee7ea1873..9e66484158 100644
--- a/unittests/Tooling/RewriterTestContext.h
+++ b/unittests/Tooling/RewriterTestContext.h
@@ -39,15 +39,15 @@ class RewriterTestContext {
Diagnostics(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
&*DiagOpts),
DiagnosticPrinter(llvm::outs(), &*DiagOpts),
- InMemoryFileSystem(new vfs::InMemoryFileSystem),
+ InMemoryFileSystem(new llvm::vfs::InMemoryFileSystem),
OverlayFileSystem(
- new vfs::OverlayFileSystem(vfs::getRealFileSystem())),
+ new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem())),
Files(FileSystemOptions(), OverlayFileSystem),
Sources(Diagnostics, Files), Rewrite(Sources, Options) {
- Diagnostics.setClient(&DiagnosticPrinter, false);
- // FIXME: To make these tests truly in-memory, we need to overlay the
- // builtin headers.
- OverlayFileSystem->pushOverlay(InMemoryFileSystem);
+ Diagnostics.setClient(&DiagnosticPrinter, false);
+ // FIXME: To make these tests truly in-memory, we need to overlay the
+ // builtin headers.
+ OverlayFileSystem->pushOverlay(InMemoryFileSystem);
}
~RewriterTestContext() {}
@@ -114,8 +114,8 @@ class RewriterTestContext {
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
DiagnosticsEngine Diagnostics;
TextDiagnosticPrinter DiagnosticPrinter;
- IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem;
- IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem;
+ IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem;
+ IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem;
FileManager Files;
SourceManager Sources;
LangOptions Options;
diff --git a/unittests/Tooling/TestVisitor.h b/unittests/Tooling/TestVisitor.h
index fb6a76ccad..1a22ae737b 100644
--- a/unittests/Tooling/TestVisitor.h
+++ b/unittests/Tooling/TestVisitor.h
@@ -44,6 +44,8 @@ public:
Lang_CXX98,
Lang_CXX11,
Lang_CXX14,
+ Lang_CXX17,
+ Lang_CXX2a,
Lang_OBJC,
Lang_OBJCXX11,
Lang_CXX = Lang_CXX98
@@ -60,6 +62,8 @@ public:
case Lang_CXX98: Args.push_back("-std=c++98"); break;
case Lang_CXX11: Args.push_back("-std=c++11"); break;
case Lang_CXX14: Args.push_back("-std=c++14"); break;
+ case Lang_CXX17: Args.push_back("-std=c++17"); break;
+ case Lang_CXX2a: Args.push_back("-std=c++2a"); break;
case Lang_OBJC:
Args.push_back("-ObjC");
Args.push_back("-fobjc-runtime=macosx-10.12.0");
diff --git a/unittests/Tooling/ToolingTest.cpp b/unittests/Tooling/ToolingTest.cpp
index 0a5150a31b..186463f80a 100644
--- a/unittests/Tooling/ToolingTest.cpp
+++ b/unittests/Tooling/ToolingTest.cpp
@@ -149,10 +149,10 @@ TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromFactoryType) {
}
TEST(ToolInvocation, TestMapVirtualFile) {
- llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem(
- new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
- llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
- new vfs::InMemoryFileSystem);
+ llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
+ new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
+ llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
+ new llvm::vfs::InMemoryFileSystem);
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
llvm::IntrusiveRefCntPtr<FileManager> Files(
new FileManager(FileSystemOptions(), OverlayFileSystem));
@@ -175,10 +175,10 @@ TEST(ToolInvocation, TestVirtualModulesCompilation) {
// mapped module.map is found on the include path. In the future, expand this
// test to run a full modules enabled compilation, so we make sure we can
// rerun modules compilations with a virtual file system.
- llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem(
- new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
- llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
- new vfs::InMemoryFileSystem);
+ llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
+ new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
+ llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
+ new llvm::vfs::InMemoryFileSystem);
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
llvm::IntrusiveRefCntPtr<FileManager> Files(
new FileManager(FileSystemOptions(), OverlayFileSystem));
@@ -403,10 +403,10 @@ TEST(ClangToolTest, ArgumentAdjusters) {
TEST(ClangToolTest, BaseVirtualFileSystemUsage) {
FixedCompilationDatabase Compilations("/", std::vector<std::string>());
- llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem(
- new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
- llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
- new vfs::InMemoryFileSystem);
+ llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
+ new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
+ llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
+ new llvm::vfs::InMemoryFileSystem);
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
InMemoryFileSystem->addFile(