<commit_before>//===- SourceCoverageView.cpp - Code coverage view for source code --------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file This class implements rendering for code coverage of source code.
///
//===----------------------------------------------------------------------===//

#include "SourceCoverageView.h"
#include "SourceCoverageViewText.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/LineIterator.h"
#include "llvm/Support/Path.h"

using namespace llvm;

std::string SourceCoverageView::formatCount(uint64_t N) {
  std::string Number = utostr(N);
  int Len = Number.size();
  if (Len <= 3)
    return Number;
  int IntLen = Len % 3 == 0 ? 3 : Len % 3;
  std::string Result(Number.data(), IntLen);
  if (IntLen != 3) {
    Result.push_back('.');
    Result += Number.substr(IntLen, 3 - IntLen);
  }
  Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
  return Result;
}

void SourceCoverageView::StreamDestructor::operator()(raw_ostream *OS) const {
  if (OS == &outs())
    return;
  delete OS;
}

/// \brief Create a file at ``Dir/ToplevelDir/@Path.Extension``. If
/// \p ToplevelDir is empty, its path component is skipped.
static Expected<SourceCoverageView::OwnedStream>
createFileInDirectory(StringRef Dir, StringRef ToplevelDir, StringRef Path,
                      StringRef Extension) {
  assert(Extension.size() && "The file extension may not be empty");

  SmallString<256> FullPath(Dir);
  if (!ToplevelDir.empty())
    sys::path::append(FullPath, ToplevelDir);

  auto PathBaseDir = sys::path::relative_path(sys::path::parent_path(Path));
  sys::path::append(FullPath, PathBaseDir);

  if (auto E = sys::fs::create_directories(FullPath))
    return errorCodeToError(E);

  auto PathFilename = (sys::path::filename(Path) + "." + Extension).str();
  sys::path::append(FullPath, PathFilename);

  std::error_code E;
  auto OS = SourceCoverageView::OwnedStream(
      new raw_fd_ostream(FullPath, E, sys::fs::F_RW));
  if (E)
    return errorCodeToError(E);
  return std::move(OS);
}

Expected<SourceCoverageView::OwnedStream>
SourceCoverageView::createOutputStream(const CoverageViewOptions &Opts,
                                       StringRef Path, StringRef Extension,
                                       bool InToplevel) {
  if (Opts.ShowOutputDirectory == "")
    return OwnedStream(&outs());

  return createFileInDirectory(Opts.ShowOutputDirectory,
                               InToplevel ? "" : "coverage", Path, Extension);
}

void SourceCoverageView::addExpansion(
    const coverage::CounterMappingRegion &Region,
    std::unique_ptr<SourceCoverageView> View) {
  ExpansionSubViews.emplace_back(Region, std::move(View));
}

void SourceCoverageView::addInstantiation(
    StringRef FunctionName, unsigned Line,
    std::unique_ptr<SourceCoverageView> View) {
  InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
}

std::unique_ptr<SourceCoverageView>
SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
                           const CoverageViewOptions &Options,
                           coverage::CoverageData &&CoverageInfo) {
  switch (Options.ShowFormat) {
  case CoverageViewOptions::OutputFormat::Text:
    return llvm::make_unique<SourceCoverageViewText>(SourceName, File, Options,
                                                     std::move(CoverageInfo));
  }
}

