xref: /minix3/external/bsd/llvm/dist/clang/lib/Index/SimpleFormatContext.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- SimpleFormatContext.h ----------------------------------*- C++ -*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc /// \file
11f4a2713aSLionel Sambuc ///
12f4a2713aSLionel Sambuc /// \brief Defines a utility class for use of clang-format in libclang
13f4a2713aSLionel Sambuc //
14f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
15f4a2713aSLionel Sambuc 
16*0a6a1f1dSLionel Sambuc #ifndef LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
17*0a6a1f1dSLionel Sambuc #define LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc #include "clang/Basic/Diagnostic.h"
20f4a2713aSLionel Sambuc #include "clang/Basic/DiagnosticOptions.h"
21f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h"
22f4a2713aSLionel Sambuc #include "clang/Basic/LangOptions.h"
23f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
24f4a2713aSLionel Sambuc #include "clang/Rewrite/Core/Rewriter.h"
25f4a2713aSLionel Sambuc #include "llvm/Support/FileSystem.h"
26f4a2713aSLionel Sambuc #include "llvm/Support/Path.h"
27f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc namespace clang {
30f4a2713aSLionel Sambuc namespace index {
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc /// \brief A small class to be used by libclang clients to format
33f4a2713aSLionel Sambuc /// a declaration string in memory. This object is instantiated once
34f4a2713aSLionel Sambuc /// and used each time a formatting is needed.
35f4a2713aSLionel Sambuc class SimpleFormatContext {
36f4a2713aSLionel Sambuc public:
SimpleFormatContext(LangOptions Options)37f4a2713aSLionel Sambuc   SimpleFormatContext(LangOptions Options)
38f4a2713aSLionel Sambuc       : DiagOpts(new DiagnosticOptions()),
39f4a2713aSLionel Sambuc         Diagnostics(new DiagnosticsEngine(new DiagnosticIDs,
40*0a6a1f1dSLionel Sambuc                                           DiagOpts.get())),
41f4a2713aSLionel Sambuc         Files((FileSystemOptions())),
42f4a2713aSLionel Sambuc         Sources(*Diagnostics, Files),
43f4a2713aSLionel Sambuc         Rewrite(Sources, Options) {
44f4a2713aSLionel Sambuc     Diagnostics->setClient(new IgnoringDiagConsumer, true);
45f4a2713aSLionel Sambuc   }
46f4a2713aSLionel Sambuc 
~SimpleFormatContext()47f4a2713aSLionel Sambuc   ~SimpleFormatContext() { }
48f4a2713aSLionel Sambuc 
createInMemoryFile(StringRef Name,StringRef Content)49f4a2713aSLionel Sambuc   FileID createInMemoryFile(StringRef Name, StringRef Content) {
50*0a6a1f1dSLionel Sambuc     std::unique_ptr<llvm::MemoryBuffer> Source =
51f4a2713aSLionel Sambuc         llvm::MemoryBuffer::getMemBuffer(Content);
52f4a2713aSLionel Sambuc     const FileEntry *Entry =
53f4a2713aSLionel Sambuc         Files.getVirtualFile(Name, Source->getBufferSize(), 0);
54*0a6a1f1dSLionel Sambuc     Sources.overrideFileContents(Entry, std::move(Source));
55*0a6a1f1dSLionel Sambuc     assert(Entry != nullptr);
56f4a2713aSLionel Sambuc     return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
57f4a2713aSLionel Sambuc   }
58f4a2713aSLionel Sambuc 
getRewrittenText(FileID ID)59f4a2713aSLionel Sambuc   std::string getRewrittenText(FileID ID) {
60f4a2713aSLionel Sambuc     std::string Result;
61f4a2713aSLionel Sambuc     llvm::raw_string_ostream OS(Result);
62f4a2713aSLionel Sambuc     Rewrite.getEditBuffer(ID).write(OS);
63f4a2713aSLionel Sambuc     OS.flush();
64f4a2713aSLionel Sambuc     return Result;
65f4a2713aSLionel Sambuc   }
66f4a2713aSLionel Sambuc 
67f4a2713aSLionel Sambuc   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
68f4a2713aSLionel Sambuc   IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
69f4a2713aSLionel Sambuc   FileManager Files;
70f4a2713aSLionel Sambuc   SourceManager Sources;
71f4a2713aSLionel Sambuc   Rewriter Rewrite;
72f4a2713aSLionel Sambuc };
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc } // end namespace index
75f4a2713aSLionel Sambuc } // end namespace clang
76f4a2713aSLionel Sambuc 
77f4a2713aSLionel Sambuc #endif
78