Home | History | Annotate | Download | only in libclang
      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_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 
     29 namespace clang {
     30 
     31 /// \brief A small class to be used by libclang clients to format
     32 /// a declaration string in memory. This object is instantiated once
     33 /// and used each time a formatting is needed.
     34 class SimpleFormatContext {
     35 public:
     36   SimpleFormatContext(LangOptions Options)
     37       : DiagOpts(new DiagnosticOptions()),
     38         Diagnostics(new DiagnosticsEngine(new DiagnosticIDs,
     39                                           DiagOpts.getPtr())),
     40         Files((FileSystemOptions())),
     41         Sources(*Diagnostics, Files),
     42         Rewrite(Sources, Options) {
     43     Diagnostics->setClient(new IgnoringDiagConsumer, true);
     44   }
     45 
     46   ~SimpleFormatContext() { }
     47 
     48   FileID createInMemoryFile(StringRef Name, StringRef Content) {
     49     const llvm::MemoryBuffer *Source =
     50       llvm::MemoryBuffer::getMemBuffer(Content);
     51     const FileEntry *Entry =
     52       Files.getVirtualFile(Name, Source->getBufferSize(), 0);
     53     Sources.overrideFileContents(Entry, Source, true);
     54     assert(Entry != NULL);
     55     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 
     66   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
     67   IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
     68   FileManager Files;
     69   SourceManager Sources;
     70   Rewriter Rewrite;
     71 };
     72 
     73 } // end namespace clang
     74 
     75 #endif
     76