blob: 809bdec3d677ffcf0c170d7621940ebb3f4164c5 [file] [log] [blame]
Ted Kremenek64dddfb2010-11-16 02:03:55 +00001//===- CXString.h - Routines for manipulating CXStrings -------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ted Kremenek64dddfb2010-11-16 02:03:55 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines routines for manipulating CXStrings.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000013#ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H
14#define LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H
Ted Kremenek64dddfb2010-11-16 02:03:55 +000015
16#include "clang-c/Index.h"
Chris Lattner01cf8db2011-07-20 06:58:45 +000017#include "clang/Basic/LLVM.h"
Ted Kremenek91554282010-11-16 08:15:36 +000018#include "llvm/ADT/SmallString.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000019#include "llvm/ADT/StringRef.h"
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +000020#include "llvm/Support/Compiler.h"
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +000021#include <string>
Chandler Carruth5553d0d2014-01-07 11:51:46 +000022#include <vector>
Ted Kremenek64dddfb2010-11-16 02:03:55 +000023
24namespace clang {
25namespace cxstring {
Dmitri Gribenkob95b3f12013-01-26 22:44:19 +000026
27struct CXStringBuf;
Ted Kremenek64dddfb2010-11-16 02:03:55 +000028
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000029/// Create a CXString object for an empty "" string.
Dmitri Gribenko36a6dd02013-02-01 14:21:22 +000030CXString createEmpty();
31
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000032/// Create a CXString object for an NULL string.
Dmitri Gribenko946aca82013-02-01 16:38:41 +000033///
34/// A NULL string should be used as an "invalid" value in case of errors.
Dmitri Gribenkof98dfba2013-02-01 14:13:32 +000035CXString createNull();
36
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000037/// Create a CXString object from a nul-terminated C string. New
Dmitri Gribenko3c66b0b2013-02-02 00:02:12 +000038/// CXString may contain a pointer to \p String.
39///
40/// \p String should not be changed by the caller afterwards.
41CXString createRef(const char *String);
42
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000043/// Create a CXString object from a nul-terminated C string. New
Dmitri Gribenko3c66b0b2013-02-02 00:02:12 +000044/// CXString will contain a copy of \p String.
45///
46/// \p String can be changed or freed by the caller.
47CXString createDup(const char *String);
Ted Kremenek64dddfb2010-11-16 02:03:55 +000048
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000049/// Create a CXString object from a StringRef. New CXString may
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +000050/// contain a pointer to the undrelying data of \p String.
51///
52/// \p String should not be changed by the caller afterwards.
53CXString createRef(StringRef String);
54
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000055/// Create a CXString object from a StringRef. New CXString will
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +000056/// contain a copy of \p String.
57///
58/// \p String can be changed or freed by the caller.
59CXString createDup(StringRef String);
60
61// Usually std::string is intended to be used as backing storage for CXString.
62// In this case, call \c createRef(String.c_str()).
63//
64// If you need to make a copy, call \c createDup(StringRef(String)).
Aaron Ballmanabc18922015-02-15 22:54:08 +000065CXString createRef(std::string String) = delete;
Ted Kremenek91554282010-11-16 08:15:36 +000066
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000067/// Create a CXString object that is backed by a string buffer.
Ted Kremenek91554282010-11-16 08:15:36 +000068CXString createCXString(CXStringBuf *buf);
69
Saleem Abdulrasool5d92eae2015-11-12 03:57:16 +000070CXStringSet *createSet(const std::vector<std::string> &Strings);
71
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000072/// A string pool used for fast allocation/deallocation of strings.
Dmitri Gribenkob95b3f12013-01-26 22:44:19 +000073class CXStringPool {
74public:
75 ~CXStringPool();
Ted Kremenek91554282010-11-16 08:15:36 +000076
Dmitri Gribenkob95b3f12013-01-26 22:44:19 +000077 CXStringBuf *getCXStringBuf(CXTranslationUnit TU);
78
79private:
80 std::vector<CXStringBuf *> Pool;
81
82 friend struct CXStringBuf;
83};
84
85struct CXStringBuf {
86 SmallString<128> Data;
87 CXTranslationUnit TU;
88
89 CXStringBuf(CXTranslationUnit TU) : TU(TU) {}
90
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000091 /// Return this buffer to the pool.
Dmitri Gribenkob95b3f12013-01-26 22:44:19 +000092 void dispose();
93};
94
Ted Kremenek91554282010-11-16 08:15:36 +000095CXStringBuf *getCXStringBuf(CXTranslationUnit TU);
Ted Kremenek64dddfb2010-11-16 02:03:55 +000096
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000097/// Returns true if the CXString data is managed by a pool.
Ted Kremenek5b8ad402011-08-17 22:19:53 +000098bool isManagedByPool(CXString str);
99
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000100}
Alp Toker9d85b182014-07-07 01:23:14 +0000101
102static inline StringRef getContents(const CXUnsavedFile &UF) {
103 return StringRef(UF.Contents, UF.Length);
104}
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000105}
Ted Kremenek64dddfb2010-11-16 02:03:55 +0000106
107#endif
108