Ted Kremenek | ea834df | 2010-11-16 02:03:55 +0000 | [diff] [blame] | 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 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 14 | #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H |
| 15 | #define LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H |
Ted Kremenek | ea834df | 2010-11-16 02:03:55 +0000 | [diff] [blame] | 16 | |
| 17 | #include "clang-c/Index.h" |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 18 | #include "clang/Basic/LLVM.h" |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | f59edb9 | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringRef.h" |
Dmitri Gribenko | 5595ded | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Dmitri Gribenko | 5595ded | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 22 | #include <string> |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 23 | #include <vector> |
Ted Kremenek | ea834df | 2010-11-16 02:03:55 +0000 | [diff] [blame] | 24 | |
| 25 | namespace clang { |
| 26 | namespace cxstring { |
Dmitri Gribenko | 9c48d16 | 2013-01-26 22:44:19 +0000 | [diff] [blame] | 27 | |
| 28 | struct CXStringBuf; |
Ted Kremenek | ea834df | 2010-11-16 02:03:55 +0000 | [diff] [blame] | 29 | |
Dmitri Gribenko | dc66adb | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 30 | /// \brief Create a CXString object for an empty "" string. |
| 31 | CXString createEmpty(); |
| 32 | |
Dmitri Gribenko | dad4c1a | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 33 | /// \brief Create a CXString object for an NULL string. |
Dmitri Gribenko | 15a2aa0 | 2013-02-01 16:38:41 +0000 | [diff] [blame] | 34 | /// |
| 35 | /// A NULL string should be used as an "invalid" value in case of errors. |
Dmitri Gribenko | dad4c1a | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 36 | CXString createNull(); |
| 37 | |
Dmitri Gribenko | 0c4394c | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 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); |
Ted Kremenek | ea834df | 2010-11-16 02:03:55 +0000 | [diff] [blame] | 49 | |
Dmitri Gribenko | 5595ded | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 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)). |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 66 | CXString createRef(std::string String) = delete; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 67 | |
| 68 | /// \brief Create a CXString object that is backed by a string buffer. |
| 69 | CXString createCXString(CXStringBuf *buf); |
| 70 | |
Dmitri Gribenko | 9c48d16 | 2013-01-26 22:44:19 +0000 | [diff] [blame] | 71 | /// \brief A string pool used for fast allocation/deallocation of strings. |
| 72 | class CXStringPool { |
| 73 | public: |
| 74 | ~CXStringPool(); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 75 | |
Dmitri Gribenko | 9c48d16 | 2013-01-26 22:44:19 +0000 | [diff] [blame] | 76 | CXStringBuf *getCXStringBuf(CXTranslationUnit TU); |
| 77 | |
| 78 | private: |
| 79 | std::vector<CXStringBuf *> Pool; |
| 80 | |
| 81 | friend struct CXStringBuf; |
| 82 | }; |
| 83 | |
| 84 | struct CXStringBuf { |
| 85 | SmallString<128> Data; |
| 86 | CXTranslationUnit TU; |
| 87 | |
| 88 | CXStringBuf(CXTranslationUnit TU) : TU(TU) {} |
| 89 | |
| 90 | /// \brief Return this buffer to the pool. |
| 91 | void dispose(); |
| 92 | }; |
| 93 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 94 | CXStringBuf *getCXStringBuf(CXTranslationUnit TU); |
Ted Kremenek | ea834df | 2010-11-16 02:03:55 +0000 | [diff] [blame] | 95 | |
Ted Kremenek | baf82b0 | 2011-08-17 22:19:53 +0000 | [diff] [blame] | 96 | /// \brief Returns true if the CXString data is managed by a pool. |
| 97 | bool isManagedByPool(CXString str); |
| 98 | |
Ted Kremenek | ea834df | 2010-11-16 02:03:55 +0000 | [diff] [blame] | 99 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 100 | |
| 101 | static inline StringRef getContents(const CXUnsavedFile &UF) { |
| 102 | return StringRef(UF.Contents, UF.Length); |
| 103 | } |
Ted Kremenek | ea834df | 2010-11-16 02:03:55 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | #endif |
| 107 | |