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 | |
| 14 | #ifndef LLVM_CLANG_REWRITER_TEST_CONTEXT_H |
| 15 | #define LLVM_CLANG_REWRITER_TEST_CONTEXT_H |
| 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: |
| 37 | RewriterTestContext() |
Douglas Gregor | 275e883 | 2012-10-23 22:55:10 +0000 | [diff] [blame] | 38 | : DiagOpts(new DiagnosticOptions()), |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 39 | Diagnostics(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
Douglas Gregor | 275e883 | 2012-10-23 22:55:10 +0000 | [diff] [blame] | 40 | &*DiagOpts), |
| 41 | DiagnosticPrinter(llvm::outs(), &*DiagOpts), |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 42 | Files((FileSystemOptions())), |
| 43 | Sources(Diagnostics, Files), |
| 44 | Rewrite(Sources, Options) { |
| 45 | Diagnostics.setClient(&DiagnosticPrinter, false); |
| 46 | } |
| 47 | |
Rafael Espindola | 8b96094 | 2013-06-26 21:02:22 +0000 | [diff] [blame^] | 48 | ~RewriterTestContext() {} |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 49 | |
| 50 | FileID createInMemoryFile(StringRef Name, StringRef Content) { |
| 51 | const llvm::MemoryBuffer *Source = |
| 52 | llvm::MemoryBuffer::getMemBuffer(Content); |
| 53 | const FileEntry *Entry = |
| 54 | Files.getVirtualFile(Name, Source->getBufferSize(), 0); |
| 55 | Sources.overrideFileContents(Entry, Source, true); |
| 56 | assert(Entry != NULL); |
| 57 | return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User); |
| 58 | } |
| 59 | |
Rafael Espindola | 8b96094 | 2013-06-26 21:02:22 +0000 | [diff] [blame^] | 60 | // FIXME: this code is mostly a duplicate of |
| 61 | // unittests/Tooling/RefactoringTest.cpp. Figure out a way to share it. |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 62 | FileID createOnDiskFile(StringRef Name, StringRef Content) { |
Rafael Espindola | 8b96094 | 2013-06-26 21:02:22 +0000 | [diff] [blame^] | 63 | SmallString<1024> Path; |
| 64 | int FD; |
| 65 | llvm::error_code EC = |
| 66 | llvm::sys::fs::unique_file(Twine(Name) + "%%%%%%", FD, Path); |
| 67 | assert(!EC); |
| 68 | (void)EC; |
| 69 | |
| 70 | llvm::raw_fd_ostream OutStream(FD, true); |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 71 | OutStream << Content; |
| 72 | OutStream.close(); |
| 73 | const FileEntry *File = Files.getFile(Path); |
| 74 | assert(File != NULL); |
Rafael Espindola | 8b96094 | 2013-06-26 21:02:22 +0000 | [diff] [blame^] | 75 | |
| 76 | StringRef Found = TemporaryFiles.GetOrCreateValue(Name, Path.str()).second; |
| 77 | assert(Found == Path); |
| 78 | (void)Found; |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 79 | return Sources.createFileID(File, SourceLocation(), SrcMgr::C_User); |
| 80 | } |
| 81 | |
| 82 | SourceLocation getLocation(FileID ID, unsigned Line, unsigned Column) { |
| 83 | SourceLocation Result = Sources.translateFileLineCol( |
| 84 | Sources.getFileEntryForID(ID), Line, Column); |
| 85 | assert(Result.isValid()); |
| 86 | return Result; |
| 87 | } |
| 88 | |
| 89 | std::string getRewrittenText(FileID ID) { |
| 90 | std::string Result; |
| 91 | llvm::raw_string_ostream OS(Result); |
| 92 | Rewrite.getEditBuffer(ID).write(OS); |
Manuel Klimek | ee56b54 | 2012-06-05 20:16:30 +0000 | [diff] [blame] | 93 | OS.flush(); |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 94 | return Result; |
| 95 | } |
| 96 | |
| 97 | std::string getFileContentFromDisk(StringRef Name) { |
Rafael Espindola | 8b96094 | 2013-06-26 21:02:22 +0000 | [diff] [blame^] | 98 | std::string Path = TemporaryFiles.lookup(Name); |
| 99 | assert(!Path.empty()); |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 100 | // We need to read directly from the FileManager without relaying through |
| 101 | // a FileEntry, as otherwise we'd read through an already opened file |
| 102 | // descriptor, which might not see the changes made. |
| 103 | // FIXME: Figure out whether there is a way to get the SourceManger to |
| 104 | // reopen the file. |
| 105 | return Files.getBufferForFile(Path, NULL)->getBuffer(); |
| 106 | } |
| 107 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 108 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts; |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 109 | DiagnosticsEngine Diagnostics; |
| 110 | TextDiagnosticPrinter DiagnosticPrinter; |
| 111 | FileManager Files; |
| 112 | SourceManager Sources; |
| 113 | LangOptions Options; |
| 114 | Rewriter Rewrite; |
| 115 | |
Manuel Klimek | 0a8b9cd | 2012-06-06 21:28:13 +0000 | [diff] [blame] | 116 | // Will be set once on disk files are generated. |
Rafael Espindola | 8b96094 | 2013-06-26 21:02:22 +0000 | [diff] [blame^] | 117 | llvm::StringMap<std::string> TemporaryFiles; |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | } // end namespace clang |
| 121 | |
| 122 | #endif |