diff options
author | Fariborz Jahanian <fjahanian@apple.com> | 2012-12-18 23:02:59 +0000 |
---|---|---|
committer | Fariborz Jahanian <fjahanian@apple.com> | 2012-12-18 23:02:59 +0000 |
commit | 88b9521364735a6c9a7ccd23c5bd19d81a80cdd3 (patch) | |
tree | 337bba80c937259ad37d0e7a27acc11706e708e5 /tools/libclang/SimpleFormatContext.h | |
parent | 813bc7faa74bda8162db3dede57e07bfe764e86e (diff) |
This is the libclang patch providing minimal API to
use clang's formatter. Currently, formatter is used
to format declaration tags for xml comments. Since formatter
is in flux and its change will break several of the clang comment
tests, only a single tests is formatted using this facility.
Doug has reviewed and approved it for check-in.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@170467 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/libclang/SimpleFormatContext.h')
-rw-r--r-- | tools/libclang/SimpleFormatContext.h | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/tools/libclang/SimpleFormatContext.h b/tools/libclang/SimpleFormatContext.h new file mode 100644 index 0000000000..f322e63d06 --- /dev/null +++ b/tools/libclang/SimpleFormatContext.h @@ -0,0 +1,73 @@ +//===--- SimpleFormatContext.h ----------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines a utility class for Rewriter related tests. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_SIMPLE_FORM_CONTEXT_H +#define LLVM_CLANG_SIMPLE_FORM_CONTEXT_H + +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/DiagnosticOptions.h" +#include "clang/Basic/FileManager.h" +#include "clang/Basic/LangOptions.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Rewrite/Core/Rewriter.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/raw_ostream.h" + +namespace clang { + +/// \brief 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. +class SimpleFormatContext { + public: + SimpleFormatContext(LangOptions Options) + : DiagOpts(new DiagnosticOptions()), + Diagnostics(new DiagnosticsEngine(new DiagnosticIDs, + DiagOpts.getPtr())), + Files((FileSystemOptions())), + Sources(*Diagnostics, Files), + Rewrite(Sources, Options) { + Diagnostics->setClient(new IgnoringDiagConsumer, true); + } + + ~SimpleFormatContext() { } + + FileID createInMemoryFile(StringRef Name, StringRef Content) { + const llvm::MemoryBuffer *Source = + llvm::MemoryBuffer::getMemBuffer(Content); + const FileEntry *Entry = + Files.getVirtualFile(Name, Source->getBufferSize(), 0); + Sources.overrideFileContents(Entry, Source, true); + assert(Entry != NULL); + return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User); + } + + std::string getRewrittenText(FileID ID) { + std::string Result; + llvm::raw_string_ostream OS(Result); + Rewrite.getEditBuffer(ID).write(OS); + OS.flush(); + return Result; + } + + llvm::IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts; + llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics; + FileManager Files; + SourceManager Sources; + Rewriter Rewrite; +}; + +} // end namespace clang + +#endif |