LLVM API Documentation
00001 //===- GCOVr.cpp - LLVM coverage tool -------------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // GCOV implements the interface to read and write coverage files that use 00011 // 'gcov' format. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Support/GCOV.h" 00016 #include "llvm/ADT/OwningPtr.h" 00017 #include "llvm/ADT/STLExtras.h" 00018 #include "llvm/Support/MemoryObject.h" 00019 #include "llvm/Support/system_error.h" 00020 using namespace llvm; 00021 00022 //===----------------------------------------------------------------------===// 00023 // GCOVFile implementation. 00024 00025 /// ~GCOVFile - Delete GCOVFile and its content. 00026 GCOVFile::~GCOVFile() { 00027 DeleteContainerPointers(Functions); 00028 } 00029 00030 /// isGCDAFile - Return true if Format identifies a .gcda file. 00031 static bool isGCDAFile(GCOV::GCOVFormat Format) { 00032 return Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404; 00033 } 00034 00035 /// isGCNOFile - Return true if Format identifies a .gcno file. 00036 static bool isGCNOFile(GCOV::GCOVFormat Format) { 00037 return Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404; 00038 } 00039 00040 /// read - Read GCOV buffer. 00041 bool GCOVFile::read(GCOVBuffer &Buffer) { 00042 GCOV::GCOVFormat Format = Buffer.readGCOVFormat(); 00043 if (Format == GCOV::InvalidGCOV) 00044 return false; 00045 00046 unsigned i = 0; 00047 while (1) { 00048 GCOVFunction *GFun = NULL; 00049 if (isGCDAFile(Format)) { 00050 // Use existing function while reading .gcda file. 00051 assert(i < Functions.size() && ".gcda data does not match .gcno data"); 00052 GFun = Functions[i]; 00053 } else if (isGCNOFile(Format)){ 00054 GFun = new GCOVFunction(); 00055 Functions.push_back(GFun); 00056 } 00057 if (!GFun || !GFun->read(Buffer, Format)) 00058 break; 00059 ++i; 00060 } 00061 return true; 00062 } 00063 00064 /// dump - Dump GCOVFile content on standard out for debugging purposes. 00065 void GCOVFile::dump() { 00066 for (SmallVector<GCOVFunction *, 16>::iterator I = Functions.begin(), 00067 E = Functions.end(); I != E; ++I) 00068 (*I)->dump(); 00069 } 00070 00071 /// collectLineCounts - Collect line counts. This must be used after 00072 /// reading .gcno and .gcda files. 00073 void GCOVFile::collectLineCounts(FileInfo &FI) { 00074 for (SmallVector<GCOVFunction *, 16>::iterator I = Functions.begin(), 00075 E = Functions.end(); I != E; ++I) 00076 (*I)->collectLineCounts(FI); 00077 FI.print(); 00078 } 00079 00080 //===----------------------------------------------------------------------===// 00081 // GCOVFunction implementation. 00082 00083 /// ~GCOVFunction - Delete GCOVFunction and its content. 00084 GCOVFunction::~GCOVFunction() { 00085 DeleteContainerPointers(Blocks); 00086 } 00087 00088 /// read - Read a aunction from the buffer. Return false if buffer cursor 00089 /// does not point to a function tag. 00090 bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) { 00091 if (!Buff.readFunctionTag()) 00092 return false; 00093 00094 Buff.readInt(); // Function header length 00095 Ident = Buff.readInt(); 00096 Buff.readInt(); // Checksum #1 00097 if (Format != GCOV::GCNO_402) 00098 Buff.readInt(); // Checksum #2 00099 00100 Name = Buff.readString(); 00101 if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404) 00102 Filename = Buff.readString(); 00103 00104 if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) { 00105 Buff.readArcTag(); 00106 uint32_t Count = Buff.readInt() / 2; 00107 for (unsigned i = 0, e = Count; i != e; ++i) { 00108 Blocks[i]->addCount(Buff.readInt64()); 00109 } 00110 return true; 00111 } 00112 00113 LineNumber = Buff.readInt(); 00114 00115 // read blocks. 00116 bool BlockTagFound = Buff.readBlockTag(); 00117 (void)BlockTagFound; 00118 assert(BlockTagFound && "Block Tag not found!"); 00119 uint32_t BlockCount = Buff.readInt(); 00120 for (int i = 0, e = BlockCount; i != e; ++i) { 00121 Buff.readInt(); // Block flags; 00122 Blocks.push_back(new GCOVBlock(i)); 00123 } 00124 00125 // read edges. 00126 while (Buff.readEdgeTag()) { 00127 uint32_t EdgeCount = (Buff.readInt() - 1) / 2; 00128 uint32_t BlockNo = Buff.readInt(); 00129 assert(BlockNo < BlockCount && "Unexpected Block number!"); 00130 for (int i = 0, e = EdgeCount; i != e; ++i) { 00131 Blocks[BlockNo]->addEdge(Buff.readInt()); 00132 Buff.readInt(); // Edge flag 00133 } 00134 } 00135 00136 // read line table. 00137 while (Buff.readLineTag()) { 00138 uint32_t LineTableLength = Buff.readInt(); 00139 uint32_t Size = Buff.getCursor() + LineTableLength*4; 00140 uint32_t BlockNo = Buff.readInt(); 00141 assert(BlockNo < BlockCount && "Unexpected Block number!"); 00142 GCOVBlock *Block = Blocks[BlockNo]; 00143 Buff.readInt(); // flag 00144 while (Buff.getCursor() != (Size - 4)) { 00145 StringRef Filename = Buff.readString(); 00146 if (Buff.getCursor() == (Size - 4)) break; 00147 while (uint32_t L = Buff.readInt()) 00148 Block->addLine(Filename, L); 00149 } 00150 Buff.readInt(); // flag 00151 } 00152 return true; 00153 } 00154 00155 /// dump - Dump GCOVFunction content on standard out for debugging purposes. 00156 void GCOVFunction::dump() { 00157 outs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n"; 00158 for (SmallVector<GCOVBlock *, 16>::iterator I = Blocks.begin(), 00159 E = Blocks.end(); I != E; ++I) 00160 (*I)->dump(); 00161 } 00162 00163 /// collectLineCounts - Collect line counts. This must be used after 00164 /// reading .gcno and .gcda files. 00165 void GCOVFunction::collectLineCounts(FileInfo &FI) { 00166 for (SmallVector<GCOVBlock *, 16>::iterator I = Blocks.begin(), 00167 E = Blocks.end(); I != E; ++I) 00168 (*I)->collectLineCounts(FI); 00169 } 00170 00171 //===----------------------------------------------------------------------===// 00172 // GCOVBlock implementation. 00173 00174 /// ~GCOVBlock - Delete GCOVBlock and its content. 00175 GCOVBlock::~GCOVBlock() { 00176 Edges.clear(); 00177 DeleteContainerSeconds(Lines); 00178 } 00179 00180 void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) { 00181 GCOVLines *&LinesForFile = Lines[Filename]; 00182 if (!LinesForFile) 00183 LinesForFile = new GCOVLines(); 00184 LinesForFile->add(LineNo); 00185 } 00186 00187 /// collectLineCounts - Collect line counts. This must be used after 00188 /// reading .gcno and .gcda files. 00189 void GCOVBlock::collectLineCounts(FileInfo &FI) { 00190 for (StringMap<GCOVLines *>::iterator I = Lines.begin(), 00191 E = Lines.end(); I != E; ++I) 00192 I->second->collectLineCounts(FI, I->first(), Counter); 00193 } 00194 00195 /// dump - Dump GCOVBlock content on standard out for debugging purposes. 00196 void GCOVBlock::dump() { 00197 outs() << "Block : " << Number << " Counter : " << Counter << "\n"; 00198 if (!Edges.empty()) { 00199 outs() << "\tEdges : "; 00200 for (SmallVector<uint32_t, 16>::iterator I = Edges.begin(), E = Edges.end(); 00201 I != E; ++I) 00202 outs() << (*I) << ","; 00203 outs() << "\n"; 00204 } 00205 if (!Lines.empty()) { 00206 outs() << "\tLines : "; 00207 for (StringMap<GCOVLines *>::iterator LI = Lines.begin(), 00208 LE = Lines.end(); LI != LE; ++LI) { 00209 outs() << LI->first() << " -> "; 00210 LI->second->dump(); 00211 outs() << "\n"; 00212 } 00213 } 00214 } 00215 00216 //===----------------------------------------------------------------------===// 00217 // GCOVLines implementation. 00218 00219 /// collectLineCounts - Collect line counts. This must be used after 00220 /// reading .gcno and .gcda files. 00221 void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename, 00222 uint32_t Count) { 00223 for (SmallVector<uint32_t, 16>::iterator I = Lines.begin(), 00224 E = Lines.end(); I != E; ++I) 00225 FI.addLineCount(Filename, *I, Count); 00226 } 00227 00228 /// dump - Dump GCOVLines content on standard out for debugging purposes. 00229 void GCOVLines::dump() { 00230 for (SmallVector<uint32_t, 16>::iterator I = Lines.begin(), 00231 E = Lines.end(); I != E; ++I) 00232 outs() << (*I) << ","; 00233 } 00234 00235 //===----------------------------------------------------------------------===// 00236 // FileInfo implementation. 00237 00238 /// addLineCount - Add line count for the given line number in a file. 00239 void FileInfo::addLineCount(StringRef Filename, uint32_t Line, uint32_t Count) { 00240 if (LineInfo.find(Filename) == LineInfo.end()) { 00241 OwningPtr<MemoryBuffer> Buff; 00242 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) { 00243 errs() << Filename << ": " << ec.message() << "\n"; 00244 return; 00245 } 00246 StringRef AllLines = Buff.take()->getBuffer(); 00247 LineCounts L(AllLines.count('\n')+2); 00248 L[Line-1] = Count; 00249 LineInfo[Filename] = L; 00250 return; 00251 } 00252 LineCounts &L = LineInfo[Filename]; 00253 L[Line-1] = Count; 00254 } 00255 00256 /// print - Print source files with collected line count information. 00257 void FileInfo::print() { 00258 for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end(); 00259 I != E; ++I) { 00260 StringRef Filename = I->first(); 00261 outs() << Filename << "\n"; 00262 LineCounts &L = LineInfo[Filename]; 00263 OwningPtr<MemoryBuffer> Buff; 00264 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) { 00265 errs() << Filename << ": " << ec.message() << "\n"; 00266 return; 00267 } 00268 StringRef AllLines = Buff.take()->getBuffer(); 00269 for (unsigned i = 0, e = L.size(); i != e; ++i) { 00270 if (L[i]) 00271 outs() << L[i] << ":\t"; 00272 else 00273 outs() << " :\t"; 00274 std::pair<StringRef, StringRef> P = AllLines.split('\n'); 00275 if (AllLines != P.first) 00276 outs() << P.first; 00277 outs() << "\n"; 00278 AllLines = P.second; 00279 } 00280 } 00281 } 00282 00283