]> granicus.if.org Git - llvm/commitdiff
[llvm-cov] Get rid of all invalid filename references
authorVedant Kumar <vsk@apple.com>
Fri, 23 Sep 2016 18:57:32 +0000 (18:57 +0000)
committerVedant Kumar <vsk@apple.com>
Fri, 23 Sep 2016 18:57:32 +0000 (18:57 +0000)
We used to append filenames into a vector of std::string, and then
append a reference to each string into a separate vector. This made it
easier to work with the getUniqueSourceFiles API. But it's buggy.

std::string has a small-string optimization, so you can't expect to
capture a reference to one if you're copying it into a growing vector.
Add a test that triggers this invalid reference to std::string scenario,
and kill the issue with fire by just using ArrayRef<std::string>
everywhere.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@282281 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/llvm-cov/scan-directory.test
tools/llvm-cov/CodeCoverage.cpp
tools/llvm-cov/CoverageExporterJson.cpp
tools/llvm-cov/CoverageReport.cpp
tools/llvm-cov/CoverageReport.h
tools/llvm-cov/SourceCoverageView.h
tools/llvm-cov/SourceCoverageViewHTML.cpp
tools/llvm-cov/SourceCoverageViewHTML.h
tools/llvm-cov/SourceCoverageViewText.cpp
tools/llvm-cov/SourceCoverageViewText.h

index 9bacfc209ddb13c056b074cde52d133aa04d735f..b7f2858426780f50177808a30526e4d8537cfe74 100644 (file)
@@ -2,8 +2,18 @@ RUN: mkdir -p %t/a/b/
 RUN: echo "" > %t/a/b/c.tmp
 RUN: echo "" > %t/a/d.tmp
 
-RUN: llvm-cov show /dev/null -instr-profile /dev/null -dump-collected-paths %t | FileCheck %s
-RUN: llvm-cov show /dev/null -instr-profile /dev/null -dump-collected-paths %t/a/b/c.tmp %t/a/d.tmp | FileCheck %s
+RUN: llvm-cov show /dev/null -instr-profile /dev/null -dump-collected-paths %t | FileCheck %s --check-prefix=REAL
+RUN: llvm-cov show /dev/null -instr-profile /dev/null -dump-collected-paths %t/a/b/c.tmp %t/a/d.tmp | FileCheck %s --check-prefix=REAL
 
-CHECK-DAG: {{.*}}c.tmp
-CHECK-DAG: {{.*}}d.tmp
+REAL-DAG: {{.*}}c.tmp
+REAL-DAG: {{.*}}d.tmp
+
+RUN: llvm-cov show /dev/null -instr-profile /dev/null -dump-collected-paths -filename-equivalence %t a b c d e f | FileCheck %s --check-prefix=EQUIV
+EQUIV-DAG: {{.*}}c.tmp
+EQUIV-DAG: {{.*}}d.tmp
+EQUIV-DAG: a
+EQUIV-DAG: b
+EQUIV-DAG: c
+EQUIV-DAG: d
+EQUIV-DAG: e
+EQUIV-DAG: f
index 237d877cde167c4b8a545106d68b191b2dd76468..51ac6ef3d8847ba563eb0b8de100b98a6d9f7327 100644 (file)
@@ -64,7 +64,8 @@ private:
   /// \brief Print the warning message to the error output stream.
   void warning(const Twine &Message, StringRef Whence = "");
 
-  /// \brief Copy \p Path into the list of input source files.
+  /// \brief Convert \p Path into an absolute path and append it to the list
+  /// of collected paths.
   void addCollectedPath(const std::string &Path);
 
   /// \brief If \p Path is a regular file, collect the path. If it's a
@@ -116,7 +117,7 @@ private:
   std::string PGOFilename;
 
   /// A list of input source files.
-  std::vector<StringRef> SourceFiles;
+  std::vector<std::string> SourceFiles;
 
   /// Whether or not we're in -filename-equivalence mode.
   bool CompareFilenamesOnly;
@@ -131,9 +132,6 @@ private:
   /// A cache for demangled symbol names.
   StringMap<std::string> DemangledNames;
 
-  /// File paths (absolute, or otherwise) to input source files.
-  std::vector<std::string> CollectedPaths;
-
   /// Errors and warnings which have not been printed.
   std::mutex ErrsLock;
 
