Browse Source

Improved the comments parsing in the new parser with support for raw and full comments.

pull/222/head
triton 11 years ago
parent
commit
6f0b2f729e
  1. 16
      src/CppParser/AST.cpp
  2. 27
      src/CppParser/AST.h
  3. 61
      src/CppParser/Comments.cpp
  4. 4
      src/CppParser/Helpers.h

16
src/CppParser/AST.cpp

@ -72,12 +72,6 @@ PackExpansionType::PackExpansionType() : Type(TypeKind::PackExpansion) {} @@ -72,12 +72,6 @@ PackExpansionType::PackExpansionType() : Type(TypeKind::PackExpansion) {}
BuiltinType::BuiltinType() : CppSharp::CppParser::AST::Type(TypeKind::Builtin) {}
// RawComment
DEF_STRING(RawComment, Text)
DEF_STRING(RawComment, BriefText)
RawComment::RawComment() : FullComment(0) {}
VTableComponent::VTableComponent() : Offset(0), Declaration(0) {}
// VTableLayout
@ -471,4 +465,14 @@ TranslationUnit* ASTContext::FindOrCreateModule(std::string File) @@ -471,4 +465,14 @@ TranslationUnit* ASTContext::FindOrCreateModule(std::string File)
return unit;
}
// Comments
Comment::Comment(CommentKind kind) : Kind(kind) {}
DEF_STRING(RawComment, Text)
DEF_STRING(RawComment, BriefText)
RawComment::RawComment() : FullComment(0) {}
FullComment::FullComment() : Comment(CommentKind::FullComment) {}
} } }

27
src/CppParser/AST.h

@ -710,8 +710,21 @@ struct CS_API ASTContext @@ -710,8 +710,21 @@ struct CS_API ASTContext
#pragma region Comments
#define DECLARE_COMMENT_KIND(kind) \
kind##Type();
enum struct CommentKind
{
FullComment,
};
struct CS_API CS_ABSTRACT Comment
{
Comment(CommentKind kind);
CommentKind Kind;
};
struct CS_API FullComment : public Comment
{
FullComment();
};
enum struct RawCommentKind
{
@ -725,17 +738,19 @@ enum struct RawCommentKind @@ -725,17 +738,19 @@ enum struct RawCommentKind
Merged
};
struct FullComment;
struct CS_API RawComment
{
RawComment();
RawCommentKind Kind;
RawCommentKind RawCommentKind;
STRING(Text)
STRING(BriefText)
CppSharp::CppParser::AST::FullComment* FullComment;
FullComment* FullComment;
};
#pragma region Commands
#pragma endregion
#pragma endregion
} } }

61
src/CppParser/Comments.cpp

