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