[cfe-commits] r96423 - in /cfe/trunk/tools/CIndex: CIndex.cpp CIndexDiagnostic.cpp CIndexUSRs.cpp CIndexer.h

Ted Kremenek kremenek at apple.com
Tue Feb 16 16:41:08 PST 2010


Author: kremenek
Date: Tue Feb 16 18:41:08 2010
New Revision: 96423

URL: http://llvm.org/viewvc/llvm-project?rev=96423&view=rev
Log:
Move createCXString() functions out of CIndexer and into the clang::cxstring namespace.
We can much more succinctly refer to these functions this way.

Also change the default behavior of createCXString(StringRef&) to duplicate the
string.  This is almost always what we want.  The other case is where we pass
a constant c-string, which uses the other version of createCXString().

Modified:
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp
    cfe/trunk/tools/CIndex/CIndexUSRs.cpp
    cfe/trunk/tools/CIndex/CIndexer.h

Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=96423&r1=96422&r2=96423&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Tue Feb 16 18:41:08 2010
@@ -33,6 +33,7 @@
 
 using namespace clang;
 using namespace clang::cxcursor;
+using namespace clang::cxstring;
 using namespace idx;
 
 //===----------------------------------------------------------------------===//
@@ -897,33 +898,6 @@
   return VisitExpr(E);
 }
 
-CXString CIndexer::createCXString(const char *String, bool DupString){
-  CXString Str;
-  if (DupString) {
-    Str.Spelling = strdup(String);
-    Str.MustFreeString = 1;
-  } else {
-    Str.Spelling = String;
-    Str.MustFreeString = 0;
-  }
-  return Str;
-}
-
-CXString CIndexer::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.MustFreeString = 1;
-  } else {
-    Result.Spelling = String.data();
-    Result.MustFreeString = 0;
-  }
-  return Result;
-}
-
 extern "C" {
 CXIndex clang_createIndex(int excludeDeclarationsFromPCH) {
   CIndexer *CIdxr = new CIndexer();
@@ -1137,11 +1111,10 @@
 
 CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
   if (!CTUnit)
-    return CIndexer::createCXString("");
+    return createCXString("");
   
   ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
-  return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(),
-                                  true);
+  return createCXString(CXXUnit->getOriginalSourceFileName(), true);
 }
 
 CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
@@ -1338,22 +1311,21 @@
 static CXString getDeclSpelling(Decl *D) {
   NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
   if (!ND)
-    return CIndexer::createCXString("");
+    return createCXString("");
   
   if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
-    return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(),
-                                    true);
+    return createCXString(OMD->getSelector().getAsString());
   
   if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
     // No, this isn't the same as the code below. getIdentifier() is non-virtual
     // and returns different names. NamedDecl returns the class name and
     // ObjCCategoryImplDecl returns the category name.
-    return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart());
+    return createCXString(CIMP->getIdentifier()->getNameStart());
   
   if (ND->getIdentifier())
-    return CIndexer::createCXString(ND->getIdentifier()->getNameStart());
+    return createCXString(ND->getIdentifier()->getNameStart());
   
-  return CIndexer::createCXString("");
+  return createCXString("");
 }
     
 CXString clang_getCursorSpelling(CXCursor C) {
@@ -1364,28 +1336,27 @@
     switch (C.kind) {
     case CXCursor_ObjCSuperClassRef: {
       ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
-      return CIndexer::createCXString(Super->getIdentifier()->getNameStart());
+      return createCXString(Super->getIdentifier()->getNameStart());
     }
     case CXCursor_ObjCClassRef: {
       ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
-      return CIndexer::createCXString(Class->getIdentifier()->getNameStart());
+      return createCXString(Class->getIdentifier()->getNameStart());
     }
     case CXCursor_ObjCProtocolRef: {
       ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
       assert(OID && "getCursorSpelling(): Missing protocol decl");
-      return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
+      return createCXString(OID->getIdentifier()->getNameStart());
     }
     case CXCursor_TypeRef: {
       TypeDecl *Type = getCursorTypeRef(C).first;
       assert(Type && "Missing type decl");
 
-      return CIndexer::createCXString(
-               getCursorContext(C).getTypeDeclType(Type).getAsString().c_str(),
-                                      true);
+      return createCXString(getCursorContext(C).getTypeDeclType(Type).
+                              getAsString());
     }
 
     default:
-      return CIndexer::createCXString("<not implemented>");
+      return createCXString("<not implemented>");
     }
   }
 
