blob: 17793154a3ae1ed880552730c2366158fabd86a5 [file] [log] [blame]
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001//===--- SimpleFormatContext.h ----------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00006//
7//===----------------------------------------------------------------------===//
8//
Fariborz Jahanianae9d8ae2012-12-19 17:03:46 +00009/// \file
Fariborz Jahanian799d96b2012-12-19 01:00:36 +000010///
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000011/// Defines a utility class for use of clang-format in libclang
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000012//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000015#ifndef LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
16#define LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000017
18#include "clang/Basic/Diagnostic.h"
19#include "clang/Basic/DiagnosticOptions.h"
20#include "clang/Basic/FileManager.h"
21#include "clang/Basic/LangOptions.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Rewrite/Core/Rewriter.h"
24#include "llvm/Support/FileSystem.h"
25#include "llvm/Support/Path.h"
26#include "llvm/Support/raw_ostream.h"
27
28namespace clang {
Dmitri Gribenko9e605112013-11-13 22:16:51 +000029namespace index {
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000030
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000031/// A small class to be used by libclang clients to format
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000032/// a declaration string in memory. This object is instantiated once
33/// and used each time a formatting is needed.
34class SimpleFormatContext {
Fariborz Jahanianb67908a2012-12-19 00:01:48 +000035public:
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000036 SimpleFormatContext(LangOptions Options)
37 : DiagOpts(new DiagnosticOptions()),
Jonas Devliegherefc514902018-10-10 13:27:25 +000038 Diagnostics(new DiagnosticsEngine(new DiagnosticIDs, DiagOpts.get())),
39 InMemoryFileSystem(new llvm::vfs::InMemoryFileSystem),
Benjamin Kramerb93511a2015-10-06 10:23:34 +000040 Files(FileSystemOptions(), InMemoryFileSystem),
Jonas Devliegherefc514902018-10-10 13:27:25 +000041 Sources(*Diagnostics, Files), Rewrite(Sources, Options) {
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000042 Diagnostics->setClient(new IgnoringDiagConsumer, true);
43 }
44
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000045 FileID createInMemoryFile(StringRef Name, StringRef Content) {
Benjamin Kramerb93511a2015-10-06 10:23:34 +000046 InMemoryFileSystem->addFile(Name, 0,
47 llvm::MemoryBuffer::getMemBuffer(Content));
48 const FileEntry *Entry = Files.getFile(Name);
Craig Topper236bde32014-05-26 06:21:51 +000049 assert(Entry != nullptr);
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000050 return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
51 }
52
53 std::string getRewrittenText(FileID ID) {
54 std::string Result;
55 llvm::raw_string_ostream OS(Result);
56 Rewrite.getEditBuffer(ID).write(OS);
57 OS.flush();
58 return Result;
59 }
60
Dmitri Gribenkof8579502013-01-12 19:30:44 +000061 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
62 IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
Jonas Devliegherefc514902018-10-10 13:27:25 +000063 IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem;
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000064 FileManager Files;
65 SourceManager Sources;
66 Rewriter Rewrite;
67};
68
Dmitri Gribenko9e605112013-11-13 22:16:51 +000069} // end namespace index
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +000070} // end namespace clang
71
72#endif