2012-05-23 01:01:35 +08:00
|
|
|
//===--- RewriterTestContext.h ----------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a utility class for Rewriter related tests.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#ifndef LLVM_CLANG_UNITTESTS_TOOLING_REWRITERTESTCONTEXT_H
|
|
|
|
#define LLVM_CLANG_UNITTESTS_TOOLING_REWRITERTESTCONTEXT_H
|
2012-05-23 01:01:35 +08:00
|
|
|
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2012-10-24 06:55:10 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2012-05-23 01:01:35 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/LangOptions.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
2012-09-01 13:09:24 +08:00
|
|
|
#include "clang/Rewrite/Core/Rewriter.h"
|
2012-06-07 05:28:13 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-05-23 01:01:35 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
|
|
|
/// \brief A class that sets up a ready to use Rewriter.
|
|
|
|
///
|
|
|
|
/// Useful in unit tests that need a Rewriter. Creates all dependencies
|
|
|
|
/// of a Rewriter with default values for testing and provides convenience
|
|
|
|
/// methods, which help with writing tests that change files.
|
|
|
|
class RewriterTestContext {
|
|
|
|
public:
|
2015-10-05 21:55:14 +08:00
|
|
|
RewriterTestContext()
|
|
|
|
: DiagOpts(new DiagnosticOptions()),
|
|
|
|
Diagnostics(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
|
|
|
|
&*DiagOpts),
|
|
|
|
DiagnosticPrinter(llvm::outs(), &*DiagOpts),
|
|
|
|
InMemoryFileSystem(new vfs::InMemoryFileSystem),
|
|
|
|
OverlayFileSystem(
|
|
|
|
new vfs::OverlayFileSystem(vfs::getRealFileSystem())),
|
|
|
|
Files(FileSystemOptions(), OverlayFileSystem),
|
|
|
|
Sources(Diagnostics, Files), Rewrite(Sources, Options) {
|
2012-05-23 01:01:35 +08:00
|
|
|
Diagnostics.setClient(&DiagnosticPrinter, false);
|
2015-10-05 21:55:14 +08:00
|
|
|
// FIXME: To make these tests truly in-memory, we need to overlay the
|
|
|
|
// builtin headers.
|
|
|
|
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
|
2012-05-23 01:01:35 +08:00
|
|
|
}
|
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
~RewriterTestContext() {}
|
2012-05-23 01:01:35 +08:00
|
|
|
|
|
|
|
FileID createInMemoryFile(StringRef Name, StringRef Content) {
|
2014-08-28 04:03:29 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Source =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(Content);
|
2015-10-05 21:55:14 +08:00
|
|
|
InMemoryFileSystem->addFile(Name, 0, std::move(Source));
|
|
|
|
|
|
|
|
const FileEntry *Entry = Files.getFile(Name);
|
2014-06-08 16:38:12 +08:00
|
|
|
assert(Entry != nullptr);
|
2012-05-23 01:01:35 +08:00
|
|
|
return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
|
|
|
|
}
|
|
|
|
|
2013-06-27 05:02:22 +08:00
|
|
|
// FIXME: this code is mostly a duplicate of
|
|
|
|
// unittests/Tooling/RefactoringTest.cpp. Figure out a way to share it.
|
2012-05-23 01:01:35 +08:00
|
|
|
FileID createOnDiskFile(StringRef Name, StringRef Content) {
|
2013-06-27 05:02:22 +08:00
|
|
|
SmallString<1024> Path;
|
|
|
|
int FD;
|
2014-06-12 22:02:15 +08:00
|
|
|
std::error_code EC = llvm::sys::fs::createTemporaryFile(Name, "", FD, Path);
|
2013-06-27 05:02:22 +08:00
|
|
|
assert(!EC);
|
|
|
|
(void)EC;
|
|
|
|
|
|
|
|
llvm::raw_fd_ostream OutStream(FD, true);
|
2012-05-23 01:01:35 +08:00
|
|
|
OutStream << Content;
|
|
|
|
OutStream.close();
|
|
|
|
const FileEntry *File = Files.getFile(Path);
|
2014-06-08 16:38:12 +08:00
|
|
|
assert(File != nullptr);
|
2013-06-27 05:02:22 +08:00
|
|
|
|
2014-11-19 11:06:06 +08:00
|
|
|
StringRef Found =
|
|
|
|
TemporaryFiles.insert(std::make_pair(Name, Path.str())).first->second;
|
2013-06-27 05:02:22 +08:00
|
|
|
assert(Found == Path);
|
|
|
|
(void)Found;
|
2012-05-23 01:01:35 +08:00
|
|
|
return Sources.createFileID(File, SourceLocation(), SrcMgr::C_User);
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation getLocation(FileID ID, unsigned Line, unsigned Column) {
|
|
|
|
SourceLocation Result = Sources.translateFileLineCol(
|
|
|
|
Sources.getFileEntryForID(ID), Line, Column);
|
|
|
|
assert(Result.isValid());
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getRewrittenText(FileID ID) {
|
|
|
|
std::string Result;
|
|
|
|
llvm::raw_string_ostream OS(Result);
|
|
|
|
Rewrite.getEditBuffer(ID).write(OS);
|
2012-06-06 04:16:30 +08:00
|
|
|
OS.flush();
|
2012-05-23 01:01:35 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getFileContentFromDisk(StringRef Name) {
|
2013-06-27 05:02:22 +08:00
|
|
|
std::string Path = TemporaryFiles.lookup(Name);
|
|
|
|
assert(!Path.empty());
|
2012-05-23 01:01:35 +08:00
|
|
|
// We need to read directly from the FileManager without relaying through
|
|
|
|
// a FileEntry, as otherwise we'd read through an already opened file
|
|
|
|
// descriptor, which might not see the changes made.
|
|
|
|
// FIXME: Figure out whether there is a way to get the SourceManger to
|
|
|
|
// reopen the file.
|
2014-10-27 06:44:13 +08:00
|
|
|
auto FileBuffer = Files.getBufferForFile(Path);
|
|
|
|
return (*FileBuffer)->getBuffer();
|
2012-05-23 01:01:35 +08:00
|
|
|
}
|
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
|
2012-05-23 01:01:35 +08:00
|
|
|
DiagnosticsEngine Diagnostics;
|
|
|
|
TextDiagnosticPrinter DiagnosticPrinter;
|
2015-10-05 21:55:14 +08:00
|
|
|
IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem;
|
|
|
|
IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem;
|
2012-05-23 01:01:35 +08:00
|
|
|
FileManager Files;
|
|
|
|
SourceManager Sources;
|
|
|
|
LangOptions Options;
|
|
|
|
Rewriter Rewrite;
|
|
|
|
|
2012-06-07 05:28:13 +08:00
|
|
|
// Will be set once on disk files are generated.
|
2013-06-27 05:02:22 +08:00
|
|
|
llvm::StringMap<std::string> TemporaryFiles;
|
2012-05-23 01:01:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
#endif
|