clang-tools 22.0.0git
TweakTests.cpp
Go to the documentation of this file.
1//===-- TweakTests.cpp ------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "TestFS.h"
10#include "refactor/Tweak.h"
11#include "clang/Basic/Diagnostic.h"
12#include "clang/Basic/DiagnosticIDs.h"
13#include "clang/Basic/DiagnosticOptions.h"
14#include "clang/Basic/FileManager.h"
15#include "clang/Basic/FileSystemOptions.h"
16#include "clang/Basic/LLVM.h"
17#include "clang/Basic/SourceLocation.h"
18#include "clang/Basic/SourceManager.h"
19#include "clang/Tooling/Core/Replacement.h"
20#include "llvm/ADT/IntrusiveRefCntPtr.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/Error.h"
23#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/VirtualFileSystem.h"
25#include "llvm/Testing/Support/Error.h"
26#include "gmock/gmock-matchers.h"
27#include "gmock/gmock.h"
28#include "gtest/gtest.h"
29#include <cassert>
30#include <string>
31#include <utility>
32#include <vector>
33
34namespace clang {
35namespace clangd {
36namespace {
37
38TEST(FileEdits, AbsolutePath) {
39 auto RelPaths = {"a.h", "foo.cpp", "test/test.cpp"};
40
41 llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemFS(
42 new llvm::vfs::InMemoryFileSystem);
43 MemFS->setCurrentWorkingDirectory(testRoot());
44 for (const auto *Path : RelPaths)
45 MemFS->addFile(Path, 0, llvm::MemoryBuffer::getMemBuffer("", Path));
46 FileManager FM(FileSystemOptions(), MemFS);
47 DiagnosticOptions DiagOpts;
48 DiagnosticsEngine DE(DiagnosticIDs::create(), DiagOpts);
49 SourceManager SM(DE, FM);
50
51 for (const auto *Path : RelPaths) {
52 auto FID = SM.createFileID(*FM.getOptionalFileRef(Path), SourceLocation(),
53 clang::SrcMgr::C_User);
54 auto Res = Tweak::Effect::fileEdit(SM, FID, tooling::Replacements());
55 ASSERT_THAT_EXPECTED(Res, llvm::Succeeded());
56 EXPECT_EQ(Res->first, testPath(Path));
57 }
58}
59
60} // namespace
61} // namespace clangd
62} // namespace clang
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:45
std::string testPath(PathRef File, llvm::sys::path::Style Style)
Definition TestFS.cpp:93
llvm::StringMap< Edit > FileEdits
A mapping from absolute file path (the one used for accessing the underlying VFS) to edits.
Definition SourceCode.h:209
TEST(BackgroundQueueTest, Priority)
std::string Path
A typedef to represent a file path.
Definition Path.h:26
const char * testRoot()
Definition TestFS.cpp:85
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static llvm::Expected< std::pair< Path, Edit > > fileEdit(const SourceManager &SM, FileID FID, tooling::Replacements Replacements)
Path is the absolute, symlink-resolved path for the file pointed by FID in SM.
Definition Tweak.cpp:104