1 //===--- SimpleFormatContext.h ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 /// 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"
32 /// 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 {
37 SimpleFormatContext(LangOptions Options)
38 : DiagOpts(new DiagnosticOptions()),
39 Diagnostics(new DiagnosticsEngine(new DiagnosticIDs,
41 InMemoryFileSystem(new vfs::InMemoryFileSystem),
42 Files(FileSystemOptions(), InMemoryFileSystem),
43 Sources(*Diagnostics, Files),
44 Rewrite(Sources, Options) {
45 Diagnostics->setClient(new IgnoringDiagConsumer, true);
48 FileID createInMemoryFile(StringRef Name, StringRef Content) {
49 InMemoryFileSystem->addFile(Name, 0,
50 llvm::MemoryBuffer::getMemBuffer(Content));
51 const FileEntry *Entry = Files.getFile(Name);
52 assert(Entry != nullptr);
53 return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
56 std::string getRewrittenText(FileID ID) {
58 llvm::raw_string_ostream OS(Result);
59 Rewrite.getEditBuffer(ID).write(OS);
64 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
65 IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
66 IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem;
68 SourceManager Sources;
72 } // end namespace index
73 } // end namespace clang