clang API Documentation

SimpleFormatContext.h
Go to the documentation of this file.
00001 //===--- SimpleFormatContext.h ----------------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 /// \file
00011 ///
00012 /// \brief Defines a utility class for use of clang-format in libclang
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
00017 #define LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
00018 
00019 #include "clang/Basic/Diagnostic.h"
00020 #include "clang/Basic/DiagnosticOptions.h"
00021 #include "clang/Basic/FileManager.h"
00022 #include "clang/Basic/LangOptions.h"
00023 #include "clang/Basic/SourceManager.h"
00024 #include "clang/Rewrite/Core/Rewriter.h"
00025 #include "llvm/Support/FileSystem.h"
00026 #include "llvm/Support/Path.h"
00027 #include "llvm/Support/raw_ostream.h"
00028 
00029 namespace clang {
00030 namespace index {
00031 
00032 /// \brief A small class to be used by libclang clients to format
00033 /// a declaration string in memory. This object is instantiated once
00034 /// and used each time a formatting is needed.
00035 class SimpleFormatContext {
00036 public:
00037   SimpleFormatContext(LangOptions Options)
00038       : DiagOpts(new DiagnosticOptions()),
00039         Diagnostics(new DiagnosticsEngine(new DiagnosticIDs,
00040                                           DiagOpts.get())),
00041         Files((FileSystemOptions())),
00042         Sources(*Diagnostics, Files),
00043         Rewrite(Sources, Options) {
00044     Diagnostics->setClient(new IgnoringDiagConsumer, true);
00045   }
00046 
00047   ~SimpleFormatContext() { }
00048 
00049   FileID createInMemoryFile(StringRef Name, StringRef Content) {
00050     std::unique_ptr<llvm::MemoryBuffer> Source =
00051         llvm::MemoryBuffer::getMemBuffer(Content);
00052     const FileEntry *Entry =
00053         Files.getVirtualFile(Name, Source->getBufferSize(), 0);
00054     Sources.overrideFileContents(Entry, std::move(Source));
00055     assert(Entry != nullptr);
00056     return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
00057   }
00058 
00059   std::string getRewrittenText(FileID ID) {
00060     std::string Result;
00061     llvm::raw_string_ostream OS(Result);
00062     Rewrite.getEditBuffer(ID).write(OS);
00063     OS.flush();
00064     return Result;
00065   }
00066 
00067   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
00068   IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
00069   FileManager Files;
00070   SourceManager Sources;
00071   Rewriter Rewrite;
00072 };
00073 
00074 } // end namespace index
00075 } // end namespace clang
00076 
00077 #endif