@@ -1393,13 +1364,13 @@
     Decl *D = getDeclFromExpr(getCursorExpr(C));
     if (D)
       return getDeclSpelling(D);
-    return CIndexer::createCXString("");
+    return createCXString("");
   }
 
   if (clang_isDeclaration(C.kind))
     return getDeclSpelling(getCursorDecl(C));
   
-  return CIndexer::createCXString("");
+  return createCXString("");
 }
 
 const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
@@ -1920,14 +1891,13 @@
   case CXToken_Identifier:
   case CXToken_Keyword:
     // We know we have an IdentifierInfo*, so use that.
-    return CIndexer::createCXString(
-              static_cast<IdentifierInfo *>(CXTok.ptr_data)->getNameStart());
+    return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
+                            ->getNameStart());
 
   case CXToken_Literal: {
     // We have stashed the starting pointer in the ptr_data field. Use it.
     const char *Text = static_cast<const char *>(CXTok.ptr_data);
-    return CIndexer::createCXString(llvm::StringRef(Text, CXTok.int_data[2]), 
-                                    true);
+    return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
   }
       
   case CXToken_Punctuation:
@@ -1939,7 +1909,7 @@
   // deconstructing the source location.
   ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
   if (!CXXUnit)
-    return CIndexer::createCXString("");
+    return createCXString("");
   
   SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
   std::pair<FileID, unsigned> LocInfo
@@ -1947,9 +1917,8 @@
   std::pair<const char *,const char *> Buffer
     = CXXUnit->getSourceManager().getBufferData(LocInfo.first);
 
-  return CIndexer::createCXString(llvm::StringRef(Buffer.first+LocInfo.second,
-                                                  CXTok.int_data[2]), 
-                                  true);
+  return createCXString(llvm::StringRef(Buffer.first+LocInfo.second,
+                                        CXTok.int_data[2]));
 }
  
 CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
@@ -2154,6 +2123,35 @@
 
 } // end: extern "C"
 
+namespace clang { namespace cxstring {
+CXString createCXString(const char *String, bool DupString){
+  CXString Str;
+  if (DupString) {
+    Str.Spelling = strdup(String);
+    Str.MustFreeString = 1;
+  } else {
+    Str.Spelling = String;
+    Str.MustFreeString = 0;
+  }
+  return Str;
+}
+
+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.MustFreeString = 1;
+  } else {
+    Result.Spelling = String.data();
+    Result.MustFreeString = 0;
+  }
+  return Result;
+}
+}}
+
 //===----------------------------------------------------------------------===//
 // Misc. utility functions.
 //===----------------------------------------------------------------------===//
@@ -2161,8 +2159,7 @@
 extern "C" {
 
 CXString clang_getClangVersion() {
-  return CIndexer::createCXString(getClangFullVersion(), true);
+  return createCXString(getClangFullVersion());
 }
 
 } // end: extern "C"
-

Modified: cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp?rev=96423&r1=96422&r2=96423&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp Tue Feb 16 18:41:08 2010
@@ -19,26 +19,27 @@
 
 using namespace clang;
 using namespace clang::cxloc;
+using namespace clang::cxstring;
 
 //-----------------------------------------------------------------------------
-// Opaque data structures                        
+// Opaque data structures
 //-----------------------------------------------------------------------------
 namespace {
   /// \brief The storage behind a CXDiagnostic
   struct CXStoredDiagnostic {
     /// \brief The translation unit this diagnostic came from.
     const LangOptions *LangOptsPtr;
-    
+
     /// \brief The severity level of this diagnostic.
     Diagnostic::Level Level;
-    
+
     /// \brief A reference to the diagnostic information.
     const DiagnosticInfo &Info;
   };
 }
 
 //-----------------------------------------------------------------------------
-// CIndex Diagnostic Client                        
+// CIndex Diagnostic Client
 //-----------------------------------------------------------------------------
 CIndexDiagnosticClient::~CIndexDiagnosticClient() { }
 
@@ -65,15 +66,15 @@
 }
 
 //-----------------------------------------------------------------------------
