aboutsummaryrefslogtreecommitdiff
path: root/tools/libclang/CXString.h
blob: ed3ed4ad2d521cca33c52a551b26391bef4193b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//===- CXString.h - Routines for manipulating CXStrings -------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines routines for manipulating CXStrings.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_CXSTRING_H
#define LLVM_CLANG_CXSTRING_H

#include "clang-c/Index.h"
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Compiler.h"
#include <string>
#include <vector>

namespace clang {
namespace cxstring {

struct CXStringBuf;

/// \brief Create a CXString object for an empty "" string.
CXString createEmpty();

/// \brief Create a CXString object for an NULL string.
///
/// A NULL string should be used as an "invalid" value in case of errors.
CXString createNull();

/// \brief Create a CXString object from a nul-terminated C string.  New
/// CXString may contain a pointer to \p String.
///
/// \p String should not be changed by the caller afterwards.
CXString createRef(const char *String);

/// \brief Create a CXString object from a nul-terminated C string.  New
/// CXString will contain a copy of \p String.
///
/// \p String can be changed or freed by the caller.
CXString createDup(const char *String);

/// \brief Create a CXString object from a StringRef.  New CXString may
/// contain a pointer to the undrelying data of \p String.
///
/// \p String should not be changed by the caller afterwards.
CXString createRef(StringRef String);

/// \brief Create a CXString object from a StringRef.  New CXString will
/// contain a copy of \p String.
///
/// \p String can be changed or freed by the caller.
CXString createDup(StringRef String);

// Usually std::string is intended to be used as backing storage for CXString.
// In this case, call \c createRef(String.c_str()).
//
// If you need to make a copy, call \c createDup(StringRef(String)).
CXString createRef(std::string String) LLVM_DELETED_FUNCTION;

/// \brief Create a CXString object that is backed by a string buffer.
CXString createCXString(CXStringBuf *buf);

/// \brief A string pool used for fast allocation/deallocation of strings.
class CXStringPool {
public:
  ~CXStringPool();

  CXStringBuf *getCXStringBuf(CXTranslationUnit TU);

private:
  std::vector<CXStringBuf *> Pool;

  friend struct CXStringBuf;
};

struct CXStringBuf {
  SmallString<128> Data;
  CXTranslationUnit TU;

  CXStringBuf(CXTranslationUnit TU) : TU(TU) {}

  /// \brief Return this buffer to the pool.
  void dispose();
};

CXStringBuf *getCXStringBuf(CXTranslationUnit TU);

/// \brief Returns true if the CXString data is managed by a pool.
bool isManagedByPool(CXString str);

}

static inline StringRef getContents(const CXUnsavedFile &UF) {
  return StringRef(UF.Contents, UF.Length);
}
}

#endif