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 | |
| 15 | #ifndef CPP11_MIGRATE_VIRTUAL_FILE_HELPER_H |
| 16 | #define CPP11_MIGRATE_VIRTUAL_FILE_HELPER_H |
| 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 { |
| 30 | llvm::StringRef FileName; |
| 31 | llvm::StringRef Code; |
| 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)); |
| 52 | for (llvm::SmallVectorImpl<VirtualFile>::iterator I = VirtualFiles.begin(), |
| 53 | E = VirtualFiles.end(); |
| 54 | I != E; ++I) { |
| 55 | llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(I->Code); |
| 56 | const FileEntry *Entry = Files.getVirtualFile( |
| 57 | I->FileName, Buf->getBufferSize(), /*ModificationTime=*/0); |
| 58 | Sources->overrideFileContents(Entry, Buf); |
| 59 | } |
| 60 | return *Sources; |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts; |
| 65 | DiagnosticsEngine Diagnostics; |
| 66 | TextDiagnosticPrinter DiagnosticPrinter; |
| 67 | FileManager Files; |
| 68 | // most tests don't need more than one file |
| 69 | llvm::SmallVector<VirtualFile, 1> VirtualFiles; |
| 70 | llvm::OwningPtr<SourceManager> Sources; |
| 71 | }; |
| 72 | |
| 73 | } // end namespace clang |
| 74 | |
| 75 | #endif // CPP11_MIGRATE_VIRTUAL_FILE_HELPER_H |