blob: f322e63d0606e11e06f1c43bcd65dd3b8a5bd6ec [file] [log] [blame]
Fariborz Jahanian88b95212012-12-18 23:02:59 +00001//===--- SimpleFormatContext.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_SIMPLE_FORM_CONTEXT_H
15#define LLVM_CLANG_SIMPLE_FORM_CONTEXT_H
16
17#include "clang/Basic/Diagnostic.h"
18#include "clang/Basic/DiagnosticOptions.h"
19#include "clang/Basic/FileManager.h"
20#include "clang/Basic/LangOptions.h"
21#include "clang/Basic/SourceManager.h"
22#include "clang/Rewrite/Core/Rewriter.h"
23#include "llvm/Support/FileSystem.h"
24#include "llvm/Support/Path.h"
25#include "llvm/Support/raw_ostream.h"
26
27namespace clang {
28
29/// \brief A small class to be used by libclang clients to format
30/// a declaration string in memory. This object is instantiated once
31/// and used each time a formatting is needed.
32class SimpleFormatContext {
33 public:
34 SimpleFormatContext(LangOptions Options)
35 : DiagOpts(new DiagnosticOptions()),
36 Diagnostics(new DiagnosticsEngine(new DiagnosticIDs,
37 DiagOpts.getPtr())),
38 Files((FileSystemOptions())),
39 Sources(*Diagnostics, Files),
40 Rewrite(Sources, Options) {
41 Diagnostics->setClient(new IgnoringDiagConsumer, true);
42 }
43
44 ~SimpleFormatContext() { }
45
46 FileID createInMemoryFile(StringRef Name, StringRef Content) {
47 const llvm::MemoryBuffer *Source =
48 llvm::MemoryBuffer::getMemBuffer(Content);
49 const FileEntry *Entry =
50 Files.getVirtualFile(Name, Source->getBufferSize(), 0);
51 Sources.overrideFileContents(Entry, Source, true);
52 assert(Entry != NULL);
53 return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
54 }
55
56 std::string getRewrittenText(FileID ID) {
57 std::string Result;
58 llvm::raw_string_ostream OS(Result);
59 Rewrite.getEditBuffer(ID).write(OS);
60 OS.flush();
61 return Result;
62 }
63
64 llvm::IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
65 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
66 FileManager Files;
67 SourceManager Sources;
68 Rewriter Rewrite;
69};
70
71} // end namespace clang
72
73#endif