blob: 24adcac60201a4899ede3f106df3a6dfd1616dc9 [file] [log] [blame]
Fariborz Jahanian9b7ab872012-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//
Fariborz Jahanianae9d8ae2012-12-19 17:03:46 +000010/// \file
Fariborz Jahanian799d96b2012-12-19 01:00:36 +000011///
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012/// Defines a utility class for use of clang-format in libclang
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000013//
14//===----------------------------------------------------------------------===//
15
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000016#ifndef LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
17#define LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000018
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
29namespace clang {
Dmitri Gribenko9e605112013-11-13 22:16:51 +000030namespace index {
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000031
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000032/// A small class to be used by libclang clients to format
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000033/// a declaration string in memory. This object is instantiated once
34/// and used each time a formatting is needed.
35class SimpleFormatContext {
Fariborz Jahanianb67908a2012-12-19 00:01:48 +000036public:
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000037 SimpleFormatContext(LangOptions Options)
38 : DiagOpts(new DiagnosticOptions()),
Jonas Devliegherefc514902018-10-10 13:27:25 +000039 Diagnostics(new DiagnosticsEngine(new DiagnosticIDs, DiagOpts.get())),
40 InMemoryFileSystem(new llvm::vfs::InMemoryFileSystem),
Benjamin Kramerb93511a2015-10-06 10:23:34 +000041 Files(FileSystemOptions(), InMemoryFileSystem),
Jonas Devliegherefc514902018-10-10 13:27:25 +000042 Sources(*Diagnostics, Files), Rewrite(Sources, Options) {
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000043 Diagnostics->setClient(new IgnoringDiagConsumer, true);
44 }
45
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000046 FileID createInMemoryFile(StringRef Name, StringRef Content) {
Benjamin Kramerb93511a2015-10-06 10:23:34 +000047 InMemoryFileSystem->addFile(Name, 0,
48 llvm::MemoryBuffer::getMemBuffer(Content));
49 const FileEntry *Entry = Files.getFile(Name);
Craig Topper236bde32014-05-26 06:21:51 +000050 assert(Entry != nullptr);
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000051 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
Dmitri Gribenkof8579502013-01-12 19:30:44 +000062 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
63 IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
Jonas Devliegherefc514902018-10-10 13:27:25 +000064 IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem;
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000065 FileManager Files;
66 SourceManager Sources;
67 Rewriter Rewrite;
68};
69
Dmitri Gribenko9e605112013-11-13 22:16:51 +000070} // end namespace index
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000071} // end namespace clang
72
73#endif