blob: 72ac0cf46914a5dbebcf81074d2d6ea967035328 [file] [log] [blame]
Ted Kremenekea834df2010-11-16 02:03:55 +00001//===- 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 Hines176edba2014-12-01 14:53:08 -080014#ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H
15#define LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H
Ted Kremenekea834df2010-11-16 02:03:55 +000016
17#include "clang-c/Index.h"
Chris Lattner686775d2011-07-20 06:58:45 +000018#include "clang/Basic/LLVM.h"
Ted Kremeneka60ed472010-11-16 08:15:36 +000019#include "llvm/ADT/SmallString.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000020#include "llvm/ADT/StringRef.h"
Dmitri Gribenko5595ded2013-02-02 02:19:29 +000021#include "llvm/Support/Compiler.h"
Dmitri Gribenko5595ded2013-02-02 02:19:29 +000022#include <string>
Stephen Hines651f13c2014-04-23 16:59:28 -070023#include <vector>
Ted Kremenekea834df2010-11-16 02:03:55 +000024
25namespace clang {
26namespace cxstring {
Dmitri Gribenko9c48d162013-01-26 22:44:19 +000027
28struct CXStringBuf;
Ted Kremenekea834df2010-11-16 02:03:55 +000029
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +000030/// \brief Create a CXString object for an empty "" string.
31CXString createEmpty();
32
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +000033/// \brief Create a CXString object for an NULL string.
Dmitri Gribenko15a2aa02013-02-01 16:38:41 +000034///
35/// A NULL string should be used as an "invalid" value in case of errors.
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +000036CXString createNull();
37
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +000038/// \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.
42CXString 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.
48CXString createDup(const char *String);
Ted Kremenekea834df2010-11-16 02:03:55 +000049
Dmitri Gribenko5595ded2013-02-02 02:19:29 +000050/// \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.
54CXString 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.
60CXString 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 Hines0e2c34f2015-03-23 12:09:02 -070066CXString createRef(std::string String) = delete;
Ted Kremeneka60ed472010-11-16 08:15:36 +000067
68/// \brief Create a CXString object that is backed by a string buffer.
69CXString createCXString(CXStringBuf *buf);
70
Dmitri Gribenko9c48d162013-01-26 22:44:19 +000071/// \brief A string pool used for fast allocation/deallocation of strings.
72class CXStringPool {
73public:
74 ~CXStringPool();
Ted Kremeneka60ed472010-11-16 08:15:36 +000075
Dmitri Gribenko9c48d162013-01-26 22:44:19 +000076 CXStringBuf *getCXStringBuf(CXTranslationUnit TU);
77
78private:
79 std::vector<CXStringBuf *> Pool;
80
81 friend struct CXStringBuf;
82};
83
84struct 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 Kremeneka60ed472010-11-16 08:15:36 +000094CXStringBuf *getCXStringBuf(CXTranslationUnit TU);
Ted Kremenekea834df2010-11-16 02:03:55 +000095
Ted Kremenekbaf82b02011-08-17 22:19:53 +000096/// \brief Returns true if the CXString data is managed by a pool.
97bool isManagedByPool(CXString str);
98
Ted Kremenekea834df2010-11-16 02:03:55 +000099}
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700100
101static inline StringRef getContents(const CXUnsavedFile &UF) {
102 return StringRef(UF.Contents, UF.Length);
103}
Ted Kremenekea834df2010-11-16 02:03:55 +0000104}
105
106#endif
107