blob: 73f9aa662f6a9dcdeb9fefc00b3c2af011df9d05 [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() {
Manuel Klimekbfbfee52012-05-22 17:01:35 +000046 }
47
48 FileID createInMemoryFile(StringRef Name, StringRef Content) {
49 const llvm::MemoryBuffer *Source =
50 llvm::MemoryBuffer::getMemBuffer(Content);
51 const FileEntry *Entry =
52 Files.getVirtualFile(Name, Source->getBufferSize(), 0);
53 Sources.overrideFileContents(Entry, Source, true);
54 assert(Entry != NULL);
55 return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
56 }
57
58 FileID createOnDiskFile(StringRef Name, StringRef Content) {
59 if (!TemporaryDirectory.isValid()) {
60 std::string ErrorInfo;
61 TemporaryDirectory = llvm::sys::Path::GetTemporaryDirectory(&ErrorInfo);
62 assert(ErrorInfo.empty());
63 }
64 llvm::SmallString<1024> Path(TemporaryDirectory.str());
65 llvm::sys::path::append(Path, Name);
66 std::string ErrorInfo;
67 llvm::raw_fd_ostream OutStream(Path.c_str(),
68 ErrorInfo, llvm::raw_fd_ostream::F_Binary);
69 assert(ErrorInfo.empty());
70 OutStream << Content;
71 OutStream.close();
72 const FileEntry *File = Files.getFile(Path);
73 assert(File != NULL);
74 return Sources.createFileID(File, SourceLocation(), SrcMgr::C_User);
75 }
76
77 SourceLocation getLocation(FileID ID, unsigned Line, unsigned Column) {
78 SourceLocation Result = Sources.translateFileLineCol(
79 Sources.getFileEntryForID(ID), Line, Column);
80 assert(Result.isValid());
81 return Result;
82 }
83
84 std::string getRewrittenText(FileID ID) {
85 std::string Result;
86 llvm::raw_string_ostream OS(Result);
87 Rewrite.getEditBuffer(ID).write(OS);
Manuel Klimekd6f65692012-06-05 20:16:30 +000088 OS.flush();
Manuel Klimekbfbfee52012-05-22 17:01:35 +000089 return Result;
90 }
91
92 std::string getFileContentFromDisk(StringRef Name) {
93 llvm::SmallString<1024> Path(TemporaryDirectory.str());
94 llvm::sys::path::append(Path, Name);
95 // We need to read directly from the FileManager without relaying through
96 // a FileEntry, as otherwise we'd read through an already opened file
97 // descriptor, which might not see the changes made.
98 // FIXME: Figure out whether there is a way to get the SourceManger to
99 // reopen the file.
100 return Files.getBufferForFile(Path, NULL)->getBuffer();
101 }
102
103 DiagnosticsEngine Diagnostics;
104 TextDiagnosticPrinter DiagnosticPrinter;
105 FileManager Files;
106 SourceManager Sources;
107 LangOptions Options;
108 Rewriter Rewrite;
109
110 // Will be set once on disk files are generated.
111 llvm::sys::Path TemporaryDirectory;
112};
113
114} // end namespace clang
115
116#endif