[cfe-commits] r166707 - /cfe/trunk/tools/libclang/CXComment.cpp

Dmitri Gribenko gribozavr at gmail.com
Thu Oct 25 11:28:26 PDT 2012


Author: gribozavr
Date: Thu Oct 25 13:28:26 2012
New Revision: 166707

URL: http://llvm.org/viewvc/llvm-project?rev=166707&view=rev
Log:
Comment to XML conversion: avoid memory allocation while pretty-printing the
declaration.

Modified:
    cfe/trunk/tools/libclang/CXComment.cpp

Modified: cfe/trunk/tools/libclang/CXComment.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXComment.cpp?rev=166707&r1=166706&r2=166707&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXComment.cpp (original)
+++ cfe/trunk/tools/libclang/CXComment.cpp Thu Oct 25 13:28:26 2012
@@ -891,6 +891,19 @@
   const CommandTraits &Traits;
   const SourceManager &SM;
 };
+
+void getSourceTextOfDeclaration(const DeclInfo *ThisDecl,
+                                SmallVectorImpl<char> &Str) {
+  ASTContext &Context = ThisDecl->CurrentDecl->getASTContext();
+  const LangOptions &LangOpts = Context.getLangOpts();
+  llvm::raw_svector_ostream OS(Str);
+  PrintingPolicy PPolicy(LangOpts);
+  PPolicy.SuppressAttributes = true;
+  PPolicy.TerseOutput = true;
+  ThisDecl->CurrentDecl->print(OS, PPolicy,
+                               /*Indentation*/0, /*PrintInstantiation*/true);
+}
+
 } // end unnamed namespace
 
 void CommentASTToXMLConverter::visitTextComment(const TextComment *C) {
@@ -1032,20 +1045,6 @@
   Result << "</Verbatim>";
 }
 
-static std::string getSourceTextOfDeclaration(const DeclInfo *ThisDecl) {
-  
-  ASTContext &Context = ThisDecl->CurrentDecl->getASTContext();
-  const LangOptions &LangOpts = Context.getLangOpts();
-  std::string SStr;
-  llvm::raw_string_ostream S(SStr);
-  PrintingPolicy PPolicy(LangOpts);
-  PPolicy.SuppressAttributes = true;
-  PPolicy.TerseOutput = true;
-  ThisDecl->CurrentDecl->print(S, PPolicy,
-                               /*Indentation*/0, /*PrintInstantiation*/true);
-  return S.str();
-}
-
 void CommentASTToXMLConverter::visitFullComment(const FullComment *C) {
   FullCommentParts Parts(C, Traits);
 
@@ -1164,11 +1163,16 @@
     Result << "<Other><Name>unknown</Name>";
   }
 
+  {
+    // Pretty-print the declaration.
+    Result << "<Declaration>";
+    SmallString<128> Declaration;
+    getSourceTextOfDeclaration(DI, Declaration);
+    appendToResultWithXMLEscaping(Declaration);
+    Result << "</Declaration>";
+  }
+
   bool FirstParagraphIsBrief = false;
-  Result << "<Declaration>";
-  appendToResultWithXMLEscaping(getSourceTextOfDeclaration(DI));
-  Result << "</Declaration>";
-  
   if (Parts.Brief) {
     Result << "<Abstract>";
     visit(Parts.Brief);





More information about the cfe-commits mailing list