Changes in / [dc99f38:7d6639d]


Ignore:
Location:
src
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • src/CodeTools/DeclStats.cc

    rdc99f38 r7d6639d  
    1717
    1818#include <iostream>
     19#include <string>
     20#include <unordered_map>
     21#include <unordered_set>
     22
     23#include "Common/VectorMap.h"
     24#include "Parser/LinkageSpec.h"
     25#include "SynTree/Declaration.h"
     26#include "SynTree/Visitor.h"
    1927
    2028namespace CodeTools {
     29       
     30        class DeclStats : public Visitor {
     31                struct Stats {
     32                        unsigned n_decls;     ///< Total number of declarations
     33                        unsigned mono_decls;  ///< Monomorphic declarations
     34                        unsigned poly_decls;  ///< Polymorphic declarations
     35                        /// Count of declarations with each name
     36                        std::unordered_map<std::string, unsigned> by_name;
     37                        /// Count of declarations with each number of parameters
     38                        VectorMap<unsigned> by_params;
     39                        /// Count of declarations with each number of return types
     40                        VectorMap<unsigned> by_returns;
     41
     42                        Stats() : n_decls(0), mono_decls(0), poly_decls(0), by_name() {}
     43
     44                        Stats& operator+= (const Stats& o) {
     45                                n_decls += o.n_decls;
     46                                mono_decls += o.mono_decls;
     47                                poly_decls += o.poly_decls;
     48                               
     49                                for ( auto& entry : o.by_name ) {
     50                                        by_name[ entry.first ] += entry.second;
     51                                }
     52                               
     53                                by_params.reserve( o.by_params.size() );
     54                                for ( unsigned i = 0; i < o.by_params.size(); ++i ) {
     55                                        by_params[i] += o.by_params[i];
     56                                }
     57
     58                                by_returns.reserve( o.by_returns.size() );
     59                                for ( unsigned i = 0; i < o.by_returns.size(); ++i ) {
     60                                        by_returns[i] += o.by_returns[i];
     61                                }
     62
     63                                return *this;
     64                        }
     65                };
     66
     67                Stats for_linkage[LinkageSpec::NoOfSpecs];   ///< Stores separate stats per linkage
     68                std::unordered_set<std::string> seen_names;  ///< Stores manglenames already seen to avoid double-counting
     69                Stats total;
     70
     71        public:
     72                using Visitor::visit;
     73
     74                virtual void visit( FunctionDecl *decl ) {
     75                        // skip if already seen declaration for this function
     76                        const std::string& mangleName = decl->get_mangleName().empty() ? decl->get_name() : decl->get_mangleName();
     77                        if ( ! seen_names.insert( mangleName ).second ) return;
     78                       
     79                        Stats& stats = for_linkage[ decl->get_linkage() ];
     80
     81                        ++stats.n_decls;
     82                        FunctionType* fnTy = decl->get_functionType();
     83                        if ( fnTy->get_forall().empty() ) ++stats.mono_decls; else ++stats.poly_decls;
     84
     85                        ++stats.by_name[ decl->get_name() ];
     86
     87                        unsigned n_params = 0;
     88                        for ( auto pdecl : fnTy->get_parameters() ) { n_params += pdecl->get_type()->size(); }
     89                        ++stats.by_params.at( n_params );
     90
     91                        unsigned n_returns = 0;
     92                        for ( auto rdecl : fnTy->get_returnVals() ) { n_returns += rdecl->get_type()->size(); }
     93                        ++stats.by_returns.at( n_returns );
     94                }
     95
     96        private:
     97                template<typename F>
     98                void printAll( const char* name, F extract ) {
     99                        std::cout << "\"" << name << "\",";
     100                        for ( const auto& stats : for_linkage ) {
     101                                std::cout << "," << extract(stats);
     102                        }
     103                        std::cout << "," << extract(total) << std::endl;
     104                }
     105
     106                template<typename F>
     107                void printAllMap( const char* name, F extract ) {
     108                        for ( const auto& entry : extract(total) ) {
     109                                const auto& key = entry.first;
     110                                std::cout << "\"" << name << "\"," << key;
     111                                for ( const auto& stats : for_linkage ) {
     112                                        const auto& map = extract(stats);
     113                                        auto it = map.find( key );
     114                                        if ( it == map.end() ) std::cout << ",0";
     115                                        else std::cout << "," << it->second;
     116                                }
     117                                std::cout  << "," << entry.second << std::endl;
     118                        }
     119                }
     120
     121                template<typename F>
     122                void printAllHisto( const char* name, F extract ) {
     123                        VectorMap<unsigned> histos[LinkageSpec::NoOfSpecs];
     124                        VectorMap<unsigned> thisto;
     125
     126                        for ( const auto& entry : extract(total) ) { ++thisto.at( entry.second ); }
     127
     128                        for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) {
     129                                // can't be a higher count in one of the sub-histograms than the total
     130                                histos[i].reserve( thisto.size() );
     131
     132                                for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; }
     133                        }
     134
     135                        for ( unsigned i = 0; i < thisto.size(); ++i ) {
     136                                std::cout << "\"" << name << "\"," << i;
     137                                for ( const auto& histo : histos ) {
     138                                        std::cout << "," << histo[i];
     139                                }
     140                                std::cout << "," << thisto[i] << std::endl;
     141                        }
     142                }
     143               
     144        public:
     145                void print() {
     146                        for ( auto& stats : for_linkage ) {
     147                                total += stats;
     148                        }
     149
     150                        std::cout << ",,\"intrinsic\",\"Cforall\",\"C\",\"autogen\",\"builtin\",\"TOTAL\"" << std::endl;
     151
     152                        printAll("mono_decls", [](const Stats& stats) { return stats.mono_decls; });
     153                        printAll("poly_decls", [](const Stats& stats) { return stats.poly_decls; });
     154                        printAll("n_decls", [](const Stats& stats) { return stats.n_decls; });
     155                        printAll("unique_names", [](const Stats& stats) { return stats.by_name.size(); });
     156
     157                        printAllHisto("overloads", [](const Stats& stats) { return stats.by_name; });
     158                        printAllMap("n_params", [](const Stats& stats) { return stats.by_params; });
     159                        printAllMap("n_returns", [](const Stats& stats) { return stats.by_returns; });
     160                }
     161        };
    21162
    22163        void printDeclStats( std::list< Declaration * > &translationUnit ) {
    23                 std::cout << "DeclStats not yet implemented." << std::endl;
     164                DeclStats stats;
     165                acceptAll( translationUnit, stats );
     166                stats.print();
    24167        }
    25168       
  • src/CodeTools/DeclStats.h

    rdc99f38 r7d6639d  
    2121namespace CodeTools {
    2222
     23        /// Prints summary information about a translation unit's function declarations and calls
    2324        void printDeclStats( std::list< Declaration * > &translationUnit );
    2425       
  • src/Common/ScopedMap.h

    rdc99f38 r7d6639d  
    102102                iterator operator-- (int) { iterator tmp = *this; --(*this); return tmp; }
    103103
    104                 bool operator== (const iterator &that) {
     104                bool operator== (const iterator &that) const {
    105105                        return scopes == that.scopes && level == that.level && it == that.it;
    106106                }
    107                 bool operator!= (const iterator &that) { return !( *this == that ); }
     107                bool operator!= (const iterator &that) const { return !( *this == that ); }
    108108
    109109                size_type get_level() const { return level; }
     
    180180                const_iterator operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; }
    181181
    182                 bool operator== (const const_iterator &that) {
     182                bool operator== (const const_iterator &that) const {
    183183                        return scopes == that.scopes && level == that.level && it == that.it;
    184184                }
    185                 bool operator!= (const const_iterator &that) { return !( *this == that ); }
     185                bool operator!= (const const_iterator &that) const { return !( *this == that ); }
    186186
    187187                size_type get_level() const { return level; }
  • src/main.cc

    rdc99f38 r7d6639d  
    214214                } // if
    215215
    216                 if ( declstatsp ) {
    217                         CodeTools::printDeclStats( translationUnit );
    218                         deleteAll( translationUnit );
    219                         return 0;
    220                 }
    221 
    222216                // add the assignment statement after the initialization of a type parameter
    223217                OPTPRINT( "validate" )
     
    251245                        LibCfa::makeLibCfa( translationUnit );
    252246                } // if
     247
     248                if ( declstatsp ) {
     249                        CodeTools::printDeclStats( translationUnit );
     250                        deleteAll( translationUnit );
     251                        return 0;
     252                }
    253253
    254254                if ( bresolvep ) {
Note: See TracChangeset for help on using the changeset viewer.