-// C Interface Routines                        
+// C Interface Routines
 //-----------------------------------------------------------------------------
 extern "C" {
-  
+
 enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
   if (!StoredDiag)
     return CXDiagnostic_Ignored;
-  
+
   switch (StoredDiag->Level) {
   case Diagnostic::Ignored: return CXDiagnostic_Ignored;
   case Diagnostic::Note:    return CXDiagnostic_Note;
@@ -81,16 +82,16 @@
   case Diagnostic::Error:   return CXDiagnostic_Error;
   case Diagnostic::Fatal:   return CXDiagnostic_Fatal;
   }
- 
+
   llvm_unreachable("Invalid diagnostic level");
   return CXDiagnostic_Ignored;
 }
-  
+
 CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
   if (!StoredDiag || StoredDiag->Info.getLocation().isInvalid())
     return clang_getNullLocation();
-  
+
   return translateSourceLocation(StoredDiag->Info.getLocation().getManager(),
                                  *StoredDiag->LangOptsPtr,
                                  StoredDiag->Info.getLocation());
@@ -99,27 +100,27 @@
 CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
   if (!StoredDiag)
-    return CIndexer::createCXString("");
-  
+    return createCXString("");
+
   llvm::SmallString<64> Spelling;
   StoredDiag->Info.FormatDiagnostic(Spelling);
-  return CIndexer::createCXString(Spelling.str(), true);
+  return createCXString(Spelling.str(), true);
 }
 
 unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
   if (!StoredDiag || StoredDiag->Info.getLocation().isInvalid())
     return 0;
-  
+
   return StoredDiag->Info.getNumRanges();
 }
-  
+
 CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
-  if (!StoredDiag || Range >= StoredDiag->Info.getNumRanges() || 
+  if (!StoredDiag || Range >= StoredDiag->Info.getNumRanges() ||
       StoredDiag->Info.getLocation().isInvalid())
     return clang_getNullRange();
-  
+
   return translateSourceRange(StoredDiag->Info.getLocation().getManager(),
                               *StoredDiag->LangOptsPtr,
                               StoredDiag->Info.getRange(Range));
@@ -129,27 +130,27 @@
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
   if (!StoredDiag)
     return 0;
-  
+
   return StoredDiag->Info.getNumCodeModificationHints();
 }
 
-enum CXFixItKind clang_getDiagnosticFixItKind(CXDiagnostic Diag, 
+enum CXFixItKind clang_getDiagnosticFixItKind(CXDiagnostic Diag,
                                               unsigned FixIt) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
   if (!StoredDiag || FixIt >= StoredDiag->Info.getNumCodeModificationHints())
     return CXFixIt_Insertion;
-  
+
   const CodeModificationHint &Hint
     = StoredDiag->Info.getCodeModificationHint(FixIt);
   if (Hint.RemoveRange.isInvalid())
     return CXFixIt_Insertion;
   if (Hint.InsertionLoc.isInvalid())
     return CXFixIt_Removal;
-  
-  return CXFixIt_Replacement;  
+
+  return CXFixIt_Replacement;
 }
 
-CXString clang_getDiagnosticFixItInsertion(CXDiagnostic Diag, 
+CXString clang_getDiagnosticFixItInsertion(CXDiagnostic Diag,
                                            unsigned FixIt,
                                            CXSourceLocation *Location) {
   if (Location)
@@ -157,8 +158,8 @@
 
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
   if (!StoredDiag || FixIt >= StoredDiag->Info.getNumCodeModificationHints())
-    return CIndexer::createCXString("");
-  
+    return createCXString("");
+
   const CodeModificationHint &Hint
     = StoredDiag->Info.getCodeModificationHint(FixIt);
 
@@ -167,16 +168,16 @@
                                     StoredDiag->Info.getLocation().getManager(),
                                         *StoredDiag->LangOptsPtr,
                                         Hint.InsertionLoc);
-  return CIndexer::createCXString(Hint.CodeToInsert);
+  return createCXString(Hint.CodeToInsert);
 }
 
