From c545e58b52040d9e8e0f16179e56c46ed6026524 Mon Sep 17 00:00:00 2001 From: Dodji Seketeli Date: Wed, 16 Apr 2014 17:09:49 +0200 Subject: [PATCH] Add dumping routines for declaration location * include/abg-fwd.h (get_global_scope()): New overload for const decl_base&. Move the other overloads up in the file. (get_translation_unit): Add an overload for decl_base&. Constify the others. (dump_decl_location): Declare new functions. * src/abg-ir.cc (get_global_scope): Define the overload for const decl_base&. Write the other overloads in terms of this one. (get_translation_unit): Likewise, define the overload for const decl_base&. Write the other overloads in terms of this one. (dump_decl_location): Define these new overloads. Signed-off-by: Dodji Seketeli --- include/abg-fwd.h | 31 ++++++++++++++++++++----------- src/abg-ir.cc | 28 ++++++++++++++++++++++++---- src/abg-writer.cc | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 15 deletions(-) diff --git a/include/abg-fwd.h b/include/abg-fwd.h index c9f8adf4..9fbb8319 100644 --- a/include/abg-fwd.h +++ b/include/abg-fwd.h @@ -113,14 +113,23 @@ add_decl_to_scope(shared_ptr, scope_decl*); shared_ptr add_decl_to_scope (shared_ptr, shared_ptr); +const global_scope* +get_global_scope(const decl_base&); + +const global_scope* +get_global_scope(const decl_base*); + const global_scope* get_global_scope(const shared_ptr); translation_unit* -get_translation_unit(const shared_ptr); +get_translation_unit(const decl_base&); translation_unit* -get_translation_unit(decl_base*); +get_translation_unit(const decl_base*); + +translation_unit* +get_translation_unit(const shared_ptr); bool is_global_scope(const scope_decl*); @@ -314,15 +323,6 @@ set_member_function_is_virtual(const shared_ptr&, bool); shared_ptr strip_typedef(const shared_ptr); -const global_scope* -get_global_scope(const decl_base* decl); - -translation_unit* -get_translation_unit(decl_base* decl); - -translation_unit* -get_translation_unit(const shared_ptr); - string get_type_name(const shared_ptr); @@ -428,5 +428,14 @@ dump(const shared_ptr, std::ostream&); void dump(const shared_ptr); +void +dump_decl_location(const decl_base&); + +void +dump_decl_location(const decl_base*); + +void +dump_decl_location(const shared_ptr&); + } // end namespace abigail #endif // __ABG_IRFWD_H__ diff --git a/src/abg-ir.cc b/src/abg-ir.cc index 640342d7..5bfca347 100644 --- a/src/abg-ir.cc +++ b/src/abg-ir.cc @@ -1421,18 +1421,28 @@ insert_decl_into_scope(decl_base_sptr decl, /// @return the global scope of the decl, or a null pointer if the /// decl is not yet added to a translation_unit. const global_scope* -get_global_scope(const decl_base* decl) +get_global_scope(const decl_base& decl) { - if (const global_scope* s = dynamic_cast(decl)) + if (const global_scope* s = dynamic_cast(&decl)) return s; - scope_decl* scope = decl ? decl->get_scope() : 0; + scope_decl* scope = decl.get_scope(); while (scope && !dynamic_cast(scope)) scope = scope->get_scope(); return scope ? dynamic_cast (scope) : 0; } +/// return the global scope as seen by a given declaration. +/// +/// @param decl the declaration to consider. +/// +/// @return the global scope of the decl, or a null pointer if the +/// decl is not yet added to a translation_unit. +const global_scope* +get_global_scope(const decl_base* decl) +{return get_global_scope(*decl);} + /// Return the global scope as seen by a given declaration. /// /// @param decl the declaration to consider. @@ -1642,7 +1652,7 @@ types_are_compatible(const decl_base_sptr d1, /// @return the resulting translation unit, or null if the decl is not /// yet added to a translation unit. translation_unit* -get_translation_unit(decl_base* decl) +get_translation_unit(const decl_base& decl) { const global_scope* global = get_global_scope(decl); @@ -1651,6 +1661,16 @@ get_translation_unit(decl_base* decl) return 0; } +/// Return the translation unit a declaration belongs to. +/// +/// @param decl the declaration to consider. +/// +/// @return the resulting translation unit, or null if the decl is not +/// yet added to a translation unit. +translation_unit* +get_translation_unit(const decl_base* decl) +{return decl ? get_translation_unit(*decl) : 0;} + /// Return the translation unit a declaration belongs to. /// /// @param decl the declaration to consider. diff --git a/src/abg-writer.cc b/src/abg-writer.cc index e4395871..7461fc19 100644 --- a/src/abg-writer.cc +++ b/src/abg-writer.cc @@ -2218,5 +2218,50 @@ dump(const translation_unit_sptr t) if (t) dump(*t); } + +/// Serialize the source location of a decl to an output stream for +/// debugging purposes. +/// +/// @param d the declaration to consider. +/// +/// @param o the output stream to serizalize the location to. +void +dump_decl_location(const decl_base& d, ostream& o) +{ + string path; + unsigned line = 0, col = 0; + translation_unit* tu = get_translation_unit(d); + + tu->get_loc_mgr().expand_location(d.get_location(), path, line, col); + o << path << ":" << line << "," << col << "\n"; +} + +/// Serialize the source location of a decl to stderr for debugging +/// purposes. +/// +/// @param d the declaration to consider. +void +dump_decl_location(const decl_base& d) +{dump_decl_location(d, cerr);} + +/// Serialize the source location of a dcl to stderr for debugging +/// purposes. +/// +/// @param d the declaration to consider. +void +dump_decl_location(const decl_base* d) +{ + if (d) + dump_decl_location(*d); +} + +/// Serialize the source location of a dcl to stderr for debugging +/// purposes. +/// +/// @param d the declaration to consider. +void +dump_decl_location(const decl_base_sptr d) +{dump_decl_location(d.get());} + // } //end namespace abigail