blob: f68be6b4576a5d3b9748240b06ade6f98eb89a93 [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"
Manuel Klimek7b699ac2012-06-06 21:28:13 +000024#include "llvm/Support/FileSystem.h"
Manuel Klimekbfbfee52012-05-22 17:01:35 +000025#include "llvm/Support/Path.h"
26#include "llvm/Support/raw_ostream.h"
27
28namespace clang {
29
30/// \brief A class that sets up a ready to use Rewriter.
31///
32/// Useful in unit tests that need a Rewriter. Creates all dependencies
33/// of a Rewriter with default values for testing and provides convenience
34/// methods, which help with writing tests that change files.
35class RewriterTestContext {
36 public:
37 RewriterTestContext()
38 : Diagnostics(llvm::IntrusiveRefCntPtr<DiagnosticIDs>()),
39 DiagnosticPrinter(llvm::outs(), DiagnosticOptions()),
40 Files((FileSystemOptions())),
41 Sources(Diagnostics, Files),
42 Rewrite(Sources, Options) {
43 Diagnostics.setClient(&DiagnosticPrinter, false);
44 }
45
46 ~RewriterTestContext() {
Manuel Klimek7b699ac2012-06-06 21:28:13 +000047 if (!TemporaryDirectory.empty()) {
48 uint32_t RemovedCount = 0;
49 llvm::sys::fs::remove_all(TemporaryDirectory.str(), RemovedCount);
50 }
Manuel Klimekbfbfee52012-05-22 17:01:35 +000051 }
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) {
Manuel Klimek7b699ac2012-06-06 21:28:13 +000064 if (TemporaryDirectory.empty()) {
65 int FD;
Benjamin Kramerc2a50d42012-06-07 09:57:21 +000066 bool error =
67 llvm::sys::fs::unique_file("rewriter-test-%%-%%-%%-%%/anchor", FD,
68 TemporaryDirectory);
69 assert(!error); (void)error;
Manuel Klimek7b699ac2012-06-06 21:28:13 +000070 llvm::raw_fd_ostream Closer(FD, /*shouldClose=*/true);
71 TemporaryDirectory = llvm::sys::path::parent_path(TemporaryDirectory);
Manuel Klimekbfbfee52012-05-22 17:01:35 +000072 }
Manuel Klimek7b699ac2012-06-06 21:28:13 +000073 llvm::SmallString<1024> Path(TemporaryDirectory);
Manuel Klimekbfbfee52012-05-22 17:01:35 +000074 llvm::sys::path::append(Path, Name);
75 std::string ErrorInfo;
76 llvm::raw_fd_ostream OutStream(Path.c_str(),
77 ErrorInfo, llvm::raw_fd_ostream::F_Binary);
78 assert(ErrorInfo.empty());
79 OutStream << Content;
80 OutStream.close();
81 const FileEntry *File = Files.getFile(Path);
82 assert(File != NULL);
83 return Sources.createFileID(File, SourceLocation(), SrcMgr::C_User);
84 }
85
86 SourceLocation getLocation(FileID ID, unsigned Line, unsigned Column) {
87 SourceLocation Result = Sources.translateFileLineCol(
88 Sources.getFileEntryForID(ID), Line, Column);
89 assert(Result.isValid());
90 return Result;
91 }
92
93 std::string getRewrittenText(FileID ID) {
94 std::string Result;
95 llvm::raw_string_ostream OS(Result);
96 Rewrite.getEditBuffer(ID).write(OS);
Manuel Klimekd6f65692012-06-05 20:16:30 +000097 OS.flush();
Manuel Klimekbfbfee52012-05-22 17:01:35 +000098 return Result;
99 }
100
101 std::string getFileContentFromDisk(StringRef Name) {
102 llvm::SmallString<1024> Path(TemporaryDirectory.str());
103 llvm::sys::path::append(Path, Name);
104 // We need to read directly from the FileManager without relaying through
105 // a FileEntry, as otherwise we'd read through an already opened file
106 // descriptor, which might not see the changes made.
107 // FIXME: Figure out whether there is a way to get the SourceManger to
108 // reopen the file.
109 return Files.getBufferForFile(Path, NULL)->getBuffer();
110 }
111
112 DiagnosticsEngine Diagnostics;
113 TextDiagnosticPrinter DiagnosticPrinter;
114 FileManager Files;
115 SourceManager Sources;
116 LangOptions Options;
117 Rewriter Rewrite;
118
Manuel Klimek7b699ac2012-06-06 21:28:13 +0000119 // Will be set once on disk files are generated.
120 SmallString<128> TemporaryDirectory;
Manuel Klimekbfbfee52012-05-22 17:01:35 +0000121};
122
123} // end namespace clang
124
125#endif