[cfe-commits] r119322 - in /cfe/trunk/tools/libclang: CXString.cpp CXString.h
Ted Kremenek
kremenek at apple.com
Mon Nov 15 18:03:55 PST 2010
Author: kremenek
Date: Mon Nov 15 20:03:55 2010
New Revision: 119322
URL: http://llvm.org/viewvc/llvm-project?rev=119322&view=rev
Log:
Add CXString.cpp and CXString.h
Added:
cfe/trunk/tools/libclang/CXString.cpp
cfe/trunk/tools/libclang/CXString.h
Added: cfe/trunk/tools/libclang/CXString.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXString.cpp?rev=119322&view=auto
==============================================================================
--- cfe/trunk/tools/libclang/CXString.cpp (added)
+++ cfe/trunk/tools/libclang/CXString.cpp Mon Nov 15 20:03:55 2010
@@ -0,0 +1,67 @@
+//===- CXString.cpp - 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. It should be the
+// only file that has internal knowledge of the encoding of the data in
+// CXStrings.
+//
+//===----------------------------------------------------------------------===//
+
+#include "CXString.h"
+#include "clang/Frontend/ASTUnit.h"
+#include "clang-c/Index.h"
+#include "llvm/Support/ErrorHandling.h"
+
+using namespace clang;
+using namespace clang::cxstring;
+
+enum CXStringFlag { CXS_Unmanaged, CXS_Malloc };
+
+CXString cxstring::createCXString(const char *String, bool DupString){
+ CXString Str;
+ if (DupString) {
+ Str.Spelling = strdup(String);
+ Str.private_flags = (unsigned) CXS_Malloc;
+ } else {
+ Str.Spelling = String;
+ Str.private_flags = (unsigned) CXS_Unmanaged;
+ }
+ return Str;
+}
+
+CXString cxstring::createCXString(llvm::StringRef String, bool DupString) {
+ CXString Result;
+ if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
+ char *Spelling = (char *)malloc(String.size() + 1);
+ memmove(Spelling, String.data(), String.size());
+ Spelling[String.size()] = 0;
+ Result.Spelling = Spelling;
+ Result.private_flags = (unsigned) CXS_Malloc;
+ } else {
+ Result.Spelling = String.data();
+ Result.private_flags = (unsigned) CXS_Unmanaged;
+ }
+ return Result;
+}
+
+//===----------------------------------------------------------------------===//
+// libClang public APIs.
+//===----------------------------------------------------------------------===//
+
+extern "C" {
+const char *clang_getCString(CXString string) {
+ return string.Spelling;
+}
+
+void clang_disposeString(CXString string) {
+ if (string.private_flags == CXS_Malloc && string.Spelling)
+ free((void*)string.Spelling);
+}
+} // end: extern "C"
+
Added: cfe/trunk/tools/libclang/CXString.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXString.h?rev=119322&view=auto
==============================================================================
--- cfe/trunk/tools/libclang/CXString.h (added)
+++ cfe/trunk/tools/libclang/CXString.h Mon Nov 15 20:03:55 2010
@@ -0,0 +1,33 @@
+//===- 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 "llvm/ADT/StringRef.h"
+
+namespace clang {
+namespace cxstring {
+
+/// \brief Create a CXString object from a C string.
+CXString createCXString(const char *String, bool DupString = false);
+
+/// \brief Create a CXString ojbect from a StringRef.
+CXString createCXString(llvm::StringRef String, bool DupString = true);
+
+}
+}
+
+#endif
+
More information about the cfe-commits
mailing list