Home | History | Annotate | Download | only in libcodecoverage
      1 /*
      2  * Copyright 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef __VTS_SYSFUZZER_LIBMEASUREMENT_GCDA_PARSER_H__
     18 #define __VTS_SYSFUZZER_LIBMEASUREMENT_GCDA_PARSER_H__
     19 
     20 #include <iostream>
     21 #include <string>
     22 #include <vector>
     23 
     24 #include "GcdaFile.h"
     25 
     26 using namespace std;
     27 
     28 namespace android {
     29 namespace vts {
     30 
     31 // Parses a GCDA file and extracts raw coverage info.
     32 class GcdaRawCoverageParser {
     33  public:
     34   GcdaRawCoverageParser(const char* filename)
     35     : filename_(filename),
     36       gcda_file_(new GcdaFile(filename_)) {}
     37 
     38   virtual ~GcdaRawCoverageParser() {}
     39 
     40   // Parses a given file and returns a vector which contains IDs of raw
     41   // coverage measurement units (e.g., basic blocks).
     42   vector<unsigned> Parse();
     43 
     44  protected:
     45   // Parses the GCOV magic number.
     46   bool ParseMagic();
     47 
     48   // Parses the GCOV file body.
     49   void ParseBody();
     50 
     51   // Processes tag for functions.
     52   void TagFunction(unsigned /*tag*/, unsigned length) {
     53     /* unsigned long pos = */ gcda_file_->Position();
     54 
     55     if (length) {
     56       gcda_file_->ReadUnsigned();  // ident %u
     57       unsigned lineno_checksum = gcda_file_->ReadUnsigned();
     58       result.push_back(lineno_checksum);
     59       gcda_file_->ReadUnsigned();  // cfg_checksum 0x%08x
     60     }
     61   }
     62 
     63   // Processes tag for blocks.
     64   void TagBlocks(unsigned /*tag*/, unsigned length) {
     65     unsigned n_blocks = GCOV_TAG_BLOCKS_NUM(length);
     66     cout << __func__ << ": " << n_blocks << " blocks" << endl;
     67   }
     68 
     69   // Processes tag for arcs.
     70   void TagArcs(unsigned /*tag*/, unsigned length) {
     71     unsigned n_arcs = GCOV_TAG_ARCS_NUM(length);
     72     cout << __func__ << ": " << n_arcs << " arcs" << endl;
     73   }
     74 
     75   // Processes tag for lines.
     76   void TagLines(unsigned /*tag*/, unsigned /*length*/) {
     77     cout << __func__ << endl;
     78   }
     79 
     80  private:
     81   // the name of a target file.
     82   const string filename_;
     83 
     84   // global GcovFile data structure.
     85   GcdaFile* gcda_file_;
     86 
     87   // vector containing the parsed, raw coverage data.
     88   vector<unsigned> result;
     89 };
     90 
     91 }  // namespace vts
     92 }  // namespace android
     93 
     94 #endif
     95