aboutsummaryrefslogtreecommitdiff
path: root/tools/libclang/CXComment.cpp
diff options
context:
space:
mode:
authorFariborz Jahanian <fjahanian@apple.com>2012-12-18 23:02:59 +0000
committerFariborz Jahanian <fjahanian@apple.com>2012-12-18 23:02:59 +0000
commit88b9521364735a6c9a7ccd23c5bd19d81a80cdd3 (patch)
tree337bba80c937259ad37d0e7a27acc11706e708e5 /tools/libclang/CXComment.cpp
parent813bc7faa74bda8162db3dede57e07bfe764e86e (diff)
This is the libclang patch providing minimal API to
use clang's formatter. Currently, formatter is used to format declaration tags for xml comments. Since formatter is in flux and its change will break several of the clang comment tests, only a single tests is formatted using this facility. Doug has reviewed and approved it for check-in. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@170467 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/libclang/CXComment.cpp')
-rw-r--r--tools/libclang/CXComment.cpp74
1 files changed, 70 insertions, 4 deletions
diff --git a/tools/libclang/CXComment.cpp b/tools/libclang/CXComment.cpp
index 8c8fe74bba..070b8b8207 100644
--- a/tools/libclang/CXComment.cpp
+++ b/tools/libclang/CXComment.cpp
@@ -15,13 +15,17 @@
#include "CXComment.h"
#include "CXCursor.h"
#include "CXString.h"
+#include "SimpleFormatContext.h"
#include "clang/AST/CommentCommandTraits.h"
#include "clang/AST/CommentVisitor.h"
#include "clang/AST/Decl.h"
#include "clang/AST/PrettyPrinter.h"
+#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
+#include "clang/Format/Format.h"
+#include "clang/Lex/Lexer.h"
#include <climits>
using namespace clang;
@@ -856,8 +860,12 @@ public:
CommentASTToXMLConverter(const FullComment *FC,
SmallVectorImpl<char> &Str,
const CommandTraits &Traits,
- const SourceManager &SM) :
- FC(FC), Result(Str), Traits(Traits), SM(SM) { }
+ const SourceManager &SM,
+ SimpleFormatContext &RTC,
+ unsigned FUID) :
+ FC(FC), Result(Str), Traits(Traits), SM(SM),
+ FormatRewriterContext(RTC),
+ FormatInMemoryUniqueId(FUID) { }
// Inline content.
void visitTextComment(const TextComment *C);
@@ -878,6 +886,10 @@ public:
// Helpers.
void appendToResultWithXMLEscaping(StringRef S);
+
+ unsigned getFormatInMemoryUniqueId() { return FormatInMemoryUniqueId; }
+ SimpleFormatContext &getFormatRewriterContext()
+ { return FormatRewriterContext; }
private:
const FullComment *FC;
@@ -887,6 +899,8 @@ private:
const CommandTraits &Traits;
const SourceManager &SM;
+ SimpleFormatContext &FormatRewriterContext;
+ unsigned FormatInMemoryUniqueId;
};
void getSourceTextOfDeclaration(const DeclInfo *ThisDecl,
@@ -900,6 +914,43 @@ void getSourceTextOfDeclaration(const DeclInfo *ThisDecl,
ThisDecl->CurrentDecl->print(OS, PPolicy,
/*Indentation*/0, /*PrintInstantiation*/true);
}
+
+void formatTextOfDeclaration(CommentASTToXMLConverter *C,
+ const DeclInfo *DI,
+ SmallString<128> &Declaration) {
+ // FIXME. This conditional is TEMPORARY. We don't want to break multiple
+ // large tests each time Format.cpp changes. This condition will
+ // go away and formatting will happen for all declarations.
+ if (getenv("LIBCLANG_ACTIVATE_FORMAT")) {
+ SimpleFormatContext &FormatRewriterContext =
+ C->getFormatRewriterContext();
+ // FIXME. formatting API expects null terminated input string.
+ // There might be more efficient way of doing this.
+ std::string StringDecl = Declaration.str();
+
+ // Formatter specific code.
+ // Form a unique in memory buffer name.
+ llvm::SmallString<128> filename;
+ filename += "xmldecl";
+ filename += llvm::utostr(C->getFormatInMemoryUniqueId());
+ filename += ".xd";
+ FileID ID = FormatRewriterContext.createInMemoryFile(filename, StringDecl);
+ SourceLocation Start =
+ FormatRewriterContext.Sources.getLocForStartOfFile(ID).getLocWithOffset(0);
+ unsigned Length = Declaration.size();
+
+ std::vector<CharSourceRange>
+ Ranges(1, CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length)));
+ ASTContext &Context = DI->CurrentDecl->getASTContext();
+ const LangOptions &LangOpts = Context.getLangOpts();
+ Lexer Lex(ID, FormatRewriterContext.Sources.getBuffer(ID),
+ FormatRewriterContext.Sources, LangOpts);
+ tooling::Replacements Replace =
+ reformat(format::getLLVMStyle(), Lex, FormatRewriterContext.Sources, Ranges);
+ applyAllReplacements(Replace, FormatRewriterContext.Rewrite);
+ Declaration = FormatRewriterContext.getRewrittenText(ID);
+ }
+}
} // end unnamed namespace
@@ -1165,7 +1216,9 @@ void CommentASTToXMLConverter::visitFullComment(const FullComment *C) {
Result << "<Declaration>";
SmallString<128> Declaration;
getSourceTextOfDeclaration(DI, Declaration);
+ formatTextOfDeclaration(this, DI, Declaration);
appendToResultWithXMLEscaping(Declaration);
+
Result << "</Declaration>";
}
@@ -1320,12 +1373,25 @@ CXString clang_FullComment_getAsXML(CXComment CXC) {
const FullComment *FC = getASTNodeAs<FullComment>(CXC);
if (!FC)
return createCXString((const char *) 0);
-
+ ASTContext &Context = FC->getDeclInfo()->CurrentDecl->getASTContext();
CXTranslationUnit TU = CXC.TranslationUnit;
SourceManager &SM = static_cast<ASTUnit *>(TU->TUData)->getSourceManager();
+
+ SimpleFormatContext *SFC =
+ static_cast<SimpleFormatContext*>(TU->FormatContext);
+ if (!SFC) {
+ SFC = new SimpleFormatContext(Context.getLangOpts());
+ TU->FormatContext = SFC;
+ }
+ else if ((TU->FormatInMemoryUniqueId % 10) == 0) {
+ delete SFC;
+ SFC = new SimpleFormatContext(Context.getLangOpts());
+ TU->FormatContext = SFC;
+ }
SmallString<1024> XML;
- CommentASTToXMLConverter Converter(FC, XML, getCommandTraits(CXC), SM);
+ CommentASTToXMLConverter Converter(FC, XML, getCommandTraits(CXC), SM,
+ *SFC, TU->FormatInMemoryUniqueId++);
Converter.visit(FC);
return createCXString(XML.str(), /* DupString = */ true);
}