@ -14,7 +14,7 @@ using namespace CppSharp::CppParser; @@ -14,7 +14,7 @@ using namespace CppSharp::CppParser;
//-----------------------------------//
static RawCommentKind
ConvertCommentKind(clang::RawComment::CommentKind Kind)
ConvertRawCommentKind(clang::RawComment::CommentKind Kind)
{
using clang::RawComment;
@ -39,7 +39,7 @@ RawComment* Parser::WalkRawComment(const clang::RawComment* RC) @@ -39,7 +39,7 @@ RawComment* Parser::WalkRawComment(const clang::RawComment* RC)
auto &SM = C->getSourceManager();
auto Comment = new RawComment();
Comment->Kind = ConvertCommentKind(RC->getKind());
Comment->RawCommentKind = ConvertRawCommentKind(RC->getKind());
Comment->Text = RC->getRawText(SM);
Comment->BriefText = RC->getBriefText(*AST);
@ -94,20 +94,33 @@ static void HandleBlockCommand(const clang::comments::BlockCommandComment *CK, @@ -94,20 +94,33 @@ static void HandleBlockCommand(const clang::comments::BlockCommandComment *CK,
BC->Arguments->Add(Arg);
}
}
#endif
static Comment^ ConvertCommentBlock(clang::comments::Comment* C)
static Comment* ConvertCommentBlock(clang::comments::Comment* C)
{
using namespace clang;
using clang::comments::Comment;
using namespace clix;
using namespace CppSharp::AST;
// This needs to have an underscore else we get an ICE under VS2012.
Comment^ _Comment;
CppSharp::CppParser::AST::Comment* _Comment = 0;
switch(C->getCommentKind())
{
case Comment::FullCommentKind:
{
auto CK = cast<clang::comments::FullComment>(C);
auto FC = new FullComment();
_Comment = FC;
#if 0
for (auto I = CK->child_begin(), E = CK->child_end(); I != E; ++I)
{
auto Content = ConvertCommentBlock(*I);
FC->Blocks->Add(dynamic_cast<BlockContentComment^>(Content));
}
break;
#endif
}
#if 0
case Comment::BlockCommandCommentKind:
{
auto CK = cast<const clang::comments::BlockCommandComment>(C);
@ -172,18 +185,6 @@ static Comment^ ConvertCommentBlock(clang::comments::Comment* C) @@ -172,18 +185,6 @@ static Comment^ ConvertCommentBlock(clang::comments::Comment* C)
PC->IsWhitespace = CK->isWhitespace();
break;
}
case Comment::FullCommentKind:
{
auto CK = cast<clang::comments::FullComment>(C);
auto FC = new FullComment();
_Comment = FC;
for (auto I = CK->child_begin(), E = CK->child_end(); I != E; ++I)
{
auto Content = ConvertCommentBlock(*I);
FC->Blocks->Add(dynamic_cast<BlockContentComment^>(Content));
}
break;
}
case Comment::HTMLStartTagCommentKind:
{
auto CK = cast<clang::comments::HTMLStartTagComment>(C);
@ -238,6 +239,7 @@ static Comment^ ConvertCommentBlock(clang::comments::Comment* C) @@ -238,6 +239,7 @@ static Comment^ ConvertCommentBlock(clang::comments::Comment* C)
VL->Text = marshalString<E_UTF8>(CK->getText());
break;
}
#endif
case Comment::NoCommentKind: return nullptr;
default:
llvm_unreachable("Unknown comment kind");
@ -246,12 +248,10 @@ static Comment^ ConvertCommentBlock(clang::comments::Comment* C) @@ -246,12 +248,10 @@ static Comment^ ConvertCommentBlock(clang::comments::Comment* C)
assert(_Comment && "Invalid comment instance");
return _Comment;
}
#endif
void Parser::HandleComments(clang::Decl* D, Declaration* Decl)
{
using namespace clang;
using namespace clang::comments;
const clang::RawComment* RC = 0;
if (!(RC = AST->getRawCommentForAnyRedecl(D)))
@ -260,24 +260,9 @@ void Parser::HandleComments(clang::Decl* D, Declaration* Decl) @@ -260,24 +260,9 @@ void Parser::HandleComments(clang::Decl* D, Declaration* Decl)
auto RawComment = WalkRawComment(RC);
Decl->Comment = RawComment;
#if 0
if (FullComment* FC = RC->parse(*AST, &C->getPreprocessor(), D))
if (clang::comments::FullComment* FC = RC->parse(*AST, &C->getPreprocessor(), D))
{
auto CB = static_cast<FullComment^>(ConvertCommentBlock(FC));
auto CB = static_cast<FullComment*>(ConvertCommentBlock(FC));
RawComment->FullComment = CB;
}
#endif
// Debug Text
SourceManager& SM = C->getSourceManager();
const LangOptions& LangOpts = C->getLangOpts();
auto Range = CharSourceRange::getTokenRange(D->getSourceRange());
bool Invalid;
StringRef DeclText = Lexer::getSourceText(Range, SM, LangOpts, &Invalid);
//assert(!Invalid && "Should have a valid location");
if (!Invalid)
Decl->DebugText = DeclText;
}

4
src/CppParser/Helpers.h

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
/************************************************************************
*
* CppSharp
* Licensed under the simplified BSD license. All rights reserved.
* Licensed under the simplified BSD license.
*
************************************************************************/
@ -21,6 +21,8 @@ @@ -21,6 +21,8 @@
#define CS_API
#endif
#define CS_ABSTRACT
/** We use these macros to workaround the lack of good standard C++
* containers/string support in the C# binding backend. */

Loading…
Cancel
Save