<commit_before>/*
 Copyright 2019 <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

/*
 * File:   ParseCache.cpp
 * Author: alain
 *
 * Created on April 29, 2017, 4:20 PM
 */

#include <Surelog/Cache/ParseCache.h>
#include <Surelog/Cache/parser_generated.h>
#include <Surelog/CommandLine/CommandLineParser.h>
#include <Surelog/Design/Design.h>
#include <Surelog/Design/DesignElement.h>
#include <Surelog/Design/FileContent.h>
#include <Surelog/Library/Library.h>
#include <Surelog/Package/Precompiled.h>
#include <Surelog/SourceCompile/CompileSourceFile.h>
#include <Surelog/SourceCompile/Compiler.h>
#include <Surelog/SourceCompile/ParseFile.h>
#include <Surelog/SourceCompile/SymbolTable.h>
#include <Surelog/Utils/FileUtils.h>

namespace SURELOG {
namespace fs = std::filesystem;

ParseCache::ParseCache(ParseFile* parser)
    : m_parse(parser), m_isPrecompiled(false) {}

static constexpr char FlbSchemaVersion[] = "1.0";

fs::path ParseCache::getCacheFileName_(const fs::path& svFileNameIn) {
  fs::path svFileName = svFileNameIn;
  CommandLineParser* clp =
      m_parse->getCompileSourceFile()->getCommandLineParser();
  Precompiled* prec = Precompiled::getSingleton();
  SymbolId cacheDirId = clp->getCacheDir();
  if (svFileName.empty()) svFileName = m_parse->getPpFileName();
  fs::path baseFileName = FileUtils::basename(svFileName);
  if (prec->isFilePrecompiled(baseFileName)) {
    fs::path packageRepDir = m_parse->getSymbol(clp->getPrecompiledDir());
    cacheDirId =
        clp->mutableSymbolTable()->registerSymbol(packageRepDir.string());
    m_isPrecompiled = true;
    svFileName = baseFileName;
  } else {
    if (clp->noCacheHash()) {
      svFileName = baseFileName;
    } else {
      svFileName = svFileName.parent_path().filename() / baseFileName;
    }
  }

  fs::path cacheDirName = m_parse->getSymbol(cacheDirId);
  Library* lib = m_parse->getLibrary();
  std::string libName = lib->getName();
  fs::path cacheFileName =
      cacheDirName / libName / (svFileName.string() + ".slpa");
  FileUtils::mkDirs(cacheDirName / libName);
  return cacheFileName;
}

bool ParseCache::restore_(const fs::path& cacheFileName) {
  uint8_t* buffer_pointer = openFlatBuffers(cacheFileName);<commit_msg>Make Parse Cache write Slpa to correct subfolder<commit_after>/*
 Copyright 2019 <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

/*
 * File:   ParseCache.cpp
 * Author: alain
 *
 * Created on April 29, 2017, 4:20 PM
 */

#include <Surelog/Cache/ParseCache.h>
#include <Surelog/Cache/parser_generated.h>
#include <Surelog/CommandLine/CommandLineParser.h>
#include <Surelog/Design/Design.h>
#include <Surelog/Design/DesignElement.h>
#include <Surelog/Design/FileContent.h>
#include <Surelog/Library/Library.h>
#include <Surelog/Package/Precompiled.h>
#include <Surelog/SourceCompile/CompileSourceFile.h>
#include <Surelog/SourceCompile/Compiler.h>
#include <Surelog/SourceCompile/ParseFile.h>
#include <Surelog/SourceCompile/SymbolTable.h>
#include <Surelog/Utils/FileUtils.h>

namespace SURELOG {
namespace fs = std::filesystem;

ParseCache::ParseCache(ParseFile* parser)
    : m_parse(parser), m_isPrecompiled(false) {}

static constexpr char FlbSchemaVersion[] = "1.0";

fs::path ParseCache::getCacheFileName_(const fs::path& svFileNameIn) {
  fs::path svFileName = svFileNameIn;
  CommandLineParser* clp =
      m_parse->getCompileSourceFile()->getCommandLineParser();
  Precompiled* prec = Precompiled::getSingleton();
  SymbolId cacheDirId = clp->getCacheDir();
  if (svFileName.empty()) svFileName = m_parse->getPpFileName();
  fs::path baseFileName = FileUtils::basename(svFileName);
  fs::path cacheFileName;
  if (prec->isFilePrecompiled(baseFileName)) {
    fs::path packageRepDir = m_parse->getSymbol(clp->getPrecompiledDir());
    cacheDirId =
        clp->mutableSymbolTable()->registerSymbol(packageRepDir.string());
    m_isPrecompiled = true;
    svFileName = baseFileName;
  } else {
    if (clp->noCacheHash()) {
      cacheFileName = svFileName.string() + ".slpa";
    } else {
      svFileName = svFileName.parent_path().filename() / baseFileName;
    }
  }

  fs::path cacheDirName = m_parse->getSymbol(cacheDirId);
  Library* lib = m_parse->getLibrary();
  std::string libName = lib->getName();
  if (cacheFileName.empty()) {
    cacheFileName = cacheDirName / libName / (svFileName.string() + ".slpa");
  }

  FileUtils::mkDirs(cacheDirName / libName);
  return cacheFileName;
}

bool ParseCache::restore_(const fs::path& cacheFileName) {
  uint8_t* buffer_pointer = openFlatBuffers(cacheFileName);
{'fraction_non_alphanumeric': 0.12074583635047068, 'fraction_numerical': 0.00724112961622013, 'mean_word_length': 4.985915492957746, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4406623', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1660, 'n_words': 414}