clang  3.7.0
SimpleFormatContext.h
Go to the documentation of this file.
1 //===--- 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 //
10 /// \file
11 ///
12 /// \brief Defines a utility class for use of clang-format in libclang
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
17 #define LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
18 
19 #include "clang/Basic/Diagnostic.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/raw_ostream.h"
28 
29 namespace clang {
30 namespace index {
31 
32 /// \brief A small class to be used by libclang clients to format
33 /// a declaration string in memory. This object is instantiated once
34 /// and used each time a formatting is needed.
36 public:
38  : DiagOpts(new DiagnosticOptions()),
40  DiagOpts.get())),
43  Rewrite(Sources, Options) {
44  Diagnostics->setClient(new IgnoringDiagConsumer, true);
45  }
46 
47  FileID createInMemoryFile(StringRef Name, StringRef Content) {
48  std::unique_ptr<llvm::MemoryBuffer> Source =
49  llvm::MemoryBuffer::getMemBuffer(Content);
50  const FileEntry *Entry =
51  Files.getVirtualFile(Name, Source->getBufferSize(), 0);
52  Sources.overrideFileContents(Entry, std::move(Source));
53  assert(Entry != nullptr);
55  }
56 
57  std::string getRewrittenText(FileID ID) {
58  std::string Result;
59  llvm::raw_string_ostream OS(Result);
60  Rewrite.getEditBuffer(ID).write(OS);
61  OS.flush();
62  return Result;
63  }
64 
70 };
71 
72 } // end namespace index
73 } // end namespace clang
74 
75 #endif
Implements support for file system lookup, file system caching, and directory search management...
Definition: FileManager.h:115
Defines the clang::FileManager interface and associated types.
IntrusiveRefCntPtr< DiagnosticsEngine > Diagnostics
Defines the SourceManager interface.
RewriteBuffer & getEditBuffer(FileID FID)
Definition: Rewriter.cpp:225
SimpleFormatContext(LangOptions Options)
A small class to be used by libclang clients to format a declaration string in memory. This object is instantiated once and used each time a formatting is needed.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:135
std::string getRewrittenText(FileID ID)
ID
Defines the set of possible language-specific address spaces.
Definition: AddressSpaces.h:27
Defines the clang::LangOptions interface.
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...
The result type of a method or function.
void overrideFileContents(const FileEntry *SourceFile, llvm::MemoryBuffer *Buffer, bool DoNotFree)
Override the contents of the given source file by providing an already-allocated buffer.
const MatchFinder::MatchFinderOptions & Options
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
Options for controlling the compiler diagnostics engine.
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:53
const FileEntry * getVirtualFile(StringRef Filename, off_t Size, time_t ModificationTime)
Retrieve a file entry for a "virtual" file that acts as if there were a file with the given name on d...
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
A diagnostic client that ignores all diagnostics.
Definition: Diagnostic.h:1365
FileID createInMemoryFile(StringRef Name, StringRef Content)
Used for handling and querying diagnostic IDs.
Defines the Diagnostic-related interfaces.
IntrusiveRefCntPtr< DiagnosticOptions > DiagOpts
raw_ostream & write(raw_ostream &Stream) const
Write to Stream the result of applying all changes to the original buffer. Note that it isn't safe to...
Definition: Rewriter.cpp:27
Keeps track of options that affect how file operations are performed.
This class handles loading and caching of source files into memory.