blob: a460863f803e0379a762f955cab6b0ff56d0556a [file] [log] [blame]
Fariborz Jahanian88b95212012-12-18 23:02:59 +00001//===--- 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//
Fariborz Jahanian4547fb52012-12-19 17:03:46 +000010/// \file
Fariborz Jahaniane3ec31a2012-12-19 01:00:36 +000011///
Fariborz Jahanian4547fb52012-12-19 17:03:46 +000012/// \brief Defines a utility class for use of clang-format in libclang
Fariborz Jahanian88b95212012-12-18 23:02:59 +000013//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_SIMPLE_FORM_CONTEXT_H
17#define LLVM_CLANG_SIMPLE_FORM_CONTEXT_H
18
19#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/DiagnosticOptions.h"
21#include "clang/Basic/FileManager.h"
22#include "clang/Basic/LangOptions.h"
23#include "clang/Basic/SourceManager.h"
24#include "clang/Rewrite/Core/Rewriter.h"
25#include "llvm/Support/FileSystem.h"
26#include "llvm/Support/Path.h"
27#include "llvm/Support/raw_ostream.h"
28
29namespace clang {
Dmitri Gribenko86cfda22013-11-13 22:16:51 +000030namespace index {
Fariborz Jahanian88b95212012-12-18 23:02:59 +000031
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.
35class SimpleFormatContext {
Fariborz Jahanian7c106832012-12-19 00:01:48 +000036public:
Fariborz Jahanian88b95212012-12-18 23:02:59 +000037 SimpleFormatContext(LangOptions Options)
38 : DiagOpts(new DiagnosticOptions()),
Dmitri Gribenko86cfda22013-11-13 22:16:51 +000039 Diagnostics(new DiagnosticsEngine(new DiagnosticIDs,
Stephen Hinesc568f1e2014-07-21 00:47:37 -070040 DiagOpts.get())),
Fariborz Jahanian88b95212012-12-18 23:02:59 +000041 Files((FileSystemOptions())),
42 Sources(*Diagnostics, Files),
43 Rewrite(Sources, Options) {
44 Diagnostics->setClient(new IgnoringDiagConsumer, true);
45 }
46
47 ~SimpleFormatContext() { }
48
49 FileID createInMemoryFile(StringRef Name, StringRef Content) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -070050 llvm::MemoryBuffer *Source = llvm::MemoryBuffer::getMemBuffer(Content);
Fariborz Jahanian88b95212012-12-18 23:02:59 +000051 const FileEntry *Entry =
Dmitri Gribenko86cfda22013-11-13 22:16:51 +000052 Files.getVirtualFile(Name, Source->getBufferSize(), 0);
Stephen Hines6bcf27b2014-05-29 04:14:42 -070053 Sources.overrideFileContents(Entry, Source);
54 assert(Entry != nullptr);
Fariborz Jahanian88b95212012-12-18 23:02:59 +000055 return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
56 }
57
58 std::string getRewrittenText(FileID ID) {
59 std::string Result;
60 llvm::raw_string_ostream OS(Result);
61 Rewrite.getEditBuffer(ID).write(OS);
62 OS.flush();
63 return Result;
64 }
65
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +000066 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
67 IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
Fariborz Jahanian88b95212012-12-18 23:02:59 +000068 FileManager Files;
69 SourceManager Sources;
70 Rewriter Rewrite;
71};
72
Dmitri Gribenko86cfda22013-11-13 22:16:51 +000073} // end namespace index
Fariborz Jahanian88b95212012-12-18 23:02:59 +000074} // end namespace clang
75
76#endif