summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/unittests/cpp11-migrate/VirtualFileHelper.h
blob: 4ae29fbe0c09c71f72f55a285a020c5ec411dff0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//===--- VirtualFileHelper.h ------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \brief This file defines an utility class for tests that needs a source
/// manager for a virtual file with customizable content.
///
//===----------------------------------------------------------------------===//

#ifndef CPP11_MIGRATE_VIRTUAL_FILE_HELPER_H
#define CPP11_MIGRATE_VIRTUAL_FILE_HELPER_H

#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"

namespace clang {

/// \brief Class that provides easy access to a SourceManager and that allows to
/// map virtual files conveniently.
class VirtualFileHelper {
  struct VirtualFile {
    llvm::StringRef FileName;
    llvm::StringRef Code;
  };

public:
  VirtualFileHelper()
      : DiagOpts(new DiagnosticOptions()),
        Diagnostics(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
                    &*DiagOpts),
        DiagnosticPrinter(llvm::outs(), &*DiagOpts),
        Files((FileSystemOptions())) {}

  /// \brief Create a virtual file \p FileName, with content \p Code.
  void mapFile(llvm::StringRef FileName, llvm::StringRef Code) {
    VirtualFile VF = { FileName, Code };
    VirtualFiles.push_back(VF);
  }

  /// \brief Create a new \c SourceManager with the virtual files and contents
  /// mapped to it.
  SourceManager &getNewSourceManager() {
    Sources.reset(new SourceManager(Diagnostics, Files));
    for (llvm::SmallVectorImpl<VirtualFile>::iterator I = VirtualFiles.begin(),
                                                      E = VirtualFiles.end();
         I != E; ++I) {
      llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(I->Code);
      const FileEntry *Entry = Files.getVirtualFile(
          I->FileName, Buf->getBufferSize(), /*ModificationTime=*/0);
      Sources->overrideFileContents(Entry, Buf);
    }
    return *Sources;
  }

private:
  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
  DiagnosticsEngine Diagnostics;
  TextDiagnosticPrinter DiagnosticPrinter;
  FileManager Files;
  // most tests don't need more than one file
  llvm::SmallVector<VirtualFile, 1> VirtualFiles;
  llvm::OwningPtr<SourceManager> Sources;
};

} // end namespace clang

#endif // CPP11_MIGRATE_VIRTUAL_FILE_HELPER_H
OpenPOWER on IntegriCloud