void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
                               bool ShowSourceName, unsigned ViewDepth) {
  if (ShowSourceName)
    renderSourceName(OS);

  // We need the expansions and instantiations sorted so we can go through them
  // while we iterate lines.
  std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
  std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
  auto NextESV = ExpansionSubViews.begin();
  auto EndESV = ExpansionSubViews.end();
  auto NextISV = InstantiationSubViews.begin();
  auto EndISV = InstantiationSubViews.end();
<commit_msg>Fix "not all control paths return a value" warning on MSVC<commit_after>//===- SourceCoverageView.cpp - Code coverage view for source code --------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file This class implements rendering for code coverage of source code.
///
//===----------------------------------------------------------------------===//

#include "SourceCoverageView.h"
#include "SourceCoverageViewText.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/LineIterator.h"
#include "llvm/Support/Path.h"

using namespace llvm;

std::string SourceCoverageView::formatCount(uint64_t N) {
  std::string Number = utostr(N);
  int Len = Number.size();
  if (Len <= 3)
    return Number;
  int IntLen = Len % 3 == 0 ? 3 : Len % 3;
  std::string Result(Number.data(), IntLen);
  if (IntLen != 3) {
    Result.push_back('.');
    Result += Number.substr(IntLen, 3 - IntLen);
  }
  Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
  return Result;
}

void SourceCoverageView::StreamDestructor::operator()(raw_ostream *OS) const {
  if (OS == &outs())
    return;
  delete OS;
}

/// \brief Create a file at ``Dir/ToplevelDir/@Path.Extension``. If
/// \p ToplevelDir is empty, its path component is skipped.
static Expected<SourceCoverageView::OwnedStream>
createFileInDirectory(StringRef Dir, StringRef ToplevelDir, StringRef Path,
                      StringRef Extension) {
  assert(Extension.size() && "The file extension may not be empty");

  SmallString<256> FullPath(Dir);
  if (!ToplevelDir.empty())
    sys::path::append(FullPath, ToplevelDir);

  auto PathBaseDir = sys::path::relative_path(sys::path::parent_path(Path));
  sys::path::append(FullPath, PathBaseDir);

  if (auto E = sys::fs::create_directories(FullPath))
    return errorCodeToError(E);

  auto PathFilename = (sys::path::filename(Path) + "." + Extension).str();
  sys::path::append(FullPath, PathFilename);

  std::error_code E;
  auto OS = SourceCoverageView::OwnedStream(
      new raw_fd_ostream(FullPath, E, sys::fs::F_RW));
  if (E)
    return errorCodeToError(E);
  return std::move(OS);
}

Expected<SourceCoverageView::OwnedStream>
SourceCoverageView::createOutputStream(const CoverageViewOptions &Opts,
                                       StringRef Path, StringRef Extension,
                                       bool InToplevel) {
  if (Opts.ShowOutputDirectory == "")
    return OwnedStream(&outs());

  return createFileInDirectory(Opts.ShowOutputDirectory,
                               InToplevel ? "" : "coverage", Path, Extension);
}

void SourceCoverageView::addExpansion(
    const coverage::CounterMappingRegion &Region,
    std::unique_ptr<SourceCoverageView> View) {
  ExpansionSubViews.emplace_back(Region, std::move(View));
}

void SourceCoverageView::addInstantiation(
    StringRef FunctionName, unsigned Line,
    std::unique_ptr<SourceCoverageView> View) {
  InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
}

std::unique_ptr<SourceCoverageView>
SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
                           const CoverageViewOptions &Options,
                           coverage::CoverageData &&CoverageInfo) {
  switch (Options.ShowFormat) {
  case CoverageViewOptions::OutputFormat::Text:
    return llvm::make_unique<SourceCoverageViewText>(SourceName, File, Options,
                                                     std::move(CoverageInfo));
  }
  llvm_unreachable("Unknown coverage output format!");
}

void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
                               bool ShowSourceName, unsigned ViewDepth) {
  if (ShowSourceName)
    renderSourceName(OS);

  // We need the expansions and instantiations sorted so we can go through them
  // while we iterate lines.
  std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
  std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
  auto NextESV = ExpansionSubViews.begin();
  auto EndESV = ExpansionSubViews.end();
  auto NextISV = InstantiationSubViews.begin();
  auto EndISV = InstantiationSubViews.end();

{'fraction_non_alphanumeric': 0.15721826945145742, 'fraction_numerical': 0.003213220105577232, 'mean_word_length': 3.132290184921764, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4807986', 'n_tokens_mistral': 2595, 'n_tokens_neox': 2492, 'n_words': 578}