]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Index/SimpleFormatContext.h
Merge clang trunk r351319, resolve conflicts, and update FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Index / SimpleFormatContext.h
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 /// Defines a utility class for use of clang-format in libclang
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
17 #define LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
18
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"
28
29 namespace clang {
30 namespace index {
31
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 {
36 public:
37   SimpleFormatContext(LangOptions Options)
38       : DiagOpts(new DiagnosticOptions()),
39         Diagnostics(new DiagnosticsEngine(new DiagnosticIDs, DiagOpts.get())),
40         InMemoryFileSystem(new llvm::vfs::InMemoryFileSystem),
41         Files(FileSystemOptions(), InMemoryFileSystem),
42         Sources(*Diagnostics, Files), Rewrite(Sources, Options) {
43     Diagnostics->setClient(new IgnoringDiagConsumer, true);
44   }
45
46   FileID createInMemoryFile(StringRef Name, StringRef Content) {
47     InMemoryFileSystem->addFile(Name, 0,
48                                 llvm::MemoryBuffer::getMemBuffer(Content));
49     const FileEntry *Entry = Files.getFile(Name);
50     assert(Entry != nullptr);
51     return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
52   }
53
54   std::string getRewrittenText(FileID ID) {
55     std::string Result;
56     llvm::raw_string_ostream OS(Result);
57     Rewrite.getEditBuffer(ID).write(OS);
58     OS.flush();
59     return Result;
60   }
61
62   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
63   IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
64   IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem;
65   FileManager Files;
66   SourceManager Sources;
67   Rewriter Rewrite;
68 };
69
70 } // end namespace index
71 } // end namespace clang
72
73 #endif