From 6a3998002f3374c0b87ec218c9da9bb73958302e Mon Sep 17 00:00:00 2001 From: triton Date: Tue, 8 Oct 2013 01:27:33 +0100 Subject: [PATCH] Refactored and fixed parsing of original source text for declarations. --- src/Parser/Comments.cpp | 13 --------- src/Parser/Parser.cpp | 58 ++++++++++++++++++++++++++++------------- src/Parser/Parser.h | 4 ++- 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/Parser/Comments.cpp b/src/Parser/Comments.cpp index 53cc4558..31c48caf 100644 --- a/src/Parser/Comments.cpp +++ b/src/Parser/Comments.cpp @@ -265,17 +265,4 @@ void Parser::HandleComments(clang::Decl* D, CppSharp::AST::Declaration^ Decl) auto CB = safe_cast(ConvertCommentBlock(FC)); RawComment->FullComment = CB; } - - // 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 = marshalString(DeclText); } diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index b4657248..85b438b2 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -613,9 +613,9 @@ CppSharp::AST::Class^ Parser::WalkRecordCXX(clang::CXXRecordDecl* Record) { auto MD = cast(D); auto Method = WalkMethodCXX(MD); + HandleDeclaration(MD, Method); Method->AccessDecl = AccessDecl; RC->Methods->Add(Method); - HandleComments(MD, Method); break; } case Decl::Field: @@ -626,8 +626,8 @@ CppSharp::AST::Class^ Parser::WalkRecordCXX(clang::CXXRecordDecl* Record) if (Layout) Field->Offset = Layout->getFieldOffset(FD->getFieldIndex()); + HandleDeclaration(FD, Field); RC->Fields->Add(Field); - HandleComments(FD, Field); break; } case Decl::AccessSpec: @@ -642,7 +642,7 @@ CppSharp::AST::Class^ Parser::WalkRecordCXX(clang::CXXRecordDecl* Record) auto range = SourceRange(startLoc, AS->getColonLoc()); HandlePreprocessedEntities(AccessDecl, range, CppSharp::AST::MacroLocation::Unknown); - + HandleDeclaration(AS, AccessDecl); RC->Specifiers->Add(AccessDecl); break; } @@ -1558,6 +1558,7 @@ void Parser::WalkFunction(clang::FunctionDecl* FD, CppSharp::AST::Function^ F, F->Mangled = marshalString(Mangled); SourceLocation ParamStartLoc = FD->getLocStart(); + SourceLocation ResultLoc; auto FTSI = FD->getTypeSourceInfo(); if (FTSI) @@ -1591,6 +1592,7 @@ void Parser::WalkFunction(clang::FunctionDecl* FD, CppSharp::AST::Function^ F, P->QualifiedType = GetQualifiedType(VD->getType(), WalkType(VD->getType(), &PTL)); P->HasDefaultValue = VD->hasDefaultArg(); P->Namespace = NS; + HandleDeclaration(VD, P); F->Parameters->Add(P); @@ -1864,6 +1866,40 @@ void Parser::HandlePreprocessedEntities(CppSharp::AST::Declaration^ Decl, } } +void Parser::HandleOriginalText(clang::Decl* D, CppSharp::AST::Declaration^ Decl) +{ + auto &SM = C->getSourceManager(); + auto &LangOpts = C->getLangOpts(); + + auto Range = clang::CharSourceRange::getTokenRange(D->getSourceRange()); + + bool Invalid; + auto DeclText = clang::Lexer::getSourceText(Range, SM, LangOpts, &Invalid); + + if (!Invalid) + Decl->DebugText = clix::marshalString(DeclText); +} + +void Parser::HandleDeclaration(clang::Decl* D, CppSharp::AST::Declaration^ Decl) +{ + if (Decl->PreprocessedEntities->Count == 0) + { + auto startLoc = GetDeclStartLocation(C.get(), D); + auto endLoc = D->getLocEnd(); + auto range = clang::SourceRange(startLoc, endLoc); + + HandlePreprocessedEntities(Decl, range); + } + + HandleOriginalText(D, Decl); + HandleComments(D, Decl); + + if (const clang::ValueDecl *VD = clang::dyn_cast_or_null(D)) + Decl->IsDependent = VD->getType()->isDependentType(); + + Decl->Access = ConvertToAccess(D->getAccess()); +} + //-----------------------------------// CppSharp::AST::Declaration^ Parser::WalkDeclarationDef(clang::Decl* D) @@ -2065,21 +2101,7 @@ CppSharp::AST::Declaration^ Parser::WalkDeclaration(clang::Decl* D, } }; if (Decl) - { - if (Decl->PreprocessedEntities->Count == 0) - { - auto startLoc = GetDeclStartLocation(C.get(), D); - auto endLoc = D->getLocEnd(); - auto range = clang::SourceRange(startLoc, endLoc); - - HandlePreprocessedEntities(Decl, range); - } - HandleComments(D, Decl); - - if (const ValueDecl *VD = dyn_cast_or_null(D)) - Decl->IsDependent = VD->getType()->isDependentType(); - Decl->Access = ConvertToAccess(D->getAccess()); - } + HandleDeclaration(D, Decl); return Decl; } diff --git a/src/Parser/Parser.h b/src/Parser/Parser.h index b8ebd0b5..1cc1f08c 100644 --- a/src/Parser/Parser.h +++ b/src/Parser/Parser.h @@ -81,7 +81,6 @@ protected: std::string GetDeclMangledName(clang::Decl*, clang::TargetCXXABI, bool IsDependent = false); std::string GetTypeName(const clang::Type*); - void HandleComments(clang::Decl* D, CppSharp::AST::Declaration^); void WalkFunction(clang::FunctionDecl* FD, CppSharp::AST::Function^ F, bool IsDependent = false); void HandlePreprocessedEntities(CppSharp::AST::Declaration^ Decl, clang::SourceRange sourceRange, @@ -95,6 +94,9 @@ protected: CppSharp::AST::DeclarationContext^ GetNamespace(clang::Decl*, clang::DeclContext*); CppSharp::AST::DeclarationContext^ GetNamespace(clang::Decl*); + void HandleDeclaration(clang::Decl* D, CppSharp::AST::Declaration^); + void HandleOriginalText(clang::Decl* D, CppSharp::AST::Declaration^); + void HandleComments(clang::Decl* D, CppSharp::AST::Declaration^); void HandleDiagnostics(ParserResult^ res); int Index;