[f57faf6f] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
| 7 | // CodeLocationTools.cpp -- Additional tools for code locations.
|
---|
| 8 | //
|
---|
| 9 | // Author : Andrew Beach
|
---|
| 10 | // Created On : Fri Dec 4 15:42:00 2020
|
---|
| 11 | // Last Modified By : Andrew Beach
|
---|
| 12 | // Last Modified On : Mon Dec 7 15:15:00 2020
|
---|
| 13 | // Update Count : 0
|
---|
| 14 | //
|
---|
| 15 |
|
---|
| 16 | #include "CodeLocationTools.hpp"
|
---|
| 17 |
|
---|
| 18 | #include <type_traits>
|
---|
| 19 |
|
---|
| 20 | #include "AST/Pass.hpp"
|
---|
| 21 | #include "AST/TranslationUnit.hpp"
|
---|
| 22 | #include "Common/CodeLocation.h"
|
---|
| 23 |
|
---|
| 24 | namespace {
|
---|
| 25 |
|
---|
| 26 | template<typename node_t>
|
---|
| 27 | struct has_code_location : public std::is_base_of<ast::ParseNode, node_t> {};
|
---|
| 28 |
|
---|
| 29 | template<typename node_t, bool has_location>
|
---|
| 30 | struct __GetCL;
|
---|
| 31 |
|
---|
| 32 | template<typename node_t>
|
---|
| 33 | struct __GetCL<node_t, true> {
|
---|
[c9e0991] | 34 | static inline CodeLocation const * get( node_t const * node ) {
|
---|
| 35 | return &node->location;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | static inline CodeLocation * get( node_t * node ) {
|
---|
[f57faf6f] | 39 | return &node->location;
|
---|
| 40 | }
|
---|
| 41 | };
|
---|
| 42 |
|
---|
| 43 | template<typename node_t>
|
---|
| 44 | struct __GetCL<node_t, false> {
|
---|
[c9e0991] | 45 | static inline CodeLocation * get( node_t const * ) {
|
---|
[f57faf6f] | 46 | return nullptr;
|
---|
| 47 | }
|
---|
| 48 | };
|
---|
| 49 |
|
---|
| 50 | template<typename node_t>
|
---|
| 51 | CodeLocation const * get_code_location( node_t const * node ) {
|
---|
| 52 | return __GetCL< node_t, has_code_location< node_t >::value >::get( node );
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[c9e0991] | 55 | template<typename node_t>
|
---|
| 56 | CodeLocation * get_code_location( node_t * node ) {
|
---|
| 57 | return __GetCL< node_t, has_code_location< node_t >::value >::get( node );
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | // Fill every location with a nearby (parent) location.
|
---|
| 61 | class FillCore : public ast::WithGuards {
|
---|
| 62 | CodeLocation const * parent;
|
---|
| 63 | public:
|
---|
| 64 | FillCore() : parent( nullptr ) {}
|
---|
| 65 |
|
---|
| 66 | template<typename node_t>
|
---|
| 67 | node_t const * previsit( node_t const * node ) {
|
---|
| 68 | GuardValue( parent );
|
---|
| 69 | CodeLocation const * location = get_code_location( node );
|
---|
| 70 | if ( location && location->isUnset() ) {
|
---|
| 71 | assert( parent );
|
---|
| 72 | node_t * newNode = ast::mutate( node );
|
---|
| 73 | CodeLocation * newLocation = get_code_location( newNode );
|
---|
| 74 | assert( newLocation );
|
---|
| 75 | *newLocation = *parent;
|
---|
| 76 | parent = newLocation;
|
---|
| 77 | return newNode;
|
---|
| 78 | } else if ( location ) {
|
---|
| 79 | parent = location;
|
---|
| 80 | }
|
---|
| 81 | return node;
|
---|
| 82 | }
|
---|
| 83 | };
|
---|
| 84 |
|
---|
[f57faf6f] | 85 | // Collect pointers to all the nodes with unset code locations.
|
---|
| 86 | class CollectCore {
|
---|
| 87 | std::list< ast::ptr< ast::Node > > & unset;
|
---|
| 88 | public:
|
---|
| 89 | CollectCore( std::list< ast::ptr< ast::Node > > & unset ) :
|
---|
| 90 | unset( unset )
|
---|
| 91 | {}
|
---|
| 92 |
|
---|
| 93 | template<typename node_t>
|
---|
| 94 | void previsit( node_t const * node ) {
|
---|
| 95 | CodeLocation const * location = get_code_location( node );
|
---|
| 96 | if ( location && location->isUnset() ) {
|
---|
| 97 | unset.push_back( node );
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | };
|
---|
| 101 |
|
---|
| 102 | } // namespace
|
---|
| 103 |
|
---|
| 104 | void checkAllCodeLocations( ast::TranslationUnit const & unit ) {
|
---|
| 105 | std::list< ast::ptr< ast::Node > > unsetNodes;
|
---|
| 106 | {
|
---|
| 107 | ast::Pass<CollectCore> collector( unsetNodes );
|
---|
| 108 | for ( auto node : unit.decls ) {
|
---|
| 109 | node->accept( collector );
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | if ( unsetNodes.empty() ) {
|
---|
| 113 | return;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | std::cerr << "Total nodes without a set code location: "
|
---|
| 117 | << unsetNodes.size() << std::endl;
|
---|
| 118 |
|
---|
| 119 | assert( unsetNodes.empty() );
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | void forceFillCodeLocations( ast::TranslationUnit & unit ) {
|
---|
| 123 | ast::Pass<FillCore>::run( unit );
|
---|
| 124 | }
|
---|