-CXSourceRange clang_getDiagnosticFixItRemoval(CXDiagnostic Diag, 
+CXSourceRange clang_getDiagnosticFixItRemoval(CXDiagnostic Diag,
                                               unsigned FixIt) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
   if (!StoredDiag || FixIt >= StoredDiag->Info.getNumCodeModificationHints() ||
       StoredDiag->Info.getLocation().isInvalid())
     return clang_getNullRange();
-  
+
   const CodeModificationHint &Hint
     = StoredDiag->Info.getCodeModificationHint(FixIt);
   return translateSourceRange(StoredDiag->Info.getLocation().getManager(),
@@ -184,7 +185,7 @@
                               Hint.RemoveRange);
 }
 
-CXString clang_getDiagnosticFixItReplacement(CXDiagnostic Diag, 
+CXString clang_getDiagnosticFixItReplacement(CXDiagnostic Diag,
                                              unsigned FixIt,
                                              CXSourceRange *Range) {
   if (Range)
@@ -195,19 +196,19 @@
       StoredDiag->Info.getLocation().isInvalid()) {
     if (Range)
       *Range = clang_getNullRange();
-    
-    return CIndexer::createCXString("");
+
+    return createCXString("");
   }
-  
+
   const CodeModificationHint &Hint
     = StoredDiag->Info.getCodeModificationHint(FixIt);
   if (Range)
     *Range = translateSourceRange(StoredDiag->Info.getLocation().getManager(),
                                   *StoredDiag->LangOptsPtr,
                                   Hint.RemoveRange);
-  return CIndexer::createCXString(Hint.CodeToInsert);  
+  return createCXString(Hint.CodeToInsert);
 }
-  
+
 } // end extern "C"
 
 void clang::ReportSerializedDiagnostics(const llvm::sys::Path &DiagnosticsPath,
@@ -250,7 +251,7 @@
   StringRef Buffer = F->getBuffer();
   const char *Memory = Buffer.data(), *MemoryEnd = Memory + Buffer.size();
   while (Memory != MemoryEnd) {
-    DiagnosticBuilder DB = Diags.Deserialize(FileMgr, SourceMgr, 
+    DiagnosticBuilder DB = Diags.Deserialize(FileMgr, SourceMgr,
                                              Memory, MemoryEnd);
     if (!DB.isActive())
       return;

Modified: cfe/trunk/tools/CIndex/CIndexUSRs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndexUSRs.cpp?rev=96423&r1=96422&r2=96423&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndexUSRs.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndexUSRs.cpp Tue Feb 16 18:41:08 2010
@@ -17,6 +17,8 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/raw_ostream.h"
 
+using namespace clang::cxstring;
+
 //===----------------------------------------------------------------------===//
 // USR generation.
 //===----------------------------------------------------------------------===//
@@ -156,14 +158,14 @@
     USRGenerator UG(Out);
     UG.Visit(static_cast<Decl*>(D));
     if (UG.ignoreResults())
-      return CIndexer::createCXString(NULL);
+      return createCXString(NULL);
   }
   
   if (StrBuf.empty())
-    return CIndexer::createCXString(NULL);
+    return createCXString(NULL);
   
   // Return a copy of the string that must be disposed by the caller.
-  return CIndexer::createCXString(StrBuf.c_str(), true);
+  return createCXString(StrBuf.str(), true);
 }  
 
 
@@ -173,7 +175,7 @@
   if (Decl *D = cxcursor::getCursorDecl(C))
     return ConstructUSR(D);  
   
-  return CIndexer::createCXString(NULL);
+  return createCXString(NULL);
 }
 
 } // end extern "C"

Modified: cfe/trunk/tools/CIndex/CIndexer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndexer.h?rev=96423&r1=96422&r2=96423&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndexer.h (original)
+++ cfe/trunk/tools/CIndex/CIndexer.h Tue Feb 16 18:41:08 2010
@@ -24,6 +24,13 @@
 
 using namespace clang;
 
+namespace clang {
+namespace cxstring {
+  CXString createCXString(const char *String, bool DupString = false);
+  CXString createCXString(llvm::StringRef String, bool DupString = true);  
+}
+}
+
 class CIndexer {
   bool UseExternalASTGeneration;
   bool OnlyLocalDecls;
@@ -49,10 +56,6 @@
   
   /// \brief Get the path of the clang resource files.
   std::string getClangResourcesPath();
-
-  static CXString createCXString(const char *String, bool DupString = false);
-  static CXString createCXString(llvm::StringRef String, 
-                                 bool DupString = false);
 };
 
 namespace clang {





More information about the cfe-commits mailing list