@@ -168,7 +166,7 @@ void CodeCoverageTool::warning(const Twine &Message, StringRef Whence) {
 
 void CodeCoverageTool::addCollectedPath(const std::string &Path) {
   if (CompareFilenamesOnly) {
-    CollectedPaths.push_back(Path);
+    SourceFiles.emplace_back(Path);
   } else {
     SmallString<128> EffectivePath(Path);
     if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) {
@@ -176,10 +174,8 @@ void CodeCoverageTool::addCollectedPath(const std::string &Path) {
       return;
     }
     sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true);
-    CollectedPaths.push_back(EffectivePath.str());
+    SourceFiles.emplace_back(EffectivePath.str());
   }
-
-  SourceFiles.emplace_back(CollectedPaths.back());
 }
 
 void CodeCoverageTool::collectPaths(const std::string &Path) {
@@ -597,7 +593,7 @@ int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
       collectPaths(File);
 
     if (DebugDumpCollectedPaths) {
-      for (StringRef SF : SourceFiles)
+      for (const std::string &SF : SourceFiles)
         outs() << SF << '\n';
       ::exit(0);
     }
@@ -751,11 +747,11 @@ int CodeCoverageTool::show(int argc, const char **argv,
     ThreadCount = std::thread::hardware_concurrency();
   ThreadPool Pool(ThreadCount);
 
-  for (StringRef SourceFile : SourceFiles) {
-    Pool.async([this, SourceFile, &Coverage, &Printer, ShowFilenames] {
+  for (const std::string &SourceFile : SourceFiles) {
+    Pool.async([this, &SourceFile, &Coverage, &Printer, ShowFilenames] {
       auto View = createSourceFileView(SourceFile, *Coverage);
       if (!View) {
-        warning("The file '" + SourceFile.str() + "' isn't covered.");
+        warning("The file '" + SourceFile + "' isn't covered.");
         return;
       }
 
index e8dee147c00045c7965e9f8bcd954af93f97f8a3..06dc176737a35c4bd8d3a46248f5c64ac71de908 100644 (file)
@@ -175,7 +175,9 @@ class CoverageExporterJson {
     emitDictKey("files");
 
     FileCoverageSummary Totals = FileCoverageSummary("Totals");
-    std::vector<StringRef> SourceFiles = Coverage.getUniqueSourceFiles();
+    std::vector<std::string> SourceFiles;
+    for (StringRef SF : Coverage.getUniqueSourceFiles())
+      SourceFiles.emplace_back(SF);
     auto FileReports =
         CoverageReport::prepareFileReports(Coverage, Totals, SourceFiles);
     renderFiles(SourceFiles, FileReports);
@@ -235,7 +237,7 @@ class CoverageExporterJson {
   }
 
   /// \brief Render an array of all the source files, also pass back a Summary.
-  void renderFiles(ArrayRef<StringRef> SourceFiles,
+  void renderFiles(ArrayRef<std::string> SourceFiles,
                    ArrayRef<FileCoverageSummary> FileReports) {
     // Start List of Files.
     emitArrayStart();
index 0d34722eae5890d9ab79828d2521e8b4e973b56b..24d7c661aadcc9d57e2a4237669a17b7427f2496 100644 (file)
@@ -120,7 +120,7 @@ raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
 
 /// \brief Determine the length of the longest common prefix of the strings in
 /// \p Strings.
-unsigned getLongestCommonPrefixLen(ArrayRef<StringRef> Strings) {
+unsigned getLongestCommonPrefixLen(ArrayRef<std::string> Strings) {
   unsigned LCP = Strings[0].size();
   for (unsigned I = 1, E = Strings.size(); LCP > 0 && I < E; ++I) {
     auto Mismatch =
@@ -215,7 +215,7 @@ void CoverageReport::render(const FunctionCoverageSummary &Function,
   OS << "\n";
 }
 
-void CoverageReport::renderFunctionReports(ArrayRef<StringRef> Files,
+void CoverageReport::renderFunctionReports(ArrayRef<std::string> Files,
                                            raw_ostream &OS) {
   bool isFirst = true;
   for (StringRef Filename : Files) {
@@ -261,7 +261,7 @@ void CoverageReport::renderFunctionReports(ArrayRef<StringRef> Files,
 std::vector<FileCoverageSummary>
 CoverageReport::prepareFileReports(const coverage::CoverageMapping &Coverage,
                                    FileCoverageSummary &Totals,
-                                   ArrayRef<StringRef> Files) {
+                                   ArrayRef<std::string> Files) {
   std::vector<FileCoverageSummary> FileReports;
   unsigned LCP = 0;
   if (Files.size() > 1)
@@ -298,12 +298,14 @@ CoverageReport::prepareFileReports(const coverage::CoverageMapping &Coverage,
 }
 
 void CoverageReport::renderFileReports(raw_ostream &OS) const {
-  std::vector<StringRef> UniqueSourceFiles = Coverage.getUniqueSourceFiles();
+  std::vector<std::string> UniqueSourceFiles;
+  for (StringRef SF : Coverage.getUniqueSourceFiles())
+    UniqueSourceFiles.emplace_back(SF.str());
   renderFileReports(OS, UniqueSourceFiles);
 }
 
 void CoverageReport::renderFileReports(raw_ostream &OS,
-                                       ArrayRef<StringRef> Files) const {
+                                       ArrayRef<std::string> Files) const {
   FileCoverageSummary Totals("TOTAL");
   auto FileReports = prepareFileReports(Coverage, Totals, Files);
 
index 37bb842f13d807a8c7ee8fd77fc5aa37e197845c..7a416497e258eda35296619299ebf62a54815de4 100644 (file)
@@ -32,18 +32,18 @@ public:
                  const coverage::CoverageMapping &Coverage)
       : Options(Options), Coverage(Coverage) {}
 
-  void renderFunctionReports(ArrayRef<StringRef> Files, raw_ostream &OS);
+  void renderFunctionReports(ArrayRef<std::string> Files, raw_ostream &OS);
 
   /// Prepare file reports for the files specified in \p Files.
   static std::vector<FileCoverageSummary>
   prepareFileReports(const coverage::CoverageMapping &Coverage,
-                     FileCoverageSummary &Totals, ArrayRef<StringRef> Files);
+                     FileCoverageSummary &Totals, ArrayRef<std::string> Files);
 
   /// Render file reports for every unique file in the coverage mapping.
   void renderFileReports(raw_ostream &OS) const;
 
   /// Render file reports for the files specified in \p Files.
-  void renderFileReports(raw_ostream &OS, ArrayRef<StringRef> Files) const;
+  void renderFileReports(raw_ostream &OS, ArrayRef<std::string> Files) const;
 };
 
 } // end namespace llvm
index 129c5902070e5fedd901dba4484e85f9eac56f15..2069fe56d8c4a2f11fced65cf3d644d4ad339b5f 100644 (file)
@@ -142,7 +142,7 @@ public:
   virtual void closeViewFile(OwnedStream OS) = 0;
 
   /// \brief Create an index which lists reports for the given source files.
-  virtual Error createIndexFile(ArrayRef<StringRef> SourceFiles,
+  virtual Error createIndexFile(ArrayRef<std::string> SourceFiles,
                                 const coverage::CoverageMapping &Coverage) = 0;
 
   /// @}
index 1cb283cdc1dcc2527eb535e76ec12b503742a0bc..2707260b8add68e4d24235013bc0efe6e7d96ed3 100644 (file)
@@ -347,7 +347,7 @@ void CoveragePrinterHTML::emitFileSummary(raw_ostream &OS, StringRef SF,
 }
 
 Error CoveragePrinterHTML::createIndexFile(
-    ArrayRef<StringRef> SourceFiles,
+    ArrayRef<std::string> SourceFiles,
     const coverage::CoverageMapping &Coverage) {
   // Emit the default stylesheet.
   auto CSSOrErr = createOutputStream("style", "css", /*InToplevel=*/true);
index ad4b2b8ab06647327e66c40fa50451da99022d53..94b08a5e7fc4ea3782dd3b5c3475ca6062f25c72 100644 (file)
@@ -28,7 +28,7 @@ public:
 
   void closeViewFile(OwnedStream OS) override;
 
-  Error createIndexFile(ArrayRef<StringRef> SourceFiles,
+  Error createIndexFile(ArrayRef<std::string> SourceFiles,
                         const coverage::CoverageMapping &Coverage) override;
 
   CoveragePrinterHTML(const CoverageViewOptions &Opts)
index f5201d9c808e61a5a899e3dab74648e6578829e7..68891298a07b69bf7cdc23ed5d74ac215ff0cdcb 100644 (file)
@@ -29,7 +29,7 @@ void CoveragePrinterText::closeViewFile(OwnedStream OS) {
 }
 
 Error CoveragePrinterText::createIndexFile(
-    ArrayRef<StringRef> SourceFiles,
+    ArrayRef<std::string> SourceFiles,
     const coverage::CoverageMapping &Coverage) {
   auto OSOrErr = createOutputStream("index", "txt", /*InToplevel=*/true);
   if (Error E = OSOrErr.takeError())
@@ -38,7 +38,7 @@ Error CoveragePrinterText::createIndexFile(
   raw_ostream &OSRef = *OS.get();
 
   CoverageReport Report(Opts, Coverage);
-  Report.renderFileReports(OSRef);
+  Report.renderFileReports(OSRef, SourceFiles);
 
   Opts.colored_ostream(OSRef, raw_ostream::CYAN) << "\n"
                                                  << Opts.getLLVMVersionString();
index 3968aa70dd51271f79d22b56375677d3454550e3..c3f20de92978f898278fdb6bc04dfb78e573a727 100644 (file)
@@ -26,7 +26,7 @@ public:
 
   void closeViewFile(OwnedStream OS) override;
 
-  Error createIndexFile(ArrayRef<StringRef> SourceFiles,
+  Error createIndexFile(ArrayRef<std::string> SourceFiles,
                         const coverage::CoverageMapping &Coverage) override;
 
   CoveragePrinterText(const CoverageViewOptions &Opts)