blob: 4ec00da9ced861f65d5c3e983411eecc7bde8f41 [file] [log] [blame]
Edwin Vane40be0dc2013-07-22 16:36:58 +00001//===--- 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 Carruth8e8db652013-09-04 19:15:54 +000015#ifndef CLANG_MODERNIZE_VIRTUAL_FILE_HELPER_H
16#define CLANG_MODERNIZE_VIRTUAL_FILE_HELPER_H
Edwin Vane40be0dc2013-07-22 16:36:58 +000017
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
24namespace clang {
25
26/// \brief Class that provides easy access to a SourceManager and that allows to
27/// map virtual files conveniently.
28class VirtualFileHelper {
29 struct VirtualFile {
Guillaume Papin64feb392013-08-27 14:50:26 +000030 std::string FileName;
31 std::string Code;
Edwin Vane40be0dc2013-07-22 16:36:58 +000032 };
33
34public:
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 Papin64feb392013-08-27 14:50:26 +000052 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 Vane40be0dc2013-07-22 16:36:58 +000061 I != E; ++I) {
62 llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(I->Code);
Guillaume Papin64feb392013-08-27 14:50:26 +000063 const FileEntry *Entry = SM.getFileManager().getVirtualFile(
Edwin Vane40be0dc2013-07-22 16:36:58 +000064 I->FileName, Buf->getBufferSize(), /*ModificationTime=*/0);
Guillaume Papin64feb392013-08-27 14:50:26 +000065 SM.overrideFileContents(Entry, Buf);
Edwin Vane40be0dc2013-07-22 16:36:58 +000066 }
Edwin Vane40be0dc2013-07-22 16:36:58 +000067 }
68
69private:
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 Charles6a2dc5c2014-03-09 09:24:40 +000076 std::unique_ptr<SourceManager> Sources;
Edwin Vane40be0dc2013-07-22 16:36:58 +000077};
78
79} // end namespace clang
80
Chandler Carruth8e8db652013-09-04 19:15:54 +000081#endif // CLANG_MODERNIZE_VIRTUAL_FILE_HELPER_H