2012-12-19 07:02:59 +08:00
|
|
|
//===--- SimpleFormatContext.h ----------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-12-20 01:03:46 +08:00
|
|
|
/// \file
|
2012-12-19 09:00:36 +08:00
|
|
|
///
|
2012-12-20 01:03:46 +08:00
|
|
|
/// \brief Defines a utility class for use of clang-format in libclang
|
2012-12-19 07:02:59 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#ifndef LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
|
|
|
|
#define LLVM_CLANG_LIB_INDEX_SIMPLEFORMATCONTEXT_H
|
2012-12-19 07:02:59 +08:00
|
|
|
|
|
|
|
#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 {
|
2013-11-14 06:16:51 +08:00
|
|
|
namespace index {
|
2012-12-19 07:02:59 +08:00
|
|
|
|
|
|
|
/// \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 {
|
2012-12-19 08:01:48 +08:00
|
|
|
public:
|
2012-12-19 07:02:59 +08:00
|
|
|
SimpleFormatContext(LangOptions Options)
|
|
|
|
: DiagOpts(new DiagnosticOptions()),
|
2013-11-14 06:16:51 +08:00
|
|
|
Diagnostics(new DiagnosticsEngine(new DiagnosticIDs,
|
2014-07-05 11:08:06 +08:00
|
|
|
DiagOpts.get())),
|
2012-12-19 07:02:59 +08:00
|
|
|
Files((FileSystemOptions())),
|
|
|
|
Sources(*Diagnostics, Files),
|
|
|
|
Rewrite(Sources, Options) {
|
|
|
|
Diagnostics->setClient(new IgnoringDiagConsumer, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileID createInMemoryFile(StringRef Name, StringRef Content) {
|
2014-08-28 04:03:29 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Source =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(Content);
|
2012-12-19 07:02:59 +08:00
|
|
|
const FileEntry *Entry =
|
2013-11-14 06:16:51 +08:00
|
|
|
Files.getVirtualFile(Name, Source->getBufferSize(), 0);
|
2014-08-28 04:54:45 +08:00
|
|
|
Sources.overrideFileContents(Entry, std::move(Source));
|
2014-05-26 14:21:51 +08:00
|
|
|
assert(Entry != nullptr);
|
2012-12-19 07:02:59 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
|
2012-12-19 07:02:59 +08:00
|
|
|
FileManager Files;
|
|
|
|
SourceManager Sources;
|
|
|
|
Rewriter Rewrite;
|
|
|
|
};
|
|
|
|
|
2013-11-14 06:16:51 +08:00
|
|
|
} // end namespace index
|
2012-12-19 07:02:59 +08:00
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
#endif
|