Fariborz Jahanian | 9b7ab87 | 2012-12-18 23:02:59 +0000 | [diff] [blame] | 1 | //===--- SimpleFormatContext.h ----------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Jahanian | 9b7ab87 | 2012-12-18 23:02:59 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Fariborz Jahanian | ae9d8ae | 2012-12-19 17:03:46 +0000 | [diff] [blame] | 9 | /// \file |
Fariborz Jahanian | 799d96b | 2012-12-19 01:00:36 +0000 | [diff] [blame] | 10 | /// |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 11 | /// Defines a utility class for use of clang-format in libclang |
Fariborz Jahanian | 9b7ab87 | 2012-12-18 23:02:59 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 15 | #ifndef LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H |
| 16 | #define LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H |
Fariborz Jahanian | 9b7ab87 | 2012-12-18 23:02:59 +0000 | [diff] [blame] | 17 | |
| 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 | |
| 28 | namespace clang { |
Dmitri Gribenko | 9e60511 | 2013-11-13 22:16:51 +0000 | [diff] [blame] | 29 | namespace index { |
Fariborz Jahanian | 9b7ab87 | 2012-12-18 23:02:59 +0000 | [diff] [blame] | 30 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 31 | /// A small class to be used by libclang clients to format |
Fariborz Jahanian | 9b7ab87 | 2012-12-18 23:02:59 +0000 | [diff] [blame] | 32 | /// a declaration string in memory. This object is instantiated once |
| 33 | /// and used each time a formatting is needed. |
| 34 | class SimpleFormatContext { |
Fariborz Jahanian | b67908a | 2012-12-19 00:01:48 +0000 | [diff] [blame] | 35 | public: |
Fariborz Jahanian | 9b7ab87 | 2012-12-18 23:02:59 +0000 | [diff] [blame] | 36 | SimpleFormatContext(LangOptions Options) |
| 37 | : DiagOpts(new DiagnosticOptions()), |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 38 | Diagnostics(new DiagnosticsEngine(new DiagnosticIDs, DiagOpts.get())), |
| 39 | InMemoryFileSystem(new llvm::vfs::InMemoryFileSystem), |
Benjamin Kramer | b93511a | 2015-10-06 10:23:34 +0000 | [diff] [blame] | 40 | Files(FileSystemOptions(), InMemoryFileSystem), |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 41 | Sources(*Diagnostics, Files), Rewrite(Sources, Options) { |
Fariborz Jahanian | 9b7ab87 | 2012-12-18 23:02:59 +0000 | [diff] [blame] | 42 | Diagnostics->setClient(new IgnoringDiagConsumer, true); |
| 43 | } |
| 44 | |
Fariborz Jahanian | 9b7ab87 | 2012-12-18 23:02:59 +0000 | [diff] [blame] | 45 | FileID createInMemoryFile(StringRef Name, StringRef Content) { |
Benjamin Kramer | b93511a | 2015-10-06 10:23:34 +0000 | [diff] [blame] | 46 | InMemoryFileSystem->addFile(Name, 0, |
| 47 | llvm::MemoryBuffer::getMemBuffer(Content)); |
| 48 | const FileEntry *Entry = Files.getFile(Name); |
Craig Topper | 236bde3 | 2014-05-26 06:21:51 +0000 | [diff] [blame] | 49 | assert(Entry != nullptr); |
Fariborz Jahanian | 9b7ab87 | 2012-12-18 23:02:59 +0000 | [diff] [blame] | 50 | 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 Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 61 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts; |
| 62 | IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics; |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 63 | IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem; |
Fariborz Jahanian | 9b7ab87 | 2012-12-18 23:02:59 +0000 | [diff] [blame] | 64 | FileManager Files; |
| 65 | SourceManager Sources; |
| 66 | Rewriter Rewrite; |
| 67 | }; |
| 68 | |
Dmitri Gribenko | 9e60511 | 2013-11-13 22:16:51 +0000 | [diff] [blame] | 69 | } // end namespace index |
Fariborz Jahanian | 9b7ab87 | 2012-12-18 23:02:59 +0000 | [diff] [blame] | 70 | } // end namespace clang |
| 71 | |
| 72 | #endif |