1ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek//===- CXString.h - Routines for manipulating CXStrings -------------------===//
2ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek//
3ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek//                     The LLVM Compiler Infrastructure
4ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek//
5ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek// This file is distributed under the University of Illinois Open Source
6ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek// License. See LICENSE.TXT for details.
7ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek//
8ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek//===----------------------------------------------------------------------===//
9ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek//
10ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek// This file defines routines for manipulating CXStrings.
11ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek//
12ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek//===----------------------------------------------------------------------===//
13ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek
14ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek#ifndef LLVM_CLANG_CXSTRING_H
15ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek#define LLVM_CLANG_CXSTRING_H
16ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek
17ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek#include "clang-c/Index.h"
18686775deca8b8685eb90801495880e3abdd844c2Chris Lattner#include "clang/Basic/LLVM.h"
19a60ed47da13393796d8552b9fdca12abbb3eea42Ted Kremenek#include "llvm/ADT/SmallString.h"
20f59edb96b2d0bfe612b732f19519ab84bb995bd4Chandler Carruth#include "llvm/ADT/StringRef.h"
215595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko#include "llvm/Support/Compiler.h"
225595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko#include <string>
23651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include <vector>
24ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek
25ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremeneknamespace clang {
26ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremeneknamespace cxstring {
279c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko
289c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenkostruct CXStringBuf;
29ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek
30dc66adb40ee871b4c578ba615f45d82476c7119eDmitri Gribenko/// \brief Create a CXString object for an empty "" string.
31dc66adb40ee871b4c578ba615f45d82476c7119eDmitri GribenkoCXString createEmpty();
32dc66adb40ee871b4c578ba615f45d82476c7119eDmitri Gribenko
33dad4c1a9ac4ef1aa591ac2ef20dc4c30d96f9f2aDmitri Gribenko/// \brief Create a CXString object for an NULL string.
3415a2aa0954bcf38411c14cf67c65e336022a7a1bDmitri Gribenko///
3515a2aa0954bcf38411c14cf67c65e336022a7a1bDmitri Gribenko/// A NULL string should be used as an "invalid" value in case of errors.
36dad4c1a9ac4ef1aa591ac2ef20dc4c30d96f9f2aDmitri GribenkoCXString createNull();
37dad4c1a9ac4ef1aa591ac2ef20dc4c30d96f9f2aDmitri Gribenko
380c4394c7f63008fbf4d335710b34f71afab362a3Dmitri Gribenko/// \brief Create a CXString object from a nul-terminated C string.  New
390c4394c7f63008fbf4d335710b34f71afab362a3Dmitri Gribenko/// CXString may contain a pointer to \p String.
400c4394c7f63008fbf4d335710b34f71afab362a3Dmitri Gribenko///
410c4394c7f63008fbf4d335710b34f71afab362a3Dmitri Gribenko/// \p String should not be changed by the caller afterwards.
420c4394c7f63008fbf4d335710b34f71afab362a3Dmitri GribenkoCXString createRef(const char *String);
430c4394c7f63008fbf4d335710b34f71afab362a3Dmitri Gribenko
440c4394c7f63008fbf4d335710b34f71afab362a3Dmitri Gribenko/// \brief Create a CXString object from a nul-terminated C string.  New
450c4394c7f63008fbf4d335710b34f71afab362a3Dmitri Gribenko/// CXString will contain a copy of \p String.
460c4394c7f63008fbf4d335710b34f71afab362a3Dmitri Gribenko///
470c4394c7f63008fbf4d335710b34f71afab362a3Dmitri Gribenko/// \p String can be changed or freed by the caller.
480c4394c7f63008fbf4d335710b34f71afab362a3Dmitri GribenkoCXString createDup(const char *String);
49ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek
505595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko/// \brief Create a CXString object from a StringRef.  New CXString may
515595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko/// contain a pointer to the undrelying data of \p String.
525595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko///
535595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko/// \p String should not be changed by the caller afterwards.
545595ded882b22d77fdf535bd1a4c6c090110348aDmitri GribenkoCXString createRef(StringRef String);
555595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko
565595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko/// \brief Create a CXString object from a StringRef.  New CXString will
575595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko/// contain a copy of \p String.
585595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko///
595595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko/// \p String can be changed or freed by the caller.
605595ded882b22d77fdf535bd1a4c6c090110348aDmitri GribenkoCXString createDup(StringRef String);
615595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko
625595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko// Usually std::string is intended to be used as backing storage for CXString.
635595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko// In this case, call \c createRef(String.c_str()).
645595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko//
655595ded882b22d77fdf535bd1a4c6c090110348aDmitri Gribenko// If you need to make a copy, call \c createDup(StringRef(String)).
665595ded882b22d77fdf535bd1a4c6c090110348aDmitri GribenkoCXString createRef(std::string String) LLVM_DELETED_FUNCTION;
67a60ed47da13393796d8552b9fdca12abbb3eea42Ted Kremenek
68a60ed47da13393796d8552b9fdca12abbb3eea42Ted Kremenek/// \brief Create a CXString object that is backed by a string buffer.
69a60ed47da13393796d8552b9fdca12abbb3eea42Ted KremenekCXString createCXString(CXStringBuf *buf);
70a60ed47da13393796d8552b9fdca12abbb3eea42Ted Kremenek
719c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko/// \brief A string pool used for fast allocation/deallocation of strings.
729c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenkoclass CXStringPool {
739c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenkopublic:
749c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko  ~CXStringPool();
759c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko
769c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko  CXStringBuf *getCXStringBuf(CXTranslationUnit TU);
779c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko
789c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenkoprivate:
799c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko  std::vector<CXStringBuf *> Pool;
809c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko
819c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko  friend struct CXStringBuf;
829c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko};
839c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko
849c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenkostruct CXStringBuf {
859c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko  SmallString<128> Data;
869c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko  CXTranslationUnit TU;
879c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko
889c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko  CXStringBuf(CXTranslationUnit TU) : TU(TU) {}
899c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko
909c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko  /// \brief Return this buffer to the pool.
919c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko  void dispose();
929c48d16a11872624410ab3a5944edcba0f32818cDmitri Gribenko};
93a60ed47da13393796d8552b9fdca12abbb3eea42Ted Kremenek
94a60ed47da13393796d8552b9fdca12abbb3eea42Ted KremenekCXStringBuf *getCXStringBuf(CXTranslationUnit TU);
95ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek
96baf82b0fdf5c23dff80660add40bb92bd850bba0Ted Kremenek/// \brief Returns true if the CXString data is managed by a pool.
97baf82b0fdf5c23dff80660add40bb92bd850bba0Ted Kremenekbool isManagedByPool(CXString str);
98baf82b0fdf5c23dff80660add40bb92bd850bba0Ted Kremenek
99ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek}
100ef8225444452a1486bd721f3285301fe84643b00Stephen Hines
101ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesstatic inline StringRef getContents(const CXUnsavedFile &UF) {
102ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  return StringRef(UF.Contents, UF.Length);
103ef8225444452a1486bd721f3285301fe84643b00Stephen Hines}
104ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek}
105ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek
106ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek#endif
107ea834df7cd07d67a77e7dd6e48e8db27464be2fdTed Kremenek
108