clang  11.0.0git
Go to the documentation of this file.
1 //===--- SimpleFormatContext.h ----------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 ///
11 /// Defines a utility class for use of clang-format in libclang
12 //
13 //===----------------------------------------------------------------------===//
18 #include "clang/Basic/Diagnostic.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/raw_ostream.h"
28 namespace clang {
29 namespace index {
31 /// A small class to be used by libclang clients to format
32 /// a declaration string in memory. This object is instantiated once
33 /// and used each time a formatting is needed.
35 public:
37  : DiagOpts(new DiagnosticOptions()),
41  Sources(*Diagnostics, Files), Rewrite(Sources, Options) {
42  Diagnostics->setClient(new IgnoringDiagConsumer, true);
43  }
45  FileID createInMemoryFile(StringRef Name, StringRef Content) {
46  InMemoryFileSystem->addFile(Name, 0,
47  llvm::MemoryBuffer::getMemBuffer(Content));
48  const FileEntry *Entry = Files.getFile(Name);
49  assert(Entry != nullptr);
51  }
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  }
67 };
69 } // end namespace index
70 } // end namespace clang
72 #endif
Implements support for file system lookup, file system caching, and directory search management...
Definition: FileManager.h:172
Defines the clang::FileManager interface and associated types.
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Definition: Dominators.h:30
FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, int LoadedID=0, unsigned LoadedOffset=0)
Create a new FileID that represents the specified file being #included from the specified IncludePosi...
IntrusiveRefCntPtr< DiagnosticsEngine > Diagnostics
Defines the SourceManager interface.
RewriteBuffer & getEditBuffer(FileID FID)
getEditBuffer - This is like getRewriteBufferFor, but always returns a buffer, and allows you to writ...
Definition: Rewriter.cpp:240
SimpleFormatContext(LangOptions Options)
A small class to be used by libclang clients to format a declaration string in memory.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:54
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:153
Defines the Diagnostic-related interfaces.
IntrusiveRefCntPtr< llvm::vfs::InMemoryFileSystem > InMemoryFileSystem
std::string getRewrittenText(FileID ID)
Defines the clang::LangOptions interface.
Encodes a location in the source.
raw_ostream & write(raw_ostream &Stream) const
Write to Stream the result of applying all changes to the original buffer.
Definition: Rewriter.cpp:37
Options for controlling the compiler diagnostics engine.
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:79
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Dataflow Directional Tag Classes.
A diagnostic client that ignores all diagnostics.
Definition: Diagnostic.h:1581
FileID createInMemoryFile(StringRef Name, StringRef Content)
Used for handling and querying diagnostic IDs.
IntrusiveRefCntPtr< DiagnosticOptions > DiagOpts
llvm::ErrorOr< const FileEntry * > getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
Keeps track of options that affect how file operations are performed.
Rewriter - This is the main interface to the rewrite buffers.
Definition: Rewriter.h:32
This class handles loading and caching of source files into memory.