blob: ed19261187016bb373e60e0f05b576d61b0b7dd5 [file] [log] [blame]
Ted Kremenek64dddfb2010-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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H
15#define LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H
Ted Kremenek64dddfb2010-11-16 02:03:55 +000016
17#include "clang-c/Index.h"
Chris Lattner01cf8db2011-07-20 06:58:45 +000018#include "clang/Basic/LLVM.h"
Ted Kremenek91554282010-11-16 08:15:36 +000019#include "llvm/ADT/SmallString.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000020#include "llvm/ADT/StringRef.h"
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +000021#include "llvm/Support/Compiler.h"
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +000022#include <string>
Chandler Carruth5553d0d2014-01-07 11:51:46 +000023#include <vector>
Ted Kremenek64dddfb2010-11-16 02:03:55 +000024
25namespace clang {
26namespace cxstring {
Dmitri Gribenkob95b3f12013-01-26 22:44:19 +000027
28struct CXStringBuf;
Ted Kremenek64dddfb2010-11-16 02:03:55 +000029
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000030/// Create a CXString object for an empty "" string.
Dmitri Gribenko36a6dd02013-02-01 14:21:22 +000031CXString createEmpty();
32
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000033/// Create a CXString object for an NULL string.
Dmitri Gribenko946aca82013-02-01 16:38:41 +000034///
35/// A NULL string should be used as an "invalid" value in case of errors.
Dmitri Gribenkof98dfba2013-02-01 14:13:32 +000036CXString createNull();
37
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000038/// Create a CXString object from a nul-terminated C string. New
Dmitri Gribenko3c66b0b2013-02-02 00:02:12 +000039/// 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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000044/// Create a CXString object from a nul-terminated C string. New
Dmitri Gribenko3c66b0b2013-02-02 00:02:12 +000045/// 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 Kremenek64dddfb2010-11-16 02:03:55 +000049
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000050/// Create a CXString object from a StringRef. New CXString may
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +000051/// 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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000056/// Create a CXString object from a StringRef. New CXString will
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +000057/// 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)).
Aaron Ballmanabc18922015-02-15 22:54:08 +000066CXString createRef(std::string String) = delete;
Ted Kremenek91554282010-11-16 08:15:36 +000067
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000068/// Create a CXString object that is backed by a string buffer.
Ted Kremenek91554282010-11-16 08:15:36 +000069CXString createCXString(CXStringBuf *buf);
70
Saleem Abdulrasool5d92eae2015-11-12 03:57:16 +000071CXStringSet *createSet(const std::vector<std::string> &Strings);
72
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000073/// A string pool used for fast allocation/deallocation of strings.
Dmitri Gribenkob95b3f12013-01-26 22:44:19 +000074class CXStringPool {
75public:
76 ~CXStringPool();
Ted Kremenek91554282010-11-16 08:15:36 +000077
Dmitri Gribenkob95b3f12013-01-26 22:44:19 +000078 CXStringBuf *getCXStringBuf(CXTranslationUnit TU);
79
80private:
81 std::vector<CXStringBuf *> Pool;
82
83 friend struct CXStringBuf;
84};
85
86struct CXStringBuf {
87 SmallString<128> Data;
88 CXTranslationUnit TU;
89
90 CXStringBuf(CXTranslationUnit TU) : TU(TU) {}
91
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000092 /// Return this buffer to the pool.
Dmitri Gribenkob95b3f12013-01-26 22:44:19 +000093 void dispose();
94};
95
Ted Kremenek91554282010-11-16 08:15:36 +000096CXStringBuf *getCXStringBuf(CXTranslationUnit TU);
Ted Kremenek64dddfb2010-11-16 02:03:55 +000097
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000098/// Returns true if the CXString data is managed by a pool.
Ted Kremenek5b8ad402011-08-17 22:19:53 +000099bool isManagedByPool(CXString str);
100
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000101}
Alp Toker9d85b182014-07-07 01:23:14 +0000102
103static inline StringRef getContents(const CXUnsavedFile &UF) {
104 return StringRef(UF.Contents, UF.Length);
105}
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000106}
Ted Kremenek64dddfb2010-11-16 02:03:55 +0000107
108#endif
109