Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 1 | //===--- RewriterTestContext.h ----------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a utility class for Rewriter related tests. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 14 | #ifndef LLVM_CLANG_UNITTESTS_TOOLING_REWRITERTESTCONTEXT_H |
| 15 | #define LLVM_CLANG_UNITTESTS_TOOLING_REWRITERTESTCONTEXT_H |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 16 | |
| 17 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 275e883 | 2012-10-23 22:55:10 +0000 | [diff] [blame] | 18 | #include "clang/Basic/DiagnosticOptions.h" |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 19 | #include "clang/Basic/FileManager.h" |
| 20 | #include "clang/Basic/LangOptions.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Ted Kremenek | cdf8149 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 23 | #include "clang/Rewrite/Core/Rewriter.h" |
Manuel Klimek | 0a8b9cd | 2012-06-06 21:28:13 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Path.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | |
| 28 | namespace clang { |
| 29 | |
| 30 | /// \brief A class that sets up a ready to use Rewriter. |
| 31 | /// |
| 32 | /// Useful in unit tests that need a Rewriter. Creates all dependencies |
| 33 | /// of a Rewriter with default values for testing and provides convenience |
| 34 | /// methods, which help with writing tests that change files. |
| 35 | class RewriterTestContext { |
| 36 | public: |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 37 | RewriterTestContext() |
| 38 | : DiagOpts(new DiagnosticOptions()), |
| 39 | Diagnostics(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 40 | &*DiagOpts), |
| 41 | DiagnosticPrinter(llvm::outs(), &*DiagOpts), |
| 42 | InMemoryFileSystem(new vfs::InMemoryFileSystem), |
| 43 | OverlayFileSystem( |
| 44 | new vfs::OverlayFileSystem(vfs::getRealFileSystem())), |
| 45 | Files(FileSystemOptions(), OverlayFileSystem), |
| 46 | Sources(Diagnostics, Files), Rewrite(Sources, Options) { |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 47 | Diagnostics.setClient(&DiagnosticPrinter, false); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 48 | // FIXME: To make these tests truly in-memory, we need to overlay the |
| 49 | // builtin headers. |
| 50 | OverlayFileSystem->pushOverlay(InMemoryFileSystem); |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 53 | ~RewriterTestContext() {} |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 54 | |
| 55 | FileID createInMemoryFile(StringRef Name, StringRef Content) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 56 | std::unique_ptr<llvm::MemoryBuffer> Source = |
| 57 | llvm::MemoryBuffer::getMemBuffer(Content); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 58 | InMemoryFileSystem->addFile(Name, 0, std::move(Source)); |
| 59 | |
| 60 | const FileEntry *Entry = Files.getFile(Name); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 61 | assert(Entry != nullptr); |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 62 | return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User); |
| 63 | } |
| 64 | |
Rafael Espindola | 8b96094 | 2013-06-26 21:02:22 +0000 | [diff] [blame] | 65 | // FIXME: this code is mostly a duplicate of |
| 66 | // unittests/Tooling/RefactoringTest.cpp. Figure out a way to share it. |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 67 | FileID createOnDiskFile(StringRef Name, StringRef Content) { |
Rafael Espindola | 8b96094 | 2013-06-26 21:02:22 +0000 | [diff] [blame] | 68 | SmallString<1024> Path; |
| 69 | int FD; |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 70 | std::error_code EC = llvm::sys::fs::createTemporaryFile(Name, "", FD, Path); |
Rafael Espindola | 8b96094 | 2013-06-26 21:02:22 +0000 | [diff] [blame] | 71 | assert(!EC); |
| 72 | (void)EC; |
| 73 | |
| 74 | llvm::raw_fd_ostream OutStream(FD, true); |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 75 | OutStream << Content; |
| 76 | OutStream.close(); |
| 77 | const FileEntry *File = Files.getFile(Path); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 78 | assert(File != nullptr); |
Rafael Espindola | 8b96094 | 2013-06-26 21:02:22 +0000 | [diff] [blame] | 79 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 80 | StringRef Found = |
| 81 | TemporaryFiles.insert(std::make_pair(Name, Path.str())).first->second; |
Rafael Espindola | 8b96094 | 2013-06-26 21:02:22 +0000 | [diff] [blame] | 82 | assert(Found == Path); |
| 83 | (void)Found; |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 84 | return Sources.createFileID(File, SourceLocation(), SrcMgr::C_User); |
| 85 | } |
| 86 | |
| 87 | SourceLocation getLocation(FileID ID, unsigned Line, unsigned Column) { |
| 88 | SourceLocation Result = Sources.translateFileLineCol( |
| 89 | Sources.getFileEntryForID(ID), Line, Column); |
| 90 | assert(Result.isValid()); |
| 91 | return Result; |
| 92 | } |
| 93 | |
| 94 | std::string getRewrittenText(FileID ID) { |
| 95 | std::string Result; |
| 96 | llvm::raw_string_ostream OS(Result); |
| 97 | Rewrite.getEditBuffer(ID).write(OS); |
Manuel Klimek | ee56b54 | 2012-06-05 20:16:30 +0000 | [diff] [blame] | 98 | OS.flush(); |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 99 | return Result; |
| 100 | } |
| 101 | |
| 102 | std::string getFileContentFromDisk(StringRef Name) { |
Rafael Espindola | 8b96094 | 2013-06-26 21:02:22 +0000 | [diff] [blame] | 103 | std::string Path = TemporaryFiles.lookup(Name); |
| 104 | assert(!Path.empty()); |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 105 | // We need to read directly from the FileManager without relaying through |
| 106 | // a FileEntry, as otherwise we'd read through an already opened file |
| 107 | // descriptor, which might not see the changes made. |
| 108 | // FIXME: Figure out whether there is a way to get the SourceManger to |
| 109 | // reopen the file. |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 110 | auto FileBuffer = Files.getBufferForFile(Path); |
| 111 | return (*FileBuffer)->getBuffer(); |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 114 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts; |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 115 | DiagnosticsEngine Diagnostics; |
| 116 | TextDiagnosticPrinter DiagnosticPrinter; |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 117 | IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem; |
| 118 | IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem; |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 119 | FileManager Files; |
| 120 | SourceManager Sources; |
| 121 | LangOptions Options; |
| 122 | Rewriter Rewrite; |
| 123 | |
Manuel Klimek | 0a8b9cd | 2012-06-06 21:28:13 +0000 | [diff] [blame] | 124 | // Will be set once on disk files are generated. |
Rafael Espindola | 8b96094 | 2013-06-26 21:02:22 +0000 | [diff] [blame] | 125 | llvm::StringMap<std::string> TemporaryFiles; |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | } // end namespace clang |
| 129 | |
| 130 | #endif |