CXString.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===- CXString.h - Routines for manipulating CXStrings -------------------===//
  2. //
  3. // 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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines routines for manipulating CXStrings.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H
  13. #define LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H
  14. #include "clang-c/Index.h"
  15. #include "clang/Basic/LLVM.h"
  16. #include "llvm/ADT/SmallString.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/Support/Compiler.h"
  19. #include <string>
  20. #include <vector>
  21. namespace clang {
  22. namespace cxstring {
  23. struct CXStringBuf;
  24. /// Create a CXString object for an empty "" string.
  25. CXString createEmpty();
  26. /// Create a CXString object for an NULL string.
  27. ///
  28. /// A NULL string should be used as an "invalid" value in case of errors.
  29. CXString createNull();
  30. /// Create a CXString object from a nul-terminated C string. New
  31. /// CXString may contain a pointer to \p String.
  32. ///
  33. /// \p String should not be changed by the caller afterwards.
  34. CXString createRef(const char *String);
  35. /// Create a CXString object from a nul-terminated C string. New
  36. /// CXString will contain a copy of \p String.
  37. ///
  38. /// \p String can be changed or freed by the caller.
  39. CXString createDup(const char *String);
  40. /// Create a CXString object from a StringRef. New CXString may
  41. /// contain a pointer to the undrelying data of \p String.
  42. ///
  43. /// \p String should not be changed by the caller afterwards.
  44. CXString createRef(StringRef String);
  45. /// Create a CXString object from a StringRef. New CXString will
  46. /// contain a copy of \p String.
  47. ///
  48. /// \p String can be changed or freed by the caller.
  49. CXString createDup(StringRef String);
  50. // Usually std::string is intended to be used as backing storage for CXString.
  51. // In this case, call \c createRef(String.c_str()).
  52. //
  53. // If you need to make a copy, call \c createDup(StringRef(String)).
  54. CXString createRef(std::string String) = delete;
  55. /// Create a CXString object that is backed by a string buffer.
  56. CXString createCXString(CXStringBuf *buf);
  57. CXStringSet *createSet(const std::vector<std::string> &Strings);
  58. /// A string pool used for fast allocation/deallocation of strings.
  59. class CXStringPool {
  60. public:
  61. ~CXStringPool();
  62. CXStringBuf *getCXStringBuf(CXTranslationUnit TU);
  63. private:
  64. std::vector<CXStringBuf *> Pool;
  65. friend struct CXStringBuf;
  66. };
  67. struct CXStringBuf {
  68. SmallString<128> Data;
  69. CXTranslationUnit TU;
  70. CXStringBuf(CXTranslationUnit TU) : TU(TU) {}
  71. /// Return this buffer to the pool.
  72. void dispose();
  73. };
  74. CXStringBuf *getCXStringBuf(CXTranslationUnit TU);
  75. /// Returns true if the CXString data is managed by a pool.
  76. bool isManagedByPool(CXString str);
  77. }
  78. static inline StringRef getContents(const CXUnsavedFile &UF) {
  79. return StringRef(UF.Contents, UF.Length);
  80. }
  81. }
  82. #endif