blob: eecb1d0cb8953c825bf91f3084e4ae94ad58efc6 [file] [log] [blame]
Manuel Klimekbfbfee52012-05-22 17:01:35 +00001//===--- RewriterTestContext.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// This file defines a utility class for Rewriter related tests.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_REWRITER_TEST_CONTEXT_H
15#define LLVM_CLANG_REWRITER_TEST_CONTEXT_H
16
17#include "clang/Basic/Diagnostic.h"
18#include "clang/Basic/FileManager.h"
19#include "clang/Basic/LangOptions.h"
20#include "clang/Basic/SourceManager.h"
21#include "clang/Frontend/DiagnosticOptions.h"
22#include "clang/Frontend/TextDiagnosticPrinter.h"
23#include "clang/Rewrite/Rewriter.h"
24#include "llvm/Support/Path.h"
25#include "llvm/Support/raw_ostream.h"
26
27namespace clang {
28
29/// \brief A class that sets up a ready to use Rewriter.
30///
31/// Useful in unit tests that need a Rewriter. Creates all dependencies
32/// of a Rewriter with default values for testing and provides convenience
33/// methods, which help with writing tests that change files.
34class RewriterTestContext {
35 public:
36 RewriterTestContext()
37 : Diagnostics(llvm::IntrusiveRefCntPtr<DiagnosticIDs>()),
38 DiagnosticPrinter(llvm::outs(), DiagnosticOptions()),
39 Files((FileSystemOptions())),
40 Sources(Diagnostics, Files),
41 Rewrite(Sources, Options) {
42 Diagnostics.setClient(&DiagnosticPrinter, false);
43 }
44
45 ~RewriterTestContext() {
46 if (TemporaryDirectory.isValid()) {
47 std::string ErrorInfo;
48 TemporaryDirectory.eraseFromDisk(true, &ErrorInfo);
49 assert(ErrorInfo.empty());
50 }
51 }
52
53 FileID createInMemoryFile(StringRef Name, StringRef Content) {
54 const llvm::MemoryBuffer *Source =
55 llvm::MemoryBuffer::getMemBuffer(Content);
56 const FileEntry *Entry =
57 Files.getVirtualFile(Name, Source->getBufferSize(), 0);
58 Sources.overrideFileContents(Entry, Source, true);
59 assert(Entry != NULL);
60 return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
61 }
62
63 FileID createOnDiskFile(StringRef Name, StringRef Content) {
64 if (!TemporaryDirectory.isValid()) {
65 std::string ErrorInfo;
66 TemporaryDirectory = llvm::sys::Path::GetTemporaryDirectory(&ErrorInfo);
67 assert(ErrorInfo.empty());
68 }
69 llvm::SmallString<1024> Path(TemporaryDirectory.str());
70 llvm::sys::path::append(Path, Name);
71 std::string ErrorInfo;
72 llvm::raw_fd_ostream OutStream(Path.c_str(),
73 ErrorInfo, llvm::raw_fd_ostream::F_Binary);
74 assert(ErrorInfo.empty());
75 OutStream << Content;
76 OutStream.close();
77 const FileEntry *File = Files.getFile(Path);
78 assert(File != NULL);
79 return Sources.createFileID(File, SourceLocation(), SrcMgr::C_User);
80 }
81
82 SourceLocation getLocation(FileID ID, unsigned Line, unsigned Column) {
83 SourceLocation Result = Sources.translateFileLineCol(
84 Sources.getFileEntryForID(ID), Line, Column);
85 assert(Result.isValid());
86 return Result;
87 }
88
89 std::string getRewrittenText(FileID ID) {
90 std::string Result;
91 llvm::raw_string_ostream OS(Result);
92 Rewrite.getEditBuffer(ID).write(OS);
93 return Result;
94 }
95
96 std::string getFileContentFromDisk(StringRef Name) {
97 llvm::SmallString<1024> Path(TemporaryDirectory.str());
98 llvm::sys::path::append(Path, Name);
99 // We need to read directly from the FileManager without relaying through
100 // a FileEntry, as otherwise we'd read through an already opened file
101 // descriptor, which might not see the changes made.
102 // FIXME: Figure out whether there is a way to get the SourceManger to
103 // reopen the file.
104 return Files.getBufferForFile(Path, NULL)->getBuffer();
105 }
106
107 DiagnosticsEngine Diagnostics;
108 TextDiagnosticPrinter DiagnosticPrinter;
109 FileManager Files;
110 SourceManager Sources;
111 LangOptions Options;
112 Rewriter Rewrite;
113
114 // Will be set once on disk files are generated.
115 llvm::sys::Path TemporaryDirectory;
116};
117
118} // end namespace clang
119
120#endif