[llvm] 7f7cb79 - [llvm-cov gcov] Don't suppress .gcov output if .gcda is corrupted

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 16 14:55:47 PDT 2020


Author: Fangrui Song
Date: 2020-06-16T14:55:38-07:00
New Revision: 7f7cb79b57838155a2faf46300491d45eedf8907

URL: https://github.com/llvm/llvm-project/commit/7f7cb79b57838155a2faf46300491d45eedf8907
DIFF: https://github.com/llvm/llvm-project/commit/7f7cb79b57838155a2faf46300491d45eedf8907.diff

LOG: [llvm-cov gcov] Don't suppress .gcov output if .gcda is corrupted

If .gcda is corrupted, gcov continues to produce a .gcov and just
assumes execution counts are zeros. This is reasonable, because the
program can corrupt its .gcda output. The code path should be similar to
the code path without .gcda.

Added: 
    

Modified: 
    llvm/include/llvm/ProfileData/GCOV.h
    llvm/lib/ProfileData/GCOV.cpp
    llvm/test/tools/llvm-cov/llvm-cov.test
    llvm/tools/llvm-cov/gcov.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ProfileData/GCOV.h b/llvm/include/llvm/ProfileData/GCOV.h
index 58c454cbba80..7b9ba4410b65 100644
--- a/llvm/include/llvm/ProfileData/GCOV.h
+++ b/llvm/include/llvm/ProfileData/GCOV.h
@@ -98,7 +98,6 @@ class GCOVBuffer {
     } else if (magic == "adcg") {
       de = DataExtractor(buf.substr(4), true, 0);
     } else {
-      errs() << "unexpected file type: " << magic << "\n";
       return false;
     }
     return true;

diff  --git a/llvm/lib/ProfileData/GCOV.cpp b/llvm/lib/ProfileData/GCOV.cpp
index 9527993536ab..35d40db11716 100644
--- a/llvm/lib/ProfileData/GCOV.cpp
+++ b/llvm/lib/ProfileData/GCOV.cpp
@@ -742,10 +742,10 @@ void FileInfo::print(raw_ostream &InfoOS, StringRef MainFilename,
            << f->Name << '\n';
       const LineData &line = LineInfo[source.filename];
       for (uint32_t lineNum = 0; lineNum != line.LastLine; ++lineNum) {
-        BlockLines::const_iterator blocksIt = line.Blocks.find(lineNum);
-        if (blocksIt == line.Blocks.end())
+        BlockLines::const_iterator BlocksIt = line.Blocks.find(lineNum);
+        if (BlocksIt == line.Blocks.end())
           continue;
-        const BlockVector &blocks = blocksIt->second;
+        const BlockVector &blocks = BlocksIt->second;
         // GCC 8 (r254259) added third third field for Ada:
         // lcount:<line>,<count>,<has_unexecuted_blocks>
         // We don't need the third field.

diff  --git a/llvm/test/tools/llvm-cov/llvm-cov.test b/llvm/test/tools/llvm-cov/llvm-cov.test
index 4e9ef083c611..127e9b3b693f 100644
--- a/llvm/test/tools/llvm-cov/llvm-cov.test
+++ b/llvm/test/tools/llvm-cov/llvm-cov.test
@@ -169,11 +169,17 @@ NO-GCDA-NEXT:  Creating 'test.h.gcov'
 # Invalid gcno file.
 RUN: llvm-cov gcov test.c -gcno=test_read_fail.gcno
 
+# Not a .gcda file. Error but keep the .gcov output.
+RUN: echo invalid > not.gcda
+RUN: llvm-cov gcov test.c -gcda=not.gcda 2> %t.err | FileCheck %s --check-prefix=NO-GCDA
+RUN: FileCheck %s --check-prefix=NOT-GCDA < %t.err
+NOT-GCDA: not.gcda:not a gcov data file
+
 # Bad file checksum on gcda.
-RUN: llvm-cov gcov test.c -gcda=test_file_checksum_fail.gcda
+RUN: llvm-cov gcov test.c -gcda=test_file_checksum_fail.gcda 2> %t.err | FileCheck %s --check-prefix=NO-GCDA
 
 # Bad function checksum on gcda
-RUN: llvm-cov gcov test.c -gcda=test_func_checksum_fail.gcda
+RUN: llvm-cov gcov test.c -gcda=test_func_checksum_fail.gcda 2> %t.err | FileCheck %s --check-prefix=NO-GCDA
 
 # Has arcs from exit blocks
 RUN-DISABLED: llvm-cov gcov test_exit_block_arcs.c 2>&1 | FileCheck %s -check-prefix=EXIT_BLOCK_ARCS

diff  --git a/llvm/tools/llvm-cov/gcov.cpp b/llvm/tools/llvm-cov/gcov.cpp
index 4359ff466af0..7a1dbbfe9338 100644
--- a/llvm/tools/llvm-cov/gcov.cpp
+++ b/llvm/tools/llvm-cov/gcov.cpp
@@ -65,11 +65,11 @@ static void reportCoverage(StringRef SourceFile, StringRef ObjectDir,
     // Clear the filename to make it clear we didn't read anything.
     GCDA = "-";
   } else {
-    GCOVBuffer GCDA_GB(GCDA_Buff.get().get());
-    if (!GF.readGCDA(GCDA_GB)) {
+    GCOVBuffer gcda_buf(GCDA_Buff.get().get());
+    if (!gcda_buf.readGCDAFormat())
+      errs() << GCDA << ":not a gcov data file\n";
+    else if (!GF.readGCDA(gcda_buf))
       errs() << "Invalid .gcda File!\n";
-      return;
-    }
   }
 
   if (DumpGCOV)


        


More information about the llvm-commits mailing list