Edwin Vane | 40be0dc | 2013-07-22 16:36:58 +0000 | [diff] [blame] | 1 | //===--- VirtualFileHelper.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 | /// \brief This file defines an utility class for tests that needs a source |
| 11 | /// manager for a virtual file with customizable content. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | 8e8db65 | 2013-09-04 19:15:54 +0000 | [diff] [blame] | 15 | #ifndef CLANG_MODERNIZE_VIRTUAL_FILE_HELPER_H |
| 16 | #define CLANG_MODERNIZE_VIRTUAL_FILE_HELPER_H |
Edwin Vane | 40be0dc | 2013-07-22 16:36:58 +0000 | [diff] [blame] | 17 | |
| 18 | #include "clang/Basic/Diagnostic.h" |
| 19 | #include "clang/Basic/DiagnosticOptions.h" |
| 20 | #include "clang/Basic/FileManager.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
| 22 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 23 | |
| 24 | namespace clang { |
| 25 | |
| 26 | /// \brief Class that provides easy access to a SourceManager and that allows to |
| 27 | /// map virtual files conveniently. |
| 28 | class VirtualFileHelper { |
| 29 | struct VirtualFile { |
Guillaume Papin | 64feb39 | 2013-08-27 14:50:26 +0000 | [diff] [blame] | 30 | std::string FileName; |
| 31 | std::string Code; |
Edwin Vane | 40be0dc | 2013-07-22 16:36:58 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | public: |
| 35 | VirtualFileHelper() |
| 36 | : DiagOpts(new DiagnosticOptions()), |
| 37 | Diagnostics(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 38 | &*DiagOpts), |
| 39 | DiagnosticPrinter(llvm::outs(), &*DiagOpts), |
| 40 | Files((FileSystemOptions())) {} |
| 41 | |
| 42 | /// \brief Create a virtual file \p FileName, with content \p Code. |
| 43 | void mapFile(llvm::StringRef FileName, llvm::StringRef Code) { |
| 44 | VirtualFile VF = { FileName, Code }; |
| 45 | VirtualFiles.push_back(VF); |
| 46 | } |
| 47 | |
| 48 | /// \brief Create a new \c SourceManager with the virtual files and contents |
| 49 | /// mapped to it. |
| 50 | SourceManager &getNewSourceManager() { |
| 51 | Sources.reset(new SourceManager(Diagnostics, Files)); |
Guillaume Papin | 64feb39 | 2013-08-27 14:50:26 +0000 | [diff] [blame] | 52 | mapVirtualFiles(*Sources); |
| 53 | return *Sources; |
| 54 | } |
| 55 | |
| 56 | /// \brief Map the virtual file contents in the given \c SourceManager. |
| 57 | void mapVirtualFiles(SourceManager &SM) const { |
| 58 | for (llvm::SmallVectorImpl<VirtualFile>::const_iterator |
| 59 | I = VirtualFiles.begin(), |
| 60 | E = VirtualFiles.end(); |
Edwin Vane | 40be0dc | 2013-07-22 16:36:58 +0000 | [diff] [blame] | 61 | I != E; ++I) { |
| 62 | llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(I->Code); |
Guillaume Papin | 64feb39 | 2013-08-27 14:50:26 +0000 | [diff] [blame] | 63 | const FileEntry *Entry = SM.getFileManager().getVirtualFile( |
Edwin Vane | 40be0dc | 2013-07-22 16:36:58 +0000 | [diff] [blame] | 64 | I->FileName, Buf->getBufferSize(), /*ModificationTime=*/0); |
Guillaume Papin | 64feb39 | 2013-08-27 14:50:26 +0000 | [diff] [blame] | 65 | SM.overrideFileContents(Entry, Buf); |
Edwin Vane | 40be0dc | 2013-07-22 16:36:58 +0000 | [diff] [blame] | 66 | } |
Edwin Vane | 40be0dc | 2013-07-22 16:36:58 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | private: |
| 70 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts; |
| 71 | DiagnosticsEngine Diagnostics; |
| 72 | TextDiagnosticPrinter DiagnosticPrinter; |
| 73 | FileManager Files; |
| 74 | // most tests don't need more than one file |
| 75 | llvm::SmallVector<VirtualFile, 1> VirtualFiles; |
Ahmed Charles | 6a2dc5c | 2014-03-09 09:24:40 +0000 | [diff] [blame^] | 76 | std::unique_ptr<SourceManager> Sources; |
Edwin Vane | 40be0dc | 2013-07-22 16:36:58 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | } // end namespace clang |
| 80 | |
Chandler Carruth | 8e8db65 | 2013-09-04 19:15:54 +0000 | [diff] [blame] | 81 | #endif // CLANG_MODERNIZE_VIRTUAL_FILE_HELPER_H |