From 6f0b2f729e7d3362d83a463e406253db1ecf9d52 Mon Sep 17 00:00:00 2001 From: triton Date: Tue, 8 Apr 2014 18:58:33 +0100 Subject: [PATCH] Improved the comments parsing in the new parser with support for raw and full comments. --- src/CppParser/AST.cpp | 16 ++++++---- src/CppParser/AST.h | 27 +++++++++++++---- src/CppParser/Comments.cpp | 61 ++++++++++++++------------------------ src/CppParser/Helpers.h | 4 ++- 4 files changed, 57 insertions(+), 51 deletions(-) diff --git a/src/CppParser/AST.cpp b/src/CppParser/AST.cpp index 5cca3884..c544e93d 100644 --- a/src/CppParser/AST.cpp +++ b/src/CppParser/AST.cpp @@ -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) 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) {} + } } } \ No newline at end of file diff --git a/src/CppParser/AST.h b/src/CppParser/AST.h index 7395f076..0fe5fd41 100644 --- a/src/CppParser/AST.h +++ b/src/CppParser/AST.h @@ -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 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 } } } \ No newline at end of file diff --git a/src/CppParser/Comments.cpp b/src/CppParser/Comments.cpp index 47ebb4e5..20216881 100644 --- a/src/CppParser/Comments.cpp +++ b/src/CppParser/Comments.cpp @@ -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) 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, 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(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(Content)); + } + break; +#endif + } +#if 0 case Comment::BlockCommandCommentKind: { auto CK = cast(C); @@ -172,18 +185,6 @@ static Comment^ ConvertCommentBlock(clang::comments::Comment* C) PC->IsWhitespace = CK->isWhitespace(); break; } - case Comment::FullCommentKind: - { - auto CK = cast(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(Content)); - } - break; - } case Comment::HTMLStartTagCommentKind: { auto CK = cast(C); @@ -238,6 +239,7 @@ static Comment^ ConvertCommentBlock(clang::comments::Comment* C) VL->Text = marshalString(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) 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) 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(ConvertCommentBlock(FC)); + auto CB = static_cast(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; } diff --git a/src/CppParser/Helpers.h b/src/CppParser/Helpers.h index f1e9d4a7..3e9d1f44 100644 --- a/src/CppParser/Helpers.h +++ b/src/CppParser/Helpers.h @@ -1,7 +1,7 @@ /************************************************************************ * * CppSharp -* Licensed under the simplified BSD license. All rights reserved. +* Licensed under the simplified BSD license. * ************************************************************************/ @@ -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. */