etc/services - sync with NetBSD-8
[minix.git] / external / bsd / llvm / dist / clang / lib / Index / SimpleFormatContext.h
blob080a4ad04ddf8ac3facc243562dc93fff186c4bc
1 //===--- 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 /// \file
11 ///
12 /// \brief Defines a utility class for use of clang-format in libclang
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
17 #define LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
19 #include "clang/Basic/Diagnostic.h"
20 #include "clang/Basic/DiagnosticOptions.h"
21 #include "clang/Basic/FileManager.h"
22 #include "clang/Basic/LangOptions.h"
23 #include "clang/Basic/SourceManager.h"
24 #include "clang/Rewrite/Core/Rewriter.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/raw_ostream.h"
29 namespace clang {
30 namespace index {
32 /// \brief A small class to be used by libclang clients to format
33 /// a declaration string in memory. This object is instantiated once
34 /// and used each time a formatting is needed.
35 class SimpleFormatContext {
36 public:
37 SimpleFormatContext(LangOptions Options)
38 : DiagOpts(new DiagnosticOptions()),
39 Diagnostics(new DiagnosticsEngine(new DiagnosticIDs,
40 DiagOpts.get())),
41 Files((FileSystemOptions())),
42 Sources(*Diagnostics, Files),
43 Rewrite(Sources, Options) {
44 Diagnostics->setClient(new IgnoringDiagConsumer, true);
47 ~SimpleFormatContext() { }
49 FileID createInMemoryFile(StringRef Name, StringRef Content) {
50 std::unique_ptr<llvm::MemoryBuffer> Source =
51 llvm::MemoryBuffer::getMemBuffer(Content);
52 const FileEntry *Entry =
53 Files.getVirtualFile(Name, Source->getBufferSize(), 0);
54 Sources.overrideFileContents(Entry, std::move(Source));
55 assert(Entry != nullptr);
56 return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
59 std::string getRewrittenText(FileID ID) {
60 std::string Result;
61 llvm::raw_string_ostream OS(Result);
62 Rewrite.getEditBuffer(ID).write(OS);
63 OS.flush();
64 return Result;
67 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
68 IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
69 FileManager Files;
70 SourceManager Sources;
71 Rewriter Rewrite;
74 } // end namespace index
75 } // end namespace clang
77 #endif