blob: 5fa4d53a1735f6a936cfb4cd39d357aea1179e43 [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) {
Rafael Espindola7fa03032014-08-27 20:03:22 +000062 std::unique_ptr<llvm::MemoryBuffer> Buf =
63 llvm::MemoryBuffer::getMemBuffer(I->Code);
Guillaume Papin64feb392013-08-27 14:50:26 +000064 const FileEntry *Entry = SM.getFileManager().getVirtualFile(
Edwin Vane40be0dc2013-07-22 16:36:58 +000065 I->FileName, Buf->getBufferSize(), /*ModificationTime=*/0);
David Blaikie715c98a2014-08-27 20:54:50 +000066 SM.overrideFileContents(Entry, std::move(Buf));
Edwin Vane40be0dc2013-07-22 16:36:58 +000067 }
Edwin Vane40be0dc2013-07-22 16:36:58 +000068 }
69
70private:
71 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
72 DiagnosticsEngine Diagnostics;
73 TextDiagnosticPrinter DiagnosticPrinter;
74 FileManager Files;
75 // most tests don't need more than one file
76 llvm::SmallVector<VirtualFile, 1> VirtualFiles;
Ahmed Charles6a2dc5c2014-03-09 09:24:40 +000077 std::unique_ptr<SourceManager> Sources;
Edwin Vane40be0dc2013-07-22 16:36:58 +000078};
79
80} // end namespace clang
81
Chandler Carruth8e8db652013-09-04 19:15:54 +000082#endif // CLANG_MODERNIZE_VIRTUAL_FILE_HELPER_H