Home | History | Annotate | Download | only in libclang
      1 //===- CXString.h - Routines for manipulating CXStrings -------------------===//
      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 // This file defines routines for manipulating CXStrings.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H
     15 #define LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H
     16 
     17 #include "clang-c/Index.h"
     18 #include "clang/Basic/LLVM.h"
     19 #include "llvm/ADT/SmallString.h"
     20 #include "llvm/ADT/StringRef.h"
     21 #include "llvm/Support/Compiler.h"
     22 #include <string>
     23 #include <vector>
     24 
     25 namespace clang {
     26 namespace cxstring {
     27 
     28 struct CXStringBuf;
     29 
     30 /// \brief Create a CXString object for an empty "" string.
     31 CXString createEmpty();
     32 
     33 /// \brief Create a CXString object for an NULL string.
     34 ///
     35 /// A NULL string should be used as an "invalid" value in case of errors.
     36 CXString createNull();
     37 
     38 /// \brief Create a CXString object from a nul-terminated C string.  New
     39 /// CXString may contain a pointer to \p String.
     40 ///
     41 /// \p String should not be changed by the caller afterwards.
     42 CXString createRef(const char *String);
     43 
     44 /// \brief Create a CXString object from a nul-terminated C string.  New
     45 /// CXString will contain a copy of \p String.
     46 ///
     47 /// \p String can be changed or freed by the caller.
     48 CXString createDup(const char *String);
     49 
     50 /// \brief Create a CXString object from a StringRef.  New CXString may
     51 /// contain a pointer to the undrelying data of \p String.
     52 ///
     53 /// \p String should not be changed by the caller afterwards.
     54 CXString createRef(StringRef String);
     55 
     56 /// \brief Create a CXString object from a StringRef.  New CXString will
     57 /// contain a copy of \p String.
     58 ///
     59 /// \p String can be changed or freed by the caller.
     60 CXString createDup(StringRef String);
     61 
     62 // Usually std::string is intended to be used as backing storage for CXString.
     63 // In this case, call \c createRef(String.c_str()).
     64 //
     65 // If you need to make a copy, call \c createDup(StringRef(String)).
     66 CXString createRef(std::string String) = delete;
     67 
     68 /// \brief Create a CXString object that is backed by a string buffer.
     69 CXString createCXString(CXStringBuf *buf);
     70 
     71 CXStringSet *createSet(const std::vector<std::string> &Strings);
     72 
     73 /// \brief A string pool used for fast allocation/deallocation of strings.
     74 class CXStringPool {
     75 public:
     76   ~CXStringPool();
     77 
     78   CXStringBuf *getCXStringBuf(CXTranslationUnit TU);
     79 
     80 private:
     81   std::vector<CXStringBuf *> Pool;
     82 
     83   friend struct CXStringBuf;
     84 };
     85 
     86 struct CXStringBuf {
     87   SmallString<128> Data;
     88   CXTranslationUnit TU;
     89 
     90   CXStringBuf(CXTranslationUnit TU) : TU(TU) {}
     91 
     92   /// \brief Return this buffer to the pool.
     93   void dispose();
     94 };
     95 
     96 CXStringBuf *getCXStringBuf(CXTranslationUnit TU);
     97 
     98 /// \brief Returns true if the CXString data is managed by a pool.
     99 bool isManagedByPool(CXString str);
    100 
    101 }
    102 
    103 static inline StringRef getContents(const CXUnsavedFile &UF) {
    104   return StringRef(UF.Contents, UF.Length);
    105 }
    106 }
    107 
    108 #endif
    109 
    110